blob: fb7aeddd49abde18feb996d303ab3dec9949815b [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 Moolenaar7d131b02020-05-08 19:10:34 +02004303 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004304 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004305 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004306 break;
4307 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004308 tv1->vval.v_number = res;
4309 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004310 }
4311 else
4312 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004313 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004314 generate_two_op(cctx, op);
4315 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 }
4317
4318 return OK;
4319}
4320
4321/*
4322 * + number addition
4323 * - number subtraction
4324 * .. string concatenation
4325 */
4326 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004327compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328{
4329 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004330 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004332 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004333
4334 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004335 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336 return FAIL;
4337
4338 /*
4339 * Repeat computing, until no "+", "-" or ".." is following.
4340 */
4341 for (;;)
4342 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004343 op = may_peek_next_line(cctx, *arg, &next);
4344 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345 break;
4346 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004347 if (next != NULL)
4348 {
4349 *arg = next_line_from_context(cctx, TRUE);
4350 op = skipwhite(*arg);
4351 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004352
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004353 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004354 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004355 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004356 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357 }
4358
Bram Moolenaare0de1712020-12-02 17:36:54 +01004359 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004360 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004361
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004362 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004363 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004364 return FAIL;
4365
Bram Moolenaara5565e42020-05-09 15:44:01 +02004366 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004367 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004368 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4369 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4370 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4371 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004373 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4374 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004375
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004376 // concat/subtract/add constant numbers
4377 if (*op == '+')
4378 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4379 else if (*op == '-')
4380 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4381 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004382 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004383 // concatenate constant strings
4384 char_u *s1 = tv1->vval.v_string;
4385 char_u *s2 = tv2->vval.v_string;
4386 size_t len1 = STRLEN(s1);
4387
4388 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4389 if (tv1->vval.v_string == NULL)
4390 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004391 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004392 return FAIL;
4393 }
4394 mch_memmove(tv1->vval.v_string, s1, len1);
4395 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004396 vim_free(s1);
4397 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004398 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004399 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400 }
4401 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004402 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004403 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004404 if (*op == '.')
4405 {
4406 if (may_generate_2STRING(-2, cctx) == FAIL
4407 || may_generate_2STRING(-1, cctx) == FAIL)
4408 return FAIL;
4409 generate_instr_drop(cctx, ISN_CONCAT, 1);
4410 }
4411 else
4412 generate_two_op(cctx, op);
4413 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 }
4415
4416 return OK;
4417}
4418
4419/*
4420 * expr5a == expr5b
4421 * expr5a =~ expr5b
4422 * expr5a != expr5b
4423 * expr5a !~ expr5b
4424 * expr5a > expr5b
4425 * expr5a >= expr5b
4426 * expr5a < expr5b
4427 * expr5a <= expr5b
4428 * expr5a is expr5b
4429 * expr5a isnot expr5b
4430 *
4431 * Produces instructions:
4432 * EVAL expr5a Push result of "expr5a"
4433 * EVAL expr5b Push result of "expr5b"
4434 * COMPARE one of the compare instructions
4435 */
4436 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004437compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004438{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004439 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004441 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004444 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445
4446 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004447 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 return FAIL;
4449
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004450 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004451 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004452
4453 /*
4454 * If there is a comparative operator, use it.
4455 */
4456 if (type != EXPR_UNKNOWN)
4457 {
4458 int ic = FALSE; // Default: do not ignore case
4459
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004460 if (next != NULL)
4461 {
4462 *arg = next_line_from_context(cctx, TRUE);
4463 p = skipwhite(*arg);
4464 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004465 if (type_is && (p[len] == '?' || p[len] == '#'))
4466 {
4467 semsg(_(e_invexpr2), *arg);
4468 return FAIL;
4469 }
4470 // extra question mark appended: ignore case
4471 if (p[len] == '?')
4472 {
4473 ic = TRUE;
4474 ++len;
4475 }
4476 // extra '#' appended: match case (ignored)
4477 else if (p[len] == '#')
4478 ++len;
4479 // nothing appended: match case
4480
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004481 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004482 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004483 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004484 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004485 }
4486
4487 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004488 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004489 return FAIL;
4490
Bram Moolenaara5565e42020-05-09 15:44:01 +02004491 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004492 return FAIL;
4493
Bram Moolenaara5565e42020-05-09 15:44:01 +02004494 if (ppconst->pp_used == ppconst_used + 2)
4495 {
4496 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4497 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4498 int ret;
4499
4500 // Both sides are a constant, compute the result now.
4501 // First check for a valid combination of types, this is more
4502 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004503 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004504 ret = FAIL;
4505 else
4506 {
4507 ret = typval_compare(tv1, tv2, type, ic);
4508 tv1->v_type = VAR_BOOL;
4509 tv1->vval.v_number = tv1->vval.v_number
4510 ? VVAL_TRUE : VVAL_FALSE;
4511 clear_tv(tv2);
4512 --ppconst->pp_used;
4513 }
4514 return ret;
4515 }
4516
4517 generate_ppconst(cctx, ppconst);
4518 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004519 }
4520
4521 return OK;
4522}
4523
Bram Moolenaar7f141552020-05-09 17:35:53 +02004524static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4525
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004526/*
4527 * Compile || or &&.
4528 */
4529 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004530compile_and_or(
4531 char_u **arg,
4532 cctx_T *cctx,
4533 char *op,
4534 ppconst_T *ppconst,
4535 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004537 char_u *next;
4538 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004539 int opchar = *op;
4540
4541 if (p[0] == opchar && p[1] == opchar)
4542 {
4543 garray_T *instr = &cctx->ctx_instr;
4544 garray_T end_ga;
4545
4546 /*
4547 * Repeat until there is no following "||" or "&&"
4548 */
4549 ga_init2(&end_ga, sizeof(int), 10);
4550 while (p[0] == opchar && p[1] == opchar)
4551 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004552 if (next != NULL)
4553 {
4554 *arg = next_line_from_context(cctx, TRUE);
4555 p = skipwhite(*arg);
4556 }
4557
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004558 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4559 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004560 semsg(_(e_white_space_required_before_and_after_str_at_str),
4561 op, *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004562 return FAIL;
4563 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004565 // TODO: use ppconst if the value is a constant and check
4566 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004567 generate_ppconst(cctx, ppconst);
4568
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004569 // Every part must evaluate to a bool.
4570 if (bool_on_stack(cctx) == FAIL)
4571 {
4572 ga_clear(&end_ga);
4573 return FAIL;
4574 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004575
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 if (ga_grow(&end_ga, 1) == FAIL)
4577 {
4578 ga_clear(&end_ga);
4579 return FAIL;
4580 }
4581 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4582 ++end_ga.ga_len;
4583 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004584 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585
4586 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004587 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004588 {
4589 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004590 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004591 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004592
Bram Moolenaara5565e42020-05-09 15:44:01 +02004593 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4594 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595 {
4596 ga_clear(&end_ga);
4597 return FAIL;
4598 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004599
4600 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004602 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004603
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004604 // Every part must evaluate to a bool.
4605 if (bool_on_stack(cctx) == FAIL)
4606 {
4607 ga_clear(&end_ga);
4608 return FAIL;
4609 }
4610
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004611 // Fill in the end label in all jumps.
4612 while (end_ga.ga_len > 0)
4613 {
4614 isn_T *isn;
4615
4616 --end_ga.ga_len;
4617 isn = ((isn_T *)instr->ga_data)
4618 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4619 isn->isn_arg.jump.jump_where = instr->ga_len;
4620 }
4621 ga_clear(&end_ga);
4622 }
4623
4624 return OK;
4625}
4626
4627/*
4628 * expr4a && expr4a && expr4a logical AND
4629 *
4630 * Produces instructions:
4631 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004632 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004633 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004635 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636 * EVAL expr4c Push result of "expr4c"
4637 * end:
4638 */
4639 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004640compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004641{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004642 int ppconst_used = ppconst->pp_used;
4643
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004645 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004646 return FAIL;
4647
4648 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004649 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004650}
4651
4652/*
4653 * expr3a || expr3b || expr3c logical OR
4654 *
4655 * Produces instructions:
4656 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004657 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004658 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004659 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004660 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004661 * EVAL expr3c Push result of "expr3c"
4662 * end:
4663 */
4664 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004665compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004666{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004667 int ppconst_used = ppconst->pp_used;
4668
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004669 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004670 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004671 return FAIL;
4672
4673 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004674 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004675}
4676
4677/*
4678 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004679 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004680 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004681 * JUMP_IF_FALSE alt jump if false
4682 * EVAL expr1a
4683 * JUMP_ALWAYS end
4684 * alt: EVAL expr1b
4685 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004686 *
4687 * Toplevel expression: expr2 ?? expr1
4688 * Produces instructions:
4689 * EVAL expr2 Push result of "expr2"
4690 * JUMP_AND_KEEP_IF_TRUE end jump if true
4691 * EVAL expr1
4692 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004693 */
4694 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004695compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004696{
4697 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004698 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004699 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004700
Bram Moolenaar3988f642020-08-27 22:43:03 +02004701 // Ignore all kinds of errors when not producing code.
4702 if (cctx->ctx_skip == SKIP_YES)
4703 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004704 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004705 return OK;
4706 }
4707
Bram Moolenaar61a89812020-05-07 16:58:17 +02004708 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004709 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710 return FAIL;
4711
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004712 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713 if (*p == '?')
4714 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004715 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004716 garray_T *instr = &cctx->ctx_instr;
4717 garray_T *stack = &cctx->ctx_type_stack;
4718 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004719 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004720 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004721 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004722 int has_const_expr = FALSE;
4723 int const_value = FALSE;
4724 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004725
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004726 if (next != NULL)
4727 {
4728 *arg = next_line_from_context(cctx, TRUE);
4729 p = skipwhite(*arg);
4730 }
4731
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004732 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004733 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004734 semsg(_(e_white_space_required_before_and_after_str_at_str),
4735 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004736 return FAIL;
4737 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738
Bram Moolenaara5565e42020-05-09 15:44:01 +02004739 if (ppconst->pp_used == ppconst_used + 1)
4740 {
4741 // the condition is a constant, we know whether the ? or the :
4742 // expression is to be evaluated.
4743 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004744 if (op_falsy)
4745 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4746 else
4747 {
4748 int error = FALSE;
4749
4750 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4751 &error);
4752 if (error)
4753 return FAIL;
4754 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004755 cctx->ctx_skip = save_skip == SKIP_YES ||
4756 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4757
4758 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4759 // "left ?? right" and "left" is truthy: produce "left"
4760 generate_ppconst(cctx, ppconst);
4761 else
4762 {
4763 clear_tv(&ppconst->pp_tv[ppconst_used]);
4764 --ppconst->pp_used;
4765 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004766 }
4767 else
4768 {
4769 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004770 if (op_falsy)
4771 end_idx = instr->ga_len;
4772 generate_JUMP(cctx, op_falsy
4773 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4774 if (op_falsy)
4775 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004776 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004777
4778 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01004779 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004780 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004781 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004782 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004783
Bram Moolenaara5565e42020-05-09 15:44:01 +02004784 if (!has_const_expr)
4785 {
4786 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004787
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004788 if (!op_falsy)
4789 {
4790 // remember the type and drop it
4791 --stack->ga_len;
4792 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004793
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004794 end_idx = instr->ga_len;
4795 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004796
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004797 // jump here from JUMP_IF_FALSE
4798 isn = ((isn_T *)instr->ga_data) + alt_idx;
4799 isn->isn_arg.jump.jump_where = instr->ga_len;
4800 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004801 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004802
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004803 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004804 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004805 // Check for the ":".
4806 p = may_peek_next_line(cctx, *arg, &next);
4807 if (*p != ':')
4808 {
4809 emsg(_(e_missing_colon));
4810 return FAIL;
4811 }
4812 if (next != NULL)
4813 {
4814 *arg = next_line_from_context(cctx, TRUE);
4815 p = skipwhite(*arg);
4816 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004817
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004818 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4819 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004820 semsg(_(e_white_space_required_before_and_after_str_at_str),
4821 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004822 return FAIL;
4823 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004824
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004825 // evaluate the third expression
4826 if (has_const_expr)
4827 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004828 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01004829 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004830 return FAIL;
4831 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4832 return FAIL;
4833 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004834
Bram Moolenaara5565e42020-05-09 15:44:01 +02004835 if (!has_const_expr)
4836 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004837 type_T **typep;
4838
Bram Moolenaara5565e42020-05-09 15:44:01 +02004839 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004840
Bram Moolenaara5565e42020-05-09 15:44:01 +02004841 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004842 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4843 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004844
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004845 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004846 isn = ((isn_T *)instr->ga_data) + end_idx;
4847 isn->isn_arg.jump.jump_where = instr->ga_len;
4848 }
4849
4850 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004851 }
4852 return OK;
4853}
4854
4855/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004856 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004857 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4858 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004859 */
4860 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004861compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004862{
4863 ppconst_T ppconst;
4864
4865 CLEAR_FIELD(ppconst);
4866 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4867 {
4868 clear_ppconst(&ppconst);
4869 return FAIL;
4870 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004871 if (is_const != NULL)
4872 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004873 if (generate_ppconst(cctx, &ppconst) == FAIL)
4874 return FAIL;
4875 return OK;
4876}
4877
4878/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004879 * Toplevel expression.
4880 */
4881 static int
4882compile_expr0(char_u **arg, cctx_T *cctx)
4883{
4884 return compile_expr0_ext(arg, cctx, NULL);
4885}
4886
4887/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004888 * compile "return [expr]"
4889 */
4890 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004891compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892{
4893 char_u *p = arg;
4894 garray_T *stack = &cctx->ctx_type_stack;
4895 type_T *stack_type;
4896
4897 if (*p != NUL && *p != '|' && *p != '\n')
4898 {
4899 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004900 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004901 return NULL;
4902
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004903 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004904 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004905 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01004906 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01004907 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
4908 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004909 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004910 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004911 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004912 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004913 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004914 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4915 && stack_type->tt_type != VAR_VOID
4916 && stack_type->tt_type != VAR_UNKNOWN)
4917 {
4918 emsg(_(e_returning_value_in_function_without_return_type));
4919 return NULL;
4920 }
4921 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01004922 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004923 return NULL;
4924 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02004925 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004926 }
4927 else
4928 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004929 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004930 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004931 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4932 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004933 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004934 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004935 return NULL;
4936 }
4937
4938 // No argument, return zero.
4939 generate_PUSHNR(cctx, 0);
4940 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01004941
4942 // Undo any command modifiers.
4943 generate_undo_cmdmods(cctx);
4944
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004945 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004946 return NULL;
4947
4948 // "return val | endif" is possible
4949 return skipwhite(p);
4950}
4951
4952/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004953 * Get a line from the compilation context, compatible with exarg_T getline().
4954 * Return a pointer to the line in allocated memory.
4955 * Return NULL for end-of-file or some error.
4956 */
4957 static char_u *
4958exarg_getline(
4959 int c UNUSED,
4960 void *cookie,
4961 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004962 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004963{
4964 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004965 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004966
Bram Moolenaar66250c92020-08-20 15:02:42 +02004967 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004968 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004969 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004970 return NULL;
4971 ++cctx->ctx_lnum;
4972 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4973 // Comment lines result in NULL pointers, skip them.
4974 if (p != NULL)
4975 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004976 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004977}
4978
4979/*
4980 * Compile a nested :def command.
4981 */
4982 static char_u *
4983compile_nested_function(exarg_T *eap, cctx_T *cctx)
4984{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004985 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004986 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004987 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004988 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004989 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004990 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004991
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004992 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004993 {
4994 emsg(_(e_cannot_use_bang_with_nested_def));
4995 return NULL;
4996 }
4997
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004998 if (*name_start == '/')
4999 {
5000 name_end = skip_regexp(name_start + 1, '/', TRUE);
5001 if (*name_end == '/')
5002 ++name_end;
5003 eap->nextcmd = check_nextcmd(name_end);
5004 }
5005 if (name_end == name_start || *skipwhite(name_end) != '(')
5006 {
5007 if (!ends_excmd2(name_start, name_end))
5008 {
5009 semsg(_(e_invalid_command_str), eap->cmd);
5010 return NULL;
5011 }
5012
5013 // "def" or "def Name": list functions
5014 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5015 return NULL;
5016 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5017 }
5018
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005019 // Only g:Func() can use a namespace.
5020 if (name_start[1] == ':' && !is_global)
5021 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005022 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005023 return NULL;
5024 }
Bram Moolenaareef21022020-08-01 22:16:43 +02005025 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
5026 return NULL;
5027
Bram Moolenaar04b12692020-05-04 23:24:44 +02005028 eap->arg = name_end;
5029 eap->getline = exarg_getline;
5030 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005031 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005032 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005033 lambda_name = vim_strsave(get_lambda_name());
5034 if (lambda_name == NULL)
5035 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005036 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005037
Bram Moolenaar822ba242020-05-24 23:00:18 +02005038 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005039 {
5040 r = eap->skip ? OK : FAIL;
5041 goto theend;
5042 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005043 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02005044 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005045 {
5046 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005047 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005048 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005049
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005050 if (is_global)
5051 {
5052 char_u *func_name = vim_strnsave(name_start + 2,
5053 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005054
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005055 if (func_name == NULL)
5056 r = FAIL;
5057 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005058 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005059 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005060 lambda_name = NULL;
5061 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005062 }
5063 else
5064 {
5065 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005066 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005067 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005068 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02005069
Bram Moolenaareef21022020-08-01 22:16:43 +02005070 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005071 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005072 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005073 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005074 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005075
5076 // copy over the block scope IDs
5077 if (block_depth > 0)
5078 {
5079 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5080 if (ufunc->uf_block_ids != NULL)
5081 {
5082 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5083 sizeof(int) * block_depth);
5084 ufunc->uf_block_depth = block_depth;
5085 }
5086 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005087 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005088 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005089 r = OK;
5090
5091theend:
5092 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005093 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005094}
5095
5096/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005097 * Return the length of an assignment operator, or zero if there isn't one.
5098 */
5099 int
5100assignment_len(char_u *p, int *heredoc)
5101{
5102 if (*p == '=')
5103 {
5104 if (p[1] == '<' && p[2] == '<')
5105 {
5106 *heredoc = TRUE;
5107 return 3;
5108 }
5109 return 1;
5110 }
5111 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5112 return 2;
5113 if (STRNCMP(p, "..=", 3) == 0)
5114 return 3;
5115 return 0;
5116}
5117
5118// words that cannot be used as a variable
5119static char *reserved[] = {
5120 "true",
5121 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005122 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005123 NULL
5124};
5125
Bram Moolenaar752fc692021-01-04 21:57:11 +01005126// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005127typedef enum {
5128 dest_local,
5129 dest_option,
5130 dest_env,
5131 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005132 dest_buffer,
5133 dest_window,
5134 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005135 dest_vimvar,
5136 dest_script,
5137 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005138 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005139} assign_dest_T;
5140
Bram Moolenaar752fc692021-01-04 21:57:11 +01005141// Used by compile_lhs() to store information about the LHS of an assignment
5142// and one argument of ":unlet" with an index.
5143typedef struct {
5144 assign_dest_T lhs_dest; // type of destination
5145
5146 char_u *lhs_name; // allocated name including
5147 // "[expr]" or ".name".
5148 size_t lhs_varlen; // length of the variable without
5149 // "[expr]" or ".name"
5150 char_u *lhs_dest_end; // end of the destination, including
5151 // "[expr]" or ".name".
5152
5153 int lhs_has_index; // has "[expr]" or ".name"
5154
5155 int lhs_new_local; // create new local variable
5156 int lhs_opt_flags; // for when destination is an option
5157 int lhs_vimvaridx; // for when destination is a v:var
5158
5159 lvar_T lhs_local_lvar; // used for existing local destination
5160 lvar_T lhs_arg_lvar; // used for argument destination
5161 lvar_T *lhs_lvar; // points to destination lvar
5162 int lhs_scriptvar_sid;
5163 int lhs_scriptvar_idx;
5164
5165 int lhs_has_type; // type was specified
5166 type_T *lhs_type;
5167 type_T *lhs_member_type;
5168} lhs_T;
5169
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005170/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005171 * Generate the load instruction for "name".
5172 */
5173 static void
5174generate_loadvar(
5175 cctx_T *cctx,
5176 assign_dest_T dest,
5177 char_u *name,
5178 lvar_T *lvar,
5179 type_T *type)
5180{
5181 switch (dest)
5182 {
5183 case dest_option:
5184 // TODO: check the option exists
5185 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5186 break;
5187 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005188 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5189 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5190 else
5191 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005192 break;
5193 case dest_buffer:
5194 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5195 break;
5196 case dest_window:
5197 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5198 break;
5199 case dest_tab:
5200 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5201 break;
5202 case dest_script:
5203 compile_load_scriptvar(cctx,
5204 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5205 break;
5206 case dest_env:
5207 // Include $ in the name here
5208 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5209 break;
5210 case dest_reg:
5211 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5212 break;
5213 case dest_vimvar:
5214 generate_LOADV(cctx, name + 2, TRUE);
5215 break;
5216 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005217 if (lvar->lv_from_outer > 0)
5218 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5219 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005220 else
5221 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5222 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005223 case dest_expr:
5224 // list or dict value should already be on the stack.
5225 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005226 }
5227}
5228
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005229/*
5230 * Skip over "[expr]" or ".member".
5231 * Does not check for any errors.
5232 */
5233 static char_u *
5234skip_index(char_u *start)
5235{
5236 char_u *p = start;
5237
5238 if (*p == '[')
5239 {
5240 p = skipwhite(p + 1);
5241 (void)skip_expr(&p, NULL);
5242 p = skipwhite(p);
5243 if (*p == ']')
5244 return p + 1;
5245 return p;
5246 }
5247 // if (*p == '.')
5248 return to_name_end(p + 1, TRUE);
5249}
5250
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005251 void
5252vim9_declare_error(char_u *name)
5253{
5254 char *scope = "";
5255
5256 switch (*name)
5257 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005258 case 'g': scope = _("global"); break;
5259 case 'b': scope = _("buffer"); break;
5260 case 'w': scope = _("window"); break;
5261 case 't': scope = _("tab"); break;
5262 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005263 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005264 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005265 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005266 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005267 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005268 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005269 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005270 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005271 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005272}
5273
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005274/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005275 * For one assignment figure out the type of destination. Return it in "dest".
5276 * When not recognized "dest" is not set.
5277 * For an option "opt_flags" is set.
5278 * For a v:var "vimvaridx" is set.
5279 * "type" is set to the destination type if known, unchanted otherwise.
5280 * Return FAIL if an error message was given.
5281 */
5282 static int
5283get_var_dest(
5284 char_u *name,
5285 assign_dest_T *dest,
5286 int cmdidx,
5287 int *opt_flags,
5288 int *vimvaridx,
5289 type_T **type,
5290 cctx_T *cctx)
5291{
5292 char_u *p;
5293
5294 if (*name == '&')
5295 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005296 int cc;
5297 long numval;
5298 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005299
5300 *dest = dest_option;
5301 if (cmdidx == CMD_final || cmdidx == CMD_const)
5302 {
5303 emsg(_(e_const_option));
5304 return FAIL;
5305 }
5306 p = name;
5307 p = find_option_end(&p, opt_flags);
5308 if (p == NULL)
5309 {
5310 // cannot happen?
5311 emsg(_(e_letunexp));
5312 return FAIL;
5313 }
5314 cc = *p;
5315 *p = NUL;
5316 opt_type = get_option_value(skip_option_env_lead(name),
5317 &numval, NULL, *opt_flags);
5318 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005319 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005320 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005321 case gov_unknown:
5322 semsg(_(e_unknown_option), name);
5323 return FAIL;
5324 case gov_string:
5325 case gov_hidden_string:
5326 *type = &t_string;
5327 break;
5328 case gov_bool:
5329 case gov_hidden_bool:
5330 *type = &t_bool;
5331 break;
5332 case gov_number:
5333 case gov_hidden_number:
5334 *type = &t_number;
5335 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005336 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005337 }
5338 else if (*name == '$')
5339 {
5340 *dest = dest_env;
5341 *type = &t_string;
5342 }
5343 else if (*name == '@')
5344 {
5345 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5346 {
5347 emsg_invreg(name[1]);
5348 return FAIL;
5349 }
5350 *dest = dest_reg;
5351 *type = &t_string;
5352 }
5353 else if (STRNCMP(name, "g:", 2) == 0)
5354 {
5355 *dest = dest_global;
5356 }
5357 else if (STRNCMP(name, "b:", 2) == 0)
5358 {
5359 *dest = dest_buffer;
5360 }
5361 else if (STRNCMP(name, "w:", 2) == 0)
5362 {
5363 *dest = dest_window;
5364 }
5365 else if (STRNCMP(name, "t:", 2) == 0)
5366 {
5367 *dest = dest_tab;
5368 }
5369 else if (STRNCMP(name, "v:", 2) == 0)
5370 {
5371 typval_T *vtv;
5372 int di_flags;
5373
5374 *vimvaridx = find_vim_var(name + 2, &di_flags);
5375 if (*vimvaridx < 0)
5376 {
5377 semsg(_(e_variable_not_found_str), name);
5378 return FAIL;
5379 }
5380 // We use the current value of "sandbox" here, is that OK?
5381 if (var_check_ro(di_flags, name, FALSE))
5382 return FAIL;
5383 *dest = dest_vimvar;
5384 vtv = get_vim_var_tv(*vimvaridx);
5385 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5386 }
5387 return OK;
5388}
5389
5390/*
5391 * Generate a STORE instruction for "dest", not being "dest_local".
5392 * Return FAIL when out of memory.
5393 */
5394 static int
5395generate_store_var(
5396 cctx_T *cctx,
5397 assign_dest_T dest,
5398 int opt_flags,
5399 int vimvaridx,
5400 int scriptvar_idx,
5401 int scriptvar_sid,
5402 type_T *type,
5403 char_u *name)
5404{
5405 switch (dest)
5406 {
5407 case dest_option:
5408 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5409 opt_flags);
5410 case dest_global:
5411 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005412 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5413 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005414 case dest_buffer:
5415 // include b: with the name, easier to execute that way
5416 return generate_STORE(cctx, ISN_STOREB, 0, name);
5417 case dest_window:
5418 // include w: with the name, easier to execute that way
5419 return generate_STORE(cctx, ISN_STOREW, 0, name);
5420 case dest_tab:
5421 // include t: with the name, easier to execute that way
5422 return generate_STORE(cctx, ISN_STORET, 0, name);
5423 case dest_env:
5424 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5425 case dest_reg:
5426 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5427 case dest_vimvar:
5428 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5429 case dest_script:
5430 if (scriptvar_idx < 0)
5431 {
5432 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005433 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005434
Bram Moolenaar41d61962020-12-06 20:12:43 +01005435 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005436 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5437 scriptvar_sid, type);
5438 if (name_s != name)
5439 vim_free(name_s);
5440 return r;
5441 }
5442 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5443 scriptvar_sid, scriptvar_idx, type);
5444 case dest_local:
5445 case dest_expr:
5446 // cannot happen
5447 break;
5448 }
5449 return FAIL;
5450}
5451
Bram Moolenaar752fc692021-01-04 21:57:11 +01005452 static int
5453is_decl_command(int cmdidx)
5454{
5455 return cmdidx == CMD_let || cmdidx == CMD_var
5456 || cmdidx == CMD_final || cmdidx == CMD_const;
5457}
5458
5459/*
5460 * Figure out the LHS type and other properties for an assignment or one item
5461 * of ":unlet" with an index.
5462 * Returns OK or FAIL.
5463 */
5464 static int
5465compile_lhs(
5466 char_u *var_start,
5467 lhs_T *lhs,
5468 int cmdidx,
5469 int heredoc,
5470 int oplen,
5471 cctx_T *cctx)
5472{
5473 char_u *var_end;
5474 int is_decl = is_decl_command(cmdidx);
5475
5476 CLEAR_POINTER(lhs);
5477 lhs->lhs_dest = dest_local;
5478 lhs->lhs_vimvaridx = -1;
5479 lhs->lhs_scriptvar_idx = -1;
5480
5481 // "dest_end" is the end of the destination, including "[expr]" or
5482 // ".name".
5483 // "var_end" is the end of the variable/option/etc. name.
5484 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5485 if (*var_start == '@')
5486 var_end = var_start + 2;
5487 else
5488 {
5489 // skip over the leading "&", "&l:", "&g:" and "$"
5490 var_end = skip_option_env_lead(var_start);
5491 var_end = to_name_end(var_end, TRUE);
5492 }
5493
5494 // "a: type" is declaring variable "a" with a type, not dict "a:".
5495 if (is_decl && lhs->lhs_dest_end == var_start + 2
5496 && lhs->lhs_dest_end[-1] == ':')
5497 --lhs->lhs_dest_end;
5498 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5499 --var_end;
5500
5501 // compute the length of the destination without "[expr]" or ".name"
5502 lhs->lhs_varlen = var_end - var_start;
5503 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5504 if (lhs->lhs_name == NULL)
5505 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005506
5507 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5508 // Something follows after the variable: "var[idx]" or "var.key".
5509 lhs->lhs_has_index = TRUE;
5510
Bram Moolenaar752fc692021-01-04 21:57:11 +01005511 if (heredoc)
5512 lhs->lhs_type = &t_list_string;
5513 else
5514 lhs->lhs_type = &t_any;
5515
5516 if (cctx->ctx_skip != SKIP_YES)
5517 {
5518 int declare_error = FALSE;
5519
5520 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5521 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5522 &lhs->lhs_type, cctx) == FAIL)
5523 return FAIL;
5524 if (lhs->lhs_dest != dest_local)
5525 {
5526 // Specific kind of variable recognized.
5527 declare_error = is_decl;
5528 }
5529 else
5530 {
5531 int idx;
5532
5533 // No specific kind of variable recognized, just a name.
5534 for (idx = 0; reserved[idx] != NULL; ++idx)
5535 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5536 {
5537 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5538 return FAIL;
5539 }
5540
5541
5542 if (lookup_local(var_start, lhs->lhs_varlen,
5543 &lhs->lhs_local_lvar, cctx) == OK)
5544 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5545 else
5546 {
5547 CLEAR_FIELD(lhs->lhs_arg_lvar);
5548 if (arg_exists(var_start, lhs->lhs_varlen,
5549 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5550 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5551 {
5552 if (is_decl)
5553 {
5554 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5555 return FAIL;
5556 }
5557 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5558 }
5559 }
5560 if (lhs->lhs_lvar != NULL)
5561 {
5562 if (is_decl)
5563 {
5564 semsg(_(e_variable_already_declared), lhs->lhs_name);
5565 return FAIL;
5566 }
5567 }
5568 else
5569 {
5570 int script_namespace = lhs->lhs_varlen > 1
5571 && STRNCMP(var_start, "s:", 2) == 0;
5572 int script_var = (script_namespace
5573 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
5574 FALSE, cctx)
5575 : script_var_exists(var_start, lhs->lhs_varlen,
5576 TRUE, cctx)) == OK;
5577 imported_T *import =
5578 find_imported(var_start, lhs->lhs_varlen, cctx);
5579
5580 if (script_namespace || script_var || import != NULL)
5581 {
5582 char_u *rawname = lhs->lhs_name
5583 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5584
5585 if (is_decl)
5586 {
5587 if (script_namespace)
5588 semsg(_(e_cannot_declare_script_variable_in_function),
5589 lhs->lhs_name);
5590 else
5591 semsg(_(e_variable_already_declared_in_script),
5592 lhs->lhs_name);
5593 return FAIL;
5594 }
5595 else if (cctx->ctx_ufunc->uf_script_ctx_version
5596 == SCRIPT_VERSION_VIM9
5597 && script_namespace
5598 && !script_var && import == NULL)
5599 {
5600 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5601 return FAIL;
5602 }
5603
5604 lhs->lhs_dest = dest_script;
5605
5606 // existing script-local variables should have a type
5607 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5608 if (import != NULL)
5609 lhs->lhs_scriptvar_sid = import->imp_sid;
5610 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5611 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005612 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005613 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005614 lhs->lhs_scriptvar_sid, rawname,
5615 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5616 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005617 if (lhs->lhs_scriptvar_idx >= 0)
5618 {
5619 scriptitem_T *si = SCRIPT_ITEM(
5620 lhs->lhs_scriptvar_sid);
5621 svar_T *sv =
5622 ((svar_T *)si->sn_var_vals.ga_data)
5623 + lhs->lhs_scriptvar_idx;
5624 lhs->lhs_type = sv->sv_type;
5625 }
5626 }
5627 }
5628 else if (check_defined(var_start, lhs->lhs_varlen, cctx)
5629 == FAIL)
5630 return FAIL;
5631 }
5632 }
5633
5634 if (declare_error)
5635 {
5636 vim9_declare_error(lhs->lhs_name);
5637 return FAIL;
5638 }
5639 }
5640
5641 // handle "a:name" as a name, not index "name" on "a"
5642 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5643 var_end = lhs->lhs_dest_end;
5644
5645 if (lhs->lhs_dest != dest_option)
5646 {
5647 if (is_decl && *var_end == ':')
5648 {
5649 char_u *p;
5650
5651 // parse optional type: "let var: type = expr"
5652 if (!VIM_ISWHITE(var_end[1]))
5653 {
5654 semsg(_(e_white_space_required_after_str), ":");
5655 return FAIL;
5656 }
5657 p = skipwhite(var_end + 1);
5658 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5659 if (lhs->lhs_type == NULL)
5660 return FAIL;
5661 lhs->lhs_has_type = TRUE;
5662 }
5663 else if (lhs->lhs_lvar != NULL)
5664 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5665 }
5666
5667 if (oplen == 3 && !heredoc && lhs->lhs_dest != dest_global
5668 && lhs->lhs_type->tt_type != VAR_STRING
5669 && lhs->lhs_type->tt_type != VAR_ANY)
5670 {
5671 emsg(_(e_can_only_concatenate_to_string));
5672 return FAIL;
5673 }
5674
5675 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5676 && cctx->ctx_skip != SKIP_YES)
5677 {
5678 if (oplen > 1 && !heredoc)
5679 {
5680 // +=, /=, etc. require an existing variable
5681 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5682 return FAIL;
5683 }
5684 if (!is_decl)
5685 {
5686 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5687 return FAIL;
5688 }
5689
5690 // new local variable
5691 if ((lhs->lhs_type->tt_type == VAR_FUNC
5692 || lhs->lhs_type->tt_type == VAR_PARTIAL)
5693 && var_wrong_func_name(lhs->lhs_name, TRUE))
5694 return FAIL;
5695 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5696 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5697 if (lhs->lhs_lvar == NULL)
5698 return FAIL;
5699 lhs->lhs_new_local = TRUE;
5700 }
5701
5702 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01005703 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005704 {
5705 // Something follows after the variable: "var[idx]" or "var.key".
5706 // TODO: should we also handle "->func()" here?
5707 if (is_decl)
5708 {
5709 emsg(_(e_cannot_use_index_when_declaring_variable));
5710 return FAIL;
5711 }
5712
5713 if (var_start[lhs->lhs_varlen] == '['
5714 || var_start[lhs->lhs_varlen] == '.')
5715 {
5716 char_u *after = var_start + lhs->lhs_varlen;
5717 char_u *p;
5718
5719 // Only the last index is used below, if there are others
5720 // before it generate code for the expression. Thus for
5721 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5722 for (;;)
5723 {
5724 p = skip_index(after);
5725 if (*p != '[' && *p != '.')
5726 break;
5727 after = p;
5728 }
5729 if (after > var_start + lhs->lhs_varlen)
5730 {
5731 lhs->lhs_varlen = after - var_start;
5732 lhs->lhs_dest = dest_expr;
5733 // We don't know the type before evaluating the expression,
5734 // use "any" until then.
5735 lhs->lhs_type = &t_any;
5736 }
5737
Bram Moolenaar752fc692021-01-04 21:57:11 +01005738 if (lhs->lhs_type->tt_member == NULL)
5739 lhs->lhs_member_type = &t_any;
5740 else
5741 lhs->lhs_member_type = lhs->lhs_type->tt_member;
5742 }
5743 else
5744 {
5745 semsg("Not supported yet: %s", var_start);
5746 return FAIL;
5747 }
5748 }
5749 return OK;
5750}
5751
5752/*
5753 * Assignment to a list or dict member, or ":unlet" for the item, using the
5754 * information in "lhs".
5755 * Returns OK or FAIL.
5756 */
5757 static int
5758compile_assign_unlet(
5759 char_u *var_start,
5760 lhs_T *lhs,
5761 int is_assign,
5762 type_T *rhs_type,
5763 cctx_T *cctx)
5764{
5765 char_u *p;
5766 int r;
5767 vartype_T dest_type;
5768 size_t varlen = lhs->lhs_varlen;
5769 garray_T *stack = &cctx->ctx_type_stack;
5770
5771 // Compile the "idx" in "var[idx]" or "key" in "var.key".
5772 p = var_start + varlen;
5773 if (*p == '[')
5774 {
5775 p = skipwhite(p + 1);
5776 r = compile_expr0(&p, cctx);
5777 if (r == OK && *skipwhite(p) != ']')
5778 {
5779 // this should not happen
5780 emsg(_(e_missbrac));
5781 r = FAIL;
5782 }
5783 }
5784 else // if (*p == '.')
5785 {
5786 char_u *key_end = to_name_end(p + 1, TRUE);
5787 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5788
5789 r = generate_PUSHS(cctx, key);
5790 }
5791 if (r == FAIL)
5792 return FAIL;
5793
5794 if (lhs->lhs_type == &t_any)
5795 {
5796 // Index on variable of unknown type: check at runtime.
5797 dest_type = VAR_ANY;
5798 }
5799 else
5800 {
5801 dest_type = lhs->lhs_type->tt_type;
5802 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
5803 return FAIL;
5804 if (dest_type == VAR_LIST
5805 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
5806 != VAR_NUMBER)
5807 {
5808 emsg(_(e_number_exp));
5809 return FAIL;
5810 }
5811 }
5812
5813 // Load the dict or list. On the stack we then have:
5814 // - value (for assignment, not for :unlet)
5815 // - index
5816 // - variable
5817 if (lhs->lhs_dest == dest_expr)
5818 {
5819 int c = var_start[varlen];
5820
5821 // Evaluate "ll[expr]" of "ll[expr][idx]"
5822 p = var_start;
5823 var_start[varlen] = NUL;
5824 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
5825 {
5826 // this should not happen
5827 emsg(_(e_missbrac));
5828 return FAIL;
5829 }
5830 var_start[varlen] = c;
5831
5832 lhs->lhs_type = stack->ga_len == 0 ? &t_void
5833 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5834 // now we can properly check the type
5835 if (lhs->lhs_type->tt_member != NULL && rhs_type != &t_void
Bram Moolenaar351ead02021-01-16 16:07:01 +01005836 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01005837 FALSE, FALSE) == FAIL)
5838 return FAIL;
5839 }
5840 else
5841 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
5842 lhs->lhs_lvar, lhs->lhs_type);
5843
5844 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
5845 {
5846 if (is_assign)
5847 {
5848 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
5849
5850 if (isn == NULL)
5851 return FAIL;
5852 isn->isn_arg.vartype = dest_type;
5853 }
5854 else
5855 {
5856 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
5857 return FAIL;
5858 }
5859 }
5860 else
5861 {
5862 emsg(_(e_indexable_type_required));
5863 return FAIL;
5864 }
5865
5866 return OK;
5867}
5868
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005869/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005870 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005871 * "let name"
5872 * "var name = expr"
5873 * "final name = expr"
5874 * "const name = expr"
5875 * "name = expr"
5876 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005877 * Return NULL for an error.
5878 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005879 */
5880 static char_u *
5881compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5882{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005883 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005884 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005885 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005886 char_u *ret = NULL;
5887 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005888 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005889 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005890 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005891 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005892 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005893 int oplen = 0;
5894 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005895 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005896 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005897 int is_decl = is_decl_command(cmdidx);
5898 lhs_T lhs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005899
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005900 // Skip over the "var" or "[var, var]" to get to any "=".
5901 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5902 if (p == NULL)
5903 return *arg == '[' ? arg : NULL;
5904
5905 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005906 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005907 // TODO: should we allow this, and figure out type inference from list
5908 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005909 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005910 return NULL;
5911 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01005912 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005913
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005914 sp = p;
5915 p = skipwhite(p);
5916 op = p;
5917 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005918
5919 if (var_count > 0 && oplen == 0)
5920 // can be something like "[1, 2]->func()"
5921 return arg;
5922
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005923 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005924 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005925 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005926 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005927 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005928
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005929 if (heredoc)
5930 {
5931 list_T *l;
5932 listitem_T *li;
5933
5934 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005935 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005936 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005937 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005938 if (l == NULL)
5939 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005940
Bram Moolenaar078269b2020-09-21 20:35:55 +02005941 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005942 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005943 // Push each line and the create the list.
5944 FOR_ALL_LIST_ITEMS(l, li)
5945 {
5946 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5947 li->li_tv.vval.v_string = NULL;
5948 }
5949 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005950 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005951 list_free(l);
5952 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005953 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005954 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005955 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005956 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005957 char_u *wp;
5958
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005959 // for "[var, var] = expr" evaluate the expression here, loop over the
5960 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005961 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005962
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005963 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01005964 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005965 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005966 if (compile_expr0(&p, cctx) == FAIL)
5967 return NULL;
5968 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005969
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005970 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005971 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005972 type_T *stacktype;
5973
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005974 stacktype = stack->ga_len == 0 ? &t_void
5975 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005976 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005977 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005978 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005979 goto theend;
5980 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01005981 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005982 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005983 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005984 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005985 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5986 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005987 if (stacktype->tt_member != NULL)
5988 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005989 }
5990 }
5991
5992 /*
5993 * Loop over variables in "[var, var] = expr".
5994 * For "var = expr" and "let var: type" this is done only once.
5995 */
5996 if (var_count > 0)
5997 var_start = skipwhite(arg + 1); // skip over the "["
5998 else
5999 var_start = arg;
6000 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6001 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006002 int instr_count = -1;
6003
Bram Moolenaar752fc692021-01-04 21:57:11 +01006004 vim_free(lhs.lhs_name);
6005
6006 /*
6007 * Figure out the LHS type and other properties.
6008 */
6009 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6010 goto theend;
6011
6012 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006013 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006014 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006015 goto theend;
6016 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006017 if (!is_decl && lhs.lhs_lvar != NULL
6018 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006019 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006020 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006021 goto theend;
6022 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006023
6024 if (!heredoc)
6025 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006026 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006027 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006028 if (oplen > 0 && var_count == 0)
6029 {
6030 // skip over the "=" and the expression
6031 p = skipwhite(op + oplen);
6032 compile_expr0(&p, cctx);
6033 }
6034 }
6035 else if (oplen > 0)
6036 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006037 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006038 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006039
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006040 // For "var = expr" evaluate the expression.
6041 if (var_count == 0)
6042 {
6043 int r;
6044
6045 // for "+=", "*=", "..=" etc. first load the current value
6046 if (*op != '=')
6047 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006048 generate_loadvar(cctx, lhs.lhs_dest, lhs.lhs_name,
6049 lhs.lhs_lvar, lhs.lhs_type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006050
Bram Moolenaar752fc692021-01-04 21:57:11 +01006051 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006052 {
6053 // TODO: get member from list or dict
6054 emsg("Index with operation not supported yet");
6055 goto theend;
6056 }
6057 }
6058
6059 // Compile the expression. Temporarily hide the new local
6060 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006061 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006062 --cctx->ctx_locals.ga_len;
6063 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006064 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006065 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006066 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006067 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006068 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006069 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006070 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006071 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006072 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006073 ++cctx->ctx_locals.ga_len;
6074 if (r == FAIL)
6075 goto theend;
6076 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006077 else if (semicolon && var_idx == var_count - 1)
6078 {
6079 // For "[var; var] = expr" get the rest of the list
6080 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6081 goto theend;
6082 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006083 else
6084 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006085 // For "[var, var] = expr" get the "var_idx" item from the
6086 // list.
6087 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006088 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006089 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006090
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006091 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006092 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006093 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006094 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006095 if ((rhs_type->tt_type == VAR_FUNC
6096 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006097 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006098 goto theend;
6099
Bram Moolenaar752fc692021-01-04 21:57:11 +01006100 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006101 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006102 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006103 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006104 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006105 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006106 }
6107 else
6108 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006109 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006110 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006111 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006112 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006113 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006114 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006115 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006116 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006117 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006118 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006119 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006120 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006121 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006122 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006123 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006124
Bram Moolenaar71abe482020-09-14 22:28:30 +02006125 // without operator check type here, otherwise below
Bram Moolenaar752fc692021-01-04 21:57:11 +01006126 if (lhs.lhs_has_index)
6127 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006128 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006129 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006130 goto theend;
6131 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006132 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006133 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006134 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006135 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006136 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006137 else if (cmdidx == CMD_final)
6138 {
6139 emsg(_(e_final_requires_a_value));
6140 goto theend;
6141 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006142 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006143 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006144 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006145 goto theend;
6146 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006147 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006148 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006149 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006150 goto theend;
6151 }
6152 else
6153 {
6154 // variables are always initialized
6155 if (ga_grow(instr, 1) == FAIL)
6156 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006157 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006158 {
6159 case VAR_BOOL:
6160 generate_PUSHBOOL(cctx, VVAL_FALSE);
6161 break;
6162 case VAR_FLOAT:
6163#ifdef FEAT_FLOAT
6164 generate_PUSHF(cctx, 0.0);
6165#endif
6166 break;
6167 case VAR_STRING:
6168 generate_PUSHS(cctx, NULL);
6169 break;
6170 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006171 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006172 break;
6173 case VAR_FUNC:
6174 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6175 break;
6176 case VAR_LIST:
6177 generate_NEWLIST(cctx, 0);
6178 break;
6179 case VAR_DICT:
6180 generate_NEWDICT(cctx, 0);
6181 break;
6182 case VAR_JOB:
6183 generate_PUSHJOB(cctx, NULL);
6184 break;
6185 case VAR_CHANNEL:
6186 generate_PUSHCHANNEL(cctx, NULL);
6187 break;
6188 case VAR_NUMBER:
6189 case VAR_UNKNOWN:
6190 case VAR_ANY:
6191 case VAR_PARTIAL:
6192 case VAR_VOID:
6193 case VAR_SPECIAL: // cannot happen
6194 generate_PUSHNR(cctx, 0);
6195 break;
6196 }
6197 }
6198 if (var_count == 0)
6199 end = p;
6200 }
6201
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006202 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006203 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006204 break;
6205
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006206 if (oplen > 0 && *op != '=')
6207 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006208 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006209 type_T *stacktype;
6210
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006211 if (*op == '.')
6212 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006213 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006214 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006215 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006216 if (
6217#ifdef FEAT_FLOAT
6218 // If variable is float operation with number is OK.
6219 !(expected == &t_float && stacktype == &t_number) &&
6220#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006221 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006222 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006223 goto theend;
6224
6225 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006226 {
6227 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6228 goto theend;
6229 }
6230 else if (*op == '+')
6231 {
6232 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006233 operator_type(lhs.lhs_member_type, stacktype),
6234 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006235 goto theend;
6236 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006237 else if (generate_two_op(cctx, op) == FAIL)
6238 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006239 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006240
Bram Moolenaar752fc692021-01-04 21:57:11 +01006241 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006242 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006243 // Use the info in "lhs" to store the value at the index in the
6244 // list or dict.
6245 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6246 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006247 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006248 }
6249 else
6250 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006251 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
6252 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006253 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006254 generate_LOCKCONST(cctx);
6255
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006256 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006257 && (lhs.lhs_type->tt_type == VAR_DICT
6258 || lhs.lhs_type->tt_type == VAR_LIST)
6259 && lhs.lhs_type->tt_member != NULL
6260 && lhs.lhs_type->tt_member != &t_any
6261 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006262 // Set the type in the list or dict, so that it can be checked,
6263 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006264 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006265
Bram Moolenaar752fc692021-01-04 21:57:11 +01006266 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006267 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006268 if (generate_store_var(cctx, lhs.lhs_dest,
6269 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6270 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6271 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006272 goto theend;
6273 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006274 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006275 {
6276 isn_T *isn = ((isn_T *)instr->ga_data)
6277 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006278
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006279 // optimization: turn "var = 123" from ISN_PUSHNR +
6280 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006281 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006282 && instr->ga_len == instr_count + 1
6283 && isn->isn_type == ISN_PUSHNR)
6284 {
6285 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006286
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006287 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006288 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006289 isn->isn_arg.storenr.stnr_val = val;
6290 if (stack->ga_len > 0)
6291 --stack->ga_len;
6292 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006293 else if (lhs.lhs_lvar->lv_from_outer > 0)
6294 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6295 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006296 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006297 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006298 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006299 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006300
6301 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006302 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006303 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006304
6305 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006306 if (var_count > 0 && !semicolon)
6307 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006308 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006309 goto theend;
6310 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006311
Bram Moolenaarb2097502020-07-19 17:17:02 +02006312 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006313
6314theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006315 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006316 return ret;
6317}
6318
6319/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006320 * Check for an assignment at "eap->cmd", compile it if found.
6321 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6322 */
6323 static int
6324may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6325{
6326 char_u *pskip;
6327 char_u *p;
6328
6329 // Assuming the command starts with a variable or function name,
6330 // find what follows.
6331 // Skip over "var.member", "var[idx]" and the like.
6332 // Also "&opt = val", "$ENV = val" and "@r = val".
6333 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6334 ? eap->cmd + 1 : eap->cmd;
6335 p = to_name_end(pskip, TRUE);
6336 if (p > eap->cmd && *p != NUL)
6337 {
6338 char_u *var_end;
6339 int oplen;
6340 int heredoc;
6341
6342 if (eap->cmd[0] == '@')
6343 var_end = eap->cmd + 2;
6344 else
6345 var_end = find_name_end(pskip, NULL, NULL,
6346 FNE_CHECK_START | FNE_INCL_BR);
6347 oplen = assignment_len(skipwhite(var_end), &heredoc);
6348 if (oplen > 0)
6349 {
6350 size_t len = p - eap->cmd;
6351
6352 // Recognize an assignment if we recognize the variable
6353 // name:
6354 // "g:var = expr"
6355 // "local = expr" where "local" is a local var.
6356 // "script = expr" where "script" is a script-local var.
6357 // "import = expr" where "import" is an imported var
6358 // "&opt = expr"
6359 // "$ENV = expr"
6360 // "@r = expr"
6361 if (*eap->cmd == '&'
6362 || *eap->cmd == '$'
6363 || *eap->cmd == '@'
6364 || ((len) > 2 && eap->cmd[1] == ':')
6365 || lookup_local(eap->cmd, len, NULL, cctx) == OK
6366 || arg_exists(eap->cmd, len, NULL, NULL, NULL, cctx) == OK
6367 || script_var_exists(eap->cmd, len, FALSE, cctx) == OK
6368 || find_imported(eap->cmd, len, cctx) != NULL)
6369 {
6370 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6371 if (*line == NULL || *line == eap->cmd)
6372 return FAIL;
6373 return OK;
6374 }
6375 }
6376 }
6377
6378 if (*eap->cmd == '[')
6379 {
6380 // [var, var] = expr
6381 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6382 if (*line == NULL)
6383 return FAIL;
6384 if (*line != eap->cmd)
6385 return OK;
6386 }
6387 return NOTDONE;
6388}
6389
6390/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006391 * Check if "name" can be "unlet".
6392 */
6393 int
6394check_vim9_unlet(char_u *name)
6395{
6396 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6397 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006398 // "unlet s:var" is allowed in legacy script.
6399 if (*name == 's' && !script_is_vim9())
6400 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006401 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006402 return FAIL;
6403 }
6404 return OK;
6405}
6406
6407/*
6408 * Callback passed to ex_unletlock().
6409 */
6410 static int
6411compile_unlet(
6412 lval_T *lvp,
6413 char_u *name_end,
6414 exarg_T *eap,
6415 int deep UNUSED,
6416 void *coookie)
6417{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006418 cctx_T *cctx = coookie;
6419 char_u *p = lvp->ll_name;
6420 int cc = *name_end;
6421 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006422
Bram Moolenaar752fc692021-01-04 21:57:11 +01006423 if (cctx->ctx_skip == SKIP_YES)
6424 return OK;
6425
6426 *name_end = NUL;
6427 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006428 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006429 // :unlet $ENV_VAR
6430 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6431 }
6432 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6433 {
6434 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006435
Bram Moolenaar752fc692021-01-04 21:57:11 +01006436 // This is similar to assigning: lookup the list/dict, compile the
6437 // idx/key. Then instead of storing the value unlet the item.
6438 // unlet {list}[idx]
6439 // unlet {dict}[key] dict.key
6440 //
6441 // Figure out the LHS type and other properties.
6442 //
6443 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6444
6445 // : unlet an indexed item
6446 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006447 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006448 iemsg("called compile_lhs() without an index");
6449 ret = FAIL;
6450 }
6451 else
6452 {
6453 // Use the info in "lhs" to unlet the item at the index in the
6454 // list or dict.
6455 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006456 }
6457
Bram Moolenaar752fc692021-01-04 21:57:11 +01006458 vim_free(lhs.lhs_name);
6459 }
6460 else if (check_vim9_unlet(p) == FAIL)
6461 {
6462 ret = FAIL;
6463 }
6464 else
6465 {
6466 // Normal name. Only supports g:, w:, t: and b: namespaces.
6467 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006468 }
6469
Bram Moolenaar752fc692021-01-04 21:57:11 +01006470 *name_end = cc;
6471 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006472}
6473
6474/*
6475 * compile "unlet var", "lock var" and "unlock var"
6476 * "arg" points to "var".
6477 */
6478 static char_u *
6479compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6480{
6481 char_u *p = arg;
6482
6483 if (eap->cmdidx != CMD_unlet)
6484 {
6485 emsg("Sorry, :lock and unlock not implemented yet");
6486 return NULL;
6487 }
6488
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006489 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6490 compile_unlet, cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006491 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6492}
6493
6494/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006495 * Compile an :import command.
6496 */
6497 static char_u *
6498compile_import(char_u *arg, cctx_T *cctx)
6499{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006500 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006501}
6502
6503/*
6504 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6505 */
6506 static int
6507compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6508{
6509 garray_T *instr = &cctx->ctx_instr;
6510 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6511
6512 if (endlabel == NULL)
6513 return FAIL;
6514 endlabel->el_next = *el;
6515 *el = endlabel;
6516 endlabel->el_end_label = instr->ga_len;
6517
6518 generate_JUMP(cctx, when, 0);
6519 return OK;
6520}
6521
6522 static void
6523compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
6524{
6525 garray_T *instr = &cctx->ctx_instr;
6526
6527 while (*el != NULL)
6528 {
6529 endlabel_T *cur = (*el);
6530 isn_T *isn;
6531
6532 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
6533 isn->isn_arg.jump.jump_where = instr->ga_len;
6534 *el = cur->el_next;
6535 vim_free(cur);
6536 }
6537}
6538
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006539 static void
6540compile_free_jump_to_end(endlabel_T **el)
6541{
6542 while (*el != NULL)
6543 {
6544 endlabel_T *cur = (*el);
6545
6546 *el = cur->el_next;
6547 vim_free(cur);
6548 }
6549}
6550
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006551/*
6552 * Create a new scope and set up the generic items.
6553 */
6554 static scope_T *
6555new_scope(cctx_T *cctx, scopetype_T type)
6556{
6557 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6558
6559 if (scope == NULL)
6560 return NULL;
6561 scope->se_outer = cctx->ctx_scope;
6562 cctx->ctx_scope = scope;
6563 scope->se_type = type;
6564 scope->se_local_count = cctx->ctx_locals.ga_len;
6565 return scope;
6566}
6567
6568/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006569 * Free the current scope and go back to the outer scope.
6570 */
6571 static void
6572drop_scope(cctx_T *cctx)
6573{
6574 scope_T *scope = cctx->ctx_scope;
6575
6576 if (scope == NULL)
6577 {
6578 iemsg("calling drop_scope() without a scope");
6579 return;
6580 }
6581 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006582 switch (scope->se_type)
6583 {
6584 case IF_SCOPE:
6585 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6586 case FOR_SCOPE:
6587 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6588 case WHILE_SCOPE:
6589 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6590 case TRY_SCOPE:
6591 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6592 case NO_SCOPE:
6593 case BLOCK_SCOPE:
6594 break;
6595 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006596 vim_free(scope);
6597}
6598
6599/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006600 * compile "if expr"
6601 *
6602 * "if expr" Produces instructions:
6603 * EVAL expr Push result of "expr"
6604 * JUMP_IF_FALSE end
6605 * ... body ...
6606 * end:
6607 *
6608 * "if expr | else" Produces instructions:
6609 * EVAL expr Push result of "expr"
6610 * JUMP_IF_FALSE else
6611 * ... body ...
6612 * JUMP_ALWAYS end
6613 * else:
6614 * ... body ...
6615 * end:
6616 *
6617 * "if expr1 | elseif expr2 | else" Produces instructions:
6618 * EVAL expr Push result of "expr"
6619 * JUMP_IF_FALSE elseif
6620 * ... body ...
6621 * JUMP_ALWAYS end
6622 * elseif:
6623 * EVAL expr Push result of "expr"
6624 * JUMP_IF_FALSE else
6625 * ... body ...
6626 * JUMP_ALWAYS end
6627 * else:
6628 * ... body ...
6629 * end:
6630 */
6631 static char_u *
6632compile_if(char_u *arg, cctx_T *cctx)
6633{
6634 char_u *p = arg;
6635 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006636 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006637 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006638 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006639 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006640
Bram Moolenaara5565e42020-05-09 15:44:01 +02006641 CLEAR_FIELD(ppconst);
6642 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006643 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006644 clear_ppconst(&ppconst);
6645 return NULL;
6646 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006647 if (cctx->ctx_skip == SKIP_YES)
6648 clear_ppconst(&ppconst);
6649 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006650 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006651 int error = FALSE;
6652 int v;
6653
Bram Moolenaara5565e42020-05-09 15:44:01 +02006654 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006655 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006656 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006657 if (error)
6658 return NULL;
6659 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006660 }
6661 else
6662 {
6663 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006664 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006665 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006666 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006667 if (bool_on_stack(cctx) == FAIL)
6668 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006669 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006670
6671 scope = new_scope(cctx, IF_SCOPE);
6672 if (scope == NULL)
6673 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006674 scope->se_skip_save = skip_save;
6675 // "is_had_return" will be reset if any block does not end in :return
6676 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006677
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006678 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006679 {
6680 // "where" is set when ":elseif", "else" or ":endif" is found
6681 scope->se_u.se_if.is_if_label = instr->ga_len;
6682 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6683 }
6684 else
6685 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006686
6687 return p;
6688}
6689
6690 static char_u *
6691compile_elseif(char_u *arg, cctx_T *cctx)
6692{
6693 char_u *p = arg;
6694 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006695 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006696 isn_T *isn;
6697 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006698 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006699 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006700
6701 if (scope == NULL || scope->se_type != IF_SCOPE)
6702 {
6703 emsg(_(e_elseif_without_if));
6704 return NULL;
6705 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006706 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006707 if (!cctx->ctx_had_return)
6708 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006709
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006710 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006711 {
6712 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006713 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006714 return NULL;
6715 // previous "if" or "elseif" jumps here
6716 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6717 isn->isn_arg.jump.jump_where = instr->ga_len;
6718 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006719
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006720 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006721 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006722 if (cctx->ctx_skip == SKIP_YES)
6723 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006724 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006725 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006726 clear_ppconst(&ppconst);
6727 return NULL;
6728 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006729 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006730 if (scope->se_skip_save == SKIP_YES)
6731 clear_ppconst(&ppconst);
6732 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006733 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006734 int error = FALSE;
6735 int v;
6736
Bram Moolenaar7f141552020-05-09 17:35:53 +02006737 // The expression results in a constant.
6738 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006739 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6740 if (error)
6741 return NULL;
6742 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006743 clear_ppconst(&ppconst);
6744 scope->se_u.se_if.is_if_label = -1;
6745 }
6746 else
6747 {
6748 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006749 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006750 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006751 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006752 if (bool_on_stack(cctx) == FAIL)
6753 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006754
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006755 // "where" is set when ":elseif", "else" or ":endif" is found
6756 scope->se_u.se_if.is_if_label = instr->ga_len;
6757 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6758 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006759
6760 return p;
6761}
6762
6763 static char_u *
6764compile_else(char_u *arg, cctx_T *cctx)
6765{
6766 char_u *p = arg;
6767 garray_T *instr = &cctx->ctx_instr;
6768 isn_T *isn;
6769 scope_T *scope = cctx->ctx_scope;
6770
6771 if (scope == NULL || scope->se_type != IF_SCOPE)
6772 {
6773 emsg(_(e_else_without_if));
6774 return NULL;
6775 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006776 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006777 if (!cctx->ctx_had_return)
6778 scope->se_u.se_if.is_had_return = FALSE;
6779 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006780
Bram Moolenaarefd88552020-06-18 20:50:10 +02006781 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006782 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006783 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006784 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006785 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006786 if (!cctx->ctx_had_return
6787 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6788 JUMP_ALWAYS, cctx) == FAIL)
6789 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006790 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006791
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006792 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006793 {
6794 if (scope->se_u.se_if.is_if_label >= 0)
6795 {
6796 // previous "if" or "elseif" jumps here
6797 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6798 isn->isn_arg.jump.jump_where = instr->ga_len;
6799 scope->se_u.se_if.is_if_label = -1;
6800 }
6801 }
6802
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006803 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006804 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6805 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006806
6807 return p;
6808}
6809
6810 static char_u *
6811compile_endif(char_u *arg, cctx_T *cctx)
6812{
6813 scope_T *scope = cctx->ctx_scope;
6814 ifscope_T *ifscope;
6815 garray_T *instr = &cctx->ctx_instr;
6816 isn_T *isn;
6817
6818 if (scope == NULL || scope->se_type != IF_SCOPE)
6819 {
6820 emsg(_(e_endif_without_if));
6821 return NULL;
6822 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006823 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006824 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006825 if (!cctx->ctx_had_return)
6826 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006827
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006828 if (scope->se_u.se_if.is_if_label >= 0)
6829 {
6830 // previous "if" or "elseif" jumps here
6831 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6832 isn->isn_arg.jump.jump_where = instr->ga_len;
6833 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006834 // Fill in the "end" label in jumps at the end of the blocks.
6835 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006836 cctx->ctx_skip = scope->se_skip_save;
6837
6838 // If all the blocks end in :return and there is an :else then the
6839 // had_return flag is set.
6840 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006841
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006842 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006843 return arg;
6844}
6845
6846/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01006847 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006848 *
6849 * Produces instructions:
6850 * PUSHNR -1
6851 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01006852 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006853 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6854 * - if beyond end, jump to "end"
6855 * - otherwise get item from list and push it
6856 * STORE var Store item in "var"
6857 * ... body ...
6858 * JUMP top Jump back to repeat
6859 * end: DROP Drop the result of "expr"
6860 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006861 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
6862 * UNPACK 2 Split item in 2
6863 * STORE var1 Store item in "var1"
6864 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006865 */
6866 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006867compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006868{
Bram Moolenaar792f7862020-11-23 08:31:18 +01006869 char_u *arg;
6870 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006871 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006872 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006873 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006874 int var_count = 0;
6875 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006876 size_t varlen;
6877 garray_T *instr = &cctx->ctx_instr;
6878 garray_T *stack = &cctx->ctx_type_stack;
6879 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006880 lvar_T *loop_lvar; // loop iteration variable
6881 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006882 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006883 type_T *item_type = &t_any;
6884 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006885
Bram Moolenaar792f7862020-11-23 08:31:18 +01006886 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
6887 if (var_count == 0)
6888 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006889
6890 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006891 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006892 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6893 return NULL;
6894 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006895 {
6896 emsg(_(e_missing_in));
6897 return NULL;
6898 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006899 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006900 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6901 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006902
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006903 scope = new_scope(cctx, FOR_SCOPE);
6904 if (scope == NULL)
6905 return NULL;
6906
Bram Moolenaar792f7862020-11-23 08:31:18 +01006907 // Reserve a variable to store the loop iteration counter and initialize it
6908 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006909 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6910 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006911 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006912 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006913 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006914 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006915 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006916 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006917
6918 // compile "expr", it remains on the stack until "endfor"
6919 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006920 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006921 {
6922 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006923 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006924 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006925 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006926
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006927 // Now that we know the type of "var", check that it is a list, now or at
6928 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006929 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01006930 if (need_type(vartype, &t_list_any, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006931 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006932 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006933 return NULL;
6934 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006935
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006936 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006937 {
6938 if (var_count == 1)
6939 item_type = vartype->tt_member;
6940 else if (vartype->tt_member->tt_type == VAR_LIST
6941 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
6942 item_type = vartype->tt_member->tt_member;
6943 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006944
6945 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006946 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006947 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006948
Bram Moolenaar792f7862020-11-23 08:31:18 +01006949 arg = arg_start;
6950 if (var_count > 1)
6951 {
6952 generate_UNPACK(cctx, var_count, semicolon);
6953 arg = skipwhite(arg + 1); // skip white after '['
6954
6955 // the list item is replaced by a number of items
6956 if (ga_grow(stack, var_count - 1) == FAIL)
6957 {
6958 drop_scope(cctx);
6959 return NULL;
6960 }
6961 --stack->ga_len;
6962 for (idx = 0; idx < var_count; ++idx)
6963 {
6964 ((type_T **)stack->ga_data)[stack->ga_len] =
6965 (semicolon && idx == 0) ? vartype : item_type;
6966 ++stack->ga_len;
6967 }
6968 }
6969
6970 for (idx = 0; idx < var_count; ++idx)
6971 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006972 assign_dest_T dest = dest_local;
6973 int opt_flags = 0;
6974 int vimvaridx = -1;
6975 type_T *type = &t_any;
6976
6977 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006978 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006979 name = vim_strnsave(arg, varlen);
6980 if (name == NULL)
6981 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006982
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006983 // TODO: script var not supported?
6984 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
6985 &vimvaridx, &type, cctx) == FAIL)
6986 goto failed;
6987 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006988 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006989 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
6990 0, 0, type, name) == FAIL)
6991 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006992 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006993 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006994 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01006995 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006996 {
6997 semsg(_(e_variable_already_declared), arg);
6998 goto failed;
6999 }
7000
Bram Moolenaarea870692020-12-02 14:24:30 +01007001 if (STRNCMP(name, "s:", 2) == 0)
7002 {
7003 semsg(_(e_cannot_declare_script_variable_in_function), name);
7004 goto failed;
7005 }
7006
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007007 // Reserve a variable to store "var".
7008 // TODO: check for type
7009 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
7010 if (var_lvar == NULL)
7011 // out of memory or used as an argument
7012 goto failed;
7013
7014 if (semicolon && idx == var_count - 1)
7015 var_lvar->lv_type = vartype;
7016 else
7017 var_lvar->lv_type = item_type;
7018 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7019 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007020
7021 if (*p == ',' || *p == ';')
7022 ++p;
7023 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007024 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007025 }
7026
7027 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007028
7029failed:
7030 vim_free(name);
7031 drop_scope(cctx);
7032 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007033}
7034
7035/*
7036 * compile "endfor"
7037 */
7038 static char_u *
7039compile_endfor(char_u *arg, cctx_T *cctx)
7040{
7041 garray_T *instr = &cctx->ctx_instr;
7042 scope_T *scope = cctx->ctx_scope;
7043 forscope_T *forscope;
7044 isn_T *isn;
7045
7046 if (scope == NULL || scope->se_type != FOR_SCOPE)
7047 {
7048 emsg(_(e_for));
7049 return NULL;
7050 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007051 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007052 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007053 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007054
7055 // At end of ":for" scope jump back to the FOR instruction.
7056 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7057
7058 // Fill in the "end" label in the FOR statement so it can jump here
7059 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7060 isn->isn_arg.forloop.for_end = instr->ga_len;
7061
7062 // Fill in the "end" label any BREAK statements
7063 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
7064
7065 // Below the ":for" scope drop the "expr" list from the stack.
7066 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7067 return NULL;
7068
7069 vim_free(scope);
7070
7071 return arg;
7072}
7073
7074/*
7075 * compile "while expr"
7076 *
7077 * Produces instructions:
7078 * top: EVAL expr Push result of "expr"
7079 * JUMP_IF_FALSE end jump if false
7080 * ... body ...
7081 * JUMP top Jump back to repeat
7082 * end:
7083 *
7084 */
7085 static char_u *
7086compile_while(char_u *arg, cctx_T *cctx)
7087{
7088 char_u *p = arg;
7089 garray_T *instr = &cctx->ctx_instr;
7090 scope_T *scope;
7091
7092 scope = new_scope(cctx, WHILE_SCOPE);
7093 if (scope == NULL)
7094 return NULL;
7095
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007096 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007097
7098 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007099 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007100 return NULL;
7101
Bram Moolenaar13106602020-10-04 16:06:05 +02007102 if (bool_on_stack(cctx) == FAIL)
7103 return FAIL;
7104
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007105 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007106 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007107 JUMP_IF_FALSE, cctx) == FAIL)
7108 return FAIL;
7109
7110 return p;
7111}
7112
7113/*
7114 * compile "endwhile"
7115 */
7116 static char_u *
7117compile_endwhile(char_u *arg, cctx_T *cctx)
7118{
7119 scope_T *scope = cctx->ctx_scope;
7120
7121 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7122 {
7123 emsg(_(e_while));
7124 return NULL;
7125 }
7126 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007127 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007128
7129 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007130 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007131
7132 // Fill in the "end" label in the WHILE statement so it can jump here.
7133 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007134 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007135
7136 vim_free(scope);
7137
7138 return arg;
7139}
7140
7141/*
7142 * compile "continue"
7143 */
7144 static char_u *
7145compile_continue(char_u *arg, cctx_T *cctx)
7146{
7147 scope_T *scope = cctx->ctx_scope;
7148
7149 for (;;)
7150 {
7151 if (scope == NULL)
7152 {
7153 emsg(_(e_continue));
7154 return NULL;
7155 }
7156 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7157 break;
7158 scope = scope->se_outer;
7159 }
7160
7161 // Jump back to the FOR or WHILE instruction.
7162 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007163 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
7164 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007165 return arg;
7166}
7167
7168/*
7169 * compile "break"
7170 */
7171 static char_u *
7172compile_break(char_u *arg, cctx_T *cctx)
7173{
7174 scope_T *scope = cctx->ctx_scope;
7175 endlabel_T **el;
7176
7177 for (;;)
7178 {
7179 if (scope == NULL)
7180 {
7181 emsg(_(e_break));
7182 return NULL;
7183 }
7184 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7185 break;
7186 scope = scope->se_outer;
7187 }
7188
7189 // Jump to the end of the FOR or WHILE loop.
7190 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007191 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007192 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007193 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007194 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7195 return FAIL;
7196
7197 return arg;
7198}
7199
7200/*
7201 * compile "{" start of block
7202 */
7203 static char_u *
7204compile_block(char_u *arg, cctx_T *cctx)
7205{
7206 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7207 return NULL;
7208 return skipwhite(arg + 1);
7209}
7210
7211/*
7212 * compile end of block: drop one scope
7213 */
7214 static void
7215compile_endblock(cctx_T *cctx)
7216{
7217 scope_T *scope = cctx->ctx_scope;
7218
7219 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007220 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007221 vim_free(scope);
7222}
7223
7224/*
7225 * compile "try"
7226 * Creates a new scope for the try-endtry, pointing to the first catch and
7227 * finally.
7228 * Creates another scope for the "try" block itself.
7229 * TRY instruction sets up exception handling at runtime.
7230 *
7231 * "try"
7232 * TRY -> catch1, -> finally push trystack entry
7233 * ... try block
7234 * "throw {exception}"
7235 * EVAL {exception}
7236 * THROW create exception
7237 * ... try block
7238 * " catch {expr}"
7239 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007240 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007241 * EVAL {expr}
7242 * MATCH
7243 * JUMP nomatch -> catch2
7244 * CATCH remove exception
7245 * ... catch block
7246 * " catch"
7247 * JUMP -> finally
7248 * catch2: CATCH remove exception
7249 * ... catch block
7250 * " finally"
7251 * finally:
7252 * ... finally block
7253 * " endtry"
7254 * ENDTRY pop trystack entry, may rethrow
7255 */
7256 static char_u *
7257compile_try(char_u *arg, cctx_T *cctx)
7258{
7259 garray_T *instr = &cctx->ctx_instr;
7260 scope_T *try_scope;
7261 scope_T *scope;
7262
7263 // scope that holds the jumps that go to catch/finally/endtry
7264 try_scope = new_scope(cctx, TRY_SCOPE);
7265 if (try_scope == NULL)
7266 return NULL;
7267
Bram Moolenaar69f70502021-01-01 16:10:46 +01007268 if (cctx->ctx_skip != SKIP_YES)
7269 {
7270 // "catch" is set when the first ":catch" is found.
7271 // "finally" is set when ":finally" or ":endtry" is found
7272 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
7273 if (generate_instr(cctx, ISN_TRY) == NULL)
7274 return NULL;
7275 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007276
7277 // scope for the try block itself
7278 scope = new_scope(cctx, BLOCK_SCOPE);
7279 if (scope == NULL)
7280 return NULL;
7281
7282 return arg;
7283}
7284
7285/*
7286 * compile "catch {expr}"
7287 */
7288 static char_u *
7289compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7290{
7291 scope_T *scope = cctx->ctx_scope;
7292 garray_T *instr = &cctx->ctx_instr;
7293 char_u *p;
7294 isn_T *isn;
7295
7296 // end block scope from :try or :catch
7297 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7298 compile_endblock(cctx);
7299 scope = cctx->ctx_scope;
7300
7301 // Error if not in a :try scope
7302 if (scope == NULL || scope->se_type != TRY_SCOPE)
7303 {
7304 emsg(_(e_catch));
7305 return NULL;
7306 }
7307
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007308 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007309 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007310 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007311 return NULL;
7312 }
7313
Bram Moolenaar69f70502021-01-01 16:10:46 +01007314 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007315 {
Bram Moolenaar69f70502021-01-01 16:10:46 +01007316 // Jump from end of previous block to :finally or :endtry
7317 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7318 JUMP_ALWAYS, cctx) == FAIL)
7319 return NULL;
7320
7321 // End :try or :catch scope: set value in ISN_TRY instruction
7322 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7323 if (isn->isn_arg.try.try_catch == 0)
7324 isn->isn_arg.try.try_catch = instr->ga_len;
7325 if (scope->se_u.se_try.ts_catch_label != 0)
7326 {
7327 // Previous catch without match jumps here
7328 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7329 isn->isn_arg.jump.jump_where = instr->ga_len;
7330 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007331 }
7332
7333 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007334 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007335 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007336 scope->se_u.se_try.ts_caught_all = TRUE;
7337 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007338 }
7339 else
7340 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007341 char_u *end;
7342 char_u *pat;
7343 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007344 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007345 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007346
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007347 // Push v:exception, push {expr} and MATCH
7348 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7349
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007350 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007351 if (*end != *p)
7352 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007353 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007354 vim_free(tofree);
7355 return FAIL;
7356 }
7357 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007358 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007359 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007360 len = (int)(end - tofree);
7361 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007362 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007363 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007364 if (pat == NULL)
7365 return FAIL;
7366 if (generate_PUSHS(cctx, pat) == FAIL)
7367 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007368
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007369 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7370 return NULL;
7371
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007372 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007373 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7374 return NULL;
7375 }
7376
Bram Moolenaar69f70502021-01-01 16:10:46 +01007377 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007378 return NULL;
7379
7380 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7381 return NULL;
7382 return p;
7383}
7384
7385 static char_u *
7386compile_finally(char_u *arg, cctx_T *cctx)
7387{
7388 scope_T *scope = cctx->ctx_scope;
7389 garray_T *instr = &cctx->ctx_instr;
7390 isn_T *isn;
7391
7392 // end block scope from :try or :catch
7393 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7394 compile_endblock(cctx);
7395 scope = cctx->ctx_scope;
7396
7397 // Error if not in a :try scope
7398 if (scope == NULL || scope->se_type != TRY_SCOPE)
7399 {
7400 emsg(_(e_finally));
7401 return NULL;
7402 }
7403
7404 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007405 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007406 if (isn->isn_arg.try.try_finally != 0)
7407 {
7408 emsg(_(e_finally_dup));
7409 return NULL;
7410 }
7411
7412 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007413 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007414
Bram Moolenaar585fea72020-04-02 22:33:21 +02007415 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007416 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007417 {
7418 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007419 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007420 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007421 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007422 }
7423
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007424 // TODO: set index in ts_finally_label jumps
7425
7426 return arg;
7427}
7428
7429 static char_u *
7430compile_endtry(char_u *arg, cctx_T *cctx)
7431{
7432 scope_T *scope = cctx->ctx_scope;
7433 garray_T *instr = &cctx->ctx_instr;
7434 isn_T *isn;
7435
7436 // end block scope from :catch or :finally
7437 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7438 compile_endblock(cctx);
7439 scope = cctx->ctx_scope;
7440
7441 // Error if not in a :try scope
7442 if (scope == NULL || scope->se_type != TRY_SCOPE)
7443 {
7444 if (scope == NULL)
7445 emsg(_(e_no_endtry));
7446 else if (scope->se_type == WHILE_SCOPE)
7447 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007448 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007449 emsg(_(e_endfor));
7450 else
7451 emsg(_(e_endif));
7452 return NULL;
7453 }
7454
Bram Moolenaar69f70502021-01-01 16:10:46 +01007455 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007456 {
Bram Moolenaar69f70502021-01-01 16:10:46 +01007457 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7458 if (isn->isn_arg.try.try_catch == 0
7459 && isn->isn_arg.try.try_finally == 0)
7460 {
7461 emsg(_(e_missing_catch_or_finally));
7462 return NULL;
7463 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007464
Bram Moolenaar69f70502021-01-01 16:10:46 +01007465 // Fill in the "end" label in jumps at the end of the blocks, if not
7466 // done by ":finally".
7467 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007468
Bram Moolenaar69f70502021-01-01 16:10:46 +01007469 // End :catch or :finally scope: set value in ISN_TRY instruction
7470 if (isn->isn_arg.try.try_catch == 0)
7471 isn->isn_arg.try.try_catch = instr->ga_len;
7472 if (isn->isn_arg.try.try_finally == 0)
7473 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007474
Bram Moolenaar69f70502021-01-01 16:10:46 +01007475 if (scope->se_u.se_try.ts_catch_label != 0)
7476 {
7477 // Last catch without match jumps here
7478 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7479 isn->isn_arg.jump.jump_where = instr->ga_len;
7480 }
Bram Moolenaare8593122020-07-18 15:17:02 +02007481 }
7482
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007483 compile_endblock(cctx);
7484
Bram Moolenaar69f70502021-01-01 16:10:46 +01007485 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_ENDTRY) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007486 return NULL;
7487 return arg;
7488}
7489
7490/*
7491 * compile "throw {expr}"
7492 */
7493 static char_u *
7494compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7495{
7496 char_u *p = skipwhite(arg);
7497
Bram Moolenaara5565e42020-05-09 15:44:01 +02007498 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007499 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01007500 if (cctx->ctx_skip == SKIP_YES)
7501 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007502 if (may_generate_2STRING(-1, cctx) == FAIL)
7503 return NULL;
7504 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7505 return NULL;
7506
7507 return p;
7508}
7509
7510/*
7511 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007512 * compile "echomsg expr"
7513 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007514 * compile "execute expr"
7515 */
7516 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007517compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007518{
7519 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007520 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007521 int count = 0;
7522
7523 for (;;)
7524 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007525 if (ends_excmd2(prev, p))
7526 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007527 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007528 return NULL;
7529 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007530 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007531 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007532 }
7533
Bram Moolenaare4984292020-12-13 14:19:25 +01007534 if (count > 0)
7535 {
7536 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7537 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7538 else if (cmdidx == CMD_execute)
7539 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7540 else if (cmdidx == CMD_echomsg)
7541 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7542 else
7543 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7544 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007545 return p;
7546}
7547
7548/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007549 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01007550 * instruction to compute it and return OK.
7551 * Otherwise return FAIL, the caller must deal with any range.
7552 */
7553 static int
7554compile_variable_range(exarg_T *eap, cctx_T *cctx)
7555{
7556 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7557 char_u *p = skipdigits(eap->cmd);
7558
7559 if (p == range_end)
7560 return FAIL;
7561 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7562}
7563
7564/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007565 * :put r
7566 * :put ={expr}
7567 */
7568 static char_u *
7569compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7570{
7571 char_u *line = arg;
7572 linenr_T lnum;
7573 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007574 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007575
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007576 eap->regname = *line;
7577
7578 if (eap->regname == '=')
7579 {
7580 char_u *p = line + 1;
7581
7582 if (compile_expr0(&p, cctx) == FAIL)
7583 return NULL;
7584 line = p;
7585 }
7586 else if (eap->regname != NUL)
7587 ++line;
7588
Bram Moolenaar08597872020-12-10 19:43:40 +01007589 if (compile_variable_range(eap, cctx) == OK)
7590 {
7591 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
7592 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007593 else
Bram Moolenaar08597872020-12-10 19:43:40 +01007594 {
7595 // Either no range or a number.
7596 // "errormsg" will not be set because the range is ADDR_LINES.
7597 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01007598 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01007599 return NULL;
7600 if (eap->addr_count == 0)
7601 lnum = -1;
7602 else
7603 lnum = eap->line2;
7604 if (above)
7605 --lnum;
7606 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007607
7608 generate_PUT(cctx, eap->regname, lnum);
7609 return line;
7610}
7611
7612/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007613 * A command that is not compiled, execute with legacy code.
7614 */
7615 static char_u *
7616compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7617{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007618 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007619 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007620 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007621
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007622 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007623 goto theend;
7624
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007625 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007626 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007627 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007628 int usefilter = FALSE;
7629
7630 has_expr = argt & (EX_XFILE | EX_EXPAND);
7631
7632 // If the command can be followed by a bar, find the bar and truncate
7633 // it, so that the following command can be compiled.
7634 // The '|' is overwritten with a NUL, it is put back below.
7635 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7636 && *eap->arg == '!')
7637 // :w !filter or :r !filter or :r! filter
7638 usefilter = TRUE;
7639 if ((argt & EX_TRLBAR) && !usefilter)
7640 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007641 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007642 separate_nextcmd(eap);
7643 if (eap->nextcmd != NULL)
7644 nextcmd = eap->nextcmd;
7645 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01007646 else if (eap->cmdidx == CMD_wincmd)
7647 {
7648 p = eap->arg;
7649 if (*p != NUL)
7650 ++p;
7651 if (*p == 'g' || *p == Ctrl_G)
7652 ++p;
7653 p = skipwhite(p);
7654 if (*p == '|')
7655 {
7656 *p = NUL;
7657 nextcmd = p + 1;
7658 }
7659 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007660 }
7661
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007662 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
7663 {
7664 // expand filename in "syntax include [@group] filename"
7665 has_expr = TRUE;
7666 eap->arg = skipwhite(eap->arg + 7);
7667 if (*eap->arg == '@')
7668 eap->arg = skiptowhite(eap->arg);
7669 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007670
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007671 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
7672 && STRLEN(eap->arg) > 4)
7673 {
7674 int delim = *eap->arg;
7675
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007676 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007677 if (*p == delim)
7678 {
7679 eap->arg = p + 1;
7680 has_expr = TRUE;
7681 }
7682 }
7683
Bram Moolenaarecac5912021-01-05 19:23:28 +01007684 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
7685 {
7686 // TODO: should only expand when appropriate for the command
7687 eap->arg = skiptowhite(eap->arg);
7688 has_expr = TRUE;
7689 }
7690
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007691 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007692 {
7693 int count = 0;
7694 char_u *start = skipwhite(line);
7695
7696 // :cmd xxx`=expr1`yyy`=expr2`zzz
7697 // PUSHS ":cmd xxx"
7698 // eval expr1
7699 // PUSHS "yyy"
7700 // eval expr2
7701 // PUSHS "zzz"
7702 // EXECCONCAT 5
7703 for (;;)
7704 {
7705 if (p > start)
7706 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007707 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007708 ++count;
7709 }
7710 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007711 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007712 return NULL;
7713 may_generate_2STRING(-1, cctx);
7714 ++count;
7715 p = skipwhite(p);
7716 if (*p != '`')
7717 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007718 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007719 return NULL;
7720 }
7721 start = p + 1;
7722
7723 p = (char_u *)strstr((char *)start, "`=");
7724 if (p == NULL)
7725 {
7726 if (*skipwhite(start) != NUL)
7727 {
7728 generate_PUSHS(cctx, vim_strsave(start));
7729 ++count;
7730 }
7731 break;
7732 }
7733 }
7734 generate_EXECCONCAT(cctx, count);
7735 }
7736 else
7737 generate_EXEC(cctx, line);
7738
7739theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007740 if (*nextcmd != NUL)
7741 {
7742 // the parser expects a pointer to the bar, put it back
7743 --nextcmd;
7744 *nextcmd = '|';
7745 }
7746
7747 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007748}
7749
7750/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007751 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007752 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007753 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007754 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007755add_def_function(ufunc_T *ufunc)
7756{
7757 dfunc_T *dfunc;
7758
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007759 if (def_functions.ga_len == 0)
7760 {
7761 // The first position is not used, so that a zero uf_dfunc_idx means it
7762 // wasn't set.
7763 if (ga_grow(&def_functions, 1) == FAIL)
7764 return FAIL;
7765 ++def_functions.ga_len;
7766 }
7767
Bram Moolenaar09689a02020-05-09 22:50:08 +02007768 // Add the function to "def_functions".
7769 if (ga_grow(&def_functions, 1) == FAIL)
7770 return FAIL;
7771 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7772 CLEAR_POINTER(dfunc);
7773 dfunc->df_idx = def_functions.ga_len;
7774 ufunc->uf_dfunc_idx = dfunc->df_idx;
7775 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01007776 dfunc->df_name = vim_strsave(ufunc->uf_name);
7777 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007778 ++def_functions.ga_len;
7779 return OK;
7780}
7781
7782/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007783 * After ex_function() has collected all the function lines: parse and compile
7784 * the lines into instructions.
7785 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007786 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
7787 * the return statement (used for lambda). When uf_ret_type is already set
7788 * then check that it matches.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007789 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007790 * This can be used recursively through compile_lambda(), which may reallocate
7791 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007792 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007793 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007794 int
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007795compile_def_function(ufunc_T *ufunc, int check_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007796{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007797 char_u *line = NULL;
7798 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007799 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007800 cctx_T cctx;
7801 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007802 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007803 int ret = FAIL;
7804 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007805 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007806 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007807 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007808
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007809 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01007810 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007811 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007812 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007813 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7814 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01007815 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007816 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007817 else
7818 {
7819 if (add_def_function(ufunc) == FAIL)
7820 return FAIL;
7821 new_def_function = TRUE;
7822 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007823
Bram Moolenaar985116a2020-07-12 17:31:09 +02007824 ufunc->uf_def_status = UF_COMPILING;
7825
Bram Moolenaara80faa82020-04-12 19:37:17 +02007826 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007827 cctx.ctx_ufunc = ufunc;
7828 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007829 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007830 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7831 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7832 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7833 cctx.ctx_type_list = &ufunc->uf_type_list;
7834 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7835 instr = &cctx.ctx_instr;
7836
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007837 // Set the context to the function, it may be compiled when called from
7838 // another script. Set the script version to the most modern one.
7839 // The line number will be set in next_line_from_context().
7840 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007841 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7842
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007843 // Make sure error messages are OK.
7844 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7845 if (do_estack_push)
7846 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007847 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007848
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007849 if (ufunc->uf_def_args.ga_len > 0)
7850 {
7851 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007852 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007853 int i;
7854 char_u *arg;
7855 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007856 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007857
7858 // Produce instructions for the default values of optional arguments.
7859 // Store the instruction index in uf_def_arg_idx[] so that we know
7860 // where to start when the function is called, depending on the number
7861 // of arguments.
7862 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7863 if (ufunc->uf_def_arg_idx == NULL)
7864 goto erret;
7865 for (i = 0; i < count; ++i)
7866 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007867 garray_T *stack = &cctx.ctx_type_stack;
7868 type_T *val_type;
7869 int arg_idx = first_def_arg + i;
7870
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007871 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7872 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007873 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007874 goto erret;
7875
7876 // If no type specified use the type of the default value.
7877 // Otherwise check that the default value type matches the
7878 // specified type.
7879 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7880 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007881 {
7882 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007883 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007884 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007885 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7886 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007887 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007888
7889 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007890 goto erret;
7891 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007892 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007893
7894 if (did_set_arg_type)
7895 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007896 }
7897
7898 /*
7899 * Loop over all the lines of the function and generate instructions.
7900 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007901 for (;;)
7902 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007903 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007904 int starts_with_colon = FALSE;
7905 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007906 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007907
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007908 // Bail out on the first error to avoid a flood of errors and report
7909 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007910 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007911 goto erret;
7912
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007913 if (line != NULL && *line == '|')
7914 // the line continues after a '|'
7915 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007916 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007917 && !(*line == '#' && (line == cctx.ctx_line_start
7918 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007919 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007920 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007921 goto erret;
7922 }
7923 else
7924 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007925 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007926 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007927 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007928 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007929 }
7930
Bram Moolenaara80faa82020-04-12 19:37:17 +02007931 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007932 ea.cmdlinep = &line;
7933 ea.cmd = skipwhite(line);
7934
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007935 // Some things can be recognized by the first character.
7936 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007937 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007938 case '#':
Bram Moolenaare0de1712020-12-02 17:36:54 +01007939 // "#" starts a comment
7940 line = (char_u *)"";
7941 continue;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007942
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007943 case '}':
7944 {
7945 // "}" ends a block scope
7946 scopetype_T stype = cctx.ctx_scope == NULL
7947 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007948
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007949 if (stype == BLOCK_SCOPE)
7950 {
7951 compile_endblock(&cctx);
7952 line = ea.cmd;
7953 }
7954 else
7955 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007956 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007957 goto erret;
7958 }
7959 if (line != NULL)
7960 line = skipwhite(ea.cmd + 1);
7961 continue;
7962 }
7963
7964 case '{':
7965 // "{" starts a block scope
7966 // "{'a': 1}->func() is something else
7967 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7968 {
7969 line = compile_block(ea.cmd, &cctx);
7970 continue;
7971 }
7972 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007973 }
7974
7975 /*
7976 * COMMAND MODIFIERS
7977 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007978 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007979 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7980 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007981 {
7982 if (errormsg != NULL)
7983 goto erret;
7984 // empty line or comment
7985 line = (char_u *)"";
7986 continue;
7987 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007988 generate_cmdmods(&cctx, &local_cmdmod);
7989 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007990
Bram Moolenaare88c8e82020-11-01 17:03:37 +01007991 // Check if there was a colon after the last command modifier or before
7992 // the current position.
7993 for (p = ea.cmd; p >= line; --p)
7994 {
7995 if (*p == ':')
7996 starts_with_colon = TRUE;
7997 if (p < ea.cmd && !VIM_ISWHITE(*p))
7998 break;
7999 }
8000
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008001 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008002 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008003 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008004 {
8005 if (*ea.cmd == '(')
8006 // not for "call()"
8007 ea.cmd = p;
8008 else
8009 ea.cmd = skipwhite(ea.cmd);
8010 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008011
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008012 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008013 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008014 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008015
Bram Moolenaar17126b12021-01-07 22:03:02 +01008016 // Check for assignment after command modifiers.
8017 assign = may_compile_assignment(&ea, &line, &cctx);
8018 if (assign == OK)
8019 goto nextline;
8020 if (assign == FAIL)
8021 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008022 }
8023
8024 /*
8025 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008026 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008027 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008028 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008029 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008030 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008031 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008032 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008033 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008034 if (!starts_with_colon)
8035 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008036 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008037 goto erret;
8038 }
8039 if (ends_excmd2(line, ea.cmd))
8040 {
8041 // A range without a command: jump to the line.
8042 // TODO: compile to a more efficient command, possibly
8043 // calling parse_cmd_address().
8044 ea.cmdidx = CMD_SIZE;
8045 line = compile_exec(line, &ea, &cctx);
8046 goto nextline;
8047 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008048 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008049 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008050 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +01008051 : (int (*)(char_u *, size_t, void *, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008052 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008053
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008054 if (p == NULL)
8055 {
8056 if (cctx.ctx_skip != SKIP_YES)
8057 emsg(_(e_ambiguous_use_of_user_defined_command));
8058 goto erret;
8059 }
8060
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008061 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8062 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008063 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008064 {
8065 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008066 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008067 }
8068
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008069 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008070 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008071 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008072 // CMD_var cannot happen, compile_assignment() above would be
8073 // used. Most likely an assignment to a non-existing variable.
8074 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008075 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008076 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008077 }
8078
Bram Moolenaar3988f642020-08-27 22:43:03 +02008079 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008080 && ea.cmdidx != CMD_elseif
8081 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008082 && ea.cmdidx != CMD_endif
8083 && ea.cmdidx != CMD_endfor
8084 && ea.cmdidx != CMD_endwhile
8085 && ea.cmdidx != CMD_catch
8086 && ea.cmdidx != CMD_finally
8087 && ea.cmdidx != CMD_endtry)
8088 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008089 emsg(_(e_unreachable_code_after_return));
8090 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008091 }
8092
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008093 p = skipwhite(p);
8094 if (ea.cmdidx != CMD_SIZE
8095 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8096 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008097 if (ea.cmdidx >= 0)
8098 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008099 if ((ea.argt & EX_BANG) && *p == '!')
8100 {
8101 ea.forceit = TRUE;
8102 p = skipwhite(p + 1);
8103 }
8104 }
8105
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008106 switch (ea.cmdidx)
8107 {
8108 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008109 ea.arg = p;
8110 line = compile_nested_function(&ea, &cctx);
8111 break;
8112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008113 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008114 // TODO: should we allow this, e.g. to declare a global
8115 // function?
8116 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008117 goto erret;
8118
8119 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008120 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008121 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008122 break;
8123
8124 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008125 emsg(_(e_cannot_use_let_in_vim9_script));
8126 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008127 case CMD_var:
8128 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008129 case CMD_const:
8130 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008131 if (line == p)
8132 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008133 break;
8134
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008135 case CMD_unlet:
8136 case CMD_unlockvar:
8137 case CMD_lockvar:
8138 line = compile_unletlock(p, &ea, &cctx);
8139 break;
8140
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008141 case CMD_import:
8142 line = compile_import(p, &cctx);
8143 break;
8144
8145 case CMD_if:
8146 line = compile_if(p, &cctx);
8147 break;
8148 case CMD_elseif:
8149 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008150 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008151 break;
8152 case CMD_else:
8153 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008154 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008155 break;
8156 case CMD_endif:
8157 line = compile_endif(p, &cctx);
8158 break;
8159
8160 case CMD_while:
8161 line = compile_while(p, &cctx);
8162 break;
8163 case CMD_endwhile:
8164 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008165 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008166 break;
8167
8168 case CMD_for:
8169 line = compile_for(p, &cctx);
8170 break;
8171 case CMD_endfor:
8172 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008173 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008174 break;
8175 case CMD_continue:
8176 line = compile_continue(p, &cctx);
8177 break;
8178 case CMD_break:
8179 line = compile_break(p, &cctx);
8180 break;
8181
8182 case CMD_try:
8183 line = compile_try(p, &cctx);
8184 break;
8185 case CMD_catch:
8186 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008187 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008188 break;
8189 case CMD_finally:
8190 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008191 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008192 break;
8193 case CMD_endtry:
8194 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008195 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008196 break;
8197 case CMD_throw:
8198 line = compile_throw(p, &cctx);
8199 break;
8200
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008201 case CMD_eval:
8202 if (compile_expr0(&p, &cctx) == FAIL)
8203 goto erret;
8204
Bram Moolenaar3988f642020-08-27 22:43:03 +02008205 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008206 generate_instr_drop(&cctx, ISN_DROP, 1);
8207
8208 line = skipwhite(p);
8209 break;
8210
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008211 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008212 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008213 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008214 case CMD_echomsg:
8215 case CMD_echoerr:
8216 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008217 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008218
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008219 case CMD_put:
8220 ea.cmd = cmd;
8221 line = compile_put(p, &ea, &cctx);
8222 break;
8223
Bram Moolenaar3988f642020-08-27 22:43:03 +02008224 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008225
Bram Moolenaarae616492020-07-28 20:07:27 +02008226 case CMD_append:
8227 case CMD_change:
8228 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008229 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008230 case CMD_xit:
8231 not_in_vim9(&ea);
8232 goto erret;
8233
Bram Moolenaar002262f2020-07-08 17:47:57 +02008234 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008235 if (cctx.ctx_skip != SKIP_YES)
8236 {
8237 semsg(_(e_invalid_command_str), ea.cmd);
8238 goto erret;
8239 }
8240 // We don't check for a next command here.
8241 line = (char_u *)"";
8242 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008243
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008244 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008245 if (cctx.ctx_skip == SKIP_YES)
8246 {
8247 // We don't check for a next command here.
8248 line = (char_u *)"";
8249 }
8250 else
8251 {
8252 // Not recognized, execute with do_cmdline_cmd().
8253 ea.arg = p;
8254 line = compile_exec(line, &ea, &cctx);
8255 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008256 break;
8257 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008258nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008259 if (line == NULL)
8260 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008261 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008262
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008263 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008264 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008265
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008266 if (cctx.ctx_type_stack.ga_len < 0)
8267 {
8268 iemsg("Type stack underflow");
8269 goto erret;
8270 }
8271 }
8272
8273 if (cctx.ctx_scope != NULL)
8274 {
8275 if (cctx.ctx_scope->se_type == IF_SCOPE)
8276 emsg(_(e_endif));
8277 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8278 emsg(_(e_endwhile));
8279 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8280 emsg(_(e_endfor));
8281 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008282 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008283 goto erret;
8284 }
8285
Bram Moolenaarefd88552020-06-18 20:50:10 +02008286 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008287 {
8288 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8289 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008290 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008291 goto erret;
8292 }
8293
8294 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008295 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008296 }
8297
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008298 {
8299 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8300 + ufunc->uf_dfunc_idx;
8301 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008302 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008303 dfunc->df_instr = instr->ga_data;
8304 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008305 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008306 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008307 if (cctx.ctx_outer_used)
8308 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008309 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008310 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008311
8312 ret = OK;
8313
8314erret:
8315 if (ret == FAIL)
8316 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008317 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008318 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8319 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008320
8321 for (idx = 0; idx < instr->ga_len; ++idx)
8322 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008323 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008324 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008325
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008326 // If using the last entry in the table and it was added above, we
8327 // might as well remove it.
8328 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008329 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008330 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008331 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008332 ufunc->uf_dfunc_idx = 0;
8333 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008334 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008335
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008336 while (cctx.ctx_scope != NULL)
8337 drop_scope(&cctx);
8338
Bram Moolenaar20431c92020-03-20 18:39:46 +01008339 // Don't execute this function body.
8340 ga_clear_strings(&ufunc->uf_lines);
8341
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008342 if (errormsg != NULL)
8343 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008344 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008345 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008346 }
8347
8348 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008349 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008350 if (do_estack_push)
8351 estack_pop();
8352
Bram Moolenaar20431c92020-03-20 18:39:46 +01008353 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008354 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008355 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008356 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008357}
8358
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008359 void
8360set_function_type(ufunc_T *ufunc)
8361{
8362 int varargs = ufunc->uf_va_name != NULL;
8363 int argcount = ufunc->uf_args.ga_len;
8364
8365 // Create a type for the function, with the return type and any
8366 // argument types.
8367 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8368 // The type is included in "tt_args".
8369 if (argcount > 0 || varargs)
8370 {
8371 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8372 argcount, &ufunc->uf_type_list);
8373 // Add argument types to the function type.
8374 if (func_type_add_arg_types(ufunc->uf_func_type,
8375 argcount + varargs,
8376 &ufunc->uf_type_list) == FAIL)
8377 return;
8378 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8379 ufunc->uf_func_type->tt_min_argcount =
8380 argcount - ufunc->uf_def_args.ga_len;
8381 if (ufunc->uf_arg_types == NULL)
8382 {
8383 int i;
8384
8385 // lambda does not have argument types.
8386 for (i = 0; i < argcount; ++i)
8387 ufunc->uf_func_type->tt_args[i] = &t_any;
8388 }
8389 else
8390 mch_memmove(ufunc->uf_func_type->tt_args,
8391 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8392 if (varargs)
8393 {
8394 ufunc->uf_func_type->tt_args[argcount] =
8395 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8396 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8397 }
8398 }
8399 else
8400 // No arguments, can use a predefined type.
8401 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8402 argcount, &ufunc->uf_type_list);
8403}
8404
8405
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008406/*
8407 * Delete an instruction, free what it contains.
8408 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008409 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008410delete_instr(isn_T *isn)
8411{
8412 switch (isn->isn_type)
8413 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008414 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008415 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008416 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008417 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008418 case ISN_LOADENV:
8419 case ISN_LOADG:
8420 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008421 case ISN_LOADT:
8422 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008423 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008424 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008425 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008426 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008427 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008428 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008429 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008430 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008431 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008432 case ISN_STOREW:
8433 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008434 vim_free(isn->isn_arg.string);
8435 break;
8436
8437 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008438 case ISN_STORES:
8439 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008440 break;
8441
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008442 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008443 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008444 vim_free(isn->isn_arg.unlet.ul_name);
8445 break;
8446
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008447 case ISN_STOREOPT:
8448 vim_free(isn->isn_arg.storeopt.so_name);
8449 break;
8450
8451 case ISN_PUSHBLOB: // push blob isn_arg.blob
8452 blob_unref(isn->isn_arg.blob);
8453 break;
8454
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008455 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008456#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008457 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008458#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008459 break;
8460
8461 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008462#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008463 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008464#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008465 break;
8466
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008467 case ISN_UCALL:
8468 vim_free(isn->isn_arg.ufunc.cuf_name);
8469 break;
8470
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008471 case ISN_FUNCREF:
8472 {
8473 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8474 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01008475 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008476
Bram Moolenaar077a4232020-12-22 18:33:27 +01008477 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
8478 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02008479 }
8480 break;
8481
8482 case ISN_DCALL:
8483 {
8484 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8485 + isn->isn_arg.dfunc.cdf_idx;
8486
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008487 if (dfunc->df_ufunc != NULL
8488 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008489 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008490 }
8491 break;
8492
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008493 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008494 {
8495 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8496 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8497
8498 if (ufunc != NULL)
8499 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008500 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02008501 func_ptr_unref(ufunc);
8502 }
8503
8504 vim_free(lambda);
8505 vim_free(isn->isn_arg.newfunc.nf_global);
8506 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008507 break;
8508
Bram Moolenaar5e654232020-09-16 15:22:00 +02008509 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01008510 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02008511 free_type(isn->isn_arg.type.ct_type);
8512 break;
8513
Bram Moolenaar02194d22020-10-24 23:08:38 +02008514 case ISN_CMDMOD:
8515 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8516 ->cmod_filter_regmatch.regprog);
8517 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8518 break;
8519
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008520 case ISN_LOADSCRIPT:
8521 case ISN_STORESCRIPT:
8522 vim_free(isn->isn_arg.script.scriptref);
8523 break;
8524
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008525 case ISN_2BOOL:
8526 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008527 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008528 case ISN_ADDBLOB:
8529 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008530 case ISN_ANYINDEX:
8531 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008532 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008533 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008534 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008535 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008536 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008537 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008538 case ISN_COMPAREANY:
8539 case ISN_COMPAREBLOB:
8540 case ISN_COMPAREBOOL:
8541 case ISN_COMPAREDICT:
8542 case ISN_COMPAREFLOAT:
8543 case ISN_COMPAREFUNC:
8544 case ISN_COMPARELIST:
8545 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008546 case ISN_COMPARESPECIAL:
8547 case ISN_COMPARESTRING:
8548 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008549 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008550 case ISN_DROP:
8551 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008552 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008553 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008554 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008555 case ISN_EXECCONCAT:
8556 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008557 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008558 case ISN_GETITEM:
8559 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008560 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008561 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008562 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008563 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008564 case ISN_LOADBDICT:
8565 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008566 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008567 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008568 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008569 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008570 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008571 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008572 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008573 case ISN_NEGATENR:
8574 case ISN_NEWDICT:
8575 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008576 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008577 case ISN_OPFLOAT:
8578 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008579 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008580 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008581 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008582 case ISN_PUSHF:
8583 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008584 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008585 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008586 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01008587 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008588 case ISN_SHUFFLE:
8589 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008590 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01008591 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008592 case ISN_STORENR:
8593 case ISN_STOREOUTER:
8594 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008595 case ISN_STOREV:
8596 case ISN_STRINDEX:
8597 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008598 case ISN_THROW:
8599 case ISN_TRY:
Bram Moolenaar752fc692021-01-04 21:57:11 +01008600 case ISN_UNLETINDEX:
Bram Moolenaar792f7862020-11-23 08:31:18 +01008601 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008602 // nothing allocated
8603 break;
8604 }
8605}
8606
8607/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008608 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01008609 */
8610 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008611delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008612{
8613 int idx;
8614
8615 ga_clear(&dfunc->df_def_args_isn);
8616
8617 if (dfunc->df_instr != NULL)
8618 {
8619 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
8620 delete_instr(dfunc->df_instr + idx);
8621 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008622 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008623 }
8624
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008625 if (mark_deleted)
8626 dfunc->df_deleted = TRUE;
8627 if (dfunc->df_ufunc != NULL)
8628 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008629}
8630
8631/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008632 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008633 * function, unless another user function still uses it.
8634 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008635 */
8636 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008637unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008638{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008639 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008640 {
8641 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8642 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008643
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008644 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008645 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008646 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008647 ufunc->uf_dfunc_idx = 0;
8648 if (dfunc->df_ufunc == ufunc)
8649 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008650 }
8651}
8652
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008653/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008654 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008655 */
8656 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008657link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008658{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008659 if (ufunc->uf_dfunc_idx > 0)
8660 {
8661 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8662 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008663
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008664 ++dfunc->df_refcount;
8665 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008666}
8667
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008668#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008669/*
8670 * Free all functions defined with ":def".
8671 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008672 void
8673free_def_functions(void)
8674{
Bram Moolenaar20431c92020-03-20 18:39:46 +01008675 int idx;
8676
8677 for (idx = 0; idx < def_functions.ga_len; ++idx)
8678 {
8679 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
8680
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008681 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008682 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008683 }
8684
8685 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008686}
8687#endif
8688
8689
8690#endif // FEAT_EVAL