blob: e972033f0bad5bb379ceac8a06ac6710a539445f [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 Moolenaarc5f59fa2021-01-21 12:34:14 +01004294 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4295 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4296 varnumber_T res = 0;
4297 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004298
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004299 // both are numbers: compute the result
4300 switch (*op)
4301 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004302 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004303 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004304 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004305 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004306 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004307 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004308 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004309 break;
4310 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004311 if (failed)
4312 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004313 tv1->vval.v_number = res;
4314 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004315 }
4316 else
4317 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004318 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004319 generate_two_op(cctx, op);
4320 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321 }
4322
4323 return OK;
4324}
4325
4326/*
4327 * + number addition
4328 * - number subtraction
4329 * .. string concatenation
4330 */
4331 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004332compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004333{
4334 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004335 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004337 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338
4339 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004340 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004341 return FAIL;
4342
4343 /*
4344 * Repeat computing, until no "+", "-" or ".." is following.
4345 */
4346 for (;;)
4347 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004348 op = may_peek_next_line(cctx, *arg, &next);
4349 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 break;
4351 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004352 if (next != NULL)
4353 {
4354 *arg = next_line_from_context(cctx, TRUE);
4355 op = skipwhite(*arg);
4356 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004358 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004359 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004360 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004361 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362 }
4363
Bram Moolenaare0de1712020-12-02 17:36:54 +01004364 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004365 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004367 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004368 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004369 return FAIL;
4370
Bram Moolenaara5565e42020-05-09 15:44:01 +02004371 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004372 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004373 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4374 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4375 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4376 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004378 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4379 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004380
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004381 // concat/subtract/add constant numbers
4382 if (*op == '+')
4383 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4384 else if (*op == '-')
4385 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4386 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004387 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004388 // concatenate constant strings
4389 char_u *s1 = tv1->vval.v_string;
4390 char_u *s2 = tv2->vval.v_string;
4391 size_t len1 = STRLEN(s1);
4392
4393 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4394 if (tv1->vval.v_string == NULL)
4395 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004396 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004397 return FAIL;
4398 }
4399 mch_memmove(tv1->vval.v_string, s1, len1);
4400 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004401 vim_free(s1);
4402 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004403 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004404 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004405 }
4406 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004407 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004408 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004409 if (*op == '.')
4410 {
4411 if (may_generate_2STRING(-2, cctx) == FAIL
4412 || may_generate_2STRING(-1, cctx) == FAIL)
4413 return FAIL;
4414 generate_instr_drop(cctx, ISN_CONCAT, 1);
4415 }
4416 else
4417 generate_two_op(cctx, op);
4418 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419 }
4420
4421 return OK;
4422}
4423
4424/*
4425 * expr5a == expr5b
4426 * expr5a =~ expr5b
4427 * expr5a != expr5b
4428 * expr5a !~ expr5b
4429 * expr5a > expr5b
4430 * expr5a >= expr5b
4431 * expr5a < expr5b
4432 * expr5a <= expr5b
4433 * expr5a is expr5b
4434 * expr5a isnot expr5b
4435 *
4436 * Produces instructions:
4437 * EVAL expr5a Push result of "expr5a"
4438 * EVAL expr5b Push result of "expr5b"
4439 * COMPARE one of the compare instructions
4440 */
4441 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004442compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004444 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004446 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004447 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004449 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450
4451 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004452 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004453 return FAIL;
4454
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004455 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004456 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004457
4458 /*
4459 * If there is a comparative operator, use it.
4460 */
4461 if (type != EXPR_UNKNOWN)
4462 {
4463 int ic = FALSE; // Default: do not ignore case
4464
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004465 if (next != NULL)
4466 {
4467 *arg = next_line_from_context(cctx, TRUE);
4468 p = skipwhite(*arg);
4469 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470 if (type_is && (p[len] == '?' || p[len] == '#'))
4471 {
4472 semsg(_(e_invexpr2), *arg);
4473 return FAIL;
4474 }
4475 // extra question mark appended: ignore case
4476 if (p[len] == '?')
4477 {
4478 ic = TRUE;
4479 ++len;
4480 }
4481 // extra '#' appended: match case (ignored)
4482 else if (p[len] == '#')
4483 ++len;
4484 // nothing appended: match case
4485
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004486 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004488 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004489 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490 }
4491
4492 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004493 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004494 return FAIL;
4495
Bram Moolenaara5565e42020-05-09 15:44:01 +02004496 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004497 return FAIL;
4498
Bram Moolenaara5565e42020-05-09 15:44:01 +02004499 if (ppconst->pp_used == ppconst_used + 2)
4500 {
4501 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4502 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4503 int ret;
4504
4505 // Both sides are a constant, compute the result now.
4506 // First check for a valid combination of types, this is more
4507 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004508 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004509 ret = FAIL;
4510 else
4511 {
4512 ret = typval_compare(tv1, tv2, type, ic);
4513 tv1->v_type = VAR_BOOL;
4514 tv1->vval.v_number = tv1->vval.v_number
4515 ? VVAL_TRUE : VVAL_FALSE;
4516 clear_tv(tv2);
4517 --ppconst->pp_used;
4518 }
4519 return ret;
4520 }
4521
4522 generate_ppconst(cctx, ppconst);
4523 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004524 }
4525
4526 return OK;
4527}
4528
Bram Moolenaar7f141552020-05-09 17:35:53 +02004529static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4530
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004531/*
4532 * Compile || or &&.
4533 */
4534 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004535compile_and_or(
4536 char_u **arg,
4537 cctx_T *cctx,
4538 char *op,
4539 ppconst_T *ppconst,
4540 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004541{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004542 char_u *next;
4543 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004544 int opchar = *op;
4545
4546 if (p[0] == opchar && p[1] == opchar)
4547 {
4548 garray_T *instr = &cctx->ctx_instr;
4549 garray_T end_ga;
4550
4551 /*
4552 * Repeat until there is no following "||" or "&&"
4553 */
4554 ga_init2(&end_ga, sizeof(int), 10);
4555 while (p[0] == opchar && p[1] == opchar)
4556 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004557 if (next != NULL)
4558 {
4559 *arg = next_line_from_context(cctx, TRUE);
4560 p = skipwhite(*arg);
4561 }
4562
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004563 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4564 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004565 semsg(_(e_white_space_required_before_and_after_str_at_str),
4566 op, *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004567 return FAIL;
4568 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004569
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004570 // TODO: use ppconst if the value is a constant and check
4571 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004572 generate_ppconst(cctx, ppconst);
4573
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004574 // Every part must evaluate to a bool.
4575 if (bool_on_stack(cctx) == FAIL)
4576 {
4577 ga_clear(&end_ga);
4578 return FAIL;
4579 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004580
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004581 if (ga_grow(&end_ga, 1) == FAIL)
4582 {
4583 ga_clear(&end_ga);
4584 return FAIL;
4585 }
4586 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4587 ++end_ga.ga_len;
4588 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004589 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004590
4591 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004592 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004593 {
4594 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004595 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004596 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004597
Bram Moolenaara5565e42020-05-09 15:44:01 +02004598 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4599 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600 {
4601 ga_clear(&end_ga);
4602 return FAIL;
4603 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004604
4605 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004606 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004607 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004609 // Every part must evaluate to a bool.
4610 if (bool_on_stack(cctx) == FAIL)
4611 {
4612 ga_clear(&end_ga);
4613 return FAIL;
4614 }
4615
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616 // Fill in the end label in all jumps.
4617 while (end_ga.ga_len > 0)
4618 {
4619 isn_T *isn;
4620
4621 --end_ga.ga_len;
4622 isn = ((isn_T *)instr->ga_data)
4623 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4624 isn->isn_arg.jump.jump_where = instr->ga_len;
4625 }
4626 ga_clear(&end_ga);
4627 }
4628
4629 return OK;
4630}
4631
4632/*
4633 * expr4a && expr4a && expr4a logical AND
4634 *
4635 * Produces instructions:
4636 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004637 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004638 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004640 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004641 * EVAL expr4c Push result of "expr4c"
4642 * end:
4643 */
4644 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004645compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004646{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004647 int ppconst_used = ppconst->pp_used;
4648
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004649 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004650 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004651 return FAIL;
4652
4653 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004654 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004655}
4656
4657/*
4658 * expr3a || expr3b || expr3c logical OR
4659 *
4660 * Produces instructions:
4661 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004662 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004663 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004664 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004665 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004666 * EVAL expr3c Push result of "expr3c"
4667 * end:
4668 */
4669 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004670compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004671{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004672 int ppconst_used = ppconst->pp_used;
4673
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004674 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004675 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004676 return FAIL;
4677
4678 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004679 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004680}
4681
4682/*
4683 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004685 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004686 * JUMP_IF_FALSE alt jump if false
4687 * EVAL expr1a
4688 * JUMP_ALWAYS end
4689 * alt: EVAL expr1b
4690 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004691 *
4692 * Toplevel expression: expr2 ?? expr1
4693 * Produces instructions:
4694 * EVAL expr2 Push result of "expr2"
4695 * JUMP_AND_KEEP_IF_TRUE end jump if true
4696 * EVAL expr1
4697 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698 */
4699 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004700compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004701{
4702 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004703 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004704 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705
Bram Moolenaar3988f642020-08-27 22:43:03 +02004706 // Ignore all kinds of errors when not producing code.
4707 if (cctx->ctx_skip == SKIP_YES)
4708 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004709 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004710 return OK;
4711 }
4712
Bram Moolenaar61a89812020-05-07 16:58:17 +02004713 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004714 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715 return FAIL;
4716
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004717 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004718 if (*p == '?')
4719 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004720 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721 garray_T *instr = &cctx->ctx_instr;
4722 garray_T *stack = &cctx->ctx_type_stack;
4723 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004724 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004725 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004726 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004727 int has_const_expr = FALSE;
4728 int const_value = FALSE;
4729 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004730
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004731 if (next != NULL)
4732 {
4733 *arg = next_line_from_context(cctx, TRUE);
4734 p = skipwhite(*arg);
4735 }
4736
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004737 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004738 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004739 semsg(_(e_white_space_required_before_and_after_str_at_str),
4740 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004741 return FAIL;
4742 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004743
Bram Moolenaara5565e42020-05-09 15:44:01 +02004744 if (ppconst->pp_used == ppconst_used + 1)
4745 {
4746 // the condition is a constant, we know whether the ? or the :
4747 // expression is to be evaluated.
4748 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004749 if (op_falsy)
4750 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4751 else
4752 {
4753 int error = FALSE;
4754
4755 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4756 &error);
4757 if (error)
4758 return FAIL;
4759 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004760 cctx->ctx_skip = save_skip == SKIP_YES ||
4761 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4762
4763 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4764 // "left ?? right" and "left" is truthy: produce "left"
4765 generate_ppconst(cctx, ppconst);
4766 else
4767 {
4768 clear_tv(&ppconst->pp_tv[ppconst_used]);
4769 --ppconst->pp_used;
4770 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004771 }
4772 else
4773 {
4774 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004775 if (op_falsy)
4776 end_idx = instr->ga_len;
4777 generate_JUMP(cctx, op_falsy
4778 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4779 if (op_falsy)
4780 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004781 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004782
4783 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01004784 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004785 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004786 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004787 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004788
Bram Moolenaara5565e42020-05-09 15:44:01 +02004789 if (!has_const_expr)
4790 {
4791 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004793 if (!op_falsy)
4794 {
4795 // remember the type and drop it
4796 --stack->ga_len;
4797 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004799 end_idx = instr->ga_len;
4800 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004801
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004802 // jump here from JUMP_IF_FALSE
4803 isn = ((isn_T *)instr->ga_data) + alt_idx;
4804 isn->isn_arg.jump.jump_where = instr->ga_len;
4805 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004806 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004807
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004808 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004809 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004810 // Check for the ":".
4811 p = may_peek_next_line(cctx, *arg, &next);
4812 if (*p != ':')
4813 {
4814 emsg(_(e_missing_colon));
4815 return FAIL;
4816 }
4817 if (next != NULL)
4818 {
4819 *arg = next_line_from_context(cctx, TRUE);
4820 p = skipwhite(*arg);
4821 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004822
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004823 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4824 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004825 semsg(_(e_white_space_required_before_and_after_str_at_str),
4826 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004827 return FAIL;
4828 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004829
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004830 // evaluate the third expression
4831 if (has_const_expr)
4832 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004833 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01004834 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004835 return FAIL;
4836 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4837 return FAIL;
4838 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004839
Bram Moolenaara5565e42020-05-09 15:44:01 +02004840 if (!has_const_expr)
4841 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004842 type_T **typep;
4843
Bram Moolenaara5565e42020-05-09 15:44:01 +02004844 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004845
Bram Moolenaara5565e42020-05-09 15:44:01 +02004846 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004847 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4848 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004849
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004850 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004851 isn = ((isn_T *)instr->ga_data) + end_idx;
4852 isn->isn_arg.jump.jump_where = instr->ga_len;
4853 }
4854
4855 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004856 }
4857 return OK;
4858}
4859
4860/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004861 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004862 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4863 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004864 */
4865 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004866compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004867{
4868 ppconst_T ppconst;
4869
4870 CLEAR_FIELD(ppconst);
4871 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4872 {
4873 clear_ppconst(&ppconst);
4874 return FAIL;
4875 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004876 if (is_const != NULL)
4877 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004878 if (generate_ppconst(cctx, &ppconst) == FAIL)
4879 return FAIL;
4880 return OK;
4881}
4882
4883/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004884 * Toplevel expression.
4885 */
4886 static int
4887compile_expr0(char_u **arg, cctx_T *cctx)
4888{
4889 return compile_expr0_ext(arg, cctx, NULL);
4890}
4891
4892/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893 * compile "return [expr]"
4894 */
4895 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004896compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004897{
4898 char_u *p = arg;
4899 garray_T *stack = &cctx->ctx_type_stack;
4900 type_T *stack_type;
4901
4902 if (*p != NUL && *p != '|' && *p != '\n')
4903 {
4904 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004905 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004906 return NULL;
4907
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004908 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004909 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004910 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01004911 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01004912 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
4913 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004914 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004915 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004916 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004917 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004918 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004919 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4920 && stack_type->tt_type != VAR_VOID
4921 && stack_type->tt_type != VAR_UNKNOWN)
4922 {
4923 emsg(_(e_returning_value_in_function_without_return_type));
4924 return NULL;
4925 }
4926 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01004927 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004928 return NULL;
4929 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02004930 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004931 }
4932 else
4933 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004934 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004935 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004936 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4937 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004939 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004940 return NULL;
4941 }
4942
4943 // No argument, return zero.
4944 generate_PUSHNR(cctx, 0);
4945 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01004946
4947 // Undo any command modifiers.
4948 generate_undo_cmdmods(cctx);
4949
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004950 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004951 return NULL;
4952
4953 // "return val | endif" is possible
4954 return skipwhite(p);
4955}
4956
4957/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004958 * Get a line from the compilation context, compatible with exarg_T getline().
4959 * Return a pointer to the line in allocated memory.
4960 * Return NULL for end-of-file or some error.
4961 */
4962 static char_u *
4963exarg_getline(
4964 int c UNUSED,
4965 void *cookie,
4966 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004967 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004968{
4969 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004970 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004971
Bram Moolenaar66250c92020-08-20 15:02:42 +02004972 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004973 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004974 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004975 return NULL;
4976 ++cctx->ctx_lnum;
4977 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4978 // Comment lines result in NULL pointers, skip them.
4979 if (p != NULL)
4980 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004981 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004982}
4983
4984/*
4985 * Compile a nested :def command.
4986 */
4987 static char_u *
4988compile_nested_function(exarg_T *eap, cctx_T *cctx)
4989{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004990 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004991 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004992 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004993 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004994 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004995 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004996
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004997 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004998 {
4999 emsg(_(e_cannot_use_bang_with_nested_def));
5000 return NULL;
5001 }
5002
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005003 if (*name_start == '/')
5004 {
5005 name_end = skip_regexp(name_start + 1, '/', TRUE);
5006 if (*name_end == '/')
5007 ++name_end;
5008 eap->nextcmd = check_nextcmd(name_end);
5009 }
5010 if (name_end == name_start || *skipwhite(name_end) != '(')
5011 {
5012 if (!ends_excmd2(name_start, name_end))
5013 {
5014 semsg(_(e_invalid_command_str), eap->cmd);
5015 return NULL;
5016 }
5017
5018 // "def" or "def Name": list functions
5019 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5020 return NULL;
5021 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5022 }
5023
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005024 // Only g:Func() can use a namespace.
5025 if (name_start[1] == ':' && !is_global)
5026 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005027 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005028 return NULL;
5029 }
Bram Moolenaareef21022020-08-01 22:16:43 +02005030 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
5031 return NULL;
5032
Bram Moolenaar04b12692020-05-04 23:24:44 +02005033 eap->arg = name_end;
5034 eap->getline = exarg_getline;
5035 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005036 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005037 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005038 lambda_name = vim_strsave(get_lambda_name());
5039 if (lambda_name == NULL)
5040 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005041 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005042
Bram Moolenaar822ba242020-05-24 23:00:18 +02005043 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005044 {
5045 r = eap->skip ? OK : FAIL;
5046 goto theend;
5047 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005048 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02005049 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005050 {
5051 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005052 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005053 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005054
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005055 if (is_global)
5056 {
5057 char_u *func_name = vim_strnsave(name_start + 2,
5058 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005059
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005060 if (func_name == NULL)
5061 r = FAIL;
5062 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005063 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005064 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005065 lambda_name = NULL;
5066 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005067 }
5068 else
5069 {
5070 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005071 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005072 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005073 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02005074
Bram Moolenaareef21022020-08-01 22:16:43 +02005075 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005076 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005077 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005078 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005079 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005080
5081 // copy over the block scope IDs
5082 if (block_depth > 0)
5083 {
5084 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5085 if (ufunc->uf_block_ids != NULL)
5086 {
5087 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5088 sizeof(int) * block_depth);
5089 ufunc->uf_block_depth = block_depth;
5090 }
5091 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005092 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005093 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005094 r = OK;
5095
5096theend:
5097 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005098 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005099}
5100
5101/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005102 * Return the length of an assignment operator, or zero if there isn't one.
5103 */
5104 int
5105assignment_len(char_u *p, int *heredoc)
5106{
5107 if (*p == '=')
5108 {
5109 if (p[1] == '<' && p[2] == '<')
5110 {
5111 *heredoc = TRUE;
5112 return 3;
5113 }
5114 return 1;
5115 }
5116 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5117 return 2;
5118 if (STRNCMP(p, "..=", 3) == 0)
5119 return 3;
5120 return 0;
5121}
5122
5123// words that cannot be used as a variable
5124static char *reserved[] = {
5125 "true",
5126 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005127 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005128 NULL
5129};
5130
Bram Moolenaar752fc692021-01-04 21:57:11 +01005131// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005132typedef enum {
5133 dest_local,
5134 dest_option,
5135 dest_env,
5136 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005137 dest_buffer,
5138 dest_window,
5139 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005140 dest_vimvar,
5141 dest_script,
5142 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005143 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005144} assign_dest_T;
5145
Bram Moolenaar752fc692021-01-04 21:57:11 +01005146// Used by compile_lhs() to store information about the LHS of an assignment
5147// and one argument of ":unlet" with an index.
5148typedef struct {
5149 assign_dest_T lhs_dest; // type of destination
5150
5151 char_u *lhs_name; // allocated name including
5152 // "[expr]" or ".name".
5153 size_t lhs_varlen; // length of the variable without
5154 // "[expr]" or ".name"
5155 char_u *lhs_dest_end; // end of the destination, including
5156 // "[expr]" or ".name".
5157
5158 int lhs_has_index; // has "[expr]" or ".name"
5159
5160 int lhs_new_local; // create new local variable
5161 int lhs_opt_flags; // for when destination is an option
5162 int lhs_vimvaridx; // for when destination is a v:var
5163
5164 lvar_T lhs_local_lvar; // used for existing local destination
5165 lvar_T lhs_arg_lvar; // used for argument destination
5166 lvar_T *lhs_lvar; // points to destination lvar
5167 int lhs_scriptvar_sid;
5168 int lhs_scriptvar_idx;
5169
5170 int lhs_has_type; // type was specified
5171 type_T *lhs_type;
5172 type_T *lhs_member_type;
5173} lhs_T;
5174
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005175/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005176 * Generate the load instruction for "name".
5177 */
5178 static void
5179generate_loadvar(
5180 cctx_T *cctx,
5181 assign_dest_T dest,
5182 char_u *name,
5183 lvar_T *lvar,
5184 type_T *type)
5185{
5186 switch (dest)
5187 {
5188 case dest_option:
5189 // TODO: check the option exists
5190 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5191 break;
5192 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005193 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5194 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5195 else
5196 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005197 break;
5198 case dest_buffer:
5199 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5200 break;
5201 case dest_window:
5202 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5203 break;
5204 case dest_tab:
5205 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5206 break;
5207 case dest_script:
5208 compile_load_scriptvar(cctx,
5209 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5210 break;
5211 case dest_env:
5212 // Include $ in the name here
5213 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5214 break;
5215 case dest_reg:
5216 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5217 break;
5218 case dest_vimvar:
5219 generate_LOADV(cctx, name + 2, TRUE);
5220 break;
5221 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005222 if (lvar->lv_from_outer > 0)
5223 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5224 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005225 else
5226 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5227 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005228 case dest_expr:
5229 // list or dict value should already be on the stack.
5230 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005231 }
5232}
5233
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005234/*
5235 * Skip over "[expr]" or ".member".
5236 * Does not check for any errors.
5237 */
5238 static char_u *
5239skip_index(char_u *start)
5240{
5241 char_u *p = start;
5242
5243 if (*p == '[')
5244 {
5245 p = skipwhite(p + 1);
5246 (void)skip_expr(&p, NULL);
5247 p = skipwhite(p);
5248 if (*p == ']')
5249 return p + 1;
5250 return p;
5251 }
5252 // if (*p == '.')
5253 return to_name_end(p + 1, TRUE);
5254}
5255
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005256 void
5257vim9_declare_error(char_u *name)
5258{
5259 char *scope = "";
5260
5261 switch (*name)
5262 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005263 case 'g': scope = _("global"); break;
5264 case 'b': scope = _("buffer"); break;
5265 case 'w': scope = _("window"); break;
5266 case 't': scope = _("tab"); break;
5267 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005268 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005269 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005270 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005271 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005272 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005273 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005274 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005275 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005276 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005277}
5278
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005279/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005280 * For one assignment figure out the type of destination. Return it in "dest".
5281 * When not recognized "dest" is not set.
5282 * For an option "opt_flags" is set.
5283 * For a v:var "vimvaridx" is set.
5284 * "type" is set to the destination type if known, unchanted otherwise.
5285 * Return FAIL if an error message was given.
5286 */
5287 static int
5288get_var_dest(
5289 char_u *name,
5290 assign_dest_T *dest,
5291 int cmdidx,
5292 int *opt_flags,
5293 int *vimvaridx,
5294 type_T **type,
5295 cctx_T *cctx)
5296{
5297 char_u *p;
5298
5299 if (*name == '&')
5300 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005301 int cc;
5302 long numval;
5303 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005304
5305 *dest = dest_option;
5306 if (cmdidx == CMD_final || cmdidx == CMD_const)
5307 {
5308 emsg(_(e_const_option));
5309 return FAIL;
5310 }
5311 p = name;
5312 p = find_option_end(&p, opt_flags);
5313 if (p == NULL)
5314 {
5315 // cannot happen?
5316 emsg(_(e_letunexp));
5317 return FAIL;
5318 }
5319 cc = *p;
5320 *p = NUL;
5321 opt_type = get_option_value(skip_option_env_lead(name),
5322 &numval, NULL, *opt_flags);
5323 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005324 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005325 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005326 case gov_unknown:
5327 semsg(_(e_unknown_option), name);
5328 return FAIL;
5329 case gov_string:
5330 case gov_hidden_string:
5331 *type = &t_string;
5332 break;
5333 case gov_bool:
5334 case gov_hidden_bool:
5335 *type = &t_bool;
5336 break;
5337 case gov_number:
5338 case gov_hidden_number:
5339 *type = &t_number;
5340 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005341 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005342 }
5343 else if (*name == '$')
5344 {
5345 *dest = dest_env;
5346 *type = &t_string;
5347 }
5348 else if (*name == '@')
5349 {
5350 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5351 {
5352 emsg_invreg(name[1]);
5353 return FAIL;
5354 }
5355 *dest = dest_reg;
5356 *type = &t_string;
5357 }
5358 else if (STRNCMP(name, "g:", 2) == 0)
5359 {
5360 *dest = dest_global;
5361 }
5362 else if (STRNCMP(name, "b:", 2) == 0)
5363 {
5364 *dest = dest_buffer;
5365 }
5366 else if (STRNCMP(name, "w:", 2) == 0)
5367 {
5368 *dest = dest_window;
5369 }
5370 else if (STRNCMP(name, "t:", 2) == 0)
5371 {
5372 *dest = dest_tab;
5373 }
5374 else if (STRNCMP(name, "v:", 2) == 0)
5375 {
5376 typval_T *vtv;
5377 int di_flags;
5378
5379 *vimvaridx = find_vim_var(name + 2, &di_flags);
5380 if (*vimvaridx < 0)
5381 {
5382 semsg(_(e_variable_not_found_str), name);
5383 return FAIL;
5384 }
5385 // We use the current value of "sandbox" here, is that OK?
5386 if (var_check_ro(di_flags, name, FALSE))
5387 return FAIL;
5388 *dest = dest_vimvar;
5389 vtv = get_vim_var_tv(*vimvaridx);
5390 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5391 }
5392 return OK;
5393}
5394
5395/*
5396 * Generate a STORE instruction for "dest", not being "dest_local".
5397 * Return FAIL when out of memory.
5398 */
5399 static int
5400generate_store_var(
5401 cctx_T *cctx,
5402 assign_dest_T dest,
5403 int opt_flags,
5404 int vimvaridx,
5405 int scriptvar_idx,
5406 int scriptvar_sid,
5407 type_T *type,
5408 char_u *name)
5409{
5410 switch (dest)
5411 {
5412 case dest_option:
5413 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5414 opt_flags);
5415 case dest_global:
5416 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005417 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5418 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005419 case dest_buffer:
5420 // include b: with the name, easier to execute that way
5421 return generate_STORE(cctx, ISN_STOREB, 0, name);
5422 case dest_window:
5423 // include w: with the name, easier to execute that way
5424 return generate_STORE(cctx, ISN_STOREW, 0, name);
5425 case dest_tab:
5426 // include t: with the name, easier to execute that way
5427 return generate_STORE(cctx, ISN_STORET, 0, name);
5428 case dest_env:
5429 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5430 case dest_reg:
5431 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5432 case dest_vimvar:
5433 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5434 case dest_script:
5435 if (scriptvar_idx < 0)
5436 {
5437 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005438 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005439
Bram Moolenaar41d61962020-12-06 20:12:43 +01005440 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005441 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5442 scriptvar_sid, type);
5443 if (name_s != name)
5444 vim_free(name_s);
5445 return r;
5446 }
5447 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5448 scriptvar_sid, scriptvar_idx, type);
5449 case dest_local:
5450 case dest_expr:
5451 // cannot happen
5452 break;
5453 }
5454 return FAIL;
5455}
5456
Bram Moolenaar752fc692021-01-04 21:57:11 +01005457 static int
5458is_decl_command(int cmdidx)
5459{
5460 return cmdidx == CMD_let || cmdidx == CMD_var
5461 || cmdidx == CMD_final || cmdidx == CMD_const;
5462}
5463
5464/*
5465 * Figure out the LHS type and other properties for an assignment or one item
5466 * of ":unlet" with an index.
5467 * Returns OK or FAIL.
5468 */
5469 static int
5470compile_lhs(
5471 char_u *var_start,
5472 lhs_T *lhs,
5473 int cmdidx,
5474 int heredoc,
5475 int oplen,
5476 cctx_T *cctx)
5477{
5478 char_u *var_end;
5479 int is_decl = is_decl_command(cmdidx);
5480
5481 CLEAR_POINTER(lhs);
5482 lhs->lhs_dest = dest_local;
5483 lhs->lhs_vimvaridx = -1;
5484 lhs->lhs_scriptvar_idx = -1;
5485
5486 // "dest_end" is the end of the destination, including "[expr]" or
5487 // ".name".
5488 // "var_end" is the end of the variable/option/etc. name.
5489 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5490 if (*var_start == '@')
5491 var_end = var_start + 2;
5492 else
5493 {
5494 // skip over the leading "&", "&l:", "&g:" and "$"
5495 var_end = skip_option_env_lead(var_start);
5496 var_end = to_name_end(var_end, TRUE);
5497 }
5498
5499 // "a: type" is declaring variable "a" with a type, not dict "a:".
5500 if (is_decl && lhs->lhs_dest_end == var_start + 2
5501 && lhs->lhs_dest_end[-1] == ':')
5502 --lhs->lhs_dest_end;
5503 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5504 --var_end;
5505
5506 // compute the length of the destination without "[expr]" or ".name"
5507 lhs->lhs_varlen = var_end - var_start;
5508 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5509 if (lhs->lhs_name == NULL)
5510 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005511
5512 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5513 // Something follows after the variable: "var[idx]" or "var.key".
5514 lhs->lhs_has_index = TRUE;
5515
Bram Moolenaar752fc692021-01-04 21:57:11 +01005516 if (heredoc)
5517 lhs->lhs_type = &t_list_string;
5518 else
5519 lhs->lhs_type = &t_any;
5520
5521 if (cctx->ctx_skip != SKIP_YES)
5522 {
5523 int declare_error = FALSE;
5524
5525 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5526 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5527 &lhs->lhs_type, cctx) == FAIL)
5528 return FAIL;
5529 if (lhs->lhs_dest != dest_local)
5530 {
5531 // Specific kind of variable recognized.
5532 declare_error = is_decl;
5533 }
5534 else
5535 {
5536 int idx;
5537
5538 // No specific kind of variable recognized, just a name.
5539 for (idx = 0; reserved[idx] != NULL; ++idx)
5540 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5541 {
5542 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5543 return FAIL;
5544 }
5545
5546
5547 if (lookup_local(var_start, lhs->lhs_varlen,
5548 &lhs->lhs_local_lvar, cctx) == OK)
5549 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5550 else
5551 {
5552 CLEAR_FIELD(lhs->lhs_arg_lvar);
5553 if (arg_exists(var_start, lhs->lhs_varlen,
5554 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5555 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5556 {
5557 if (is_decl)
5558 {
5559 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5560 return FAIL;
5561 }
5562 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5563 }
5564 }
5565 if (lhs->lhs_lvar != NULL)
5566 {
5567 if (is_decl)
5568 {
5569 semsg(_(e_variable_already_declared), lhs->lhs_name);
5570 return FAIL;
5571 }
5572 }
5573 else
5574 {
5575 int script_namespace = lhs->lhs_varlen > 1
5576 && STRNCMP(var_start, "s:", 2) == 0;
5577 int script_var = (script_namespace
5578 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
5579 FALSE, cctx)
5580 : script_var_exists(var_start, lhs->lhs_varlen,
5581 TRUE, cctx)) == OK;
5582 imported_T *import =
5583 find_imported(var_start, lhs->lhs_varlen, cctx);
5584
5585 if (script_namespace || script_var || import != NULL)
5586 {
5587 char_u *rawname = lhs->lhs_name
5588 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5589
5590 if (is_decl)
5591 {
5592 if (script_namespace)
5593 semsg(_(e_cannot_declare_script_variable_in_function),
5594 lhs->lhs_name);
5595 else
5596 semsg(_(e_variable_already_declared_in_script),
5597 lhs->lhs_name);
5598 return FAIL;
5599 }
5600 else if (cctx->ctx_ufunc->uf_script_ctx_version
5601 == SCRIPT_VERSION_VIM9
5602 && script_namespace
5603 && !script_var && import == NULL)
5604 {
5605 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5606 return FAIL;
5607 }
5608
5609 lhs->lhs_dest = dest_script;
5610
5611 // existing script-local variables should have a type
5612 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5613 if (import != NULL)
5614 lhs->lhs_scriptvar_sid = import->imp_sid;
5615 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5616 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005617 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005618 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005619 lhs->lhs_scriptvar_sid, rawname,
5620 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5621 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005622 if (lhs->lhs_scriptvar_idx >= 0)
5623 {
5624 scriptitem_T *si = SCRIPT_ITEM(
5625 lhs->lhs_scriptvar_sid);
5626 svar_T *sv =
5627 ((svar_T *)si->sn_var_vals.ga_data)
5628 + lhs->lhs_scriptvar_idx;
5629 lhs->lhs_type = sv->sv_type;
5630 }
5631 }
5632 }
5633 else if (check_defined(var_start, lhs->lhs_varlen, cctx)
5634 == FAIL)
5635 return FAIL;
5636 }
5637 }
5638
5639 if (declare_error)
5640 {
5641 vim9_declare_error(lhs->lhs_name);
5642 return FAIL;
5643 }
5644 }
5645
5646 // handle "a:name" as a name, not index "name" on "a"
5647 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5648 var_end = lhs->lhs_dest_end;
5649
5650 if (lhs->lhs_dest != dest_option)
5651 {
5652 if (is_decl && *var_end == ':')
5653 {
5654 char_u *p;
5655
5656 // parse optional type: "let var: type = expr"
5657 if (!VIM_ISWHITE(var_end[1]))
5658 {
5659 semsg(_(e_white_space_required_after_str), ":");
5660 return FAIL;
5661 }
5662 p = skipwhite(var_end + 1);
5663 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5664 if (lhs->lhs_type == NULL)
5665 return FAIL;
5666 lhs->lhs_has_type = TRUE;
5667 }
5668 else if (lhs->lhs_lvar != NULL)
5669 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5670 }
5671
5672 if (oplen == 3 && !heredoc && lhs->lhs_dest != dest_global
5673 && lhs->lhs_type->tt_type != VAR_STRING
5674 && lhs->lhs_type->tt_type != VAR_ANY)
5675 {
5676 emsg(_(e_can_only_concatenate_to_string));
5677 return FAIL;
5678 }
5679
5680 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5681 && cctx->ctx_skip != SKIP_YES)
5682 {
5683 if (oplen > 1 && !heredoc)
5684 {
5685 // +=, /=, etc. require an existing variable
5686 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5687 return FAIL;
5688 }
5689 if (!is_decl)
5690 {
5691 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5692 return FAIL;
5693 }
5694
5695 // new local variable
5696 if ((lhs->lhs_type->tt_type == VAR_FUNC
5697 || lhs->lhs_type->tt_type == VAR_PARTIAL)
5698 && var_wrong_func_name(lhs->lhs_name, TRUE))
5699 return FAIL;
5700 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5701 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5702 if (lhs->lhs_lvar == NULL)
5703 return FAIL;
5704 lhs->lhs_new_local = TRUE;
5705 }
5706
5707 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01005708 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005709 {
5710 // Something follows after the variable: "var[idx]" or "var.key".
5711 // TODO: should we also handle "->func()" here?
5712 if (is_decl)
5713 {
5714 emsg(_(e_cannot_use_index_when_declaring_variable));
5715 return FAIL;
5716 }
5717
5718 if (var_start[lhs->lhs_varlen] == '['
5719 || var_start[lhs->lhs_varlen] == '.')
5720 {
5721 char_u *after = var_start + lhs->lhs_varlen;
5722 char_u *p;
5723
5724 // Only the last index is used below, if there are others
5725 // before it generate code for the expression. Thus for
5726 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5727 for (;;)
5728 {
5729 p = skip_index(after);
5730 if (*p != '[' && *p != '.')
5731 break;
5732 after = p;
5733 }
5734 if (after > var_start + lhs->lhs_varlen)
5735 {
5736 lhs->lhs_varlen = after - var_start;
5737 lhs->lhs_dest = dest_expr;
5738 // We don't know the type before evaluating the expression,
5739 // use "any" until then.
5740 lhs->lhs_type = &t_any;
5741 }
5742
Bram Moolenaar752fc692021-01-04 21:57:11 +01005743 if (lhs->lhs_type->tt_member == NULL)
5744 lhs->lhs_member_type = &t_any;
5745 else
5746 lhs->lhs_member_type = lhs->lhs_type->tt_member;
5747 }
5748 else
5749 {
5750 semsg("Not supported yet: %s", var_start);
5751 return FAIL;
5752 }
5753 }
5754 return OK;
5755}
5756
5757/*
5758 * Assignment to a list or dict member, or ":unlet" for the item, using the
5759 * information in "lhs".
5760 * Returns OK or FAIL.
5761 */
5762 static int
5763compile_assign_unlet(
5764 char_u *var_start,
5765 lhs_T *lhs,
5766 int is_assign,
5767 type_T *rhs_type,
5768 cctx_T *cctx)
5769{
5770 char_u *p;
5771 int r;
5772 vartype_T dest_type;
5773 size_t varlen = lhs->lhs_varlen;
5774 garray_T *stack = &cctx->ctx_type_stack;
5775
5776 // Compile the "idx" in "var[idx]" or "key" in "var.key".
5777 p = var_start + varlen;
5778 if (*p == '[')
5779 {
5780 p = skipwhite(p + 1);
5781 r = compile_expr0(&p, cctx);
5782 if (r == OK && *skipwhite(p) != ']')
5783 {
5784 // this should not happen
5785 emsg(_(e_missbrac));
5786 r = FAIL;
5787 }
5788 }
5789 else // if (*p == '.')
5790 {
5791 char_u *key_end = to_name_end(p + 1, TRUE);
5792 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5793
5794 r = generate_PUSHS(cctx, key);
5795 }
5796 if (r == FAIL)
5797 return FAIL;
5798
5799 if (lhs->lhs_type == &t_any)
5800 {
5801 // Index on variable of unknown type: check at runtime.
5802 dest_type = VAR_ANY;
5803 }
5804 else
5805 {
5806 dest_type = lhs->lhs_type->tt_type;
5807 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
5808 return FAIL;
5809 if (dest_type == VAR_LIST
Bram Moolenaarf30a14d2021-01-17 21:51:24 +01005810 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
5811 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005812 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005813 }
5814
5815 // Load the dict or list. On the stack we then have:
5816 // - value (for assignment, not for :unlet)
5817 // - index
5818 // - variable
5819 if (lhs->lhs_dest == dest_expr)
5820 {
5821 int c = var_start[varlen];
5822
5823 // Evaluate "ll[expr]" of "ll[expr][idx]"
5824 p = var_start;
5825 var_start[varlen] = NUL;
5826 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
5827 {
5828 // this should not happen
5829 emsg(_(e_missbrac));
5830 return FAIL;
5831 }
5832 var_start[varlen] = c;
5833
5834 lhs->lhs_type = stack->ga_len == 0 ? &t_void
5835 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5836 // now we can properly check the type
5837 if (lhs->lhs_type->tt_member != NULL && rhs_type != &t_void
Bram Moolenaar351ead02021-01-16 16:07:01 +01005838 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01005839 FALSE, FALSE) == FAIL)
5840 return FAIL;
5841 }
5842 else
5843 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
5844 lhs->lhs_lvar, lhs->lhs_type);
5845
5846 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
5847 {
5848 if (is_assign)
5849 {
5850 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
5851
5852 if (isn == NULL)
5853 return FAIL;
5854 isn->isn_arg.vartype = dest_type;
5855 }
5856 else
5857 {
5858 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
5859 return FAIL;
5860 }
5861 }
5862 else
5863 {
5864 emsg(_(e_indexable_type_required));
5865 return FAIL;
5866 }
5867
5868 return OK;
5869}
5870
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005871/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005872 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005873 * "let name"
5874 * "var name = expr"
5875 * "final name = expr"
5876 * "const name = expr"
5877 * "name = expr"
5878 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005879 * Return NULL for an error.
5880 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005881 */
5882 static char_u *
5883compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5884{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005885 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005886 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005887 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005888 char_u *ret = NULL;
5889 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005890 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005891 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005892 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005893 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005894 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005895 int oplen = 0;
5896 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005897 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005898 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005899 int is_decl = is_decl_command(cmdidx);
5900 lhs_T lhs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005901
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005902 // Skip over the "var" or "[var, var]" to get to any "=".
5903 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5904 if (p == NULL)
5905 return *arg == '[' ? arg : NULL;
5906
5907 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005908 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005909 // TODO: should we allow this, and figure out type inference from list
5910 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005911 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005912 return NULL;
5913 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01005914 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005915
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005916 sp = p;
5917 p = skipwhite(p);
5918 op = p;
5919 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005920
5921 if (var_count > 0 && oplen == 0)
5922 // can be something like "[1, 2]->func()"
5923 return arg;
5924
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005925 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005926 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005927 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005928 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005929 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005930
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005931 if (heredoc)
5932 {
5933 list_T *l;
5934 listitem_T *li;
5935
5936 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005937 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005938 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005939 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005940 if (l == NULL)
5941 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005942
Bram Moolenaar078269b2020-09-21 20:35:55 +02005943 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005944 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005945 // Push each line and the create the list.
5946 FOR_ALL_LIST_ITEMS(l, li)
5947 {
5948 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5949 li->li_tv.vval.v_string = NULL;
5950 }
5951 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005952 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005953 list_free(l);
5954 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005955 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005956 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005957 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005958 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005959 char_u *wp;
5960
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005961 // for "[var, var] = expr" evaluate the expression here, loop over the
5962 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005963 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005964
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005965 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01005966 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005967 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005968 if (compile_expr0(&p, cctx) == FAIL)
5969 return NULL;
5970 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005971
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005972 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005973 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005974 type_T *stacktype;
5975
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005976 stacktype = stack->ga_len == 0 ? &t_void
5977 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005978 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005979 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005980 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005981 goto theend;
5982 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01005983 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005984 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005985 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005986 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005987 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5988 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005989 if (stacktype->tt_member != NULL)
5990 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005991 }
5992 }
5993
5994 /*
5995 * Loop over variables in "[var, var] = expr".
5996 * For "var = expr" and "let var: type" this is done only once.
5997 */
5998 if (var_count > 0)
5999 var_start = skipwhite(arg + 1); // skip over the "["
6000 else
6001 var_start = arg;
6002 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6003 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006004 int instr_count = -1;
6005
Bram Moolenaar752fc692021-01-04 21:57:11 +01006006 vim_free(lhs.lhs_name);
6007
6008 /*
6009 * Figure out the LHS type and other properties.
6010 */
6011 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6012 goto theend;
6013
6014 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006015 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006016 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006017 goto theend;
6018 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006019 if (!is_decl && lhs.lhs_lvar != NULL
6020 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006021 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006022 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006023 goto theend;
6024 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006025
6026 if (!heredoc)
6027 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006028 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006029 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006030 if (oplen > 0 && var_count == 0)
6031 {
6032 // skip over the "=" and the expression
6033 p = skipwhite(op + oplen);
6034 compile_expr0(&p, cctx);
6035 }
6036 }
6037 else if (oplen > 0)
6038 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006039 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006040 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006041
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006042 // For "var = expr" evaluate the expression.
6043 if (var_count == 0)
6044 {
6045 int r;
6046
6047 // for "+=", "*=", "..=" etc. first load the current value
6048 if (*op != '=')
6049 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006050 generate_loadvar(cctx, lhs.lhs_dest, lhs.lhs_name,
6051 lhs.lhs_lvar, lhs.lhs_type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006052
Bram Moolenaar752fc692021-01-04 21:57:11 +01006053 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006054 {
6055 // TODO: get member from list or dict
6056 emsg("Index with operation not supported yet");
6057 goto theend;
6058 }
6059 }
6060
6061 // Compile the expression. Temporarily hide the new local
6062 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006063 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006064 --cctx->ctx_locals.ga_len;
6065 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006066 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006067 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006068 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006069 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006070 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006071 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006072 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006073 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006074 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006075 ++cctx->ctx_locals.ga_len;
6076 if (r == FAIL)
6077 goto theend;
6078 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006079 else if (semicolon && var_idx == var_count - 1)
6080 {
6081 // For "[var; var] = expr" get the rest of the list
6082 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6083 goto theend;
6084 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006085 else
6086 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006087 // For "[var, var] = expr" get the "var_idx" item from the
6088 // list.
6089 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006090 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006091 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006092
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006093 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006094 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006095 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006096 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006097 if ((rhs_type->tt_type == VAR_FUNC
6098 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006099 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006100 goto theend;
6101
Bram Moolenaar752fc692021-01-04 21:57:11 +01006102 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006103 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006104 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006105 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006106 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006107 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006108 }
6109 else
6110 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006111 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006112 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006113 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006114 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006115 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006116 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006117 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006118 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006119 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006120 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006121 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006122 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006123 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006124 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006125 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006126
Bram Moolenaar71abe482020-09-14 22:28:30 +02006127 // without operator check type here, otherwise below
Bram Moolenaar752fc692021-01-04 21:57:11 +01006128 if (lhs.lhs_has_index)
6129 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006130 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006131 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006132 goto theend;
6133 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006134 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006135 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006136 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006137 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006138 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006139 else if (cmdidx == CMD_final)
6140 {
6141 emsg(_(e_final_requires_a_value));
6142 goto theend;
6143 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006144 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006145 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006146 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006147 goto theend;
6148 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006149 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006150 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006151 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006152 goto theend;
6153 }
6154 else
6155 {
6156 // variables are always initialized
6157 if (ga_grow(instr, 1) == FAIL)
6158 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006159 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006160 {
6161 case VAR_BOOL:
6162 generate_PUSHBOOL(cctx, VVAL_FALSE);
6163 break;
6164 case VAR_FLOAT:
6165#ifdef FEAT_FLOAT
6166 generate_PUSHF(cctx, 0.0);
6167#endif
6168 break;
6169 case VAR_STRING:
6170 generate_PUSHS(cctx, NULL);
6171 break;
6172 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006173 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006174 break;
6175 case VAR_FUNC:
6176 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6177 break;
6178 case VAR_LIST:
6179 generate_NEWLIST(cctx, 0);
6180 break;
6181 case VAR_DICT:
6182 generate_NEWDICT(cctx, 0);
6183 break;
6184 case VAR_JOB:
6185 generate_PUSHJOB(cctx, NULL);
6186 break;
6187 case VAR_CHANNEL:
6188 generate_PUSHCHANNEL(cctx, NULL);
6189 break;
6190 case VAR_NUMBER:
6191 case VAR_UNKNOWN:
6192 case VAR_ANY:
6193 case VAR_PARTIAL:
6194 case VAR_VOID:
6195 case VAR_SPECIAL: // cannot happen
6196 generate_PUSHNR(cctx, 0);
6197 break;
6198 }
6199 }
6200 if (var_count == 0)
6201 end = p;
6202 }
6203
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006204 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006205 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006206 break;
6207
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006208 if (oplen > 0 && *op != '=')
6209 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006210 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006211 type_T *stacktype;
6212
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006213 if (*op == '.')
6214 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006215 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006216 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006217 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006218 if (
6219#ifdef FEAT_FLOAT
6220 // If variable is float operation with number is OK.
6221 !(expected == &t_float && stacktype == &t_number) &&
6222#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006223 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006224 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006225 goto theend;
6226
6227 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006228 {
6229 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6230 goto theend;
6231 }
6232 else if (*op == '+')
6233 {
6234 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006235 operator_type(lhs.lhs_member_type, stacktype),
6236 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006237 goto theend;
6238 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006239 else if (generate_two_op(cctx, op) == FAIL)
6240 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006241 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006242
Bram Moolenaar752fc692021-01-04 21:57:11 +01006243 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006244 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006245 // Use the info in "lhs" to store the value at the index in the
6246 // list or dict.
6247 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6248 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006249 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006250 }
6251 else
6252 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006253 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
6254 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006255 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006256 generate_LOCKCONST(cctx);
6257
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006258 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006259 && (lhs.lhs_type->tt_type == VAR_DICT
6260 || lhs.lhs_type->tt_type == VAR_LIST)
6261 && lhs.lhs_type->tt_member != NULL
6262 && lhs.lhs_type->tt_member != &t_any
6263 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006264 // Set the type in the list or dict, so that it can be checked,
6265 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006266 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006267
Bram Moolenaar752fc692021-01-04 21:57:11 +01006268 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006269 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006270 if (generate_store_var(cctx, lhs.lhs_dest,
6271 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6272 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6273 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006274 goto theend;
6275 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006276 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006277 {
6278 isn_T *isn = ((isn_T *)instr->ga_data)
6279 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006280
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006281 // optimization: turn "var = 123" from ISN_PUSHNR +
6282 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006283 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006284 && instr->ga_len == instr_count + 1
6285 && isn->isn_type == ISN_PUSHNR)
6286 {
6287 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006288
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006289 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006290 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006291 isn->isn_arg.storenr.stnr_val = val;
6292 if (stack->ga_len > 0)
6293 --stack->ga_len;
6294 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006295 else if (lhs.lhs_lvar->lv_from_outer > 0)
6296 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6297 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006298 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006299 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006300 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006301 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006302
6303 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006304 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006305 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006306
6307 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006308 if (var_count > 0 && !semicolon)
6309 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006310 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006311 goto theend;
6312 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006313
Bram Moolenaarb2097502020-07-19 17:17:02 +02006314 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006315
6316theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006317 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006318 return ret;
6319}
6320
6321/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006322 * Check for an assignment at "eap->cmd", compile it if found.
6323 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6324 */
6325 static int
6326may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6327{
6328 char_u *pskip;
6329 char_u *p;
6330
6331 // Assuming the command starts with a variable or function name,
6332 // find what follows.
6333 // Skip over "var.member", "var[idx]" and the like.
6334 // Also "&opt = val", "$ENV = val" and "@r = val".
6335 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6336 ? eap->cmd + 1 : eap->cmd;
6337 p = to_name_end(pskip, TRUE);
6338 if (p > eap->cmd && *p != NUL)
6339 {
6340 char_u *var_end;
6341 int oplen;
6342 int heredoc;
6343
6344 if (eap->cmd[0] == '@')
6345 var_end = eap->cmd + 2;
6346 else
6347 var_end = find_name_end(pskip, NULL, NULL,
6348 FNE_CHECK_START | FNE_INCL_BR);
6349 oplen = assignment_len(skipwhite(var_end), &heredoc);
6350 if (oplen > 0)
6351 {
6352 size_t len = p - eap->cmd;
6353
6354 // Recognize an assignment if we recognize the variable
6355 // name:
6356 // "g:var = expr"
6357 // "local = expr" where "local" is a local var.
6358 // "script = expr" where "script" is a script-local var.
6359 // "import = expr" where "import" is an imported var
6360 // "&opt = expr"
6361 // "$ENV = expr"
6362 // "@r = expr"
6363 if (*eap->cmd == '&'
6364 || *eap->cmd == '$'
6365 || *eap->cmd == '@'
6366 || ((len) > 2 && eap->cmd[1] == ':')
6367 || lookup_local(eap->cmd, len, NULL, cctx) == OK
6368 || arg_exists(eap->cmd, len, NULL, NULL, NULL, cctx) == OK
6369 || script_var_exists(eap->cmd, len, FALSE, cctx) == OK
6370 || find_imported(eap->cmd, len, cctx) != NULL)
6371 {
6372 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6373 if (*line == NULL || *line == eap->cmd)
6374 return FAIL;
6375 return OK;
6376 }
6377 }
6378 }
6379
6380 if (*eap->cmd == '[')
6381 {
6382 // [var, var] = expr
6383 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6384 if (*line == NULL)
6385 return FAIL;
6386 if (*line != eap->cmd)
6387 return OK;
6388 }
6389 return NOTDONE;
6390}
6391
6392/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006393 * Check if "name" can be "unlet".
6394 */
6395 int
6396check_vim9_unlet(char_u *name)
6397{
6398 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6399 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006400 // "unlet s:var" is allowed in legacy script.
6401 if (*name == 's' && !script_is_vim9())
6402 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006403 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006404 return FAIL;
6405 }
6406 return OK;
6407}
6408
6409/*
6410 * Callback passed to ex_unletlock().
6411 */
6412 static int
6413compile_unlet(
6414 lval_T *lvp,
6415 char_u *name_end,
6416 exarg_T *eap,
6417 int deep UNUSED,
6418 void *coookie)
6419{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006420 cctx_T *cctx = coookie;
6421 char_u *p = lvp->ll_name;
6422 int cc = *name_end;
6423 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006424
Bram Moolenaar752fc692021-01-04 21:57:11 +01006425 if (cctx->ctx_skip == SKIP_YES)
6426 return OK;
6427
6428 *name_end = NUL;
6429 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006430 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006431 // :unlet $ENV_VAR
6432 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6433 }
6434 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6435 {
6436 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006437
Bram Moolenaar752fc692021-01-04 21:57:11 +01006438 // This is similar to assigning: lookup the list/dict, compile the
6439 // idx/key. Then instead of storing the value unlet the item.
6440 // unlet {list}[idx]
6441 // unlet {dict}[key] dict.key
6442 //
6443 // Figure out the LHS type and other properties.
6444 //
6445 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6446
6447 // : unlet an indexed item
6448 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006449 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006450 iemsg("called compile_lhs() without an index");
6451 ret = FAIL;
6452 }
6453 else
6454 {
6455 // Use the info in "lhs" to unlet the item at the index in the
6456 // list or dict.
6457 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006458 }
6459
Bram Moolenaar752fc692021-01-04 21:57:11 +01006460 vim_free(lhs.lhs_name);
6461 }
6462 else if (check_vim9_unlet(p) == FAIL)
6463 {
6464 ret = FAIL;
6465 }
6466 else
6467 {
6468 // Normal name. Only supports g:, w:, t: and b: namespaces.
6469 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006470 }
6471
Bram Moolenaar752fc692021-01-04 21:57:11 +01006472 *name_end = cc;
6473 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006474}
6475
6476/*
6477 * compile "unlet var", "lock var" and "unlock var"
6478 * "arg" points to "var".
6479 */
6480 static char_u *
6481compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6482{
6483 char_u *p = arg;
6484
6485 if (eap->cmdidx != CMD_unlet)
6486 {
6487 emsg("Sorry, :lock and unlock not implemented yet");
6488 return NULL;
6489 }
6490
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006491 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6492 compile_unlet, cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006493 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6494}
6495
6496/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006497 * Compile an :import command.
6498 */
6499 static char_u *
6500compile_import(char_u *arg, cctx_T *cctx)
6501{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006502 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006503}
6504
6505/*
6506 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6507 */
6508 static int
6509compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6510{
6511 garray_T *instr = &cctx->ctx_instr;
6512 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6513
6514 if (endlabel == NULL)
6515 return FAIL;
6516 endlabel->el_next = *el;
6517 *el = endlabel;
6518 endlabel->el_end_label = instr->ga_len;
6519
6520 generate_JUMP(cctx, when, 0);
6521 return OK;
6522}
6523
6524 static void
6525compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
6526{
6527 garray_T *instr = &cctx->ctx_instr;
6528
6529 while (*el != NULL)
6530 {
6531 endlabel_T *cur = (*el);
6532 isn_T *isn;
6533
6534 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
6535 isn->isn_arg.jump.jump_where = instr->ga_len;
6536 *el = cur->el_next;
6537 vim_free(cur);
6538 }
6539}
6540
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006541 static void
6542compile_free_jump_to_end(endlabel_T **el)
6543{
6544 while (*el != NULL)
6545 {
6546 endlabel_T *cur = (*el);
6547
6548 *el = cur->el_next;
6549 vim_free(cur);
6550 }
6551}
6552
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006553/*
6554 * Create a new scope and set up the generic items.
6555 */
6556 static scope_T *
6557new_scope(cctx_T *cctx, scopetype_T type)
6558{
6559 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6560
6561 if (scope == NULL)
6562 return NULL;
6563 scope->se_outer = cctx->ctx_scope;
6564 cctx->ctx_scope = scope;
6565 scope->se_type = type;
6566 scope->se_local_count = cctx->ctx_locals.ga_len;
6567 return scope;
6568}
6569
6570/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006571 * Free the current scope and go back to the outer scope.
6572 */
6573 static void
6574drop_scope(cctx_T *cctx)
6575{
6576 scope_T *scope = cctx->ctx_scope;
6577
6578 if (scope == NULL)
6579 {
6580 iemsg("calling drop_scope() without a scope");
6581 return;
6582 }
6583 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006584 switch (scope->se_type)
6585 {
6586 case IF_SCOPE:
6587 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6588 case FOR_SCOPE:
6589 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6590 case WHILE_SCOPE:
6591 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6592 case TRY_SCOPE:
6593 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6594 case NO_SCOPE:
6595 case BLOCK_SCOPE:
6596 break;
6597 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006598 vim_free(scope);
6599}
6600
6601/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006602 * compile "if expr"
6603 *
6604 * "if expr" Produces instructions:
6605 * EVAL expr Push result of "expr"
6606 * JUMP_IF_FALSE end
6607 * ... body ...
6608 * end:
6609 *
6610 * "if expr | else" Produces instructions:
6611 * EVAL expr Push result of "expr"
6612 * JUMP_IF_FALSE else
6613 * ... body ...
6614 * JUMP_ALWAYS end
6615 * else:
6616 * ... body ...
6617 * end:
6618 *
6619 * "if expr1 | elseif expr2 | else" Produces instructions:
6620 * EVAL expr Push result of "expr"
6621 * JUMP_IF_FALSE elseif
6622 * ... body ...
6623 * JUMP_ALWAYS end
6624 * elseif:
6625 * EVAL expr Push result of "expr"
6626 * JUMP_IF_FALSE else
6627 * ... body ...
6628 * JUMP_ALWAYS end
6629 * else:
6630 * ... body ...
6631 * end:
6632 */
6633 static char_u *
6634compile_if(char_u *arg, cctx_T *cctx)
6635{
6636 char_u *p = arg;
6637 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006638 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006639 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006640 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006641 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006642
Bram Moolenaara5565e42020-05-09 15:44:01 +02006643 CLEAR_FIELD(ppconst);
6644 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006645 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006646 clear_ppconst(&ppconst);
6647 return NULL;
6648 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006649 if (cctx->ctx_skip == SKIP_YES)
6650 clear_ppconst(&ppconst);
6651 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006652 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006653 int error = FALSE;
6654 int v;
6655
Bram Moolenaara5565e42020-05-09 15:44:01 +02006656 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006657 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006658 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006659 if (error)
6660 return NULL;
6661 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006662 }
6663 else
6664 {
6665 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006666 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006667 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006668 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006669 if (bool_on_stack(cctx) == FAIL)
6670 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006671 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006672
6673 scope = new_scope(cctx, IF_SCOPE);
6674 if (scope == NULL)
6675 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006676 scope->se_skip_save = skip_save;
6677 // "is_had_return" will be reset if any block does not end in :return
6678 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006679
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006680 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006681 {
6682 // "where" is set when ":elseif", "else" or ":endif" is found
6683 scope->se_u.se_if.is_if_label = instr->ga_len;
6684 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6685 }
6686 else
6687 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006688
6689 return p;
6690}
6691
6692 static char_u *
6693compile_elseif(char_u *arg, cctx_T *cctx)
6694{
6695 char_u *p = arg;
6696 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006697 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006698 isn_T *isn;
6699 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006700 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006701 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006702
6703 if (scope == NULL || scope->se_type != IF_SCOPE)
6704 {
6705 emsg(_(e_elseif_without_if));
6706 return NULL;
6707 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006708 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006709 if (!cctx->ctx_had_return)
6710 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006711
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006712 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006713 {
6714 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006715 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006716 return NULL;
6717 // previous "if" or "elseif" jumps here
6718 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6719 isn->isn_arg.jump.jump_where = instr->ga_len;
6720 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006721
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006722 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006723 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006724 if (cctx->ctx_skip == SKIP_YES)
6725 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006726 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006727 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006728 clear_ppconst(&ppconst);
6729 return NULL;
6730 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006731 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006732 if (scope->se_skip_save == SKIP_YES)
6733 clear_ppconst(&ppconst);
6734 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006735 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006736 int error = FALSE;
6737 int v;
6738
Bram Moolenaar7f141552020-05-09 17:35:53 +02006739 // The expression results in a constant.
6740 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006741 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6742 if (error)
6743 return NULL;
6744 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006745 clear_ppconst(&ppconst);
6746 scope->se_u.se_if.is_if_label = -1;
6747 }
6748 else
6749 {
6750 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006751 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006752 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006753 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006754 if (bool_on_stack(cctx) == FAIL)
6755 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006756
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006757 // "where" is set when ":elseif", "else" or ":endif" is found
6758 scope->se_u.se_if.is_if_label = instr->ga_len;
6759 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6760 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006761
6762 return p;
6763}
6764
6765 static char_u *
6766compile_else(char_u *arg, cctx_T *cctx)
6767{
6768 char_u *p = arg;
6769 garray_T *instr = &cctx->ctx_instr;
6770 isn_T *isn;
6771 scope_T *scope = cctx->ctx_scope;
6772
6773 if (scope == NULL || scope->se_type != IF_SCOPE)
6774 {
6775 emsg(_(e_else_without_if));
6776 return NULL;
6777 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006778 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006779 if (!cctx->ctx_had_return)
6780 scope->se_u.se_if.is_had_return = FALSE;
6781 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006782
Bram Moolenaarefd88552020-06-18 20:50:10 +02006783 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006784 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006785 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006786 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006787 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006788 if (!cctx->ctx_had_return
6789 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6790 JUMP_ALWAYS, cctx) == FAIL)
6791 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006792 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006793
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006794 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006795 {
6796 if (scope->se_u.se_if.is_if_label >= 0)
6797 {
6798 // previous "if" or "elseif" jumps here
6799 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6800 isn->isn_arg.jump.jump_where = instr->ga_len;
6801 scope->se_u.se_if.is_if_label = -1;
6802 }
6803 }
6804
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006805 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006806 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6807 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006808
6809 return p;
6810}
6811
6812 static char_u *
6813compile_endif(char_u *arg, cctx_T *cctx)
6814{
6815 scope_T *scope = cctx->ctx_scope;
6816 ifscope_T *ifscope;
6817 garray_T *instr = &cctx->ctx_instr;
6818 isn_T *isn;
6819
6820 if (scope == NULL || scope->se_type != IF_SCOPE)
6821 {
6822 emsg(_(e_endif_without_if));
6823 return NULL;
6824 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006825 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006826 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006827 if (!cctx->ctx_had_return)
6828 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006829
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006830 if (scope->se_u.se_if.is_if_label >= 0)
6831 {
6832 // previous "if" or "elseif" jumps here
6833 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6834 isn->isn_arg.jump.jump_where = instr->ga_len;
6835 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006836 // Fill in the "end" label in jumps at the end of the blocks.
6837 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006838 cctx->ctx_skip = scope->se_skip_save;
6839
6840 // If all the blocks end in :return and there is an :else then the
6841 // had_return flag is set.
6842 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006843
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006844 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006845 return arg;
6846}
6847
6848/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01006849 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006850 *
6851 * Produces instructions:
6852 * PUSHNR -1
6853 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01006854 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006855 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6856 * - if beyond end, jump to "end"
6857 * - otherwise get item from list and push it
6858 * STORE var Store item in "var"
6859 * ... body ...
6860 * JUMP top Jump back to repeat
6861 * end: DROP Drop the result of "expr"
6862 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006863 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
6864 * UNPACK 2 Split item in 2
6865 * STORE var1 Store item in "var1"
6866 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006867 */
6868 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006869compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006870{
Bram Moolenaar792f7862020-11-23 08:31:18 +01006871 char_u *arg;
6872 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006873 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006874 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006875 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006876 int var_count = 0;
6877 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006878 size_t varlen;
6879 garray_T *instr = &cctx->ctx_instr;
6880 garray_T *stack = &cctx->ctx_type_stack;
6881 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006882 lvar_T *loop_lvar; // loop iteration variable
6883 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006884 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006885 type_T *item_type = &t_any;
6886 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006887
Bram Moolenaar792f7862020-11-23 08:31:18 +01006888 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01006889 if (p == NULL)
6890 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006891 if (var_count == 0)
6892 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006893
6894 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006895 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006896 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6897 return NULL;
6898 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006899 {
6900 emsg(_(e_missing_in));
6901 return NULL;
6902 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006903 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006904 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6905 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006906
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006907 scope = new_scope(cctx, FOR_SCOPE);
6908 if (scope == NULL)
6909 return NULL;
6910
Bram Moolenaar792f7862020-11-23 08:31:18 +01006911 // Reserve a variable to store the loop iteration counter and initialize it
6912 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006913 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6914 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006915 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006916 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006917 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006918 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006919 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006920 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006921
6922 // compile "expr", it remains on the stack until "endfor"
6923 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006924 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006925 {
6926 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006927 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006928 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006929 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006930
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006931 // Now that we know the type of "var", check that it is a list, now or at
6932 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006933 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01006934 if (need_type(vartype, &t_list_any, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006935 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006936 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006937 return NULL;
6938 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006939
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006940 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006941 {
6942 if (var_count == 1)
6943 item_type = vartype->tt_member;
6944 else if (vartype->tt_member->tt_type == VAR_LIST
6945 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
6946 item_type = vartype->tt_member->tt_member;
6947 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006948
6949 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006950 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006951 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006952
Bram Moolenaar792f7862020-11-23 08:31:18 +01006953 arg = arg_start;
6954 if (var_count > 1)
6955 {
6956 generate_UNPACK(cctx, var_count, semicolon);
6957 arg = skipwhite(arg + 1); // skip white after '['
6958
6959 // the list item is replaced by a number of items
6960 if (ga_grow(stack, var_count - 1) == FAIL)
6961 {
6962 drop_scope(cctx);
6963 return NULL;
6964 }
6965 --stack->ga_len;
6966 for (idx = 0; idx < var_count; ++idx)
6967 {
6968 ((type_T **)stack->ga_data)[stack->ga_len] =
6969 (semicolon && idx == 0) ? vartype : item_type;
6970 ++stack->ga_len;
6971 }
6972 }
6973
6974 for (idx = 0; idx < var_count; ++idx)
6975 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006976 assign_dest_T dest = dest_local;
6977 int opt_flags = 0;
6978 int vimvaridx = -1;
6979 type_T *type = &t_any;
6980
6981 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006982 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006983 name = vim_strnsave(arg, varlen);
6984 if (name == NULL)
6985 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006986
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006987 // TODO: script var not supported?
6988 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
6989 &vimvaridx, &type, cctx) == FAIL)
6990 goto failed;
6991 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006992 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006993 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
6994 0, 0, type, name) == FAIL)
6995 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006996 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006997 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006998 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01006999 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007000 {
7001 semsg(_(e_variable_already_declared), arg);
7002 goto failed;
7003 }
7004
Bram Moolenaarea870692020-12-02 14:24:30 +01007005 if (STRNCMP(name, "s:", 2) == 0)
7006 {
7007 semsg(_(e_cannot_declare_script_variable_in_function), name);
7008 goto failed;
7009 }
7010
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007011 // Reserve a variable to store "var".
7012 // TODO: check for type
7013 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
7014 if (var_lvar == NULL)
7015 // out of memory or used as an argument
7016 goto failed;
7017
7018 if (semicolon && idx == var_count - 1)
7019 var_lvar->lv_type = vartype;
7020 else
7021 var_lvar->lv_type = item_type;
7022 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7023 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007024
Bram Moolenaar036d0712021-01-17 20:23:38 +01007025 if (*p == ':')
7026 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007027 if (*p == ',' || *p == ';')
7028 ++p;
7029 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007030 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007031 }
7032
7033 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007034
7035failed:
7036 vim_free(name);
7037 drop_scope(cctx);
7038 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007039}
7040
7041/*
7042 * compile "endfor"
7043 */
7044 static char_u *
7045compile_endfor(char_u *arg, cctx_T *cctx)
7046{
7047 garray_T *instr = &cctx->ctx_instr;
7048 scope_T *scope = cctx->ctx_scope;
7049 forscope_T *forscope;
7050 isn_T *isn;
7051
7052 if (scope == NULL || scope->se_type != FOR_SCOPE)
7053 {
7054 emsg(_(e_for));
7055 return NULL;
7056 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007057 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007058 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007059 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007060
7061 // At end of ":for" scope jump back to the FOR instruction.
7062 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7063
7064 // Fill in the "end" label in the FOR statement so it can jump here
7065 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7066 isn->isn_arg.forloop.for_end = instr->ga_len;
7067
7068 // Fill in the "end" label any BREAK statements
7069 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
7070
7071 // Below the ":for" scope drop the "expr" list from the stack.
7072 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7073 return NULL;
7074
7075 vim_free(scope);
7076
7077 return arg;
7078}
7079
7080/*
7081 * compile "while expr"
7082 *
7083 * Produces instructions:
7084 * top: EVAL expr Push result of "expr"
7085 * JUMP_IF_FALSE end jump if false
7086 * ... body ...
7087 * JUMP top Jump back to repeat
7088 * end:
7089 *
7090 */
7091 static char_u *
7092compile_while(char_u *arg, cctx_T *cctx)
7093{
7094 char_u *p = arg;
7095 garray_T *instr = &cctx->ctx_instr;
7096 scope_T *scope;
7097
7098 scope = new_scope(cctx, WHILE_SCOPE);
7099 if (scope == NULL)
7100 return NULL;
7101
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007102 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007103
7104 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007105 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007106 return NULL;
7107
Bram Moolenaar13106602020-10-04 16:06:05 +02007108 if (bool_on_stack(cctx) == FAIL)
7109 return FAIL;
7110
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007111 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007112 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007113 JUMP_IF_FALSE, cctx) == FAIL)
7114 return FAIL;
7115
7116 return p;
7117}
7118
7119/*
7120 * compile "endwhile"
7121 */
7122 static char_u *
7123compile_endwhile(char_u *arg, cctx_T *cctx)
7124{
7125 scope_T *scope = cctx->ctx_scope;
7126
7127 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7128 {
7129 emsg(_(e_while));
7130 return NULL;
7131 }
7132 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007133 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007134
7135 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007136 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007137
7138 // Fill in the "end" label in the WHILE statement so it can jump here.
7139 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007140 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007141
7142 vim_free(scope);
7143
7144 return arg;
7145}
7146
7147/*
7148 * compile "continue"
7149 */
7150 static char_u *
7151compile_continue(char_u *arg, cctx_T *cctx)
7152{
7153 scope_T *scope = cctx->ctx_scope;
7154
7155 for (;;)
7156 {
7157 if (scope == NULL)
7158 {
7159 emsg(_(e_continue));
7160 return NULL;
7161 }
7162 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7163 break;
7164 scope = scope->se_outer;
7165 }
7166
7167 // Jump back to the FOR or WHILE instruction.
7168 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007169 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
7170 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007171 return arg;
7172}
7173
7174/*
7175 * compile "break"
7176 */
7177 static char_u *
7178compile_break(char_u *arg, cctx_T *cctx)
7179{
7180 scope_T *scope = cctx->ctx_scope;
7181 endlabel_T **el;
7182
7183 for (;;)
7184 {
7185 if (scope == NULL)
7186 {
7187 emsg(_(e_break));
7188 return NULL;
7189 }
7190 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7191 break;
7192 scope = scope->se_outer;
7193 }
7194
7195 // Jump to the end of the FOR or WHILE loop.
7196 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007197 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007198 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007199 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007200 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7201 return FAIL;
7202
7203 return arg;
7204}
7205
7206/*
7207 * compile "{" start of block
7208 */
7209 static char_u *
7210compile_block(char_u *arg, cctx_T *cctx)
7211{
7212 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7213 return NULL;
7214 return skipwhite(arg + 1);
7215}
7216
7217/*
7218 * compile end of block: drop one scope
7219 */
7220 static void
7221compile_endblock(cctx_T *cctx)
7222{
7223 scope_T *scope = cctx->ctx_scope;
7224
7225 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007226 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007227 vim_free(scope);
7228}
7229
7230/*
7231 * compile "try"
7232 * Creates a new scope for the try-endtry, pointing to the first catch and
7233 * finally.
7234 * Creates another scope for the "try" block itself.
7235 * TRY instruction sets up exception handling at runtime.
7236 *
7237 * "try"
7238 * TRY -> catch1, -> finally push trystack entry
7239 * ... try block
7240 * "throw {exception}"
7241 * EVAL {exception}
7242 * THROW create exception
7243 * ... try block
7244 * " catch {expr}"
7245 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007246 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007247 * EVAL {expr}
7248 * MATCH
7249 * JUMP nomatch -> catch2
7250 * CATCH remove exception
7251 * ... catch block
7252 * " catch"
7253 * JUMP -> finally
7254 * catch2: CATCH remove exception
7255 * ... catch block
7256 * " finally"
7257 * finally:
7258 * ... finally block
7259 * " endtry"
7260 * ENDTRY pop trystack entry, may rethrow
7261 */
7262 static char_u *
7263compile_try(char_u *arg, cctx_T *cctx)
7264{
7265 garray_T *instr = &cctx->ctx_instr;
7266 scope_T *try_scope;
7267 scope_T *scope;
7268
7269 // scope that holds the jumps that go to catch/finally/endtry
7270 try_scope = new_scope(cctx, TRY_SCOPE);
7271 if (try_scope == NULL)
7272 return NULL;
7273
Bram Moolenaar69f70502021-01-01 16:10:46 +01007274 if (cctx->ctx_skip != SKIP_YES)
7275 {
7276 // "catch" is set when the first ":catch" is found.
7277 // "finally" is set when ":finally" or ":endtry" is found
7278 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
7279 if (generate_instr(cctx, ISN_TRY) == NULL)
7280 return NULL;
7281 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007282
7283 // scope for the try block itself
7284 scope = new_scope(cctx, BLOCK_SCOPE);
7285 if (scope == NULL)
7286 return NULL;
7287
7288 return arg;
7289}
7290
7291/*
7292 * compile "catch {expr}"
7293 */
7294 static char_u *
7295compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7296{
7297 scope_T *scope = cctx->ctx_scope;
7298 garray_T *instr = &cctx->ctx_instr;
7299 char_u *p;
7300 isn_T *isn;
7301
7302 // end block scope from :try or :catch
7303 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7304 compile_endblock(cctx);
7305 scope = cctx->ctx_scope;
7306
7307 // Error if not in a :try scope
7308 if (scope == NULL || scope->se_type != TRY_SCOPE)
7309 {
7310 emsg(_(e_catch));
7311 return NULL;
7312 }
7313
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007314 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007315 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007316 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007317 return NULL;
7318 }
7319
Bram Moolenaar69f70502021-01-01 16:10:46 +01007320 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007321 {
Bram Moolenaar69f70502021-01-01 16:10:46 +01007322 // Jump from end of previous block to :finally or :endtry
7323 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7324 JUMP_ALWAYS, cctx) == FAIL)
7325 return NULL;
7326
7327 // End :try or :catch scope: set value in ISN_TRY instruction
7328 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7329 if (isn->isn_arg.try.try_catch == 0)
7330 isn->isn_arg.try.try_catch = instr->ga_len;
7331 if (scope->se_u.se_try.ts_catch_label != 0)
7332 {
7333 // Previous catch without match jumps here
7334 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7335 isn->isn_arg.jump.jump_where = instr->ga_len;
7336 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007337 }
7338
7339 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007340 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007341 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007342 scope->se_u.se_try.ts_caught_all = TRUE;
7343 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007344 }
7345 else
7346 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007347 char_u *end;
7348 char_u *pat;
7349 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007350 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007351 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007352
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007353 // Push v:exception, push {expr} and MATCH
7354 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7355
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007356 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007357 if (*end != *p)
7358 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007359 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007360 vim_free(tofree);
7361 return FAIL;
7362 }
7363 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007364 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007365 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007366 len = (int)(end - tofree);
7367 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007368 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007369 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007370 if (pat == NULL)
7371 return FAIL;
7372 if (generate_PUSHS(cctx, pat) == FAIL)
7373 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007374
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007375 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7376 return NULL;
7377
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007378 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007379 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7380 return NULL;
7381 }
7382
Bram Moolenaar69f70502021-01-01 16:10:46 +01007383 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007384 return NULL;
7385
7386 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7387 return NULL;
7388 return p;
7389}
7390
7391 static char_u *
7392compile_finally(char_u *arg, cctx_T *cctx)
7393{
7394 scope_T *scope = cctx->ctx_scope;
7395 garray_T *instr = &cctx->ctx_instr;
7396 isn_T *isn;
7397
7398 // end block scope from :try or :catch
7399 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7400 compile_endblock(cctx);
7401 scope = cctx->ctx_scope;
7402
7403 // Error if not in a :try scope
7404 if (scope == NULL || scope->se_type != TRY_SCOPE)
7405 {
7406 emsg(_(e_finally));
7407 return NULL;
7408 }
7409
7410 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007411 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007412 if (isn->isn_arg.try.try_finally != 0)
7413 {
7414 emsg(_(e_finally_dup));
7415 return NULL;
7416 }
7417
7418 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007419 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007420
Bram Moolenaar585fea72020-04-02 22:33:21 +02007421 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007422 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007423 {
7424 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007425 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007426 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007427 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007428 }
7429
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007430 // TODO: set index in ts_finally_label jumps
7431
7432 return arg;
7433}
7434
7435 static char_u *
7436compile_endtry(char_u *arg, cctx_T *cctx)
7437{
7438 scope_T *scope = cctx->ctx_scope;
7439 garray_T *instr = &cctx->ctx_instr;
7440 isn_T *isn;
7441
7442 // end block scope from :catch or :finally
7443 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7444 compile_endblock(cctx);
7445 scope = cctx->ctx_scope;
7446
7447 // Error if not in a :try scope
7448 if (scope == NULL || scope->se_type != TRY_SCOPE)
7449 {
7450 if (scope == NULL)
7451 emsg(_(e_no_endtry));
7452 else if (scope->se_type == WHILE_SCOPE)
7453 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007454 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007455 emsg(_(e_endfor));
7456 else
7457 emsg(_(e_endif));
7458 return NULL;
7459 }
7460
Bram Moolenaar69f70502021-01-01 16:10:46 +01007461 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007462 {
Bram Moolenaar69f70502021-01-01 16:10:46 +01007463 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7464 if (isn->isn_arg.try.try_catch == 0
7465 && isn->isn_arg.try.try_finally == 0)
7466 {
7467 emsg(_(e_missing_catch_or_finally));
7468 return NULL;
7469 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007470
Bram Moolenaar69f70502021-01-01 16:10:46 +01007471 // Fill in the "end" label in jumps at the end of the blocks, if not
7472 // done by ":finally".
7473 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007474
Bram Moolenaar69f70502021-01-01 16:10:46 +01007475 // End :catch or :finally scope: set value in ISN_TRY instruction
7476 if (isn->isn_arg.try.try_catch == 0)
7477 isn->isn_arg.try.try_catch = instr->ga_len;
7478 if (isn->isn_arg.try.try_finally == 0)
7479 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007480
Bram Moolenaar69f70502021-01-01 16:10:46 +01007481 if (scope->se_u.se_try.ts_catch_label != 0)
7482 {
7483 // Last catch without match jumps here
7484 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7485 isn->isn_arg.jump.jump_where = instr->ga_len;
7486 }
Bram Moolenaare8593122020-07-18 15:17:02 +02007487 }
7488
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007489 compile_endblock(cctx);
7490
Bram Moolenaar69f70502021-01-01 16:10:46 +01007491 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_ENDTRY) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007492 return NULL;
7493 return arg;
7494}
7495
7496/*
7497 * compile "throw {expr}"
7498 */
7499 static char_u *
7500compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7501{
7502 char_u *p = skipwhite(arg);
7503
Bram Moolenaara5565e42020-05-09 15:44:01 +02007504 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007505 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01007506 if (cctx->ctx_skip == SKIP_YES)
7507 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007508 if (may_generate_2STRING(-1, cctx) == FAIL)
7509 return NULL;
7510 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7511 return NULL;
7512
7513 return p;
7514}
7515
7516/*
7517 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007518 * compile "echomsg expr"
7519 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007520 * compile "execute expr"
7521 */
7522 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007523compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007524{
7525 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007526 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007527 int count = 0;
7528
7529 for (;;)
7530 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007531 if (ends_excmd2(prev, p))
7532 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007533 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007534 return NULL;
7535 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007536 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007537 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007538 }
7539
Bram Moolenaare4984292020-12-13 14:19:25 +01007540 if (count > 0)
7541 {
7542 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7543 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7544 else if (cmdidx == CMD_execute)
7545 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7546 else if (cmdidx == CMD_echomsg)
7547 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7548 else
7549 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7550 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007551 return p;
7552}
7553
7554/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007555 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01007556 * instruction to compute it and return OK.
7557 * Otherwise return FAIL, the caller must deal with any range.
7558 */
7559 static int
7560compile_variable_range(exarg_T *eap, cctx_T *cctx)
7561{
7562 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7563 char_u *p = skipdigits(eap->cmd);
7564
7565 if (p == range_end)
7566 return FAIL;
7567 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7568}
7569
7570/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007571 * :put r
7572 * :put ={expr}
7573 */
7574 static char_u *
7575compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7576{
7577 char_u *line = arg;
7578 linenr_T lnum;
7579 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007580 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007581
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007582 eap->regname = *line;
7583
7584 if (eap->regname == '=')
7585 {
7586 char_u *p = line + 1;
7587
7588 if (compile_expr0(&p, cctx) == FAIL)
7589 return NULL;
7590 line = p;
7591 }
7592 else if (eap->regname != NUL)
7593 ++line;
7594
Bram Moolenaar08597872020-12-10 19:43:40 +01007595 if (compile_variable_range(eap, cctx) == OK)
7596 {
7597 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
7598 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007599 else
Bram Moolenaar08597872020-12-10 19:43:40 +01007600 {
7601 // Either no range or a number.
7602 // "errormsg" will not be set because the range is ADDR_LINES.
7603 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01007604 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01007605 return NULL;
7606 if (eap->addr_count == 0)
7607 lnum = -1;
7608 else
7609 lnum = eap->line2;
7610 if (above)
7611 --lnum;
7612 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007613
7614 generate_PUT(cctx, eap->regname, lnum);
7615 return line;
7616}
7617
7618/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007619 * A command that is not compiled, execute with legacy code.
7620 */
7621 static char_u *
7622compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7623{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007624 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007625 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007626 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007627
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007628 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007629 goto theend;
7630
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007631 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007632 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007633 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007634 int usefilter = FALSE;
7635
7636 has_expr = argt & (EX_XFILE | EX_EXPAND);
7637
7638 // If the command can be followed by a bar, find the bar and truncate
7639 // it, so that the following command can be compiled.
7640 // The '|' is overwritten with a NUL, it is put back below.
7641 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7642 && *eap->arg == '!')
7643 // :w !filter or :r !filter or :r! filter
7644 usefilter = TRUE;
7645 if ((argt & EX_TRLBAR) && !usefilter)
7646 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007647 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007648 separate_nextcmd(eap);
7649 if (eap->nextcmd != NULL)
7650 nextcmd = eap->nextcmd;
7651 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01007652 else if (eap->cmdidx == CMD_wincmd)
7653 {
7654 p = eap->arg;
7655 if (*p != NUL)
7656 ++p;
7657 if (*p == 'g' || *p == Ctrl_G)
7658 ++p;
7659 p = skipwhite(p);
7660 if (*p == '|')
7661 {
7662 *p = NUL;
7663 nextcmd = p + 1;
7664 }
7665 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007666 }
7667
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007668 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
7669 {
7670 // expand filename in "syntax include [@group] filename"
7671 has_expr = TRUE;
7672 eap->arg = skipwhite(eap->arg + 7);
7673 if (*eap->arg == '@')
7674 eap->arg = skiptowhite(eap->arg);
7675 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007676
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007677 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
7678 && STRLEN(eap->arg) > 4)
7679 {
7680 int delim = *eap->arg;
7681
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007682 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007683 if (*p == delim)
7684 {
7685 eap->arg = p + 1;
7686 has_expr = TRUE;
7687 }
7688 }
7689
Bram Moolenaarecac5912021-01-05 19:23:28 +01007690 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
7691 {
7692 // TODO: should only expand when appropriate for the command
7693 eap->arg = skiptowhite(eap->arg);
7694 has_expr = TRUE;
7695 }
7696
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007697 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007698 {
7699 int count = 0;
7700 char_u *start = skipwhite(line);
7701
7702 // :cmd xxx`=expr1`yyy`=expr2`zzz
7703 // PUSHS ":cmd xxx"
7704 // eval expr1
7705 // PUSHS "yyy"
7706 // eval expr2
7707 // PUSHS "zzz"
7708 // EXECCONCAT 5
7709 for (;;)
7710 {
7711 if (p > start)
7712 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007713 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007714 ++count;
7715 }
7716 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007717 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007718 return NULL;
7719 may_generate_2STRING(-1, cctx);
7720 ++count;
7721 p = skipwhite(p);
7722 if (*p != '`')
7723 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007724 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007725 return NULL;
7726 }
7727 start = p + 1;
7728
7729 p = (char_u *)strstr((char *)start, "`=");
7730 if (p == NULL)
7731 {
7732 if (*skipwhite(start) != NUL)
7733 {
7734 generate_PUSHS(cctx, vim_strsave(start));
7735 ++count;
7736 }
7737 break;
7738 }
7739 }
7740 generate_EXECCONCAT(cctx, count);
7741 }
7742 else
7743 generate_EXEC(cctx, line);
7744
7745theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007746 if (*nextcmd != NUL)
7747 {
7748 // the parser expects a pointer to the bar, put it back
7749 --nextcmd;
7750 *nextcmd = '|';
7751 }
7752
7753 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007754}
7755
7756/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007757 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007758 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007759 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007760 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007761add_def_function(ufunc_T *ufunc)
7762{
7763 dfunc_T *dfunc;
7764
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007765 if (def_functions.ga_len == 0)
7766 {
7767 // The first position is not used, so that a zero uf_dfunc_idx means it
7768 // wasn't set.
7769 if (ga_grow(&def_functions, 1) == FAIL)
7770 return FAIL;
7771 ++def_functions.ga_len;
7772 }
7773
Bram Moolenaar09689a02020-05-09 22:50:08 +02007774 // Add the function to "def_functions".
7775 if (ga_grow(&def_functions, 1) == FAIL)
7776 return FAIL;
7777 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7778 CLEAR_POINTER(dfunc);
7779 dfunc->df_idx = def_functions.ga_len;
7780 ufunc->uf_dfunc_idx = dfunc->df_idx;
7781 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01007782 dfunc->df_name = vim_strsave(ufunc->uf_name);
7783 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007784 ++def_functions.ga_len;
7785 return OK;
7786}
7787
7788/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007789 * After ex_function() has collected all the function lines: parse and compile
7790 * the lines into instructions.
7791 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007792 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
7793 * the return statement (used for lambda). When uf_ret_type is already set
7794 * then check that it matches.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007795 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007796 * This can be used recursively through compile_lambda(), which may reallocate
7797 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007798 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007799 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007800 int
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007801compile_def_function(ufunc_T *ufunc, int check_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007802{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007803 char_u *line = NULL;
7804 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007805 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007806 cctx_T cctx;
7807 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007808 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007809 int ret = FAIL;
7810 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007811 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007812 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007813 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007814
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007815 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01007816 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007817 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007818 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007819 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7820 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01007821 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007822 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007823 else
7824 {
7825 if (add_def_function(ufunc) == FAIL)
7826 return FAIL;
7827 new_def_function = TRUE;
7828 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007829
Bram Moolenaar985116a2020-07-12 17:31:09 +02007830 ufunc->uf_def_status = UF_COMPILING;
7831
Bram Moolenaara80faa82020-04-12 19:37:17 +02007832 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007833 cctx.ctx_ufunc = ufunc;
7834 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007835 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007836 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7837 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7838 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7839 cctx.ctx_type_list = &ufunc->uf_type_list;
7840 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7841 instr = &cctx.ctx_instr;
7842
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007843 // Set the context to the function, it may be compiled when called from
7844 // another script. Set the script version to the most modern one.
7845 // The line number will be set in next_line_from_context().
7846 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007847 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7848
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007849 // Make sure error messages are OK.
7850 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7851 if (do_estack_push)
7852 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007853 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007854
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007855 if (ufunc->uf_def_args.ga_len > 0)
7856 {
7857 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007858 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007859 int i;
7860 char_u *arg;
7861 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007862 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007863
7864 // Produce instructions for the default values of optional arguments.
7865 // Store the instruction index in uf_def_arg_idx[] so that we know
7866 // where to start when the function is called, depending on the number
7867 // of arguments.
7868 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7869 if (ufunc->uf_def_arg_idx == NULL)
7870 goto erret;
7871 for (i = 0; i < count; ++i)
7872 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007873 garray_T *stack = &cctx.ctx_type_stack;
7874 type_T *val_type;
7875 int arg_idx = first_def_arg + i;
7876
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007877 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7878 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007879 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007880 goto erret;
7881
7882 // If no type specified use the type of the default value.
7883 // Otherwise check that the default value type matches the
7884 // specified type.
7885 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7886 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007887 {
7888 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007889 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007890 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007891 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7892 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007893 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007894
7895 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007896 goto erret;
7897 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007898 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007899
7900 if (did_set_arg_type)
7901 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007902 }
7903
7904 /*
7905 * Loop over all the lines of the function and generate instructions.
7906 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007907 for (;;)
7908 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007909 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007910 int starts_with_colon = FALSE;
7911 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007912 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007913
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007914 // Bail out on the first error to avoid a flood of errors and report
7915 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007916 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007917 goto erret;
7918
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007919 if (line != NULL && *line == '|')
7920 // the line continues after a '|'
7921 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007922 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007923 && !(*line == '#' && (line == cctx.ctx_line_start
7924 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007925 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007926 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007927 goto erret;
7928 }
7929 else
7930 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007931 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007932 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007933 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007934 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007935 }
7936
Bram Moolenaara80faa82020-04-12 19:37:17 +02007937 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007938 ea.cmdlinep = &line;
7939 ea.cmd = skipwhite(line);
7940
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007941 // Some things can be recognized by the first character.
7942 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007943 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007944 case '#':
Bram Moolenaare0de1712020-12-02 17:36:54 +01007945 // "#" starts a comment
7946 line = (char_u *)"";
7947 continue;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007948
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007949 case '}':
7950 {
7951 // "}" ends a block scope
7952 scopetype_T stype = cctx.ctx_scope == NULL
7953 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007954
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007955 if (stype == BLOCK_SCOPE)
7956 {
7957 compile_endblock(&cctx);
7958 line = ea.cmd;
7959 }
7960 else
7961 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007962 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007963 goto erret;
7964 }
7965 if (line != NULL)
7966 line = skipwhite(ea.cmd + 1);
7967 continue;
7968 }
7969
7970 case '{':
7971 // "{" starts a block scope
7972 // "{'a': 1}->func() is something else
7973 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7974 {
7975 line = compile_block(ea.cmd, &cctx);
7976 continue;
7977 }
7978 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007979 }
7980
7981 /*
7982 * COMMAND MODIFIERS
7983 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007984 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007985 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7986 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007987 {
7988 if (errormsg != NULL)
7989 goto erret;
7990 // empty line or comment
7991 line = (char_u *)"";
7992 continue;
7993 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007994 generate_cmdmods(&cctx, &local_cmdmod);
7995 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007996
Bram Moolenaare88c8e82020-11-01 17:03:37 +01007997 // Check if there was a colon after the last command modifier or before
7998 // the current position.
7999 for (p = ea.cmd; p >= line; --p)
8000 {
8001 if (*p == ':')
8002 starts_with_colon = TRUE;
8003 if (p < ea.cmd && !VIM_ISWHITE(*p))
8004 break;
8005 }
8006
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008007 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008008 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008009 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008010 {
8011 if (*ea.cmd == '(')
8012 // not for "call()"
8013 ea.cmd = p;
8014 else
8015 ea.cmd = skipwhite(ea.cmd);
8016 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008017
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008018 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008019 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008020 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008021
Bram Moolenaar17126b12021-01-07 22:03:02 +01008022 // Check for assignment after command modifiers.
8023 assign = may_compile_assignment(&ea, &line, &cctx);
8024 if (assign == OK)
8025 goto nextline;
8026 if (assign == FAIL)
8027 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008028 }
8029
8030 /*
8031 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008032 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008033 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008034 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008035 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008036 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008037 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008038 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008039 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008040 if (!starts_with_colon)
8041 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008042 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008043 goto erret;
8044 }
8045 if (ends_excmd2(line, ea.cmd))
8046 {
8047 // A range without a command: jump to the line.
8048 // TODO: compile to a more efficient command, possibly
8049 // calling parse_cmd_address().
8050 ea.cmdidx = CMD_SIZE;
8051 line = compile_exec(line, &ea, &cctx);
8052 goto nextline;
8053 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008054 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008055 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008056 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +01008057 : (int (*)(char_u *, size_t, void *, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008058 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008059
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008060 if (p == NULL)
8061 {
8062 if (cctx.ctx_skip != SKIP_YES)
8063 emsg(_(e_ambiguous_use_of_user_defined_command));
8064 goto erret;
8065 }
8066
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008067 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8068 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008069 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008070 {
8071 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008072 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008073 }
8074
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008075 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008076 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008077 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008078 // CMD_var cannot happen, compile_assignment() above would be
8079 // used. Most likely an assignment to a non-existing variable.
8080 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008081 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008082 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008083 }
8084
Bram Moolenaar3988f642020-08-27 22:43:03 +02008085 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008086 && ea.cmdidx != CMD_elseif
8087 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008088 && ea.cmdidx != CMD_endif
8089 && ea.cmdidx != CMD_endfor
8090 && ea.cmdidx != CMD_endwhile
8091 && ea.cmdidx != CMD_catch
8092 && ea.cmdidx != CMD_finally
8093 && ea.cmdidx != CMD_endtry)
8094 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008095 emsg(_(e_unreachable_code_after_return));
8096 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008097 }
8098
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008099 p = skipwhite(p);
8100 if (ea.cmdidx != CMD_SIZE
8101 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8102 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008103 if (ea.cmdidx >= 0)
8104 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008105 if ((ea.argt & EX_BANG) && *p == '!')
8106 {
8107 ea.forceit = TRUE;
8108 p = skipwhite(p + 1);
8109 }
8110 }
8111
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008112 switch (ea.cmdidx)
8113 {
8114 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008115 ea.arg = p;
8116 line = compile_nested_function(&ea, &cctx);
8117 break;
8118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008119 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008120 // TODO: should we allow this, e.g. to declare a global
8121 // function?
8122 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008123 goto erret;
8124
8125 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008126 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008127 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008128 break;
8129
8130 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008131 emsg(_(e_cannot_use_let_in_vim9_script));
8132 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008133 case CMD_var:
8134 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008135 case CMD_const:
8136 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008137 if (line == p)
8138 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008139 break;
8140
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008141 case CMD_unlet:
8142 case CMD_unlockvar:
8143 case CMD_lockvar:
8144 line = compile_unletlock(p, &ea, &cctx);
8145 break;
8146
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008147 case CMD_import:
8148 line = compile_import(p, &cctx);
8149 break;
8150
8151 case CMD_if:
8152 line = compile_if(p, &cctx);
8153 break;
8154 case CMD_elseif:
8155 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008156 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008157 break;
8158 case CMD_else:
8159 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008160 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008161 break;
8162 case CMD_endif:
8163 line = compile_endif(p, &cctx);
8164 break;
8165
8166 case CMD_while:
8167 line = compile_while(p, &cctx);
8168 break;
8169 case CMD_endwhile:
8170 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008171 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008172 break;
8173
8174 case CMD_for:
8175 line = compile_for(p, &cctx);
8176 break;
8177 case CMD_endfor:
8178 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008179 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008180 break;
8181 case CMD_continue:
8182 line = compile_continue(p, &cctx);
8183 break;
8184 case CMD_break:
8185 line = compile_break(p, &cctx);
8186 break;
8187
8188 case CMD_try:
8189 line = compile_try(p, &cctx);
8190 break;
8191 case CMD_catch:
8192 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008193 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008194 break;
8195 case CMD_finally:
8196 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008197 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008198 break;
8199 case CMD_endtry:
8200 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008201 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008202 break;
8203 case CMD_throw:
8204 line = compile_throw(p, &cctx);
8205 break;
8206
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008207 case CMD_eval:
8208 if (compile_expr0(&p, &cctx) == FAIL)
8209 goto erret;
8210
Bram Moolenaar3988f642020-08-27 22:43:03 +02008211 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008212 generate_instr_drop(&cctx, ISN_DROP, 1);
8213
8214 line = skipwhite(p);
8215 break;
8216
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008217 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008218 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008219 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008220 case CMD_echomsg:
8221 case CMD_echoerr:
8222 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008223 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008224
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008225 case CMD_put:
8226 ea.cmd = cmd;
8227 line = compile_put(p, &ea, &cctx);
8228 break;
8229
Bram Moolenaar3988f642020-08-27 22:43:03 +02008230 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008231
Bram Moolenaarae616492020-07-28 20:07:27 +02008232 case CMD_append:
8233 case CMD_change:
8234 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008235 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008236 case CMD_xit:
8237 not_in_vim9(&ea);
8238 goto erret;
8239
Bram Moolenaar002262f2020-07-08 17:47:57 +02008240 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008241 if (cctx.ctx_skip != SKIP_YES)
8242 {
8243 semsg(_(e_invalid_command_str), ea.cmd);
8244 goto erret;
8245 }
8246 // We don't check for a next command here.
8247 line = (char_u *)"";
8248 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008249
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008250 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008251 if (cctx.ctx_skip == SKIP_YES)
8252 {
8253 // We don't check for a next command here.
8254 line = (char_u *)"";
8255 }
8256 else
8257 {
8258 // Not recognized, execute with do_cmdline_cmd().
8259 ea.arg = p;
8260 line = compile_exec(line, &ea, &cctx);
8261 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008262 break;
8263 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008264nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008265 if (line == NULL)
8266 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008267 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008268
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008269 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008270 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008271
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008272 if (cctx.ctx_type_stack.ga_len < 0)
8273 {
8274 iemsg("Type stack underflow");
8275 goto erret;
8276 }
8277 }
8278
8279 if (cctx.ctx_scope != NULL)
8280 {
8281 if (cctx.ctx_scope->se_type == IF_SCOPE)
8282 emsg(_(e_endif));
8283 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8284 emsg(_(e_endwhile));
8285 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8286 emsg(_(e_endfor));
8287 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008288 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008289 goto erret;
8290 }
8291
Bram Moolenaarefd88552020-06-18 20:50:10 +02008292 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008293 {
8294 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8295 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008296 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008297 goto erret;
8298 }
8299
8300 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008301 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008302 }
8303
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008304 {
8305 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8306 + ufunc->uf_dfunc_idx;
8307 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008308 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008309 dfunc->df_instr = instr->ga_data;
8310 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008311 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008312 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008313 if (cctx.ctx_outer_used)
8314 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008315 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008316 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008317
8318 ret = OK;
8319
8320erret:
8321 if (ret == FAIL)
8322 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008323 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008324 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8325 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008326
8327 for (idx = 0; idx < instr->ga_len; ++idx)
8328 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008329 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008330 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008331
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008332 // If using the last entry in the table and it was added above, we
8333 // might as well remove it.
8334 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008335 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008336 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008337 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008338 ufunc->uf_dfunc_idx = 0;
8339 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008340 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008341
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008342 while (cctx.ctx_scope != NULL)
8343 drop_scope(&cctx);
8344
Bram Moolenaar20431c92020-03-20 18:39:46 +01008345 // Don't execute this function body.
8346 ga_clear_strings(&ufunc->uf_lines);
8347
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008348 if (errormsg != NULL)
8349 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008350 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008351 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008352 }
8353
8354 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008355 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008356 if (do_estack_push)
8357 estack_pop();
8358
Bram Moolenaar20431c92020-03-20 18:39:46 +01008359 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008360 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008361 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008362 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008363}
8364
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008365 void
8366set_function_type(ufunc_T *ufunc)
8367{
8368 int varargs = ufunc->uf_va_name != NULL;
8369 int argcount = ufunc->uf_args.ga_len;
8370
8371 // Create a type for the function, with the return type and any
8372 // argument types.
8373 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8374 // The type is included in "tt_args".
8375 if (argcount > 0 || varargs)
8376 {
8377 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8378 argcount, &ufunc->uf_type_list);
8379 // Add argument types to the function type.
8380 if (func_type_add_arg_types(ufunc->uf_func_type,
8381 argcount + varargs,
8382 &ufunc->uf_type_list) == FAIL)
8383 return;
8384 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8385 ufunc->uf_func_type->tt_min_argcount =
8386 argcount - ufunc->uf_def_args.ga_len;
8387 if (ufunc->uf_arg_types == NULL)
8388 {
8389 int i;
8390
8391 // lambda does not have argument types.
8392 for (i = 0; i < argcount; ++i)
8393 ufunc->uf_func_type->tt_args[i] = &t_any;
8394 }
8395 else
8396 mch_memmove(ufunc->uf_func_type->tt_args,
8397 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8398 if (varargs)
8399 {
8400 ufunc->uf_func_type->tt_args[argcount] =
8401 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8402 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8403 }
8404 }
8405 else
8406 // No arguments, can use a predefined type.
8407 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8408 argcount, &ufunc->uf_type_list);
8409}
8410
8411
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008412/*
8413 * Delete an instruction, free what it contains.
8414 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008415 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008416delete_instr(isn_T *isn)
8417{
8418 switch (isn->isn_type)
8419 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008420 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008421 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008422 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008423 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008424 case ISN_LOADENV:
8425 case ISN_LOADG:
8426 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008427 case ISN_LOADT:
8428 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008429 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008430 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008431 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008432 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008433 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008434 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008435 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008436 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008437 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008438 case ISN_STOREW:
8439 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008440 vim_free(isn->isn_arg.string);
8441 break;
8442
8443 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008444 case ISN_STORES:
8445 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008446 break;
8447
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008448 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008449 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008450 vim_free(isn->isn_arg.unlet.ul_name);
8451 break;
8452
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008453 case ISN_STOREOPT:
8454 vim_free(isn->isn_arg.storeopt.so_name);
8455 break;
8456
8457 case ISN_PUSHBLOB: // push blob isn_arg.blob
8458 blob_unref(isn->isn_arg.blob);
8459 break;
8460
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008461 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008462#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008463 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008464#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008465 break;
8466
8467 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008468#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008469 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008470#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008471 break;
8472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008473 case ISN_UCALL:
8474 vim_free(isn->isn_arg.ufunc.cuf_name);
8475 break;
8476
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008477 case ISN_FUNCREF:
8478 {
8479 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8480 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01008481 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008482
Bram Moolenaar077a4232020-12-22 18:33:27 +01008483 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
8484 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02008485 }
8486 break;
8487
8488 case ISN_DCALL:
8489 {
8490 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8491 + isn->isn_arg.dfunc.cdf_idx;
8492
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008493 if (dfunc->df_ufunc != NULL
8494 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008495 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008496 }
8497 break;
8498
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008499 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008500 {
8501 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8502 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8503
8504 if (ufunc != NULL)
8505 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008506 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02008507 func_ptr_unref(ufunc);
8508 }
8509
8510 vim_free(lambda);
8511 vim_free(isn->isn_arg.newfunc.nf_global);
8512 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008513 break;
8514
Bram Moolenaar5e654232020-09-16 15:22:00 +02008515 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01008516 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02008517 free_type(isn->isn_arg.type.ct_type);
8518 break;
8519
Bram Moolenaar02194d22020-10-24 23:08:38 +02008520 case ISN_CMDMOD:
8521 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8522 ->cmod_filter_regmatch.regprog);
8523 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8524 break;
8525
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008526 case ISN_LOADSCRIPT:
8527 case ISN_STORESCRIPT:
8528 vim_free(isn->isn_arg.script.scriptref);
8529 break;
8530
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008531 case ISN_2BOOL:
8532 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008533 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008534 case ISN_ADDBLOB:
8535 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008536 case ISN_ANYINDEX:
8537 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008538 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008539 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008540 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008541 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008542 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008543 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008544 case ISN_COMPAREANY:
8545 case ISN_COMPAREBLOB:
8546 case ISN_COMPAREBOOL:
8547 case ISN_COMPAREDICT:
8548 case ISN_COMPAREFLOAT:
8549 case ISN_COMPAREFUNC:
8550 case ISN_COMPARELIST:
8551 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008552 case ISN_COMPARESPECIAL:
8553 case ISN_COMPARESTRING:
8554 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008555 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008556 case ISN_DROP:
8557 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008558 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008559 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008560 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008561 case ISN_EXECCONCAT:
8562 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008563 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008564 case ISN_GETITEM:
8565 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008566 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008567 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008568 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008569 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008570 case ISN_LOADBDICT:
8571 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008572 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008573 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008574 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008575 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008576 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008577 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008578 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008579 case ISN_NEGATENR:
8580 case ISN_NEWDICT:
8581 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008582 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008583 case ISN_OPFLOAT:
8584 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008585 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008586 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008587 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008588 case ISN_PUSHF:
8589 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008590 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008591 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008592 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01008593 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008594 case ISN_SHUFFLE:
8595 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008596 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01008597 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008598 case ISN_STORENR:
8599 case ISN_STOREOUTER:
8600 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008601 case ISN_STOREV:
8602 case ISN_STRINDEX:
8603 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008604 case ISN_THROW:
8605 case ISN_TRY:
Bram Moolenaar752fc692021-01-04 21:57:11 +01008606 case ISN_UNLETINDEX:
Bram Moolenaar792f7862020-11-23 08:31:18 +01008607 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008608 // nothing allocated
8609 break;
8610 }
8611}
8612
8613/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008614 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01008615 */
8616 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008617delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008618{
8619 int idx;
8620
8621 ga_clear(&dfunc->df_def_args_isn);
8622
8623 if (dfunc->df_instr != NULL)
8624 {
8625 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
8626 delete_instr(dfunc->df_instr + idx);
8627 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008628 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008629 }
8630
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008631 if (mark_deleted)
8632 dfunc->df_deleted = TRUE;
8633 if (dfunc->df_ufunc != NULL)
8634 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008635}
8636
8637/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008638 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008639 * function, unless another user function still uses it.
8640 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008641 */
8642 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008643unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008644{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008645 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008646 {
8647 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8648 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008649
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008650 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008651 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008652 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008653 ufunc->uf_dfunc_idx = 0;
8654 if (dfunc->df_ufunc == ufunc)
8655 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008656 }
8657}
8658
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008659/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008660 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008661 */
8662 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008663link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008664{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008665 if (ufunc->uf_dfunc_idx > 0)
8666 {
8667 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8668 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008669
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008670 ++dfunc->df_refcount;
8671 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008672}
8673
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008674#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008675/*
8676 * Free all functions defined with ":def".
8677 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008678 void
8679free_def_functions(void)
8680{
Bram Moolenaar20431c92020-03-20 18:39:46 +01008681 int idx;
8682
8683 for (idx = 0; idx < def_functions.ga_len; ++idx)
8684 {
8685 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
8686
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008687 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008688 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008689 }
8690
8691 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008692}
8693#endif
8694
8695
8696#endif // FEAT_EVAL