blob: 0064bd20550ca04345bc19ceabc83d95533b7cd3 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
Bram Moolenaarab360522021-01-10 14:02:28 +0100111 int lv_from_outer; // nesting level, using ctx_outer scope
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200129 int ctx_has_closure; // set to one if a closures was created in
130 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200144
Bram Moolenaar02194d22020-10-24 23:08:38 +0200145 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100146};
147
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100148static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149
150/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100151 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100152 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100153 * If "lvar" is NULL only check if the variable can be found.
154 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100155 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100156 static int
157lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158{
159 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100160 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100161
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100162 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100163 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200164
165 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100166 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
167 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100168 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
169 if (STRNCMP(name, lvp->lv_name, len) == 0
170 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200171 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100172 if (lvar != NULL)
173 {
174 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100175 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100176 }
177 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200180
181 // Find local in outer function scope.
182 if (cctx->ctx_outer != NULL)
183 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100184 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200185 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100186 if (lvar != NULL)
187 {
188 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100189 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100190 }
191 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200192 }
193 }
194
Bram Moolenaar709664c2020-12-12 14:33:41 +0100195 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196}
197
198/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200199 * Lookup an argument in the current function and an enclosing function.
200 * Returns the argument index in "idxp"
201 * Returns the argument type in "type"
202 * Sets "gen_load_outer" to TRUE if found in outer scope.
203 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100204 */
205 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200206arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200207 char_u *name,
208 size_t len,
209 int *idxp,
210 type_T **type,
211 int *gen_load_outer,
212 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213{
214 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200215 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100217 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200218 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
220 {
221 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
222
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200223 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
224 {
225 if (idxp != NULL)
226 {
227 // Arguments are located above the frame pointer. One further
228 // if there is a vararg argument
229 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
230 + STACK_FRAME_SIZE)
231 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
232
233 if (cctx->ctx_ufunc->uf_arg_types != NULL)
234 *type = cctx->ctx_ufunc->uf_arg_types[idx];
235 else
236 *type = &t_any;
237 }
238 return OK;
239 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100240 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100241
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200242 va_name = cctx->ctx_ufunc->uf_va_name;
243 if (va_name != NULL
244 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
245 {
246 if (idxp != NULL)
247 {
248 // varargs is always the last argument
249 *idxp = -STACK_FRAME_SIZE - 1;
250 *type = cctx->ctx_ufunc->uf_va_type;
251 }
252 return OK;
253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200255 if (cctx->ctx_outer != NULL)
256 {
257 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200258 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200259 == OK)
260 {
Bram Moolenaarab360522021-01-10 14:02:28 +0100261 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200262 return OK;
263 }
264 }
265
266 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100267}
268
269/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200270 * Lookup a script-local variable in the current script, possibly defined in a
271 * block that contains the function "cctx->ctx_ufunc".
272 * "cctx" is NULL at the script level.
273 * if "len" is <= 0 "name" must be NUL terminated.
274 * Return NULL when not found.
275 */
276 static sallvar_T *
277find_script_var(char_u *name, size_t len, cctx_T *cctx)
278{
279 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
280 hashitem_T *hi;
281 int cc;
282 sallvar_T *sav;
283 ufunc_T *ufunc;
284
285 // Find the list of all script variables with the right name.
286 if (len > 0)
287 {
288 cc = name[len];
289 name[len] = NUL;
290 }
291 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
292 if (len > 0)
293 name[len] = cc;
294 if (HASHITEM_EMPTY(hi))
295 return NULL;
296
297 sav = HI2SAV(hi);
298 if (sav->sav_block_id == 0 || cctx == NULL)
299 // variable defined in the script scope or not in a function.
300 return sav;
301
302 // Go over the variables with this name and find one that was visible
303 // from the function.
304 ufunc = cctx->ctx_ufunc;
305 while (sav != NULL)
306 {
307 int idx;
308
309 // Go over the blocks that this function was defined in. If the
310 // variable block ID matches it was visible to the function.
311 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
312 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
313 return sav;
314 sav = sav->sav_next;
315 }
316
317 return NULL;
318}
319
320/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100321 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200322 */
323 static int
324script_is_vim9()
325{
326 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
327}
328
329/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200330 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200331 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
332 * without "s:".
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200333 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100334 * Returns OK or FAIL.
335 */
336 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200337script_var_exists(char_u *name, size_t len, int vim9script, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100338{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200339 int is_vim9_script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200341 if (current_sctx.sc_sid <= 0)
342 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200343 is_vim9_script = script_is_vim9();
344 if (vim9script && !is_vim9_script)
Bram Moolenaar53900992020-08-22 19:02:02 +0200345 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200346 if (is_vim9_script)
347 {
348 // Check script variables that were visible where the function was
349 // defined.
350 if (find_script_var(name, len, cctx) != NULL)
351 return OK;
352 }
353 else
354 {
355 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
356 dictitem_T *di;
357 int cc;
358
359 // Check script variables that are currently visible
360 cc = name[len];
361 name[len] = NUL;
362 di = find_var_in_ht(ht, 0, name, TRUE);
363 name[len] = cc;
364 if (di != NULL)
365 return OK;
366 }
367
368 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100369}
370
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100371/*
372 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200373 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200374 * Does not check the global namespace.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100375 * Return FAIL and give an error if it defined.
376 */
377 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200378check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100379{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200380 int c = p[len];
381 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200382
383 p[len] = NUL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200384 if (script_var_exists(p, len, FALSE, cctx) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100385 || (cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100386 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200387 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200388 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200389 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100390 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200391 // A local or script-local function can shadow a global function.
392 if (ufunc == NULL || !func_is_global(ufunc)
393 || (p[0] == 'g' && p[1] == ':'))
394 {
395 p[len] = c;
396 semsg(_(e_name_already_defined_str), p);
397 return FAIL;
398 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100399 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200400 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100401 return OK;
402}
403
Bram Moolenaar65b95452020-07-19 14:03:09 +0200404
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100405/////////////////////////////////////////////////////////////////////
406// Following generate_ functions expect the caller to call ga_grow().
407
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200408#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
409#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100411/*
412 * Generate an instruction without arguments.
413 * Returns a pointer to the new instruction, NULL if failed.
414 */
415 static isn_T *
416generate_instr(cctx_T *cctx, isntype_T isn_type)
417{
418 garray_T *instr = &cctx->ctx_instr;
419 isn_T *isn;
420
Bram Moolenaar080457c2020-03-03 21:53:32 +0100421 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100422 if (ga_grow(instr, 1) == FAIL)
423 return NULL;
424 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
425 isn->isn_type = isn_type;
426 isn->isn_lnum = cctx->ctx_lnum + 1;
427 ++instr->ga_len;
428
429 return isn;
430}
431
432/*
433 * Generate an instruction without arguments.
434 * "drop" will be removed from the stack.
435 * Returns a pointer to the new instruction, NULL if failed.
436 */
437 static isn_T *
438generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
439{
440 garray_T *stack = &cctx->ctx_type_stack;
441
Bram Moolenaar080457c2020-03-03 21:53:32 +0100442 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100443 stack->ga_len -= drop;
444 return generate_instr(cctx, isn_type);
445}
446
447/*
448 * Generate instruction "isn_type" and put "type" on the type stack.
449 */
450 static isn_T *
451generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
452{
453 isn_T *isn;
454 garray_T *stack = &cctx->ctx_type_stack;
455
456 if ((isn = generate_instr(cctx, isn_type)) == NULL)
457 return NULL;
458
459 if (ga_grow(stack, 1) == FAIL)
460 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200461 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462 ++stack->ga_len;
463
464 return isn;
465}
466
467/*
468 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200469 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100470 */
471 static int
472may_generate_2STRING(int offset, cctx_T *cctx)
473{
474 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200475 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100476 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100477 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100478
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100479 RETURN_OK_IF_SKIP(cctx);
480 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200481 switch ((*type)->tt_type)
482 {
483 // nothing to be done
484 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100485
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200486 // conversion possible
487 case VAR_SPECIAL:
488 case VAR_BOOL:
489 case VAR_NUMBER:
490 case VAR_FLOAT:
491 break;
492
493 // conversion possible (with runtime check)
494 case VAR_ANY:
495 case VAR_UNKNOWN:
496 isntype = ISN_2STRING_ANY;
497 break;
498
499 // conversion not possible
500 case VAR_VOID:
501 case VAR_BLOB:
502 case VAR_FUNC:
503 case VAR_PARTIAL:
504 case VAR_LIST:
505 case VAR_DICT:
506 case VAR_JOB:
507 case VAR_CHANNEL:
508 to_string_error((*type)->tt_type);
509 return FAIL;
510 }
511
512 *type = &t_string;
513 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100514 return FAIL;
515 isn->isn_arg.number = offset;
516
517 return OK;
518}
519
520 static int
521check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
522{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200523 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200525 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100526 {
527 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200528 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200530 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100531 return FAIL;
532 }
533 return OK;
534}
535
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200536 static int
537generate_add_instr(
538 cctx_T *cctx,
539 vartype_T vartype,
540 type_T *type1,
541 type_T *type2)
542{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100543 garray_T *stack = &cctx->ctx_type_stack;
544 isn_T *isn = generate_instr_drop(cctx,
545 vartype == VAR_NUMBER ? ISN_OPNR
546 : vartype == VAR_LIST ? ISN_ADDLIST
547 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200548#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100549 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200550#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100551 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200552
553 if (vartype != VAR_LIST && vartype != VAR_BLOB
554 && type1->tt_type != VAR_ANY
555 && type2->tt_type != VAR_ANY
556 && check_number_or_float(
557 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
558 return FAIL;
559
560 if (isn != NULL)
561 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100562
563 // When concatenating two lists with different member types the member type
564 // becomes "any".
565 if (vartype == VAR_LIST
566 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
567 && type1->tt_member != type2->tt_member)
568 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
569
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200570 return isn == NULL ? FAIL : OK;
571}
572
573/*
574 * Get the type to use for an instruction for an operation on "type1" and
575 * "type2". If they are matching use a type-specific instruction. Otherwise
576 * fall back to runtime type checking.
577 */
578 static vartype_T
579operator_type(type_T *type1, type_T *type2)
580{
581 if (type1->tt_type == type2->tt_type
582 && (type1->tt_type == VAR_NUMBER
583 || type1->tt_type == VAR_LIST
584#ifdef FEAT_FLOAT
585 || type1->tt_type == VAR_FLOAT
586#endif
587 || type1->tt_type == VAR_BLOB))
588 return type1->tt_type;
589 return VAR_ANY;
590}
591
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100592/*
593 * Generate an instruction with two arguments. The instruction depends on the
594 * type of the arguments.
595 */
596 static int
597generate_two_op(cctx_T *cctx, char_u *op)
598{
599 garray_T *stack = &cctx->ctx_type_stack;
600 type_T *type1;
601 type_T *type2;
602 vartype_T vartype;
603 isn_T *isn;
604
Bram Moolenaar080457c2020-03-03 21:53:32 +0100605 RETURN_OK_IF_SKIP(cctx);
606
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200607 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100608 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
609 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200610 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100611
612 switch (*op)
613 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200614 case '+':
615 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100616 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100617 break;
618
619 case '-':
620 case '*':
621 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
622 op) == FAIL)
623 return FAIL;
624 if (vartype == VAR_NUMBER)
625 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
626#ifdef FEAT_FLOAT
627 else if (vartype == VAR_FLOAT)
628 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
629#endif
630 else
631 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
632 if (isn != NULL)
633 isn->isn_arg.op.op_type = *op == '*'
634 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
635 break;
636
Bram Moolenaar4c683752020-04-05 21:38:23 +0200637 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100638 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200639 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100640 && type2->tt_type != VAR_NUMBER))
641 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200642 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100643 return FAIL;
644 }
645 isn = generate_instr_drop(cctx,
646 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
647 if (isn != NULL)
648 isn->isn_arg.op.op_type = EXPR_REM;
649 break;
650 }
651
652 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200653 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100654 {
655 type_T *type = &t_any;
656
657#ifdef FEAT_FLOAT
658 // float+number and number+float results in float
659 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
660 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
661 type = &t_float;
662#endif
663 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
664 }
665
666 return OK;
667}
668
669/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200670 * Get the instruction to use for comparing "type1" with "type2"
671 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100672 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200673 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100674get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675{
676 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100677
Bram Moolenaar4c683752020-04-05 21:38:23 +0200678 if (type1 == VAR_UNKNOWN)
679 type1 = VAR_ANY;
680 if (type2 == VAR_UNKNOWN)
681 type2 = VAR_ANY;
682
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 if (type1 == type2)
684 {
685 switch (type1)
686 {
687 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
688 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
689 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
690 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
691 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
692 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
693 case VAR_LIST: isntype = ISN_COMPARELIST; break;
694 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
695 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100696 default: isntype = ISN_COMPAREANY; break;
697 }
698 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200699 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100700 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
701 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
702 isntype = ISN_COMPAREANY;
703
Bram Moolenaar657137c2021-01-09 15:45:23 +0100704 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100705 && (isntype == ISN_COMPAREBOOL
706 || isntype == ISN_COMPARESPECIAL
707 || isntype == ISN_COMPARENR
708 || isntype == ISN_COMPAREFLOAT))
709 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200710 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100711 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200712 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100713 }
714 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100715 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100716 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
717 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100718 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
719 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100720 && (type1 == VAR_BLOB || type2 == VAR_BLOB
721 || type1 == VAR_LIST || type2 == VAR_LIST))))
722 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200723 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200725 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100726 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200727 return isntype;
728}
729
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200730 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100731check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200732{
733 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
734 return FAIL;
735 return OK;
736}
737
Bram Moolenaara5565e42020-05-09 15:44:01 +0200738/*
739 * Generate an ISN_COMPARE* instruction with a boolean result.
740 */
741 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100742generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200743{
744 isntype_T isntype;
745 isn_T *isn;
746 garray_T *stack = &cctx->ctx_type_stack;
747 vartype_T type1;
748 vartype_T type2;
749
750 RETURN_OK_IF_SKIP(cctx);
751
752 // Get the known type of the two items on the stack. If they are matching
753 // use a type-specific instruction. Otherwise fall back to runtime type
754 // checking.
755 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
756 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100757 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200758 if (isntype == ISN_DROP)
759 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100760
761 if ((isn = generate_instr(cctx, isntype)) == NULL)
762 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100763 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764 isn->isn_arg.op.op_ic = ic;
765
766 // takes two arguments, puts one bool back
767 if (stack->ga_len >= 2)
768 {
769 --stack->ga_len;
770 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
771 }
772
773 return OK;
774}
775
776/*
777 * Generate an ISN_2BOOL instruction.
778 */
779 static int
780generate_2BOOL(cctx_T *cctx, int invert)
781{
782 isn_T *isn;
783 garray_T *stack = &cctx->ctx_type_stack;
784
Bram Moolenaar080457c2020-03-03 21:53:32 +0100785 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100786 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
787 return FAIL;
788 isn->isn_arg.number = invert;
789
790 // type becomes bool
791 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
792
793 return OK;
794}
795
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200796/*
797 * Generate an ISN_COND2BOOL instruction.
798 */
799 static int
800generate_COND2BOOL(cctx_T *cctx)
801{
802 isn_T *isn;
803 garray_T *stack = &cctx->ctx_type_stack;
804
805 RETURN_OK_IF_SKIP(cctx);
806 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
807 return FAIL;
808
809 // type becomes bool
810 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
811
812 return OK;
813}
814
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100815 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200816generate_TYPECHECK(
817 cctx_T *cctx,
818 type_T *expected,
819 int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100820{
821 isn_T *isn;
822 garray_T *stack = &cctx->ctx_type_stack;
823
Bram Moolenaar080457c2020-03-03 21:53:32 +0100824 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100825 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
826 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200827 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100828 isn->isn_arg.type.ct_off = offset;
829
Bram Moolenaar5e654232020-09-16 15:22:00 +0200830 // type becomes expected
831 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100832
833 return OK;
834}
835
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100836 static int
837generate_SETTYPE(
838 cctx_T *cctx,
839 type_T *expected)
840{
841 isn_T *isn;
842
843 RETURN_OK_IF_SKIP(cctx);
844 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
845 return FAIL;
846 isn->isn_arg.type.ct_type = alloc_type(expected);
847 return OK;
848}
849
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100850/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200851 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
852 * used. Return FALSE if the types will never match.
853 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100854 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200855use_typecheck(type_T *actual, type_T *expected)
856{
857 if (actual->tt_type == VAR_ANY
858 || actual->tt_type == VAR_UNKNOWN
859 || (actual->tt_type == VAR_FUNC
860 && (expected->tt_type == VAR_FUNC
861 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100862 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
863 && ((actual->tt_member == &t_void)
864 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200865 return TRUE;
866 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
867 && actual->tt_type == expected->tt_type)
868 // This takes care of a nested list or dict.
869 return use_typecheck(actual->tt_member, expected->tt_member);
870 return FALSE;
871}
872
873/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200874 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200875 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200876 * - "actual" is a type that can be "expected" type: add a runtime check; or
877 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200878 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
879 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200880 */
881 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200882need_type(
883 type_T *actual,
884 type_T *expected,
885 int offset,
886 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200887 int silent,
888 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200889{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200890 if (expected == &t_bool && actual != &t_bool
891 && (actual->tt_flags & TTFLAG_BOOL_OK))
892 {
893 // Using "0", "1" or the result of an expression with "&&" or "||" as a
894 // boolean is OK but requires a conversion.
895 generate_2BOOL(cctx, FALSE);
896 return OK;
897 }
898
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200899 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200900 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200901
902 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200903 // If it's a constant a runtime check makes no sense.
904 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200905 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200906 generate_TYPECHECK(cctx, expected, offset);
907 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200908 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200909
910 if (!silent)
911 type_mismatch(expected, actual);
912 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200913}
914
915/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100916 * Check that the top of the type stack has a type that can be used as a
917 * condition. Give an error and return FAIL if not.
918 */
919 static int
920bool_on_stack(cctx_T *cctx)
921{
922 garray_T *stack = &cctx->ctx_type_stack;
923 type_T *type;
924
925 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
926 if (type == &t_bool)
927 return OK;
928
929 if (type == &t_any || type == &t_number)
930 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
931 // This requires a runtime type check.
932 return generate_COND2BOOL(cctx);
933
934 return need_type(type, &t_bool, -1, cctx, FALSE, FALSE);
935}
936
937/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100938 * Generate an ISN_PUSHNR instruction.
939 */
940 static int
941generate_PUSHNR(cctx_T *cctx, varnumber_T number)
942{
943 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200944 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100945
Bram Moolenaar080457c2020-03-03 21:53:32 +0100946 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100947 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
948 return FAIL;
949 isn->isn_arg.number = number;
950
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200951 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200952 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +0100953 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100954 return OK;
955}
956
957/*
958 * Generate an ISN_PUSHBOOL instruction.
959 */
960 static int
961generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
962{
963 isn_T *isn;
964
Bram Moolenaar080457c2020-03-03 21:53:32 +0100965 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100966 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
967 return FAIL;
968 isn->isn_arg.number = number;
969
970 return OK;
971}
972
973/*
974 * Generate an ISN_PUSHSPEC instruction.
975 */
976 static int
977generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
978{
979 isn_T *isn;
980
Bram Moolenaar080457c2020-03-03 21:53:32 +0100981 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100982 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
983 return FAIL;
984 isn->isn_arg.number = number;
985
986 return OK;
987}
988
989#ifdef FEAT_FLOAT
990/*
991 * Generate an ISN_PUSHF instruction.
992 */
993 static int
994generate_PUSHF(cctx_T *cctx, float_T fnumber)
995{
996 isn_T *isn;
997
Bram Moolenaar080457c2020-03-03 21:53:32 +0100998 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100999 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1000 return FAIL;
1001 isn->isn_arg.fnumber = fnumber;
1002
1003 return OK;
1004}
1005#endif
1006
1007/*
1008 * Generate an ISN_PUSHS instruction.
1009 * Consumes "str".
1010 */
1011 static int
1012generate_PUSHS(cctx_T *cctx, char_u *str)
1013{
1014 isn_T *isn;
1015
Bram Moolenaar508b5612021-01-02 18:17:26 +01001016 if (cctx->ctx_skip == SKIP_YES)
1017 {
1018 vim_free(str);
1019 return OK;
1020 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001021 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1022 return FAIL;
1023 isn->isn_arg.string = str;
1024
1025 return OK;
1026}
1027
1028/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001029 * Generate an ISN_PUSHCHANNEL instruction.
1030 * Consumes "channel".
1031 */
1032 static int
1033generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1034{
1035 isn_T *isn;
1036
Bram Moolenaar080457c2020-03-03 21:53:32 +01001037 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001038 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1039 return FAIL;
1040 isn->isn_arg.channel = channel;
1041
1042 return OK;
1043}
1044
1045/*
1046 * Generate an ISN_PUSHJOB instruction.
1047 * Consumes "job".
1048 */
1049 static int
1050generate_PUSHJOB(cctx_T *cctx, job_T *job)
1051{
1052 isn_T *isn;
1053
Bram Moolenaar080457c2020-03-03 21:53:32 +01001054 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001055 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001056 return FAIL;
1057 isn->isn_arg.job = job;
1058
1059 return OK;
1060}
1061
1062/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001063 * Generate an ISN_PUSHBLOB instruction.
1064 * Consumes "blob".
1065 */
1066 static int
1067generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1068{
1069 isn_T *isn;
1070
Bram Moolenaar080457c2020-03-03 21:53:32 +01001071 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001072 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1073 return FAIL;
1074 isn->isn_arg.blob = blob;
1075
1076 return OK;
1077}
1078
1079/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001080 * Generate an ISN_PUSHFUNC instruction with name "name".
1081 * Consumes "name".
1082 */
1083 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001084generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001085{
1086 isn_T *isn;
1087
Bram Moolenaar080457c2020-03-03 21:53:32 +01001088 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001089 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001090 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001091 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001092
1093 return OK;
1094}
1095
1096/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001097 * Generate an ISN_GETITEM instruction with "index".
1098 */
1099 static int
1100generate_GETITEM(cctx_T *cctx, int index)
1101{
1102 isn_T *isn;
1103 garray_T *stack = &cctx->ctx_type_stack;
1104 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1105 type_T *item_type = &t_any;
1106
1107 RETURN_OK_IF_SKIP(cctx);
1108
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001109 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001110 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001111 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001112 emsg(_(e_listreq));
1113 return FAIL;
1114 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001115 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001116 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1117 return FAIL;
1118 isn->isn_arg.number = index;
1119
1120 // add the item type to the type stack
1121 if (ga_grow(stack, 1) == FAIL)
1122 return FAIL;
1123 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1124 ++stack->ga_len;
1125 return OK;
1126}
1127
1128/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001129 * Generate an ISN_SLICE instruction with "count".
1130 */
1131 static int
1132generate_SLICE(cctx_T *cctx, int count)
1133{
1134 isn_T *isn;
1135
1136 RETURN_OK_IF_SKIP(cctx);
1137 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1138 return FAIL;
1139 isn->isn_arg.number = count;
1140 return OK;
1141}
1142
1143/*
1144 * Generate an ISN_CHECKLEN instruction with "min_len".
1145 */
1146 static int
1147generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1148{
1149 isn_T *isn;
1150
1151 RETURN_OK_IF_SKIP(cctx);
1152
1153 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1154 return FAIL;
1155 isn->isn_arg.checklen.cl_min_len = min_len;
1156 isn->isn_arg.checklen.cl_more_OK = more_OK;
1157
1158 return OK;
1159}
1160
1161/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162 * Generate an ISN_STORE instruction.
1163 */
1164 static int
1165generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1166{
1167 isn_T *isn;
1168
Bram Moolenaar080457c2020-03-03 21:53:32 +01001169 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001170 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1171 return FAIL;
1172 if (name != NULL)
1173 isn->isn_arg.string = vim_strsave(name);
1174 else
1175 isn->isn_arg.number = idx;
1176
1177 return OK;
1178}
1179
1180/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001181 * Generate an ISN_STOREOUTER instruction.
1182 */
1183 static int
1184generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1185{
1186 isn_T *isn;
1187
1188 RETURN_OK_IF_SKIP(cctx);
1189 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1190 return FAIL;
1191 isn->isn_arg.outer.outer_idx = idx;
1192 isn->isn_arg.outer.outer_depth = level;
1193
1194 return OK;
1195}
1196
1197/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001198 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1199 */
1200 static int
1201generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1202{
1203 isn_T *isn;
1204
Bram Moolenaar080457c2020-03-03 21:53:32 +01001205 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1207 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001208 isn->isn_arg.storenr.stnr_idx = idx;
1209 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001210
1211 return OK;
1212}
1213
1214/*
1215 * Generate an ISN_STOREOPT instruction
1216 */
1217 static int
1218generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1219{
1220 isn_T *isn;
1221
Bram Moolenaar080457c2020-03-03 21:53:32 +01001222 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001223 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001224 return FAIL;
1225 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1226 isn->isn_arg.storeopt.so_flags = opt_flags;
1227
1228 return OK;
1229}
1230
1231/*
1232 * Generate an ISN_LOAD or similar instruction.
1233 */
1234 static int
1235generate_LOAD(
1236 cctx_T *cctx,
1237 isntype_T isn_type,
1238 int idx,
1239 char_u *name,
1240 type_T *type)
1241{
1242 isn_T *isn;
1243
Bram Moolenaar080457c2020-03-03 21:53:32 +01001244 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001245 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1246 return FAIL;
1247 if (name != NULL)
1248 isn->isn_arg.string = vim_strsave(name);
1249 else
1250 isn->isn_arg.number = idx;
1251
1252 return OK;
1253}
1254
1255/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001256 * Generate an ISN_LOADOUTER instruction
1257 */
1258 static int
1259generate_LOADOUTER(
1260 cctx_T *cctx,
1261 int idx,
1262 int nesting,
1263 type_T *type)
1264{
1265 isn_T *isn;
1266
1267 RETURN_OK_IF_SKIP(cctx);
1268 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1269 return FAIL;
1270 isn->isn_arg.outer.outer_idx = idx;
1271 isn->isn_arg.outer.outer_depth = nesting;
1272
1273 return OK;
1274}
1275
1276/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001277 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001278 */
1279 static int
1280generate_LOADV(
1281 cctx_T *cctx,
1282 char_u *name,
1283 int error)
1284{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001285 int di_flags;
1286 int vidx = find_vim_var(name, &di_flags);
1287 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001288
Bram Moolenaar080457c2020-03-03 21:53:32 +01001289 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001290 if (vidx < 0)
1291 {
1292 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001293 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001294 return FAIL;
1295 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001296 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001297
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001298 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001299}
1300
1301/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001302 * Generate an ISN_UNLET instruction.
1303 */
1304 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001305generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001306{
1307 isn_T *isn;
1308
1309 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001310 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001311 return FAIL;
1312 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1313 isn->isn_arg.unlet.ul_forceit = forceit;
1314
1315 return OK;
1316}
1317
1318/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001319 * Generate an ISN_LOCKCONST instruction.
1320 */
1321 static int
1322generate_LOCKCONST(cctx_T *cctx)
1323{
1324 isn_T *isn;
1325
1326 RETURN_OK_IF_SKIP(cctx);
1327 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1328 return FAIL;
1329 return OK;
1330}
1331
1332/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001333 * Generate an ISN_LOADS instruction.
1334 */
1335 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001336generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001338 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001340 int sid,
1341 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342{
1343 isn_T *isn;
1344
Bram Moolenaar080457c2020-03-03 21:53:32 +01001345 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001346 if (isn_type == ISN_LOADS)
1347 isn = generate_instr_type(cctx, isn_type, type);
1348 else
1349 isn = generate_instr_drop(cctx, isn_type, 1);
1350 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001351 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001352 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1353 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354
1355 return OK;
1356}
1357
1358/*
1359 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1360 */
1361 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001362generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363 cctx_T *cctx,
1364 isntype_T isn_type,
1365 int sid,
1366 int idx,
1367 type_T *type)
1368{
1369 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001370 scriptref_T *sref;
1371 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001372
Bram Moolenaar080457c2020-03-03 21:53:32 +01001373 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001374 if (isn_type == ISN_LOADSCRIPT)
1375 isn = generate_instr_type(cctx, isn_type, type);
1376 else
1377 isn = generate_instr_drop(cctx, isn_type, 1);
1378 if (isn == NULL)
1379 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001380
1381 // This requires three arguments, which doesn't fit in an instruction, thus
1382 // we need to allocate a struct for this.
1383 sref = ALLOC_ONE(scriptref_T);
1384 if (sref == NULL)
1385 return FAIL;
1386 isn->isn_arg.script.scriptref = sref;
1387 sref->sref_sid = sid;
1388 sref->sref_idx = idx;
1389 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001390 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001391 return OK;
1392}
1393
1394/*
1395 * Generate an ISN_NEWLIST instruction.
1396 */
1397 static int
1398generate_NEWLIST(cctx_T *cctx, int count)
1399{
1400 isn_T *isn;
1401 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402 type_T *type;
1403 type_T *member;
1404
Bram Moolenaar080457c2020-03-03 21:53:32 +01001405 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1407 return FAIL;
1408 isn->isn_arg.number = count;
1409
Bram Moolenaar127542b2020-08-09 17:22:04 +02001410 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001411 if (count == 0)
1412 member = &t_void;
1413 else
1414 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001415 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1416 cctx->ctx_type_list);
1417 type = get_list_type(member, cctx->ctx_type_list);
1418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001419 // drop the value types
1420 stack->ga_len -= count;
1421
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001422 // add the list type to the type stack
1423 if (ga_grow(stack, 1) == FAIL)
1424 return FAIL;
1425 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1426 ++stack->ga_len;
1427
1428 return OK;
1429}
1430
1431/*
1432 * Generate an ISN_NEWDICT instruction.
1433 */
1434 static int
1435generate_NEWDICT(cctx_T *cctx, int count)
1436{
1437 isn_T *isn;
1438 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001439 type_T *type;
1440 type_T *member;
1441
Bram Moolenaar080457c2020-03-03 21:53:32 +01001442 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001443 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1444 return FAIL;
1445 isn->isn_arg.number = count;
1446
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001447 if (count == 0)
1448 member = &t_void;
1449 else
1450 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001451 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1452 cctx->ctx_type_list);
1453 type = get_dict_type(member, cctx->ctx_type_list);
1454
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455 // drop the key and value types
1456 stack->ga_len -= 2 * count;
1457
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001458 // add the dict type to the type stack
1459 if (ga_grow(stack, 1) == FAIL)
1460 return FAIL;
1461 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1462 ++stack->ga_len;
1463
1464 return OK;
1465}
1466
1467/*
1468 * Generate an ISN_FUNCREF instruction.
1469 */
1470 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001471generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472{
1473 isn_T *isn;
1474 garray_T *stack = &cctx->ctx_type_stack;
1475
Bram Moolenaar080457c2020-03-03 21:53:32 +01001476 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001477 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1478 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001479 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001480 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001481
Bram Moolenaarab360522021-01-10 14:02:28 +01001482 // if the referenced function is a closure, it may use items further up in
1483 // the nested context, including this one.
1484 if (ufunc->uf_flags & FC_CLOSURE)
1485 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001487 if (ga_grow(stack, 1) == FAIL)
1488 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001489 ((type_T **)stack->ga_data)[stack->ga_len] =
1490 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491 ++stack->ga_len;
1492
1493 return OK;
1494}
1495
1496/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001497 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001498 * "lambda_name" and "func_name" must be in allocated memory and will be
1499 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001500 */
1501 static int
1502generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1503{
1504 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001505
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001506 if (cctx->ctx_skip == SKIP_YES)
1507 {
1508 vim_free(lambda_name);
1509 vim_free(func_name);
1510 return OK;
1511 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001512 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001513 {
1514 vim_free(lambda_name);
1515 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001516 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001517 }
1518 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001519 isn->isn_arg.newfunc.nf_global = func_name;
1520
1521 return OK;
1522}
1523
1524/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001525 * Generate an ISN_DEF instruction: list functions
1526 */
1527 static int
1528generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1529{
1530 isn_T *isn;
1531
1532 RETURN_OK_IF_SKIP(cctx);
1533 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1534 return FAIL;
1535 if (len > 0)
1536 {
1537 isn->isn_arg.string = vim_strnsave(name, len);
1538 if (isn->isn_arg.string == NULL)
1539 return FAIL;
1540 }
1541 return OK;
1542}
1543
1544/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001545 * Generate an ISN_JUMP instruction.
1546 */
1547 static int
1548generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1549{
1550 isn_T *isn;
1551 garray_T *stack = &cctx->ctx_type_stack;
1552
Bram Moolenaar080457c2020-03-03 21:53:32 +01001553 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001554 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1555 return FAIL;
1556 isn->isn_arg.jump.jump_when = when;
1557 isn->isn_arg.jump.jump_where = where;
1558
1559 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1560 --stack->ga_len;
1561
1562 return OK;
1563}
1564
1565 static int
1566generate_FOR(cctx_T *cctx, int loop_idx)
1567{
1568 isn_T *isn;
1569 garray_T *stack = &cctx->ctx_type_stack;
1570
Bram Moolenaar080457c2020-03-03 21:53:32 +01001571 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1573 return FAIL;
1574 isn->isn_arg.forloop.for_idx = loop_idx;
1575
1576 if (ga_grow(stack, 1) == FAIL)
1577 return FAIL;
1578 // type doesn't matter, will be stored next
1579 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1580 ++stack->ga_len;
1581
1582 return OK;
1583}
1584
1585/*
1586 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001587 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588 * Return FAIL if the number of arguments is wrong.
1589 */
1590 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001591generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592{
1593 isn_T *isn;
1594 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001595 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001596 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001597 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001598
Bram Moolenaar080457c2020-03-03 21:53:32 +01001599 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001600 argoff = check_internal_func(func_idx, argcount);
1601 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001602 return FAIL;
1603
Bram Moolenaar389df252020-07-09 21:20:47 +02001604 if (method_call && argoff > 1)
1605 {
1606 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1607 return FAIL;
1608 isn->isn_arg.shuffle.shfl_item = argcount;
1609 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1610 }
1611
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001612 if (argcount > 0)
1613 {
1614 // Check the types of the arguments.
1615 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
1616 if (internal_func_check_arg_types(argtypes, func_idx, argcount) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001617 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001618 if (internal_func_is_map(func_idx))
1619 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001620 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001621
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1623 return FAIL;
1624 isn->isn_arg.bfunc.cbf_idx = func_idx;
1625 isn->isn_arg.bfunc.cbf_argcount = argcount;
1626
Bram Moolenaar94738d82020-10-21 14:25:07 +02001627 // Drop the argument types and push the return type.
1628 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629 if (ga_grow(stack, 1) == FAIL)
1630 return FAIL;
1631 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001632 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001633 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001634
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001635 if (maptype != NULL && maptype->tt_member != NULL
1636 && maptype->tt_member != &t_any)
1637 // Check that map() didn't change the item types.
1638 generate_TYPECHECK(cctx, maptype, -1);
1639
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640 return OK;
1641}
1642
1643/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001644 * Generate an ISN_LISTAPPEND instruction. Works like add().
1645 * Argument count is already checked.
1646 */
1647 static int
1648generate_LISTAPPEND(cctx_T *cctx)
1649{
1650 garray_T *stack = &cctx->ctx_type_stack;
1651 type_T *list_type;
1652 type_T *item_type;
1653 type_T *expected;
1654
1655 // Caller already checked that list_type is a list.
1656 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1657 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1658 expected = list_type->tt_member;
1659 if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL)
1660 return FAIL;
1661
1662 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1663 return FAIL;
1664
1665 --stack->ga_len; // drop the argument
1666 return OK;
1667}
1668
1669/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001670 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1671 * Argument count is already checked.
1672 */
1673 static int
1674generate_BLOBAPPEND(cctx_T *cctx)
1675{
1676 garray_T *stack = &cctx->ctx_type_stack;
1677 type_T *item_type;
1678
1679 // Caller already checked that blob_type is a blob.
1680 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1681 if (need_type(item_type, &t_number, -1, cctx, FALSE, FALSE) == FAIL)
1682 return FAIL;
1683
1684 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1685 return FAIL;
1686
1687 --stack->ga_len; // drop the argument
1688 return OK;
1689}
1690
1691/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001692 * Generate an ISN_DCALL or ISN_UCALL instruction.
1693 * Return FAIL if the number of arguments is wrong.
1694 */
1695 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001696generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697{
1698 isn_T *isn;
1699 garray_T *stack = &cctx->ctx_type_stack;
1700 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001701 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702
Bram Moolenaar080457c2020-03-03 21:53:32 +01001703 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001704 if (argcount > regular_args && !has_varargs(ufunc))
1705 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001706 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 return FAIL;
1708 }
1709 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1710 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001711 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001712 return FAIL;
1713 }
1714
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001715 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001716 {
1717 int i;
1718
1719 for (i = 0; i < argcount; ++i)
1720 {
1721 type_T *expected;
1722 type_T *actual;
1723
1724 if (i < regular_args)
1725 {
1726 if (ufunc->uf_arg_types == NULL)
1727 continue;
1728 expected = ufunc->uf_arg_types[i];
1729 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001730 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1731 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001732 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001733 else
1734 expected = ufunc->uf_va_type->tt_member;
1735 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001736 if (need_type(actual, expected, -argcount + i, cctx,
1737 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001738 {
1739 arg_type_mismatch(expected, actual, i + 1);
1740 return FAIL;
1741 }
1742 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001743 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001744 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1745 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001746 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001747 }
1748
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001749 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001750 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001751 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001753 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001754 {
1755 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1756 isn->isn_arg.dfunc.cdf_argcount = argcount;
1757 }
1758 else
1759 {
1760 // A user function may be deleted and redefined later, can't use the
1761 // ufunc pointer, need to look it up again at runtime.
1762 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1763 isn->isn_arg.ufunc.cuf_argcount = argcount;
1764 }
1765
1766 stack->ga_len -= argcount; // drop the arguments
1767 if (ga_grow(stack, 1) == FAIL)
1768 return FAIL;
1769 // add return value
1770 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1771 ++stack->ga_len;
1772
1773 return OK;
1774}
1775
1776/*
1777 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1778 */
1779 static int
1780generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1781{
1782 isn_T *isn;
1783 garray_T *stack = &cctx->ctx_type_stack;
1784
Bram Moolenaar080457c2020-03-03 21:53:32 +01001785 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001786 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1787 return FAIL;
1788 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1789 isn->isn_arg.ufunc.cuf_argcount = argcount;
1790
1791 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001792 if (ga_grow(stack, 1) == FAIL)
1793 return FAIL;
1794 // add return value
1795 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1796 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001797
1798 return OK;
1799}
1800
1801/*
1802 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001803 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804 */
1805 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001806generate_PCALL(
1807 cctx_T *cctx,
1808 int argcount,
1809 char_u *name,
1810 type_T *type,
1811 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001812{
1813 isn_T *isn;
1814 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001815 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001816
Bram Moolenaar080457c2020-03-03 21:53:32 +01001817 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001818
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001819 if (type->tt_type == VAR_ANY)
1820 ret_type = &t_any;
1821 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001822 {
1823 if (type->tt_argcount != -1)
1824 {
1825 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1826
1827 if (argcount < type->tt_min_argcount - varargs)
1828 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001829 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001830 return FAIL;
1831 }
1832 if (!varargs && argcount > type->tt_argcount)
1833 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001834 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001835 return FAIL;
1836 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001837 if (type->tt_args != NULL)
1838 {
1839 int i;
1840
1841 for (i = 0; i < argcount; ++i)
1842 {
1843 int offset = -argcount + i - 1;
1844 type_T *actual = ((type_T **)stack->ga_data)[
1845 stack->ga_len + offset];
1846 type_T *expected;
1847
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001848 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001849 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001850 type->tt_argcount - 1]->tt_member;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001851 else
1852 expected = type->tt_args[i];
1853 if (need_type(actual, expected, offset,
1854 cctx, TRUE, FALSE) == FAIL)
1855 {
1856 arg_type_mismatch(expected, actual, i + 1);
1857 return FAIL;
1858 }
1859 }
1860 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001861 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001862 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001863 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001864 else
1865 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001866 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001867 return FAIL;
1868 }
1869
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001870 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1871 return FAIL;
1872 isn->isn_arg.pfunc.cpf_top = at_top;
1873 isn->isn_arg.pfunc.cpf_argcount = argcount;
1874
1875 stack->ga_len -= argcount; // drop the arguments
1876
1877 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001878 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001879
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001880 // If partial is above the arguments it must be cleared and replaced with
1881 // the return value.
1882 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1883 return FAIL;
1884
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001885 return OK;
1886}
1887
1888/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001889 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001890 */
1891 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001892generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001893{
1894 isn_T *isn;
1895 garray_T *stack = &cctx->ctx_type_stack;
1896 type_T *type;
1897
Bram Moolenaar080457c2020-03-03 21:53:32 +01001898 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001899 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001900 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001901 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001903 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001905 if (type->tt_type != VAR_DICT && type != &t_any)
1906 {
1907 emsg(_(e_dictreq));
1908 return FAIL;
1909 }
1910 // change dict type to dict member type
1911 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01001912 {
1913 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
1914 type->tt_member == &t_unknown ? &t_any : type->tt_member;
1915 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001916
1917 return OK;
1918}
1919
1920/*
1921 * Generate an ISN_ECHO instruction.
1922 */
1923 static int
1924generate_ECHO(cctx_T *cctx, int with_white, int count)
1925{
1926 isn_T *isn;
1927
Bram Moolenaar080457c2020-03-03 21:53:32 +01001928 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1930 return FAIL;
1931 isn->isn_arg.echo.echo_with_white = with_white;
1932 isn->isn_arg.echo.echo_count = count;
1933
1934 return OK;
1935}
1936
Bram Moolenaarad39c092020-02-26 18:23:43 +01001937/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001938 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001939 */
1940 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001941generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001942{
1943 isn_T *isn;
1944
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001945 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001946 return FAIL;
1947 isn->isn_arg.number = count;
1948
1949 return OK;
1950}
1951
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001952/*
1953 * Generate an ISN_PUT instruction.
1954 */
1955 static int
1956generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1957{
1958 isn_T *isn;
1959
1960 RETURN_OK_IF_SKIP(cctx);
1961 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1962 return FAIL;
1963 isn->isn_arg.put.put_regname = regname;
1964 isn->isn_arg.put.put_lnum = lnum;
1965 return OK;
1966}
1967
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968 static int
1969generate_EXEC(cctx_T *cctx, char_u *line)
1970{
1971 isn_T *isn;
1972
Bram Moolenaar080457c2020-03-03 21:53:32 +01001973 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001974 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1975 return FAIL;
1976 isn->isn_arg.string = vim_strsave(line);
1977 return OK;
1978}
1979
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001980 static int
1981generate_EXECCONCAT(cctx_T *cctx, int count)
1982{
1983 isn_T *isn;
1984
1985 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1986 return FAIL;
1987 isn->isn_arg.number = count;
1988 return OK;
1989}
1990
Bram Moolenaar08597872020-12-10 19:43:40 +01001991/*
1992 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1993 */
1994 static int
1995generate_RANGE(cctx_T *cctx, char_u *range)
1996{
1997 isn_T *isn;
1998 garray_T *stack = &cctx->ctx_type_stack;
1999
2000 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2001 return FAIL;
2002 isn->isn_arg.string = range;
2003
2004 if (ga_grow(stack, 1) == FAIL)
2005 return FAIL;
2006 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2007 ++stack->ga_len;
2008 return OK;
2009}
2010
Bram Moolenaar792f7862020-11-23 08:31:18 +01002011 static int
2012generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2013{
2014 isn_T *isn;
2015
2016 RETURN_OK_IF_SKIP(cctx);
2017 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2018 return FAIL;
2019 isn->isn_arg.unpack.unp_count = var_count;
2020 isn->isn_arg.unpack.unp_semicolon = semicolon;
2021 return OK;
2022}
2023
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002025 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002026 */
2027 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002028generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002029{
2030 isn_T *isn;
2031
Bram Moolenaar02194d22020-10-24 23:08:38 +02002032 if (cmod->cmod_flags != 0
2033 || cmod->cmod_split != 0
2034 || cmod->cmod_verbose != 0
2035 || cmod->cmod_tab != 0
2036 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002037 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002038 cctx->ctx_has_cmdmod = TRUE;
2039
2040 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002041 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002042 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2043 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2044 return FAIL;
2045 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002046 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002047 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002048 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002049
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002050 return OK;
2051}
2052
2053 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002054generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002055{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002056 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2057 return FAIL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002058 return OK;
2059}
2060
2061/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002062 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002063 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002065 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002066reserve_local(
2067 cctx_T *cctx,
2068 char_u *name,
2069 size_t len,
2070 int isConst,
2071 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002073 lvar_T *lvar;
2074
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002075 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002076 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002077 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002078 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002079 }
2080
2081 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002082 return NULL;
2083 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002084 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002085
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002086 // Every local variable uses the next entry on the stack. We could re-use
2087 // the last ones when leaving a scope, but then variables used in a closure
2088 // might get overwritten. To keep things simple do not re-use stack
2089 // entries. This is less efficient, but memory is cheap these days.
2090 lvar->lv_idx = cctx->ctx_locals_count++;
2091
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002092 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002093 lvar->lv_const = isConst;
2094 lvar->lv_type = type;
2095
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002096 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002097}
2098
2099/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002100 * Remove local variables above "new_top".
2101 */
2102 static void
2103unwind_locals(cctx_T *cctx, int new_top)
2104{
2105 if (cctx->ctx_locals.ga_len > new_top)
2106 {
2107 int idx;
2108 lvar_T *lvar;
2109
2110 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2111 {
2112 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2113 vim_free(lvar->lv_name);
2114 }
2115 }
2116 cctx->ctx_locals.ga_len = new_top;
2117}
2118
2119/*
2120 * Free all local variables.
2121 */
2122 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002123free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002124{
2125 unwind_locals(cctx, 0);
2126 ga_clear(&cctx->ctx_locals);
2127}
2128
2129/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002130 * Find "name" in script-local items of script "sid".
2131 * Returns the index in "sn_var_vals" if found.
2132 * If found but not in "sn_var_vals" returns -1.
2133 * If not found returns -2.
2134 */
2135 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002136get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002137{
2138 hashtab_T *ht;
2139 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002140 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002141 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002142 int idx;
2143
Bram Moolenaare3d46852020-08-29 13:39:17 +02002144 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002145 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002146 if (sid == current_sctx.sc_sid)
2147 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002148 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002149
2150 if (sav == NULL)
2151 return -2;
2152 idx = sav->sav_var_vals_idx;
2153 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2154 if (check_writable && sv->sv_const)
2155 semsg(_(e_readonlyvar), name);
2156 return idx;
2157 }
2158
2159 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002160 ht = &SCRIPT_VARS(sid);
2161 di = find_var_in_ht(ht, 0, name, TRUE);
2162 if (di == NULL)
2163 return -2;
2164
2165 // Now find the svar_T index in sn_var_vals.
2166 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2167 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002168 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169 if (sv->sv_tv == &di->di_tv)
2170 {
2171 if (check_writable && sv->sv_const)
2172 semsg(_(e_readonlyvar), name);
2173 return idx;
2174 }
2175 }
2176 return -1;
2177}
2178
2179/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002180 * Find "name" in imported items of the current script or in "cctx" if not
2181 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002182 */
2183 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002184find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002185{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002186 int idx;
2187
Bram Moolenaare3d46852020-08-29 13:39:17 +02002188 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002189 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002190 if (cctx != NULL)
2191 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2192 {
2193 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2194 + idx;
2195
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002196 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2197 : STRLEN(import->imp_name) == len
2198 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002199 return import;
2200 }
2201
Bram Moolenaarefa94442020-08-08 22:16:00 +02002202 return find_imported_in_script(name, len, current_sctx.sc_sid);
2203}
2204
2205 imported_T *
2206find_imported_in_script(char_u *name, size_t len, int sid)
2207{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002208 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002209 int idx;
2210
Bram Moolenaare3d46852020-08-29 13:39:17 +02002211 if (!SCRIPT_ID_VALID(sid))
2212 return NULL;
2213 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002214 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2215 {
2216 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2217
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002218 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2219 : STRLEN(import->imp_name) == len
2220 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002221 return import;
2222 }
2223 return NULL;
2224}
2225
2226/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002227 * Free all imported variables.
2228 */
2229 static void
2230free_imported(cctx_T *cctx)
2231{
2232 int idx;
2233
2234 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2235 {
2236 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2237
2238 vim_free(import->imp_name);
2239 }
2240 ga_clear(&cctx->ctx_imports);
2241}
2242
2243/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002244 * Return a pointer to the next line that isn't empty or only contains a
2245 * comment. Skips over white space.
2246 * Returns NULL if there is none.
2247 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002248 char_u *
2249peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002250{
2251 int lnum = cctx->ctx_lnum;
2252
2253 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2254 {
2255 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002256 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002257
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002258 // ignore NULLs inserted for continuation lines
2259 if (line != NULL)
2260 {
2261 p = skipwhite(line);
2262 if (*p != NUL && !vim9_comment_start(p))
2263 return p;
2264 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002265 }
2266 return NULL;
2267}
2268
2269/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002270 * Called when checking for a following operator at "arg". When the rest of
2271 * the line is empty or only a comment, peek the next line. If there is a next
2272 * line return a pointer to it and set "nextp".
2273 * Otherwise skip over white space.
2274 */
2275 static char_u *
2276may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2277{
2278 char_u *p = skipwhite(arg);
2279
2280 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002281 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002282 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002283 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002284 if (*nextp != NULL)
2285 return *nextp;
2286 }
2287 return p;
2288}
2289
2290/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002291 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002292 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002293 * Returns NULL when at the end.
2294 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002295 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002296next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002297{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002298 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002299
2300 do
2301 {
2302 ++cctx->ctx_lnum;
2303 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002304 {
2305 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002306 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002307 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002308 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002309 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002310 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002311 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002312 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002313 return line;
2314}
2315
2316/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002317 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002318 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002319 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002320 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2321 */
2322 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002323may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002324{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002325 *arg = skipwhite(whitep);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002326 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002327 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002328 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002329
2330 if (next == NULL)
2331 return FAIL;
2332 *arg = skipwhite(next);
2333 }
2334 return OK;
2335}
2336
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002337/*
2338 * Idem, and give an error when failed.
2339 */
2340 static int
2341may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2342{
2343 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2344 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002345 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002346 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002347 return FAIL;
2348 }
2349 return OK;
2350}
2351
2352
Bram Moolenaara5565e42020-05-09 15:44:01 +02002353// Structure passed between the compile_expr* functions to keep track of
2354// constants that have been parsed but for which no code was produced yet. If
2355// possible expressions on these constants are applied at compile time. If
2356// that is not possible, the code to push the constants needs to be generated
2357// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002358// Using 50 should be more than enough of 5 levels of ().
2359#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002360typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002361 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002362 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002363 int pp_is_const; // all generated code was constants, used for a
2364 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002365} ppconst_T;
2366
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002367static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002368static int compile_expr0(char_u **arg, cctx_T *cctx);
2369static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2370
Bram Moolenaara5565e42020-05-09 15:44:01 +02002371/*
2372 * Generate a PUSH instruction for "tv".
2373 * "tv" will be consumed or cleared.
2374 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2375 */
2376 static int
2377generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2378{
2379 if (tv != NULL)
2380 {
2381 switch (tv->v_type)
2382 {
2383 case VAR_UNKNOWN:
2384 break;
2385 case VAR_BOOL:
2386 generate_PUSHBOOL(cctx, tv->vval.v_number);
2387 break;
2388 case VAR_SPECIAL:
2389 generate_PUSHSPEC(cctx, tv->vval.v_number);
2390 break;
2391 case VAR_NUMBER:
2392 generate_PUSHNR(cctx, tv->vval.v_number);
2393 break;
2394#ifdef FEAT_FLOAT
2395 case VAR_FLOAT:
2396 generate_PUSHF(cctx, tv->vval.v_float);
2397 break;
2398#endif
2399 case VAR_BLOB:
2400 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2401 tv->vval.v_blob = NULL;
2402 break;
2403 case VAR_STRING:
2404 generate_PUSHS(cctx, tv->vval.v_string);
2405 tv->vval.v_string = NULL;
2406 break;
2407 default:
2408 iemsg("constant type not supported");
2409 clear_tv(tv);
2410 return FAIL;
2411 }
2412 tv->v_type = VAR_UNKNOWN;
2413 }
2414 return OK;
2415}
2416
2417/*
2418 * Generate code for any ppconst entries.
2419 */
2420 static int
2421generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2422{
2423 int i;
2424 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002425 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002426
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002427 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002428 for (i = 0; i < ppconst->pp_used; ++i)
2429 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2430 ret = FAIL;
2431 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002432 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002433 return ret;
2434}
2435
2436/*
2437 * Clear ppconst constants. Used when failing.
2438 */
2439 static void
2440clear_ppconst(ppconst_T *ppconst)
2441{
2442 int i;
2443
2444 for (i = 0; i < ppconst->pp_used; ++i)
2445 clear_tv(&ppconst->pp_tv[i]);
2446 ppconst->pp_used = 0;
2447}
2448
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002449/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002450 * Generate an instruction to load script-local variable "name", without the
2451 * leading "s:".
2452 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002453 */
2454 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002455compile_load_scriptvar(
2456 cctx_T *cctx,
2457 char_u *name, // variable NUL terminated
2458 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002459 char_u **end, // end of variable
2460 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002461{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002462 scriptitem_T *si;
2463 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002464 imported_T *import;
2465
Bram Moolenaare3d46852020-08-29 13:39:17 +02002466 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2467 return FAIL;
2468 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002469 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002470 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002472 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002473 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2474 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 }
2476 if (idx >= 0)
2477 {
2478 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2479
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002480 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481 current_sctx.sc_sid, idx, sv->sv_type);
2482 return OK;
2483 }
2484
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002485 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486 if (import != NULL)
2487 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002488 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002489 {
2490 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002491 char_u *exp_name;
2492 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002493 ufunc_T *ufunc;
2494 type_T *type;
2495
2496 // Used "import * as Name", need to lookup the member.
2497 if (*p != '.')
2498 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002499 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002500 return FAIL;
2501 }
2502 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002503 if (VIM_ISWHITE(*p))
2504 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002505 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002506 return FAIL;
2507 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002508
Bram Moolenaar1c991142020-07-04 13:15:31 +02002509 // isolate one name
2510 exp_name = p;
2511 while (eval_isnamec(*p))
2512 ++p;
2513 cc = *p;
2514 *p = NUL;
2515
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002516 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002517 *p = cc;
2518 p = skipwhite(p);
2519
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002520 // TODO: what if it is a function?
2521 if (idx < 0)
2522 return FAIL;
2523 *end = p;
2524
2525 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2526 import->imp_sid,
2527 idx,
2528 type);
2529 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002530 else if (import->imp_funcname != NULL)
2531 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002532 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002533 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2534 import->imp_sid,
2535 import->imp_var_vals_idx,
2536 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002537 return OK;
2538 }
2539
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002540 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002541 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542 return FAIL;
2543}
2544
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002545 static int
2546generate_funcref(cctx_T *cctx, char_u *name)
2547{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002548 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002549
2550 if (ufunc == NULL)
2551 return FAIL;
2552
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002553 // Need to compile any default values to get the argument types.
2554 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2555 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2556 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002557 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002558}
2559
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560/*
2561 * Compile a variable name into a load instruction.
2562 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002563 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 * When "error" is FALSE do not give an error when not found.
2565 */
2566 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002567compile_load(
2568 char_u **arg,
2569 char_u *end_arg,
2570 cctx_T *cctx,
2571 int is_expr,
2572 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002573{
2574 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002575 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002576 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002578 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579
2580 if (*(*arg + 1) == ':')
2581 {
2582 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002583 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002584 {
2585 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002587 switch (**arg)
2588 {
2589 case 'g': isn_type = ISN_LOADGDICT; break;
2590 case 'w': isn_type = ISN_LOADWDICT; break;
2591 case 't': isn_type = ISN_LOADTDICT; break;
2592 case 'b': isn_type = ISN_LOADBDICT; break;
2593 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002594 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002595 goto theend;
2596 }
2597 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2598 goto theend;
2599 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002600 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601 else
2602 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002603 isntype_T isn_type = ISN_DROP;
2604
2605 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2606 if (name == NULL)
2607 return FAIL;
2608
2609 switch (**arg)
2610 {
2611 case 'v': res = generate_LOADV(cctx, name, error);
2612 break;
2613 case 's': res = compile_load_scriptvar(cctx, name,
2614 NULL, NULL, error);
2615 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002616 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2617 isn_type = ISN_LOADG;
2618 else
2619 {
2620 isn_type = ISN_LOADAUTO;
2621 vim_free(name);
2622 name = vim_strnsave(*arg, end - *arg);
2623 if (name == NULL)
2624 return FAIL;
2625 }
2626 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002627 case 'w': isn_type = ISN_LOADW; break;
2628 case 't': isn_type = ISN_LOADT; break;
2629 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002630 default: // cannot happen, just in case
2631 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002632 goto theend;
2633 }
2634 if (isn_type != ISN_DROP)
2635 {
2636 // Global, Buffer-local, Window-local and Tabpage-local
2637 // variables can be defined later, thus we don't check if it
2638 // exists, give error at runtime.
2639 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2640 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002641 }
2642 }
2643 else
2644 {
2645 size_t len = end - *arg;
2646 int idx;
2647 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002648 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649
2650 name = vim_strnsave(*arg, end - *arg);
2651 if (name == NULL)
2652 return FAIL;
2653
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002654 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002656 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002657 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658 }
2659 else
2660 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002661 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002662
Bram Moolenaar709664c2020-12-12 14:33:41 +01002663 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002664 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002665 type = lvar.lv_type;
2666 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01002667 if (lvar.lv_from_outer != 0)
2668 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002669 else
2670 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002671 }
2672 else
2673 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002674 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002675 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002676 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002677 || find_imported(name, 0, cctx) != NULL)
2678 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002679
Bram Moolenaar0f769812020-09-12 18:32:34 +02002680 // When evaluating an expression and the name starts with an
2681 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002682 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002683 if (res == FAIL && is_expr
2684 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002685 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002686 }
2687 }
2688 if (gen_load)
2689 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01002690 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002691 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002692 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002693 cctx->ctx_outer_used = TRUE;
2694 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002695 }
2696
2697 *arg = end;
2698
2699theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002700 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002701 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702 vim_free(name);
2703 return res;
2704}
2705
2706/*
2707 * Compile the argument expressions.
2708 * "arg" points to just after the "(" and is advanced to after the ")"
2709 */
2710 static int
2711compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2712{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002713 char_u *p = *arg;
2714 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002715 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716
Bram Moolenaare6085c52020-04-12 20:19:16 +02002717 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002718 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002719 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2720 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002721 if (*p == ')')
2722 {
2723 *arg = p + 1;
2724 return OK;
2725 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002726 if (must_end)
2727 {
2728 semsg(_(e_missing_comma_before_argument_str), p);
2729 return FAIL;
2730 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002731
Bram Moolenaara5565e42020-05-09 15:44:01 +02002732 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733 return FAIL;
2734 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002735
2736 if (*p != ',' && *skipwhite(p) == ',')
2737 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002738 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002739 p = skipwhite(p);
2740 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002741 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002742 {
2743 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002744 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002745 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002746 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002747 else
2748 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002749 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002750 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002752failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002753 emsg(_(e_missing_close));
2754 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002755}
2756
2757/*
2758 * Compile a function call: name(arg1, arg2)
2759 * "arg" points to "name", "arg + varlen" to the "(".
2760 * "argcount_init" is 1 for "value->method()"
2761 * Instructions:
2762 * EVAL arg1
2763 * EVAL arg2
2764 * BCALL / DCALL / UCALL
2765 */
2766 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002767compile_call(
2768 char_u **arg,
2769 size_t varlen,
2770 cctx_T *cctx,
2771 ppconst_T *ppconst,
2772 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773{
2774 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002775 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776 int argcount = argcount_init;
2777 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002778 char_u fname_buf[FLEN_FIXED + 1];
2779 char_u *tofree = NULL;
2780 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002781 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002782 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002783 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002784
Bram Moolenaara5565e42020-05-09 15:44:01 +02002785 // we can evaluate "has('name')" at compile time
2786 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2787 {
2788 char_u *s = skipwhite(*arg + varlen + 1);
2789 typval_T argvars[2];
2790
2791 argvars[0].v_type = VAR_UNKNOWN;
2792 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002793 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002794 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002795 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002796 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002797 if (*s == ')' && argvars[0].v_type == VAR_STRING
2798 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002799 {
2800 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2801
2802 *arg = s + 1;
2803 argvars[1].v_type = VAR_UNKNOWN;
2804 tv->v_type = VAR_NUMBER;
2805 tv->vval.v_number = 0;
2806 f_has(argvars, tv);
2807 clear_tv(&argvars[0]);
2808 ++ppconst->pp_used;
2809 return OK;
2810 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002811 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002812 }
2813
2814 if (generate_ppconst(cctx, ppconst) == FAIL)
2815 return FAIL;
2816
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817 if (varlen >= sizeof(namebuf))
2818 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002819 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820 return FAIL;
2821 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002822 vim_strncpy(namebuf, *arg, varlen);
2823 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824
2825 *arg = skipwhite(*arg + varlen + 1);
2826 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002827 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828
Bram Moolenaar03290b82020-12-19 16:30:44 +01002829 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002830 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 {
2832 int idx;
2833
2834 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002835 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002836 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002837 {
2838 if (STRCMP(name, "add") == 0 && argcount == 2)
2839 {
2840 garray_T *stack = &cctx->ctx_type_stack;
2841 type_T *type = ((type_T **)stack->ga_data)[
2842 stack->ga_len - 2];
2843
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002844 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002845 if (type->tt_type == VAR_LIST)
2846 {
2847 // inline "add(list, item)" so that the type can be checked
2848 res = generate_LISTAPPEND(cctx);
2849 idx = -1;
2850 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002851 else if (type->tt_type == VAR_BLOB)
2852 {
2853 // inline "add(blob, nr)" so that the type can be checked
2854 res = generate_BLOBAPPEND(cctx);
2855 idx = -1;
2856 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002857 }
2858
2859 if (idx >= 0)
2860 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2861 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002862 else
2863 semsg(_(e_unknownfunc), namebuf);
2864 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002865 }
2866
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002867 // An argument or local variable can be a function reference, this
2868 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002869 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002870 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002871 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002872 // If we can find the function by name generate the right call.
2873 // Skip global functions here, a local funcref takes precedence.
2874 ufunc = find_func(name, FALSE, cctx);
2875 if (ufunc != NULL && !func_is_global(ufunc))
2876 {
2877 res = generate_CALL(cctx, ufunc, argcount);
2878 goto theend;
2879 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002880 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881
2882 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002883 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002884 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002886 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002887 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002888 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002889 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01002890 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002891
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002892 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002893 goto theend;
2894 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002895
Bram Moolenaar0f769812020-09-12 18:32:34 +02002896 // If we can find a global function by name generate the right call.
2897 if (ufunc != NULL)
2898 {
2899 res = generate_CALL(cctx, ufunc, argcount);
2900 goto theend;
2901 }
2902
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002903 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002904 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002905 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002906 res = generate_UCALL(cctx, name, argcount);
2907 else
2908 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002909
2910theend:
2911 vim_free(tofree);
2912 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913}
2914
2915// like NAMESPACE_CHAR but with 'a' and 'l'.
2916#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2917
2918/*
2919 * Find the end of a variable or function name. Unlike find_name_end() this
2920 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002921 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922 * Return a pointer to just after the name. Equal to "arg" if there is no
2923 * valid name.
2924 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01002925 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002926to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927{
2928 char_u *p;
2929
2930 // Quick check for valid starting character.
2931 if (!eval_isnamec1(*arg))
2932 return arg;
2933
2934 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2935 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2936 // and can be used in slice "[n:]".
2937 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002938 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2940 break;
2941 return p;
2942}
2943
2944/*
2945 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002946 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 */
2948 char_u *
2949to_name_const_end(char_u *arg)
2950{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002951 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952 typval_T rettv;
2953
2954 if (p == arg && *arg == '[')
2955 {
2956
2957 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002958 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 p = arg;
2960 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 return p;
2962}
2963
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964/*
2965 * parse a list: [expr, expr]
2966 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002967 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 */
2969 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002970compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971{
2972 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002973 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002975 int is_const;
2976 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002978 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002980 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002981 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002982 semsg(_(e_list_end), *arg);
2983 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002984 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002985 if (*p == ',')
2986 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002987 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002988 return FAIL;
2989 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002990 if (*p == ']')
2991 {
2992 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002993 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002994 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002995 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002996 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002997 if (!is_const)
2998 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 ++count;
3000 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003001 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003003 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3004 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003005 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003006 return FAIL;
3007 }
3008 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003009 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 p = skipwhite(p);
3011 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003012 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003014 ppconst->pp_is_const = is_all_const;
3015 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016}
3017
3018/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003019 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 * "*arg" points to the '{'.
Bram Moolenaare462f522020-12-27 14:43:30 +01003021 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 */
3023 static int
3024compile_lambda(char_u **arg, cctx_T *cctx)
3025{
Bram Moolenaare462f522020-12-27 14:43:30 +01003026 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 typval_T rettv;
3028 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003029 evalarg_T evalarg;
3030
3031 CLEAR_FIELD(evalarg);
3032 evalarg.eval_flags = EVAL_EVALUATE;
3033 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034
3035 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003036 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3037 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003038 {
3039 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003040 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003041 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003042
Bram Moolenaar65c44152020-12-24 15:14:01 +01003043 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003045 ++ufunc->uf_refcount;
3046 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047
Bram Moolenaar65c44152020-12-24 15:14:01 +01003048 // Compile the function into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003049 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003051 clear_evalarg(&evalarg, NULL);
3052
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003053 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003054 {
3055 // The return type will now be known.
3056 set_function_type(ufunc);
3057
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003058 // The function reference count will be 1. When the ISN_FUNCREF
3059 // instruction is deleted the reference count is decremented and the
3060 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003061 return generate_FUNCREF(cctx, ufunc);
3062 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003063
3064 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 return FAIL;
3066}
3067
3068/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003069 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003071 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 */
3073 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003074compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075{
3076 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003077 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 int count = 0;
3079 dict_T *d = dict_alloc();
3080 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003081 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003082 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003083 int is_const;
3084 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085
3086 if (d == NULL)
3087 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003088 if (generate_ppconst(cctx, ppconst) == FAIL)
3089 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003090 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003092 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093
Bram Moolenaar23c55272020-06-21 16:58:13 +02003094 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003095 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003096 *arg = NULL;
3097 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003098 }
3099
3100 if (**arg == '}')
3101 break;
3102
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003103 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003105 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003107 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003108 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003109 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3112 if (isn->isn_type == ISN_PUSHS)
3113 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003114 else
3115 {
3116 type_T *keytype = ((type_T **)stack->ga_data)
Bram Moolenaar2bede172020-11-19 18:53:18 +01003117 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003118 if (need_type(keytype, &t_string, -1, cctx,
Bram Moolenaar2bede172020-11-19 18:53:18 +01003119 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003120 return FAIL;
3121 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003122 *arg = skipwhite(*arg);
3123 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003124 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003125 emsg(_(e_missing_matching_bracket_after_dict_key));
3126 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003127 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003128 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003130 else
3131 {
3132 // {"name": value},
3133 // {'name': value},
3134 // {name: value} use "name" as a literal key
3135 key = get_literal_key(arg);
3136 if (key == NULL)
3137 return FAIL;
3138 if (generate_PUSHS(cctx, key) == FAIL)
3139 return FAIL;
3140 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141
3142 // Check for duplicate keys, if using string keys.
3143 if (key != NULL)
3144 {
3145 item = dict_find(d, key, -1);
3146 if (item != NULL)
3147 {
3148 semsg(_(e_duplicate_key), key);
3149 goto failret;
3150 }
3151 item = dictitem_alloc(key);
3152 if (item != NULL)
3153 {
3154 item->di_tv.v_type = VAR_UNKNOWN;
3155 item->di_tv.v_lock = 0;
3156 if (dict_add(d, item) == FAIL)
3157 dictitem_free(item);
3158 }
3159 }
3160
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 if (**arg != ':')
3162 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003163 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003164 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003165 else
3166 semsg(_(e_missing_dict_colon), *arg);
3167 return FAIL;
3168 }
3169 whitep = *arg + 1;
3170 if (!IS_WHITE_OR_NUL(*whitep))
3171 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003172 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173 return FAIL;
3174 }
3175
Bram Moolenaar23c55272020-06-21 16:58:13 +02003176 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003177 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003178 *arg = NULL;
3179 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003180 }
3181
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003182 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003184 if (!is_const)
3185 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003186 ++count;
3187
Bram Moolenaar2c330432020-04-13 14:41:35 +02003188 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003189 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003190 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003191 *arg = NULL;
3192 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003193 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003194 if (**arg == '}')
3195 break;
3196 if (**arg != ',')
3197 {
3198 semsg(_(e_missing_dict_comma), *arg);
3199 goto failret;
3200 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003201 if (IS_WHITE_OR_NUL(*whitep))
3202 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003203 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003204 return FAIL;
3205 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003206 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003207 if (!IS_WHITE_OR_NUL(*whitep))
3208 {
3209 semsg(_(e_white_space_required_after_str), ",");
3210 return FAIL;
3211 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 *arg = skipwhite(*arg + 1);
3213 }
3214
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 *arg = *arg + 1;
3216
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003217 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003218 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003219 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003220 *arg += STRLEN(*arg);
3221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003223 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 return generate_NEWDICT(cctx, count);
3225
3226failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003227 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003228 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003229 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003230 *arg = (char_u *)"";
3231 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 dict_unref(d);
3233 return FAIL;
3234}
3235
3236/*
3237 * Compile "&option".
3238 */
3239 static int
3240compile_get_option(char_u **arg, cctx_T *cctx)
3241{
3242 typval_T rettv;
3243 char_u *start = *arg;
3244 int ret;
3245
3246 // parse the option and get the current value to get the type.
3247 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003248 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 if (ret == OK)
3250 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003251 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003252 char_u *name = vim_strnsave(start, *arg - start);
3253 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3254 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255
3256 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3257 vim_free(name);
3258 }
3259 clear_tv(&rettv);
3260
3261 return ret;
3262}
3263
3264/*
3265 * Compile "$VAR".
3266 */
3267 static int
3268compile_get_env(char_u **arg, cctx_T *cctx)
3269{
3270 char_u *start = *arg;
3271 int len;
3272 int ret;
3273 char_u *name;
3274
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 ++*arg;
3276 len = get_env_len(arg);
3277 if (len == 0)
3278 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003279 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280 return FAIL;
3281 }
3282
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003283 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 name = vim_strnsave(start, len + 1);
3285 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3286 vim_free(name);
3287 return ret;
3288}
3289
3290/*
3291 * Compile "@r".
3292 */
3293 static int
3294compile_get_register(char_u **arg, cctx_T *cctx)
3295{
3296 int ret;
3297
3298 ++*arg;
3299 if (**arg == NUL)
3300 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003301 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 return FAIL;
3303 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003304 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 {
3306 emsg_invreg(**arg);
3307 return FAIL;
3308 }
3309 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3310 ++*arg;
3311 return ret;
3312}
3313
3314/*
3315 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003316 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317 */
3318 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003319apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003321 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003322
3323 // this works from end to start
3324 while (p > start)
3325 {
3326 --p;
3327 if (*p == '-' || *p == '+')
3328 {
3329 // only '-' has an effect, for '+' we only check the type
3330#ifdef FEAT_FLOAT
3331 if (rettv->v_type == VAR_FLOAT)
3332 {
3333 if (*p == '-')
3334 rettv->vval.v_float = -rettv->vval.v_float;
3335 }
3336 else
3337#endif
3338 {
3339 varnumber_T val;
3340 int error = FALSE;
3341
3342 // tv_get_number_chk() accepts a string, but we don't want that
3343 // here
3344 if (check_not_string(rettv) == FAIL)
3345 return FAIL;
3346 val = tv_get_number_chk(rettv, &error);
3347 clear_tv(rettv);
3348 if (error)
3349 return FAIL;
3350 if (*p == '-')
3351 val = -val;
3352 rettv->v_type = VAR_NUMBER;
3353 rettv->vval.v_number = val;
3354 }
3355 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003356 else if (numeric_only)
3357 {
3358 ++p;
3359 break;
3360 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003361 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 {
3363 int v = tv2bool(rettv);
3364
3365 // '!' is permissive in the type.
3366 clear_tv(rettv);
3367 rettv->v_type = VAR_BOOL;
3368 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3369 }
3370 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003371 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003372 return OK;
3373}
3374
3375/*
3376 * Recognize v: variables that are constants and set "rettv".
3377 */
3378 static void
3379get_vim_constant(char_u **arg, typval_T *rettv)
3380{
3381 if (STRNCMP(*arg, "v:true", 6) == 0)
3382 {
3383 rettv->v_type = VAR_BOOL;
3384 rettv->vval.v_number = VVAL_TRUE;
3385 *arg += 6;
3386 }
3387 else if (STRNCMP(*arg, "v:false", 7) == 0)
3388 {
3389 rettv->v_type = VAR_BOOL;
3390 rettv->vval.v_number = VVAL_FALSE;
3391 *arg += 7;
3392 }
3393 else if (STRNCMP(*arg, "v:null", 6) == 0)
3394 {
3395 rettv->v_type = VAR_SPECIAL;
3396 rettv->vval.v_number = VVAL_NULL;
3397 *arg += 6;
3398 }
3399 else if (STRNCMP(*arg, "v:none", 6) == 0)
3400 {
3401 rettv->v_type = VAR_SPECIAL;
3402 rettv->vval.v_number = VVAL_NONE;
3403 *arg += 6;
3404 }
3405}
3406
Bram Moolenaar657137c2021-01-09 15:45:23 +01003407 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003408get_compare_type(char_u *p, int *len, int *type_is)
3409{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003410 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003411 int i;
3412
3413 switch (p[0])
3414 {
3415 case '=': if (p[1] == '=')
3416 type = EXPR_EQUAL;
3417 else if (p[1] == '~')
3418 type = EXPR_MATCH;
3419 break;
3420 case '!': if (p[1] == '=')
3421 type = EXPR_NEQUAL;
3422 else if (p[1] == '~')
3423 type = EXPR_NOMATCH;
3424 break;
3425 case '>': if (p[1] != '=')
3426 {
3427 type = EXPR_GREATER;
3428 *len = 1;
3429 }
3430 else
3431 type = EXPR_GEQUAL;
3432 break;
3433 case '<': if (p[1] != '=')
3434 {
3435 type = EXPR_SMALLER;
3436 *len = 1;
3437 }
3438 else
3439 type = EXPR_SEQUAL;
3440 break;
3441 case 'i': if (p[1] == 's')
3442 {
3443 // "is" and "isnot"; but not a prefix of a name
3444 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3445 *len = 5;
3446 i = p[*len];
3447 if (!isalnum(i) && i != '_')
3448 {
3449 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3450 *type_is = TRUE;
3451 }
3452 }
3453 break;
3454 }
3455 return type;
3456}
3457
3458/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003459 * Skip over an expression, ignoring most errors.
3460 */
3461 static void
3462skip_expr_cctx(char_u **arg, cctx_T *cctx)
3463{
3464 evalarg_T evalarg;
3465
3466 CLEAR_FIELD(evalarg);
3467 evalarg.eval_cctx = cctx;
3468 skip_expr(arg, &evalarg);
3469}
3470
3471/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003472 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003473 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 */
3475 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003476compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003477{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003478 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479
3480 // this works from end to start
3481 while (p > start)
3482 {
3483 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003484 while (VIM_ISWHITE(*p))
3485 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003486 if (*p == '-' || *p == '+')
3487 {
3488 int negate = *p == '-';
3489 isn_T *isn;
3490
3491 // TODO: check type
3492 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3493 {
3494 --p;
3495 if (*p == '-')
3496 negate = !negate;
3497 }
3498 // only '-' has an effect, for '+' we only check the type
3499 if (negate)
3500 isn = generate_instr(cctx, ISN_NEGATENR);
3501 else
3502 isn = generate_instr(cctx, ISN_CHECKNR);
3503 if (isn == NULL)
3504 return FAIL;
3505 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003506 else if (numeric_only)
3507 {
3508 ++p;
3509 break;
3510 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511 else
3512 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003513 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003515 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003516 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003517 if (p[-1] == '!')
3518 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 }
3521 if (generate_2BOOL(cctx, invert) == FAIL)
3522 return FAIL;
3523 }
3524 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003525 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003526 return OK;
3527}
3528
3529/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003530 * Compile "(expression)": recursive!
3531 * Return FAIL/OK.
3532 */
3533 static int
3534compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3535{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003536 int ret;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003537
3538 *arg = skipwhite(*arg + 1);
3539 if (ppconst->pp_used <= PPSIZE - 10)
3540 {
3541 ret = compile_expr1(arg, cctx, ppconst);
3542 }
3543 else
3544 {
3545 // Not enough space in ppconst, flush constants.
3546 if (generate_ppconst(cctx, ppconst) == FAIL)
3547 return FAIL;
3548 ret = compile_expr0(arg, cctx);
3549 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003550 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3551 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003552 if (**arg == ')')
3553 ++*arg;
3554 else if (ret == OK)
3555 {
3556 emsg(_(e_missing_close));
3557 ret = FAIL;
3558 }
3559 return ret;
3560}
3561
3562/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003563 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003564 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565 */
3566 static int
3567compile_subscript(
3568 char_u **arg,
3569 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003570 char_u *start_leader,
3571 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003572 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003574 char_u *name_start = *end_leader;
3575
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 for (;;)
3577 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003578 char_u *p = skipwhite(*arg);
3579
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003580 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003581 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003582 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003583
3584 // If a following line starts with "->{" or "->X" advance to that
3585 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003586 // Also if a following line starts with ".x".
3587 if (next != NULL &&
3588 ((next[0] == '-' && next[1] == '>'
3589 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003590 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003591 {
3592 next = next_line_from_context(cctx, TRUE);
3593 if (next == NULL)
3594 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003595 *arg = next;
3596 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003597 }
3598 }
3599
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003600 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003601 // not a function call.
3602 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003603 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003604 garray_T *stack = &cctx->ctx_type_stack;
3605 type_T *type;
3606 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003608 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003609 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003610 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003611
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003612 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003613 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3614
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003615 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003616 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3617 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003618 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619 return FAIL;
3620 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003621 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003623 char_u *pstart = p;
3624
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003625 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003626 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003627 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003628
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 // something->method()
3630 // Apply the '!', '-' and '+' first:
3631 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003632 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003635 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003636 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003637 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003638 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003639 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003640 int argcount = 1;
3641 char_u *expr;
3642 garray_T *stack;
3643 type_T *type;
3644
3645 // Funcref call: list->(Refs[2])(arg)
3646 // or lambda: list->((arg) => expr)(arg)
3647 // Fist compile the arguments.
3648 expr = *arg;
3649 *arg = skipwhite(*arg + 1);
3650 skip_expr_cctx(arg, cctx);
3651 *arg = skipwhite(*arg);
3652 if (**arg != ')')
3653 {
3654 semsg(_(e_missing_paren), *arg);
3655 return FAIL;
3656 }
3657 ++*arg;
3658 if (**arg != '(')
3659 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003660 if (*skipwhite(*arg) == '(')
3661 emsg(_(e_nowhitespace));
3662 else
3663 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01003664 return FAIL;
3665 }
3666
3667 *arg = skipwhite(*arg + 1);
3668 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3669 return FAIL;
3670
3671 // Compile the function expression.
3672 if (compile_parenthesis(&expr, cctx, ppconst) == FAIL)
3673 return FAIL;
3674
3675 stack = &cctx->ctx_type_stack;
3676 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3677 if (generate_PCALL(cctx, argcount,
3678 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 return FAIL;
3680 }
3681 else
3682 {
3683 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003684 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003685 if (!eval_isnamec1(*p))
3686 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003687 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003688 return FAIL;
3689 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003690 if (ASCII_ISALPHA(*p) && p[1] == ':')
3691 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003692 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003693 ;
3694 if (*p != '(')
3695 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003696 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 return FAIL;
3698 }
3699 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003700 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701 return FAIL;
3702 }
3703 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003704 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003706 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003707 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003708 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003709 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003710 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003711
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003712 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003713 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003714 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003715 // TODO: blob index
3716 // TODO: more arguments
3717 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003718 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003719 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003720 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003721
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003722 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003723 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003724 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003725 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003726 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003727 // missing first index is equal to zero
3728 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003729 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003730 else
3731 {
3732 if (compile_expr0(arg, cctx) == FAIL)
3733 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003734 if (**arg == ':')
3735 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003736 semsg(_(e_white_space_required_before_and_after_str_at_str),
3737 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003738 return FAIL;
3739 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003740 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003741 return FAIL;
3742 *arg = skipwhite(*arg);
3743 }
3744 if (**arg == ':')
3745 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003746 is_slice = TRUE;
3747 ++*arg;
3748 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
3749 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003750 semsg(_(e_white_space_required_before_and_after_str_at_str),
3751 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003752 return FAIL;
3753 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003754 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003755 return FAIL;
3756 if (**arg == ']')
3757 // missing second index is equal to end of string
3758 generate_PUSHNR(cctx, -1);
3759 else
3760 {
3761 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003762 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003763 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003764 return FAIL;
3765 *arg = skipwhite(*arg);
3766 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003767 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768
3769 if (**arg != ']')
3770 {
3771 emsg(_(e_missbrac));
3772 return FAIL;
3773 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003774 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003775
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003776 // We can index a list and a dict. If we don't know the type
3777 // we can use the index value type.
3778 // TODO: If we don't know use an instruction to figure it out at
3779 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003780 typep = ((type_T **)stack->ga_data) + stack->ga_len
3781 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003782 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003783 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3784 // If the index is a string, the variable must be a Dict.
3785 if (*typep == &t_any && valtype == &t_string)
3786 vtype = VAR_DICT;
3787 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003788 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003789 if (need_type(valtype, &t_number, -1, cctx,
3790 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003791 return FAIL;
3792 if (is_slice)
3793 {
3794 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003795 if (need_type(valtype, &t_number, -2, cctx,
3796 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003797 return FAIL;
3798 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003799 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003800
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003801 if (vtype == VAR_DICT)
3802 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003803 if (is_slice)
3804 {
3805 emsg(_(e_cannot_slice_dictionary));
3806 return FAIL;
3807 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003808 if ((*typep)->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003809 {
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003810 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003811 if (*typep == &t_unknown)
3812 // empty dict was used
3813 *typep = &t_any;
3814 }
Bram Moolenaar7892b952020-07-20 22:09:34 +02003815 else
3816 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003817 if (need_type(*typep, &t_dict_any, -2, cctx,
3818 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003819 return FAIL;
3820 *typep = &t_any;
3821 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003822 if (may_generate_2STRING(-1, cctx) == FAIL)
3823 return FAIL;
3824 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3825 return FAIL;
3826 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003827 else if (vtype == VAR_STRING)
3828 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003829 *typep = &t_string;
3830 if ((is_slice
3831 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3832 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003833 return FAIL;
3834 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003835 else if (vtype == VAR_BLOB)
3836 {
3837 emsg("Sorry, blob index and slice not implemented yet");
3838 return FAIL;
3839 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003840 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003841 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003842 if (is_slice)
3843 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003844 if (generate_instr_drop(cctx,
3845 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3846 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003847 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003848 }
Bram Moolenaared591872020-08-15 22:14:53 +02003849 else
3850 {
3851 if ((*typep)->tt_type == VAR_LIST)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003852 {
Bram Moolenaared591872020-08-15 22:14:53 +02003853 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003854 if (*typep == &t_unknown)
3855 // empty list was used
3856 *typep = &t_any;
3857 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003858 if (generate_instr_drop(cctx,
3859 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3860 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003861 return FAIL;
3862 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003863 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003864 else
3865 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003866 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003867 return FAIL;
3868 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003870 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003872 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003873 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003874 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003875 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003876
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003877 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003878 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003879 {
3880 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003881 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003882 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003883 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003884 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 while (eval_isnamec(*p))
3886 MB_PTR_ADV(p);
3887 if (p == *arg)
3888 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003889 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003890 return FAIL;
3891 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003892 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893 return FAIL;
3894 *arg = p;
3895 }
3896 else
3897 break;
3898 }
3899
3900 // TODO - see handle_subscript():
3901 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3902 // Don't do this when "Func" is already a partial that was bound
3903 // explicitly (pt_auto is FALSE).
3904
3905 return OK;
3906}
3907
3908/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003909 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3910 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003912 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003913 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003914 *
3915 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 */
3917
3918/*
3919 * number number constant
3920 * 0zFFFFFFFF Blob constant
3921 * "string" string constant
3922 * 'string' literal string constant
3923 * &option-name option value
3924 * @r register contents
3925 * identifier variable value
3926 * function() function call
3927 * $VAR environment variable
3928 * (expression) nested expression
3929 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01003930 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931 *
3932 * Also handle:
3933 * ! in front logical NOT
3934 * - in front unary minus
3935 * + in front unary plus (ignored)
3936 * trailing (arg) funcref/partial call
3937 * trailing [] subscript in String or List
3938 * trailing .name entry in Dictionary
3939 * trailing ->name() method call
3940 */
3941 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003942compile_expr7(
3943 char_u **arg,
3944 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003945 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947 char_u *start_leader, *end_leader;
3948 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003949 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003950 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003952 ppconst->pp_is_const = FALSE;
3953
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 /*
3955 * Skip '!', '-' and '+' characters. They are handled later.
3956 */
3957 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003958 if (eval_leader(arg, TRUE) == FAIL)
3959 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003960 end_leader = *arg;
3961
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003962 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 switch (**arg)
3964 {
3965 /*
3966 * Number constant.
3967 */
3968 case '0': // also for blob starting with 0z
3969 case '1':
3970 case '2':
3971 case '3':
3972 case '4':
3973 case '5':
3974 case '6':
3975 case '7':
3976 case '8':
3977 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003978 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003980 // Apply "-" and "+" just before the number now, right to
3981 // left. Matters especially when "->" follows. Stops at
3982 // '!'.
3983 if (apply_leader(rettv, TRUE,
3984 start_leader, &end_leader) == FAIL)
3985 {
3986 clear_tv(rettv);
3987 return FAIL;
3988 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989 break;
3990
3991 /*
3992 * String constant: "string".
3993 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003994 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003995 return FAIL;
3996 break;
3997
3998 /*
3999 * Literal string constant: 'str''ing'.
4000 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004001 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004002 return FAIL;
4003 break;
4004
4005 /*
4006 * Constant Vim variable.
4007 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004008 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009 ret = NOTDONE;
4010 break;
4011
4012 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004013 * "true" constant
4014 */
4015 case 't': if (STRNCMP(*arg, "true", 4) == 0
4016 && !eval_isnamec((*arg)[4]))
4017 {
4018 *arg += 4;
4019 rettv->v_type = VAR_BOOL;
4020 rettv->vval.v_number = VVAL_TRUE;
4021 }
4022 else
4023 ret = NOTDONE;
4024 break;
4025
4026 /*
4027 * "false" constant
4028 */
4029 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4030 && !eval_isnamec((*arg)[5]))
4031 {
4032 *arg += 5;
4033 rettv->v_type = VAR_BOOL;
4034 rettv->vval.v_number = VVAL_FALSE;
4035 }
4036 else
4037 ret = NOTDONE;
4038 break;
4039
4040 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004041 * "null" constant
4042 */
4043 case 'n': if (STRNCMP(*arg, "null", 4) == 0
4044 && !eval_isnamec((*arg)[5]))
4045 {
4046 *arg += 4;
4047 rettv->v_type = VAR_SPECIAL;
4048 rettv->vval.v_number = VVAL_NULL;
4049 }
4050 else
4051 ret = NOTDONE;
4052 break;
4053
4054 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055 * List: [expr, expr]
4056 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004057 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058 break;
4059
4060 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061 * Dictionary: {'key': val, 'key': val}
4062 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004063 case '{': ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064 break;
4065
4066 /*
4067 * Option value: &name
4068 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004069 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4070 return FAIL;
4071 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004072 break;
4073
4074 /*
4075 * Environment variable: $VAR.
4076 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004077 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4078 return FAIL;
4079 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004080 break;
4081
4082 /*
4083 * Register contents: @r.
4084 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004085 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4086 return FAIL;
4087 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004088 break;
4089 /*
4090 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004091 * lambda: (arg, arg) => expr
4092 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004094 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4095 ret = compile_lambda(arg, cctx);
4096 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004097 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098 break;
4099
4100 default: ret = NOTDONE;
4101 break;
4102 }
4103 if (ret == FAIL)
4104 return FAIL;
4105
Bram Moolenaar1c747212020-05-09 18:28:34 +02004106 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004108 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004109 clear_tv(rettv);
4110 else
4111 // A constant expression can possibly be handled compile time,
4112 // return the value instead of generating code.
4113 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004114 }
4115 else if (ret == NOTDONE)
4116 {
4117 char_u *p;
4118 int r;
4119
4120 if (!eval_isnamec1(**arg))
4121 {
Bram Moolenaare4984292020-12-13 14:19:25 +01004122 if (ends_excmd(*skipwhite(*arg)))
4123 semsg(_(e_empty_expression_str), *arg);
4124 else
4125 semsg(_(e_name_expected_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126 return FAIL;
4127 }
4128
4129 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004130 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004131 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004132 {
4133 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4134 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004135 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004136 {
4137 if (generate_ppconst(cctx, ppconst) == FAIL)
4138 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004139 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004140 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004141 if (r == FAIL)
4142 return FAIL;
4143 }
4144
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004145 // Handle following "[]", ".member", etc.
4146 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004147 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004148 ppconst) == FAIL)
4149 return FAIL;
4150 if (ppconst->pp_used > 0)
4151 {
4152 // apply the '!', '-' and '+' before the constant
4153 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004154 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004155 return FAIL;
4156 return OK;
4157 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004158 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004160 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161}
4162
4163/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004164 * Give the "white on both sides" error, taking the operator from "p[len]".
4165 */
4166 void
4167error_white_both(char_u *op, int len)
4168{
4169 char_u buf[10];
4170
4171 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004172 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004173}
4174
4175/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004176 * <type>expr7: runtime type check / conversion
4177 */
4178 static int
4179compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4180{
4181 type_T *want_type = NULL;
4182
4183 // Recognize <type>
4184 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4185 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004186 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004187 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4188 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004189 return FAIL;
4190
4191 if (**arg != '>')
4192 {
4193 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004194 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004195 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004196 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004197 return FAIL;
4198 }
4199 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004200 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004201 return FAIL;
4202 }
4203
4204 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4205 return FAIL;
4206
4207 if (want_type != NULL)
4208 {
4209 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004210 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004211
Bram Moolenaard1103582020-08-14 22:44:25 +02004212 generate_ppconst(cctx, ppconst);
4213 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02004214 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004215 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004216 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004217 return FAIL;
4218 }
4219 }
4220
4221 return OK;
4222}
4223
4224/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004225 * * number multiplication
4226 * / number division
4227 * % number modulo
4228 */
4229 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004230compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231{
4232 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004233 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004234 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004235
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004236 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004237 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004238 return FAIL;
4239
4240 /*
4241 * Repeat computing, until no "*", "/" or "%" is following.
4242 */
4243 for (;;)
4244 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004245 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004246 if (*op != '*' && *op != '/' && *op != '%')
4247 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004248 if (next != NULL)
4249 {
4250 *arg = next_line_from_context(cctx, TRUE);
4251 op = skipwhite(*arg);
4252 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004253
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004254 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004256 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004257 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004258 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004259 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004260 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004261
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004262 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004263 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004265
4266 if (ppconst->pp_used == ppconst_used + 2
4267 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4268 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004269 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004270 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4271 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004272 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004274 // both are numbers: compute the result
4275 switch (*op)
4276 {
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;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004281 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004282 break;
4283 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004284 tv1->vval.v_number = res;
4285 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004286 }
4287 else
4288 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004289 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004290 generate_two_op(cctx, op);
4291 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004292 }
4293
4294 return OK;
4295}
4296
4297/*
4298 * + number addition
4299 * - number subtraction
4300 * .. string concatenation
4301 */
4302 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004303compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304{
4305 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004306 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004308 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004309
4310 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004311 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312 return FAIL;
4313
4314 /*
4315 * Repeat computing, until no "+", "-" or ".." is following.
4316 */
4317 for (;;)
4318 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004319 op = may_peek_next_line(cctx, *arg, &next);
4320 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321 break;
4322 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004323 if (next != NULL)
4324 {
4325 *arg = next_line_from_context(cctx, TRUE);
4326 op = skipwhite(*arg);
4327 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004329 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004331 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004332 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004333 }
4334
Bram Moolenaare0de1712020-12-02 17:36:54 +01004335 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004336 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004337
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004338 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004339 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 return FAIL;
4341
Bram Moolenaara5565e42020-05-09 15:44:01 +02004342 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004343 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004344 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4345 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4346 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4347 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004349 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4350 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004351
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004352 // concat/subtract/add constant numbers
4353 if (*op == '+')
4354 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4355 else if (*op == '-')
4356 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4357 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004358 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004359 // concatenate constant strings
4360 char_u *s1 = tv1->vval.v_string;
4361 char_u *s2 = tv2->vval.v_string;
4362 size_t len1 = STRLEN(s1);
4363
4364 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4365 if (tv1->vval.v_string == NULL)
4366 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004367 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004368 return FAIL;
4369 }
4370 mch_memmove(tv1->vval.v_string, s1, len1);
4371 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004372 vim_free(s1);
4373 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004374 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004375 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 }
4377 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004378 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004379 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004380 if (*op == '.')
4381 {
4382 if (may_generate_2STRING(-2, cctx) == FAIL
4383 || may_generate_2STRING(-1, cctx) == FAIL)
4384 return FAIL;
4385 generate_instr_drop(cctx, ISN_CONCAT, 1);
4386 }
4387 else
4388 generate_two_op(cctx, op);
4389 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390 }
4391
4392 return OK;
4393}
4394
4395/*
4396 * expr5a == expr5b
4397 * expr5a =~ expr5b
4398 * expr5a != expr5b
4399 * expr5a !~ expr5b
4400 * expr5a > expr5b
4401 * expr5a >= expr5b
4402 * expr5a < expr5b
4403 * expr5a <= expr5b
4404 * expr5a is expr5b
4405 * expr5a isnot expr5b
4406 *
4407 * Produces instructions:
4408 * EVAL expr5a Push result of "expr5a"
4409 * EVAL expr5b Push result of "expr5b"
4410 * COMPARE one of the compare instructions
4411 */
4412 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004413compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004415 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004417 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004420 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421
4422 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004423 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424 return FAIL;
4425
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004426 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004427 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004428
4429 /*
4430 * If there is a comparative operator, use it.
4431 */
4432 if (type != EXPR_UNKNOWN)
4433 {
4434 int ic = FALSE; // Default: do not ignore case
4435
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004436 if (next != NULL)
4437 {
4438 *arg = next_line_from_context(cctx, TRUE);
4439 p = skipwhite(*arg);
4440 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441 if (type_is && (p[len] == '?' || p[len] == '#'))
4442 {
4443 semsg(_(e_invexpr2), *arg);
4444 return FAIL;
4445 }
4446 // extra question mark appended: ignore case
4447 if (p[len] == '?')
4448 {
4449 ic = TRUE;
4450 ++len;
4451 }
4452 // extra '#' appended: match case (ignored)
4453 else if (p[len] == '#')
4454 ++len;
4455 // nothing appended: match case
4456
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004457 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004458 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004459 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004460 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004461 }
4462
4463 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004464 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004465 return FAIL;
4466
Bram Moolenaara5565e42020-05-09 15:44:01 +02004467 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468 return FAIL;
4469
Bram Moolenaara5565e42020-05-09 15:44:01 +02004470 if (ppconst->pp_used == ppconst_used + 2)
4471 {
4472 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4473 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4474 int ret;
4475
4476 // Both sides are a constant, compute the result now.
4477 // First check for a valid combination of types, this is more
4478 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004479 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004480 ret = FAIL;
4481 else
4482 {
4483 ret = typval_compare(tv1, tv2, type, ic);
4484 tv1->v_type = VAR_BOOL;
4485 tv1->vval.v_number = tv1->vval.v_number
4486 ? VVAL_TRUE : VVAL_FALSE;
4487 clear_tv(tv2);
4488 --ppconst->pp_used;
4489 }
4490 return ret;
4491 }
4492
4493 generate_ppconst(cctx, ppconst);
4494 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004495 }
4496
4497 return OK;
4498}
4499
Bram Moolenaar7f141552020-05-09 17:35:53 +02004500static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4501
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004502/*
4503 * Compile || or &&.
4504 */
4505 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004506compile_and_or(
4507 char_u **arg,
4508 cctx_T *cctx,
4509 char *op,
4510 ppconst_T *ppconst,
4511 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004513 char_u *next;
4514 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004515 int opchar = *op;
4516
4517 if (p[0] == opchar && p[1] == opchar)
4518 {
4519 garray_T *instr = &cctx->ctx_instr;
4520 garray_T end_ga;
4521
4522 /*
4523 * Repeat until there is no following "||" or "&&"
4524 */
4525 ga_init2(&end_ga, sizeof(int), 10);
4526 while (p[0] == opchar && p[1] == opchar)
4527 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004528 if (next != NULL)
4529 {
4530 *arg = next_line_from_context(cctx, TRUE);
4531 p = skipwhite(*arg);
4532 }
4533
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004534 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4535 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004536 semsg(_(e_white_space_required_before_and_after_str_at_str),
4537 op, *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004538 return FAIL;
4539 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004540
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004541 // TODO: use ppconst if the value is a constant and check
4542 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004543 generate_ppconst(cctx, ppconst);
4544
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004545 // Every part must evaluate to a bool.
4546 if (bool_on_stack(cctx) == FAIL)
4547 {
4548 ga_clear(&end_ga);
4549 return FAIL;
4550 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004551
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552 if (ga_grow(&end_ga, 1) == FAIL)
4553 {
4554 ga_clear(&end_ga);
4555 return FAIL;
4556 }
4557 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4558 ++end_ga.ga_len;
4559 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004560 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004561
4562 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004563 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004564 {
4565 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004566 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004567 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004568
Bram Moolenaara5565e42020-05-09 15:44:01 +02004569 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4570 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571 {
4572 ga_clear(&end_ga);
4573 return FAIL;
4574 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004575
4576 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004577 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004578 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004579
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004580 // Every part must evaluate to a bool.
4581 if (bool_on_stack(cctx) == FAIL)
4582 {
4583 ga_clear(&end_ga);
4584 return FAIL;
4585 }
4586
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587 // Fill in the end label in all jumps.
4588 while (end_ga.ga_len > 0)
4589 {
4590 isn_T *isn;
4591
4592 --end_ga.ga_len;
4593 isn = ((isn_T *)instr->ga_data)
4594 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4595 isn->isn_arg.jump.jump_where = instr->ga_len;
4596 }
4597 ga_clear(&end_ga);
4598 }
4599
4600 return OK;
4601}
4602
4603/*
4604 * expr4a && expr4a && expr4a logical AND
4605 *
4606 * Produces instructions:
4607 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004608 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004609 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004611 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004612 * EVAL expr4c Push result of "expr4c"
4613 * end:
4614 */
4615 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004616compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004617{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004618 int ppconst_used = ppconst->pp_used;
4619
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004620 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004621 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622 return FAIL;
4623
4624 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004625 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004626}
4627
4628/*
4629 * expr3a || expr3b || expr3c logical OR
4630 *
4631 * Produces instructions:
4632 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004633 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004634 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004636 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637 * EVAL expr3c Push result of "expr3c"
4638 * end:
4639 */
4640 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004641compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004643 int ppconst_used = ppconst->pp_used;
4644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004645 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004646 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004647 return FAIL;
4648
4649 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004650 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004651}
4652
4653/*
4654 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004655 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004656 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004657 * JUMP_IF_FALSE alt jump if false
4658 * EVAL expr1a
4659 * JUMP_ALWAYS end
4660 * alt: EVAL expr1b
4661 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004662 *
4663 * Toplevel expression: expr2 ?? expr1
4664 * Produces instructions:
4665 * EVAL expr2 Push result of "expr2"
4666 * JUMP_AND_KEEP_IF_TRUE end jump if true
4667 * EVAL expr1
4668 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004669 */
4670 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004671compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004672{
4673 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004674 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004675 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004676
Bram Moolenaar3988f642020-08-27 22:43:03 +02004677 // Ignore all kinds of errors when not producing code.
4678 if (cctx->ctx_skip == SKIP_YES)
4679 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004680 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004681 return OK;
4682 }
4683
Bram Moolenaar61a89812020-05-07 16:58:17 +02004684 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004685 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004686 return FAIL;
4687
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004688 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004689 if (*p == '?')
4690 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004691 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004692 garray_T *instr = &cctx->ctx_instr;
4693 garray_T *stack = &cctx->ctx_type_stack;
4694 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004695 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004696 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004697 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004698 int has_const_expr = FALSE;
4699 int const_value = FALSE;
4700 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004701
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004702 if (next != NULL)
4703 {
4704 *arg = next_line_from_context(cctx, TRUE);
4705 p = skipwhite(*arg);
4706 }
4707
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004708 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004709 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004710 semsg(_(e_white_space_required_before_and_after_str_at_str),
4711 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004712 return FAIL;
4713 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004714
Bram Moolenaara5565e42020-05-09 15:44:01 +02004715 if (ppconst->pp_used == ppconst_used + 1)
4716 {
4717 // the condition is a constant, we know whether the ? or the :
4718 // expression is to be evaluated.
4719 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004720 if (op_falsy)
4721 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4722 else
4723 {
4724 int error = FALSE;
4725
4726 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4727 &error);
4728 if (error)
4729 return FAIL;
4730 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004731 cctx->ctx_skip = save_skip == SKIP_YES ||
4732 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4733
4734 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4735 // "left ?? right" and "left" is truthy: produce "left"
4736 generate_ppconst(cctx, ppconst);
4737 else
4738 {
4739 clear_tv(&ppconst->pp_tv[ppconst_used]);
4740 --ppconst->pp_used;
4741 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004742 }
4743 else
4744 {
4745 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004746 if (op_falsy)
4747 end_idx = instr->ga_len;
4748 generate_JUMP(cctx, op_falsy
4749 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4750 if (op_falsy)
4751 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004752 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004753
4754 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01004755 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004756 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004757 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004758 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004759
Bram Moolenaara5565e42020-05-09 15:44:01 +02004760 if (!has_const_expr)
4761 {
4762 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004763
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004764 if (!op_falsy)
4765 {
4766 // remember the type and drop it
4767 --stack->ga_len;
4768 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004769
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004770 end_idx = instr->ga_len;
4771 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004772
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004773 // jump here from JUMP_IF_FALSE
4774 isn = ((isn_T *)instr->ga_data) + alt_idx;
4775 isn->isn_arg.jump.jump_where = instr->ga_len;
4776 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004777 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004778
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004779 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004780 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004781 // Check for the ":".
4782 p = may_peek_next_line(cctx, *arg, &next);
4783 if (*p != ':')
4784 {
4785 emsg(_(e_missing_colon));
4786 return FAIL;
4787 }
4788 if (next != NULL)
4789 {
4790 *arg = next_line_from_context(cctx, TRUE);
4791 p = skipwhite(*arg);
4792 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004793
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004794 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4795 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004796 semsg(_(e_white_space_required_before_and_after_str_at_str),
4797 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004798 return FAIL;
4799 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004800
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004801 // evaluate the third expression
4802 if (has_const_expr)
4803 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004804 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01004805 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004806 return FAIL;
4807 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4808 return FAIL;
4809 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004810
Bram Moolenaara5565e42020-05-09 15:44:01 +02004811 if (!has_const_expr)
4812 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004813 type_T **typep;
4814
Bram Moolenaara5565e42020-05-09 15:44:01 +02004815 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004816
Bram Moolenaara5565e42020-05-09 15:44:01 +02004817 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004818 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4819 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004820
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004821 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004822 isn = ((isn_T *)instr->ga_data) + end_idx;
4823 isn->isn_arg.jump.jump_where = instr->ga_len;
4824 }
4825
4826 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004827 }
4828 return OK;
4829}
4830
4831/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004832 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004833 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4834 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004835 */
4836 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004837compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004838{
4839 ppconst_T ppconst;
4840
4841 CLEAR_FIELD(ppconst);
4842 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4843 {
4844 clear_ppconst(&ppconst);
4845 return FAIL;
4846 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004847 if (is_const != NULL)
4848 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004849 if (generate_ppconst(cctx, &ppconst) == FAIL)
4850 return FAIL;
4851 return OK;
4852}
4853
4854/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004855 * Toplevel expression.
4856 */
4857 static int
4858compile_expr0(char_u **arg, cctx_T *cctx)
4859{
4860 return compile_expr0_ext(arg, cctx, NULL);
4861}
4862
4863/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004864 * compile "return [expr]"
4865 */
4866 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004867compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004868{
4869 char_u *p = arg;
4870 garray_T *stack = &cctx->ctx_type_stack;
4871 type_T *stack_type;
4872
4873 if (*p != NUL && *p != '|' && *p != '\n')
4874 {
4875 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004876 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 return NULL;
4878
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004879 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004880 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004881 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01004882 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01004883 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
4884 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004885 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004886 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004887 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004888 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004889 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004890 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4891 && stack_type->tt_type != VAR_VOID
4892 && stack_type->tt_type != VAR_UNKNOWN)
4893 {
4894 emsg(_(e_returning_value_in_function_without_return_type));
4895 return NULL;
4896 }
4897 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004898 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004899 return NULL;
4900 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02004901 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004902 }
4903 else
4904 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004905 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004906 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004907 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4908 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004909 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004910 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004911 return NULL;
4912 }
4913
4914 // No argument, return zero.
4915 generate_PUSHNR(cctx, 0);
4916 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004917 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004918 return NULL;
4919
4920 // "return val | endif" is possible
4921 return skipwhite(p);
4922}
4923
4924/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004925 * Get a line from the compilation context, compatible with exarg_T getline().
4926 * Return a pointer to the line in allocated memory.
4927 * Return NULL for end-of-file or some error.
4928 */
4929 static char_u *
4930exarg_getline(
4931 int c UNUSED,
4932 void *cookie,
4933 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004934 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004935{
4936 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004937 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004938
Bram Moolenaar66250c92020-08-20 15:02:42 +02004939 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004940 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004941 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004942 return NULL;
4943 ++cctx->ctx_lnum;
4944 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4945 // Comment lines result in NULL pointers, skip them.
4946 if (p != NULL)
4947 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004948 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004949}
4950
4951/*
4952 * Compile a nested :def command.
4953 */
4954 static char_u *
4955compile_nested_function(exarg_T *eap, cctx_T *cctx)
4956{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004957 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004958 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004959 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004960 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004961 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004962 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004963
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004964 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004965 {
4966 emsg(_(e_cannot_use_bang_with_nested_def));
4967 return NULL;
4968 }
4969
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004970 if (*name_start == '/')
4971 {
4972 name_end = skip_regexp(name_start + 1, '/', TRUE);
4973 if (*name_end == '/')
4974 ++name_end;
4975 eap->nextcmd = check_nextcmd(name_end);
4976 }
4977 if (name_end == name_start || *skipwhite(name_end) != '(')
4978 {
4979 if (!ends_excmd2(name_start, name_end))
4980 {
4981 semsg(_(e_invalid_command_str), eap->cmd);
4982 return NULL;
4983 }
4984
4985 // "def" or "def Name": list functions
4986 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
4987 return NULL;
4988 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
4989 }
4990
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004991 // Only g:Func() can use a namespace.
4992 if (name_start[1] == ':' && !is_global)
4993 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004994 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004995 return NULL;
4996 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004997 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4998 return NULL;
4999
Bram Moolenaar04b12692020-05-04 23:24:44 +02005000 eap->arg = name_end;
5001 eap->getline = exarg_getline;
5002 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005003 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005004 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005005 lambda_name = vim_strsave(get_lambda_name());
5006 if (lambda_name == NULL)
5007 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005008 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005009
Bram Moolenaar822ba242020-05-24 23:00:18 +02005010 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005011 {
5012 r = eap->skip ? OK : FAIL;
5013 goto theend;
5014 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005015 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02005016 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005017 {
5018 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005019 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005020 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005021
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005022 if (is_global)
5023 {
5024 char_u *func_name = vim_strnsave(name_start + 2,
5025 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005026
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005027 if (func_name == NULL)
5028 r = FAIL;
5029 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005030 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005031 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005032 lambda_name = NULL;
5033 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005034 }
5035 else
5036 {
5037 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005038 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005039 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005040 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02005041
Bram Moolenaareef21022020-08-01 22:16:43 +02005042 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005043 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005044 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005045 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005046 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005047
5048 // copy over the block scope IDs
5049 if (block_depth > 0)
5050 {
5051 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5052 if (ufunc->uf_block_ids != NULL)
5053 {
5054 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5055 sizeof(int) * block_depth);
5056 ufunc->uf_block_depth = block_depth;
5057 }
5058 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005059 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005060 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005061 r = OK;
5062
5063theend:
5064 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005065 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005066}
5067
5068/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005069 * Return the length of an assignment operator, or zero if there isn't one.
5070 */
5071 int
5072assignment_len(char_u *p, int *heredoc)
5073{
5074 if (*p == '=')
5075 {
5076 if (p[1] == '<' && p[2] == '<')
5077 {
5078 *heredoc = TRUE;
5079 return 3;
5080 }
5081 return 1;
5082 }
5083 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5084 return 2;
5085 if (STRNCMP(p, "..=", 3) == 0)
5086 return 3;
5087 return 0;
5088}
5089
5090// words that cannot be used as a variable
5091static char *reserved[] = {
5092 "true",
5093 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005094 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005095 NULL
5096};
5097
Bram Moolenaar752fc692021-01-04 21:57:11 +01005098// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005099typedef enum {
5100 dest_local,
5101 dest_option,
5102 dest_env,
5103 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005104 dest_buffer,
5105 dest_window,
5106 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005107 dest_vimvar,
5108 dest_script,
5109 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005110 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005111} assign_dest_T;
5112
Bram Moolenaar752fc692021-01-04 21:57:11 +01005113// Used by compile_lhs() to store information about the LHS of an assignment
5114// and one argument of ":unlet" with an index.
5115typedef struct {
5116 assign_dest_T lhs_dest; // type of destination
5117
5118 char_u *lhs_name; // allocated name including
5119 // "[expr]" or ".name".
5120 size_t lhs_varlen; // length of the variable without
5121 // "[expr]" or ".name"
5122 char_u *lhs_dest_end; // end of the destination, including
5123 // "[expr]" or ".name".
5124
5125 int lhs_has_index; // has "[expr]" or ".name"
5126
5127 int lhs_new_local; // create new local variable
5128 int lhs_opt_flags; // for when destination is an option
5129 int lhs_vimvaridx; // for when destination is a v:var
5130
5131 lvar_T lhs_local_lvar; // used for existing local destination
5132 lvar_T lhs_arg_lvar; // used for argument destination
5133 lvar_T *lhs_lvar; // points to destination lvar
5134 int lhs_scriptvar_sid;
5135 int lhs_scriptvar_idx;
5136
5137 int lhs_has_type; // type was specified
5138 type_T *lhs_type;
5139 type_T *lhs_member_type;
5140} lhs_T;
5141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005142/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005143 * Generate the load instruction for "name".
5144 */
5145 static void
5146generate_loadvar(
5147 cctx_T *cctx,
5148 assign_dest_T dest,
5149 char_u *name,
5150 lvar_T *lvar,
5151 type_T *type)
5152{
5153 switch (dest)
5154 {
5155 case dest_option:
5156 // TODO: check the option exists
5157 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5158 break;
5159 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005160 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5161 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5162 else
5163 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005164 break;
5165 case dest_buffer:
5166 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5167 break;
5168 case dest_window:
5169 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5170 break;
5171 case dest_tab:
5172 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5173 break;
5174 case dest_script:
5175 compile_load_scriptvar(cctx,
5176 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5177 break;
5178 case dest_env:
5179 // Include $ in the name here
5180 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5181 break;
5182 case dest_reg:
5183 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5184 break;
5185 case dest_vimvar:
5186 generate_LOADV(cctx, name + 2, TRUE);
5187 break;
5188 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005189 if (lvar->lv_from_outer > 0)
5190 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5191 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005192 else
5193 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5194 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005195 case dest_expr:
5196 // list or dict value should already be on the stack.
5197 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005198 }
5199}
5200
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005201/*
5202 * Skip over "[expr]" or ".member".
5203 * Does not check for any errors.
5204 */
5205 static char_u *
5206skip_index(char_u *start)
5207{
5208 char_u *p = start;
5209
5210 if (*p == '[')
5211 {
5212 p = skipwhite(p + 1);
5213 (void)skip_expr(&p, NULL);
5214 p = skipwhite(p);
5215 if (*p == ']')
5216 return p + 1;
5217 return p;
5218 }
5219 // if (*p == '.')
5220 return to_name_end(p + 1, TRUE);
5221}
5222
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005223 void
5224vim9_declare_error(char_u *name)
5225{
5226 char *scope = "";
5227
5228 switch (*name)
5229 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005230 case 'g': scope = _("global"); break;
5231 case 'b': scope = _("buffer"); break;
5232 case 'w': scope = _("window"); break;
5233 case 't': scope = _("tab"); break;
5234 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005235 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005236 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005237 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005238 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005239 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005240 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005241 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005242 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005243 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005244}
5245
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005246/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005247 * For one assignment figure out the type of destination. Return it in "dest".
5248 * When not recognized "dest" is not set.
5249 * For an option "opt_flags" is set.
5250 * For a v:var "vimvaridx" is set.
5251 * "type" is set to the destination type if known, unchanted otherwise.
5252 * Return FAIL if an error message was given.
5253 */
5254 static int
5255get_var_dest(
5256 char_u *name,
5257 assign_dest_T *dest,
5258 int cmdidx,
5259 int *opt_flags,
5260 int *vimvaridx,
5261 type_T **type,
5262 cctx_T *cctx)
5263{
5264 char_u *p;
5265
5266 if (*name == '&')
5267 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005268 int cc;
5269 long numval;
5270 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005271
5272 *dest = dest_option;
5273 if (cmdidx == CMD_final || cmdidx == CMD_const)
5274 {
5275 emsg(_(e_const_option));
5276 return FAIL;
5277 }
5278 p = name;
5279 p = find_option_end(&p, opt_flags);
5280 if (p == NULL)
5281 {
5282 // cannot happen?
5283 emsg(_(e_letunexp));
5284 return FAIL;
5285 }
5286 cc = *p;
5287 *p = NUL;
5288 opt_type = get_option_value(skip_option_env_lead(name),
5289 &numval, NULL, *opt_flags);
5290 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005291 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005292 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005293 case gov_unknown:
5294 semsg(_(e_unknown_option), name);
5295 return FAIL;
5296 case gov_string:
5297 case gov_hidden_string:
5298 *type = &t_string;
5299 break;
5300 case gov_bool:
5301 case gov_hidden_bool:
5302 *type = &t_bool;
5303 break;
5304 case gov_number:
5305 case gov_hidden_number:
5306 *type = &t_number;
5307 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005308 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005309 }
5310 else if (*name == '$')
5311 {
5312 *dest = dest_env;
5313 *type = &t_string;
5314 }
5315 else if (*name == '@')
5316 {
5317 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5318 {
5319 emsg_invreg(name[1]);
5320 return FAIL;
5321 }
5322 *dest = dest_reg;
5323 *type = &t_string;
5324 }
5325 else if (STRNCMP(name, "g:", 2) == 0)
5326 {
5327 *dest = dest_global;
5328 }
5329 else if (STRNCMP(name, "b:", 2) == 0)
5330 {
5331 *dest = dest_buffer;
5332 }
5333 else if (STRNCMP(name, "w:", 2) == 0)
5334 {
5335 *dest = dest_window;
5336 }
5337 else if (STRNCMP(name, "t:", 2) == 0)
5338 {
5339 *dest = dest_tab;
5340 }
5341 else if (STRNCMP(name, "v:", 2) == 0)
5342 {
5343 typval_T *vtv;
5344 int di_flags;
5345
5346 *vimvaridx = find_vim_var(name + 2, &di_flags);
5347 if (*vimvaridx < 0)
5348 {
5349 semsg(_(e_variable_not_found_str), name);
5350 return FAIL;
5351 }
5352 // We use the current value of "sandbox" here, is that OK?
5353 if (var_check_ro(di_flags, name, FALSE))
5354 return FAIL;
5355 *dest = dest_vimvar;
5356 vtv = get_vim_var_tv(*vimvaridx);
5357 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5358 }
5359 return OK;
5360}
5361
5362/*
5363 * Generate a STORE instruction for "dest", not being "dest_local".
5364 * Return FAIL when out of memory.
5365 */
5366 static int
5367generate_store_var(
5368 cctx_T *cctx,
5369 assign_dest_T dest,
5370 int opt_flags,
5371 int vimvaridx,
5372 int scriptvar_idx,
5373 int scriptvar_sid,
5374 type_T *type,
5375 char_u *name)
5376{
5377 switch (dest)
5378 {
5379 case dest_option:
5380 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5381 opt_flags);
5382 case dest_global:
5383 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005384 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5385 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005386 case dest_buffer:
5387 // include b: with the name, easier to execute that way
5388 return generate_STORE(cctx, ISN_STOREB, 0, name);
5389 case dest_window:
5390 // include w: with the name, easier to execute that way
5391 return generate_STORE(cctx, ISN_STOREW, 0, name);
5392 case dest_tab:
5393 // include t: with the name, easier to execute that way
5394 return generate_STORE(cctx, ISN_STORET, 0, name);
5395 case dest_env:
5396 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5397 case dest_reg:
5398 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5399 case dest_vimvar:
5400 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5401 case dest_script:
5402 if (scriptvar_idx < 0)
5403 {
5404 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005405 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005406
Bram Moolenaar41d61962020-12-06 20:12:43 +01005407 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005408 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5409 scriptvar_sid, type);
5410 if (name_s != name)
5411 vim_free(name_s);
5412 return r;
5413 }
5414 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5415 scriptvar_sid, scriptvar_idx, type);
5416 case dest_local:
5417 case dest_expr:
5418 // cannot happen
5419 break;
5420 }
5421 return FAIL;
5422}
5423
Bram Moolenaar752fc692021-01-04 21:57:11 +01005424 static int
5425is_decl_command(int cmdidx)
5426{
5427 return cmdidx == CMD_let || cmdidx == CMD_var
5428 || cmdidx == CMD_final || cmdidx == CMD_const;
5429}
5430
5431/*
5432 * Figure out the LHS type and other properties for an assignment or one item
5433 * of ":unlet" with an index.
5434 * Returns OK or FAIL.
5435 */
5436 static int
5437compile_lhs(
5438 char_u *var_start,
5439 lhs_T *lhs,
5440 int cmdidx,
5441 int heredoc,
5442 int oplen,
5443 cctx_T *cctx)
5444{
5445 char_u *var_end;
5446 int is_decl = is_decl_command(cmdidx);
5447
5448 CLEAR_POINTER(lhs);
5449 lhs->lhs_dest = dest_local;
5450 lhs->lhs_vimvaridx = -1;
5451 lhs->lhs_scriptvar_idx = -1;
5452
5453 // "dest_end" is the end of the destination, including "[expr]" or
5454 // ".name".
5455 // "var_end" is the end of the variable/option/etc. name.
5456 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5457 if (*var_start == '@')
5458 var_end = var_start + 2;
5459 else
5460 {
5461 // skip over the leading "&", "&l:", "&g:" and "$"
5462 var_end = skip_option_env_lead(var_start);
5463 var_end = to_name_end(var_end, TRUE);
5464 }
5465
5466 // "a: type" is declaring variable "a" with a type, not dict "a:".
5467 if (is_decl && lhs->lhs_dest_end == var_start + 2
5468 && lhs->lhs_dest_end[-1] == ':')
5469 --lhs->lhs_dest_end;
5470 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5471 --var_end;
5472
5473 // compute the length of the destination without "[expr]" or ".name"
5474 lhs->lhs_varlen = var_end - var_start;
5475 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5476 if (lhs->lhs_name == NULL)
5477 return FAIL;
5478 if (heredoc)
5479 lhs->lhs_type = &t_list_string;
5480 else
5481 lhs->lhs_type = &t_any;
5482
5483 if (cctx->ctx_skip != SKIP_YES)
5484 {
5485 int declare_error = FALSE;
5486
5487 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5488 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5489 &lhs->lhs_type, cctx) == FAIL)
5490 return FAIL;
5491 if (lhs->lhs_dest != dest_local)
5492 {
5493 // Specific kind of variable recognized.
5494 declare_error = is_decl;
5495 }
5496 else
5497 {
5498 int idx;
5499
5500 // No specific kind of variable recognized, just a name.
5501 for (idx = 0; reserved[idx] != NULL; ++idx)
5502 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5503 {
5504 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5505 return FAIL;
5506 }
5507
5508
5509 if (lookup_local(var_start, lhs->lhs_varlen,
5510 &lhs->lhs_local_lvar, cctx) == OK)
5511 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5512 else
5513 {
5514 CLEAR_FIELD(lhs->lhs_arg_lvar);
5515 if (arg_exists(var_start, lhs->lhs_varlen,
5516 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5517 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5518 {
5519 if (is_decl)
5520 {
5521 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5522 return FAIL;
5523 }
5524 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5525 }
5526 }
5527 if (lhs->lhs_lvar != NULL)
5528 {
5529 if (is_decl)
5530 {
5531 semsg(_(e_variable_already_declared), lhs->lhs_name);
5532 return FAIL;
5533 }
5534 }
5535 else
5536 {
5537 int script_namespace = lhs->lhs_varlen > 1
5538 && STRNCMP(var_start, "s:", 2) == 0;
5539 int script_var = (script_namespace
5540 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
5541 FALSE, cctx)
5542 : script_var_exists(var_start, lhs->lhs_varlen,
5543 TRUE, cctx)) == OK;
5544 imported_T *import =
5545 find_imported(var_start, lhs->lhs_varlen, cctx);
5546
5547 if (script_namespace || script_var || import != NULL)
5548 {
5549 char_u *rawname = lhs->lhs_name
5550 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5551
5552 if (is_decl)
5553 {
5554 if (script_namespace)
5555 semsg(_(e_cannot_declare_script_variable_in_function),
5556 lhs->lhs_name);
5557 else
5558 semsg(_(e_variable_already_declared_in_script),
5559 lhs->lhs_name);
5560 return FAIL;
5561 }
5562 else if (cctx->ctx_ufunc->uf_script_ctx_version
5563 == SCRIPT_VERSION_VIM9
5564 && script_namespace
5565 && !script_var && import == NULL)
5566 {
5567 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5568 return FAIL;
5569 }
5570
5571 lhs->lhs_dest = dest_script;
5572
5573 // existing script-local variables should have a type
5574 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5575 if (import != NULL)
5576 lhs->lhs_scriptvar_sid = import->imp_sid;
5577 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5578 {
5579 lhs->lhs_scriptvar_idx = get_script_item_idx(
5580 lhs->lhs_scriptvar_sid,
5581 rawname, TRUE, cctx);
5582 if (lhs->lhs_scriptvar_idx >= 0)
5583 {
5584 scriptitem_T *si = SCRIPT_ITEM(
5585 lhs->lhs_scriptvar_sid);
5586 svar_T *sv =
5587 ((svar_T *)si->sn_var_vals.ga_data)
5588 + lhs->lhs_scriptvar_idx;
5589 lhs->lhs_type = sv->sv_type;
5590 }
5591 }
5592 }
5593 else if (check_defined(var_start, lhs->lhs_varlen, cctx)
5594 == FAIL)
5595 return FAIL;
5596 }
5597 }
5598
5599 if (declare_error)
5600 {
5601 vim9_declare_error(lhs->lhs_name);
5602 return FAIL;
5603 }
5604 }
5605
5606 // handle "a:name" as a name, not index "name" on "a"
5607 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5608 var_end = lhs->lhs_dest_end;
5609
5610 if (lhs->lhs_dest != dest_option)
5611 {
5612 if (is_decl && *var_end == ':')
5613 {
5614 char_u *p;
5615
5616 // parse optional type: "let var: type = expr"
5617 if (!VIM_ISWHITE(var_end[1]))
5618 {
5619 semsg(_(e_white_space_required_after_str), ":");
5620 return FAIL;
5621 }
5622 p = skipwhite(var_end + 1);
5623 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5624 if (lhs->lhs_type == NULL)
5625 return FAIL;
5626 lhs->lhs_has_type = TRUE;
5627 }
5628 else if (lhs->lhs_lvar != NULL)
5629 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5630 }
5631
5632 if (oplen == 3 && !heredoc && lhs->lhs_dest != dest_global
5633 && lhs->lhs_type->tt_type != VAR_STRING
5634 && lhs->lhs_type->tt_type != VAR_ANY)
5635 {
5636 emsg(_(e_can_only_concatenate_to_string));
5637 return FAIL;
5638 }
5639
5640 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5641 && cctx->ctx_skip != SKIP_YES)
5642 {
5643 if (oplen > 1 && !heredoc)
5644 {
5645 // +=, /=, etc. require an existing variable
5646 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5647 return FAIL;
5648 }
5649 if (!is_decl)
5650 {
5651 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5652 return FAIL;
5653 }
5654
5655 // new local variable
5656 if ((lhs->lhs_type->tt_type == VAR_FUNC
5657 || lhs->lhs_type->tt_type == VAR_PARTIAL)
5658 && var_wrong_func_name(lhs->lhs_name, TRUE))
5659 return FAIL;
5660 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5661 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5662 if (lhs->lhs_lvar == NULL)
5663 return FAIL;
5664 lhs->lhs_new_local = TRUE;
5665 }
5666
5667 lhs->lhs_member_type = lhs->lhs_type;
5668 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5669 {
5670 // Something follows after the variable: "var[idx]" or "var.key".
5671 // TODO: should we also handle "->func()" here?
5672 if (is_decl)
5673 {
5674 emsg(_(e_cannot_use_index_when_declaring_variable));
5675 return FAIL;
5676 }
5677
5678 if (var_start[lhs->lhs_varlen] == '['
5679 || var_start[lhs->lhs_varlen] == '.')
5680 {
5681 char_u *after = var_start + lhs->lhs_varlen;
5682 char_u *p;
5683
5684 // Only the last index is used below, if there are others
5685 // before it generate code for the expression. Thus for
5686 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5687 for (;;)
5688 {
5689 p = skip_index(after);
5690 if (*p != '[' && *p != '.')
5691 break;
5692 after = p;
5693 }
5694 if (after > var_start + lhs->lhs_varlen)
5695 {
5696 lhs->lhs_varlen = after - var_start;
5697 lhs->lhs_dest = dest_expr;
5698 // We don't know the type before evaluating the expression,
5699 // use "any" until then.
5700 lhs->lhs_type = &t_any;
5701 }
5702
5703 lhs->lhs_has_index = TRUE;
5704 if (lhs->lhs_type->tt_member == NULL)
5705 lhs->lhs_member_type = &t_any;
5706 else
5707 lhs->lhs_member_type = lhs->lhs_type->tt_member;
5708 }
5709 else
5710 {
5711 semsg("Not supported yet: %s", var_start);
5712 return FAIL;
5713 }
5714 }
5715 return OK;
5716}
5717
5718/*
5719 * Assignment to a list or dict member, or ":unlet" for the item, using the
5720 * information in "lhs".
5721 * Returns OK or FAIL.
5722 */
5723 static int
5724compile_assign_unlet(
5725 char_u *var_start,
5726 lhs_T *lhs,
5727 int is_assign,
5728 type_T *rhs_type,
5729 cctx_T *cctx)
5730{
5731 char_u *p;
5732 int r;
5733 vartype_T dest_type;
5734 size_t varlen = lhs->lhs_varlen;
5735 garray_T *stack = &cctx->ctx_type_stack;
5736
5737 // Compile the "idx" in "var[idx]" or "key" in "var.key".
5738 p = var_start + varlen;
5739 if (*p == '[')
5740 {
5741 p = skipwhite(p + 1);
5742 r = compile_expr0(&p, cctx);
5743 if (r == OK && *skipwhite(p) != ']')
5744 {
5745 // this should not happen
5746 emsg(_(e_missbrac));
5747 r = FAIL;
5748 }
5749 }
5750 else // if (*p == '.')
5751 {
5752 char_u *key_end = to_name_end(p + 1, TRUE);
5753 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5754
5755 r = generate_PUSHS(cctx, key);
5756 }
5757 if (r == FAIL)
5758 return FAIL;
5759
5760 if (lhs->lhs_type == &t_any)
5761 {
5762 // Index on variable of unknown type: check at runtime.
5763 dest_type = VAR_ANY;
5764 }
5765 else
5766 {
5767 dest_type = lhs->lhs_type->tt_type;
5768 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
5769 return FAIL;
5770 if (dest_type == VAR_LIST
5771 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
5772 != VAR_NUMBER)
5773 {
5774 emsg(_(e_number_exp));
5775 return FAIL;
5776 }
5777 }
5778
5779 // Load the dict or list. On the stack we then have:
5780 // - value (for assignment, not for :unlet)
5781 // - index
5782 // - variable
5783 if (lhs->lhs_dest == dest_expr)
5784 {
5785 int c = var_start[varlen];
5786
5787 // Evaluate "ll[expr]" of "ll[expr][idx]"
5788 p = var_start;
5789 var_start[varlen] = NUL;
5790 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
5791 {
5792 // this should not happen
5793 emsg(_(e_missbrac));
5794 return FAIL;
5795 }
5796 var_start[varlen] = c;
5797
5798 lhs->lhs_type = stack->ga_len == 0 ? &t_void
5799 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5800 // now we can properly check the type
5801 if (lhs->lhs_type->tt_member != NULL && rhs_type != &t_void
5802 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, cctx,
5803 FALSE, FALSE) == FAIL)
5804 return FAIL;
5805 }
5806 else
5807 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
5808 lhs->lhs_lvar, lhs->lhs_type);
5809
5810 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
5811 {
5812 if (is_assign)
5813 {
5814 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
5815
5816 if (isn == NULL)
5817 return FAIL;
5818 isn->isn_arg.vartype = dest_type;
5819 }
5820 else
5821 {
5822 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
5823 return FAIL;
5824 }
5825 }
5826 else
5827 {
5828 emsg(_(e_indexable_type_required));
5829 return FAIL;
5830 }
5831
5832 return OK;
5833}
5834
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005835/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005836 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005837 * "let name"
5838 * "var name = expr"
5839 * "final name = expr"
5840 * "const name = expr"
5841 * "name = expr"
5842 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005843 * Return NULL for an error.
5844 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005845 */
5846 static char_u *
5847compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5848{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005849 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005850 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005851 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005852 char_u *ret = NULL;
5853 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005854 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005855 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005856 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005857 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005858 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005859 int oplen = 0;
5860 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005861 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005862 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005863 int is_decl = is_decl_command(cmdidx);
5864 lhs_T lhs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005865
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005866 // Skip over the "var" or "[var, var]" to get to any "=".
5867 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5868 if (p == NULL)
5869 return *arg == '[' ? arg : NULL;
5870
5871 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005872 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005873 // TODO: should we allow this, and figure out type inference from list
5874 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005875 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005876 return NULL;
5877 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01005878 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005879
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005880 sp = p;
5881 p = skipwhite(p);
5882 op = p;
5883 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005884
5885 if (var_count > 0 && oplen == 0)
5886 // can be something like "[1, 2]->func()"
5887 return arg;
5888
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005889 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005890 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005891 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005892 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005893 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005894
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005895 if (heredoc)
5896 {
5897 list_T *l;
5898 listitem_T *li;
5899
5900 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005901 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005902 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005903 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005904 if (l == NULL)
5905 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005906
Bram Moolenaar078269b2020-09-21 20:35:55 +02005907 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005908 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005909 // Push each line and the create the list.
5910 FOR_ALL_LIST_ITEMS(l, li)
5911 {
5912 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5913 li->li_tv.vval.v_string = NULL;
5914 }
5915 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005916 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005917 list_free(l);
5918 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005919 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005920 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005921 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005922 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005923 char_u *wp;
5924
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005925 // for "[var, var] = expr" evaluate the expression here, loop over the
5926 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005927 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005928
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005929 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01005930 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005931 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005932 if (compile_expr0(&p, cctx) == FAIL)
5933 return NULL;
5934 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005935
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005936 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005937 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005938 type_T *stacktype;
5939
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005940 stacktype = stack->ga_len == 0 ? &t_void
5941 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005942 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005943 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005944 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005945 goto theend;
5946 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005947 if (need_type(stacktype, &t_list_any, -1, cctx,
5948 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005949 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005950 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005951 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5952 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005953 if (stacktype->tt_member != NULL)
5954 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005955 }
5956 }
5957
5958 /*
5959 * Loop over variables in "[var, var] = expr".
5960 * For "var = expr" and "let var: type" this is done only once.
5961 */
5962 if (var_count > 0)
5963 var_start = skipwhite(arg + 1); // skip over the "["
5964 else
5965 var_start = arg;
5966 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5967 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005968 int instr_count = -1;
5969
Bram Moolenaar752fc692021-01-04 21:57:11 +01005970 vim_free(lhs.lhs_name);
5971
5972 /*
5973 * Figure out the LHS type and other properties.
5974 */
5975 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
5976 goto theend;
5977
5978 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02005979 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01005980 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005981 goto theend;
5982 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01005983 if (!is_decl && lhs.lhs_lvar != NULL
5984 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005985 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01005986 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005987 goto theend;
5988 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005989
5990 if (!heredoc)
5991 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005992 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005993 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005994 if (oplen > 0 && var_count == 0)
5995 {
5996 // skip over the "=" and the expression
5997 p = skipwhite(op + oplen);
5998 compile_expr0(&p, cctx);
5999 }
6000 }
6001 else if (oplen > 0)
6002 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006003 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006004 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006005
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006006 // For "var = expr" evaluate the expression.
6007 if (var_count == 0)
6008 {
6009 int r;
6010
6011 // for "+=", "*=", "..=" etc. first load the current value
6012 if (*op != '=')
6013 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006014 generate_loadvar(cctx, lhs.lhs_dest, lhs.lhs_name,
6015 lhs.lhs_lvar, lhs.lhs_type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006016
Bram Moolenaar752fc692021-01-04 21:57:11 +01006017 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006018 {
6019 // TODO: get member from list or dict
6020 emsg("Index with operation not supported yet");
6021 goto theend;
6022 }
6023 }
6024
6025 // Compile the expression. Temporarily hide the new local
6026 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006027 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006028 --cctx->ctx_locals.ga_len;
6029 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006030 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006031 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006032 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006033 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006034 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006035 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006036 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006037 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006038 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006039 ++cctx->ctx_locals.ga_len;
6040 if (r == FAIL)
6041 goto theend;
6042 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006043 else if (semicolon && var_idx == var_count - 1)
6044 {
6045 // For "[var; var] = expr" get the rest of the list
6046 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6047 goto theend;
6048 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006049 else
6050 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006051 // For "[var, var] = expr" get the "var_idx" item from the
6052 // list.
6053 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006054 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006055 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006056
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006057 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006058 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006059 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006060 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006061 if ((rhs_type->tt_type == VAR_FUNC
6062 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006063 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006064 goto theend;
6065
Bram Moolenaar752fc692021-01-04 21:57:11 +01006066 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006067 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006068 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006069 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006070 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006071 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006072 }
6073 else
6074 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006075 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006076 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006077 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006078 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006079 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006080 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006081 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006082 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006083 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006084 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006085 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006086 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006087 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006088 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006089 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006090
Bram Moolenaar71abe482020-09-14 22:28:30 +02006091 // without operator check type here, otherwise below
Bram Moolenaar752fc692021-01-04 21:57:11 +01006092 if (lhs.lhs_has_index)
6093 use_type = lhs.lhs_member_type;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006094 if (need_type(rhs_type, use_type, -1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006095 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006096 goto theend;
6097 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006098 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006099 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
6100 -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006101 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006102 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006103 else if (cmdidx == CMD_final)
6104 {
6105 emsg(_(e_final_requires_a_value));
6106 goto theend;
6107 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006108 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006109 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006110 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006111 goto theend;
6112 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006113 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006114 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006115 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006116 goto theend;
6117 }
6118 else
6119 {
6120 // variables are always initialized
6121 if (ga_grow(instr, 1) == FAIL)
6122 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006123 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006124 {
6125 case VAR_BOOL:
6126 generate_PUSHBOOL(cctx, VVAL_FALSE);
6127 break;
6128 case VAR_FLOAT:
6129#ifdef FEAT_FLOAT
6130 generate_PUSHF(cctx, 0.0);
6131#endif
6132 break;
6133 case VAR_STRING:
6134 generate_PUSHS(cctx, NULL);
6135 break;
6136 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006137 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006138 break;
6139 case VAR_FUNC:
6140 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6141 break;
6142 case VAR_LIST:
6143 generate_NEWLIST(cctx, 0);
6144 break;
6145 case VAR_DICT:
6146 generate_NEWDICT(cctx, 0);
6147 break;
6148 case VAR_JOB:
6149 generate_PUSHJOB(cctx, NULL);
6150 break;
6151 case VAR_CHANNEL:
6152 generate_PUSHCHANNEL(cctx, NULL);
6153 break;
6154 case VAR_NUMBER:
6155 case VAR_UNKNOWN:
6156 case VAR_ANY:
6157 case VAR_PARTIAL:
6158 case VAR_VOID:
6159 case VAR_SPECIAL: // cannot happen
6160 generate_PUSHNR(cctx, 0);
6161 break;
6162 }
6163 }
6164 if (var_count == 0)
6165 end = p;
6166 }
6167
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006168 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006169 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006170 break;
6171
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006172 if (oplen > 0 && *op != '=')
6173 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006174 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006175 type_T *stacktype;
6176
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006177 if (*op == '.')
6178 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006179 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006180 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006181 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006182 if (
6183#ifdef FEAT_FLOAT
6184 // If variable is float operation with number is OK.
6185 !(expected == &t_float && stacktype == &t_number) &&
6186#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006187 need_type(stacktype, expected, -1, cctx,
6188 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006189 goto theend;
6190
6191 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006192 {
6193 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6194 goto theend;
6195 }
6196 else if (*op == '+')
6197 {
6198 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006199 operator_type(lhs.lhs_member_type, stacktype),
6200 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006201 goto theend;
6202 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006203 else if (generate_two_op(cctx, op) == FAIL)
6204 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006205 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006206
Bram Moolenaar752fc692021-01-04 21:57:11 +01006207 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006208 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006209 // Use the info in "lhs" to store the value at the index in the
6210 // list or dict.
6211 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6212 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006213 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006214 }
6215 else
6216 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006217 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
6218 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006219 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006220 generate_LOCKCONST(cctx);
6221
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006222 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006223 && (lhs.lhs_type->tt_type == VAR_DICT
6224 || lhs.lhs_type->tt_type == VAR_LIST)
6225 && lhs.lhs_type->tt_member != NULL
6226 && lhs.lhs_type->tt_member != &t_any
6227 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006228 // Set the type in the list or dict, so that it can be checked,
6229 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006230 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006231
Bram Moolenaar752fc692021-01-04 21:57:11 +01006232 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006233 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006234 if (generate_store_var(cctx, lhs.lhs_dest,
6235 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6236 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6237 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006238 goto theend;
6239 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006240 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006241 {
6242 isn_T *isn = ((isn_T *)instr->ga_data)
6243 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006244
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006245 // optimization: turn "var = 123" from ISN_PUSHNR +
6246 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006247 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006248 && instr->ga_len == instr_count + 1
6249 && isn->isn_type == ISN_PUSHNR)
6250 {
6251 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006252
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006253 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006254 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006255 isn->isn_arg.storenr.stnr_val = val;
6256 if (stack->ga_len > 0)
6257 --stack->ga_len;
6258 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006259 else if (lhs.lhs_lvar->lv_from_outer > 0)
6260 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6261 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006262 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006263 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006264 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006265 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006266
6267 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006268 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006269 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006270
6271 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006272 if (var_count > 0 && !semicolon)
6273 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006274 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006275 goto theend;
6276 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006277
Bram Moolenaarb2097502020-07-19 17:17:02 +02006278 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006279
6280theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006281 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006282 return ret;
6283}
6284
6285/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006286 * Check for an assignment at "eap->cmd", compile it if found.
6287 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6288 */
6289 static int
6290may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6291{
6292 char_u *pskip;
6293 char_u *p;
6294
6295 // Assuming the command starts with a variable or function name,
6296 // find what follows.
6297 // Skip over "var.member", "var[idx]" and the like.
6298 // Also "&opt = val", "$ENV = val" and "@r = val".
6299 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6300 ? eap->cmd + 1 : eap->cmd;
6301 p = to_name_end(pskip, TRUE);
6302 if (p > eap->cmd && *p != NUL)
6303 {
6304 char_u *var_end;
6305 int oplen;
6306 int heredoc;
6307
6308 if (eap->cmd[0] == '@')
6309 var_end = eap->cmd + 2;
6310 else
6311 var_end = find_name_end(pskip, NULL, NULL,
6312 FNE_CHECK_START | FNE_INCL_BR);
6313 oplen = assignment_len(skipwhite(var_end), &heredoc);
6314 if (oplen > 0)
6315 {
6316 size_t len = p - eap->cmd;
6317
6318 // Recognize an assignment if we recognize the variable
6319 // name:
6320 // "g:var = expr"
6321 // "local = expr" where "local" is a local var.
6322 // "script = expr" where "script" is a script-local var.
6323 // "import = expr" where "import" is an imported var
6324 // "&opt = expr"
6325 // "$ENV = expr"
6326 // "@r = expr"
6327 if (*eap->cmd == '&'
6328 || *eap->cmd == '$'
6329 || *eap->cmd == '@'
6330 || ((len) > 2 && eap->cmd[1] == ':')
6331 || lookup_local(eap->cmd, len, NULL, cctx) == OK
6332 || arg_exists(eap->cmd, len, NULL, NULL, NULL, cctx) == OK
6333 || script_var_exists(eap->cmd, len, FALSE, cctx) == OK
6334 || find_imported(eap->cmd, len, cctx) != NULL)
6335 {
6336 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6337 if (*line == NULL || *line == eap->cmd)
6338 return FAIL;
6339 return OK;
6340 }
6341 }
6342 }
6343
6344 if (*eap->cmd == '[')
6345 {
6346 // [var, var] = expr
6347 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6348 if (*line == NULL)
6349 return FAIL;
6350 if (*line != eap->cmd)
6351 return OK;
6352 }
6353 return NOTDONE;
6354}
6355
6356/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006357 * Check if "name" can be "unlet".
6358 */
6359 int
6360check_vim9_unlet(char_u *name)
6361{
6362 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6363 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006364 // "unlet s:var" is allowed in legacy script.
6365 if (*name == 's' && !script_is_vim9())
6366 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006367 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006368 return FAIL;
6369 }
6370 return OK;
6371}
6372
6373/*
6374 * Callback passed to ex_unletlock().
6375 */
6376 static int
6377compile_unlet(
6378 lval_T *lvp,
6379 char_u *name_end,
6380 exarg_T *eap,
6381 int deep UNUSED,
6382 void *coookie)
6383{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006384 cctx_T *cctx = coookie;
6385 char_u *p = lvp->ll_name;
6386 int cc = *name_end;
6387 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006388
Bram Moolenaar752fc692021-01-04 21:57:11 +01006389 if (cctx->ctx_skip == SKIP_YES)
6390 return OK;
6391
6392 *name_end = NUL;
6393 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006394 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006395 // :unlet $ENV_VAR
6396 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6397 }
6398 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6399 {
6400 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006401
Bram Moolenaar752fc692021-01-04 21:57:11 +01006402 // This is similar to assigning: lookup the list/dict, compile the
6403 // idx/key. Then instead of storing the value unlet the item.
6404 // unlet {list}[idx]
6405 // unlet {dict}[key] dict.key
6406 //
6407 // Figure out the LHS type and other properties.
6408 //
6409 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6410
6411 // : unlet an indexed item
6412 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006413 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006414 iemsg("called compile_lhs() without an index");
6415 ret = FAIL;
6416 }
6417 else
6418 {
6419 // Use the info in "lhs" to unlet the item at the index in the
6420 // list or dict.
6421 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006422 }
6423
Bram Moolenaar752fc692021-01-04 21:57:11 +01006424 vim_free(lhs.lhs_name);
6425 }
6426 else if (check_vim9_unlet(p) == FAIL)
6427 {
6428 ret = FAIL;
6429 }
6430 else
6431 {
6432 // Normal name. Only supports g:, w:, t: and b: namespaces.
6433 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006434 }
6435
Bram Moolenaar752fc692021-01-04 21:57:11 +01006436 *name_end = cc;
6437 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006438}
6439
6440/*
6441 * compile "unlet var", "lock var" and "unlock var"
6442 * "arg" points to "var".
6443 */
6444 static char_u *
6445compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6446{
6447 char_u *p = arg;
6448
6449 if (eap->cmdidx != CMD_unlet)
6450 {
6451 emsg("Sorry, :lock and unlock not implemented yet");
6452 return NULL;
6453 }
6454
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006455 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6456 compile_unlet, cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006457 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6458}
6459
6460/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006461 * Compile an :import command.
6462 */
6463 static char_u *
6464compile_import(char_u *arg, cctx_T *cctx)
6465{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006466 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006467}
6468
6469/*
6470 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6471 */
6472 static int
6473compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6474{
6475 garray_T *instr = &cctx->ctx_instr;
6476 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6477
6478 if (endlabel == NULL)
6479 return FAIL;
6480 endlabel->el_next = *el;
6481 *el = endlabel;
6482 endlabel->el_end_label = instr->ga_len;
6483
6484 generate_JUMP(cctx, when, 0);
6485 return OK;
6486}
6487
6488 static void
6489compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
6490{
6491 garray_T *instr = &cctx->ctx_instr;
6492
6493 while (*el != NULL)
6494 {
6495 endlabel_T *cur = (*el);
6496 isn_T *isn;
6497
6498 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
6499 isn->isn_arg.jump.jump_where = instr->ga_len;
6500 *el = cur->el_next;
6501 vim_free(cur);
6502 }
6503}
6504
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006505 static void
6506compile_free_jump_to_end(endlabel_T **el)
6507{
6508 while (*el != NULL)
6509 {
6510 endlabel_T *cur = (*el);
6511
6512 *el = cur->el_next;
6513 vim_free(cur);
6514 }
6515}
6516
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006517/*
6518 * Create a new scope and set up the generic items.
6519 */
6520 static scope_T *
6521new_scope(cctx_T *cctx, scopetype_T type)
6522{
6523 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6524
6525 if (scope == NULL)
6526 return NULL;
6527 scope->se_outer = cctx->ctx_scope;
6528 cctx->ctx_scope = scope;
6529 scope->se_type = type;
6530 scope->se_local_count = cctx->ctx_locals.ga_len;
6531 return scope;
6532}
6533
6534/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006535 * Free the current scope and go back to the outer scope.
6536 */
6537 static void
6538drop_scope(cctx_T *cctx)
6539{
6540 scope_T *scope = cctx->ctx_scope;
6541
6542 if (scope == NULL)
6543 {
6544 iemsg("calling drop_scope() without a scope");
6545 return;
6546 }
6547 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006548 switch (scope->se_type)
6549 {
6550 case IF_SCOPE:
6551 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6552 case FOR_SCOPE:
6553 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6554 case WHILE_SCOPE:
6555 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6556 case TRY_SCOPE:
6557 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6558 case NO_SCOPE:
6559 case BLOCK_SCOPE:
6560 break;
6561 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006562 vim_free(scope);
6563}
6564
6565/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006566 * compile "if expr"
6567 *
6568 * "if expr" Produces instructions:
6569 * EVAL expr Push result of "expr"
6570 * JUMP_IF_FALSE end
6571 * ... body ...
6572 * end:
6573 *
6574 * "if expr | else" Produces instructions:
6575 * EVAL expr Push result of "expr"
6576 * JUMP_IF_FALSE else
6577 * ... body ...
6578 * JUMP_ALWAYS end
6579 * else:
6580 * ... body ...
6581 * end:
6582 *
6583 * "if expr1 | elseif expr2 | else" Produces instructions:
6584 * EVAL expr Push result of "expr"
6585 * JUMP_IF_FALSE elseif
6586 * ... body ...
6587 * JUMP_ALWAYS end
6588 * elseif:
6589 * EVAL expr Push result of "expr"
6590 * JUMP_IF_FALSE else
6591 * ... body ...
6592 * JUMP_ALWAYS end
6593 * else:
6594 * ... body ...
6595 * end:
6596 */
6597 static char_u *
6598compile_if(char_u *arg, cctx_T *cctx)
6599{
6600 char_u *p = arg;
6601 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006602 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006603 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006604 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006605 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006606
Bram Moolenaara5565e42020-05-09 15:44:01 +02006607 CLEAR_FIELD(ppconst);
6608 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006609 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006610 clear_ppconst(&ppconst);
6611 return NULL;
6612 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006613 if (cctx->ctx_skip == SKIP_YES)
6614 clear_ppconst(&ppconst);
6615 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006616 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006617 int error = FALSE;
6618 int v;
6619
Bram Moolenaara5565e42020-05-09 15:44:01 +02006620 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006621 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006622 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006623 if (error)
6624 return NULL;
6625 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006626 }
6627 else
6628 {
6629 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006630 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006631 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006632 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006633 if (bool_on_stack(cctx) == FAIL)
6634 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006635 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006636
6637 scope = new_scope(cctx, IF_SCOPE);
6638 if (scope == NULL)
6639 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006640 scope->se_skip_save = skip_save;
6641 // "is_had_return" will be reset if any block does not end in :return
6642 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006643
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006644 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006645 {
6646 // "where" is set when ":elseif", "else" or ":endif" is found
6647 scope->se_u.se_if.is_if_label = instr->ga_len;
6648 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6649 }
6650 else
6651 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006652
6653 return p;
6654}
6655
6656 static char_u *
6657compile_elseif(char_u *arg, cctx_T *cctx)
6658{
6659 char_u *p = arg;
6660 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006661 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006662 isn_T *isn;
6663 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006664 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006665 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006666
6667 if (scope == NULL || scope->se_type != IF_SCOPE)
6668 {
6669 emsg(_(e_elseif_without_if));
6670 return NULL;
6671 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006672 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006673 if (!cctx->ctx_had_return)
6674 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006675
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006676 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006677 {
6678 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006679 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006680 return NULL;
6681 // previous "if" or "elseif" jumps here
6682 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6683 isn->isn_arg.jump.jump_where = instr->ga_len;
6684 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006685
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006686 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006687 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006688 if (cctx->ctx_skip == SKIP_YES)
6689 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006690 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006691 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006692 clear_ppconst(&ppconst);
6693 return NULL;
6694 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006695 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006696 if (scope->se_skip_save == SKIP_YES)
6697 clear_ppconst(&ppconst);
6698 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006699 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006700 int error = FALSE;
6701 int v;
6702
Bram Moolenaar7f141552020-05-09 17:35:53 +02006703 // The expression results in a constant.
6704 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006705 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6706 if (error)
6707 return NULL;
6708 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006709 clear_ppconst(&ppconst);
6710 scope->se_u.se_if.is_if_label = -1;
6711 }
6712 else
6713 {
6714 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006715 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006716 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006717 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006718 if (bool_on_stack(cctx) == FAIL)
6719 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006720
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006721 // "where" is set when ":elseif", "else" or ":endif" is found
6722 scope->se_u.se_if.is_if_label = instr->ga_len;
6723 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6724 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006725
6726 return p;
6727}
6728
6729 static char_u *
6730compile_else(char_u *arg, cctx_T *cctx)
6731{
6732 char_u *p = arg;
6733 garray_T *instr = &cctx->ctx_instr;
6734 isn_T *isn;
6735 scope_T *scope = cctx->ctx_scope;
6736
6737 if (scope == NULL || scope->se_type != IF_SCOPE)
6738 {
6739 emsg(_(e_else_without_if));
6740 return NULL;
6741 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006742 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006743 if (!cctx->ctx_had_return)
6744 scope->se_u.se_if.is_had_return = FALSE;
6745 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006746
Bram Moolenaarefd88552020-06-18 20:50:10 +02006747 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006748 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006749 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006750 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006751 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006752 if (!cctx->ctx_had_return
6753 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6754 JUMP_ALWAYS, cctx) == FAIL)
6755 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006756 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006757
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006758 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006759 {
6760 if (scope->se_u.se_if.is_if_label >= 0)
6761 {
6762 // previous "if" or "elseif" jumps here
6763 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6764 isn->isn_arg.jump.jump_where = instr->ga_len;
6765 scope->se_u.se_if.is_if_label = -1;
6766 }
6767 }
6768
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006769 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006770 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6771 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006772
6773 return p;
6774}
6775
6776 static char_u *
6777compile_endif(char_u *arg, cctx_T *cctx)
6778{
6779 scope_T *scope = cctx->ctx_scope;
6780 ifscope_T *ifscope;
6781 garray_T *instr = &cctx->ctx_instr;
6782 isn_T *isn;
6783
6784 if (scope == NULL || scope->se_type != IF_SCOPE)
6785 {
6786 emsg(_(e_endif_without_if));
6787 return NULL;
6788 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006789 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006790 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006791 if (!cctx->ctx_had_return)
6792 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006793
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006794 if (scope->se_u.se_if.is_if_label >= 0)
6795 {
6796 // previous "if" or "elseif" jumps here
6797 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6798 isn->isn_arg.jump.jump_where = instr->ga_len;
6799 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006800 // Fill in the "end" label in jumps at the end of the blocks.
6801 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006802 cctx->ctx_skip = scope->se_skip_save;
6803
6804 // If all the blocks end in :return and there is an :else then the
6805 // had_return flag is set.
6806 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006807
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006808 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006809 return arg;
6810}
6811
6812/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01006813 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006814 *
6815 * Produces instructions:
6816 * PUSHNR -1
6817 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01006818 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006819 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6820 * - if beyond end, jump to "end"
6821 * - otherwise get item from list and push it
6822 * STORE var Store item in "var"
6823 * ... body ...
6824 * JUMP top Jump back to repeat
6825 * end: DROP Drop the result of "expr"
6826 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006827 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
6828 * UNPACK 2 Split item in 2
6829 * STORE var1 Store item in "var1"
6830 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006831 */
6832 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006833compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006834{
Bram Moolenaar792f7862020-11-23 08:31:18 +01006835 char_u *arg;
6836 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006837 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006838 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006839 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006840 int var_count = 0;
6841 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006842 size_t varlen;
6843 garray_T *instr = &cctx->ctx_instr;
6844 garray_T *stack = &cctx->ctx_type_stack;
6845 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006846 lvar_T *loop_lvar; // loop iteration variable
6847 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006848 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006849 type_T *item_type = &t_any;
6850 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006851
Bram Moolenaar792f7862020-11-23 08:31:18 +01006852 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
6853 if (var_count == 0)
6854 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006855
6856 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006857 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006858 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6859 return NULL;
6860 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006861 {
6862 emsg(_(e_missing_in));
6863 return NULL;
6864 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006865 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006866 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6867 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006868
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006869 scope = new_scope(cctx, FOR_SCOPE);
6870 if (scope == NULL)
6871 return NULL;
6872
Bram Moolenaar792f7862020-11-23 08:31:18 +01006873 // Reserve a variable to store the loop iteration counter and initialize it
6874 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006875 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6876 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006877 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006878 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006879 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006880 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006881 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006882 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006883
6884 // compile "expr", it remains on the stack until "endfor"
6885 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006886 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006887 {
6888 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006889 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006890 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006891 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006892
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006893 // Now that we know the type of "var", check that it is a list, now or at
6894 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006895 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006896 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006897 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006898 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006899 return NULL;
6900 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006901
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006902 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006903 {
6904 if (var_count == 1)
6905 item_type = vartype->tt_member;
6906 else if (vartype->tt_member->tt_type == VAR_LIST
6907 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
6908 item_type = vartype->tt_member->tt_member;
6909 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006910
6911 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006912 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006913 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006914
Bram Moolenaar792f7862020-11-23 08:31:18 +01006915 arg = arg_start;
6916 if (var_count > 1)
6917 {
6918 generate_UNPACK(cctx, var_count, semicolon);
6919 arg = skipwhite(arg + 1); // skip white after '['
6920
6921 // the list item is replaced by a number of items
6922 if (ga_grow(stack, var_count - 1) == FAIL)
6923 {
6924 drop_scope(cctx);
6925 return NULL;
6926 }
6927 --stack->ga_len;
6928 for (idx = 0; idx < var_count; ++idx)
6929 {
6930 ((type_T **)stack->ga_data)[stack->ga_len] =
6931 (semicolon && idx == 0) ? vartype : item_type;
6932 ++stack->ga_len;
6933 }
6934 }
6935
6936 for (idx = 0; idx < var_count; ++idx)
6937 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006938 assign_dest_T dest = dest_local;
6939 int opt_flags = 0;
6940 int vimvaridx = -1;
6941 type_T *type = &t_any;
6942
6943 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006944 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006945 name = vim_strnsave(arg, varlen);
6946 if (name == NULL)
6947 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006948
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006949 // TODO: script var not supported?
6950 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
6951 &vimvaridx, &type, cctx) == FAIL)
6952 goto failed;
6953 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006954 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006955 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
6956 0, 0, type, name) == FAIL)
6957 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006958 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006959 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006960 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01006961 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006962 {
6963 semsg(_(e_variable_already_declared), arg);
6964 goto failed;
6965 }
6966
Bram Moolenaarea870692020-12-02 14:24:30 +01006967 if (STRNCMP(name, "s:", 2) == 0)
6968 {
6969 semsg(_(e_cannot_declare_script_variable_in_function), name);
6970 goto failed;
6971 }
6972
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006973 // Reserve a variable to store "var".
6974 // TODO: check for type
6975 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6976 if (var_lvar == NULL)
6977 // out of memory or used as an argument
6978 goto failed;
6979
6980 if (semicolon && idx == var_count - 1)
6981 var_lvar->lv_type = vartype;
6982 else
6983 var_lvar->lv_type = item_type;
6984 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
6985 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006986
6987 if (*p == ',' || *p == ';')
6988 ++p;
6989 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006990 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006991 }
6992
6993 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006994
6995failed:
6996 vim_free(name);
6997 drop_scope(cctx);
6998 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006999}
7000
7001/*
7002 * compile "endfor"
7003 */
7004 static char_u *
7005compile_endfor(char_u *arg, cctx_T *cctx)
7006{
7007 garray_T *instr = &cctx->ctx_instr;
7008 scope_T *scope = cctx->ctx_scope;
7009 forscope_T *forscope;
7010 isn_T *isn;
7011
7012 if (scope == NULL || scope->se_type != FOR_SCOPE)
7013 {
7014 emsg(_(e_for));
7015 return NULL;
7016 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007017 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007018 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007019 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007020
7021 // At end of ":for" scope jump back to the FOR instruction.
7022 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7023
7024 // Fill in the "end" label in the FOR statement so it can jump here
7025 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7026 isn->isn_arg.forloop.for_end = instr->ga_len;
7027
7028 // Fill in the "end" label any BREAK statements
7029 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
7030
7031 // Below the ":for" scope drop the "expr" list from the stack.
7032 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7033 return NULL;
7034
7035 vim_free(scope);
7036
7037 return arg;
7038}
7039
7040/*
7041 * compile "while expr"
7042 *
7043 * Produces instructions:
7044 * top: EVAL expr Push result of "expr"
7045 * JUMP_IF_FALSE end jump if false
7046 * ... body ...
7047 * JUMP top Jump back to repeat
7048 * end:
7049 *
7050 */
7051 static char_u *
7052compile_while(char_u *arg, cctx_T *cctx)
7053{
7054 char_u *p = arg;
7055 garray_T *instr = &cctx->ctx_instr;
7056 scope_T *scope;
7057
7058 scope = new_scope(cctx, WHILE_SCOPE);
7059 if (scope == NULL)
7060 return NULL;
7061
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007062 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007063
7064 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007065 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007066 return NULL;
7067
Bram Moolenaar13106602020-10-04 16:06:05 +02007068 if (bool_on_stack(cctx) == FAIL)
7069 return FAIL;
7070
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007071 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007072 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007073 JUMP_IF_FALSE, cctx) == FAIL)
7074 return FAIL;
7075
7076 return p;
7077}
7078
7079/*
7080 * compile "endwhile"
7081 */
7082 static char_u *
7083compile_endwhile(char_u *arg, cctx_T *cctx)
7084{
7085 scope_T *scope = cctx->ctx_scope;
7086
7087 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7088 {
7089 emsg(_(e_while));
7090 return NULL;
7091 }
7092 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007093 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007094
7095 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007096 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007097
7098 // Fill in the "end" label in the WHILE statement so it can jump here.
7099 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007100 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007101
7102 vim_free(scope);
7103
7104 return arg;
7105}
7106
7107/*
7108 * compile "continue"
7109 */
7110 static char_u *
7111compile_continue(char_u *arg, cctx_T *cctx)
7112{
7113 scope_T *scope = cctx->ctx_scope;
7114
7115 for (;;)
7116 {
7117 if (scope == NULL)
7118 {
7119 emsg(_(e_continue));
7120 return NULL;
7121 }
7122 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7123 break;
7124 scope = scope->se_outer;
7125 }
7126
7127 // Jump back to the FOR or WHILE instruction.
7128 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007129 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
7130 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007131 return arg;
7132}
7133
7134/*
7135 * compile "break"
7136 */
7137 static char_u *
7138compile_break(char_u *arg, cctx_T *cctx)
7139{
7140 scope_T *scope = cctx->ctx_scope;
7141 endlabel_T **el;
7142
7143 for (;;)
7144 {
7145 if (scope == NULL)
7146 {
7147 emsg(_(e_break));
7148 return NULL;
7149 }
7150 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7151 break;
7152 scope = scope->se_outer;
7153 }
7154
7155 // Jump to the end of the FOR or WHILE loop.
7156 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007157 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007158 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007159 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007160 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7161 return FAIL;
7162
7163 return arg;
7164}
7165
7166/*
7167 * compile "{" start of block
7168 */
7169 static char_u *
7170compile_block(char_u *arg, cctx_T *cctx)
7171{
7172 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7173 return NULL;
7174 return skipwhite(arg + 1);
7175}
7176
7177/*
7178 * compile end of block: drop one scope
7179 */
7180 static void
7181compile_endblock(cctx_T *cctx)
7182{
7183 scope_T *scope = cctx->ctx_scope;
7184
7185 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007186 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007187 vim_free(scope);
7188}
7189
7190/*
7191 * compile "try"
7192 * Creates a new scope for the try-endtry, pointing to the first catch and
7193 * finally.
7194 * Creates another scope for the "try" block itself.
7195 * TRY instruction sets up exception handling at runtime.
7196 *
7197 * "try"
7198 * TRY -> catch1, -> finally push trystack entry
7199 * ... try block
7200 * "throw {exception}"
7201 * EVAL {exception}
7202 * THROW create exception
7203 * ... try block
7204 * " catch {expr}"
7205 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007206 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007207 * EVAL {expr}
7208 * MATCH
7209 * JUMP nomatch -> catch2
7210 * CATCH remove exception
7211 * ... catch block
7212 * " catch"
7213 * JUMP -> finally
7214 * catch2: CATCH remove exception
7215 * ... catch block
7216 * " finally"
7217 * finally:
7218 * ... finally block
7219 * " endtry"
7220 * ENDTRY pop trystack entry, may rethrow
7221 */
7222 static char_u *
7223compile_try(char_u *arg, cctx_T *cctx)
7224{
7225 garray_T *instr = &cctx->ctx_instr;
7226 scope_T *try_scope;
7227 scope_T *scope;
7228
7229 // scope that holds the jumps that go to catch/finally/endtry
7230 try_scope = new_scope(cctx, TRY_SCOPE);
7231 if (try_scope == NULL)
7232 return NULL;
7233
Bram Moolenaar69f70502021-01-01 16:10:46 +01007234 if (cctx->ctx_skip != SKIP_YES)
7235 {
7236 // "catch" is set when the first ":catch" is found.
7237 // "finally" is set when ":finally" or ":endtry" is found
7238 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
7239 if (generate_instr(cctx, ISN_TRY) == NULL)
7240 return NULL;
7241 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007242
7243 // scope for the try block itself
7244 scope = new_scope(cctx, BLOCK_SCOPE);
7245 if (scope == NULL)
7246 return NULL;
7247
7248 return arg;
7249}
7250
7251/*
7252 * compile "catch {expr}"
7253 */
7254 static char_u *
7255compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7256{
7257 scope_T *scope = cctx->ctx_scope;
7258 garray_T *instr = &cctx->ctx_instr;
7259 char_u *p;
7260 isn_T *isn;
7261
7262 // end block scope from :try or :catch
7263 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7264 compile_endblock(cctx);
7265 scope = cctx->ctx_scope;
7266
7267 // Error if not in a :try scope
7268 if (scope == NULL || scope->se_type != TRY_SCOPE)
7269 {
7270 emsg(_(e_catch));
7271 return NULL;
7272 }
7273
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007274 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007275 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007276 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007277 return NULL;
7278 }
7279
Bram Moolenaar69f70502021-01-01 16:10:46 +01007280 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007281 {
Bram Moolenaar69f70502021-01-01 16:10:46 +01007282 // Jump from end of previous block to :finally or :endtry
7283 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7284 JUMP_ALWAYS, cctx) == FAIL)
7285 return NULL;
7286
7287 // End :try or :catch scope: set value in ISN_TRY instruction
7288 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7289 if (isn->isn_arg.try.try_catch == 0)
7290 isn->isn_arg.try.try_catch = instr->ga_len;
7291 if (scope->se_u.se_try.ts_catch_label != 0)
7292 {
7293 // Previous catch without match jumps here
7294 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7295 isn->isn_arg.jump.jump_where = instr->ga_len;
7296 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007297 }
7298
7299 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007300 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007301 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007302 scope->se_u.se_try.ts_caught_all = TRUE;
7303 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007304 }
7305 else
7306 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007307 char_u *end;
7308 char_u *pat;
7309 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007310 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007311 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007312
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007313 // Push v:exception, push {expr} and MATCH
7314 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7315
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007316 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007317 if (*end != *p)
7318 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007319 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007320 vim_free(tofree);
7321 return FAIL;
7322 }
7323 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007324 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007325 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007326 len = (int)(end - tofree);
7327 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007328 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007329 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007330 if (pat == NULL)
7331 return FAIL;
7332 if (generate_PUSHS(cctx, pat) == FAIL)
7333 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007334
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007335 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7336 return NULL;
7337
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007338 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007339 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7340 return NULL;
7341 }
7342
Bram Moolenaar69f70502021-01-01 16:10:46 +01007343 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007344 return NULL;
7345
7346 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7347 return NULL;
7348 return p;
7349}
7350
7351 static char_u *
7352compile_finally(char_u *arg, cctx_T *cctx)
7353{
7354 scope_T *scope = cctx->ctx_scope;
7355 garray_T *instr = &cctx->ctx_instr;
7356 isn_T *isn;
7357
7358 // end block scope from :try or :catch
7359 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7360 compile_endblock(cctx);
7361 scope = cctx->ctx_scope;
7362
7363 // Error if not in a :try scope
7364 if (scope == NULL || scope->se_type != TRY_SCOPE)
7365 {
7366 emsg(_(e_finally));
7367 return NULL;
7368 }
7369
7370 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007371 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007372 if (isn->isn_arg.try.try_finally != 0)
7373 {
7374 emsg(_(e_finally_dup));
7375 return NULL;
7376 }
7377
7378 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007379 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007380
Bram Moolenaar585fea72020-04-02 22:33:21 +02007381 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007382 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007383 {
7384 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007385 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007386 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007387 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007388 }
7389
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007390 // TODO: set index in ts_finally_label jumps
7391
7392 return arg;
7393}
7394
7395 static char_u *
7396compile_endtry(char_u *arg, cctx_T *cctx)
7397{
7398 scope_T *scope = cctx->ctx_scope;
7399 garray_T *instr = &cctx->ctx_instr;
7400 isn_T *isn;
7401
7402 // end block scope from :catch or :finally
7403 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7404 compile_endblock(cctx);
7405 scope = cctx->ctx_scope;
7406
7407 // Error if not in a :try scope
7408 if (scope == NULL || scope->se_type != TRY_SCOPE)
7409 {
7410 if (scope == NULL)
7411 emsg(_(e_no_endtry));
7412 else if (scope->se_type == WHILE_SCOPE)
7413 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007414 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007415 emsg(_(e_endfor));
7416 else
7417 emsg(_(e_endif));
7418 return NULL;
7419 }
7420
Bram Moolenaar69f70502021-01-01 16:10:46 +01007421 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007422 {
Bram Moolenaar69f70502021-01-01 16:10:46 +01007423 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7424 if (isn->isn_arg.try.try_catch == 0
7425 && isn->isn_arg.try.try_finally == 0)
7426 {
7427 emsg(_(e_missing_catch_or_finally));
7428 return NULL;
7429 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007430
Bram Moolenaar69f70502021-01-01 16:10:46 +01007431 // Fill in the "end" label in jumps at the end of the blocks, if not
7432 // done by ":finally".
7433 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007434
Bram Moolenaar69f70502021-01-01 16:10:46 +01007435 // End :catch or :finally scope: set value in ISN_TRY instruction
7436 if (isn->isn_arg.try.try_catch == 0)
7437 isn->isn_arg.try.try_catch = instr->ga_len;
7438 if (isn->isn_arg.try.try_finally == 0)
7439 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007440
Bram Moolenaar69f70502021-01-01 16:10:46 +01007441 if (scope->se_u.se_try.ts_catch_label != 0)
7442 {
7443 // Last catch without match jumps here
7444 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7445 isn->isn_arg.jump.jump_where = instr->ga_len;
7446 }
Bram Moolenaare8593122020-07-18 15:17:02 +02007447 }
7448
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007449 compile_endblock(cctx);
7450
Bram Moolenaar69f70502021-01-01 16:10:46 +01007451 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_ENDTRY) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007452 return NULL;
7453 return arg;
7454}
7455
7456/*
7457 * compile "throw {expr}"
7458 */
7459 static char_u *
7460compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7461{
7462 char_u *p = skipwhite(arg);
7463
Bram Moolenaara5565e42020-05-09 15:44:01 +02007464 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007465 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01007466 if (cctx->ctx_skip == SKIP_YES)
7467 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007468 if (may_generate_2STRING(-1, cctx) == FAIL)
7469 return NULL;
7470 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7471 return NULL;
7472
7473 return p;
7474}
7475
7476/*
7477 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007478 * compile "echomsg expr"
7479 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007480 * compile "execute expr"
7481 */
7482 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007483compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007484{
7485 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007486 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007487 int count = 0;
7488
7489 for (;;)
7490 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007491 if (ends_excmd2(prev, p))
7492 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007493 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007494 return NULL;
7495 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007496 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007497 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007498 }
7499
Bram Moolenaare4984292020-12-13 14:19:25 +01007500 if (count > 0)
7501 {
7502 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7503 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7504 else if (cmdidx == CMD_execute)
7505 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7506 else if (cmdidx == CMD_echomsg)
7507 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7508 else
7509 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7510 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007511 return p;
7512}
7513
7514/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007515 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01007516 * instruction to compute it and return OK.
7517 * Otherwise return FAIL, the caller must deal with any range.
7518 */
7519 static int
7520compile_variable_range(exarg_T *eap, cctx_T *cctx)
7521{
7522 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7523 char_u *p = skipdigits(eap->cmd);
7524
7525 if (p == range_end)
7526 return FAIL;
7527 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7528}
7529
7530/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007531 * :put r
7532 * :put ={expr}
7533 */
7534 static char_u *
7535compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7536{
7537 char_u *line = arg;
7538 linenr_T lnum;
7539 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007540 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007541
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007542 eap->regname = *line;
7543
7544 if (eap->regname == '=')
7545 {
7546 char_u *p = line + 1;
7547
7548 if (compile_expr0(&p, cctx) == FAIL)
7549 return NULL;
7550 line = p;
7551 }
7552 else if (eap->regname != NUL)
7553 ++line;
7554
Bram Moolenaar08597872020-12-10 19:43:40 +01007555 if (compile_variable_range(eap, cctx) == OK)
7556 {
7557 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
7558 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007559 else
Bram Moolenaar08597872020-12-10 19:43:40 +01007560 {
7561 // Either no range or a number.
7562 // "errormsg" will not be set because the range is ADDR_LINES.
7563 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01007564 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01007565 return NULL;
7566 if (eap->addr_count == 0)
7567 lnum = -1;
7568 else
7569 lnum = eap->line2;
7570 if (above)
7571 --lnum;
7572 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007573
7574 generate_PUT(cctx, eap->regname, lnum);
7575 return line;
7576}
7577
7578/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007579 * A command that is not compiled, execute with legacy code.
7580 */
7581 static char_u *
7582compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7583{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007584 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007585 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007586 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007587
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007588 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007589 goto theend;
7590
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007591 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007592 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007593 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007594 int usefilter = FALSE;
7595
7596 has_expr = argt & (EX_XFILE | EX_EXPAND);
7597
7598 // If the command can be followed by a bar, find the bar and truncate
7599 // it, so that the following command can be compiled.
7600 // The '|' is overwritten with a NUL, it is put back below.
7601 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7602 && *eap->arg == '!')
7603 // :w !filter or :r !filter or :r! filter
7604 usefilter = TRUE;
7605 if ((argt & EX_TRLBAR) && !usefilter)
7606 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007607 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007608 separate_nextcmd(eap);
7609 if (eap->nextcmd != NULL)
7610 nextcmd = eap->nextcmd;
7611 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01007612 else if (eap->cmdidx == CMD_wincmd)
7613 {
7614 p = eap->arg;
7615 if (*p != NUL)
7616 ++p;
7617 if (*p == 'g' || *p == Ctrl_G)
7618 ++p;
7619 p = skipwhite(p);
7620 if (*p == '|')
7621 {
7622 *p = NUL;
7623 nextcmd = p + 1;
7624 }
7625 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007626 }
7627
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007628 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
7629 {
7630 // expand filename in "syntax include [@group] filename"
7631 has_expr = TRUE;
7632 eap->arg = skipwhite(eap->arg + 7);
7633 if (*eap->arg == '@')
7634 eap->arg = skiptowhite(eap->arg);
7635 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007636
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007637 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
7638 && STRLEN(eap->arg) > 4)
7639 {
7640 int delim = *eap->arg;
7641
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007642 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007643 if (*p == delim)
7644 {
7645 eap->arg = p + 1;
7646 has_expr = TRUE;
7647 }
7648 }
7649
Bram Moolenaarecac5912021-01-05 19:23:28 +01007650 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
7651 {
7652 // TODO: should only expand when appropriate for the command
7653 eap->arg = skiptowhite(eap->arg);
7654 has_expr = TRUE;
7655 }
7656
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007657 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007658 {
7659 int count = 0;
7660 char_u *start = skipwhite(line);
7661
7662 // :cmd xxx`=expr1`yyy`=expr2`zzz
7663 // PUSHS ":cmd xxx"
7664 // eval expr1
7665 // PUSHS "yyy"
7666 // eval expr2
7667 // PUSHS "zzz"
7668 // EXECCONCAT 5
7669 for (;;)
7670 {
7671 if (p > start)
7672 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007673 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007674 ++count;
7675 }
7676 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007677 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007678 return NULL;
7679 may_generate_2STRING(-1, cctx);
7680 ++count;
7681 p = skipwhite(p);
7682 if (*p != '`')
7683 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007684 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007685 return NULL;
7686 }
7687 start = p + 1;
7688
7689 p = (char_u *)strstr((char *)start, "`=");
7690 if (p == NULL)
7691 {
7692 if (*skipwhite(start) != NUL)
7693 {
7694 generate_PUSHS(cctx, vim_strsave(start));
7695 ++count;
7696 }
7697 break;
7698 }
7699 }
7700 generate_EXECCONCAT(cctx, count);
7701 }
7702 else
7703 generate_EXEC(cctx, line);
7704
7705theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007706 if (*nextcmd != NUL)
7707 {
7708 // the parser expects a pointer to the bar, put it back
7709 --nextcmd;
7710 *nextcmd = '|';
7711 }
7712
7713 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007714}
7715
7716/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007717 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007718 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007719 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007720 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007721add_def_function(ufunc_T *ufunc)
7722{
7723 dfunc_T *dfunc;
7724
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007725 if (def_functions.ga_len == 0)
7726 {
7727 // The first position is not used, so that a zero uf_dfunc_idx means it
7728 // wasn't set.
7729 if (ga_grow(&def_functions, 1) == FAIL)
7730 return FAIL;
7731 ++def_functions.ga_len;
7732 }
7733
Bram Moolenaar09689a02020-05-09 22:50:08 +02007734 // Add the function to "def_functions".
7735 if (ga_grow(&def_functions, 1) == FAIL)
7736 return FAIL;
7737 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7738 CLEAR_POINTER(dfunc);
7739 dfunc->df_idx = def_functions.ga_len;
7740 ufunc->uf_dfunc_idx = dfunc->df_idx;
7741 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01007742 dfunc->df_name = vim_strsave(ufunc->uf_name);
7743 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007744 ++def_functions.ga_len;
7745 return OK;
7746}
7747
7748/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007749 * After ex_function() has collected all the function lines: parse and compile
7750 * the lines into instructions.
7751 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007752 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
7753 * the return statement (used for lambda). When uf_ret_type is already set
7754 * then check that it matches.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007755 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007756 * This can be used recursively through compile_lambda(), which may reallocate
7757 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007758 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007759 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007760 int
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007761compile_def_function(ufunc_T *ufunc, int check_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007762{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007763 char_u *line = NULL;
7764 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007765 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007766 cctx_T cctx;
7767 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007768 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007769 int ret = FAIL;
7770 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007771 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007772 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007773 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007774
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007775 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01007776 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007777 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007778 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007779 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7780 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01007781 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007782 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007783 else
7784 {
7785 if (add_def_function(ufunc) == FAIL)
7786 return FAIL;
7787 new_def_function = TRUE;
7788 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007789
Bram Moolenaar985116a2020-07-12 17:31:09 +02007790 ufunc->uf_def_status = UF_COMPILING;
7791
Bram Moolenaara80faa82020-04-12 19:37:17 +02007792 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007793 cctx.ctx_ufunc = ufunc;
7794 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007795 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007796 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7797 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7798 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7799 cctx.ctx_type_list = &ufunc->uf_type_list;
7800 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7801 instr = &cctx.ctx_instr;
7802
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007803 // Set the context to the function, it may be compiled when called from
7804 // another script. Set the script version to the most modern one.
7805 // The line number will be set in next_line_from_context().
7806 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007807 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7808
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007809 // Make sure error messages are OK.
7810 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7811 if (do_estack_push)
7812 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007813 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007814
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007815 if (ufunc->uf_def_args.ga_len > 0)
7816 {
7817 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007818 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007819 int i;
7820 char_u *arg;
7821 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007822 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007823
7824 // Produce instructions for the default values of optional arguments.
7825 // Store the instruction index in uf_def_arg_idx[] so that we know
7826 // where to start when the function is called, depending on the number
7827 // of arguments.
7828 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7829 if (ufunc->uf_def_arg_idx == NULL)
7830 goto erret;
7831 for (i = 0; i < count; ++i)
7832 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007833 garray_T *stack = &cctx.ctx_type_stack;
7834 type_T *val_type;
7835 int arg_idx = first_def_arg + i;
7836
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007837 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7838 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007839 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007840 goto erret;
7841
7842 // If no type specified use the type of the default value.
7843 // Otherwise check that the default value type matches the
7844 // specified type.
7845 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7846 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007847 {
7848 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007849 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007850 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007851 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7852 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007853 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007854
7855 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007856 goto erret;
7857 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007858 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007859
7860 if (did_set_arg_type)
7861 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007862 }
7863
7864 /*
7865 * Loop over all the lines of the function and generate instructions.
7866 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007867 for (;;)
7868 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007869 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007870 int starts_with_colon = FALSE;
7871 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007872 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007873
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007874 // Bail out on the first error to avoid a flood of errors and report
7875 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007876 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007877 goto erret;
7878
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007879 if (line != NULL && *line == '|')
7880 // the line continues after a '|'
7881 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007882 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007883 && !(*line == '#' && (line == cctx.ctx_line_start
7884 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007885 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007886 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007887 goto erret;
7888 }
7889 else
7890 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007891 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007892 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007893 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007894 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007895 }
7896
Bram Moolenaara80faa82020-04-12 19:37:17 +02007897 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007898 ea.cmdlinep = &line;
7899 ea.cmd = skipwhite(line);
7900
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007901 // Some things can be recognized by the first character.
7902 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007903 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007904 case '#':
Bram Moolenaare0de1712020-12-02 17:36:54 +01007905 // "#" starts a comment
7906 line = (char_u *)"";
7907 continue;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007908
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007909 case '}':
7910 {
7911 // "}" ends a block scope
7912 scopetype_T stype = cctx.ctx_scope == NULL
7913 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007914
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007915 if (stype == BLOCK_SCOPE)
7916 {
7917 compile_endblock(&cctx);
7918 line = ea.cmd;
7919 }
7920 else
7921 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007922 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007923 goto erret;
7924 }
7925 if (line != NULL)
7926 line = skipwhite(ea.cmd + 1);
7927 continue;
7928 }
7929
7930 case '{':
7931 // "{" starts a block scope
7932 // "{'a': 1}->func() is something else
7933 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7934 {
7935 line = compile_block(ea.cmd, &cctx);
7936 continue;
7937 }
7938 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007939 }
7940
7941 /*
7942 * COMMAND MODIFIERS
7943 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007944 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007945 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7946 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007947 {
7948 if (errormsg != NULL)
7949 goto erret;
7950 // empty line or comment
7951 line = (char_u *)"";
7952 continue;
7953 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007954 generate_cmdmods(&cctx, &local_cmdmod);
7955 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007956
Bram Moolenaare88c8e82020-11-01 17:03:37 +01007957 // Check if there was a colon after the last command modifier or before
7958 // the current position.
7959 for (p = ea.cmd; p >= line; --p)
7960 {
7961 if (*p == ':')
7962 starts_with_colon = TRUE;
7963 if (p < ea.cmd && !VIM_ISWHITE(*p))
7964 break;
7965 }
7966
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007967 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007968 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007969 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007970 {
7971 if (*ea.cmd == '(')
7972 // not for "call()"
7973 ea.cmd = p;
7974 else
7975 ea.cmd = skipwhite(ea.cmd);
7976 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007977
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007978 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007979 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01007980 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007981
Bram Moolenaar17126b12021-01-07 22:03:02 +01007982 // Check for assignment after command modifiers.
7983 assign = may_compile_assignment(&ea, &line, &cctx);
7984 if (assign == OK)
7985 goto nextline;
7986 if (assign == FAIL)
7987 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007988 }
7989
7990 /*
7991 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007992 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007993 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007994 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007995 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007996 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007997 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007998 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007999 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008000 if (!starts_with_colon)
8001 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008002 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008003 goto erret;
8004 }
8005 if (ends_excmd2(line, ea.cmd))
8006 {
8007 // A range without a command: jump to the line.
8008 // TODO: compile to a more efficient command, possibly
8009 // calling parse_cmd_address().
8010 ea.cmdidx = CMD_SIZE;
8011 line = compile_exec(line, &ea, &cctx);
8012 goto nextline;
8013 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008014 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008015 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008016 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +01008017 : (int (*)(char_u *, size_t, void *, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008018 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008019
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008020 if (p == NULL)
8021 {
8022 if (cctx.ctx_skip != SKIP_YES)
8023 emsg(_(e_ambiguous_use_of_user_defined_command));
8024 goto erret;
8025 }
8026
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008027 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8028 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008029 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008030 {
8031 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008032 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008033 }
8034
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008035 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008036 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008037 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008038 // CMD_var cannot happen, compile_assignment() above would be
8039 // used. Most likely an assignment to a non-existing variable.
8040 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008041 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008042 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008043 }
8044
Bram Moolenaar3988f642020-08-27 22:43:03 +02008045 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008046 && ea.cmdidx != CMD_elseif
8047 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008048 && ea.cmdidx != CMD_endif
8049 && ea.cmdidx != CMD_endfor
8050 && ea.cmdidx != CMD_endwhile
8051 && ea.cmdidx != CMD_catch
8052 && ea.cmdidx != CMD_finally
8053 && ea.cmdidx != CMD_endtry)
8054 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008055 emsg(_(e_unreachable_code_after_return));
8056 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008057 }
8058
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008059 p = skipwhite(p);
8060 if (ea.cmdidx != CMD_SIZE
8061 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8062 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008063 if (ea.cmdidx >= 0)
8064 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008065 if ((ea.argt & EX_BANG) && *p == '!')
8066 {
8067 ea.forceit = TRUE;
8068 p = skipwhite(p + 1);
8069 }
8070 }
8071
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008072 switch (ea.cmdidx)
8073 {
8074 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008075 ea.arg = p;
8076 line = compile_nested_function(&ea, &cctx);
8077 break;
8078
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008079 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008080 // TODO: should we allow this, e.g. to declare a global
8081 // function?
8082 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008083 goto erret;
8084
8085 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008086 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008087 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008088 break;
8089
8090 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008091 emsg(_(e_cannot_use_let_in_vim9_script));
8092 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008093 case CMD_var:
8094 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008095 case CMD_const:
8096 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008097 if (line == p)
8098 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008099 break;
8100
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008101 case CMD_unlet:
8102 case CMD_unlockvar:
8103 case CMD_lockvar:
8104 line = compile_unletlock(p, &ea, &cctx);
8105 break;
8106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008107 case CMD_import:
8108 line = compile_import(p, &cctx);
8109 break;
8110
8111 case CMD_if:
8112 line = compile_if(p, &cctx);
8113 break;
8114 case CMD_elseif:
8115 line = compile_elseif(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_else:
8119 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008120 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008121 break;
8122 case CMD_endif:
8123 line = compile_endif(p, &cctx);
8124 break;
8125
8126 case CMD_while:
8127 line = compile_while(p, &cctx);
8128 break;
8129 case CMD_endwhile:
8130 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008131 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008132 break;
8133
8134 case CMD_for:
8135 line = compile_for(p, &cctx);
8136 break;
8137 case CMD_endfor:
8138 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008139 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008140 break;
8141 case CMD_continue:
8142 line = compile_continue(p, &cctx);
8143 break;
8144 case CMD_break:
8145 line = compile_break(p, &cctx);
8146 break;
8147
8148 case CMD_try:
8149 line = compile_try(p, &cctx);
8150 break;
8151 case CMD_catch:
8152 line = compile_catch(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_finally:
8156 line = compile_finally(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_endtry:
8160 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008161 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008162 break;
8163 case CMD_throw:
8164 line = compile_throw(p, &cctx);
8165 break;
8166
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008167 case CMD_eval:
8168 if (compile_expr0(&p, &cctx) == FAIL)
8169 goto erret;
8170
Bram Moolenaar3988f642020-08-27 22:43:03 +02008171 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008172 generate_instr_drop(&cctx, ISN_DROP, 1);
8173
8174 line = skipwhite(p);
8175 break;
8176
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008177 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008178 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008179 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008180 case CMD_echomsg:
8181 case CMD_echoerr:
8182 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008183 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008184
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008185 case CMD_put:
8186 ea.cmd = cmd;
8187 line = compile_put(p, &ea, &cctx);
8188 break;
8189
Bram Moolenaar3988f642020-08-27 22:43:03 +02008190 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008191
Bram Moolenaarae616492020-07-28 20:07:27 +02008192 case CMD_append:
8193 case CMD_change:
8194 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008195 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008196 case CMD_xit:
8197 not_in_vim9(&ea);
8198 goto erret;
8199
Bram Moolenaar002262f2020-07-08 17:47:57 +02008200 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008201 if (cctx.ctx_skip != SKIP_YES)
8202 {
8203 semsg(_(e_invalid_command_str), ea.cmd);
8204 goto erret;
8205 }
8206 // We don't check for a next command here.
8207 line = (char_u *)"";
8208 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008209
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008210 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008211 if (cctx.ctx_skip == SKIP_YES)
8212 {
8213 // We don't check for a next command here.
8214 line = (char_u *)"";
8215 }
8216 else
8217 {
8218 // Not recognized, execute with do_cmdline_cmd().
8219 ea.arg = p;
8220 line = compile_exec(line, &ea, &cctx);
8221 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008222 break;
8223 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008224nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008225 if (line == NULL)
8226 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008227 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008228
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008229 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008230 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008231
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008232 if (cctx.ctx_type_stack.ga_len < 0)
8233 {
8234 iemsg("Type stack underflow");
8235 goto erret;
8236 }
8237 }
8238
8239 if (cctx.ctx_scope != NULL)
8240 {
8241 if (cctx.ctx_scope->se_type == IF_SCOPE)
8242 emsg(_(e_endif));
8243 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8244 emsg(_(e_endwhile));
8245 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8246 emsg(_(e_endfor));
8247 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008248 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008249 goto erret;
8250 }
8251
Bram Moolenaarefd88552020-06-18 20:50:10 +02008252 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008253 {
8254 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8255 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008256 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008257 goto erret;
8258 }
8259
8260 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008261 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008262 }
8263
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008264 {
8265 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8266 + ufunc->uf_dfunc_idx;
8267 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008268 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008269 dfunc->df_instr = instr->ga_data;
8270 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008271 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008272 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008273 if (cctx.ctx_outer_used)
8274 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008275 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008276 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008277
8278 ret = OK;
8279
8280erret:
8281 if (ret == FAIL)
8282 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008283 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008284 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8285 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008286
8287 for (idx = 0; idx < instr->ga_len; ++idx)
8288 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008289 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008290 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008291
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008292 // If using the last entry in the table and it was added above, we
8293 // might as well remove it.
8294 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008295 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008296 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008297 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008298 ufunc->uf_dfunc_idx = 0;
8299 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008300 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008301
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008302 while (cctx.ctx_scope != NULL)
8303 drop_scope(&cctx);
8304
Bram Moolenaar20431c92020-03-20 18:39:46 +01008305 // Don't execute this function body.
8306 ga_clear_strings(&ufunc->uf_lines);
8307
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008308 if (errormsg != NULL)
8309 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008310 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008311 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008312 }
8313
8314 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008315 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008316 if (do_estack_push)
8317 estack_pop();
8318
Bram Moolenaar20431c92020-03-20 18:39:46 +01008319 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008320 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008321 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008322 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008323}
8324
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008325 void
8326set_function_type(ufunc_T *ufunc)
8327{
8328 int varargs = ufunc->uf_va_name != NULL;
8329 int argcount = ufunc->uf_args.ga_len;
8330
8331 // Create a type for the function, with the return type and any
8332 // argument types.
8333 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8334 // The type is included in "tt_args".
8335 if (argcount > 0 || varargs)
8336 {
8337 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8338 argcount, &ufunc->uf_type_list);
8339 // Add argument types to the function type.
8340 if (func_type_add_arg_types(ufunc->uf_func_type,
8341 argcount + varargs,
8342 &ufunc->uf_type_list) == FAIL)
8343 return;
8344 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8345 ufunc->uf_func_type->tt_min_argcount =
8346 argcount - ufunc->uf_def_args.ga_len;
8347 if (ufunc->uf_arg_types == NULL)
8348 {
8349 int i;
8350
8351 // lambda does not have argument types.
8352 for (i = 0; i < argcount; ++i)
8353 ufunc->uf_func_type->tt_args[i] = &t_any;
8354 }
8355 else
8356 mch_memmove(ufunc->uf_func_type->tt_args,
8357 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8358 if (varargs)
8359 {
8360 ufunc->uf_func_type->tt_args[argcount] =
8361 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8362 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8363 }
8364 }
8365 else
8366 // No arguments, can use a predefined type.
8367 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8368 argcount, &ufunc->uf_type_list);
8369}
8370
8371
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008372/*
8373 * Delete an instruction, free what it contains.
8374 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008375 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008376delete_instr(isn_T *isn)
8377{
8378 switch (isn->isn_type)
8379 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008380 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008381 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008382 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008383 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008384 case ISN_LOADENV:
8385 case ISN_LOADG:
8386 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008387 case ISN_LOADT:
8388 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008389 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008390 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008391 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008392 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008393 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008394 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008395 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008396 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008397 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008398 case ISN_STOREW:
8399 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008400 vim_free(isn->isn_arg.string);
8401 break;
8402
8403 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008404 case ISN_STORES:
8405 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008406 break;
8407
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008408 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008409 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008410 vim_free(isn->isn_arg.unlet.ul_name);
8411 break;
8412
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008413 case ISN_STOREOPT:
8414 vim_free(isn->isn_arg.storeopt.so_name);
8415 break;
8416
8417 case ISN_PUSHBLOB: // push blob isn_arg.blob
8418 blob_unref(isn->isn_arg.blob);
8419 break;
8420
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008421 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008422#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008423 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008424#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008425 break;
8426
8427 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008428#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008429 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008430#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008431 break;
8432
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008433 case ISN_UCALL:
8434 vim_free(isn->isn_arg.ufunc.cuf_name);
8435 break;
8436
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008437 case ISN_FUNCREF:
8438 {
8439 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8440 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01008441 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008442
Bram Moolenaar077a4232020-12-22 18:33:27 +01008443 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
8444 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02008445 }
8446 break;
8447
8448 case ISN_DCALL:
8449 {
8450 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8451 + isn->isn_arg.dfunc.cdf_idx;
8452
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008453 if (dfunc->df_ufunc != NULL
8454 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008455 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008456 }
8457 break;
8458
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008459 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008460 {
8461 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8462 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8463
8464 if (ufunc != NULL)
8465 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008466 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02008467 func_ptr_unref(ufunc);
8468 }
8469
8470 vim_free(lambda);
8471 vim_free(isn->isn_arg.newfunc.nf_global);
8472 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008473 break;
8474
Bram Moolenaar5e654232020-09-16 15:22:00 +02008475 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01008476 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02008477 free_type(isn->isn_arg.type.ct_type);
8478 break;
8479
Bram Moolenaar02194d22020-10-24 23:08:38 +02008480 case ISN_CMDMOD:
8481 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8482 ->cmod_filter_regmatch.regprog);
8483 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8484 break;
8485
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008486 case ISN_LOADSCRIPT:
8487 case ISN_STORESCRIPT:
8488 vim_free(isn->isn_arg.script.scriptref);
8489 break;
8490
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008491 case ISN_2BOOL:
8492 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008493 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008494 case ISN_ADDBLOB:
8495 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008496 case ISN_ANYINDEX:
8497 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008498 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008499 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008500 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008501 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008502 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008503 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008504 case ISN_COMPAREANY:
8505 case ISN_COMPAREBLOB:
8506 case ISN_COMPAREBOOL:
8507 case ISN_COMPAREDICT:
8508 case ISN_COMPAREFLOAT:
8509 case ISN_COMPAREFUNC:
8510 case ISN_COMPARELIST:
8511 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008512 case ISN_COMPARESPECIAL:
8513 case ISN_COMPARESTRING:
8514 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008515 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008516 case ISN_DROP:
8517 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008518 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008519 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008520 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008521 case ISN_EXECCONCAT:
8522 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008523 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008524 case ISN_GETITEM:
8525 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008526 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008527 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008528 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008529 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008530 case ISN_LOADBDICT:
8531 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008532 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008533 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008534 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008535 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008536 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008537 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008538 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008539 case ISN_NEGATENR:
8540 case ISN_NEWDICT:
8541 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008542 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008543 case ISN_OPFLOAT:
8544 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008545 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008546 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008547 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008548 case ISN_PUSHF:
8549 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008550 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008551 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008552 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01008553 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008554 case ISN_SHUFFLE:
8555 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008556 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01008557 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008558 case ISN_STORENR:
8559 case ISN_STOREOUTER:
8560 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008561 case ISN_STOREV:
8562 case ISN_STRINDEX:
8563 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008564 case ISN_THROW:
8565 case ISN_TRY:
Bram Moolenaar752fc692021-01-04 21:57:11 +01008566 case ISN_UNLETINDEX:
Bram Moolenaar792f7862020-11-23 08:31:18 +01008567 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008568 // nothing allocated
8569 break;
8570 }
8571}
8572
8573/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008574 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01008575 */
8576 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008577delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008578{
8579 int idx;
8580
8581 ga_clear(&dfunc->df_def_args_isn);
8582
8583 if (dfunc->df_instr != NULL)
8584 {
8585 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
8586 delete_instr(dfunc->df_instr + idx);
8587 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008588 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008589 }
8590
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008591 if (mark_deleted)
8592 dfunc->df_deleted = TRUE;
8593 if (dfunc->df_ufunc != NULL)
8594 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008595}
8596
8597/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008598 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008599 * function, unless another user function still uses it.
8600 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008601 */
8602 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008603unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008604{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008605 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008606 {
8607 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8608 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008609
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008610 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008611 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008612 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008613 ufunc->uf_dfunc_idx = 0;
8614 if (dfunc->df_ufunc == ufunc)
8615 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008616 }
8617}
8618
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008619/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008620 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008621 */
8622 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008623link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008624{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008625 if (ufunc->uf_dfunc_idx > 0)
8626 {
8627 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8628 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008629
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008630 ++dfunc->df_refcount;
8631 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008632}
8633
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008634#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008635/*
8636 * Free all functions defined with ":def".
8637 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008638 void
8639free_def_functions(void)
8640{
Bram Moolenaar20431c92020-03-20 18:39:46 +01008641 int idx;
8642
8643 for (idx = 0; idx < def_functions.ga_len; ++idx)
8644 {
8645 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
8646
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008647 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008648 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008649 }
8650
8651 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008652}
8653#endif
8654
8655
8656#endif // FEAT_EVAL