blob: bcbc57dd1247c40d464c85c3ac30b18d337987c1 [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;
477 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
478
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200479 switch ((*type)->tt_type)
480 {
481 // nothing to be done
482 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100483
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200484 // conversion possible
485 case VAR_SPECIAL:
486 case VAR_BOOL:
487 case VAR_NUMBER:
488 case VAR_FLOAT:
489 break;
490
491 // conversion possible (with runtime check)
492 case VAR_ANY:
493 case VAR_UNKNOWN:
494 isntype = ISN_2STRING_ANY;
495 break;
496
497 // conversion not possible
498 case VAR_VOID:
499 case VAR_BLOB:
500 case VAR_FUNC:
501 case VAR_PARTIAL:
502 case VAR_LIST:
503 case VAR_DICT:
504 case VAR_JOB:
505 case VAR_CHANNEL:
506 to_string_error((*type)->tt_type);
507 return FAIL;
508 }
509
510 *type = &t_string;
511 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100512 return FAIL;
513 isn->isn_arg.number = offset;
514
515 return OK;
516}
517
518 static int
519check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
520{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200521 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100522 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200523 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524 {
525 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200526 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200528 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 return FAIL;
530 }
531 return OK;
532}
533
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200534 static int
535generate_add_instr(
536 cctx_T *cctx,
537 vartype_T vartype,
538 type_T *type1,
539 type_T *type2)
540{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100541 garray_T *stack = &cctx->ctx_type_stack;
542 isn_T *isn = generate_instr_drop(cctx,
543 vartype == VAR_NUMBER ? ISN_OPNR
544 : vartype == VAR_LIST ? ISN_ADDLIST
545 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200546#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100547 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200548#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100549 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200550
551 if (vartype != VAR_LIST && vartype != VAR_BLOB
552 && type1->tt_type != VAR_ANY
553 && type2->tt_type != VAR_ANY
554 && check_number_or_float(
555 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
556 return FAIL;
557
558 if (isn != NULL)
559 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100560
561 // When concatenating two lists with different member types the member type
562 // becomes "any".
563 if (vartype == VAR_LIST
564 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
565 && type1->tt_member != type2->tt_member)
566 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
567
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200568 return isn == NULL ? FAIL : OK;
569}
570
571/*
572 * Get the type to use for an instruction for an operation on "type1" and
573 * "type2". If they are matching use a type-specific instruction. Otherwise
574 * fall back to runtime type checking.
575 */
576 static vartype_T
577operator_type(type_T *type1, type_T *type2)
578{
579 if (type1->tt_type == type2->tt_type
580 && (type1->tt_type == VAR_NUMBER
581 || type1->tt_type == VAR_LIST
582#ifdef FEAT_FLOAT
583 || type1->tt_type == VAR_FLOAT
584#endif
585 || type1->tt_type == VAR_BLOB))
586 return type1->tt_type;
587 return VAR_ANY;
588}
589
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100590/*
591 * Generate an instruction with two arguments. The instruction depends on the
592 * type of the arguments.
593 */
594 static int
595generate_two_op(cctx_T *cctx, char_u *op)
596{
597 garray_T *stack = &cctx->ctx_type_stack;
598 type_T *type1;
599 type_T *type2;
600 vartype_T vartype;
601 isn_T *isn;
602
Bram Moolenaar080457c2020-03-03 21:53:32 +0100603 RETURN_OK_IF_SKIP(cctx);
604
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200605 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100606 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
607 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200608 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100609
610 switch (*op)
611 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200612 case '+':
613 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615 break;
616
617 case '-':
618 case '*':
619 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
620 op) == FAIL)
621 return FAIL;
622 if (vartype == VAR_NUMBER)
623 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
624#ifdef FEAT_FLOAT
625 else if (vartype == VAR_FLOAT)
626 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
627#endif
628 else
629 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
630 if (isn != NULL)
631 isn->isn_arg.op.op_type = *op == '*'
632 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
633 break;
634
Bram Moolenaar4c683752020-04-05 21:38:23 +0200635 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100636 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200637 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100638 && type2->tt_type != VAR_NUMBER))
639 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200640 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100641 return FAIL;
642 }
643 isn = generate_instr_drop(cctx,
644 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
645 if (isn != NULL)
646 isn->isn_arg.op.op_type = EXPR_REM;
647 break;
648 }
649
650 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200651 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100652 {
653 type_T *type = &t_any;
654
655#ifdef FEAT_FLOAT
656 // float+number and number+float results in float
657 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
658 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
659 type = &t_float;
660#endif
661 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
662 }
663
664 return OK;
665}
666
667/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200668 * Get the instruction to use for comparing "type1" with "type2"
669 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200671 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100672get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673{
674 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675
Bram Moolenaar4c683752020-04-05 21:38:23 +0200676 if (type1 == VAR_UNKNOWN)
677 type1 = VAR_ANY;
678 if (type2 == VAR_UNKNOWN)
679 type2 = VAR_ANY;
680
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681 if (type1 == type2)
682 {
683 switch (type1)
684 {
685 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
686 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
687 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
688 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
689 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
690 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
691 case VAR_LIST: isntype = ISN_COMPARELIST; break;
692 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
693 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694 default: isntype = ISN_COMPAREANY; break;
695 }
696 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200697 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100698 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
699 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
700 isntype = ISN_COMPAREANY;
701
Bram Moolenaar657137c2021-01-09 15:45:23 +0100702 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100703 && (isntype == ISN_COMPAREBOOL
704 || isntype == ISN_COMPARESPECIAL
705 || isntype == ISN_COMPARENR
706 || isntype == ISN_COMPAREFLOAT))
707 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200708 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100709 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200710 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711 }
712 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100713 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100714 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
715 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100716 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
717 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100718 && (type1 == VAR_BLOB || type2 == VAR_BLOB
719 || type1 == VAR_LIST || type2 == VAR_LIST))))
720 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200721 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200723 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200725 return isntype;
726}
727
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200728 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100729check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200730{
731 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
732 return FAIL;
733 return OK;
734}
735
Bram Moolenaara5565e42020-05-09 15:44:01 +0200736/*
737 * Generate an ISN_COMPARE* instruction with a boolean result.
738 */
739 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100740generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200741{
742 isntype_T isntype;
743 isn_T *isn;
744 garray_T *stack = &cctx->ctx_type_stack;
745 vartype_T type1;
746 vartype_T type2;
747
748 RETURN_OK_IF_SKIP(cctx);
749
750 // Get the known type of the two items on the stack. If they are matching
751 // use a type-specific instruction. Otherwise fall back to runtime type
752 // checking.
753 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
754 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100755 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200756 if (isntype == ISN_DROP)
757 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758
759 if ((isn = generate_instr(cctx, isntype)) == NULL)
760 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100761 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762 isn->isn_arg.op.op_ic = ic;
763
764 // takes two arguments, puts one bool back
765 if (stack->ga_len >= 2)
766 {
767 --stack->ga_len;
768 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
769 }
770
771 return OK;
772}
773
774/*
775 * Generate an ISN_2BOOL instruction.
776 */
777 static int
778generate_2BOOL(cctx_T *cctx, int invert)
779{
780 isn_T *isn;
781 garray_T *stack = &cctx->ctx_type_stack;
782
Bram Moolenaar080457c2020-03-03 21:53:32 +0100783 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
785 return FAIL;
786 isn->isn_arg.number = invert;
787
788 // type becomes bool
789 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
790
791 return OK;
792}
793
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200794/*
795 * Generate an ISN_COND2BOOL instruction.
796 */
797 static int
798generate_COND2BOOL(cctx_T *cctx)
799{
800 isn_T *isn;
801 garray_T *stack = &cctx->ctx_type_stack;
802
803 RETURN_OK_IF_SKIP(cctx);
804 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
805 return FAIL;
806
807 // type becomes bool
808 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
809
810 return OK;
811}
812
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200814generate_TYPECHECK(
815 cctx_T *cctx,
816 type_T *expected,
817 int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100818{
819 isn_T *isn;
820 garray_T *stack = &cctx->ctx_type_stack;
821
Bram Moolenaar080457c2020-03-03 21:53:32 +0100822 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
824 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200825 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826 isn->isn_arg.type.ct_off = offset;
827
Bram Moolenaar5e654232020-09-16 15:22:00 +0200828 // type becomes expected
829 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100830
831 return OK;
832}
833
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100834 static int
835generate_SETTYPE(
836 cctx_T *cctx,
837 type_T *expected)
838{
839 isn_T *isn;
840
841 RETURN_OK_IF_SKIP(cctx);
842 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
843 return FAIL;
844 isn->isn_arg.type.ct_type = alloc_type(expected);
845 return OK;
846}
847
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100848/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200849 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
850 * used. Return FALSE if the types will never match.
851 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100852 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200853use_typecheck(type_T *actual, type_T *expected)
854{
855 if (actual->tt_type == VAR_ANY
856 || actual->tt_type == VAR_UNKNOWN
857 || (actual->tt_type == VAR_FUNC
858 && (expected->tt_type == VAR_FUNC
859 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100860 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
861 && ((actual->tt_member == &t_void)
862 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200863 return TRUE;
864 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
865 && actual->tt_type == expected->tt_type)
866 // This takes care of a nested list or dict.
867 return use_typecheck(actual->tt_member, expected->tt_member);
868 return FALSE;
869}
870
871/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200872 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200873 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200874 * - "actual" is a type that can be "expected" type: add a runtime check; or
875 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200876 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
877 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200878 */
879 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200880need_type(
881 type_T *actual,
882 type_T *expected,
883 int offset,
884 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200885 int silent,
886 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200887{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200888 if (expected == &t_bool && actual != &t_bool
889 && (actual->tt_flags & TTFLAG_BOOL_OK))
890 {
891 // Using "0", "1" or the result of an expression with "&&" or "||" as a
892 // boolean is OK but requires a conversion.
893 generate_2BOOL(cctx, FALSE);
894 return OK;
895 }
896
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200897 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200898 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200899
900 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200901 // If it's a constant a runtime check makes no sense.
902 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200903 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200904 generate_TYPECHECK(cctx, expected, offset);
905 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200906 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200907
908 if (!silent)
909 type_mismatch(expected, actual);
910 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200911}
912
913/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100914 * Check that the top of the type stack has a type that can be used as a
915 * condition. Give an error and return FAIL if not.
916 */
917 static int
918bool_on_stack(cctx_T *cctx)
919{
920 garray_T *stack = &cctx->ctx_type_stack;
921 type_T *type;
922
923 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
924 if (type == &t_bool)
925 return OK;
926
927 if (type == &t_any || type == &t_number)
928 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
929 // This requires a runtime type check.
930 return generate_COND2BOOL(cctx);
931
932 return need_type(type, &t_bool, -1, cctx, FALSE, FALSE);
933}
934
935/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936 * Generate an ISN_PUSHNR instruction.
937 */
938 static int
939generate_PUSHNR(cctx_T *cctx, varnumber_T number)
940{
941 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200942 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100943
Bram Moolenaar080457c2020-03-03 21:53:32 +0100944 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100945 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
946 return FAIL;
947 isn->isn_arg.number = number;
948
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200949 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200950 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +0100951 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100952 return OK;
953}
954
955/*
956 * Generate an ISN_PUSHBOOL instruction.
957 */
958 static int
959generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
960{
961 isn_T *isn;
962
Bram Moolenaar080457c2020-03-03 21:53:32 +0100963 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100964 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
965 return FAIL;
966 isn->isn_arg.number = number;
967
968 return OK;
969}
970
971/*
972 * Generate an ISN_PUSHSPEC instruction.
973 */
974 static int
975generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
976{
977 isn_T *isn;
978
Bram Moolenaar080457c2020-03-03 21:53:32 +0100979 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100980 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
981 return FAIL;
982 isn->isn_arg.number = number;
983
984 return OK;
985}
986
987#ifdef FEAT_FLOAT
988/*
989 * Generate an ISN_PUSHF instruction.
990 */
991 static int
992generate_PUSHF(cctx_T *cctx, float_T fnumber)
993{
994 isn_T *isn;
995
Bram Moolenaar080457c2020-03-03 21:53:32 +0100996 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100997 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
998 return FAIL;
999 isn->isn_arg.fnumber = fnumber;
1000
1001 return OK;
1002}
1003#endif
1004
1005/*
1006 * Generate an ISN_PUSHS instruction.
1007 * Consumes "str".
1008 */
1009 static int
1010generate_PUSHS(cctx_T *cctx, char_u *str)
1011{
1012 isn_T *isn;
1013
Bram Moolenaar508b5612021-01-02 18:17:26 +01001014 if (cctx->ctx_skip == SKIP_YES)
1015 {
1016 vim_free(str);
1017 return OK;
1018 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001019 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1020 return FAIL;
1021 isn->isn_arg.string = str;
1022
1023 return OK;
1024}
1025
1026/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001027 * Generate an ISN_PUSHCHANNEL instruction.
1028 * Consumes "channel".
1029 */
1030 static int
1031generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1032{
1033 isn_T *isn;
1034
Bram Moolenaar080457c2020-03-03 21:53:32 +01001035 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001036 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1037 return FAIL;
1038 isn->isn_arg.channel = channel;
1039
1040 return OK;
1041}
1042
1043/*
1044 * Generate an ISN_PUSHJOB instruction.
1045 * Consumes "job".
1046 */
1047 static int
1048generate_PUSHJOB(cctx_T *cctx, job_T *job)
1049{
1050 isn_T *isn;
1051
Bram Moolenaar080457c2020-03-03 21:53:32 +01001052 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001053 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001054 return FAIL;
1055 isn->isn_arg.job = job;
1056
1057 return OK;
1058}
1059
1060/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001061 * Generate an ISN_PUSHBLOB instruction.
1062 * Consumes "blob".
1063 */
1064 static int
1065generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1066{
1067 isn_T *isn;
1068
Bram Moolenaar080457c2020-03-03 21:53:32 +01001069 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001070 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1071 return FAIL;
1072 isn->isn_arg.blob = blob;
1073
1074 return OK;
1075}
1076
1077/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001078 * Generate an ISN_PUSHFUNC instruction with name "name".
1079 * Consumes "name".
1080 */
1081 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001082generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001083{
1084 isn_T *isn;
1085
Bram Moolenaar080457c2020-03-03 21:53:32 +01001086 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001087 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001088 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001089 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001090
1091 return OK;
1092}
1093
1094/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001095 * Generate an ISN_GETITEM instruction with "index".
1096 */
1097 static int
1098generate_GETITEM(cctx_T *cctx, int index)
1099{
1100 isn_T *isn;
1101 garray_T *stack = &cctx->ctx_type_stack;
1102 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1103 type_T *item_type = &t_any;
1104
1105 RETURN_OK_IF_SKIP(cctx);
1106
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001107 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001108 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001109 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001110 emsg(_(e_listreq));
1111 return FAIL;
1112 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001113 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001114 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1115 return FAIL;
1116 isn->isn_arg.number = index;
1117
1118 // add the item type to the type stack
1119 if (ga_grow(stack, 1) == FAIL)
1120 return FAIL;
1121 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1122 ++stack->ga_len;
1123 return OK;
1124}
1125
1126/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001127 * Generate an ISN_SLICE instruction with "count".
1128 */
1129 static int
1130generate_SLICE(cctx_T *cctx, int count)
1131{
1132 isn_T *isn;
1133
1134 RETURN_OK_IF_SKIP(cctx);
1135 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1136 return FAIL;
1137 isn->isn_arg.number = count;
1138 return OK;
1139}
1140
1141/*
1142 * Generate an ISN_CHECKLEN instruction with "min_len".
1143 */
1144 static int
1145generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1146{
1147 isn_T *isn;
1148
1149 RETURN_OK_IF_SKIP(cctx);
1150
1151 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1152 return FAIL;
1153 isn->isn_arg.checklen.cl_min_len = min_len;
1154 isn->isn_arg.checklen.cl_more_OK = more_OK;
1155
1156 return OK;
1157}
1158
1159/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001160 * Generate an ISN_STORE instruction.
1161 */
1162 static int
1163generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1164{
1165 isn_T *isn;
1166
Bram Moolenaar080457c2020-03-03 21:53:32 +01001167 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001168 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1169 return FAIL;
1170 if (name != NULL)
1171 isn->isn_arg.string = vim_strsave(name);
1172 else
1173 isn->isn_arg.number = idx;
1174
1175 return OK;
1176}
1177
1178/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001179 * Generate an ISN_STOREOUTER instruction.
1180 */
1181 static int
1182generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1183{
1184 isn_T *isn;
1185
1186 RETURN_OK_IF_SKIP(cctx);
1187 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1188 return FAIL;
1189 isn->isn_arg.outer.outer_idx = idx;
1190 isn->isn_arg.outer.outer_depth = level;
1191
1192 return OK;
1193}
1194
1195/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001196 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1197 */
1198 static int
1199generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1200{
1201 isn_T *isn;
1202
Bram Moolenaar080457c2020-03-03 21:53:32 +01001203 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1205 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001206 isn->isn_arg.storenr.stnr_idx = idx;
1207 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208
1209 return OK;
1210}
1211
1212/*
1213 * Generate an ISN_STOREOPT instruction
1214 */
1215 static int
1216generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1217{
1218 isn_T *isn;
1219
Bram Moolenaar080457c2020-03-03 21:53:32 +01001220 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001221 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001222 return FAIL;
1223 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1224 isn->isn_arg.storeopt.so_flags = opt_flags;
1225
1226 return OK;
1227}
1228
1229/*
1230 * Generate an ISN_LOAD or similar instruction.
1231 */
1232 static int
1233generate_LOAD(
1234 cctx_T *cctx,
1235 isntype_T isn_type,
1236 int idx,
1237 char_u *name,
1238 type_T *type)
1239{
1240 isn_T *isn;
1241
Bram Moolenaar080457c2020-03-03 21:53:32 +01001242 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001243 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1244 return FAIL;
1245 if (name != NULL)
1246 isn->isn_arg.string = vim_strsave(name);
1247 else
1248 isn->isn_arg.number = idx;
1249
1250 return OK;
1251}
1252
1253/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001254 * Generate an ISN_LOADOUTER instruction
1255 */
1256 static int
1257generate_LOADOUTER(
1258 cctx_T *cctx,
1259 int idx,
1260 int nesting,
1261 type_T *type)
1262{
1263 isn_T *isn;
1264
1265 RETURN_OK_IF_SKIP(cctx);
1266 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1267 return FAIL;
1268 isn->isn_arg.outer.outer_idx = idx;
1269 isn->isn_arg.outer.outer_depth = nesting;
1270
1271 return OK;
1272}
1273
1274/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001275 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001276 */
1277 static int
1278generate_LOADV(
1279 cctx_T *cctx,
1280 char_u *name,
1281 int error)
1282{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001283 int di_flags;
1284 int vidx = find_vim_var(name, &di_flags);
1285 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001286
Bram Moolenaar080457c2020-03-03 21:53:32 +01001287 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001288 if (vidx < 0)
1289 {
1290 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001291 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001292 return FAIL;
1293 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001294 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001295
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001296 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001297}
1298
1299/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001300 * Generate an ISN_UNLET instruction.
1301 */
1302 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001303generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001304{
1305 isn_T *isn;
1306
1307 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001308 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001309 return FAIL;
1310 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1311 isn->isn_arg.unlet.ul_forceit = forceit;
1312
1313 return OK;
1314}
1315
1316/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001317 * Generate an ISN_LOCKCONST instruction.
1318 */
1319 static int
1320generate_LOCKCONST(cctx_T *cctx)
1321{
1322 isn_T *isn;
1323
1324 RETURN_OK_IF_SKIP(cctx);
1325 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1326 return FAIL;
1327 return OK;
1328}
1329
1330/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331 * Generate an ISN_LOADS instruction.
1332 */
1333 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001334generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001335 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001336 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001338 int sid,
1339 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340{
1341 isn_T *isn;
1342
Bram Moolenaar080457c2020-03-03 21:53:32 +01001343 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001344 if (isn_type == ISN_LOADS)
1345 isn = generate_instr_type(cctx, isn_type, type);
1346 else
1347 isn = generate_instr_drop(cctx, isn_type, 1);
1348 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001350 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1351 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352
1353 return OK;
1354}
1355
1356/*
1357 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1358 */
1359 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001360generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 cctx_T *cctx,
1362 isntype_T isn_type,
1363 int sid,
1364 int idx,
1365 type_T *type)
1366{
1367 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001368 scriptref_T *sref;
1369 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001370
Bram Moolenaar080457c2020-03-03 21:53:32 +01001371 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001372 if (isn_type == ISN_LOADSCRIPT)
1373 isn = generate_instr_type(cctx, isn_type, type);
1374 else
1375 isn = generate_instr_drop(cctx, isn_type, 1);
1376 if (isn == NULL)
1377 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001378
1379 // This requires three arguments, which doesn't fit in an instruction, thus
1380 // we need to allocate a struct for this.
1381 sref = ALLOC_ONE(scriptref_T);
1382 if (sref == NULL)
1383 return FAIL;
1384 isn->isn_arg.script.scriptref = sref;
1385 sref->sref_sid = sid;
1386 sref->sref_idx = idx;
1387 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001388 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001389 return OK;
1390}
1391
1392/*
1393 * Generate an ISN_NEWLIST instruction.
1394 */
1395 static int
1396generate_NEWLIST(cctx_T *cctx, int count)
1397{
1398 isn_T *isn;
1399 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400 type_T *type;
1401 type_T *member;
1402
Bram Moolenaar080457c2020-03-03 21:53:32 +01001403 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1405 return FAIL;
1406 isn->isn_arg.number = count;
1407
Bram Moolenaar127542b2020-08-09 17:22:04 +02001408 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001409 if (count == 0)
1410 member = &t_void;
1411 else
1412 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001413 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1414 cctx->ctx_type_list);
1415 type = get_list_type(member, cctx->ctx_type_list);
1416
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001417 // drop the value types
1418 stack->ga_len -= count;
1419
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001420 // add the list type to the type stack
1421 if (ga_grow(stack, 1) == FAIL)
1422 return FAIL;
1423 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1424 ++stack->ga_len;
1425
1426 return OK;
1427}
1428
1429/*
1430 * Generate an ISN_NEWDICT instruction.
1431 */
1432 static int
1433generate_NEWDICT(cctx_T *cctx, int count)
1434{
1435 isn_T *isn;
1436 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001437 type_T *type;
1438 type_T *member;
1439
Bram Moolenaar080457c2020-03-03 21:53:32 +01001440 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1442 return FAIL;
1443 isn->isn_arg.number = count;
1444
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001445 if (count == 0)
1446 member = &t_void;
1447 else
1448 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001449 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1450 cctx->ctx_type_list);
1451 type = get_dict_type(member, cctx->ctx_type_list);
1452
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001453 // drop the key and value types
1454 stack->ga_len -= 2 * count;
1455
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001456 // add the dict type to the type stack
1457 if (ga_grow(stack, 1) == FAIL)
1458 return FAIL;
1459 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1460 ++stack->ga_len;
1461
1462 return OK;
1463}
1464
1465/*
1466 * Generate an ISN_FUNCREF instruction.
1467 */
1468 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001469generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470{
1471 isn_T *isn;
1472 garray_T *stack = &cctx->ctx_type_stack;
1473
Bram Moolenaar080457c2020-03-03 21:53:32 +01001474 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001475 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1476 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001477 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001478 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479
Bram Moolenaarab360522021-01-10 14:02:28 +01001480 // if the referenced function is a closure, it may use items further up in
1481 // the nested context, including this one.
1482 if (ufunc->uf_flags & FC_CLOSURE)
1483 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1484
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485 if (ga_grow(stack, 1) == FAIL)
1486 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001487 ((type_T **)stack->ga_data)[stack->ga_len] =
1488 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001489 ++stack->ga_len;
1490
1491 return OK;
1492}
1493
1494/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001495 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001496 * "lambda_name" and "func_name" must be in allocated memory and will be
1497 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001498 */
1499 static int
1500generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1501{
1502 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001503
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001504 if (cctx->ctx_skip == SKIP_YES)
1505 {
1506 vim_free(lambda_name);
1507 vim_free(func_name);
1508 return OK;
1509 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001510 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001511 {
1512 vim_free(lambda_name);
1513 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001514 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001515 }
1516 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001517 isn->isn_arg.newfunc.nf_global = func_name;
1518
1519 return OK;
1520}
1521
1522/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001523 * Generate an ISN_DEF instruction: list functions
1524 */
1525 static int
1526generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1527{
1528 isn_T *isn;
1529
1530 RETURN_OK_IF_SKIP(cctx);
1531 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1532 return FAIL;
1533 if (len > 0)
1534 {
1535 isn->isn_arg.string = vim_strnsave(name, len);
1536 if (isn->isn_arg.string == NULL)
1537 return FAIL;
1538 }
1539 return OK;
1540}
1541
1542/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001543 * Generate an ISN_JUMP instruction.
1544 */
1545 static int
1546generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1547{
1548 isn_T *isn;
1549 garray_T *stack = &cctx->ctx_type_stack;
1550
Bram Moolenaar080457c2020-03-03 21:53:32 +01001551 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1553 return FAIL;
1554 isn->isn_arg.jump.jump_when = when;
1555 isn->isn_arg.jump.jump_where = where;
1556
1557 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1558 --stack->ga_len;
1559
1560 return OK;
1561}
1562
1563 static int
1564generate_FOR(cctx_T *cctx, int loop_idx)
1565{
1566 isn_T *isn;
1567 garray_T *stack = &cctx->ctx_type_stack;
1568
Bram Moolenaar080457c2020-03-03 21:53:32 +01001569 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001570 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1571 return FAIL;
1572 isn->isn_arg.forloop.for_idx = loop_idx;
1573
1574 if (ga_grow(stack, 1) == FAIL)
1575 return FAIL;
1576 // type doesn't matter, will be stored next
1577 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1578 ++stack->ga_len;
1579
1580 return OK;
1581}
1582
1583/*
1584 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001585 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001586 * Return FAIL if the number of arguments is wrong.
1587 */
1588 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001589generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590{
1591 isn_T *isn;
1592 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001593 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001594 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001595 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596
Bram Moolenaar080457c2020-03-03 21:53:32 +01001597 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001598 argoff = check_internal_func(func_idx, argcount);
1599 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 return FAIL;
1601
Bram Moolenaar389df252020-07-09 21:20:47 +02001602 if (method_call && argoff > 1)
1603 {
1604 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1605 return FAIL;
1606 isn->isn_arg.shuffle.shfl_item = argcount;
1607 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1608 }
1609
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001610 if (argcount > 0)
1611 {
1612 // Check the types of the arguments.
1613 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
1614 if (internal_func_check_arg_types(argtypes, func_idx, argcount) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001615 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001616 if (internal_func_is_map(func_idx))
1617 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001618 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001619
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001620 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1621 return FAIL;
1622 isn->isn_arg.bfunc.cbf_idx = func_idx;
1623 isn->isn_arg.bfunc.cbf_argcount = argcount;
1624
Bram Moolenaar94738d82020-10-21 14:25:07 +02001625 // Drop the argument types and push the return type.
1626 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001627 if (ga_grow(stack, 1) == FAIL)
1628 return FAIL;
1629 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001630 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001631 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001632
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001633 if (maptype != NULL && maptype->tt_member != NULL
1634 && maptype->tt_member != &t_any)
1635 // Check that map() didn't change the item types.
1636 generate_TYPECHECK(cctx, maptype, -1);
1637
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001638 return OK;
1639}
1640
1641/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001642 * Generate an ISN_LISTAPPEND instruction. Works like add().
1643 * Argument count is already checked.
1644 */
1645 static int
1646generate_LISTAPPEND(cctx_T *cctx)
1647{
1648 garray_T *stack = &cctx->ctx_type_stack;
1649 type_T *list_type;
1650 type_T *item_type;
1651 type_T *expected;
1652
1653 // Caller already checked that list_type is a list.
1654 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1655 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1656 expected = list_type->tt_member;
1657 if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL)
1658 return FAIL;
1659
1660 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1661 return FAIL;
1662
1663 --stack->ga_len; // drop the argument
1664 return OK;
1665}
1666
1667/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001668 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1669 * Argument count is already checked.
1670 */
1671 static int
1672generate_BLOBAPPEND(cctx_T *cctx)
1673{
1674 garray_T *stack = &cctx->ctx_type_stack;
1675 type_T *item_type;
1676
1677 // Caller already checked that blob_type is a blob.
1678 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1679 if (need_type(item_type, &t_number, -1, cctx, FALSE, FALSE) == FAIL)
1680 return FAIL;
1681
1682 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1683 return FAIL;
1684
1685 --stack->ga_len; // drop the argument
1686 return OK;
1687}
1688
1689/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001690 * Generate an ISN_DCALL or ISN_UCALL instruction.
1691 * Return FAIL if the number of arguments is wrong.
1692 */
1693 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001694generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695{
1696 isn_T *isn;
1697 garray_T *stack = &cctx->ctx_type_stack;
1698 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001699 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700
Bram Moolenaar080457c2020-03-03 21:53:32 +01001701 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702 if (argcount > regular_args && !has_varargs(ufunc))
1703 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001704 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001705 return FAIL;
1706 }
1707 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1708 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001709 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001710 return FAIL;
1711 }
1712
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001713 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001714 {
1715 int i;
1716
1717 for (i = 0; i < argcount; ++i)
1718 {
1719 type_T *expected;
1720 type_T *actual;
1721
1722 if (i < regular_args)
1723 {
1724 if (ufunc->uf_arg_types == NULL)
1725 continue;
1726 expected = ufunc->uf_arg_types[i];
1727 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001728 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1729 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001730 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001731 else
1732 expected = ufunc->uf_va_type->tt_member;
1733 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001734 if (need_type(actual, expected, -argcount + i, cctx,
1735 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001736 {
1737 arg_type_mismatch(expected, actual, i + 1);
1738 return FAIL;
1739 }
1740 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001741 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001742 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1743 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001744 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001745 }
1746
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001747 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001748 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001749 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001751 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 {
1753 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1754 isn->isn_arg.dfunc.cdf_argcount = argcount;
1755 }
1756 else
1757 {
1758 // A user function may be deleted and redefined later, can't use the
1759 // ufunc pointer, need to look it up again at runtime.
1760 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1761 isn->isn_arg.ufunc.cuf_argcount = argcount;
1762 }
1763
1764 stack->ga_len -= argcount; // drop the arguments
1765 if (ga_grow(stack, 1) == FAIL)
1766 return FAIL;
1767 // add return value
1768 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1769 ++stack->ga_len;
1770
1771 return OK;
1772}
1773
1774/*
1775 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1776 */
1777 static int
1778generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1779{
1780 isn_T *isn;
1781 garray_T *stack = &cctx->ctx_type_stack;
1782
Bram Moolenaar080457c2020-03-03 21:53:32 +01001783 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001784 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1785 return FAIL;
1786 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1787 isn->isn_arg.ufunc.cuf_argcount = argcount;
1788
1789 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001790 if (ga_grow(stack, 1) == FAIL)
1791 return FAIL;
1792 // add return value
1793 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1794 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795
1796 return OK;
1797}
1798
1799/*
1800 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001801 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 */
1803 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001804generate_PCALL(
1805 cctx_T *cctx,
1806 int argcount,
1807 char_u *name,
1808 type_T *type,
1809 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810{
1811 isn_T *isn;
1812 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001813 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001814
Bram Moolenaar080457c2020-03-03 21:53:32 +01001815 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001816
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001817 if (type->tt_type == VAR_ANY)
1818 ret_type = &t_any;
1819 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001820 {
1821 if (type->tt_argcount != -1)
1822 {
1823 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1824
1825 if (argcount < type->tt_min_argcount - varargs)
1826 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001827 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001828 return FAIL;
1829 }
1830 if (!varargs && argcount > type->tt_argcount)
1831 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001832 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001833 return FAIL;
1834 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001835 if (type->tt_args != NULL)
1836 {
1837 int i;
1838
1839 for (i = 0; i < argcount; ++i)
1840 {
1841 int offset = -argcount + i - 1;
1842 type_T *actual = ((type_T **)stack->ga_data)[
1843 stack->ga_len + offset];
1844 type_T *expected;
1845
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001846 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001847 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001848 type->tt_argcount - 1]->tt_member;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001849 else
1850 expected = type->tt_args[i];
1851 if (need_type(actual, expected, offset,
1852 cctx, TRUE, FALSE) == FAIL)
1853 {
1854 arg_type_mismatch(expected, actual, i + 1);
1855 return FAIL;
1856 }
1857 }
1858 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001859 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001860 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001861 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001862 else
1863 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001864 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001865 return FAIL;
1866 }
1867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001868 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1869 return FAIL;
1870 isn->isn_arg.pfunc.cpf_top = at_top;
1871 isn->isn_arg.pfunc.cpf_argcount = argcount;
1872
1873 stack->ga_len -= argcount; // drop the arguments
1874
1875 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001876 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001877
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001878 // If partial is above the arguments it must be cleared and replaced with
1879 // the return value.
1880 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1881 return FAIL;
1882
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001883 return OK;
1884}
1885
1886/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001887 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001888 */
1889 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001890generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001891{
1892 isn_T *isn;
1893 garray_T *stack = &cctx->ctx_type_stack;
1894 type_T *type;
1895
Bram Moolenaar080457c2020-03-03 21:53:32 +01001896 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001897 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001898 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001899 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001900
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001901 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001903 if (type->tt_type != VAR_DICT && type != &t_any)
1904 {
1905 emsg(_(e_dictreq));
1906 return FAIL;
1907 }
1908 // change dict type to dict member type
1909 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01001910 {
1911 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
1912 type->tt_member == &t_unknown ? &t_any : type->tt_member;
1913 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001914
1915 return OK;
1916}
1917
1918/*
1919 * Generate an ISN_ECHO instruction.
1920 */
1921 static int
1922generate_ECHO(cctx_T *cctx, int with_white, int count)
1923{
1924 isn_T *isn;
1925
Bram Moolenaar080457c2020-03-03 21:53:32 +01001926 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001927 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1928 return FAIL;
1929 isn->isn_arg.echo.echo_with_white = with_white;
1930 isn->isn_arg.echo.echo_count = count;
1931
1932 return OK;
1933}
1934
Bram Moolenaarad39c092020-02-26 18:23:43 +01001935/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001936 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001937 */
1938 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001939generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001940{
1941 isn_T *isn;
1942
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001943 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001944 return FAIL;
1945 isn->isn_arg.number = count;
1946
1947 return OK;
1948}
1949
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001950/*
1951 * Generate an ISN_PUT instruction.
1952 */
1953 static int
1954generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1955{
1956 isn_T *isn;
1957
1958 RETURN_OK_IF_SKIP(cctx);
1959 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1960 return FAIL;
1961 isn->isn_arg.put.put_regname = regname;
1962 isn->isn_arg.put.put_lnum = lnum;
1963 return OK;
1964}
1965
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001966 static int
1967generate_EXEC(cctx_T *cctx, char_u *line)
1968{
1969 isn_T *isn;
1970
Bram Moolenaar080457c2020-03-03 21:53:32 +01001971 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001972 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1973 return FAIL;
1974 isn->isn_arg.string = vim_strsave(line);
1975 return OK;
1976}
1977
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001978 static int
1979generate_EXECCONCAT(cctx_T *cctx, int count)
1980{
1981 isn_T *isn;
1982
1983 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1984 return FAIL;
1985 isn->isn_arg.number = count;
1986 return OK;
1987}
1988
Bram Moolenaar08597872020-12-10 19:43:40 +01001989/*
1990 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1991 */
1992 static int
1993generate_RANGE(cctx_T *cctx, char_u *range)
1994{
1995 isn_T *isn;
1996 garray_T *stack = &cctx->ctx_type_stack;
1997
1998 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1999 return FAIL;
2000 isn->isn_arg.string = range;
2001
2002 if (ga_grow(stack, 1) == FAIL)
2003 return FAIL;
2004 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2005 ++stack->ga_len;
2006 return OK;
2007}
2008
Bram Moolenaar792f7862020-11-23 08:31:18 +01002009 static int
2010generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2011{
2012 isn_T *isn;
2013
2014 RETURN_OK_IF_SKIP(cctx);
2015 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2016 return FAIL;
2017 isn->isn_arg.unpack.unp_count = var_count;
2018 isn->isn_arg.unpack.unp_semicolon = semicolon;
2019 return OK;
2020}
2021
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002023 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002024 */
2025 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002026generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002027{
2028 isn_T *isn;
2029
Bram Moolenaar02194d22020-10-24 23:08:38 +02002030 if (cmod->cmod_flags != 0
2031 || cmod->cmod_split != 0
2032 || cmod->cmod_verbose != 0
2033 || cmod->cmod_tab != 0
2034 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002035 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002036 cctx->ctx_has_cmdmod = TRUE;
2037
2038 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002039 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002040 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2041 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2042 return FAIL;
2043 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002044 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002045 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002046 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002047
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002048 return OK;
2049}
2050
2051 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002052generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002053{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002054 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2055 return FAIL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002056 return OK;
2057}
2058
2059/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002060 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002061 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002062 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002063 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002064reserve_local(
2065 cctx_T *cctx,
2066 char_u *name,
2067 size_t len,
2068 int isConst,
2069 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002071 lvar_T *lvar;
2072
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002073 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002074 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002075 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002076 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002077 }
2078
2079 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002080 return NULL;
2081 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002082 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002083
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002084 // Every local variable uses the next entry on the stack. We could re-use
2085 // the last ones when leaving a scope, but then variables used in a closure
2086 // might get overwritten. To keep things simple do not re-use stack
2087 // entries. This is less efficient, but memory is cheap these days.
2088 lvar->lv_idx = cctx->ctx_locals_count++;
2089
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002090 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002091 lvar->lv_const = isConst;
2092 lvar->lv_type = type;
2093
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002094 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002095}
2096
2097/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002098 * Remove local variables above "new_top".
2099 */
2100 static void
2101unwind_locals(cctx_T *cctx, int new_top)
2102{
2103 if (cctx->ctx_locals.ga_len > new_top)
2104 {
2105 int idx;
2106 lvar_T *lvar;
2107
2108 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2109 {
2110 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2111 vim_free(lvar->lv_name);
2112 }
2113 }
2114 cctx->ctx_locals.ga_len = new_top;
2115}
2116
2117/*
2118 * Free all local variables.
2119 */
2120 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002121free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002122{
2123 unwind_locals(cctx, 0);
2124 ga_clear(&cctx->ctx_locals);
2125}
2126
2127/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002128 * Find "name" in script-local items of script "sid".
2129 * Returns the index in "sn_var_vals" if found.
2130 * If found but not in "sn_var_vals" returns -1.
2131 * If not found returns -2.
2132 */
2133 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002134get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135{
2136 hashtab_T *ht;
2137 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002138 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002139 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 int idx;
2141
Bram Moolenaare3d46852020-08-29 13:39:17 +02002142 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002143 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002144 if (sid == current_sctx.sc_sid)
2145 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002146 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002147
2148 if (sav == NULL)
2149 return -2;
2150 idx = sav->sav_var_vals_idx;
2151 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2152 if (check_writable && sv->sv_const)
2153 semsg(_(e_readonlyvar), name);
2154 return idx;
2155 }
2156
2157 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002158 ht = &SCRIPT_VARS(sid);
2159 di = find_var_in_ht(ht, 0, name, TRUE);
2160 if (di == NULL)
2161 return -2;
2162
2163 // Now find the svar_T index in sn_var_vals.
2164 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2165 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002166 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002167 if (sv->sv_tv == &di->di_tv)
2168 {
2169 if (check_writable && sv->sv_const)
2170 semsg(_(e_readonlyvar), name);
2171 return idx;
2172 }
2173 }
2174 return -1;
2175}
2176
2177/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002178 * Find "name" in imported items of the current script or in "cctx" if not
2179 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002180 */
2181 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002182find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002183{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002184 int idx;
2185
Bram Moolenaare3d46852020-08-29 13:39:17 +02002186 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002187 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002188 if (cctx != NULL)
2189 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2190 {
2191 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2192 + idx;
2193
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002194 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2195 : STRLEN(import->imp_name) == len
2196 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002197 return import;
2198 }
2199
Bram Moolenaarefa94442020-08-08 22:16:00 +02002200 return find_imported_in_script(name, len, current_sctx.sc_sid);
2201}
2202
2203 imported_T *
2204find_imported_in_script(char_u *name, size_t len, int sid)
2205{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002206 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002207 int idx;
2208
Bram Moolenaare3d46852020-08-29 13:39:17 +02002209 if (!SCRIPT_ID_VALID(sid))
2210 return NULL;
2211 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2213 {
2214 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2215
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002216 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2217 : STRLEN(import->imp_name) == len
2218 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002219 return import;
2220 }
2221 return NULL;
2222}
2223
2224/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002225 * Free all imported variables.
2226 */
2227 static void
2228free_imported(cctx_T *cctx)
2229{
2230 int idx;
2231
2232 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2233 {
2234 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2235
2236 vim_free(import->imp_name);
2237 }
2238 ga_clear(&cctx->ctx_imports);
2239}
2240
2241/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002242 * Return a pointer to the next line that isn't empty or only contains a
2243 * comment. Skips over white space.
2244 * Returns NULL if there is none.
2245 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002246 char_u *
2247peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002248{
2249 int lnum = cctx->ctx_lnum;
2250
2251 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2252 {
2253 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002254 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002255
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002256 // ignore NULLs inserted for continuation lines
2257 if (line != NULL)
2258 {
2259 p = skipwhite(line);
2260 if (*p != NUL && !vim9_comment_start(p))
2261 return p;
2262 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002263 }
2264 return NULL;
2265}
2266
2267/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002268 * Called when checking for a following operator at "arg". When the rest of
2269 * the line is empty or only a comment, peek the next line. If there is a next
2270 * line return a pointer to it and set "nextp".
2271 * Otherwise skip over white space.
2272 */
2273 static char_u *
2274may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2275{
2276 char_u *p = skipwhite(arg);
2277
2278 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002279 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002280 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002281 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002282 if (*nextp != NULL)
2283 return *nextp;
2284 }
2285 return p;
2286}
2287
2288/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002289 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002290 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002291 * Returns NULL when at the end.
2292 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002293 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002294next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002295{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002296 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002297
2298 do
2299 {
2300 ++cctx->ctx_lnum;
2301 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002302 {
2303 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002304 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002305 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002306 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002307 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002308 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002309 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002310 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002311 return line;
2312}
2313
2314/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002315 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002316 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002317 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002318 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2319 */
2320 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002321may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002322{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002323 *arg = skipwhite(whitep);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002324 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002325 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002326 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002327
2328 if (next == NULL)
2329 return FAIL;
2330 *arg = skipwhite(next);
2331 }
2332 return OK;
2333}
2334
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002335/*
2336 * Idem, and give an error when failed.
2337 */
2338 static int
2339may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2340{
2341 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2342 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002343 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002344 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002345 return FAIL;
2346 }
2347 return OK;
2348}
2349
2350
Bram Moolenaara5565e42020-05-09 15:44:01 +02002351// Structure passed between the compile_expr* functions to keep track of
2352// constants that have been parsed but for which no code was produced yet. If
2353// possible expressions on these constants are applied at compile time. If
2354// that is not possible, the code to push the constants needs to be generated
2355// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002356// Using 50 should be more than enough of 5 levels of ().
2357#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002358typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002359 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002360 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002361 int pp_is_const; // all generated code was constants, used for a
2362 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002363} ppconst_T;
2364
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002365static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002366static int compile_expr0(char_u **arg, cctx_T *cctx);
2367static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2368
Bram Moolenaara5565e42020-05-09 15:44:01 +02002369/*
2370 * Generate a PUSH instruction for "tv".
2371 * "tv" will be consumed or cleared.
2372 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2373 */
2374 static int
2375generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2376{
2377 if (tv != NULL)
2378 {
2379 switch (tv->v_type)
2380 {
2381 case VAR_UNKNOWN:
2382 break;
2383 case VAR_BOOL:
2384 generate_PUSHBOOL(cctx, tv->vval.v_number);
2385 break;
2386 case VAR_SPECIAL:
2387 generate_PUSHSPEC(cctx, tv->vval.v_number);
2388 break;
2389 case VAR_NUMBER:
2390 generate_PUSHNR(cctx, tv->vval.v_number);
2391 break;
2392#ifdef FEAT_FLOAT
2393 case VAR_FLOAT:
2394 generate_PUSHF(cctx, tv->vval.v_float);
2395 break;
2396#endif
2397 case VAR_BLOB:
2398 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2399 tv->vval.v_blob = NULL;
2400 break;
2401 case VAR_STRING:
2402 generate_PUSHS(cctx, tv->vval.v_string);
2403 tv->vval.v_string = NULL;
2404 break;
2405 default:
2406 iemsg("constant type not supported");
2407 clear_tv(tv);
2408 return FAIL;
2409 }
2410 tv->v_type = VAR_UNKNOWN;
2411 }
2412 return OK;
2413}
2414
2415/*
2416 * Generate code for any ppconst entries.
2417 */
2418 static int
2419generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2420{
2421 int i;
2422 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002423 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002424
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002425 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002426 for (i = 0; i < ppconst->pp_used; ++i)
2427 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2428 ret = FAIL;
2429 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002430 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002431 return ret;
2432}
2433
2434/*
2435 * Clear ppconst constants. Used when failing.
2436 */
2437 static void
2438clear_ppconst(ppconst_T *ppconst)
2439{
2440 int i;
2441
2442 for (i = 0; i < ppconst->pp_used; ++i)
2443 clear_tv(&ppconst->pp_tv[i]);
2444 ppconst->pp_used = 0;
2445}
2446
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002447/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002448 * Generate an instruction to load script-local variable "name", without the
2449 * leading "s:".
2450 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002451 */
2452 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002453compile_load_scriptvar(
2454 cctx_T *cctx,
2455 char_u *name, // variable NUL terminated
2456 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002457 char_u **end, // end of variable
2458 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002459{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002460 scriptitem_T *si;
2461 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002462 imported_T *import;
2463
Bram Moolenaare3d46852020-08-29 13:39:17 +02002464 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2465 return FAIL;
2466 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002467 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002468 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002469 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002470 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002471 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2472 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 }
2474 if (idx >= 0)
2475 {
2476 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2477
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002478 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479 current_sctx.sc_sid, idx, sv->sv_type);
2480 return OK;
2481 }
2482
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002483 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484 if (import != NULL)
2485 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002486 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002487 {
2488 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002489 char_u *exp_name;
2490 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002491 ufunc_T *ufunc;
2492 type_T *type;
2493
2494 // Used "import * as Name", need to lookup the member.
2495 if (*p != '.')
2496 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002497 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002498 return FAIL;
2499 }
2500 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002501 if (VIM_ISWHITE(*p))
2502 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002503 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002504 return FAIL;
2505 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002506
Bram Moolenaar1c991142020-07-04 13:15:31 +02002507 // isolate one name
2508 exp_name = p;
2509 while (eval_isnamec(*p))
2510 ++p;
2511 cc = *p;
2512 *p = NUL;
2513
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002514 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002515 *p = cc;
2516 p = skipwhite(p);
2517
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002518 // TODO: what if it is a function?
2519 if (idx < 0)
2520 return FAIL;
2521 *end = p;
2522
2523 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2524 import->imp_sid,
2525 idx,
2526 type);
2527 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002528 else if (import->imp_funcname != NULL)
2529 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002530 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002531 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2532 import->imp_sid,
2533 import->imp_var_vals_idx,
2534 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002535 return OK;
2536 }
2537
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002538 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002539 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 return FAIL;
2541}
2542
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002543 static int
2544generate_funcref(cctx_T *cctx, char_u *name)
2545{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002546 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002547
2548 if (ufunc == NULL)
2549 return FAIL;
2550
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002551 // Need to compile any default values to get the argument types.
2552 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2553 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2554 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002555 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002556}
2557
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558/*
2559 * Compile a variable name into a load instruction.
2560 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002561 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002562 * When "error" is FALSE do not give an error when not found.
2563 */
2564 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002565compile_load(
2566 char_u **arg,
2567 char_u *end_arg,
2568 cctx_T *cctx,
2569 int is_expr,
2570 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571{
2572 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002573 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002574 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002576 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577
2578 if (*(*arg + 1) == ':')
2579 {
2580 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002581 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002582 {
2583 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002585 switch (**arg)
2586 {
2587 case 'g': isn_type = ISN_LOADGDICT; break;
2588 case 'w': isn_type = ISN_LOADWDICT; break;
2589 case 't': isn_type = ISN_LOADTDICT; break;
2590 case 'b': isn_type = ISN_LOADBDICT; break;
2591 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002592 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002593 goto theend;
2594 }
2595 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2596 goto theend;
2597 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002598 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 else
2600 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002601 isntype_T isn_type = ISN_DROP;
2602
2603 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2604 if (name == NULL)
2605 return FAIL;
2606
2607 switch (**arg)
2608 {
2609 case 'v': res = generate_LOADV(cctx, name, error);
2610 break;
2611 case 's': res = compile_load_scriptvar(cctx, name,
2612 NULL, NULL, error);
2613 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002614 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2615 isn_type = ISN_LOADG;
2616 else
2617 {
2618 isn_type = ISN_LOADAUTO;
2619 vim_free(name);
2620 name = vim_strnsave(*arg, end - *arg);
2621 if (name == NULL)
2622 return FAIL;
2623 }
2624 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002625 case 'w': isn_type = ISN_LOADW; break;
2626 case 't': isn_type = ISN_LOADT; break;
2627 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002628 default: // cannot happen, just in case
2629 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002630 goto theend;
2631 }
2632 if (isn_type != ISN_DROP)
2633 {
2634 // Global, Buffer-local, Window-local and Tabpage-local
2635 // variables can be defined later, thus we don't check if it
2636 // exists, give error at runtime.
2637 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2638 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639 }
2640 }
2641 else
2642 {
2643 size_t len = end - *arg;
2644 int idx;
2645 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002646 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002647
2648 name = vim_strnsave(*arg, end - *arg);
2649 if (name == NULL)
2650 return FAIL;
2651
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002652 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002653 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002654 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002655 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002656 }
2657 else
2658 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002659 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002660
Bram Moolenaar709664c2020-12-12 14:33:41 +01002661 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002662 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002663 type = lvar.lv_type;
2664 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01002665 if (lvar.lv_from_outer != 0)
2666 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002667 else
2668 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002669 }
2670 else
2671 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002672 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002673 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002674 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002675 || find_imported(name, 0, cctx) != NULL)
2676 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002677
Bram Moolenaar0f769812020-09-12 18:32:34 +02002678 // When evaluating an expression and the name starts with an
2679 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002680 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002681 if (res == FAIL && is_expr
2682 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002683 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 }
2685 }
2686 if (gen_load)
2687 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01002688 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002689 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002690 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002691 cctx->ctx_outer_used = TRUE;
2692 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002693 }
2694
2695 *arg = end;
2696
2697theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002698 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002699 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002700 vim_free(name);
2701 return res;
2702}
2703
2704/*
2705 * Compile the argument expressions.
2706 * "arg" points to just after the "(" and is advanced to after the ")"
2707 */
2708 static int
2709compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2710{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002711 char_u *p = *arg;
2712 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002713 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002714
Bram Moolenaare6085c52020-04-12 20:19:16 +02002715 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002717 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2718 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002719 if (*p == ')')
2720 {
2721 *arg = p + 1;
2722 return OK;
2723 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002724 if (must_end)
2725 {
2726 semsg(_(e_missing_comma_before_argument_str), p);
2727 return FAIL;
2728 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002729
Bram Moolenaara5565e42020-05-09 15:44:01 +02002730 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002731 return FAIL;
2732 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002733
2734 if (*p != ',' && *skipwhite(p) == ',')
2735 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002736 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002737 p = skipwhite(p);
2738 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002739 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002740 {
2741 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002742 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002743 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002744 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002745 else
2746 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002747 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002748 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002749 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002750failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002751 emsg(_(e_missing_close));
2752 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002753}
2754
2755/*
2756 * Compile a function call: name(arg1, arg2)
2757 * "arg" points to "name", "arg + varlen" to the "(".
2758 * "argcount_init" is 1 for "value->method()"
2759 * Instructions:
2760 * EVAL arg1
2761 * EVAL arg2
2762 * BCALL / DCALL / UCALL
2763 */
2764 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002765compile_call(
2766 char_u **arg,
2767 size_t varlen,
2768 cctx_T *cctx,
2769 ppconst_T *ppconst,
2770 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771{
2772 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002773 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 int argcount = argcount_init;
2775 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002776 char_u fname_buf[FLEN_FIXED + 1];
2777 char_u *tofree = NULL;
2778 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002779 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002780 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002781 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782
Bram Moolenaara5565e42020-05-09 15:44:01 +02002783 // we can evaluate "has('name')" at compile time
2784 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2785 {
2786 char_u *s = skipwhite(*arg + varlen + 1);
2787 typval_T argvars[2];
2788
2789 argvars[0].v_type = VAR_UNKNOWN;
2790 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002791 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002792 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002793 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002794 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002795 if (*s == ')' && argvars[0].v_type == VAR_STRING
2796 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002797 {
2798 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2799
2800 *arg = s + 1;
2801 argvars[1].v_type = VAR_UNKNOWN;
2802 tv->v_type = VAR_NUMBER;
2803 tv->vval.v_number = 0;
2804 f_has(argvars, tv);
2805 clear_tv(&argvars[0]);
2806 ++ppconst->pp_used;
2807 return OK;
2808 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002809 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002810 }
2811
2812 if (generate_ppconst(cctx, ppconst) == FAIL)
2813 return FAIL;
2814
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 if (varlen >= sizeof(namebuf))
2816 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002817 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818 return FAIL;
2819 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002820 vim_strncpy(namebuf, *arg, varlen);
2821 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002822
2823 *arg = skipwhite(*arg + varlen + 1);
2824 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002825 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826
Bram Moolenaar03290b82020-12-19 16:30:44 +01002827 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002828 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 {
2830 int idx;
2831
2832 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002833 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002834 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002835 {
2836 if (STRCMP(name, "add") == 0 && argcount == 2)
2837 {
2838 garray_T *stack = &cctx->ctx_type_stack;
2839 type_T *type = ((type_T **)stack->ga_data)[
2840 stack->ga_len - 2];
2841
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002842 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002843 if (type->tt_type == VAR_LIST)
2844 {
2845 // inline "add(list, item)" so that the type can be checked
2846 res = generate_LISTAPPEND(cctx);
2847 idx = -1;
2848 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002849 else if (type->tt_type == VAR_BLOB)
2850 {
2851 // inline "add(blob, nr)" so that the type can be checked
2852 res = generate_BLOBAPPEND(cctx);
2853 idx = -1;
2854 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002855 }
2856
2857 if (idx >= 0)
2858 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2859 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002860 else
2861 semsg(_(e_unknownfunc), namebuf);
2862 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 }
2864
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002865 // An argument or local variable can be a function reference, this
2866 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002867 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002868 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002869 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002870 // If we can find the function by name generate the right call.
2871 // Skip global functions here, a local funcref takes precedence.
2872 ufunc = find_func(name, FALSE, cctx);
2873 if (ufunc != NULL && !func_is_global(ufunc))
2874 {
2875 res = generate_CALL(cctx, ufunc, argcount);
2876 goto theend;
2877 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002878 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002879
2880 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002881 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002882 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002884 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002885 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002886 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002887 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01002888 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002889
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002890 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002891 goto theend;
2892 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893
Bram Moolenaar0f769812020-09-12 18:32:34 +02002894 // If we can find a global function by name generate the right call.
2895 if (ufunc != NULL)
2896 {
2897 res = generate_CALL(cctx, ufunc, argcount);
2898 goto theend;
2899 }
2900
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002901 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002902 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002903 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002904 res = generate_UCALL(cctx, name, argcount);
2905 else
2906 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002907
2908theend:
2909 vim_free(tofree);
2910 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911}
2912
2913// like NAMESPACE_CHAR but with 'a' and 'l'.
2914#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2915
2916/*
2917 * Find the end of a variable or function name. Unlike find_name_end() this
2918 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002919 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 * Return a pointer to just after the name. Equal to "arg" if there is no
2921 * valid name.
2922 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01002923 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002924to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925{
2926 char_u *p;
2927
2928 // Quick check for valid starting character.
2929 if (!eval_isnamec1(*arg))
2930 return arg;
2931
2932 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2933 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2934 // and can be used in slice "[n:]".
2935 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002936 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2938 break;
2939 return p;
2940}
2941
2942/*
2943 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002944 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 */
2946 char_u *
2947to_name_const_end(char_u *arg)
2948{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002949 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002950 typval_T rettv;
2951
2952 if (p == arg && *arg == '[')
2953 {
2954
2955 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002956 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 p = arg;
2958 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 return p;
2960}
2961
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962/*
2963 * parse a list: [expr, expr]
2964 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002965 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 */
2967 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002968compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969{
2970 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002971 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002973 int is_const;
2974 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002976 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002978 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002979 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002980 semsg(_(e_list_end), *arg);
2981 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002982 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002983 if (*p == ',')
2984 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002985 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002986 return FAIL;
2987 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002988 if (*p == ']')
2989 {
2990 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002991 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002992 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002993 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002994 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002995 if (!is_const)
2996 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 ++count;
2998 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002999 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003001 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3002 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003003 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003004 return FAIL;
3005 }
3006 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003007 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008 p = skipwhite(p);
3009 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003010 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003012 ppconst->pp_is_const = is_all_const;
3013 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014}
3015
3016/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003017 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018 * "*arg" points to the '{'.
Bram Moolenaare462f522020-12-27 14:43:30 +01003019 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 */
3021 static int
3022compile_lambda(char_u **arg, cctx_T *cctx)
3023{
Bram Moolenaare462f522020-12-27 14:43:30 +01003024 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 typval_T rettv;
3026 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003027 evalarg_T evalarg;
3028
3029 CLEAR_FIELD(evalarg);
3030 evalarg.eval_flags = EVAL_EVALUATE;
3031 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032
3033 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003034 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3035 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003036 {
3037 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003038 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003039 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003040
Bram Moolenaar65c44152020-12-24 15:14:01 +01003041 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003043 ++ufunc->uf_refcount;
3044 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045
Bram Moolenaar65c44152020-12-24 15:14:01 +01003046 // Compile the function into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003047 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003049 clear_evalarg(&evalarg, NULL);
3050
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003051 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003052 {
3053 // The return type will now be known.
3054 set_function_type(ufunc);
3055
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003056 // The function reference count will be 1. When the ISN_FUNCREF
3057 // instruction is deleted the reference count is decremented and the
3058 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003059 return generate_FUNCREF(cctx, ufunc);
3060 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003061
3062 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003063 return FAIL;
3064}
3065
3066/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003067 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003069 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 */
3071 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003072compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003073{
3074 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003075 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 int count = 0;
3077 dict_T *d = dict_alloc();
3078 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003079 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003080 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003081 int is_const;
3082 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083
3084 if (d == NULL)
3085 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003086 if (generate_ppconst(cctx, ppconst) == FAIL)
3087 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003088 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003090 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091
Bram Moolenaar23c55272020-06-21 16:58:13 +02003092 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003093 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003094 *arg = NULL;
3095 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003096 }
3097
3098 if (**arg == '}')
3099 break;
3100
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003101 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003103 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003105 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003106 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003107 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3110 if (isn->isn_type == ISN_PUSHS)
3111 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003112 else
3113 {
3114 type_T *keytype = ((type_T **)stack->ga_data)
Bram Moolenaar2bede172020-11-19 18:53:18 +01003115 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003116 if (need_type(keytype, &t_string, -1, cctx,
Bram Moolenaar2bede172020-11-19 18:53:18 +01003117 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003118 return FAIL;
3119 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003120 *arg = skipwhite(*arg);
3121 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003122 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003123 emsg(_(e_missing_matching_bracket_after_dict_key));
3124 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003125 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003126 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003128 else
3129 {
3130 // {"name": value},
3131 // {'name': value},
3132 // {name: value} use "name" as a literal key
3133 key = get_literal_key(arg);
3134 if (key == NULL)
3135 return FAIL;
3136 if (generate_PUSHS(cctx, key) == FAIL)
3137 return FAIL;
3138 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139
3140 // Check for duplicate keys, if using string keys.
3141 if (key != NULL)
3142 {
3143 item = dict_find(d, key, -1);
3144 if (item != NULL)
3145 {
3146 semsg(_(e_duplicate_key), key);
3147 goto failret;
3148 }
3149 item = dictitem_alloc(key);
3150 if (item != NULL)
3151 {
3152 item->di_tv.v_type = VAR_UNKNOWN;
3153 item->di_tv.v_lock = 0;
3154 if (dict_add(d, item) == FAIL)
3155 dictitem_free(item);
3156 }
3157 }
3158
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 if (**arg != ':')
3160 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003161 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003162 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003163 else
3164 semsg(_(e_missing_dict_colon), *arg);
3165 return FAIL;
3166 }
3167 whitep = *arg + 1;
3168 if (!IS_WHITE_OR_NUL(*whitep))
3169 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003170 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 return FAIL;
3172 }
3173
Bram Moolenaar23c55272020-06-21 16:58:13 +02003174 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003175 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003176 *arg = NULL;
3177 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003178 }
3179
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003180 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003182 if (!is_const)
3183 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 ++count;
3185
Bram Moolenaar2c330432020-04-13 14:41:35 +02003186 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003187 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003188 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003189 *arg = NULL;
3190 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003191 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 if (**arg == '}')
3193 break;
3194 if (**arg != ',')
3195 {
3196 semsg(_(e_missing_dict_comma), *arg);
3197 goto failret;
3198 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003199 if (IS_WHITE_OR_NUL(*whitep))
3200 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003201 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003202 return FAIL;
3203 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003204 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003205 if (!IS_WHITE_OR_NUL(*whitep))
3206 {
3207 semsg(_(e_white_space_required_after_str), ",");
3208 return FAIL;
3209 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 *arg = skipwhite(*arg + 1);
3211 }
3212
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 *arg = *arg + 1;
3214
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003215 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003216 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003217 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003218 *arg += STRLEN(*arg);
3219
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003221 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 return generate_NEWDICT(cctx, count);
3223
3224failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003225 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003226 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003227 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003228 *arg = (char_u *)"";
3229 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230 dict_unref(d);
3231 return FAIL;
3232}
3233
3234/*
3235 * Compile "&option".
3236 */
3237 static int
3238compile_get_option(char_u **arg, cctx_T *cctx)
3239{
3240 typval_T rettv;
3241 char_u *start = *arg;
3242 int ret;
3243
3244 // parse the option and get the current value to get the type.
3245 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003246 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 if (ret == OK)
3248 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003249 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003250 char_u *name = vim_strnsave(start, *arg - start);
3251 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3252 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003253
3254 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3255 vim_free(name);
3256 }
3257 clear_tv(&rettv);
3258
3259 return ret;
3260}
3261
3262/*
3263 * Compile "$VAR".
3264 */
3265 static int
3266compile_get_env(char_u **arg, cctx_T *cctx)
3267{
3268 char_u *start = *arg;
3269 int len;
3270 int ret;
3271 char_u *name;
3272
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 ++*arg;
3274 len = get_env_len(arg);
3275 if (len == 0)
3276 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003277 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 return FAIL;
3279 }
3280
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003281 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 name = vim_strnsave(start, len + 1);
3283 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3284 vim_free(name);
3285 return ret;
3286}
3287
3288/*
3289 * Compile "@r".
3290 */
3291 static int
3292compile_get_register(char_u **arg, cctx_T *cctx)
3293{
3294 int ret;
3295
3296 ++*arg;
3297 if (**arg == NUL)
3298 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003299 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 return FAIL;
3301 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003302 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 {
3304 emsg_invreg(**arg);
3305 return FAIL;
3306 }
3307 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3308 ++*arg;
3309 return ret;
3310}
3311
3312/*
3313 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003314 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003315 */
3316 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003317apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003319 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320
3321 // this works from end to start
3322 while (p > start)
3323 {
3324 --p;
3325 if (*p == '-' || *p == '+')
3326 {
3327 // only '-' has an effect, for '+' we only check the type
3328#ifdef FEAT_FLOAT
3329 if (rettv->v_type == VAR_FLOAT)
3330 {
3331 if (*p == '-')
3332 rettv->vval.v_float = -rettv->vval.v_float;
3333 }
3334 else
3335#endif
3336 {
3337 varnumber_T val;
3338 int error = FALSE;
3339
3340 // tv_get_number_chk() accepts a string, but we don't want that
3341 // here
3342 if (check_not_string(rettv) == FAIL)
3343 return FAIL;
3344 val = tv_get_number_chk(rettv, &error);
3345 clear_tv(rettv);
3346 if (error)
3347 return FAIL;
3348 if (*p == '-')
3349 val = -val;
3350 rettv->v_type = VAR_NUMBER;
3351 rettv->vval.v_number = val;
3352 }
3353 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003354 else if (numeric_only)
3355 {
3356 ++p;
3357 break;
3358 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003359 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 {
3361 int v = tv2bool(rettv);
3362
3363 // '!' is permissive in the type.
3364 clear_tv(rettv);
3365 rettv->v_type = VAR_BOOL;
3366 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3367 }
3368 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003369 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370 return OK;
3371}
3372
3373/*
3374 * Recognize v: variables that are constants and set "rettv".
3375 */
3376 static void
3377get_vim_constant(char_u **arg, typval_T *rettv)
3378{
3379 if (STRNCMP(*arg, "v:true", 6) == 0)
3380 {
3381 rettv->v_type = VAR_BOOL;
3382 rettv->vval.v_number = VVAL_TRUE;
3383 *arg += 6;
3384 }
3385 else if (STRNCMP(*arg, "v:false", 7) == 0)
3386 {
3387 rettv->v_type = VAR_BOOL;
3388 rettv->vval.v_number = VVAL_FALSE;
3389 *arg += 7;
3390 }
3391 else if (STRNCMP(*arg, "v:null", 6) == 0)
3392 {
3393 rettv->v_type = VAR_SPECIAL;
3394 rettv->vval.v_number = VVAL_NULL;
3395 *arg += 6;
3396 }
3397 else if (STRNCMP(*arg, "v:none", 6) == 0)
3398 {
3399 rettv->v_type = VAR_SPECIAL;
3400 rettv->vval.v_number = VVAL_NONE;
3401 *arg += 6;
3402 }
3403}
3404
Bram Moolenaar657137c2021-01-09 15:45:23 +01003405 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003406get_compare_type(char_u *p, int *len, int *type_is)
3407{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003408 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003409 int i;
3410
3411 switch (p[0])
3412 {
3413 case '=': if (p[1] == '=')
3414 type = EXPR_EQUAL;
3415 else if (p[1] == '~')
3416 type = EXPR_MATCH;
3417 break;
3418 case '!': if (p[1] == '=')
3419 type = EXPR_NEQUAL;
3420 else if (p[1] == '~')
3421 type = EXPR_NOMATCH;
3422 break;
3423 case '>': if (p[1] != '=')
3424 {
3425 type = EXPR_GREATER;
3426 *len = 1;
3427 }
3428 else
3429 type = EXPR_GEQUAL;
3430 break;
3431 case '<': if (p[1] != '=')
3432 {
3433 type = EXPR_SMALLER;
3434 *len = 1;
3435 }
3436 else
3437 type = EXPR_SEQUAL;
3438 break;
3439 case 'i': if (p[1] == 's')
3440 {
3441 // "is" and "isnot"; but not a prefix of a name
3442 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3443 *len = 5;
3444 i = p[*len];
3445 if (!isalnum(i) && i != '_')
3446 {
3447 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3448 *type_is = TRUE;
3449 }
3450 }
3451 break;
3452 }
3453 return type;
3454}
3455
3456/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003457 * Skip over an expression, ignoring most errors.
3458 */
3459 static void
3460skip_expr_cctx(char_u **arg, cctx_T *cctx)
3461{
3462 evalarg_T evalarg;
3463
3464 CLEAR_FIELD(evalarg);
3465 evalarg.eval_cctx = cctx;
3466 skip_expr(arg, &evalarg);
3467}
3468
3469/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003470 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003471 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003472 */
3473 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003474compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003475{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003476 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003477
3478 // this works from end to start
3479 while (p > start)
3480 {
3481 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003482 while (VIM_ISWHITE(*p))
3483 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484 if (*p == '-' || *p == '+')
3485 {
3486 int negate = *p == '-';
3487 isn_T *isn;
3488
3489 // TODO: check type
3490 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3491 {
3492 --p;
3493 if (*p == '-')
3494 negate = !negate;
3495 }
3496 // only '-' has an effect, for '+' we only check the type
3497 if (negate)
3498 isn = generate_instr(cctx, ISN_NEGATENR);
3499 else
3500 isn = generate_instr(cctx, ISN_CHECKNR);
3501 if (isn == NULL)
3502 return FAIL;
3503 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003504 else if (numeric_only)
3505 {
3506 ++p;
3507 break;
3508 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509 else
3510 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003511 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003513 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003515 if (p[-1] == '!')
3516 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003517 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 }
3519 if (generate_2BOOL(cctx, invert) == FAIL)
3520 return FAIL;
3521 }
3522 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003523 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 return OK;
3525}
3526
3527/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003528 * Compile "(expression)": recursive!
3529 * Return FAIL/OK.
3530 */
3531 static int
3532compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3533{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003534 int ret;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003535
3536 *arg = skipwhite(*arg + 1);
3537 if (ppconst->pp_used <= PPSIZE - 10)
3538 {
3539 ret = compile_expr1(arg, cctx, ppconst);
3540 }
3541 else
3542 {
3543 // Not enough space in ppconst, flush constants.
3544 if (generate_ppconst(cctx, ppconst) == FAIL)
3545 return FAIL;
3546 ret = compile_expr0(arg, cctx);
3547 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003548 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3549 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003550 if (**arg == ')')
3551 ++*arg;
3552 else if (ret == OK)
3553 {
3554 emsg(_(e_missing_close));
3555 ret = FAIL;
3556 }
3557 return ret;
3558}
3559
3560/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003562 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003563 */
3564 static int
3565compile_subscript(
3566 char_u **arg,
3567 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003568 char_u *start_leader,
3569 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003570 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003572 char_u *name_start = *end_leader;
3573
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003574 for (;;)
3575 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003576 char_u *p = skipwhite(*arg);
3577
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003578 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003579 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003580 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003581
3582 // If a following line starts with "->{" or "->X" advance to that
3583 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003584 // Also if a following line starts with ".x".
3585 if (next != NULL &&
3586 ((next[0] == '-' && next[1] == '>'
3587 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003588 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003589 {
3590 next = next_line_from_context(cctx, TRUE);
3591 if (next == NULL)
3592 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003593 *arg = next;
3594 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003595 }
3596 }
3597
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003598 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003599 // not a function call.
3600 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003602 garray_T *stack = &cctx->ctx_type_stack;
3603 type_T *type;
3604 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003606 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003607 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003608 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003609
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003611 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3612
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003613 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3615 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003616 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003617 return FAIL;
3618 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003619 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003621 char_u *pstart = p;
3622
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003623 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003624 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003625 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003626
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 // something->method()
3628 // Apply the '!', '-' and '+' first:
3629 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003630 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003631 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003633 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003634 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003635 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003636 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003637 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003638 int argcount = 1;
3639 char_u *expr;
3640 garray_T *stack;
3641 type_T *type;
3642
3643 // Funcref call: list->(Refs[2])(arg)
3644 // or lambda: list->((arg) => expr)(arg)
3645 // Fist compile the arguments.
3646 expr = *arg;
3647 *arg = skipwhite(*arg + 1);
3648 skip_expr_cctx(arg, cctx);
3649 *arg = skipwhite(*arg);
3650 if (**arg != ')')
3651 {
3652 semsg(_(e_missing_paren), *arg);
3653 return FAIL;
3654 }
3655 ++*arg;
3656 if (**arg != '(')
3657 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003658 if (*skipwhite(*arg) == '(')
3659 emsg(_(e_nowhitespace));
3660 else
3661 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01003662 return FAIL;
3663 }
3664
3665 *arg = skipwhite(*arg + 1);
3666 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3667 return FAIL;
3668
3669 // Compile the function expression.
3670 if (compile_parenthesis(&expr, cctx, ppconst) == FAIL)
3671 return FAIL;
3672
3673 stack = &cctx->ctx_type_stack;
3674 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3675 if (generate_PCALL(cctx, argcount,
3676 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677 return FAIL;
3678 }
3679 else
3680 {
3681 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003682 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003683 if (!eval_isnamec1(*p))
3684 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003685 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003686 return FAIL;
3687 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003688 if (ASCII_ISALPHA(*p) && p[1] == ':')
3689 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003690 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 ;
3692 if (*p != '(')
3693 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003694 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695 return FAIL;
3696 }
3697 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003698 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699 return FAIL;
3700 }
3701 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003702 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003704 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003705 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003706 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003707 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003708 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003709
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003710 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003711 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003712 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003713 // TODO: blob index
3714 // TODO: more arguments
3715 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003716 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003717 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003718 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003719
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003720 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003721 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003722 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003723 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003724 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003725 // missing first index is equal to zero
3726 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003727 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003728 else
3729 {
3730 if (compile_expr0(arg, cctx) == FAIL)
3731 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003732 if (**arg == ':')
3733 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003734 semsg(_(e_white_space_required_before_and_after_str_at_str),
3735 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003736 return FAIL;
3737 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003738 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003739 return FAIL;
3740 *arg = skipwhite(*arg);
3741 }
3742 if (**arg == ':')
3743 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003744 is_slice = TRUE;
3745 ++*arg;
3746 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
3747 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003748 semsg(_(e_white_space_required_before_and_after_str_at_str),
3749 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003750 return FAIL;
3751 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003752 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003753 return FAIL;
3754 if (**arg == ']')
3755 // missing second index is equal to end of string
3756 generate_PUSHNR(cctx, -1);
3757 else
3758 {
3759 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003760 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003761 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003762 return FAIL;
3763 *arg = skipwhite(*arg);
3764 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003765 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766
3767 if (**arg != ']')
3768 {
3769 emsg(_(e_missbrac));
3770 return FAIL;
3771 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003772 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003774 // We can index a list and a dict. If we don't know the type
3775 // we can use the index value type.
3776 // TODO: If we don't know use an instruction to figure it out at
3777 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003778 typep = ((type_T **)stack->ga_data) + stack->ga_len
3779 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003780 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003781 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3782 // If the index is a string, the variable must be a Dict.
3783 if (*typep == &t_any && valtype == &t_string)
3784 vtype = VAR_DICT;
3785 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003786 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003787 if (need_type(valtype, &t_number, -1, cctx,
3788 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003789 return FAIL;
3790 if (is_slice)
3791 {
3792 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003793 if (need_type(valtype, &t_number, -2, cctx,
3794 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003795 return FAIL;
3796 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003797 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003798
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003799 if (vtype == VAR_DICT)
3800 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003801 if (is_slice)
3802 {
3803 emsg(_(e_cannot_slice_dictionary));
3804 return FAIL;
3805 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003806 if ((*typep)->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003807 {
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003808 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003809 if (*typep == &t_unknown)
3810 // empty dict was used
3811 *typep = &t_any;
3812 }
Bram Moolenaar7892b952020-07-20 22:09:34 +02003813 else
3814 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003815 if (need_type(*typep, &t_dict_any, -2, cctx,
3816 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003817 return FAIL;
3818 *typep = &t_any;
3819 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003820 if (may_generate_2STRING(-1, cctx) == FAIL)
3821 return FAIL;
3822 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3823 return FAIL;
3824 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003825 else if (vtype == VAR_STRING)
3826 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003827 *typep = &t_string;
3828 if ((is_slice
3829 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3830 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003831 return FAIL;
3832 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003833 else if (vtype == VAR_BLOB)
3834 {
3835 emsg("Sorry, blob index and slice not implemented yet");
3836 return FAIL;
3837 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003838 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003839 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003840 if (is_slice)
3841 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003842 if (generate_instr_drop(cctx,
3843 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3844 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003845 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003846 }
Bram Moolenaared591872020-08-15 22:14:53 +02003847 else
3848 {
3849 if ((*typep)->tt_type == VAR_LIST)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003850 {
Bram Moolenaared591872020-08-15 22:14:53 +02003851 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003852 if (*typep == &t_unknown)
3853 // empty list was used
3854 *typep = &t_any;
3855 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003856 if (generate_instr_drop(cctx,
3857 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3858 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003859 return FAIL;
3860 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003861 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003862 else
3863 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003864 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003865 return FAIL;
3866 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003868 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003870 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003871 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003872 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003873 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003874
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003875 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003876 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003877 {
3878 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003879 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003880 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003881 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003882 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003883 while (eval_isnamec(*p))
3884 MB_PTR_ADV(p);
3885 if (p == *arg)
3886 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003887 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 return FAIL;
3889 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003890 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003891 return FAIL;
3892 *arg = p;
3893 }
3894 else
3895 break;
3896 }
3897
3898 // TODO - see handle_subscript():
3899 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3900 // Don't do this when "Func" is already a partial that was bound
3901 // explicitly (pt_auto is FALSE).
3902
3903 return OK;
3904}
3905
3906/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003907 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3908 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003909 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003910 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003911 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003912 *
3913 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914 */
3915
3916/*
3917 * number number constant
3918 * 0zFFFFFFFF Blob constant
3919 * "string" string constant
3920 * 'string' literal string constant
3921 * &option-name option value
3922 * @r register contents
3923 * identifier variable value
3924 * function() function call
3925 * $VAR environment variable
3926 * (expression) nested expression
3927 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01003928 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003929 *
3930 * Also handle:
3931 * ! in front logical NOT
3932 * - in front unary minus
3933 * + in front unary plus (ignored)
3934 * trailing (arg) funcref/partial call
3935 * trailing [] subscript in String or List
3936 * trailing .name entry in Dictionary
3937 * trailing ->name() method call
3938 */
3939 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003940compile_expr7(
3941 char_u **arg,
3942 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003943 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 char_u *start_leader, *end_leader;
3946 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003947 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003948 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003950 ppconst->pp_is_const = FALSE;
3951
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 /*
3953 * Skip '!', '-' and '+' characters. They are handled later.
3954 */
3955 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003956 if (eval_leader(arg, TRUE) == FAIL)
3957 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003958 end_leader = *arg;
3959
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003960 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003961 switch (**arg)
3962 {
3963 /*
3964 * Number constant.
3965 */
3966 case '0': // also for blob starting with 0z
3967 case '1':
3968 case '2':
3969 case '3':
3970 case '4':
3971 case '5':
3972 case '6':
3973 case '7':
3974 case '8':
3975 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003976 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003977 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003978 // Apply "-" and "+" just before the number now, right to
3979 // left. Matters especially when "->" follows. Stops at
3980 // '!'.
3981 if (apply_leader(rettv, TRUE,
3982 start_leader, &end_leader) == FAIL)
3983 {
3984 clear_tv(rettv);
3985 return FAIL;
3986 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987 break;
3988
3989 /*
3990 * String constant: "string".
3991 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003992 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003993 return FAIL;
3994 break;
3995
3996 /*
3997 * Literal string constant: 'str''ing'.
3998 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003999 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 return FAIL;
4001 break;
4002
4003 /*
4004 * Constant Vim variable.
4005 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004006 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007 ret = NOTDONE;
4008 break;
4009
4010 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004011 * "true" constant
4012 */
4013 case 't': if (STRNCMP(*arg, "true", 4) == 0
4014 && !eval_isnamec((*arg)[4]))
4015 {
4016 *arg += 4;
4017 rettv->v_type = VAR_BOOL;
4018 rettv->vval.v_number = VVAL_TRUE;
4019 }
4020 else
4021 ret = NOTDONE;
4022 break;
4023
4024 /*
4025 * "false" constant
4026 */
4027 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4028 && !eval_isnamec((*arg)[5]))
4029 {
4030 *arg += 5;
4031 rettv->v_type = VAR_BOOL;
4032 rettv->vval.v_number = VVAL_FALSE;
4033 }
4034 else
4035 ret = NOTDONE;
4036 break;
4037
4038 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004039 * "null" constant
4040 */
4041 case 'n': if (STRNCMP(*arg, "null", 4) == 0
4042 && !eval_isnamec((*arg)[5]))
4043 {
4044 *arg += 4;
4045 rettv->v_type = VAR_SPECIAL;
4046 rettv->vval.v_number = VVAL_NULL;
4047 }
4048 else
4049 ret = NOTDONE;
4050 break;
4051
4052 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053 * List: [expr, expr]
4054 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004055 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004056 break;
4057
4058 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059 * Dictionary: {'key': val, 'key': val}
4060 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004061 case '{': ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062 break;
4063
4064 /*
4065 * Option value: &name
4066 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004067 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4068 return FAIL;
4069 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070 break;
4071
4072 /*
4073 * Environment variable: $VAR.
4074 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004075 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4076 return FAIL;
4077 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004078 break;
4079
4080 /*
4081 * Register contents: @r.
4082 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004083 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4084 return FAIL;
4085 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004086 break;
4087 /*
4088 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004089 * lambda: (arg, arg) => expr
4090 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004092 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4093 ret = compile_lambda(arg, cctx);
4094 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004095 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 break;
4097
4098 default: ret = NOTDONE;
4099 break;
4100 }
4101 if (ret == FAIL)
4102 return FAIL;
4103
Bram Moolenaar1c747212020-05-09 18:28:34 +02004104 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004105 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004106 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004107 clear_tv(rettv);
4108 else
4109 // A constant expression can possibly be handled compile time,
4110 // return the value instead of generating code.
4111 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112 }
4113 else if (ret == NOTDONE)
4114 {
4115 char_u *p;
4116 int r;
4117
4118 if (!eval_isnamec1(**arg))
4119 {
Bram Moolenaare4984292020-12-13 14:19:25 +01004120 if (ends_excmd(*skipwhite(*arg)))
4121 semsg(_(e_empty_expression_str), *arg);
4122 else
4123 semsg(_(e_name_expected_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124 return FAIL;
4125 }
4126
4127 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004128 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004130 {
4131 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4132 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004134 {
4135 if (generate_ppconst(cctx, ppconst) == FAIL)
4136 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004137 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004138 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139 if (r == FAIL)
4140 return FAIL;
4141 }
4142
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004143 // Handle following "[]", ".member", etc.
4144 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004145 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004146 ppconst) == FAIL)
4147 return FAIL;
4148 if (ppconst->pp_used > 0)
4149 {
4150 // apply the '!', '-' and '+' before the constant
4151 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004152 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004153 return FAIL;
4154 return OK;
4155 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004156 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004158 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159}
4160
4161/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004162 * Give the "white on both sides" error, taking the operator from "p[len]".
4163 */
4164 void
4165error_white_both(char_u *op, int len)
4166{
4167 char_u buf[10];
4168
4169 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004170 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004171}
4172
4173/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004174 * <type>expr7: runtime type check / conversion
4175 */
4176 static int
4177compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4178{
4179 type_T *want_type = NULL;
4180
4181 // Recognize <type>
4182 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4183 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004184 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004185 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4186 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004187 return FAIL;
4188
4189 if (**arg != '>')
4190 {
4191 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004192 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004193 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004194 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004195 return FAIL;
4196 }
4197 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004198 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004199 return FAIL;
4200 }
4201
4202 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4203 return FAIL;
4204
4205 if (want_type != NULL)
4206 {
4207 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004208 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004209
Bram Moolenaard1103582020-08-14 22:44:25 +02004210 generate_ppconst(cctx, ppconst);
4211 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02004212 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004213 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004214 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004215 return FAIL;
4216 }
4217 }
4218
4219 return OK;
4220}
4221
4222/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004223 * * number multiplication
4224 * / number division
4225 * % number modulo
4226 */
4227 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004228compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229{
4230 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004231 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004232 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004234 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004235 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236 return FAIL;
4237
4238 /*
4239 * Repeat computing, until no "*", "/" or "%" is following.
4240 */
4241 for (;;)
4242 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004243 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244 if (*op != '*' && *op != '/' && *op != '%')
4245 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004246 if (next != NULL)
4247 {
4248 *arg = next_line_from_context(cctx, TRUE);
4249 op = skipwhite(*arg);
4250 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004251
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004252 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004253 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004254 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004255 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004256 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004257 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004258 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004260 // get the second 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;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004263
4264 if (ppconst->pp_used == ppconst_used + 2
4265 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4266 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004267 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004268 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4269 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004270 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004271
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004272 // both are numbers: compute the result
4273 switch (*op)
4274 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004275 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004276 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004277 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004278 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004279 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004280 break;
4281 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004282 tv1->vval.v_number = res;
4283 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004284 }
4285 else
4286 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004287 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004288 generate_two_op(cctx, op);
4289 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 }
4291
4292 return OK;
4293}
4294
4295/*
4296 * + number addition
4297 * - number subtraction
4298 * .. string concatenation
4299 */
4300 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004301compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302{
4303 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004304 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004306 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307
4308 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004309 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004310 return FAIL;
4311
4312 /*
4313 * Repeat computing, until no "+", "-" or ".." is following.
4314 */
4315 for (;;)
4316 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004317 op = may_peek_next_line(cctx, *arg, &next);
4318 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 break;
4320 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004321 if (next != NULL)
4322 {
4323 *arg = next_line_from_context(cctx, TRUE);
4324 op = skipwhite(*arg);
4325 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004327 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004329 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004330 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331 }
4332
Bram Moolenaare0de1712020-12-02 17:36:54 +01004333 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004334 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004336 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004337 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338 return FAIL;
4339
Bram Moolenaara5565e42020-05-09 15:44:01 +02004340 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004341 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004342 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4343 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4344 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4345 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004346 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004347 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4348 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004349
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004350 // concat/subtract/add constant numbers
4351 if (*op == '+')
4352 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4353 else if (*op == '-')
4354 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4355 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004356 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004357 // concatenate constant strings
4358 char_u *s1 = tv1->vval.v_string;
4359 char_u *s2 = tv2->vval.v_string;
4360 size_t len1 = STRLEN(s1);
4361
4362 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4363 if (tv1->vval.v_string == NULL)
4364 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004365 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004366 return FAIL;
4367 }
4368 mch_memmove(tv1->vval.v_string, s1, len1);
4369 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004370 vim_free(s1);
4371 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004372 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004373 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004374 }
4375 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004376 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004377 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004378 if (*op == '.')
4379 {
4380 if (may_generate_2STRING(-2, cctx) == FAIL
4381 || may_generate_2STRING(-1, cctx) == FAIL)
4382 return FAIL;
4383 generate_instr_drop(cctx, ISN_CONCAT, 1);
4384 }
4385 else
4386 generate_two_op(cctx, op);
4387 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004388 }
4389
4390 return OK;
4391}
4392
4393/*
4394 * expr5a == expr5b
4395 * expr5a =~ expr5b
4396 * expr5a != expr5b
4397 * expr5a !~ expr5b
4398 * expr5a > expr5b
4399 * expr5a >= expr5b
4400 * expr5a < expr5b
4401 * expr5a <= expr5b
4402 * expr5a is expr5b
4403 * expr5a isnot expr5b
4404 *
4405 * Produces instructions:
4406 * EVAL expr5a Push result of "expr5a"
4407 * EVAL expr5b Push result of "expr5b"
4408 * COMPARE one of the compare instructions
4409 */
4410 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004411compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004413 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004415 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004418 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419
4420 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004421 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422 return FAIL;
4423
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004424 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004425 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004426
4427 /*
4428 * If there is a comparative operator, use it.
4429 */
4430 if (type != EXPR_UNKNOWN)
4431 {
4432 int ic = FALSE; // Default: do not ignore case
4433
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004434 if (next != NULL)
4435 {
4436 *arg = next_line_from_context(cctx, TRUE);
4437 p = skipwhite(*arg);
4438 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439 if (type_is && (p[len] == '?' || p[len] == '#'))
4440 {
4441 semsg(_(e_invexpr2), *arg);
4442 return FAIL;
4443 }
4444 // extra question mark appended: ignore case
4445 if (p[len] == '?')
4446 {
4447 ic = TRUE;
4448 ++len;
4449 }
4450 // extra '#' appended: match case (ignored)
4451 else if (p[len] == '#')
4452 ++len;
4453 // nothing appended: match case
4454
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004455 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004456 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004457 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004458 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459 }
4460
4461 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004462 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004463 return FAIL;
4464
Bram Moolenaara5565e42020-05-09 15:44:01 +02004465 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466 return FAIL;
4467
Bram Moolenaara5565e42020-05-09 15:44:01 +02004468 if (ppconst->pp_used == ppconst_used + 2)
4469 {
4470 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4471 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4472 int ret;
4473
4474 // Both sides are a constant, compute the result now.
4475 // First check for a valid combination of types, this is more
4476 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004477 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004478 ret = FAIL;
4479 else
4480 {
4481 ret = typval_compare(tv1, tv2, type, ic);
4482 tv1->v_type = VAR_BOOL;
4483 tv1->vval.v_number = tv1->vval.v_number
4484 ? VVAL_TRUE : VVAL_FALSE;
4485 clear_tv(tv2);
4486 --ppconst->pp_used;
4487 }
4488 return ret;
4489 }
4490
4491 generate_ppconst(cctx, ppconst);
4492 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004493 }
4494
4495 return OK;
4496}
4497
Bram Moolenaar7f141552020-05-09 17:35:53 +02004498static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4499
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004500/*
4501 * Compile || or &&.
4502 */
4503 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004504compile_and_or(
4505 char_u **arg,
4506 cctx_T *cctx,
4507 char *op,
4508 ppconst_T *ppconst,
4509 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004510{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004511 char_u *next;
4512 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004513 int opchar = *op;
4514
4515 if (p[0] == opchar && p[1] == opchar)
4516 {
4517 garray_T *instr = &cctx->ctx_instr;
4518 garray_T end_ga;
4519
4520 /*
4521 * Repeat until there is no following "||" or "&&"
4522 */
4523 ga_init2(&end_ga, sizeof(int), 10);
4524 while (p[0] == opchar && p[1] == opchar)
4525 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004526 if (next != NULL)
4527 {
4528 *arg = next_line_from_context(cctx, TRUE);
4529 p = skipwhite(*arg);
4530 }
4531
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004532 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4533 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004534 semsg(_(e_white_space_required_before_and_after_str_at_str),
4535 op, *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004536 return FAIL;
4537 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004538
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004539 // TODO: use ppconst if the value is a constant and check
4540 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004541 generate_ppconst(cctx, ppconst);
4542
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004543 // Every part must evaluate to a bool.
4544 if (bool_on_stack(cctx) == FAIL)
4545 {
4546 ga_clear(&end_ga);
4547 return FAIL;
4548 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004549
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004550 if (ga_grow(&end_ga, 1) == FAIL)
4551 {
4552 ga_clear(&end_ga);
4553 return FAIL;
4554 }
4555 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4556 ++end_ga.ga_len;
4557 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004558 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004559
4560 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004561 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004562 {
4563 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004564 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004565 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004566
Bram Moolenaara5565e42020-05-09 15:44:01 +02004567 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4568 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004569 {
4570 ga_clear(&end_ga);
4571 return FAIL;
4572 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004573
4574 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004576 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004577
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004578 // Every part must evaluate to a bool.
4579 if (bool_on_stack(cctx) == FAIL)
4580 {
4581 ga_clear(&end_ga);
4582 return FAIL;
4583 }
4584
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 // Fill in the end label in all jumps.
4586 while (end_ga.ga_len > 0)
4587 {
4588 isn_T *isn;
4589
4590 --end_ga.ga_len;
4591 isn = ((isn_T *)instr->ga_data)
4592 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4593 isn->isn_arg.jump.jump_where = instr->ga_len;
4594 }
4595 ga_clear(&end_ga);
4596 }
4597
4598 return OK;
4599}
4600
4601/*
4602 * expr4a && expr4a && expr4a logical AND
4603 *
4604 * Produces instructions:
4605 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004606 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004607 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004609 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610 * EVAL expr4c Push result of "expr4c"
4611 * end:
4612 */
4613 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004614compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004615{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004616 int ppconst_used = ppconst->pp_used;
4617
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004619 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004620 return FAIL;
4621
4622 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004623 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004624}
4625
4626/*
4627 * expr3a || expr3b || expr3c logical OR
4628 *
4629 * Produces instructions:
4630 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004631 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004632 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004633 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004634 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635 * EVAL expr3c Push result of "expr3c"
4636 * end:
4637 */
4638 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004639compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004640{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004641 int ppconst_used = ppconst->pp_used;
4642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004644 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004645 return FAIL;
4646
4647 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004648 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004649}
4650
4651/*
4652 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004654 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004655 * JUMP_IF_FALSE alt jump if false
4656 * EVAL expr1a
4657 * JUMP_ALWAYS end
4658 * alt: EVAL expr1b
4659 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004660 *
4661 * Toplevel expression: expr2 ?? expr1
4662 * Produces instructions:
4663 * EVAL expr2 Push result of "expr2"
4664 * JUMP_AND_KEEP_IF_TRUE end jump if true
4665 * EVAL expr1
4666 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004667 */
4668 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004669compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004670{
4671 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004672 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004673 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004674
Bram Moolenaar3988f642020-08-27 22:43:03 +02004675 // Ignore all kinds of errors when not producing code.
4676 if (cctx->ctx_skip == SKIP_YES)
4677 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004678 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004679 return OK;
4680 }
4681
Bram Moolenaar61a89812020-05-07 16:58:17 +02004682 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004683 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684 return FAIL;
4685
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004686 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004687 if (*p == '?')
4688 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004689 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004690 garray_T *instr = &cctx->ctx_instr;
4691 garray_T *stack = &cctx->ctx_type_stack;
4692 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004693 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004695 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004696 int has_const_expr = FALSE;
4697 int const_value = FALSE;
4698 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004699
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004700 if (next != NULL)
4701 {
4702 *arg = next_line_from_context(cctx, TRUE);
4703 p = skipwhite(*arg);
4704 }
4705
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004706 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004707 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004708 semsg(_(e_white_space_required_before_and_after_str_at_str),
4709 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004710 return FAIL;
4711 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004712
Bram Moolenaara5565e42020-05-09 15:44:01 +02004713 if (ppconst->pp_used == ppconst_used + 1)
4714 {
4715 // the condition is a constant, we know whether the ? or the :
4716 // expression is to be evaluated.
4717 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004718 if (op_falsy)
4719 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4720 else
4721 {
4722 int error = FALSE;
4723
4724 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4725 &error);
4726 if (error)
4727 return FAIL;
4728 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004729 cctx->ctx_skip = save_skip == SKIP_YES ||
4730 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4731
4732 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4733 // "left ?? right" and "left" is truthy: produce "left"
4734 generate_ppconst(cctx, ppconst);
4735 else
4736 {
4737 clear_tv(&ppconst->pp_tv[ppconst_used]);
4738 --ppconst->pp_used;
4739 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004740 }
4741 else
4742 {
4743 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004744 if (op_falsy)
4745 end_idx = instr->ga_len;
4746 generate_JUMP(cctx, op_falsy
4747 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4748 if (op_falsy)
4749 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004750 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004751
4752 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01004753 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004754 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004755 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004756 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004757
Bram Moolenaara5565e42020-05-09 15:44:01 +02004758 if (!has_const_expr)
4759 {
4760 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004761
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004762 if (!op_falsy)
4763 {
4764 // remember the type and drop it
4765 --stack->ga_len;
4766 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004767
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004768 end_idx = instr->ga_len;
4769 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004770
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004771 // jump here from JUMP_IF_FALSE
4772 isn = ((isn_T *)instr->ga_data) + alt_idx;
4773 isn->isn_arg.jump.jump_where = instr->ga_len;
4774 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004775 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004776
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004777 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004778 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004779 // Check for the ":".
4780 p = may_peek_next_line(cctx, *arg, &next);
4781 if (*p != ':')
4782 {
4783 emsg(_(e_missing_colon));
4784 return FAIL;
4785 }
4786 if (next != NULL)
4787 {
4788 *arg = next_line_from_context(cctx, TRUE);
4789 p = skipwhite(*arg);
4790 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004791
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004792 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4793 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004794 semsg(_(e_white_space_required_before_and_after_str_at_str),
4795 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004796 return FAIL;
4797 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004799 // evaluate the third expression
4800 if (has_const_expr)
4801 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004802 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01004803 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004804 return FAIL;
4805 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4806 return FAIL;
4807 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004808
Bram Moolenaara5565e42020-05-09 15:44:01 +02004809 if (!has_const_expr)
4810 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004811 type_T **typep;
4812
Bram Moolenaara5565e42020-05-09 15:44:01 +02004813 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004814
Bram Moolenaara5565e42020-05-09 15:44:01 +02004815 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004816 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4817 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004818
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004819 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004820 isn = ((isn_T *)instr->ga_data) + end_idx;
4821 isn->isn_arg.jump.jump_where = instr->ga_len;
4822 }
4823
4824 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004825 }
4826 return OK;
4827}
4828
4829/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004830 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004831 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4832 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004833 */
4834 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004835compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004836{
4837 ppconst_T ppconst;
4838
4839 CLEAR_FIELD(ppconst);
4840 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4841 {
4842 clear_ppconst(&ppconst);
4843 return FAIL;
4844 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004845 if (is_const != NULL)
4846 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004847 if (generate_ppconst(cctx, &ppconst) == FAIL)
4848 return FAIL;
4849 return OK;
4850}
4851
4852/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004853 * Toplevel expression.
4854 */
4855 static int
4856compile_expr0(char_u **arg, cctx_T *cctx)
4857{
4858 return compile_expr0_ext(arg, cctx, NULL);
4859}
4860
4861/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004862 * compile "return [expr]"
4863 */
4864 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004865compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004866{
4867 char_u *p = arg;
4868 garray_T *stack = &cctx->ctx_type_stack;
4869 type_T *stack_type;
4870
4871 if (*p != NUL && *p != '|' && *p != '\n')
4872 {
4873 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004874 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004875 return NULL;
4876
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004877 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004878 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004879 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01004880 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01004881 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
4882 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004883 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004884 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004885 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004886 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004887 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004888 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4889 && stack_type->tt_type != VAR_VOID
4890 && stack_type->tt_type != VAR_UNKNOWN)
4891 {
4892 emsg(_(e_returning_value_in_function_without_return_type));
4893 return NULL;
4894 }
4895 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004896 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004897 return NULL;
4898 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02004899 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004900 }
4901 else
4902 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004903 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004904 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004905 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4906 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004907 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004908 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004909 return NULL;
4910 }
4911
4912 // No argument, return zero.
4913 generate_PUSHNR(cctx, 0);
4914 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004915 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004916 return NULL;
4917
4918 // "return val | endif" is possible
4919 return skipwhite(p);
4920}
4921
4922/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004923 * Get a line from the compilation context, compatible with exarg_T getline().
4924 * Return a pointer to the line in allocated memory.
4925 * Return NULL for end-of-file or some error.
4926 */
4927 static char_u *
4928exarg_getline(
4929 int c UNUSED,
4930 void *cookie,
4931 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004932 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004933{
4934 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004935 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004936
Bram Moolenaar66250c92020-08-20 15:02:42 +02004937 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004938 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004939 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004940 return NULL;
4941 ++cctx->ctx_lnum;
4942 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4943 // Comment lines result in NULL pointers, skip them.
4944 if (p != NULL)
4945 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004946 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004947}
4948
4949/*
4950 * Compile a nested :def command.
4951 */
4952 static char_u *
4953compile_nested_function(exarg_T *eap, cctx_T *cctx)
4954{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004955 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004956 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004957 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004958 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004959 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004960 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004961
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004962 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004963 {
4964 emsg(_(e_cannot_use_bang_with_nested_def));
4965 return NULL;
4966 }
4967
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004968 if (*name_start == '/')
4969 {
4970 name_end = skip_regexp(name_start + 1, '/', TRUE);
4971 if (*name_end == '/')
4972 ++name_end;
4973 eap->nextcmd = check_nextcmd(name_end);
4974 }
4975 if (name_end == name_start || *skipwhite(name_end) != '(')
4976 {
4977 if (!ends_excmd2(name_start, name_end))
4978 {
4979 semsg(_(e_invalid_command_str), eap->cmd);
4980 return NULL;
4981 }
4982
4983 // "def" or "def Name": list functions
4984 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
4985 return NULL;
4986 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
4987 }
4988
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004989 // Only g:Func() can use a namespace.
4990 if (name_start[1] == ':' && !is_global)
4991 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004992 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004993 return NULL;
4994 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004995 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4996 return NULL;
4997
Bram Moolenaar04b12692020-05-04 23:24:44 +02004998 eap->arg = name_end;
4999 eap->getline = exarg_getline;
5000 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005001 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005002 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005003 lambda_name = vim_strsave(get_lambda_name());
5004 if (lambda_name == NULL)
5005 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005006 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005007
Bram Moolenaar822ba242020-05-24 23:00:18 +02005008 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005009 {
5010 r = eap->skip ? OK : FAIL;
5011 goto theend;
5012 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005013 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02005014 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005015 {
5016 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005017 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005018 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005019
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005020 if (is_global)
5021 {
5022 char_u *func_name = vim_strnsave(name_start + 2,
5023 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005024
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005025 if (func_name == NULL)
5026 r = FAIL;
5027 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005028 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005029 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005030 lambda_name = NULL;
5031 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005032 }
5033 else
5034 {
5035 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005036 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005037 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005038 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02005039
Bram Moolenaareef21022020-08-01 22:16:43 +02005040 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005041 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005042 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005043 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005044 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005045
5046 // copy over the block scope IDs
5047 if (block_depth > 0)
5048 {
5049 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5050 if (ufunc->uf_block_ids != NULL)
5051 {
5052 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5053 sizeof(int) * block_depth);
5054 ufunc->uf_block_depth = block_depth;
5055 }
5056 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005057 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005058 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005059 r = OK;
5060
5061theend:
5062 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005063 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005064}
5065
5066/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005067 * Return the length of an assignment operator, or zero if there isn't one.
5068 */
5069 int
5070assignment_len(char_u *p, int *heredoc)
5071{
5072 if (*p == '=')
5073 {
5074 if (p[1] == '<' && p[2] == '<')
5075 {
5076 *heredoc = TRUE;
5077 return 3;
5078 }
5079 return 1;
5080 }
5081 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5082 return 2;
5083 if (STRNCMP(p, "..=", 3) == 0)
5084 return 3;
5085 return 0;
5086}
5087
5088// words that cannot be used as a variable
5089static char *reserved[] = {
5090 "true",
5091 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005092 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005093 NULL
5094};
5095
Bram Moolenaar752fc692021-01-04 21:57:11 +01005096// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005097typedef enum {
5098 dest_local,
5099 dest_option,
5100 dest_env,
5101 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005102 dest_buffer,
5103 dest_window,
5104 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005105 dest_vimvar,
5106 dest_script,
5107 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005108 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005109} assign_dest_T;
5110
Bram Moolenaar752fc692021-01-04 21:57:11 +01005111// Used by compile_lhs() to store information about the LHS of an assignment
5112// and one argument of ":unlet" with an index.
5113typedef struct {
5114 assign_dest_T lhs_dest; // type of destination
5115
5116 char_u *lhs_name; // allocated name including
5117 // "[expr]" or ".name".
5118 size_t lhs_varlen; // length of the variable without
5119 // "[expr]" or ".name"
5120 char_u *lhs_dest_end; // end of the destination, including
5121 // "[expr]" or ".name".
5122
5123 int lhs_has_index; // has "[expr]" or ".name"
5124
5125 int lhs_new_local; // create new local variable
5126 int lhs_opt_flags; // for when destination is an option
5127 int lhs_vimvaridx; // for when destination is a v:var
5128
5129 lvar_T lhs_local_lvar; // used for existing local destination
5130 lvar_T lhs_arg_lvar; // used for argument destination
5131 lvar_T *lhs_lvar; // points to destination lvar
5132 int lhs_scriptvar_sid;
5133 int lhs_scriptvar_idx;
5134
5135 int lhs_has_type; // type was specified
5136 type_T *lhs_type;
5137 type_T *lhs_member_type;
5138} lhs_T;
5139
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005140/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005141 * Generate the load instruction for "name".
5142 */
5143 static void
5144generate_loadvar(
5145 cctx_T *cctx,
5146 assign_dest_T dest,
5147 char_u *name,
5148 lvar_T *lvar,
5149 type_T *type)
5150{
5151 switch (dest)
5152 {
5153 case dest_option:
5154 // TODO: check the option exists
5155 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5156 break;
5157 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005158 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5159 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5160 else
5161 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005162 break;
5163 case dest_buffer:
5164 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5165 break;
5166 case dest_window:
5167 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5168 break;
5169 case dest_tab:
5170 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5171 break;
5172 case dest_script:
5173 compile_load_scriptvar(cctx,
5174 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5175 break;
5176 case dest_env:
5177 // Include $ in the name here
5178 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5179 break;
5180 case dest_reg:
5181 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5182 break;
5183 case dest_vimvar:
5184 generate_LOADV(cctx, name + 2, TRUE);
5185 break;
5186 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005187 if (lvar->lv_from_outer > 0)
5188 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5189 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005190 else
5191 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5192 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005193 case dest_expr:
5194 // list or dict value should already be on the stack.
5195 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005196 }
5197}
5198
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005199/*
5200 * Skip over "[expr]" or ".member".
5201 * Does not check for any errors.
5202 */
5203 static char_u *
5204skip_index(char_u *start)
5205{
5206 char_u *p = start;
5207
5208 if (*p == '[')
5209 {
5210 p = skipwhite(p + 1);
5211 (void)skip_expr(&p, NULL);
5212 p = skipwhite(p);
5213 if (*p == ']')
5214 return p + 1;
5215 return p;
5216 }
5217 // if (*p == '.')
5218 return to_name_end(p + 1, TRUE);
5219}
5220
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005221 void
5222vim9_declare_error(char_u *name)
5223{
5224 char *scope = "";
5225
5226 switch (*name)
5227 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005228 case 'g': scope = _("global"); break;
5229 case 'b': scope = _("buffer"); break;
5230 case 'w': scope = _("window"); break;
5231 case 't': scope = _("tab"); break;
5232 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005233 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005234 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005235 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005236 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005237 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005238 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005239 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005240 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005241 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005242}
5243
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005244/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005245 * For one assignment figure out the type of destination. Return it in "dest".
5246 * When not recognized "dest" is not set.
5247 * For an option "opt_flags" is set.
5248 * For a v:var "vimvaridx" is set.
5249 * "type" is set to the destination type if known, unchanted otherwise.
5250 * Return FAIL if an error message was given.
5251 */
5252 static int
5253get_var_dest(
5254 char_u *name,
5255 assign_dest_T *dest,
5256 int cmdidx,
5257 int *opt_flags,
5258 int *vimvaridx,
5259 type_T **type,
5260 cctx_T *cctx)
5261{
5262 char_u *p;
5263
5264 if (*name == '&')
5265 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005266 int cc;
5267 long numval;
5268 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005269
5270 *dest = dest_option;
5271 if (cmdidx == CMD_final || cmdidx == CMD_const)
5272 {
5273 emsg(_(e_const_option));
5274 return FAIL;
5275 }
5276 p = name;
5277 p = find_option_end(&p, opt_flags);
5278 if (p == NULL)
5279 {
5280 // cannot happen?
5281 emsg(_(e_letunexp));
5282 return FAIL;
5283 }
5284 cc = *p;
5285 *p = NUL;
5286 opt_type = get_option_value(skip_option_env_lead(name),
5287 &numval, NULL, *opt_flags);
5288 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005289 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005290 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005291 case gov_unknown:
5292 semsg(_(e_unknown_option), name);
5293 return FAIL;
5294 case gov_string:
5295 case gov_hidden_string:
5296 *type = &t_string;
5297 break;
5298 case gov_bool:
5299 case gov_hidden_bool:
5300 *type = &t_bool;
5301 break;
5302 case gov_number:
5303 case gov_hidden_number:
5304 *type = &t_number;
5305 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005306 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005307 }
5308 else if (*name == '$')
5309 {
5310 *dest = dest_env;
5311 *type = &t_string;
5312 }
5313 else if (*name == '@')
5314 {
5315 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5316 {
5317 emsg_invreg(name[1]);
5318 return FAIL;
5319 }
5320 *dest = dest_reg;
5321 *type = &t_string;
5322 }
5323 else if (STRNCMP(name, "g:", 2) == 0)
5324 {
5325 *dest = dest_global;
5326 }
5327 else if (STRNCMP(name, "b:", 2) == 0)
5328 {
5329 *dest = dest_buffer;
5330 }
5331 else if (STRNCMP(name, "w:", 2) == 0)
5332 {
5333 *dest = dest_window;
5334 }
5335 else if (STRNCMP(name, "t:", 2) == 0)
5336 {
5337 *dest = dest_tab;
5338 }
5339 else if (STRNCMP(name, "v:", 2) == 0)
5340 {
5341 typval_T *vtv;
5342 int di_flags;
5343
5344 *vimvaridx = find_vim_var(name + 2, &di_flags);
5345 if (*vimvaridx < 0)
5346 {
5347 semsg(_(e_variable_not_found_str), name);
5348 return FAIL;
5349 }
5350 // We use the current value of "sandbox" here, is that OK?
5351 if (var_check_ro(di_flags, name, FALSE))
5352 return FAIL;
5353 *dest = dest_vimvar;
5354 vtv = get_vim_var_tv(*vimvaridx);
5355 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5356 }
5357 return OK;
5358}
5359
5360/*
5361 * Generate a STORE instruction for "dest", not being "dest_local".
5362 * Return FAIL when out of memory.
5363 */
5364 static int
5365generate_store_var(
5366 cctx_T *cctx,
5367 assign_dest_T dest,
5368 int opt_flags,
5369 int vimvaridx,
5370 int scriptvar_idx,
5371 int scriptvar_sid,
5372 type_T *type,
5373 char_u *name)
5374{
5375 switch (dest)
5376 {
5377 case dest_option:
5378 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5379 opt_flags);
5380 case dest_global:
5381 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005382 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5383 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005384 case dest_buffer:
5385 // include b: with the name, easier to execute that way
5386 return generate_STORE(cctx, ISN_STOREB, 0, name);
5387 case dest_window:
5388 // include w: with the name, easier to execute that way
5389 return generate_STORE(cctx, ISN_STOREW, 0, name);
5390 case dest_tab:
5391 // include t: with the name, easier to execute that way
5392 return generate_STORE(cctx, ISN_STORET, 0, name);
5393 case dest_env:
5394 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5395 case dest_reg:
5396 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5397 case dest_vimvar:
5398 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5399 case dest_script:
5400 if (scriptvar_idx < 0)
5401 {
5402 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005403 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005404
Bram Moolenaar41d61962020-12-06 20:12:43 +01005405 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005406 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5407 scriptvar_sid, type);
5408 if (name_s != name)
5409 vim_free(name_s);
5410 return r;
5411 }
5412 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5413 scriptvar_sid, scriptvar_idx, type);
5414 case dest_local:
5415 case dest_expr:
5416 // cannot happen
5417 break;
5418 }
5419 return FAIL;
5420}
5421
Bram Moolenaar752fc692021-01-04 21:57:11 +01005422 static int
5423is_decl_command(int cmdidx)
5424{
5425 return cmdidx == CMD_let || cmdidx == CMD_var
5426 || cmdidx == CMD_final || cmdidx == CMD_const;
5427}
5428
5429/*
5430 * Figure out the LHS type and other properties for an assignment or one item
5431 * of ":unlet" with an index.
5432 * Returns OK or FAIL.
5433 */
5434 static int
5435compile_lhs(
5436 char_u *var_start,
5437 lhs_T *lhs,
5438 int cmdidx,
5439 int heredoc,
5440 int oplen,
5441 cctx_T *cctx)
5442{
5443 char_u *var_end;
5444 int is_decl = is_decl_command(cmdidx);
5445
5446 CLEAR_POINTER(lhs);
5447 lhs->lhs_dest = dest_local;
5448 lhs->lhs_vimvaridx = -1;
5449 lhs->lhs_scriptvar_idx = -1;
5450
5451 // "dest_end" is the end of the destination, including "[expr]" or
5452 // ".name".
5453 // "var_end" is the end of the variable/option/etc. name.
5454 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5455 if (*var_start == '@')
5456 var_end = var_start + 2;
5457 else
5458 {
5459 // skip over the leading "&", "&l:", "&g:" and "$"
5460 var_end = skip_option_env_lead(var_start);
5461 var_end = to_name_end(var_end, TRUE);
5462 }
5463
5464 // "a: type" is declaring variable "a" with a type, not dict "a:".
5465 if (is_decl && lhs->lhs_dest_end == var_start + 2
5466 && lhs->lhs_dest_end[-1] == ':')
5467 --lhs->lhs_dest_end;
5468 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5469 --var_end;
5470
5471 // compute the length of the destination without "[expr]" or ".name"
5472 lhs->lhs_varlen = var_end - var_start;
5473 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5474 if (lhs->lhs_name == NULL)
5475 return FAIL;
5476 if (heredoc)
5477 lhs->lhs_type = &t_list_string;
5478 else
5479 lhs->lhs_type = &t_any;
5480
5481 if (cctx->ctx_skip != SKIP_YES)
5482 {
5483 int declare_error = FALSE;
5484
5485 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5486 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5487 &lhs->lhs_type, cctx) == FAIL)
5488 return FAIL;
5489 if (lhs->lhs_dest != dest_local)
5490 {
5491 // Specific kind of variable recognized.
5492 declare_error = is_decl;
5493 }
5494 else
5495 {
5496 int idx;
5497
5498 // No specific kind of variable recognized, just a name.
5499 for (idx = 0; reserved[idx] != NULL; ++idx)
5500 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5501 {
5502 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5503 return FAIL;
5504 }
5505
5506
5507 if (lookup_local(var_start, lhs->lhs_varlen,
5508 &lhs->lhs_local_lvar, cctx) == OK)
5509 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5510 else
5511 {
5512 CLEAR_FIELD(lhs->lhs_arg_lvar);
5513 if (arg_exists(var_start, lhs->lhs_varlen,
5514 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5515 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5516 {
5517 if (is_decl)
5518 {
5519 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5520 return FAIL;
5521 }
5522 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5523 }
5524 }
5525 if (lhs->lhs_lvar != NULL)
5526 {
5527 if (is_decl)
5528 {
5529 semsg(_(e_variable_already_declared), lhs->lhs_name);
5530 return FAIL;
5531 }
5532 }
5533 else
5534 {
5535 int script_namespace = lhs->lhs_varlen > 1
5536 && STRNCMP(var_start, "s:", 2) == 0;
5537 int script_var = (script_namespace
5538 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
5539 FALSE, cctx)
5540 : script_var_exists(var_start, lhs->lhs_varlen,
5541 TRUE, cctx)) == OK;
5542 imported_T *import =
5543 find_imported(var_start, lhs->lhs_varlen, cctx);
5544
5545 if (script_namespace || script_var || import != NULL)
5546 {
5547 char_u *rawname = lhs->lhs_name
5548 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5549
5550 if (is_decl)
5551 {
5552 if (script_namespace)
5553 semsg(_(e_cannot_declare_script_variable_in_function),
5554 lhs->lhs_name);
5555 else
5556 semsg(_(e_variable_already_declared_in_script),
5557 lhs->lhs_name);
5558 return FAIL;
5559 }
5560 else if (cctx->ctx_ufunc->uf_script_ctx_version
5561 == SCRIPT_VERSION_VIM9
5562 && script_namespace
5563 && !script_var && import == NULL)
5564 {
5565 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5566 return FAIL;
5567 }
5568
5569 lhs->lhs_dest = dest_script;
5570
5571 // existing script-local variables should have a type
5572 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5573 if (import != NULL)
5574 lhs->lhs_scriptvar_sid = import->imp_sid;
5575 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5576 {
5577 lhs->lhs_scriptvar_idx = get_script_item_idx(
5578 lhs->lhs_scriptvar_sid,
5579 rawname, TRUE, cctx);
5580 if (lhs->lhs_scriptvar_idx >= 0)
5581 {
5582 scriptitem_T *si = SCRIPT_ITEM(
5583 lhs->lhs_scriptvar_sid);
5584 svar_T *sv =
5585 ((svar_T *)si->sn_var_vals.ga_data)
5586 + lhs->lhs_scriptvar_idx;
5587 lhs->lhs_type = sv->sv_type;
5588 }
5589 }
5590 }
5591 else if (check_defined(var_start, lhs->lhs_varlen, cctx)
5592 == FAIL)
5593 return FAIL;
5594 }
5595 }
5596
5597 if (declare_error)
5598 {
5599 vim9_declare_error(lhs->lhs_name);
5600 return FAIL;
5601 }
5602 }
5603
5604 // handle "a:name" as a name, not index "name" on "a"
5605 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5606 var_end = lhs->lhs_dest_end;
5607
5608 if (lhs->lhs_dest != dest_option)
5609 {
5610 if (is_decl && *var_end == ':')
5611 {
5612 char_u *p;
5613
5614 // parse optional type: "let var: type = expr"
5615 if (!VIM_ISWHITE(var_end[1]))
5616 {
5617 semsg(_(e_white_space_required_after_str), ":");
5618 return FAIL;
5619 }
5620 p = skipwhite(var_end + 1);
5621 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5622 if (lhs->lhs_type == NULL)
5623 return FAIL;
5624 lhs->lhs_has_type = TRUE;
5625 }
5626 else if (lhs->lhs_lvar != NULL)
5627 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5628 }
5629
5630 if (oplen == 3 && !heredoc && lhs->lhs_dest != dest_global
5631 && lhs->lhs_type->tt_type != VAR_STRING
5632 && lhs->lhs_type->tt_type != VAR_ANY)
5633 {
5634 emsg(_(e_can_only_concatenate_to_string));
5635 return FAIL;
5636 }
5637
5638 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5639 && cctx->ctx_skip != SKIP_YES)
5640 {
5641 if (oplen > 1 && !heredoc)
5642 {
5643 // +=, /=, etc. require an existing variable
5644 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5645 return FAIL;
5646 }
5647 if (!is_decl)
5648 {
5649 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5650 return FAIL;
5651 }
5652
5653 // new local variable
5654 if ((lhs->lhs_type->tt_type == VAR_FUNC
5655 || lhs->lhs_type->tt_type == VAR_PARTIAL)
5656 && var_wrong_func_name(lhs->lhs_name, TRUE))
5657 return FAIL;
5658 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5659 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5660 if (lhs->lhs_lvar == NULL)
5661 return FAIL;
5662 lhs->lhs_new_local = TRUE;
5663 }
5664
5665 lhs->lhs_member_type = lhs->lhs_type;
5666 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5667 {
5668 // Something follows after the variable: "var[idx]" or "var.key".
5669 // TODO: should we also handle "->func()" here?
5670 if (is_decl)
5671 {
5672 emsg(_(e_cannot_use_index_when_declaring_variable));
5673 return FAIL;
5674 }
5675
5676 if (var_start[lhs->lhs_varlen] == '['
5677 || var_start[lhs->lhs_varlen] == '.')
5678 {
5679 char_u *after = var_start + lhs->lhs_varlen;
5680 char_u *p;
5681
5682 // Only the last index is used below, if there are others
5683 // before it generate code for the expression. Thus for
5684 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5685 for (;;)
5686 {
5687 p = skip_index(after);
5688 if (*p != '[' && *p != '.')
5689 break;
5690 after = p;
5691 }
5692 if (after > var_start + lhs->lhs_varlen)
5693 {
5694 lhs->lhs_varlen = after - var_start;
5695 lhs->lhs_dest = dest_expr;
5696 // We don't know the type before evaluating the expression,
5697 // use "any" until then.
5698 lhs->lhs_type = &t_any;
5699 }
5700
5701 lhs->lhs_has_index = TRUE;
5702 if (lhs->lhs_type->tt_member == NULL)
5703 lhs->lhs_member_type = &t_any;
5704 else
5705 lhs->lhs_member_type = lhs->lhs_type->tt_member;
5706 }
5707 else
5708 {
5709 semsg("Not supported yet: %s", var_start);
5710 return FAIL;
5711 }
5712 }
5713 return OK;
5714}
5715
5716/*
5717 * Assignment to a list or dict member, or ":unlet" for the item, using the
5718 * information in "lhs".
5719 * Returns OK or FAIL.
5720 */
5721 static int
5722compile_assign_unlet(
5723 char_u *var_start,
5724 lhs_T *lhs,
5725 int is_assign,
5726 type_T *rhs_type,
5727 cctx_T *cctx)
5728{
5729 char_u *p;
5730 int r;
5731 vartype_T dest_type;
5732 size_t varlen = lhs->lhs_varlen;
5733 garray_T *stack = &cctx->ctx_type_stack;
5734
5735 // Compile the "idx" in "var[idx]" or "key" in "var.key".
5736 p = var_start + varlen;
5737 if (*p == '[')
5738 {
5739 p = skipwhite(p + 1);
5740 r = compile_expr0(&p, cctx);
5741 if (r == OK && *skipwhite(p) != ']')
5742 {
5743 // this should not happen
5744 emsg(_(e_missbrac));
5745 r = FAIL;
5746 }
5747 }
5748 else // if (*p == '.')
5749 {
5750 char_u *key_end = to_name_end(p + 1, TRUE);
5751 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5752
5753 r = generate_PUSHS(cctx, key);
5754 }
5755 if (r == FAIL)
5756 return FAIL;
5757
5758 if (lhs->lhs_type == &t_any)
5759 {
5760 // Index on variable of unknown type: check at runtime.
5761 dest_type = VAR_ANY;
5762 }
5763 else
5764 {
5765 dest_type = lhs->lhs_type->tt_type;
5766 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
5767 return FAIL;
5768 if (dest_type == VAR_LIST
5769 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
5770 != VAR_NUMBER)
5771 {
5772 emsg(_(e_number_exp));
5773 return FAIL;
5774 }
5775 }
5776
5777 // Load the dict or list. On the stack we then have:
5778 // - value (for assignment, not for :unlet)
5779 // - index
5780 // - variable
5781 if (lhs->lhs_dest == dest_expr)
5782 {
5783 int c = var_start[varlen];
5784
5785 // Evaluate "ll[expr]" of "ll[expr][idx]"
5786 p = var_start;
5787 var_start[varlen] = NUL;
5788 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
5789 {
5790 // this should not happen
5791 emsg(_(e_missbrac));
5792 return FAIL;
5793 }
5794 var_start[varlen] = c;
5795
5796 lhs->lhs_type = stack->ga_len == 0 ? &t_void
5797 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5798 // now we can properly check the type
5799 if (lhs->lhs_type->tt_member != NULL && rhs_type != &t_void
5800 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, cctx,
5801 FALSE, FALSE) == FAIL)
5802 return FAIL;
5803 }
5804 else
5805 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
5806 lhs->lhs_lvar, lhs->lhs_type);
5807
5808 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
5809 {
5810 if (is_assign)
5811 {
5812 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
5813
5814 if (isn == NULL)
5815 return FAIL;
5816 isn->isn_arg.vartype = dest_type;
5817 }
5818 else
5819 {
5820 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
5821 return FAIL;
5822 }
5823 }
5824 else
5825 {
5826 emsg(_(e_indexable_type_required));
5827 return FAIL;
5828 }
5829
5830 return OK;
5831}
5832
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005833/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005834 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005835 * "let name"
5836 * "var name = expr"
5837 * "final name = expr"
5838 * "const name = expr"
5839 * "name = expr"
5840 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005841 * Return NULL for an error.
5842 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005843 */
5844 static char_u *
5845compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5846{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005847 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005848 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005849 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005850 char_u *ret = NULL;
5851 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005852 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005853 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005854 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005855 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005856 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005857 int oplen = 0;
5858 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005859 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005860 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005861 int is_decl = is_decl_command(cmdidx);
5862 lhs_T lhs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005863
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005864 // Skip over the "var" or "[var, var]" to get to any "=".
5865 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5866 if (p == NULL)
5867 return *arg == '[' ? arg : NULL;
5868
5869 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005870 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005871 // TODO: should we allow this, and figure out type inference from list
5872 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005873 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005874 return NULL;
5875 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01005876 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005877
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005878 sp = p;
5879 p = skipwhite(p);
5880 op = p;
5881 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005882
5883 if (var_count > 0 && oplen == 0)
5884 // can be something like "[1, 2]->func()"
5885 return arg;
5886
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005887 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005888 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005889 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005890 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005891 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005892
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005893 if (heredoc)
5894 {
5895 list_T *l;
5896 listitem_T *li;
5897
5898 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005899 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005900 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005901 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005902 if (l == NULL)
5903 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005904
Bram Moolenaar078269b2020-09-21 20:35:55 +02005905 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005906 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005907 // Push each line and the create the list.
5908 FOR_ALL_LIST_ITEMS(l, li)
5909 {
5910 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5911 li->li_tv.vval.v_string = NULL;
5912 }
5913 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005914 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005915 list_free(l);
5916 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005917 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005918 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005919 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005920 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005921 char_u *wp;
5922
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005923 // for "[var, var] = expr" evaluate the expression here, loop over the
5924 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005925 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005926
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005927 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01005928 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005929 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005930 if (compile_expr0(&p, cctx) == FAIL)
5931 return NULL;
5932 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005933
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005934 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005935 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005936 type_T *stacktype;
5937
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005938 stacktype = stack->ga_len == 0 ? &t_void
5939 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005940 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005941 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005942 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005943 goto theend;
5944 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005945 if (need_type(stacktype, &t_list_any, -1, cctx,
5946 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005947 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005948 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005949 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5950 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005951 if (stacktype->tt_member != NULL)
5952 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005953 }
5954 }
5955
5956 /*
5957 * Loop over variables in "[var, var] = expr".
5958 * For "var = expr" and "let var: type" this is done only once.
5959 */
5960 if (var_count > 0)
5961 var_start = skipwhite(arg + 1); // skip over the "["
5962 else
5963 var_start = arg;
5964 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5965 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005966 int instr_count = -1;
5967
Bram Moolenaar752fc692021-01-04 21:57:11 +01005968 vim_free(lhs.lhs_name);
5969
5970 /*
5971 * Figure out the LHS type and other properties.
5972 */
5973 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
5974 goto theend;
5975
5976 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02005977 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01005978 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005979 goto theend;
5980 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01005981 if (!is_decl && lhs.lhs_lvar != NULL
5982 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005983 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01005984 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005985 goto theend;
5986 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005987
5988 if (!heredoc)
5989 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005990 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005991 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005992 if (oplen > 0 && var_count == 0)
5993 {
5994 // skip over the "=" and the expression
5995 p = skipwhite(op + oplen);
5996 compile_expr0(&p, cctx);
5997 }
5998 }
5999 else if (oplen > 0)
6000 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006001 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006002 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006003
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006004 // For "var = expr" evaluate the expression.
6005 if (var_count == 0)
6006 {
6007 int r;
6008
6009 // for "+=", "*=", "..=" etc. first load the current value
6010 if (*op != '=')
6011 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006012 generate_loadvar(cctx, lhs.lhs_dest, lhs.lhs_name,
6013 lhs.lhs_lvar, lhs.lhs_type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006014
Bram Moolenaar752fc692021-01-04 21:57:11 +01006015 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006016 {
6017 // TODO: get member from list or dict
6018 emsg("Index with operation not supported yet");
6019 goto theend;
6020 }
6021 }
6022
6023 // Compile the expression. Temporarily hide the new local
6024 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006025 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006026 --cctx->ctx_locals.ga_len;
6027 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006028 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006029 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006030 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006031 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006032 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006033 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006034 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006035 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006036 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006037 ++cctx->ctx_locals.ga_len;
6038 if (r == FAIL)
6039 goto theend;
6040 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006041 else if (semicolon && var_idx == var_count - 1)
6042 {
6043 // For "[var; var] = expr" get the rest of the list
6044 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6045 goto theend;
6046 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006047 else
6048 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006049 // For "[var, var] = expr" get the "var_idx" item from the
6050 // list.
6051 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006052 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006053 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006054
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006055 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006056 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006057 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006058 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006059 if ((rhs_type->tt_type == VAR_FUNC
6060 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006061 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006062 goto theend;
6063
Bram Moolenaar752fc692021-01-04 21:57:11 +01006064 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006065 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006066 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006067 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006068 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006069 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006070 }
6071 else
6072 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006073 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006074 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006075 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006076 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006077 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006078 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006079 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006080 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006081 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006082 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006083 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006084 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006085 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006086 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006087 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006088
Bram Moolenaar71abe482020-09-14 22:28:30 +02006089 // without operator check type here, otherwise below
Bram Moolenaar752fc692021-01-04 21:57:11 +01006090 if (lhs.lhs_has_index)
6091 use_type = lhs.lhs_member_type;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006092 if (need_type(rhs_type, use_type, -1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006093 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006094 goto theend;
6095 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006096 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006097 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
6098 -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006099 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006100 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006101 else if (cmdidx == CMD_final)
6102 {
6103 emsg(_(e_final_requires_a_value));
6104 goto theend;
6105 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006106 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006107 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006108 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006109 goto theend;
6110 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006111 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006112 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006113 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006114 goto theend;
6115 }
6116 else
6117 {
6118 // variables are always initialized
6119 if (ga_grow(instr, 1) == FAIL)
6120 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006121 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006122 {
6123 case VAR_BOOL:
6124 generate_PUSHBOOL(cctx, VVAL_FALSE);
6125 break;
6126 case VAR_FLOAT:
6127#ifdef FEAT_FLOAT
6128 generate_PUSHF(cctx, 0.0);
6129#endif
6130 break;
6131 case VAR_STRING:
6132 generate_PUSHS(cctx, NULL);
6133 break;
6134 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006135 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006136 break;
6137 case VAR_FUNC:
6138 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6139 break;
6140 case VAR_LIST:
6141 generate_NEWLIST(cctx, 0);
6142 break;
6143 case VAR_DICT:
6144 generate_NEWDICT(cctx, 0);
6145 break;
6146 case VAR_JOB:
6147 generate_PUSHJOB(cctx, NULL);
6148 break;
6149 case VAR_CHANNEL:
6150 generate_PUSHCHANNEL(cctx, NULL);
6151 break;
6152 case VAR_NUMBER:
6153 case VAR_UNKNOWN:
6154 case VAR_ANY:
6155 case VAR_PARTIAL:
6156 case VAR_VOID:
6157 case VAR_SPECIAL: // cannot happen
6158 generate_PUSHNR(cctx, 0);
6159 break;
6160 }
6161 }
6162 if (var_count == 0)
6163 end = p;
6164 }
6165
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006166 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006167 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006168 break;
6169
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006170 if (oplen > 0 && *op != '=')
6171 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006172 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006173 type_T *stacktype;
6174
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006175 if (*op == '.')
6176 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006177 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006178 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006179 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006180 if (
6181#ifdef FEAT_FLOAT
6182 // If variable is float operation with number is OK.
6183 !(expected == &t_float && stacktype == &t_number) &&
6184#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006185 need_type(stacktype, expected, -1, cctx,
6186 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006187 goto theend;
6188
6189 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006190 {
6191 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6192 goto theend;
6193 }
6194 else if (*op == '+')
6195 {
6196 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006197 operator_type(lhs.lhs_member_type, stacktype),
6198 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006199 goto theend;
6200 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006201 else if (generate_two_op(cctx, op) == FAIL)
6202 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006203 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006204
Bram Moolenaar752fc692021-01-04 21:57:11 +01006205 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006206 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006207 // Use the info in "lhs" to store the value at the index in the
6208 // list or dict.
6209 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6210 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006211 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006212 }
6213 else
6214 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006215 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
6216 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006217 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006218 generate_LOCKCONST(cctx);
6219
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006220 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006221 && (lhs.lhs_type->tt_type == VAR_DICT
6222 || lhs.lhs_type->tt_type == VAR_LIST)
6223 && lhs.lhs_type->tt_member != NULL
6224 && lhs.lhs_type->tt_member != &t_any
6225 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006226 // Set the type in the list or dict, so that it can be checked,
6227 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006228 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006229
Bram Moolenaar752fc692021-01-04 21:57:11 +01006230 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006231 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006232 if (generate_store_var(cctx, lhs.lhs_dest,
6233 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6234 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6235 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006236 goto theend;
6237 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006238 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006239 {
6240 isn_T *isn = ((isn_T *)instr->ga_data)
6241 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006242
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006243 // optimization: turn "var = 123" from ISN_PUSHNR +
6244 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006245 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006246 && instr->ga_len == instr_count + 1
6247 && isn->isn_type == ISN_PUSHNR)
6248 {
6249 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006250
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006251 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006252 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006253 isn->isn_arg.storenr.stnr_val = val;
6254 if (stack->ga_len > 0)
6255 --stack->ga_len;
6256 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006257 else if (lhs.lhs_lvar->lv_from_outer > 0)
6258 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6259 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006260 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006261 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006262 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006263 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006264
6265 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006266 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006267 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006268
6269 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006270 if (var_count > 0 && !semicolon)
6271 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006272 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006273 goto theend;
6274 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006275
Bram Moolenaarb2097502020-07-19 17:17:02 +02006276 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006277
6278theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006279 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006280 return ret;
6281}
6282
6283/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006284 * Check for an assignment at "eap->cmd", compile it if found.
6285 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6286 */
6287 static int
6288may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6289{
6290 char_u *pskip;
6291 char_u *p;
6292
6293 // Assuming the command starts with a variable or function name,
6294 // find what follows.
6295 // Skip over "var.member", "var[idx]" and the like.
6296 // Also "&opt = val", "$ENV = val" and "@r = val".
6297 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6298 ? eap->cmd + 1 : eap->cmd;
6299 p = to_name_end(pskip, TRUE);
6300 if (p > eap->cmd && *p != NUL)
6301 {
6302 char_u *var_end;
6303 int oplen;
6304 int heredoc;
6305
6306 if (eap->cmd[0] == '@')
6307 var_end = eap->cmd + 2;
6308 else
6309 var_end = find_name_end(pskip, NULL, NULL,
6310 FNE_CHECK_START | FNE_INCL_BR);
6311 oplen = assignment_len(skipwhite(var_end), &heredoc);
6312 if (oplen > 0)
6313 {
6314 size_t len = p - eap->cmd;
6315
6316 // Recognize an assignment if we recognize the variable
6317 // name:
6318 // "g:var = expr"
6319 // "local = expr" where "local" is a local var.
6320 // "script = expr" where "script" is a script-local var.
6321 // "import = expr" where "import" is an imported var
6322 // "&opt = expr"
6323 // "$ENV = expr"
6324 // "@r = expr"
6325 if (*eap->cmd == '&'
6326 || *eap->cmd == '$'
6327 || *eap->cmd == '@'
6328 || ((len) > 2 && eap->cmd[1] == ':')
6329 || lookup_local(eap->cmd, len, NULL, cctx) == OK
6330 || arg_exists(eap->cmd, len, NULL, NULL, NULL, cctx) == OK
6331 || script_var_exists(eap->cmd, len, FALSE, cctx) == OK
6332 || find_imported(eap->cmd, len, cctx) != NULL)
6333 {
6334 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6335 if (*line == NULL || *line == eap->cmd)
6336 return FAIL;
6337 return OK;
6338 }
6339 }
6340 }
6341
6342 if (*eap->cmd == '[')
6343 {
6344 // [var, var] = expr
6345 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6346 if (*line == NULL)
6347 return FAIL;
6348 if (*line != eap->cmd)
6349 return OK;
6350 }
6351 return NOTDONE;
6352}
6353
6354/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006355 * Check if "name" can be "unlet".
6356 */
6357 int
6358check_vim9_unlet(char_u *name)
6359{
6360 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6361 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006362 // "unlet s:var" is allowed in legacy script.
6363 if (*name == 's' && !script_is_vim9())
6364 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006365 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006366 return FAIL;
6367 }
6368 return OK;
6369}
6370
6371/*
6372 * Callback passed to ex_unletlock().
6373 */
6374 static int
6375compile_unlet(
6376 lval_T *lvp,
6377 char_u *name_end,
6378 exarg_T *eap,
6379 int deep UNUSED,
6380 void *coookie)
6381{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006382 cctx_T *cctx = coookie;
6383 char_u *p = lvp->ll_name;
6384 int cc = *name_end;
6385 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006386
Bram Moolenaar752fc692021-01-04 21:57:11 +01006387 if (cctx->ctx_skip == SKIP_YES)
6388 return OK;
6389
6390 *name_end = NUL;
6391 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006392 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006393 // :unlet $ENV_VAR
6394 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6395 }
6396 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6397 {
6398 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006399
Bram Moolenaar752fc692021-01-04 21:57:11 +01006400 // This is similar to assigning: lookup the list/dict, compile the
6401 // idx/key. Then instead of storing the value unlet the item.
6402 // unlet {list}[idx]
6403 // unlet {dict}[key] dict.key
6404 //
6405 // Figure out the LHS type and other properties.
6406 //
6407 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6408
6409 // : unlet an indexed item
6410 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006411 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006412 iemsg("called compile_lhs() without an index");
6413 ret = FAIL;
6414 }
6415 else
6416 {
6417 // Use the info in "lhs" to unlet the item at the index in the
6418 // list or dict.
6419 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006420 }
6421
Bram Moolenaar752fc692021-01-04 21:57:11 +01006422 vim_free(lhs.lhs_name);
6423 }
6424 else if (check_vim9_unlet(p) == FAIL)
6425 {
6426 ret = FAIL;
6427 }
6428 else
6429 {
6430 // Normal name. Only supports g:, w:, t: and b: namespaces.
6431 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006432 }
6433
Bram Moolenaar752fc692021-01-04 21:57:11 +01006434 *name_end = cc;
6435 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006436}
6437
6438/*
6439 * compile "unlet var", "lock var" and "unlock var"
6440 * "arg" points to "var".
6441 */
6442 static char_u *
6443compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6444{
6445 char_u *p = arg;
6446
6447 if (eap->cmdidx != CMD_unlet)
6448 {
6449 emsg("Sorry, :lock and unlock not implemented yet");
6450 return NULL;
6451 }
6452
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006453 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6454 compile_unlet, cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006455 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6456}
6457
6458/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006459 * Compile an :import command.
6460 */
6461 static char_u *
6462compile_import(char_u *arg, cctx_T *cctx)
6463{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006464 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006465}
6466
6467/*
6468 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6469 */
6470 static int
6471compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6472{
6473 garray_T *instr = &cctx->ctx_instr;
6474 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6475
6476 if (endlabel == NULL)
6477 return FAIL;
6478 endlabel->el_next = *el;
6479 *el = endlabel;
6480 endlabel->el_end_label = instr->ga_len;
6481
6482 generate_JUMP(cctx, when, 0);
6483 return OK;
6484}
6485
6486 static void
6487compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
6488{
6489 garray_T *instr = &cctx->ctx_instr;
6490
6491 while (*el != NULL)
6492 {
6493 endlabel_T *cur = (*el);
6494 isn_T *isn;
6495
6496 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
6497 isn->isn_arg.jump.jump_where = instr->ga_len;
6498 *el = cur->el_next;
6499 vim_free(cur);
6500 }
6501}
6502
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006503 static void
6504compile_free_jump_to_end(endlabel_T **el)
6505{
6506 while (*el != NULL)
6507 {
6508 endlabel_T *cur = (*el);
6509
6510 *el = cur->el_next;
6511 vim_free(cur);
6512 }
6513}
6514
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006515/*
6516 * Create a new scope and set up the generic items.
6517 */
6518 static scope_T *
6519new_scope(cctx_T *cctx, scopetype_T type)
6520{
6521 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6522
6523 if (scope == NULL)
6524 return NULL;
6525 scope->se_outer = cctx->ctx_scope;
6526 cctx->ctx_scope = scope;
6527 scope->se_type = type;
6528 scope->se_local_count = cctx->ctx_locals.ga_len;
6529 return scope;
6530}
6531
6532/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006533 * Free the current scope and go back to the outer scope.
6534 */
6535 static void
6536drop_scope(cctx_T *cctx)
6537{
6538 scope_T *scope = cctx->ctx_scope;
6539
6540 if (scope == NULL)
6541 {
6542 iemsg("calling drop_scope() without a scope");
6543 return;
6544 }
6545 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006546 switch (scope->se_type)
6547 {
6548 case IF_SCOPE:
6549 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6550 case FOR_SCOPE:
6551 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6552 case WHILE_SCOPE:
6553 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6554 case TRY_SCOPE:
6555 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6556 case NO_SCOPE:
6557 case BLOCK_SCOPE:
6558 break;
6559 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006560 vim_free(scope);
6561}
6562
6563/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006564 * compile "if expr"
6565 *
6566 * "if expr" Produces instructions:
6567 * EVAL expr Push result of "expr"
6568 * JUMP_IF_FALSE end
6569 * ... body ...
6570 * end:
6571 *
6572 * "if expr | else" Produces instructions:
6573 * EVAL expr Push result of "expr"
6574 * JUMP_IF_FALSE else
6575 * ... body ...
6576 * JUMP_ALWAYS end
6577 * else:
6578 * ... body ...
6579 * end:
6580 *
6581 * "if expr1 | elseif expr2 | else" Produces instructions:
6582 * EVAL expr Push result of "expr"
6583 * JUMP_IF_FALSE elseif
6584 * ... body ...
6585 * JUMP_ALWAYS end
6586 * elseif:
6587 * EVAL expr Push result of "expr"
6588 * JUMP_IF_FALSE else
6589 * ... body ...
6590 * JUMP_ALWAYS end
6591 * else:
6592 * ... body ...
6593 * end:
6594 */
6595 static char_u *
6596compile_if(char_u *arg, cctx_T *cctx)
6597{
6598 char_u *p = arg;
6599 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006600 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006601 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006602 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006603 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006604
Bram Moolenaara5565e42020-05-09 15:44:01 +02006605 CLEAR_FIELD(ppconst);
6606 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006607 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006608 clear_ppconst(&ppconst);
6609 return NULL;
6610 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006611 if (cctx->ctx_skip == SKIP_YES)
6612 clear_ppconst(&ppconst);
6613 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006614 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006615 int error = FALSE;
6616 int v;
6617
Bram Moolenaara5565e42020-05-09 15:44:01 +02006618 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006619 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006620 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006621 if (error)
6622 return NULL;
6623 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006624 }
6625 else
6626 {
6627 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006628 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006629 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006630 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006631 if (bool_on_stack(cctx) == FAIL)
6632 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006633 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006634
6635 scope = new_scope(cctx, IF_SCOPE);
6636 if (scope == NULL)
6637 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006638 scope->se_skip_save = skip_save;
6639 // "is_had_return" will be reset if any block does not end in :return
6640 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006641
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006642 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006643 {
6644 // "where" is set when ":elseif", "else" or ":endif" is found
6645 scope->se_u.se_if.is_if_label = instr->ga_len;
6646 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6647 }
6648 else
6649 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006650
6651 return p;
6652}
6653
6654 static char_u *
6655compile_elseif(char_u *arg, cctx_T *cctx)
6656{
6657 char_u *p = arg;
6658 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006659 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006660 isn_T *isn;
6661 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006662 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006663 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006664
6665 if (scope == NULL || scope->se_type != IF_SCOPE)
6666 {
6667 emsg(_(e_elseif_without_if));
6668 return NULL;
6669 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006670 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006671 if (!cctx->ctx_had_return)
6672 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006673
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006674 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006675 {
6676 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006677 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006678 return NULL;
6679 // previous "if" or "elseif" jumps here
6680 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6681 isn->isn_arg.jump.jump_where = instr->ga_len;
6682 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006683
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006684 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006685 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006686 if (cctx->ctx_skip == SKIP_YES)
6687 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006688 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006689 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006690 clear_ppconst(&ppconst);
6691 return NULL;
6692 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006693 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006694 if (scope->se_skip_save == SKIP_YES)
6695 clear_ppconst(&ppconst);
6696 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006697 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006698 int error = FALSE;
6699 int v;
6700
Bram Moolenaar7f141552020-05-09 17:35:53 +02006701 // The expression results in a constant.
6702 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006703 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6704 if (error)
6705 return NULL;
6706 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006707 clear_ppconst(&ppconst);
6708 scope->se_u.se_if.is_if_label = -1;
6709 }
6710 else
6711 {
6712 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006713 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006714 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006715 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006716 if (bool_on_stack(cctx) == FAIL)
6717 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006718
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006719 // "where" is set when ":elseif", "else" or ":endif" is found
6720 scope->se_u.se_if.is_if_label = instr->ga_len;
6721 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6722 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006723
6724 return p;
6725}
6726
6727 static char_u *
6728compile_else(char_u *arg, cctx_T *cctx)
6729{
6730 char_u *p = arg;
6731 garray_T *instr = &cctx->ctx_instr;
6732 isn_T *isn;
6733 scope_T *scope = cctx->ctx_scope;
6734
6735 if (scope == NULL || scope->se_type != IF_SCOPE)
6736 {
6737 emsg(_(e_else_without_if));
6738 return NULL;
6739 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006740 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006741 if (!cctx->ctx_had_return)
6742 scope->se_u.se_if.is_had_return = FALSE;
6743 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006744
Bram Moolenaarefd88552020-06-18 20:50:10 +02006745 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006746 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006747 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006748 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006749 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006750 if (!cctx->ctx_had_return
6751 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6752 JUMP_ALWAYS, cctx) == FAIL)
6753 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006754 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006755
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006756 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006757 {
6758 if (scope->se_u.se_if.is_if_label >= 0)
6759 {
6760 // previous "if" or "elseif" jumps here
6761 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6762 isn->isn_arg.jump.jump_where = instr->ga_len;
6763 scope->se_u.se_if.is_if_label = -1;
6764 }
6765 }
6766
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006767 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006768 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6769 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006770
6771 return p;
6772}
6773
6774 static char_u *
6775compile_endif(char_u *arg, cctx_T *cctx)
6776{
6777 scope_T *scope = cctx->ctx_scope;
6778 ifscope_T *ifscope;
6779 garray_T *instr = &cctx->ctx_instr;
6780 isn_T *isn;
6781
6782 if (scope == NULL || scope->se_type != IF_SCOPE)
6783 {
6784 emsg(_(e_endif_without_if));
6785 return NULL;
6786 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006787 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006788 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006789 if (!cctx->ctx_had_return)
6790 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006791
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006792 if (scope->se_u.se_if.is_if_label >= 0)
6793 {
6794 // previous "if" or "elseif" jumps here
6795 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6796 isn->isn_arg.jump.jump_where = instr->ga_len;
6797 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006798 // Fill in the "end" label in jumps at the end of the blocks.
6799 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006800 cctx->ctx_skip = scope->se_skip_save;
6801
6802 // If all the blocks end in :return and there is an :else then the
6803 // had_return flag is set.
6804 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006805
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006806 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006807 return arg;
6808}
6809
6810/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01006811 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006812 *
6813 * Produces instructions:
6814 * PUSHNR -1
6815 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01006816 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006817 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6818 * - if beyond end, jump to "end"
6819 * - otherwise get item from list and push it
6820 * STORE var Store item in "var"
6821 * ... body ...
6822 * JUMP top Jump back to repeat
6823 * end: DROP Drop the result of "expr"
6824 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006825 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
6826 * UNPACK 2 Split item in 2
6827 * STORE var1 Store item in "var1"
6828 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006829 */
6830 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006831compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006832{
Bram Moolenaar792f7862020-11-23 08:31:18 +01006833 char_u *arg;
6834 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006835 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006836 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006837 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006838 int var_count = 0;
6839 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006840 size_t varlen;
6841 garray_T *instr = &cctx->ctx_instr;
6842 garray_T *stack = &cctx->ctx_type_stack;
6843 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006844 lvar_T *loop_lvar; // loop iteration variable
6845 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006846 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006847 type_T *item_type = &t_any;
6848 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006849
Bram Moolenaar792f7862020-11-23 08:31:18 +01006850 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
6851 if (var_count == 0)
6852 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006853
6854 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006855 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006856 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6857 return NULL;
6858 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006859 {
6860 emsg(_(e_missing_in));
6861 return NULL;
6862 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006863 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006864 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6865 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006866
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006867 scope = new_scope(cctx, FOR_SCOPE);
6868 if (scope == NULL)
6869 return NULL;
6870
Bram Moolenaar792f7862020-11-23 08:31:18 +01006871 // Reserve a variable to store the loop iteration counter and initialize it
6872 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006873 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6874 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006875 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006876 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006877 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006878 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006879 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006880 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006881
6882 // compile "expr", it remains on the stack until "endfor"
6883 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006884 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006885 {
6886 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006887 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006888 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006889 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006890
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006891 // Now that we know the type of "var", check that it is a list, now or at
6892 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006893 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006894 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006895 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006896 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006897 return NULL;
6898 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006899
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006900 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006901 {
6902 if (var_count == 1)
6903 item_type = vartype->tt_member;
6904 else if (vartype->tt_member->tt_type == VAR_LIST
6905 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
6906 item_type = vartype->tt_member->tt_member;
6907 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006908
6909 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006910 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006911 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006912
Bram Moolenaar792f7862020-11-23 08:31:18 +01006913 arg = arg_start;
6914 if (var_count > 1)
6915 {
6916 generate_UNPACK(cctx, var_count, semicolon);
6917 arg = skipwhite(arg + 1); // skip white after '['
6918
6919 // the list item is replaced by a number of items
6920 if (ga_grow(stack, var_count - 1) == FAIL)
6921 {
6922 drop_scope(cctx);
6923 return NULL;
6924 }
6925 --stack->ga_len;
6926 for (idx = 0; idx < var_count; ++idx)
6927 {
6928 ((type_T **)stack->ga_data)[stack->ga_len] =
6929 (semicolon && idx == 0) ? vartype : item_type;
6930 ++stack->ga_len;
6931 }
6932 }
6933
6934 for (idx = 0; idx < var_count; ++idx)
6935 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006936 assign_dest_T dest = dest_local;
6937 int opt_flags = 0;
6938 int vimvaridx = -1;
6939 type_T *type = &t_any;
6940
6941 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006942 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006943 name = vim_strnsave(arg, varlen);
6944 if (name == NULL)
6945 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006946
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006947 // TODO: script var not supported?
6948 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
6949 &vimvaridx, &type, cctx) == FAIL)
6950 goto failed;
6951 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006952 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006953 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
6954 0, 0, type, name) == FAIL)
6955 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006956 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006957 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006958 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01006959 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006960 {
6961 semsg(_(e_variable_already_declared), arg);
6962 goto failed;
6963 }
6964
Bram Moolenaarea870692020-12-02 14:24:30 +01006965 if (STRNCMP(name, "s:", 2) == 0)
6966 {
6967 semsg(_(e_cannot_declare_script_variable_in_function), name);
6968 goto failed;
6969 }
6970
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006971 // Reserve a variable to store "var".
6972 // TODO: check for type
6973 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6974 if (var_lvar == NULL)
6975 // out of memory or used as an argument
6976 goto failed;
6977
6978 if (semicolon && idx == var_count - 1)
6979 var_lvar->lv_type = vartype;
6980 else
6981 var_lvar->lv_type = item_type;
6982 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
6983 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006984
6985 if (*p == ',' || *p == ';')
6986 ++p;
6987 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006988 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006989 }
6990
6991 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006992
6993failed:
6994 vim_free(name);
6995 drop_scope(cctx);
6996 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006997}
6998
6999/*
7000 * compile "endfor"
7001 */
7002 static char_u *
7003compile_endfor(char_u *arg, cctx_T *cctx)
7004{
7005 garray_T *instr = &cctx->ctx_instr;
7006 scope_T *scope = cctx->ctx_scope;
7007 forscope_T *forscope;
7008 isn_T *isn;
7009
7010 if (scope == NULL || scope->se_type != FOR_SCOPE)
7011 {
7012 emsg(_(e_for));
7013 return NULL;
7014 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007015 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007016 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007017 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007018
7019 // At end of ":for" scope jump back to the FOR instruction.
7020 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7021
7022 // Fill in the "end" label in the FOR statement so it can jump here
7023 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7024 isn->isn_arg.forloop.for_end = instr->ga_len;
7025
7026 // Fill in the "end" label any BREAK statements
7027 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
7028
7029 // Below the ":for" scope drop the "expr" list from the stack.
7030 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7031 return NULL;
7032
7033 vim_free(scope);
7034
7035 return arg;
7036}
7037
7038/*
7039 * compile "while expr"
7040 *
7041 * Produces instructions:
7042 * top: EVAL expr Push result of "expr"
7043 * JUMP_IF_FALSE end jump if false
7044 * ... body ...
7045 * JUMP top Jump back to repeat
7046 * end:
7047 *
7048 */
7049 static char_u *
7050compile_while(char_u *arg, cctx_T *cctx)
7051{
7052 char_u *p = arg;
7053 garray_T *instr = &cctx->ctx_instr;
7054 scope_T *scope;
7055
7056 scope = new_scope(cctx, WHILE_SCOPE);
7057 if (scope == NULL)
7058 return NULL;
7059
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007060 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007061
7062 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007063 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007064 return NULL;
7065
Bram Moolenaar13106602020-10-04 16:06:05 +02007066 if (bool_on_stack(cctx) == FAIL)
7067 return FAIL;
7068
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007069 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007070 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007071 JUMP_IF_FALSE, cctx) == FAIL)
7072 return FAIL;
7073
7074 return p;
7075}
7076
7077/*
7078 * compile "endwhile"
7079 */
7080 static char_u *
7081compile_endwhile(char_u *arg, cctx_T *cctx)
7082{
7083 scope_T *scope = cctx->ctx_scope;
7084
7085 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7086 {
7087 emsg(_(e_while));
7088 return NULL;
7089 }
7090 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007091 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007092
7093 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007094 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007095
7096 // Fill in the "end" label in the WHILE statement so it can jump here.
7097 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007098 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007099
7100 vim_free(scope);
7101
7102 return arg;
7103}
7104
7105/*
7106 * compile "continue"
7107 */
7108 static char_u *
7109compile_continue(char_u *arg, cctx_T *cctx)
7110{
7111 scope_T *scope = cctx->ctx_scope;
7112
7113 for (;;)
7114 {
7115 if (scope == NULL)
7116 {
7117 emsg(_(e_continue));
7118 return NULL;
7119 }
7120 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7121 break;
7122 scope = scope->se_outer;
7123 }
7124
7125 // Jump back to the FOR or WHILE instruction.
7126 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007127 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
7128 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007129 return arg;
7130}
7131
7132/*
7133 * compile "break"
7134 */
7135 static char_u *
7136compile_break(char_u *arg, cctx_T *cctx)
7137{
7138 scope_T *scope = cctx->ctx_scope;
7139 endlabel_T **el;
7140
7141 for (;;)
7142 {
7143 if (scope == NULL)
7144 {
7145 emsg(_(e_break));
7146 return NULL;
7147 }
7148 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7149 break;
7150 scope = scope->se_outer;
7151 }
7152
7153 // Jump to the end of the FOR or WHILE loop.
7154 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007155 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007156 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007157 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007158 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7159 return FAIL;
7160
7161 return arg;
7162}
7163
7164/*
7165 * compile "{" start of block
7166 */
7167 static char_u *
7168compile_block(char_u *arg, cctx_T *cctx)
7169{
7170 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7171 return NULL;
7172 return skipwhite(arg + 1);
7173}
7174
7175/*
7176 * compile end of block: drop one scope
7177 */
7178 static void
7179compile_endblock(cctx_T *cctx)
7180{
7181 scope_T *scope = cctx->ctx_scope;
7182
7183 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007184 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007185 vim_free(scope);
7186}
7187
7188/*
7189 * compile "try"
7190 * Creates a new scope for the try-endtry, pointing to the first catch and
7191 * finally.
7192 * Creates another scope for the "try" block itself.
7193 * TRY instruction sets up exception handling at runtime.
7194 *
7195 * "try"
7196 * TRY -> catch1, -> finally push trystack entry
7197 * ... try block
7198 * "throw {exception}"
7199 * EVAL {exception}
7200 * THROW create exception
7201 * ... try block
7202 * " catch {expr}"
7203 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007204 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007205 * EVAL {expr}
7206 * MATCH
7207 * JUMP nomatch -> catch2
7208 * CATCH remove exception
7209 * ... catch block
7210 * " catch"
7211 * JUMP -> finally
7212 * catch2: CATCH remove exception
7213 * ... catch block
7214 * " finally"
7215 * finally:
7216 * ... finally block
7217 * " endtry"
7218 * ENDTRY pop trystack entry, may rethrow
7219 */
7220 static char_u *
7221compile_try(char_u *arg, cctx_T *cctx)
7222{
7223 garray_T *instr = &cctx->ctx_instr;
7224 scope_T *try_scope;
7225 scope_T *scope;
7226
7227 // scope that holds the jumps that go to catch/finally/endtry
7228 try_scope = new_scope(cctx, TRY_SCOPE);
7229 if (try_scope == NULL)
7230 return NULL;
7231
Bram Moolenaar69f70502021-01-01 16:10:46 +01007232 if (cctx->ctx_skip != SKIP_YES)
7233 {
7234 // "catch" is set when the first ":catch" is found.
7235 // "finally" is set when ":finally" or ":endtry" is found
7236 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
7237 if (generate_instr(cctx, ISN_TRY) == NULL)
7238 return NULL;
7239 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007240
7241 // scope for the try block itself
7242 scope = new_scope(cctx, BLOCK_SCOPE);
7243 if (scope == NULL)
7244 return NULL;
7245
7246 return arg;
7247}
7248
7249/*
7250 * compile "catch {expr}"
7251 */
7252 static char_u *
7253compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7254{
7255 scope_T *scope = cctx->ctx_scope;
7256 garray_T *instr = &cctx->ctx_instr;
7257 char_u *p;
7258 isn_T *isn;
7259
7260 // end block scope from :try or :catch
7261 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7262 compile_endblock(cctx);
7263 scope = cctx->ctx_scope;
7264
7265 // Error if not in a :try scope
7266 if (scope == NULL || scope->se_type != TRY_SCOPE)
7267 {
7268 emsg(_(e_catch));
7269 return NULL;
7270 }
7271
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007272 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007273 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007274 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007275 return NULL;
7276 }
7277
Bram Moolenaar69f70502021-01-01 16:10:46 +01007278 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007279 {
Bram Moolenaar69f70502021-01-01 16:10:46 +01007280 // Jump from end of previous block to :finally or :endtry
7281 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7282 JUMP_ALWAYS, cctx) == FAIL)
7283 return NULL;
7284
7285 // End :try or :catch scope: set value in ISN_TRY instruction
7286 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7287 if (isn->isn_arg.try.try_catch == 0)
7288 isn->isn_arg.try.try_catch = instr->ga_len;
7289 if (scope->se_u.se_try.ts_catch_label != 0)
7290 {
7291 // Previous catch without match jumps here
7292 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7293 isn->isn_arg.jump.jump_where = instr->ga_len;
7294 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007295 }
7296
7297 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007298 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007299 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007300 scope->se_u.se_try.ts_caught_all = TRUE;
7301 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007302 }
7303 else
7304 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007305 char_u *end;
7306 char_u *pat;
7307 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007308 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007309 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007310
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007311 // Push v:exception, push {expr} and MATCH
7312 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7313
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007314 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007315 if (*end != *p)
7316 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007317 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007318 vim_free(tofree);
7319 return FAIL;
7320 }
7321 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007322 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007323 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007324 len = (int)(end - tofree);
7325 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007326 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007327 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007328 if (pat == NULL)
7329 return FAIL;
7330 if (generate_PUSHS(cctx, pat) == FAIL)
7331 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007332
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007333 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7334 return NULL;
7335
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007336 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007337 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7338 return NULL;
7339 }
7340
Bram Moolenaar69f70502021-01-01 16:10:46 +01007341 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007342 return NULL;
7343
7344 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7345 return NULL;
7346 return p;
7347}
7348
7349 static char_u *
7350compile_finally(char_u *arg, cctx_T *cctx)
7351{
7352 scope_T *scope = cctx->ctx_scope;
7353 garray_T *instr = &cctx->ctx_instr;
7354 isn_T *isn;
7355
7356 // end block scope from :try or :catch
7357 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7358 compile_endblock(cctx);
7359 scope = cctx->ctx_scope;
7360
7361 // Error if not in a :try scope
7362 if (scope == NULL || scope->se_type != TRY_SCOPE)
7363 {
7364 emsg(_(e_finally));
7365 return NULL;
7366 }
7367
7368 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007369 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007370 if (isn->isn_arg.try.try_finally != 0)
7371 {
7372 emsg(_(e_finally_dup));
7373 return NULL;
7374 }
7375
7376 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007377 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007378
Bram Moolenaar585fea72020-04-02 22:33:21 +02007379 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007380 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007381 {
7382 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007383 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007384 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007385 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007386 }
7387
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007388 // TODO: set index in ts_finally_label jumps
7389
7390 return arg;
7391}
7392
7393 static char_u *
7394compile_endtry(char_u *arg, cctx_T *cctx)
7395{
7396 scope_T *scope = cctx->ctx_scope;
7397 garray_T *instr = &cctx->ctx_instr;
7398 isn_T *isn;
7399
7400 // end block scope from :catch or :finally
7401 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7402 compile_endblock(cctx);
7403 scope = cctx->ctx_scope;
7404
7405 // Error if not in a :try scope
7406 if (scope == NULL || scope->se_type != TRY_SCOPE)
7407 {
7408 if (scope == NULL)
7409 emsg(_(e_no_endtry));
7410 else if (scope->se_type == WHILE_SCOPE)
7411 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007412 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007413 emsg(_(e_endfor));
7414 else
7415 emsg(_(e_endif));
7416 return NULL;
7417 }
7418
Bram Moolenaar69f70502021-01-01 16:10:46 +01007419 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007420 {
Bram Moolenaar69f70502021-01-01 16:10:46 +01007421 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7422 if (isn->isn_arg.try.try_catch == 0
7423 && isn->isn_arg.try.try_finally == 0)
7424 {
7425 emsg(_(e_missing_catch_or_finally));
7426 return NULL;
7427 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007428
Bram Moolenaar69f70502021-01-01 16:10:46 +01007429 // Fill in the "end" label in jumps at the end of the blocks, if not
7430 // done by ":finally".
7431 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007432
Bram Moolenaar69f70502021-01-01 16:10:46 +01007433 // End :catch or :finally scope: set value in ISN_TRY instruction
7434 if (isn->isn_arg.try.try_catch == 0)
7435 isn->isn_arg.try.try_catch = instr->ga_len;
7436 if (isn->isn_arg.try.try_finally == 0)
7437 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007438
Bram Moolenaar69f70502021-01-01 16:10:46 +01007439 if (scope->se_u.se_try.ts_catch_label != 0)
7440 {
7441 // Last catch without match jumps here
7442 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7443 isn->isn_arg.jump.jump_where = instr->ga_len;
7444 }
Bram Moolenaare8593122020-07-18 15:17:02 +02007445 }
7446
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007447 compile_endblock(cctx);
7448
Bram Moolenaar69f70502021-01-01 16:10:46 +01007449 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_ENDTRY) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007450 return NULL;
7451 return arg;
7452}
7453
7454/*
7455 * compile "throw {expr}"
7456 */
7457 static char_u *
7458compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7459{
7460 char_u *p = skipwhite(arg);
7461
Bram Moolenaara5565e42020-05-09 15:44:01 +02007462 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007463 return NULL;
7464 if (may_generate_2STRING(-1, cctx) == FAIL)
7465 return NULL;
7466 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7467 return NULL;
7468
7469 return p;
7470}
7471
7472/*
7473 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007474 * compile "echomsg expr"
7475 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007476 * compile "execute expr"
7477 */
7478 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007479compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007480{
7481 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007482 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007483 int count = 0;
7484
7485 for (;;)
7486 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007487 if (ends_excmd2(prev, p))
7488 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007489 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007490 return NULL;
7491 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007492 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007493 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007494 }
7495
Bram Moolenaare4984292020-12-13 14:19:25 +01007496 if (count > 0)
7497 {
7498 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7499 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7500 else if (cmdidx == CMD_execute)
7501 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7502 else if (cmdidx == CMD_echomsg)
7503 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7504 else
7505 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7506 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007507 return p;
7508}
7509
7510/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007511 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01007512 * instruction to compute it and return OK.
7513 * Otherwise return FAIL, the caller must deal with any range.
7514 */
7515 static int
7516compile_variable_range(exarg_T *eap, cctx_T *cctx)
7517{
7518 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7519 char_u *p = skipdigits(eap->cmd);
7520
7521 if (p == range_end)
7522 return FAIL;
7523 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7524}
7525
7526/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007527 * :put r
7528 * :put ={expr}
7529 */
7530 static char_u *
7531compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7532{
7533 char_u *line = arg;
7534 linenr_T lnum;
7535 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007536 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007537
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007538 eap->regname = *line;
7539
7540 if (eap->regname == '=')
7541 {
7542 char_u *p = line + 1;
7543
7544 if (compile_expr0(&p, cctx) == FAIL)
7545 return NULL;
7546 line = p;
7547 }
7548 else if (eap->regname != NUL)
7549 ++line;
7550
Bram Moolenaar08597872020-12-10 19:43:40 +01007551 if (compile_variable_range(eap, cctx) == OK)
7552 {
7553 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
7554 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007555 else
Bram Moolenaar08597872020-12-10 19:43:40 +01007556 {
7557 // Either no range or a number.
7558 // "errormsg" will not be set because the range is ADDR_LINES.
7559 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01007560 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01007561 return NULL;
7562 if (eap->addr_count == 0)
7563 lnum = -1;
7564 else
7565 lnum = eap->line2;
7566 if (above)
7567 --lnum;
7568 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007569
7570 generate_PUT(cctx, eap->regname, lnum);
7571 return line;
7572}
7573
7574/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007575 * A command that is not compiled, execute with legacy code.
7576 */
7577 static char_u *
7578compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7579{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007580 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007581 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007582 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007583
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007584 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007585 goto theend;
7586
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007587 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007588 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007589 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007590 int usefilter = FALSE;
7591
7592 has_expr = argt & (EX_XFILE | EX_EXPAND);
7593
7594 // If the command can be followed by a bar, find the bar and truncate
7595 // it, so that the following command can be compiled.
7596 // The '|' is overwritten with a NUL, it is put back below.
7597 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7598 && *eap->arg == '!')
7599 // :w !filter or :r !filter or :r! filter
7600 usefilter = TRUE;
7601 if ((argt & EX_TRLBAR) && !usefilter)
7602 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007603 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007604 separate_nextcmd(eap);
7605 if (eap->nextcmd != NULL)
7606 nextcmd = eap->nextcmd;
7607 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01007608 else if (eap->cmdidx == CMD_wincmd)
7609 {
7610 p = eap->arg;
7611 if (*p != NUL)
7612 ++p;
7613 if (*p == 'g' || *p == Ctrl_G)
7614 ++p;
7615 p = skipwhite(p);
7616 if (*p == '|')
7617 {
7618 *p = NUL;
7619 nextcmd = p + 1;
7620 }
7621 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007622 }
7623
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007624 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
7625 {
7626 // expand filename in "syntax include [@group] filename"
7627 has_expr = TRUE;
7628 eap->arg = skipwhite(eap->arg + 7);
7629 if (*eap->arg == '@')
7630 eap->arg = skiptowhite(eap->arg);
7631 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007632
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007633 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
7634 && STRLEN(eap->arg) > 4)
7635 {
7636 int delim = *eap->arg;
7637
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007638 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007639 if (*p == delim)
7640 {
7641 eap->arg = p + 1;
7642 has_expr = TRUE;
7643 }
7644 }
7645
Bram Moolenaarecac5912021-01-05 19:23:28 +01007646 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
7647 {
7648 // TODO: should only expand when appropriate for the command
7649 eap->arg = skiptowhite(eap->arg);
7650 has_expr = TRUE;
7651 }
7652
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007653 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007654 {
7655 int count = 0;
7656 char_u *start = skipwhite(line);
7657
7658 // :cmd xxx`=expr1`yyy`=expr2`zzz
7659 // PUSHS ":cmd xxx"
7660 // eval expr1
7661 // PUSHS "yyy"
7662 // eval expr2
7663 // PUSHS "zzz"
7664 // EXECCONCAT 5
7665 for (;;)
7666 {
7667 if (p > start)
7668 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007669 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007670 ++count;
7671 }
7672 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007673 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007674 return NULL;
7675 may_generate_2STRING(-1, cctx);
7676 ++count;
7677 p = skipwhite(p);
7678 if (*p != '`')
7679 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007680 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007681 return NULL;
7682 }
7683 start = p + 1;
7684
7685 p = (char_u *)strstr((char *)start, "`=");
7686 if (p == NULL)
7687 {
7688 if (*skipwhite(start) != NUL)
7689 {
7690 generate_PUSHS(cctx, vim_strsave(start));
7691 ++count;
7692 }
7693 break;
7694 }
7695 }
7696 generate_EXECCONCAT(cctx, count);
7697 }
7698 else
7699 generate_EXEC(cctx, line);
7700
7701theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007702 if (*nextcmd != NUL)
7703 {
7704 // the parser expects a pointer to the bar, put it back
7705 --nextcmd;
7706 *nextcmd = '|';
7707 }
7708
7709 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007710}
7711
7712/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007713 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007714 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007715 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007716 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007717add_def_function(ufunc_T *ufunc)
7718{
7719 dfunc_T *dfunc;
7720
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007721 if (def_functions.ga_len == 0)
7722 {
7723 // The first position is not used, so that a zero uf_dfunc_idx means it
7724 // wasn't set.
7725 if (ga_grow(&def_functions, 1) == FAIL)
7726 return FAIL;
7727 ++def_functions.ga_len;
7728 }
7729
Bram Moolenaar09689a02020-05-09 22:50:08 +02007730 // Add the function to "def_functions".
7731 if (ga_grow(&def_functions, 1) == FAIL)
7732 return FAIL;
7733 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7734 CLEAR_POINTER(dfunc);
7735 dfunc->df_idx = def_functions.ga_len;
7736 ufunc->uf_dfunc_idx = dfunc->df_idx;
7737 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01007738 dfunc->df_name = vim_strsave(ufunc->uf_name);
7739 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007740 ++def_functions.ga_len;
7741 return OK;
7742}
7743
7744/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007745 * After ex_function() has collected all the function lines: parse and compile
7746 * the lines into instructions.
7747 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007748 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
7749 * the return statement (used for lambda). When uf_ret_type is already set
7750 * then check that it matches.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007751 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007752 * This can be used recursively through compile_lambda(), which may reallocate
7753 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007754 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007755 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007756 int
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007757compile_def_function(ufunc_T *ufunc, int check_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007758{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007759 char_u *line = NULL;
7760 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007761 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007762 cctx_T cctx;
7763 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007764 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007765 int ret = FAIL;
7766 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007767 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007768 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007769 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007770
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007771 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01007772 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007773 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007774 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007775 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7776 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01007777 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007778 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007779 else
7780 {
7781 if (add_def_function(ufunc) == FAIL)
7782 return FAIL;
7783 new_def_function = TRUE;
7784 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007785
Bram Moolenaar985116a2020-07-12 17:31:09 +02007786 ufunc->uf_def_status = UF_COMPILING;
7787
Bram Moolenaara80faa82020-04-12 19:37:17 +02007788 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007789 cctx.ctx_ufunc = ufunc;
7790 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007791 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007792 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7793 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7794 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7795 cctx.ctx_type_list = &ufunc->uf_type_list;
7796 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7797 instr = &cctx.ctx_instr;
7798
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007799 // Set the context to the function, it may be compiled when called from
7800 // another script. Set the script version to the most modern one.
7801 // The line number will be set in next_line_from_context().
7802 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007803 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7804
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007805 // Make sure error messages are OK.
7806 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7807 if (do_estack_push)
7808 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007809 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007810
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007811 if (ufunc->uf_def_args.ga_len > 0)
7812 {
7813 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007814 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007815 int i;
7816 char_u *arg;
7817 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007818 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007819
7820 // Produce instructions for the default values of optional arguments.
7821 // Store the instruction index in uf_def_arg_idx[] so that we know
7822 // where to start when the function is called, depending on the number
7823 // of arguments.
7824 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7825 if (ufunc->uf_def_arg_idx == NULL)
7826 goto erret;
7827 for (i = 0; i < count; ++i)
7828 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007829 garray_T *stack = &cctx.ctx_type_stack;
7830 type_T *val_type;
7831 int arg_idx = first_def_arg + i;
7832
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007833 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7834 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007835 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007836 goto erret;
7837
7838 // If no type specified use the type of the default value.
7839 // Otherwise check that the default value type matches the
7840 // specified type.
7841 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7842 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007843 {
7844 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007845 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007846 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007847 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7848 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007849 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007850
7851 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007852 goto erret;
7853 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007854 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007855
7856 if (did_set_arg_type)
7857 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007858 }
7859
7860 /*
7861 * Loop over all the lines of the function and generate instructions.
7862 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007863 for (;;)
7864 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007865 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007866 int starts_with_colon = FALSE;
7867 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007868 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007869
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007870 // Bail out on the first error to avoid a flood of errors and report
7871 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007872 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007873 goto erret;
7874
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007875 if (line != NULL && *line == '|')
7876 // the line continues after a '|'
7877 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007878 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007879 && !(*line == '#' && (line == cctx.ctx_line_start
7880 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007881 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007882 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007883 goto erret;
7884 }
7885 else
7886 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007887 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007888 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007889 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007890 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007891 }
7892
Bram Moolenaara80faa82020-04-12 19:37:17 +02007893 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007894 ea.cmdlinep = &line;
7895 ea.cmd = skipwhite(line);
7896
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007897 // Some things can be recognized by the first character.
7898 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007899 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007900 case '#':
Bram Moolenaare0de1712020-12-02 17:36:54 +01007901 // "#" starts a comment
7902 line = (char_u *)"";
7903 continue;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007904
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007905 case '}':
7906 {
7907 // "}" ends a block scope
7908 scopetype_T stype = cctx.ctx_scope == NULL
7909 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007910
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007911 if (stype == BLOCK_SCOPE)
7912 {
7913 compile_endblock(&cctx);
7914 line = ea.cmd;
7915 }
7916 else
7917 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007918 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007919 goto erret;
7920 }
7921 if (line != NULL)
7922 line = skipwhite(ea.cmd + 1);
7923 continue;
7924 }
7925
7926 case '{':
7927 // "{" starts a block scope
7928 // "{'a': 1}->func() is something else
7929 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7930 {
7931 line = compile_block(ea.cmd, &cctx);
7932 continue;
7933 }
7934 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007935 }
7936
7937 /*
7938 * COMMAND MODIFIERS
7939 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007940 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007941 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7942 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007943 {
7944 if (errormsg != NULL)
7945 goto erret;
7946 // empty line or comment
7947 line = (char_u *)"";
7948 continue;
7949 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007950 generate_cmdmods(&cctx, &local_cmdmod);
7951 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007952
Bram Moolenaare88c8e82020-11-01 17:03:37 +01007953 // Check if there was a colon after the last command modifier or before
7954 // the current position.
7955 for (p = ea.cmd; p >= line; --p)
7956 {
7957 if (*p == ':')
7958 starts_with_colon = TRUE;
7959 if (p < ea.cmd && !VIM_ISWHITE(*p))
7960 break;
7961 }
7962
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007963 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007964 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007965 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007966 {
7967 if (*ea.cmd == '(')
7968 // not for "call()"
7969 ea.cmd = p;
7970 else
7971 ea.cmd = skipwhite(ea.cmd);
7972 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007973
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007974 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007975 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01007976 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007977
Bram Moolenaar17126b12021-01-07 22:03:02 +01007978 // Check for assignment after command modifiers.
7979 assign = may_compile_assignment(&ea, &line, &cctx);
7980 if (assign == OK)
7981 goto nextline;
7982 if (assign == FAIL)
7983 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007984 }
7985
7986 /*
7987 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007988 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007989 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007990 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007991 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007992 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007993 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007994 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007995 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007996 if (!starts_with_colon)
7997 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01007998 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007999 goto erret;
8000 }
8001 if (ends_excmd2(line, ea.cmd))
8002 {
8003 // A range without a command: jump to the line.
8004 // TODO: compile to a more efficient command, possibly
8005 // calling parse_cmd_address().
8006 ea.cmdidx = CMD_SIZE;
8007 line = compile_exec(line, &ea, &cctx);
8008 goto nextline;
8009 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008010 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008011 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008012 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +01008013 : (int (*)(char_u *, size_t, void *, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008014 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008015
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008016 if (p == NULL)
8017 {
8018 if (cctx.ctx_skip != SKIP_YES)
8019 emsg(_(e_ambiguous_use_of_user_defined_command));
8020 goto erret;
8021 }
8022
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008023 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8024 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008025 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008026 {
8027 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008028 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008029 }
8030
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008031 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008032 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008033 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008034 // CMD_var cannot happen, compile_assignment() above would be
8035 // used. Most likely an assignment to a non-existing variable.
8036 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008037 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008038 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008039 }
8040
Bram Moolenaar3988f642020-08-27 22:43:03 +02008041 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008042 && ea.cmdidx != CMD_elseif
8043 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008044 && ea.cmdidx != CMD_endif
8045 && ea.cmdidx != CMD_endfor
8046 && ea.cmdidx != CMD_endwhile
8047 && ea.cmdidx != CMD_catch
8048 && ea.cmdidx != CMD_finally
8049 && ea.cmdidx != CMD_endtry)
8050 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008051 emsg(_(e_unreachable_code_after_return));
8052 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008053 }
8054
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008055 p = skipwhite(p);
8056 if (ea.cmdidx != CMD_SIZE
8057 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8058 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008059 if (ea.cmdidx >= 0)
8060 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008061 if ((ea.argt & EX_BANG) && *p == '!')
8062 {
8063 ea.forceit = TRUE;
8064 p = skipwhite(p + 1);
8065 }
8066 }
8067
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008068 switch (ea.cmdidx)
8069 {
8070 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008071 ea.arg = p;
8072 line = compile_nested_function(&ea, &cctx);
8073 break;
8074
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008075 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008076 // TODO: should we allow this, e.g. to declare a global
8077 // function?
8078 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008079 goto erret;
8080
8081 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008082 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008083 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008084 break;
8085
8086 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008087 emsg(_(e_cannot_use_let_in_vim9_script));
8088 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008089 case CMD_var:
8090 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008091 case CMD_const:
8092 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008093 if (line == p)
8094 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008095 break;
8096
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008097 case CMD_unlet:
8098 case CMD_unlockvar:
8099 case CMD_lockvar:
8100 line = compile_unletlock(p, &ea, &cctx);
8101 break;
8102
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008103 case CMD_import:
8104 line = compile_import(p, &cctx);
8105 break;
8106
8107 case CMD_if:
8108 line = compile_if(p, &cctx);
8109 break;
8110 case CMD_elseif:
8111 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008112 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008113 break;
8114 case CMD_else:
8115 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008116 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008117 break;
8118 case CMD_endif:
8119 line = compile_endif(p, &cctx);
8120 break;
8121
8122 case CMD_while:
8123 line = compile_while(p, &cctx);
8124 break;
8125 case CMD_endwhile:
8126 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008127 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008128 break;
8129
8130 case CMD_for:
8131 line = compile_for(p, &cctx);
8132 break;
8133 case CMD_endfor:
8134 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008135 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008136 break;
8137 case CMD_continue:
8138 line = compile_continue(p, &cctx);
8139 break;
8140 case CMD_break:
8141 line = compile_break(p, &cctx);
8142 break;
8143
8144 case CMD_try:
8145 line = compile_try(p, &cctx);
8146 break;
8147 case CMD_catch:
8148 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008149 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008150 break;
8151 case CMD_finally:
8152 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008153 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008154 break;
8155 case CMD_endtry:
8156 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008157 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008158 break;
8159 case CMD_throw:
8160 line = compile_throw(p, &cctx);
8161 break;
8162
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008163 case CMD_eval:
8164 if (compile_expr0(&p, &cctx) == FAIL)
8165 goto erret;
8166
Bram Moolenaar3988f642020-08-27 22:43:03 +02008167 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008168 generate_instr_drop(&cctx, ISN_DROP, 1);
8169
8170 line = skipwhite(p);
8171 break;
8172
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008173 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008174 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008175 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008176 case CMD_echomsg:
8177 case CMD_echoerr:
8178 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008179 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008180
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008181 case CMD_put:
8182 ea.cmd = cmd;
8183 line = compile_put(p, &ea, &cctx);
8184 break;
8185
Bram Moolenaar3988f642020-08-27 22:43:03 +02008186 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008187
Bram Moolenaarae616492020-07-28 20:07:27 +02008188 case CMD_append:
8189 case CMD_change:
8190 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008191 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008192 case CMD_xit:
8193 not_in_vim9(&ea);
8194 goto erret;
8195
Bram Moolenaar002262f2020-07-08 17:47:57 +02008196 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008197 if (cctx.ctx_skip != SKIP_YES)
8198 {
8199 semsg(_(e_invalid_command_str), ea.cmd);
8200 goto erret;
8201 }
8202 // We don't check for a next command here.
8203 line = (char_u *)"";
8204 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008205
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008206 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008207 if (cctx.ctx_skip == SKIP_YES)
8208 {
8209 // We don't check for a next command here.
8210 line = (char_u *)"";
8211 }
8212 else
8213 {
8214 // Not recognized, execute with do_cmdline_cmd().
8215 ea.arg = p;
8216 line = compile_exec(line, &ea, &cctx);
8217 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008218 break;
8219 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008220nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008221 if (line == NULL)
8222 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008223 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008224
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008225 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008226 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008227
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008228 if (cctx.ctx_type_stack.ga_len < 0)
8229 {
8230 iemsg("Type stack underflow");
8231 goto erret;
8232 }
8233 }
8234
8235 if (cctx.ctx_scope != NULL)
8236 {
8237 if (cctx.ctx_scope->se_type == IF_SCOPE)
8238 emsg(_(e_endif));
8239 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8240 emsg(_(e_endwhile));
8241 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8242 emsg(_(e_endfor));
8243 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008244 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008245 goto erret;
8246 }
8247
Bram Moolenaarefd88552020-06-18 20:50:10 +02008248 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008249 {
8250 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8251 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008252 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008253 goto erret;
8254 }
8255
8256 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008257 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008258 }
8259
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008260 {
8261 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8262 + ufunc->uf_dfunc_idx;
8263 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008264 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008265 dfunc->df_instr = instr->ga_data;
8266 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008267 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008268 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008269 if (cctx.ctx_outer_used)
8270 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008271 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008272 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008273
8274 ret = OK;
8275
8276erret:
8277 if (ret == FAIL)
8278 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008279 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008280 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8281 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008282
8283 for (idx = 0; idx < instr->ga_len; ++idx)
8284 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008285 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008286 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008287
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008288 // If using the last entry in the table and it was added above, we
8289 // might as well remove it.
8290 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008291 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008292 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008293 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008294 ufunc->uf_dfunc_idx = 0;
8295 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008296 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008297
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008298 while (cctx.ctx_scope != NULL)
8299 drop_scope(&cctx);
8300
Bram Moolenaar20431c92020-03-20 18:39:46 +01008301 // Don't execute this function body.
8302 ga_clear_strings(&ufunc->uf_lines);
8303
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008304 if (errormsg != NULL)
8305 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008306 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008307 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008308 }
8309
8310 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008311 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008312 if (do_estack_push)
8313 estack_pop();
8314
Bram Moolenaar20431c92020-03-20 18:39:46 +01008315 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008316 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008317 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008318 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008319}
8320
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008321 void
8322set_function_type(ufunc_T *ufunc)
8323{
8324 int varargs = ufunc->uf_va_name != NULL;
8325 int argcount = ufunc->uf_args.ga_len;
8326
8327 // Create a type for the function, with the return type and any
8328 // argument types.
8329 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8330 // The type is included in "tt_args".
8331 if (argcount > 0 || varargs)
8332 {
8333 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8334 argcount, &ufunc->uf_type_list);
8335 // Add argument types to the function type.
8336 if (func_type_add_arg_types(ufunc->uf_func_type,
8337 argcount + varargs,
8338 &ufunc->uf_type_list) == FAIL)
8339 return;
8340 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8341 ufunc->uf_func_type->tt_min_argcount =
8342 argcount - ufunc->uf_def_args.ga_len;
8343 if (ufunc->uf_arg_types == NULL)
8344 {
8345 int i;
8346
8347 // lambda does not have argument types.
8348 for (i = 0; i < argcount; ++i)
8349 ufunc->uf_func_type->tt_args[i] = &t_any;
8350 }
8351 else
8352 mch_memmove(ufunc->uf_func_type->tt_args,
8353 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8354 if (varargs)
8355 {
8356 ufunc->uf_func_type->tt_args[argcount] =
8357 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8358 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8359 }
8360 }
8361 else
8362 // No arguments, can use a predefined type.
8363 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8364 argcount, &ufunc->uf_type_list);
8365}
8366
8367
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008368/*
8369 * Delete an instruction, free what it contains.
8370 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008371 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008372delete_instr(isn_T *isn)
8373{
8374 switch (isn->isn_type)
8375 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008376 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008377 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008378 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008379 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008380 case ISN_LOADENV:
8381 case ISN_LOADG:
8382 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008383 case ISN_LOADT:
8384 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008385 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008386 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008387 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008388 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008389 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008390 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008391 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008392 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008393 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008394 case ISN_STOREW:
8395 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008396 vim_free(isn->isn_arg.string);
8397 break;
8398
8399 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008400 case ISN_STORES:
8401 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008402 break;
8403
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008404 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008405 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008406 vim_free(isn->isn_arg.unlet.ul_name);
8407 break;
8408
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008409 case ISN_STOREOPT:
8410 vim_free(isn->isn_arg.storeopt.so_name);
8411 break;
8412
8413 case ISN_PUSHBLOB: // push blob isn_arg.blob
8414 blob_unref(isn->isn_arg.blob);
8415 break;
8416
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008417 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008418#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008419 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008420#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008421 break;
8422
8423 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008424#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008425 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008426#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008427 break;
8428
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008429 case ISN_UCALL:
8430 vim_free(isn->isn_arg.ufunc.cuf_name);
8431 break;
8432
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008433 case ISN_FUNCREF:
8434 {
8435 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8436 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01008437 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008438
Bram Moolenaar077a4232020-12-22 18:33:27 +01008439 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
8440 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02008441 }
8442 break;
8443
8444 case ISN_DCALL:
8445 {
8446 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8447 + isn->isn_arg.dfunc.cdf_idx;
8448
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008449 if (dfunc->df_ufunc != NULL
8450 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008451 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008452 }
8453 break;
8454
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008455 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008456 {
8457 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8458 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8459
8460 if (ufunc != NULL)
8461 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008462 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02008463 func_ptr_unref(ufunc);
8464 }
8465
8466 vim_free(lambda);
8467 vim_free(isn->isn_arg.newfunc.nf_global);
8468 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008469 break;
8470
Bram Moolenaar5e654232020-09-16 15:22:00 +02008471 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01008472 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02008473 free_type(isn->isn_arg.type.ct_type);
8474 break;
8475
Bram Moolenaar02194d22020-10-24 23:08:38 +02008476 case ISN_CMDMOD:
8477 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8478 ->cmod_filter_regmatch.regprog);
8479 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8480 break;
8481
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008482 case ISN_LOADSCRIPT:
8483 case ISN_STORESCRIPT:
8484 vim_free(isn->isn_arg.script.scriptref);
8485 break;
8486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008487 case ISN_2BOOL:
8488 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008489 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008490 case ISN_ADDBLOB:
8491 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008492 case ISN_ANYINDEX:
8493 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008494 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008495 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008496 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008497 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008498 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008499 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008500 case ISN_COMPAREANY:
8501 case ISN_COMPAREBLOB:
8502 case ISN_COMPAREBOOL:
8503 case ISN_COMPAREDICT:
8504 case ISN_COMPAREFLOAT:
8505 case ISN_COMPAREFUNC:
8506 case ISN_COMPARELIST:
8507 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008508 case ISN_COMPARESPECIAL:
8509 case ISN_COMPARESTRING:
8510 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008511 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008512 case ISN_DROP:
8513 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008514 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008515 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008516 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008517 case ISN_EXECCONCAT:
8518 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008519 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008520 case ISN_GETITEM:
8521 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008522 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008523 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008524 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008525 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008526 case ISN_LOADBDICT:
8527 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008528 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008529 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008530 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008531 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008532 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008533 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008534 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008535 case ISN_NEGATENR:
8536 case ISN_NEWDICT:
8537 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008538 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008539 case ISN_OPFLOAT:
8540 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008541 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008542 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008543 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008544 case ISN_PUSHF:
8545 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008546 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008547 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008548 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01008549 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008550 case ISN_SHUFFLE:
8551 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008552 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01008553 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008554 case ISN_STORENR:
8555 case ISN_STOREOUTER:
8556 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008557 case ISN_STOREV:
8558 case ISN_STRINDEX:
8559 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008560 case ISN_THROW:
8561 case ISN_TRY:
Bram Moolenaar752fc692021-01-04 21:57:11 +01008562 case ISN_UNLETINDEX:
Bram Moolenaar792f7862020-11-23 08:31:18 +01008563 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008564 // nothing allocated
8565 break;
8566 }
8567}
8568
8569/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008570 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01008571 */
8572 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008573delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008574{
8575 int idx;
8576
8577 ga_clear(&dfunc->df_def_args_isn);
8578
8579 if (dfunc->df_instr != NULL)
8580 {
8581 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
8582 delete_instr(dfunc->df_instr + idx);
8583 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008584 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008585 }
8586
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008587 if (mark_deleted)
8588 dfunc->df_deleted = TRUE;
8589 if (dfunc->df_ufunc != NULL)
8590 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008591}
8592
8593/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008594 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008595 * function, unless another user function still uses it.
8596 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008597 */
8598 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008599unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008600{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008601 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008602 {
8603 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8604 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008605
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008606 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008607 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008608 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008609 ufunc->uf_dfunc_idx = 0;
8610 if (dfunc->df_ufunc == ufunc)
8611 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008612 }
8613}
8614
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008615/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008616 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008617 */
8618 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008619link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008620{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008621 if (ufunc->uf_dfunc_idx > 0)
8622 {
8623 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8624 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008625
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008626 ++dfunc->df_refcount;
8627 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008628}
8629
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008630#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008631/*
8632 * Free all functions defined with ":def".
8633 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008634 void
8635free_def_functions(void)
8636{
Bram Moolenaar20431c92020-03-20 18:39:46 +01008637 int idx;
8638
8639 for (idx = 0; idx < def_functions.ga_len; ++idx)
8640 {
8641 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
8642
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008643 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008644 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008645 }
8646
8647 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008648}
8649#endif
8650
8651
8652#endif // FEAT_EVAL