blob: a6bb56571efe599e3b193e92d84fbdeb9f4b6d8d [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
Bram Moolenaarab360522021-01-10 14:02:28 +0100111 int lv_from_outer; // nesting level, using ctx_outer scope
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200129 int ctx_has_closure; // set to one if a closures was created in
130 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200144
Bram Moolenaar02194d22020-10-24 23:08:38 +0200145 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100146};
147
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100148static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149
150/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100151 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100152 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100153 * If "lvar" is NULL only check if the variable can be found.
154 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100155 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100156 static int
157lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158{
159 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100160 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100161
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100162 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100163 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200164
165 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100166 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
167 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100168 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
169 if (STRNCMP(name, lvp->lv_name, len) == 0
170 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200171 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100172 if (lvar != NULL)
173 {
174 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100175 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100176 }
177 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200180
181 // Find local in outer function scope.
182 if (cctx->ctx_outer != NULL)
183 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100184 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200185 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100186 if (lvar != NULL)
187 {
188 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100189 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100190 }
191 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200192 }
193 }
194
Bram Moolenaar709664c2020-12-12 14:33:41 +0100195 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196}
197
198/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200199 * Lookup an argument in the current function and an enclosing function.
200 * Returns the argument index in "idxp"
201 * Returns the argument type in "type"
202 * Sets "gen_load_outer" to TRUE if found in outer scope.
203 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100204 */
205 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200206arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200207 char_u *name,
208 size_t len,
209 int *idxp,
210 type_T **type,
211 int *gen_load_outer,
212 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213{
214 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200215 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100217 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200218 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
220 {
221 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
222
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200223 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
224 {
225 if (idxp != NULL)
226 {
227 // Arguments are located above the frame pointer. One further
228 // if there is a vararg argument
229 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
230 + STACK_FRAME_SIZE)
231 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
232
233 if (cctx->ctx_ufunc->uf_arg_types != NULL)
234 *type = cctx->ctx_ufunc->uf_arg_types[idx];
235 else
236 *type = &t_any;
237 }
238 return OK;
239 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100240 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100241
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200242 va_name = cctx->ctx_ufunc->uf_va_name;
243 if (va_name != NULL
244 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
245 {
246 if (idxp != NULL)
247 {
248 // varargs is always the last argument
249 *idxp = -STACK_FRAME_SIZE - 1;
250 *type = cctx->ctx_ufunc->uf_va_type;
251 }
252 return OK;
253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200255 if (cctx->ctx_outer != NULL)
256 {
257 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200258 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200259 == OK)
260 {
Bram Moolenaarab360522021-01-10 14:02:28 +0100261 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200262 return OK;
263 }
264 }
265
266 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100267}
268
269/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200270 * Lookup a script-local variable in the current script, possibly defined in a
271 * block that contains the function "cctx->ctx_ufunc".
272 * "cctx" is NULL at the script level.
273 * if "len" is <= 0 "name" must be NUL terminated.
274 * Return NULL when not found.
275 */
276 static sallvar_T *
277find_script_var(char_u *name, size_t len, cctx_T *cctx)
278{
279 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
280 hashitem_T *hi;
281 int cc;
282 sallvar_T *sav;
283 ufunc_T *ufunc;
284
285 // Find the list of all script variables with the right name.
286 if (len > 0)
287 {
288 cc = name[len];
289 name[len] = NUL;
290 }
291 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
292 if (len > 0)
293 name[len] = cc;
294 if (HASHITEM_EMPTY(hi))
295 return NULL;
296
297 sav = HI2SAV(hi);
298 if (sav->sav_block_id == 0 || cctx == NULL)
299 // variable defined in the script scope or not in a function.
300 return sav;
301
302 // Go over the variables with this name and find one that was visible
303 // from the function.
304 ufunc = cctx->ctx_ufunc;
305 while (sav != NULL)
306 {
307 int idx;
308
309 // Go over the blocks that this function was defined in. If the
310 // variable block ID matches it was visible to the function.
311 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
312 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
313 return sav;
314 sav = sav->sav_next;
315 }
316
317 return NULL;
318}
319
320/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100321 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200322 */
323 static int
324script_is_vim9()
325{
326 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
327}
328
329/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200330 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200331 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
332 * without "s:".
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200333 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100334 * Returns OK or FAIL.
335 */
336 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200337script_var_exists(char_u *name, size_t len, int vim9script, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100338{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200339 int is_vim9_script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200341 if (current_sctx.sc_sid <= 0)
342 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200343 is_vim9_script = script_is_vim9();
344 if (vim9script && !is_vim9_script)
Bram Moolenaar53900992020-08-22 19:02:02 +0200345 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200346 if (is_vim9_script)
347 {
348 // Check script variables that were visible where the function was
349 // defined.
350 if (find_script_var(name, len, cctx) != NULL)
351 return OK;
352 }
353 else
354 {
355 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
356 dictitem_T *di;
357 int cc;
358
359 // Check script variables that are currently visible
360 cc = name[len];
361 name[len] = NUL;
362 di = find_var_in_ht(ht, 0, name, TRUE);
363 name[len] = cc;
364 if (di != NULL)
365 return OK;
366 }
367
368 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100369}
370
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100371/*
372 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200373 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200374 * Does not check the global namespace.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100375 * Return FAIL and give an error if it defined.
376 */
377 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200378check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100379{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200380 int c = p[len];
381 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200382
383 p[len] = NUL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200384 if (script_var_exists(p, len, FALSE, cctx) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100385 || (cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100386 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200387 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200388 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200389 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100390 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200391 // A local or script-local function can shadow a global function.
392 if (ufunc == NULL || !func_is_global(ufunc)
393 || (p[0] == 'g' && p[1] == ':'))
394 {
395 p[len] = c;
396 semsg(_(e_name_already_defined_str), p);
397 return FAIL;
398 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100399 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200400 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100401 return OK;
402}
403
Bram Moolenaar65b95452020-07-19 14:03:09 +0200404
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100405/////////////////////////////////////////////////////////////////////
406// Following generate_ functions expect the caller to call ga_grow().
407
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200408#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
409#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100411/*
412 * Generate an instruction without arguments.
413 * Returns a pointer to the new instruction, NULL if failed.
414 */
415 static isn_T *
416generate_instr(cctx_T *cctx, isntype_T isn_type)
417{
418 garray_T *instr = &cctx->ctx_instr;
419 isn_T *isn;
420
Bram Moolenaar080457c2020-03-03 21:53:32 +0100421 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100422 if (ga_grow(instr, 1) == FAIL)
423 return NULL;
424 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
425 isn->isn_type = isn_type;
426 isn->isn_lnum = cctx->ctx_lnum + 1;
427 ++instr->ga_len;
428
429 return isn;
430}
431
432/*
433 * Generate an instruction without arguments.
434 * "drop" will be removed from the stack.
435 * Returns a pointer to the new instruction, NULL if failed.
436 */
437 static isn_T *
438generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
439{
440 garray_T *stack = &cctx->ctx_type_stack;
441
Bram Moolenaar080457c2020-03-03 21:53:32 +0100442 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100443 stack->ga_len -= drop;
444 return generate_instr(cctx, isn_type);
445}
446
447/*
448 * Generate instruction "isn_type" and put "type" on the type stack.
449 */
450 static isn_T *
451generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
452{
453 isn_T *isn;
454 garray_T *stack = &cctx->ctx_type_stack;
455
456 if ((isn = generate_instr(cctx, isn_type)) == NULL)
457 return NULL;
458
459 if (ga_grow(stack, 1) == FAIL)
460 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200461 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462 ++stack->ga_len;
463
464 return isn;
465}
466
467/*
468 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200469 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100470 */
471 static int
472may_generate_2STRING(int offset, cctx_T *cctx)
473{
474 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200475 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100476 garray_T *stack = &cctx->ctx_type_stack;
477 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
478
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200479 switch ((*type)->tt_type)
480 {
481 // nothing to be done
482 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100483
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200484 // conversion possible
485 case VAR_SPECIAL:
486 case VAR_BOOL:
487 case VAR_NUMBER:
488 case VAR_FLOAT:
489 break;
490
491 // conversion possible (with runtime check)
492 case VAR_ANY:
493 case VAR_UNKNOWN:
494 isntype = ISN_2STRING_ANY;
495 break;
496
497 // conversion not possible
498 case VAR_VOID:
499 case VAR_BLOB:
500 case VAR_FUNC:
501 case VAR_PARTIAL:
502 case VAR_LIST:
503 case VAR_DICT:
504 case VAR_JOB:
505 case VAR_CHANNEL:
506 to_string_error((*type)->tt_type);
507 return FAIL;
508 }
509
510 *type = &t_string;
511 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100512 return FAIL;
513 isn->isn_arg.number = offset;
514
515 return OK;
516}
517
518 static int
519check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
520{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200521 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100522 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200523 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524 {
525 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200526 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200528 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 return FAIL;
530 }
531 return OK;
532}
533
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200534 static int
535generate_add_instr(
536 cctx_T *cctx,
537 vartype_T vartype,
538 type_T *type1,
539 type_T *type2)
540{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100541 garray_T *stack = &cctx->ctx_type_stack;
542 isn_T *isn = generate_instr_drop(cctx,
543 vartype == VAR_NUMBER ? ISN_OPNR
544 : vartype == VAR_LIST ? ISN_ADDLIST
545 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200546#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100547 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200548#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100549 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200550
551 if (vartype != VAR_LIST && vartype != VAR_BLOB
552 && type1->tt_type != VAR_ANY
553 && type2->tt_type != VAR_ANY
554 && check_number_or_float(
555 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
556 return FAIL;
557
558 if (isn != NULL)
559 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100560
561 // When concatenating two lists with different member types the member type
562 // becomes "any".
563 if (vartype == VAR_LIST
564 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
565 && type1->tt_member != type2->tt_member)
566 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
567
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200568 return isn == NULL ? FAIL : OK;
569}
570
571/*
572 * Get the type to use for an instruction for an operation on "type1" and
573 * "type2". If they are matching use a type-specific instruction. Otherwise
574 * fall back to runtime type checking.
575 */
576 static vartype_T
577operator_type(type_T *type1, type_T *type2)
578{
579 if (type1->tt_type == type2->tt_type
580 && (type1->tt_type == VAR_NUMBER
581 || type1->tt_type == VAR_LIST
582#ifdef FEAT_FLOAT
583 || type1->tt_type == VAR_FLOAT
584#endif
585 || type1->tt_type == VAR_BLOB))
586 return type1->tt_type;
587 return VAR_ANY;
588}
589
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100590/*
591 * Generate an instruction with two arguments. The instruction depends on the
592 * type of the arguments.
593 */
594 static int
595generate_two_op(cctx_T *cctx, char_u *op)
596{
597 garray_T *stack = &cctx->ctx_type_stack;
598 type_T *type1;
599 type_T *type2;
600 vartype_T vartype;
601 isn_T *isn;
602
Bram Moolenaar080457c2020-03-03 21:53:32 +0100603 RETURN_OK_IF_SKIP(cctx);
604
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200605 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100606 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
607 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200608 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100609
610 switch (*op)
611 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200612 case '+':
613 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615 break;
616
617 case '-':
618 case '*':
619 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
620 op) == FAIL)
621 return FAIL;
622 if (vartype == VAR_NUMBER)
623 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
624#ifdef FEAT_FLOAT
625 else if (vartype == VAR_FLOAT)
626 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
627#endif
628 else
629 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
630 if (isn != NULL)
631 isn->isn_arg.op.op_type = *op == '*'
632 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
633 break;
634
Bram Moolenaar4c683752020-04-05 21:38:23 +0200635 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100636 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200637 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100638 && type2->tt_type != VAR_NUMBER))
639 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200640 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100641 return FAIL;
642 }
643 isn = generate_instr_drop(cctx,
644 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
645 if (isn != NULL)
646 isn->isn_arg.op.op_type = EXPR_REM;
647 break;
648 }
649
650 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200651 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100652 {
653 type_T *type = &t_any;
654
655#ifdef FEAT_FLOAT
656 // float+number and number+float results in float
657 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
658 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
659 type = &t_float;
660#endif
661 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
662 }
663
664 return OK;
665}
666
667/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200668 * Get the instruction to use for comparing "type1" with "type2"
669 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200671 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100672get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673{
674 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675
Bram Moolenaar4c683752020-04-05 21:38:23 +0200676 if (type1 == VAR_UNKNOWN)
677 type1 = VAR_ANY;
678 if (type2 == VAR_UNKNOWN)
679 type2 = VAR_ANY;
680
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681 if (type1 == type2)
682 {
683 switch (type1)
684 {
685 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
686 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
687 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
688 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
689 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
690 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
691 case VAR_LIST: isntype = ISN_COMPARELIST; break;
692 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
693 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694 default: isntype = ISN_COMPAREANY; break;
695 }
696 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200697 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100698 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
699 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
700 isntype = ISN_COMPAREANY;
701
Bram Moolenaar657137c2021-01-09 15:45:23 +0100702 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100703 && (isntype == ISN_COMPAREBOOL
704 || isntype == ISN_COMPARESPECIAL
705 || isntype == ISN_COMPARENR
706 || isntype == ISN_COMPAREFLOAT))
707 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200708 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100709 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200710 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711 }
712 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100713 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100714 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
715 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100716 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
717 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100718 && (type1 == VAR_BLOB || type2 == VAR_BLOB
719 || type1 == VAR_LIST || type2 == VAR_LIST))))
720 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200721 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200723 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200725 return isntype;
726}
727
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200728 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100729check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200730{
731 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
732 return FAIL;
733 return OK;
734}
735
Bram Moolenaara5565e42020-05-09 15:44:01 +0200736/*
737 * Generate an ISN_COMPARE* instruction with a boolean result.
738 */
739 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100740generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200741{
742 isntype_T isntype;
743 isn_T *isn;
744 garray_T *stack = &cctx->ctx_type_stack;
745 vartype_T type1;
746 vartype_T type2;
747
748 RETURN_OK_IF_SKIP(cctx);
749
750 // Get the known type of the two items on the stack. If they are matching
751 // use a type-specific instruction. Otherwise fall back to runtime type
752 // checking.
753 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
754 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100755 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200756 if (isntype == ISN_DROP)
757 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758
759 if ((isn = generate_instr(cctx, isntype)) == NULL)
760 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100761 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762 isn->isn_arg.op.op_ic = ic;
763
764 // takes two arguments, puts one bool back
765 if (stack->ga_len >= 2)
766 {
767 --stack->ga_len;
768 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
769 }
770
771 return OK;
772}
773
774/*
775 * Generate an ISN_2BOOL instruction.
776 */
777 static int
778generate_2BOOL(cctx_T *cctx, int invert)
779{
780 isn_T *isn;
781 garray_T *stack = &cctx->ctx_type_stack;
782
Bram Moolenaar080457c2020-03-03 21:53:32 +0100783 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
785 return FAIL;
786 isn->isn_arg.number = invert;
787
788 // type becomes bool
789 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
790
791 return OK;
792}
793
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200794/*
795 * Generate an ISN_COND2BOOL instruction.
796 */
797 static int
798generate_COND2BOOL(cctx_T *cctx)
799{
800 isn_T *isn;
801 garray_T *stack = &cctx->ctx_type_stack;
802
803 RETURN_OK_IF_SKIP(cctx);
804 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
805 return FAIL;
806
807 // type becomes bool
808 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
809
810 return OK;
811}
812
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200814generate_TYPECHECK(
815 cctx_T *cctx,
816 type_T *expected,
817 int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100818{
819 isn_T *isn;
820 garray_T *stack = &cctx->ctx_type_stack;
821
Bram Moolenaar080457c2020-03-03 21:53:32 +0100822 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
824 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200825 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826 isn->isn_arg.type.ct_off = offset;
827
Bram Moolenaar5e654232020-09-16 15:22:00 +0200828 // type becomes expected
829 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100830
831 return OK;
832}
833
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100834 static int
835generate_SETTYPE(
836 cctx_T *cctx,
837 type_T *expected)
838{
839 isn_T *isn;
840
841 RETURN_OK_IF_SKIP(cctx);
842 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
843 return FAIL;
844 isn->isn_arg.type.ct_type = alloc_type(expected);
845 return OK;
846}
847
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100848/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200849 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
850 * used. Return FALSE if the types will never match.
851 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100852 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200853use_typecheck(type_T *actual, type_T *expected)
854{
855 if (actual->tt_type == VAR_ANY
856 || actual->tt_type == VAR_UNKNOWN
857 || (actual->tt_type == VAR_FUNC
858 && (expected->tt_type == VAR_FUNC
859 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100860 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
861 && ((actual->tt_member == &t_void)
862 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200863 return TRUE;
864 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
865 && actual->tt_type == expected->tt_type)
866 // This takes care of a nested list or dict.
867 return use_typecheck(actual->tt_member, expected->tt_member);
868 return FALSE;
869}
870
871/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200872 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200873 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200874 * - "actual" is a type that can be "expected" type: add a runtime check; or
875 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200876 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
877 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200878 */
879 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200880need_type(
881 type_T *actual,
882 type_T *expected,
883 int offset,
884 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200885 int silent,
886 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200887{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200888 if (expected == &t_bool && actual != &t_bool
889 && (actual->tt_flags & TTFLAG_BOOL_OK))
890 {
891 // Using "0", "1" or the result of an expression with "&&" or "||" as a
892 // boolean is OK but requires a conversion.
893 generate_2BOOL(cctx, FALSE);
894 return OK;
895 }
896
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200897 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200898 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200899
900 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200901 // If it's a constant a runtime check makes no sense.
902 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200903 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200904 generate_TYPECHECK(cctx, expected, offset);
905 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200906 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200907
908 if (!silent)
909 type_mismatch(expected, actual);
910 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200911}
912
913/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100914 * Check that the top of the type stack has a type that can be used as a
915 * condition. Give an error and return FAIL if not.
916 */
917 static int
918bool_on_stack(cctx_T *cctx)
919{
920 garray_T *stack = &cctx->ctx_type_stack;
921 type_T *type;
922
923 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
924 if (type == &t_bool)
925 return OK;
926
927 if (type == &t_any || type == &t_number)
928 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
929 // This requires a runtime type check.
930 return generate_COND2BOOL(cctx);
931
932 return need_type(type, &t_bool, -1, cctx, FALSE, FALSE);
933}
934
935/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936 * Generate an ISN_PUSHNR instruction.
937 */
938 static int
939generate_PUSHNR(cctx_T *cctx, varnumber_T number)
940{
941 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200942 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100943
Bram Moolenaar080457c2020-03-03 21:53:32 +0100944 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100945 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
946 return FAIL;
947 isn->isn_arg.number = number;
948
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200949 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200950 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +0100951 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100952 return OK;
953}
954
955/*
956 * Generate an ISN_PUSHBOOL instruction.
957 */
958 static int
959generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
960{
961 isn_T *isn;
962
Bram Moolenaar080457c2020-03-03 21:53:32 +0100963 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100964 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
965 return FAIL;
966 isn->isn_arg.number = number;
967
968 return OK;
969}
970
971/*
972 * Generate an ISN_PUSHSPEC instruction.
973 */
974 static int
975generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
976{
977 isn_T *isn;
978
Bram Moolenaar080457c2020-03-03 21:53:32 +0100979 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100980 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
981 return FAIL;
982 isn->isn_arg.number = number;
983
984 return OK;
985}
986
987#ifdef FEAT_FLOAT
988/*
989 * Generate an ISN_PUSHF instruction.
990 */
991 static int
992generate_PUSHF(cctx_T *cctx, float_T fnumber)
993{
994 isn_T *isn;
995
Bram Moolenaar080457c2020-03-03 21:53:32 +0100996 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100997 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
998 return FAIL;
999 isn->isn_arg.fnumber = fnumber;
1000
1001 return OK;
1002}
1003#endif
1004
1005/*
1006 * Generate an ISN_PUSHS instruction.
1007 * Consumes "str".
1008 */
1009 static int
1010generate_PUSHS(cctx_T *cctx, char_u *str)
1011{
1012 isn_T *isn;
1013
Bram Moolenaar508b5612021-01-02 18:17:26 +01001014 if (cctx->ctx_skip == SKIP_YES)
1015 {
1016 vim_free(str);
1017 return OK;
1018 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001019 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1020 return FAIL;
1021 isn->isn_arg.string = str;
1022
1023 return OK;
1024}
1025
1026/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001027 * Generate an ISN_PUSHCHANNEL instruction.
1028 * Consumes "channel".
1029 */
1030 static int
1031generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1032{
1033 isn_T *isn;
1034
Bram Moolenaar080457c2020-03-03 21:53:32 +01001035 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001036 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1037 return FAIL;
1038 isn->isn_arg.channel = channel;
1039
1040 return OK;
1041}
1042
1043/*
1044 * Generate an ISN_PUSHJOB instruction.
1045 * Consumes "job".
1046 */
1047 static int
1048generate_PUSHJOB(cctx_T *cctx, job_T *job)
1049{
1050 isn_T *isn;
1051
Bram Moolenaar080457c2020-03-03 21:53:32 +01001052 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001053 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001054 return FAIL;
1055 isn->isn_arg.job = job;
1056
1057 return OK;
1058}
1059
1060/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001061 * Generate an ISN_PUSHBLOB instruction.
1062 * Consumes "blob".
1063 */
1064 static int
1065generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1066{
1067 isn_T *isn;
1068
Bram Moolenaar080457c2020-03-03 21:53:32 +01001069 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001070 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1071 return FAIL;
1072 isn->isn_arg.blob = blob;
1073
1074 return OK;
1075}
1076
1077/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001078 * Generate an ISN_PUSHFUNC instruction with name "name".
1079 * Consumes "name".
1080 */
1081 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001082generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001083{
1084 isn_T *isn;
1085
Bram Moolenaar080457c2020-03-03 21:53:32 +01001086 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001087 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001088 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001089 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001090
1091 return OK;
1092}
1093
1094/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001095 * Generate an ISN_GETITEM instruction with "index".
1096 */
1097 static int
1098generate_GETITEM(cctx_T *cctx, int index)
1099{
1100 isn_T *isn;
1101 garray_T *stack = &cctx->ctx_type_stack;
1102 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1103 type_T *item_type = &t_any;
1104
1105 RETURN_OK_IF_SKIP(cctx);
1106
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001107 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001108 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001109 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001110 emsg(_(e_listreq));
1111 return FAIL;
1112 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001113 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001114 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1115 return FAIL;
1116 isn->isn_arg.number = index;
1117
1118 // add the item type to the type stack
1119 if (ga_grow(stack, 1) == FAIL)
1120 return FAIL;
1121 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1122 ++stack->ga_len;
1123 return OK;
1124}
1125
1126/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001127 * Generate an ISN_SLICE instruction with "count".
1128 */
1129 static int
1130generate_SLICE(cctx_T *cctx, int count)
1131{
1132 isn_T *isn;
1133
1134 RETURN_OK_IF_SKIP(cctx);
1135 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1136 return FAIL;
1137 isn->isn_arg.number = count;
1138 return OK;
1139}
1140
1141/*
1142 * Generate an ISN_CHECKLEN instruction with "min_len".
1143 */
1144 static int
1145generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1146{
1147 isn_T *isn;
1148
1149 RETURN_OK_IF_SKIP(cctx);
1150
1151 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1152 return FAIL;
1153 isn->isn_arg.checklen.cl_min_len = min_len;
1154 isn->isn_arg.checklen.cl_more_OK = more_OK;
1155
1156 return OK;
1157}
1158
1159/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001160 * Generate an ISN_STORE instruction.
1161 */
1162 static int
1163generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1164{
1165 isn_T *isn;
1166
Bram Moolenaar080457c2020-03-03 21:53:32 +01001167 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001168 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1169 return FAIL;
1170 if (name != NULL)
1171 isn->isn_arg.string = vim_strsave(name);
1172 else
1173 isn->isn_arg.number = idx;
1174
1175 return OK;
1176}
1177
1178/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001179 * Generate an ISN_STOREOUTER instruction.
1180 */
1181 static int
1182generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1183{
1184 isn_T *isn;
1185
1186 RETURN_OK_IF_SKIP(cctx);
1187 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1188 return FAIL;
1189 isn->isn_arg.outer.outer_idx = idx;
1190 isn->isn_arg.outer.outer_depth = level;
1191
1192 return OK;
1193}
1194
1195/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001196 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1197 */
1198 static int
1199generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1200{
1201 isn_T *isn;
1202
Bram Moolenaar080457c2020-03-03 21:53:32 +01001203 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1205 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001206 isn->isn_arg.storenr.stnr_idx = idx;
1207 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208
1209 return OK;
1210}
1211
1212/*
1213 * Generate an ISN_STOREOPT instruction
1214 */
1215 static int
1216generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1217{
1218 isn_T *isn;
1219
Bram Moolenaar080457c2020-03-03 21:53:32 +01001220 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001221 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001222 return FAIL;
1223 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1224 isn->isn_arg.storeopt.so_flags = opt_flags;
1225
1226 return OK;
1227}
1228
1229/*
1230 * Generate an ISN_LOAD or similar instruction.
1231 */
1232 static int
1233generate_LOAD(
1234 cctx_T *cctx,
1235 isntype_T isn_type,
1236 int idx,
1237 char_u *name,
1238 type_T *type)
1239{
1240 isn_T *isn;
1241
Bram Moolenaar080457c2020-03-03 21:53:32 +01001242 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001243 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1244 return FAIL;
1245 if (name != NULL)
1246 isn->isn_arg.string = vim_strsave(name);
1247 else
1248 isn->isn_arg.number = idx;
1249
1250 return OK;
1251}
1252
1253/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001254 * Generate an ISN_LOADOUTER instruction
1255 */
1256 static int
1257generate_LOADOUTER(
1258 cctx_T *cctx,
1259 int idx,
1260 int nesting,
1261 type_T *type)
1262{
1263 isn_T *isn;
1264
1265 RETURN_OK_IF_SKIP(cctx);
1266 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1267 return FAIL;
1268 isn->isn_arg.outer.outer_idx = idx;
1269 isn->isn_arg.outer.outer_depth = nesting;
1270
1271 return OK;
1272}
1273
1274/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001275 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001276 */
1277 static int
1278generate_LOADV(
1279 cctx_T *cctx,
1280 char_u *name,
1281 int error)
1282{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001283 int di_flags;
1284 int vidx = find_vim_var(name, &di_flags);
1285 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001286
Bram Moolenaar080457c2020-03-03 21:53:32 +01001287 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001288 if (vidx < 0)
1289 {
1290 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001291 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001292 return FAIL;
1293 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001294 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001295
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001296 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001297}
1298
1299/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001300 * Generate an ISN_UNLET instruction.
1301 */
1302 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001303generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001304{
1305 isn_T *isn;
1306
1307 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001308 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001309 return FAIL;
1310 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1311 isn->isn_arg.unlet.ul_forceit = forceit;
1312
1313 return OK;
1314}
1315
1316/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001317 * Generate an ISN_LOCKCONST instruction.
1318 */
1319 static int
1320generate_LOCKCONST(cctx_T *cctx)
1321{
1322 isn_T *isn;
1323
1324 RETURN_OK_IF_SKIP(cctx);
1325 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1326 return FAIL;
1327 return OK;
1328}
1329
1330/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331 * Generate an ISN_LOADS instruction.
1332 */
1333 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001334generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001335 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001336 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001338 int sid,
1339 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340{
1341 isn_T *isn;
1342
Bram Moolenaar080457c2020-03-03 21:53:32 +01001343 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001344 if (isn_type == ISN_LOADS)
1345 isn = generate_instr_type(cctx, isn_type, type);
1346 else
1347 isn = generate_instr_drop(cctx, isn_type, 1);
1348 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001350 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1351 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352
1353 return OK;
1354}
1355
1356/*
1357 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1358 */
1359 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001360generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 cctx_T *cctx,
1362 isntype_T isn_type,
1363 int sid,
1364 int idx,
1365 type_T *type)
1366{
1367 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001368 scriptref_T *sref;
1369 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001370
Bram Moolenaar080457c2020-03-03 21:53:32 +01001371 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001372 if (isn_type == ISN_LOADSCRIPT)
1373 isn = generate_instr_type(cctx, isn_type, type);
1374 else
1375 isn = generate_instr_drop(cctx, isn_type, 1);
1376 if (isn == NULL)
1377 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001378
1379 // This requires three arguments, which doesn't fit in an instruction, thus
1380 // we need to allocate a struct for this.
1381 sref = ALLOC_ONE(scriptref_T);
1382 if (sref == NULL)
1383 return FAIL;
1384 isn->isn_arg.script.scriptref = sref;
1385 sref->sref_sid = sid;
1386 sref->sref_idx = idx;
1387 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001388 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001389 return OK;
1390}
1391
1392/*
1393 * Generate an ISN_NEWLIST instruction.
1394 */
1395 static int
1396generate_NEWLIST(cctx_T *cctx, int count)
1397{
1398 isn_T *isn;
1399 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400 type_T *type;
1401 type_T *member;
1402
Bram Moolenaar080457c2020-03-03 21:53:32 +01001403 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1405 return FAIL;
1406 isn->isn_arg.number = count;
1407
Bram Moolenaar127542b2020-08-09 17:22:04 +02001408 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001409 if (count == 0)
1410 member = &t_void;
1411 else
1412 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001413 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1414 cctx->ctx_type_list);
1415 type = get_list_type(member, cctx->ctx_type_list);
1416
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001417 // drop the value types
1418 stack->ga_len -= count;
1419
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001420 // add the list type to the type stack
1421 if (ga_grow(stack, 1) == FAIL)
1422 return FAIL;
1423 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1424 ++stack->ga_len;
1425
1426 return OK;
1427}
1428
1429/*
1430 * Generate an ISN_NEWDICT instruction.
1431 */
1432 static int
1433generate_NEWDICT(cctx_T *cctx, int count)
1434{
1435 isn_T *isn;
1436 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001437 type_T *type;
1438 type_T *member;
1439
Bram Moolenaar080457c2020-03-03 21:53:32 +01001440 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1442 return FAIL;
1443 isn->isn_arg.number = count;
1444
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001445 if (count == 0)
1446 member = &t_void;
1447 else
1448 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001449 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1450 cctx->ctx_type_list);
1451 type = get_dict_type(member, cctx->ctx_type_list);
1452
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001453 // drop the key and value types
1454 stack->ga_len -= 2 * count;
1455
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001456 // add the dict type to the type stack
1457 if (ga_grow(stack, 1) == FAIL)
1458 return FAIL;
1459 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1460 ++stack->ga_len;
1461
1462 return OK;
1463}
1464
1465/*
1466 * Generate an ISN_FUNCREF instruction.
1467 */
1468 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001469generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470{
1471 isn_T *isn;
1472 garray_T *stack = &cctx->ctx_type_stack;
1473
Bram Moolenaar080457c2020-03-03 21:53:32 +01001474 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001475 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1476 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001477 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001478 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479
Bram Moolenaarab360522021-01-10 14:02:28 +01001480 // if the referenced function is a closure, it may use items further up in
1481 // the nested context, including this one.
1482 if (ufunc->uf_flags & FC_CLOSURE)
1483 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1484
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485 if (ga_grow(stack, 1) == FAIL)
1486 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001487 ((type_T **)stack->ga_data)[stack->ga_len] =
1488 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001489 ++stack->ga_len;
1490
1491 return OK;
1492}
1493
1494/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001495 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001496 * "lambda_name" and "func_name" must be in allocated memory and will be
1497 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001498 */
1499 static int
1500generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1501{
1502 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001503
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001504 if (cctx->ctx_skip == SKIP_YES)
1505 {
1506 vim_free(lambda_name);
1507 vim_free(func_name);
1508 return OK;
1509 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001510 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001511 {
1512 vim_free(lambda_name);
1513 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001514 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001515 }
1516 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001517 isn->isn_arg.newfunc.nf_global = func_name;
1518
1519 return OK;
1520}
1521
1522/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001523 * Generate an ISN_DEF instruction: list functions
1524 */
1525 static int
1526generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1527{
1528 isn_T *isn;
1529
1530 RETURN_OK_IF_SKIP(cctx);
1531 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1532 return FAIL;
1533 if (len > 0)
1534 {
1535 isn->isn_arg.string = vim_strnsave(name, len);
1536 if (isn->isn_arg.string == NULL)
1537 return FAIL;
1538 }
1539 return OK;
1540}
1541
1542/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001543 * Generate an ISN_JUMP instruction.
1544 */
1545 static int
1546generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1547{
1548 isn_T *isn;
1549 garray_T *stack = &cctx->ctx_type_stack;
1550
Bram Moolenaar080457c2020-03-03 21:53:32 +01001551 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1553 return FAIL;
1554 isn->isn_arg.jump.jump_when = when;
1555 isn->isn_arg.jump.jump_where = where;
1556
1557 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1558 --stack->ga_len;
1559
1560 return OK;
1561}
1562
1563 static int
1564generate_FOR(cctx_T *cctx, int loop_idx)
1565{
1566 isn_T *isn;
1567 garray_T *stack = &cctx->ctx_type_stack;
1568
Bram Moolenaar080457c2020-03-03 21:53:32 +01001569 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001570 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1571 return FAIL;
1572 isn->isn_arg.forloop.for_idx = loop_idx;
1573
1574 if (ga_grow(stack, 1) == FAIL)
1575 return FAIL;
1576 // type doesn't matter, will be stored next
1577 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1578 ++stack->ga_len;
1579
1580 return OK;
1581}
1582
1583/*
1584 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001585 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001586 * Return FAIL if the number of arguments is wrong.
1587 */
1588 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001589generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590{
1591 isn_T *isn;
1592 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001593 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001594 type_T **argtypes = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001595
Bram Moolenaar080457c2020-03-03 21:53:32 +01001596 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001597 argoff = check_internal_func(func_idx, argcount);
1598 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599 return FAIL;
1600
Bram Moolenaar389df252020-07-09 21:20:47 +02001601 if (method_call && argoff > 1)
1602 {
1603 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1604 return FAIL;
1605 isn->isn_arg.shuffle.shfl_item = argcount;
1606 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1607 }
1608
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001609 if (argcount > 0)
1610 {
1611 // Check the types of the arguments.
1612 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
1613 if (internal_func_check_arg_types(argtypes, func_idx, argcount) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001614 return FAIL;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001615 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001616
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001617 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1618 return FAIL;
1619 isn->isn_arg.bfunc.cbf_idx = func_idx;
1620 isn->isn_arg.bfunc.cbf_argcount = argcount;
1621
Bram Moolenaar94738d82020-10-21 14:25:07 +02001622 // Drop the argument types and push the return type.
1623 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001624 if (ga_grow(stack, 1) == FAIL)
1625 return FAIL;
1626 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001627 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001628 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629
1630 return OK;
1631}
1632
1633/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001634 * Generate an ISN_LISTAPPEND instruction. Works like add().
1635 * Argument count is already checked.
1636 */
1637 static int
1638generate_LISTAPPEND(cctx_T *cctx)
1639{
1640 garray_T *stack = &cctx->ctx_type_stack;
1641 type_T *list_type;
1642 type_T *item_type;
1643 type_T *expected;
1644
1645 // Caller already checked that list_type is a list.
1646 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1647 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1648 expected = list_type->tt_member;
1649 if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL)
1650 return FAIL;
1651
1652 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1653 return FAIL;
1654
1655 --stack->ga_len; // drop the argument
1656 return OK;
1657}
1658
1659/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001660 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1661 * Argument count is already checked.
1662 */
1663 static int
1664generate_BLOBAPPEND(cctx_T *cctx)
1665{
1666 garray_T *stack = &cctx->ctx_type_stack;
1667 type_T *item_type;
1668
1669 // Caller already checked that blob_type is a blob.
1670 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1671 if (need_type(item_type, &t_number, -1, cctx, FALSE, FALSE) == FAIL)
1672 return FAIL;
1673
1674 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1675 return FAIL;
1676
1677 --stack->ga_len; // drop the argument
1678 return OK;
1679}
1680
1681/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001682 * Generate an ISN_DCALL or ISN_UCALL instruction.
1683 * Return FAIL if the number of arguments is wrong.
1684 */
1685 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001686generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687{
1688 isn_T *isn;
1689 garray_T *stack = &cctx->ctx_type_stack;
1690 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001691 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001692
Bram Moolenaar080457c2020-03-03 21:53:32 +01001693 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001694 if (argcount > regular_args && !has_varargs(ufunc))
1695 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001696 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697 return FAIL;
1698 }
1699 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1700 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001701 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702 return FAIL;
1703 }
1704
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001705 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001706 {
1707 int i;
1708
1709 for (i = 0; i < argcount; ++i)
1710 {
1711 type_T *expected;
1712 type_T *actual;
1713
1714 if (i < regular_args)
1715 {
1716 if (ufunc->uf_arg_types == NULL)
1717 continue;
1718 expected = ufunc->uf_arg_types[i];
1719 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001720 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1721 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001722 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001723 else
1724 expected = ufunc->uf_va_type->tt_member;
1725 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001726 if (need_type(actual, expected, -argcount + i, cctx,
1727 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001728 {
1729 arg_type_mismatch(expected, actual, i + 1);
1730 return FAIL;
1731 }
1732 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001733 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001734 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1735 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001736 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001737 }
1738
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001739 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001740 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001741 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001743 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001744 {
1745 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1746 isn->isn_arg.dfunc.cdf_argcount = argcount;
1747 }
1748 else
1749 {
1750 // A user function may be deleted and redefined later, can't use the
1751 // ufunc pointer, need to look it up again at runtime.
1752 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1753 isn->isn_arg.ufunc.cuf_argcount = argcount;
1754 }
1755
1756 stack->ga_len -= argcount; // drop the arguments
1757 if (ga_grow(stack, 1) == FAIL)
1758 return FAIL;
1759 // add return value
1760 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1761 ++stack->ga_len;
1762
1763 return OK;
1764}
1765
1766/*
1767 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1768 */
1769 static int
1770generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1771{
1772 isn_T *isn;
1773 garray_T *stack = &cctx->ctx_type_stack;
1774
Bram Moolenaar080457c2020-03-03 21:53:32 +01001775 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1777 return FAIL;
1778 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1779 isn->isn_arg.ufunc.cuf_argcount = argcount;
1780
1781 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001782 if (ga_grow(stack, 1) == FAIL)
1783 return FAIL;
1784 // add return value
1785 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1786 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787
1788 return OK;
1789}
1790
1791/*
1792 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001793 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001794 */
1795 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001796generate_PCALL(
1797 cctx_T *cctx,
1798 int argcount,
1799 char_u *name,
1800 type_T *type,
1801 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802{
1803 isn_T *isn;
1804 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001805 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806
Bram Moolenaar080457c2020-03-03 21:53:32 +01001807 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001808
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001809 if (type->tt_type == VAR_ANY)
1810 ret_type = &t_any;
1811 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001812 {
1813 if (type->tt_argcount != -1)
1814 {
1815 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1816
1817 if (argcount < type->tt_min_argcount - varargs)
1818 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001819 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001820 return FAIL;
1821 }
1822 if (!varargs && argcount > type->tt_argcount)
1823 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001824 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001825 return FAIL;
1826 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001827 if (type->tt_args != NULL)
1828 {
1829 int i;
1830
1831 for (i = 0; i < argcount; ++i)
1832 {
1833 int offset = -argcount + i - 1;
1834 type_T *actual = ((type_T **)stack->ga_data)[
1835 stack->ga_len + offset];
1836 type_T *expected;
1837
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001838 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001839 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001840 type->tt_argcount - 1]->tt_member;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001841 else
1842 expected = type->tt_args[i];
1843 if (need_type(actual, expected, offset,
1844 cctx, TRUE, FALSE) == FAIL)
1845 {
1846 arg_type_mismatch(expected, actual, i + 1);
1847 return FAIL;
1848 }
1849 }
1850 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001851 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001852 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001853 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001854 else
1855 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001856 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001857 return FAIL;
1858 }
1859
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001860 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1861 return FAIL;
1862 isn->isn_arg.pfunc.cpf_top = at_top;
1863 isn->isn_arg.pfunc.cpf_argcount = argcount;
1864
1865 stack->ga_len -= argcount; // drop the arguments
1866
1867 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001868 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001869
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001870 // If partial is above the arguments it must be cleared and replaced with
1871 // the return value.
1872 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1873 return FAIL;
1874
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001875 return OK;
1876}
1877
1878/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001879 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001880 */
1881 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001882generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001883{
1884 isn_T *isn;
1885 garray_T *stack = &cctx->ctx_type_stack;
1886 type_T *type;
1887
Bram Moolenaar080457c2020-03-03 21:53:32 +01001888 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001889 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001890 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001891 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001892
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001893 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001894 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001895 if (type->tt_type != VAR_DICT && type != &t_any)
1896 {
1897 emsg(_(e_dictreq));
1898 return FAIL;
1899 }
1900 // change dict type to dict member type
1901 if (type->tt_type == VAR_DICT)
1902 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001903
1904 return OK;
1905}
1906
1907/*
1908 * Generate an ISN_ECHO instruction.
1909 */
1910 static int
1911generate_ECHO(cctx_T *cctx, int with_white, int count)
1912{
1913 isn_T *isn;
1914
Bram Moolenaar080457c2020-03-03 21:53:32 +01001915 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001916 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1917 return FAIL;
1918 isn->isn_arg.echo.echo_with_white = with_white;
1919 isn->isn_arg.echo.echo_count = count;
1920
1921 return OK;
1922}
1923
Bram Moolenaarad39c092020-02-26 18:23:43 +01001924/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001925 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001926 */
1927 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001928generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001929{
1930 isn_T *isn;
1931
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001932 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001933 return FAIL;
1934 isn->isn_arg.number = count;
1935
1936 return OK;
1937}
1938
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001939/*
1940 * Generate an ISN_PUT instruction.
1941 */
1942 static int
1943generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1944{
1945 isn_T *isn;
1946
1947 RETURN_OK_IF_SKIP(cctx);
1948 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1949 return FAIL;
1950 isn->isn_arg.put.put_regname = regname;
1951 isn->isn_arg.put.put_lnum = lnum;
1952 return OK;
1953}
1954
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955 static int
1956generate_EXEC(cctx_T *cctx, char_u *line)
1957{
1958 isn_T *isn;
1959
Bram Moolenaar080457c2020-03-03 21:53:32 +01001960 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001961 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1962 return FAIL;
1963 isn->isn_arg.string = vim_strsave(line);
1964 return OK;
1965}
1966
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001967 static int
1968generate_EXECCONCAT(cctx_T *cctx, int count)
1969{
1970 isn_T *isn;
1971
1972 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1973 return FAIL;
1974 isn->isn_arg.number = count;
1975 return OK;
1976}
1977
Bram Moolenaar08597872020-12-10 19:43:40 +01001978/*
1979 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1980 */
1981 static int
1982generate_RANGE(cctx_T *cctx, char_u *range)
1983{
1984 isn_T *isn;
1985 garray_T *stack = &cctx->ctx_type_stack;
1986
1987 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1988 return FAIL;
1989 isn->isn_arg.string = range;
1990
1991 if (ga_grow(stack, 1) == FAIL)
1992 return FAIL;
1993 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
1994 ++stack->ga_len;
1995 return OK;
1996}
1997
Bram Moolenaar792f7862020-11-23 08:31:18 +01001998 static int
1999generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2000{
2001 isn_T *isn;
2002
2003 RETURN_OK_IF_SKIP(cctx);
2004 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2005 return FAIL;
2006 isn->isn_arg.unpack.unp_count = var_count;
2007 isn->isn_arg.unpack.unp_semicolon = semicolon;
2008 return OK;
2009}
2010
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002012 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002013 */
2014 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002015generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002016{
2017 isn_T *isn;
2018
Bram Moolenaar02194d22020-10-24 23:08:38 +02002019 if (cmod->cmod_flags != 0
2020 || cmod->cmod_split != 0
2021 || cmod->cmod_verbose != 0
2022 || cmod->cmod_tab != 0
2023 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002024 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002025 cctx->ctx_has_cmdmod = TRUE;
2026
2027 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002028 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002029 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2030 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2031 return FAIL;
2032 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002033 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002034 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002035 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002036
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002037 return OK;
2038}
2039
2040 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002041generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002042{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002043 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2044 return FAIL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002045 return OK;
2046}
2047
2048/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002049 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002050 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002051 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002052 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002053reserve_local(
2054 cctx_T *cctx,
2055 char_u *name,
2056 size_t len,
2057 int isConst,
2058 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002059{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002060 lvar_T *lvar;
2061
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002062 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002063 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002064 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002065 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002066 }
2067
2068 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002069 return NULL;
2070 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002071 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002073 // Every local variable uses the next entry on the stack. We could re-use
2074 // the last ones when leaving a scope, but then variables used in a closure
2075 // might get overwritten. To keep things simple do not re-use stack
2076 // entries. This is less efficient, but memory is cheap these days.
2077 lvar->lv_idx = cctx->ctx_locals_count++;
2078
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002079 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002080 lvar->lv_const = isConst;
2081 lvar->lv_type = type;
2082
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002083 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002084}
2085
2086/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002087 * Remove local variables above "new_top".
2088 */
2089 static void
2090unwind_locals(cctx_T *cctx, int new_top)
2091{
2092 if (cctx->ctx_locals.ga_len > new_top)
2093 {
2094 int idx;
2095 lvar_T *lvar;
2096
2097 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2098 {
2099 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2100 vim_free(lvar->lv_name);
2101 }
2102 }
2103 cctx->ctx_locals.ga_len = new_top;
2104}
2105
2106/*
2107 * Free all local variables.
2108 */
2109 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002110free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002111{
2112 unwind_locals(cctx, 0);
2113 ga_clear(&cctx->ctx_locals);
2114}
2115
2116/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002117 * Find "name" in script-local items of script "sid".
2118 * Returns the index in "sn_var_vals" if found.
2119 * If found but not in "sn_var_vals" returns -1.
2120 * If not found returns -2.
2121 */
2122 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002123get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124{
2125 hashtab_T *ht;
2126 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002127 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002128 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002129 int idx;
2130
Bram Moolenaare3d46852020-08-29 13:39:17 +02002131 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002132 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002133 if (sid == current_sctx.sc_sid)
2134 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002135 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002136
2137 if (sav == NULL)
2138 return -2;
2139 idx = sav->sav_var_vals_idx;
2140 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2141 if (check_writable && sv->sv_const)
2142 semsg(_(e_readonlyvar), name);
2143 return idx;
2144 }
2145
2146 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002147 ht = &SCRIPT_VARS(sid);
2148 di = find_var_in_ht(ht, 0, name, TRUE);
2149 if (di == NULL)
2150 return -2;
2151
2152 // Now find the svar_T index in sn_var_vals.
2153 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2154 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002155 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002156 if (sv->sv_tv == &di->di_tv)
2157 {
2158 if (check_writable && sv->sv_const)
2159 semsg(_(e_readonlyvar), name);
2160 return idx;
2161 }
2162 }
2163 return -1;
2164}
2165
2166/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002167 * Find "name" in imported items of the current script or in "cctx" if not
2168 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169 */
2170 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002171find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002172{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002173 int idx;
2174
Bram Moolenaare3d46852020-08-29 13:39:17 +02002175 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002176 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002177 if (cctx != NULL)
2178 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2179 {
2180 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2181 + idx;
2182
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002183 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2184 : STRLEN(import->imp_name) == len
2185 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002186 return import;
2187 }
2188
Bram Moolenaarefa94442020-08-08 22:16:00 +02002189 return find_imported_in_script(name, len, current_sctx.sc_sid);
2190}
2191
2192 imported_T *
2193find_imported_in_script(char_u *name, size_t len, int sid)
2194{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002195 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002196 int idx;
2197
Bram Moolenaare3d46852020-08-29 13:39:17 +02002198 if (!SCRIPT_ID_VALID(sid))
2199 return NULL;
2200 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002201 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2202 {
2203 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2204
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002205 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2206 : STRLEN(import->imp_name) == len
2207 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002208 return import;
2209 }
2210 return NULL;
2211}
2212
2213/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002214 * Free all imported variables.
2215 */
2216 static void
2217free_imported(cctx_T *cctx)
2218{
2219 int idx;
2220
2221 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2222 {
2223 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2224
2225 vim_free(import->imp_name);
2226 }
2227 ga_clear(&cctx->ctx_imports);
2228}
2229
2230/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002231 * Return a pointer to the next line that isn't empty or only contains a
2232 * comment. Skips over white space.
2233 * Returns NULL if there is none.
2234 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002235 char_u *
2236peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002237{
2238 int lnum = cctx->ctx_lnum;
2239
2240 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2241 {
2242 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002243 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002244
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002245 // ignore NULLs inserted for continuation lines
2246 if (line != NULL)
2247 {
2248 p = skipwhite(line);
2249 if (*p != NUL && !vim9_comment_start(p))
2250 return p;
2251 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002252 }
2253 return NULL;
2254}
2255
2256/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002257 * Called when checking for a following operator at "arg". When the rest of
2258 * the line is empty or only a comment, peek the next line. If there is a next
2259 * line return a pointer to it and set "nextp".
2260 * Otherwise skip over white space.
2261 */
2262 static char_u *
2263may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2264{
2265 char_u *p = skipwhite(arg);
2266
2267 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002268 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002269 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002270 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002271 if (*nextp != NULL)
2272 return *nextp;
2273 }
2274 return p;
2275}
2276
2277/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002278 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002279 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002280 * Returns NULL when at the end.
2281 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002282 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002283next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002284{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002285 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002286
2287 do
2288 {
2289 ++cctx->ctx_lnum;
2290 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002291 {
2292 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002293 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002294 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002295 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002296 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002297 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002298 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002299 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002300 return line;
2301}
2302
2303/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002304 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002305 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002306 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002307 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2308 */
2309 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002310may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002311{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002312 *arg = skipwhite(whitep);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002313 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002314 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002315 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002316
2317 if (next == NULL)
2318 return FAIL;
2319 *arg = skipwhite(next);
2320 }
2321 return OK;
2322}
2323
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002324/*
2325 * Idem, and give an error when failed.
2326 */
2327 static int
2328may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2329{
2330 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2331 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002332 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002333 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002334 return FAIL;
2335 }
2336 return OK;
2337}
2338
2339
Bram Moolenaara5565e42020-05-09 15:44:01 +02002340// Structure passed between the compile_expr* functions to keep track of
2341// constants that have been parsed but for which no code was produced yet. If
2342// possible expressions on these constants are applied at compile time. If
2343// that is not possible, the code to push the constants needs to be generated
2344// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002345// Using 50 should be more than enough of 5 levels of ().
2346#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002347typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002348 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002349 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002350 int pp_is_const; // all generated code was constants, used for a
2351 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002352} ppconst_T;
2353
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002354static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002355static int compile_expr0(char_u **arg, cctx_T *cctx);
2356static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2357
Bram Moolenaara5565e42020-05-09 15:44:01 +02002358/*
2359 * Generate a PUSH instruction for "tv".
2360 * "tv" will be consumed or cleared.
2361 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2362 */
2363 static int
2364generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2365{
2366 if (tv != NULL)
2367 {
2368 switch (tv->v_type)
2369 {
2370 case VAR_UNKNOWN:
2371 break;
2372 case VAR_BOOL:
2373 generate_PUSHBOOL(cctx, tv->vval.v_number);
2374 break;
2375 case VAR_SPECIAL:
2376 generate_PUSHSPEC(cctx, tv->vval.v_number);
2377 break;
2378 case VAR_NUMBER:
2379 generate_PUSHNR(cctx, tv->vval.v_number);
2380 break;
2381#ifdef FEAT_FLOAT
2382 case VAR_FLOAT:
2383 generate_PUSHF(cctx, tv->vval.v_float);
2384 break;
2385#endif
2386 case VAR_BLOB:
2387 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2388 tv->vval.v_blob = NULL;
2389 break;
2390 case VAR_STRING:
2391 generate_PUSHS(cctx, tv->vval.v_string);
2392 tv->vval.v_string = NULL;
2393 break;
2394 default:
2395 iemsg("constant type not supported");
2396 clear_tv(tv);
2397 return FAIL;
2398 }
2399 tv->v_type = VAR_UNKNOWN;
2400 }
2401 return OK;
2402}
2403
2404/*
2405 * Generate code for any ppconst entries.
2406 */
2407 static int
2408generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2409{
2410 int i;
2411 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002412 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002413
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002414 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002415 for (i = 0; i < ppconst->pp_used; ++i)
2416 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2417 ret = FAIL;
2418 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002419 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002420 return ret;
2421}
2422
2423/*
2424 * Clear ppconst constants. Used when failing.
2425 */
2426 static void
2427clear_ppconst(ppconst_T *ppconst)
2428{
2429 int i;
2430
2431 for (i = 0; i < ppconst->pp_used; ++i)
2432 clear_tv(&ppconst->pp_tv[i]);
2433 ppconst->pp_used = 0;
2434}
2435
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002436/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002437 * Generate an instruction to load script-local variable "name", without the
2438 * leading "s:".
2439 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002440 */
2441 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002442compile_load_scriptvar(
2443 cctx_T *cctx,
2444 char_u *name, // variable NUL terminated
2445 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002446 char_u **end, // end of variable
2447 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002449 scriptitem_T *si;
2450 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002451 imported_T *import;
2452
Bram Moolenaare3d46852020-08-29 13:39:17 +02002453 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2454 return FAIL;
2455 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002456 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002457 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002458 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002459 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002460 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2461 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002462 }
2463 if (idx >= 0)
2464 {
2465 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2466
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002467 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002468 current_sctx.sc_sid, idx, sv->sv_type);
2469 return OK;
2470 }
2471
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002472 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 if (import != NULL)
2474 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002475 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002476 {
2477 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002478 char_u *exp_name;
2479 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002480 ufunc_T *ufunc;
2481 type_T *type;
2482
2483 // Used "import * as Name", need to lookup the member.
2484 if (*p != '.')
2485 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002486 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002487 return FAIL;
2488 }
2489 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002490 if (VIM_ISWHITE(*p))
2491 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002492 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002493 return FAIL;
2494 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002495
Bram Moolenaar1c991142020-07-04 13:15:31 +02002496 // isolate one name
2497 exp_name = p;
2498 while (eval_isnamec(*p))
2499 ++p;
2500 cc = *p;
2501 *p = NUL;
2502
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002503 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002504 *p = cc;
2505 p = skipwhite(p);
2506
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002507 // TODO: what if it is a function?
2508 if (idx < 0)
2509 return FAIL;
2510 *end = p;
2511
2512 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2513 import->imp_sid,
2514 idx,
2515 type);
2516 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002517 else if (import->imp_funcname != NULL)
2518 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002519 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002520 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2521 import->imp_sid,
2522 import->imp_var_vals_idx,
2523 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002524 return OK;
2525 }
2526
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002527 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002528 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 return FAIL;
2530}
2531
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002532 static int
2533generate_funcref(cctx_T *cctx, char_u *name)
2534{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002535 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002536
2537 if (ufunc == NULL)
2538 return FAIL;
2539
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002540 // Need to compile any default values to get the argument types.
2541 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2542 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2543 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002544 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002545}
2546
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547/*
2548 * Compile a variable name into a load instruction.
2549 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002550 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002551 * When "error" is FALSE do not give an error when not found.
2552 */
2553 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002554compile_load(
2555 char_u **arg,
2556 char_u *end_arg,
2557 cctx_T *cctx,
2558 int is_expr,
2559 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560{
2561 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002562 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002563 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002565 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566
2567 if (*(*arg + 1) == ':')
2568 {
2569 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002570 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002571 {
2572 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002573
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002574 switch (**arg)
2575 {
2576 case 'g': isn_type = ISN_LOADGDICT; break;
2577 case 'w': isn_type = ISN_LOADWDICT; break;
2578 case 't': isn_type = ISN_LOADTDICT; break;
2579 case 'b': isn_type = ISN_LOADBDICT; break;
2580 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002581 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002582 goto theend;
2583 }
2584 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2585 goto theend;
2586 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002587 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588 else
2589 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002590 isntype_T isn_type = ISN_DROP;
2591
2592 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2593 if (name == NULL)
2594 return FAIL;
2595
2596 switch (**arg)
2597 {
2598 case 'v': res = generate_LOADV(cctx, name, error);
2599 break;
2600 case 's': res = compile_load_scriptvar(cctx, name,
2601 NULL, NULL, error);
2602 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002603 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2604 isn_type = ISN_LOADG;
2605 else
2606 {
2607 isn_type = ISN_LOADAUTO;
2608 vim_free(name);
2609 name = vim_strnsave(*arg, end - *arg);
2610 if (name == NULL)
2611 return FAIL;
2612 }
2613 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002614 case 'w': isn_type = ISN_LOADW; break;
2615 case 't': isn_type = ISN_LOADT; break;
2616 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002617 default: // cannot happen, just in case
2618 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002619 goto theend;
2620 }
2621 if (isn_type != ISN_DROP)
2622 {
2623 // Global, Buffer-local, Window-local and Tabpage-local
2624 // variables can be defined later, thus we don't check if it
2625 // exists, give error at runtime.
2626 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2627 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628 }
2629 }
2630 else
2631 {
2632 size_t len = end - *arg;
2633 int idx;
2634 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002635 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002636
2637 name = vim_strnsave(*arg, end - *arg);
2638 if (name == NULL)
2639 return FAIL;
2640
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002641 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002643 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002644 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645 }
2646 else
2647 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002648 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002649
Bram Moolenaar709664c2020-12-12 14:33:41 +01002650 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002652 type = lvar.lv_type;
2653 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01002654 if (lvar.lv_from_outer != 0)
2655 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002656 else
2657 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658 }
2659 else
2660 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002661 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002662 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002663 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002664 || find_imported(name, 0, cctx) != NULL)
2665 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002666
Bram Moolenaar0f769812020-09-12 18:32:34 +02002667 // When evaluating an expression and the name starts with an
2668 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002669 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002670 if (res == FAIL && is_expr
2671 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002672 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002673 }
2674 }
2675 if (gen_load)
2676 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01002677 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002678 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002679 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002680 cctx->ctx_outer_used = TRUE;
2681 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002682 }
2683
2684 *arg = end;
2685
2686theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002687 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002688 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002689 vim_free(name);
2690 return res;
2691}
2692
2693/*
2694 * Compile the argument expressions.
2695 * "arg" points to just after the "(" and is advanced to after the ")"
2696 */
2697 static int
2698compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2699{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002700 char_u *p = *arg;
2701 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002702 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002703
Bram Moolenaare6085c52020-04-12 20:19:16 +02002704 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002706 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2707 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002708 if (*p == ')')
2709 {
2710 *arg = p + 1;
2711 return OK;
2712 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002713 if (must_end)
2714 {
2715 semsg(_(e_missing_comma_before_argument_str), p);
2716 return FAIL;
2717 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002718
Bram Moolenaara5565e42020-05-09 15:44:01 +02002719 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002720 return FAIL;
2721 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002722
2723 if (*p != ',' && *skipwhite(p) == ',')
2724 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002725 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002726 p = skipwhite(p);
2727 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002729 {
2730 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002731 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002732 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002733 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002734 else
2735 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002736 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002737 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002738 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002739failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002740 emsg(_(e_missing_close));
2741 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742}
2743
2744/*
2745 * Compile a function call: name(arg1, arg2)
2746 * "arg" points to "name", "arg + varlen" to the "(".
2747 * "argcount_init" is 1 for "value->method()"
2748 * Instructions:
2749 * EVAL arg1
2750 * EVAL arg2
2751 * BCALL / DCALL / UCALL
2752 */
2753 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002754compile_call(
2755 char_u **arg,
2756 size_t varlen,
2757 cctx_T *cctx,
2758 ppconst_T *ppconst,
2759 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760{
2761 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002762 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002763 int argcount = argcount_init;
2764 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002765 char_u fname_buf[FLEN_FIXED + 1];
2766 char_u *tofree = NULL;
2767 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002768 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002769 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002770 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771
Bram Moolenaara5565e42020-05-09 15:44:01 +02002772 // we can evaluate "has('name')" at compile time
2773 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2774 {
2775 char_u *s = skipwhite(*arg + varlen + 1);
2776 typval_T argvars[2];
2777
2778 argvars[0].v_type = VAR_UNKNOWN;
2779 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002780 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002781 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002782 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002783 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002784 if (*s == ')' && argvars[0].v_type == VAR_STRING
2785 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002786 {
2787 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2788
2789 *arg = s + 1;
2790 argvars[1].v_type = VAR_UNKNOWN;
2791 tv->v_type = VAR_NUMBER;
2792 tv->vval.v_number = 0;
2793 f_has(argvars, tv);
2794 clear_tv(&argvars[0]);
2795 ++ppconst->pp_used;
2796 return OK;
2797 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002798 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002799 }
2800
2801 if (generate_ppconst(cctx, ppconst) == FAIL)
2802 return FAIL;
2803
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 if (varlen >= sizeof(namebuf))
2805 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002806 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 return FAIL;
2808 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002809 vim_strncpy(namebuf, *arg, varlen);
2810 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811
2812 *arg = skipwhite(*arg + varlen + 1);
2813 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002814 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815
Bram Moolenaar03290b82020-12-19 16:30:44 +01002816 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002817 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818 {
2819 int idx;
2820
2821 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002822 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002823 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002824 {
2825 if (STRCMP(name, "add") == 0 && argcount == 2)
2826 {
2827 garray_T *stack = &cctx->ctx_type_stack;
2828 type_T *type = ((type_T **)stack->ga_data)[
2829 stack->ga_len - 2];
2830
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002831 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002832 if (type->tt_type == VAR_LIST)
2833 {
2834 // inline "add(list, item)" so that the type can be checked
2835 res = generate_LISTAPPEND(cctx);
2836 idx = -1;
2837 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002838 else if (type->tt_type == VAR_BLOB)
2839 {
2840 // inline "add(blob, nr)" so that the type can be checked
2841 res = generate_BLOBAPPEND(cctx);
2842 idx = -1;
2843 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002844 }
2845
2846 if (idx >= 0)
2847 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2848 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002849 else
2850 semsg(_(e_unknownfunc), namebuf);
2851 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 }
2853
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002854 // An argument or local variable can be a function reference, this
2855 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002856 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002857 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002858 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002859 // If we can find the function by name generate the right call.
2860 // Skip global functions here, a local funcref takes precedence.
2861 ufunc = find_func(name, FALSE, cctx);
2862 if (ufunc != NULL && !func_is_global(ufunc))
2863 {
2864 res = generate_CALL(cctx, ufunc, argcount);
2865 goto theend;
2866 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002867 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002868
2869 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002870 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002871 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002873 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002874 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002875 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002876 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01002877 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002878
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002879 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002880 goto theend;
2881 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882
Bram Moolenaar0f769812020-09-12 18:32:34 +02002883 // If we can find a global function by name generate the right call.
2884 if (ufunc != NULL)
2885 {
2886 res = generate_CALL(cctx, ufunc, argcount);
2887 goto theend;
2888 }
2889
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002890 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002891 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002892 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002893 res = generate_UCALL(cctx, name, argcount);
2894 else
2895 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002896
2897theend:
2898 vim_free(tofree);
2899 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900}
2901
2902// like NAMESPACE_CHAR but with 'a' and 'l'.
2903#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2904
2905/*
2906 * Find the end of a variable or function name. Unlike find_name_end() this
2907 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002908 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002909 * Return a pointer to just after the name. Equal to "arg" if there is no
2910 * valid name.
2911 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01002912 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002913to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914{
2915 char_u *p;
2916
2917 // Quick check for valid starting character.
2918 if (!eval_isnamec1(*arg))
2919 return arg;
2920
2921 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2922 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2923 // and can be used in slice "[n:]".
2924 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002925 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2927 break;
2928 return p;
2929}
2930
2931/*
2932 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002933 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002934 */
2935 char_u *
2936to_name_const_end(char_u *arg)
2937{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002938 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939 typval_T rettv;
2940
2941 if (p == arg && *arg == '[')
2942 {
2943
2944 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002945 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 p = arg;
2947 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 return p;
2949}
2950
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951/*
2952 * parse a list: [expr, expr]
2953 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002954 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955 */
2956 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002957compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958{
2959 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002960 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002962 int is_const;
2963 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002965 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002967 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002968 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002969 semsg(_(e_list_end), *arg);
2970 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002971 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002972 if (*p == ',')
2973 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002974 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002975 return FAIL;
2976 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002977 if (*p == ']')
2978 {
2979 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002980 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002981 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002982 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002983 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002984 if (!is_const)
2985 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 ++count;
2987 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002988 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002990 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2991 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002992 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002993 return FAIL;
2994 }
2995 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002996 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 p = skipwhite(p);
2998 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002999 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003001 ppconst->pp_is_const = is_all_const;
3002 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003}
3004
3005/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003006 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 * "*arg" points to the '{'.
Bram Moolenaare462f522020-12-27 14:43:30 +01003008 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 */
3010 static int
3011compile_lambda(char_u **arg, cctx_T *cctx)
3012{
Bram Moolenaare462f522020-12-27 14:43:30 +01003013 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 typval_T rettv;
3015 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003016 evalarg_T evalarg;
3017
3018 CLEAR_FIELD(evalarg);
3019 evalarg.eval_flags = EVAL_EVALUATE;
3020 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021
3022 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003023 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3024 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003025 {
3026 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003027 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003028 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003029
Bram Moolenaar65c44152020-12-24 15:14:01 +01003030 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003032 ++ufunc->uf_refcount;
3033 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034
Bram Moolenaar65c44152020-12-24 15:14:01 +01003035 // Compile the function into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003036 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003038 clear_evalarg(&evalarg, NULL);
3039
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003040 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003041 {
3042 // The return type will now be known.
3043 set_function_type(ufunc);
3044
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003045 // The function reference count will be 1. When the ISN_FUNCREF
3046 // instruction is deleted the reference count is decremented and the
3047 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003048 return generate_FUNCREF(cctx, ufunc);
3049 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003050
3051 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052 return FAIL;
3053}
3054
3055/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003056 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003057 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003058 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 */
3060 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003061compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062{
3063 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003064 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 int count = 0;
3066 dict_T *d = dict_alloc();
3067 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003068 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003069 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003070 int is_const;
3071 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072
3073 if (d == NULL)
3074 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003075 if (generate_ppconst(cctx, ppconst) == FAIL)
3076 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003077 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003079 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080
Bram Moolenaar23c55272020-06-21 16:58:13 +02003081 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003082 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003083 *arg = NULL;
3084 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003085 }
3086
3087 if (**arg == '}')
3088 break;
3089
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003090 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003092 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003094 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003095 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003096 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3099 if (isn->isn_type == ISN_PUSHS)
3100 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003101 else
3102 {
3103 type_T *keytype = ((type_T **)stack->ga_data)
Bram Moolenaar2bede172020-11-19 18:53:18 +01003104 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003105 if (need_type(keytype, &t_string, -1, cctx,
Bram Moolenaar2bede172020-11-19 18:53:18 +01003106 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003107 return FAIL;
3108 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003109 *arg = skipwhite(*arg);
3110 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003111 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003112 emsg(_(e_missing_matching_bracket_after_dict_key));
3113 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003114 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003115 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003117 else
3118 {
3119 // {"name": value},
3120 // {'name': value},
3121 // {name: value} use "name" as a literal key
3122 key = get_literal_key(arg);
3123 if (key == NULL)
3124 return FAIL;
3125 if (generate_PUSHS(cctx, key) == FAIL)
3126 return FAIL;
3127 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128
3129 // Check for duplicate keys, if using string keys.
3130 if (key != NULL)
3131 {
3132 item = dict_find(d, key, -1);
3133 if (item != NULL)
3134 {
3135 semsg(_(e_duplicate_key), key);
3136 goto failret;
3137 }
3138 item = dictitem_alloc(key);
3139 if (item != NULL)
3140 {
3141 item->di_tv.v_type = VAR_UNKNOWN;
3142 item->di_tv.v_lock = 0;
3143 if (dict_add(d, item) == FAIL)
3144 dictitem_free(item);
3145 }
3146 }
3147
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 if (**arg != ':')
3149 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003150 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003151 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003152 else
3153 semsg(_(e_missing_dict_colon), *arg);
3154 return FAIL;
3155 }
3156 whitep = *arg + 1;
3157 if (!IS_WHITE_OR_NUL(*whitep))
3158 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003159 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160 return FAIL;
3161 }
3162
Bram Moolenaar23c55272020-06-21 16:58:13 +02003163 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003164 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003165 *arg = NULL;
3166 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003167 }
3168
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003169 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003171 if (!is_const)
3172 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173 ++count;
3174
Bram Moolenaar2c330432020-04-13 14:41:35 +02003175 whitep = *arg;
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 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181 if (**arg == '}')
3182 break;
3183 if (**arg != ',')
3184 {
3185 semsg(_(e_missing_dict_comma), *arg);
3186 goto failret;
3187 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003188 if (IS_WHITE_OR_NUL(*whitep))
3189 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003190 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003191 return FAIL;
3192 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003193 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003194 if (!IS_WHITE_OR_NUL(*whitep))
3195 {
3196 semsg(_(e_white_space_required_after_str), ",");
3197 return FAIL;
3198 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 *arg = skipwhite(*arg + 1);
3200 }
3201
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 *arg = *arg + 1;
3203
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003204 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003205 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003206 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003207 *arg += STRLEN(*arg);
3208
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003210 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 return generate_NEWDICT(cctx, count);
3212
3213failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003214 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003215 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003216 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003217 *arg = (char_u *)"";
3218 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 dict_unref(d);
3220 return FAIL;
3221}
3222
3223/*
3224 * Compile "&option".
3225 */
3226 static int
3227compile_get_option(char_u **arg, cctx_T *cctx)
3228{
3229 typval_T rettv;
3230 char_u *start = *arg;
3231 int ret;
3232
3233 // parse the option and get the current value to get the type.
3234 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003235 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 if (ret == OK)
3237 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003238 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003239 char_u *name = vim_strnsave(start, *arg - start);
3240 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3241 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242
3243 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3244 vim_free(name);
3245 }
3246 clear_tv(&rettv);
3247
3248 return ret;
3249}
3250
3251/*
3252 * Compile "$VAR".
3253 */
3254 static int
3255compile_get_env(char_u **arg, cctx_T *cctx)
3256{
3257 char_u *start = *arg;
3258 int len;
3259 int ret;
3260 char_u *name;
3261
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 ++*arg;
3263 len = get_env_len(arg);
3264 if (len == 0)
3265 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003266 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 return FAIL;
3268 }
3269
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003270 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 name = vim_strnsave(start, len + 1);
3272 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3273 vim_free(name);
3274 return ret;
3275}
3276
3277/*
3278 * Compile "@r".
3279 */
3280 static int
3281compile_get_register(char_u **arg, cctx_T *cctx)
3282{
3283 int ret;
3284
3285 ++*arg;
3286 if (**arg == NUL)
3287 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003288 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289 return FAIL;
3290 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003291 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 {
3293 emsg_invreg(**arg);
3294 return FAIL;
3295 }
3296 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3297 ++*arg;
3298 return ret;
3299}
3300
3301/*
3302 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003303 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003304 */
3305 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003306apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003308 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309
3310 // this works from end to start
3311 while (p > start)
3312 {
3313 --p;
3314 if (*p == '-' || *p == '+')
3315 {
3316 // only '-' has an effect, for '+' we only check the type
3317#ifdef FEAT_FLOAT
3318 if (rettv->v_type == VAR_FLOAT)
3319 {
3320 if (*p == '-')
3321 rettv->vval.v_float = -rettv->vval.v_float;
3322 }
3323 else
3324#endif
3325 {
3326 varnumber_T val;
3327 int error = FALSE;
3328
3329 // tv_get_number_chk() accepts a string, but we don't want that
3330 // here
3331 if (check_not_string(rettv) == FAIL)
3332 return FAIL;
3333 val = tv_get_number_chk(rettv, &error);
3334 clear_tv(rettv);
3335 if (error)
3336 return FAIL;
3337 if (*p == '-')
3338 val = -val;
3339 rettv->v_type = VAR_NUMBER;
3340 rettv->vval.v_number = val;
3341 }
3342 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003343 else if (numeric_only)
3344 {
3345 ++p;
3346 break;
3347 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003348 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349 {
3350 int v = tv2bool(rettv);
3351
3352 // '!' is permissive in the type.
3353 clear_tv(rettv);
3354 rettv->v_type = VAR_BOOL;
3355 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3356 }
3357 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003358 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 return OK;
3360}
3361
3362/*
3363 * Recognize v: variables that are constants and set "rettv".
3364 */
3365 static void
3366get_vim_constant(char_u **arg, typval_T *rettv)
3367{
3368 if (STRNCMP(*arg, "v:true", 6) == 0)
3369 {
3370 rettv->v_type = VAR_BOOL;
3371 rettv->vval.v_number = VVAL_TRUE;
3372 *arg += 6;
3373 }
3374 else if (STRNCMP(*arg, "v:false", 7) == 0)
3375 {
3376 rettv->v_type = VAR_BOOL;
3377 rettv->vval.v_number = VVAL_FALSE;
3378 *arg += 7;
3379 }
3380 else if (STRNCMP(*arg, "v:null", 6) == 0)
3381 {
3382 rettv->v_type = VAR_SPECIAL;
3383 rettv->vval.v_number = VVAL_NULL;
3384 *arg += 6;
3385 }
3386 else if (STRNCMP(*arg, "v:none", 6) == 0)
3387 {
3388 rettv->v_type = VAR_SPECIAL;
3389 rettv->vval.v_number = VVAL_NONE;
3390 *arg += 6;
3391 }
3392}
3393
Bram Moolenaar657137c2021-01-09 15:45:23 +01003394 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003395get_compare_type(char_u *p, int *len, int *type_is)
3396{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003397 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003398 int i;
3399
3400 switch (p[0])
3401 {
3402 case '=': if (p[1] == '=')
3403 type = EXPR_EQUAL;
3404 else if (p[1] == '~')
3405 type = EXPR_MATCH;
3406 break;
3407 case '!': if (p[1] == '=')
3408 type = EXPR_NEQUAL;
3409 else if (p[1] == '~')
3410 type = EXPR_NOMATCH;
3411 break;
3412 case '>': if (p[1] != '=')
3413 {
3414 type = EXPR_GREATER;
3415 *len = 1;
3416 }
3417 else
3418 type = EXPR_GEQUAL;
3419 break;
3420 case '<': if (p[1] != '=')
3421 {
3422 type = EXPR_SMALLER;
3423 *len = 1;
3424 }
3425 else
3426 type = EXPR_SEQUAL;
3427 break;
3428 case 'i': if (p[1] == 's')
3429 {
3430 // "is" and "isnot"; but not a prefix of a name
3431 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3432 *len = 5;
3433 i = p[*len];
3434 if (!isalnum(i) && i != '_')
3435 {
3436 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3437 *type_is = TRUE;
3438 }
3439 }
3440 break;
3441 }
3442 return type;
3443}
3444
3445/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003446 * Skip over an expression, ignoring most errors.
3447 */
3448 static void
3449skip_expr_cctx(char_u **arg, cctx_T *cctx)
3450{
3451 evalarg_T evalarg;
3452
3453 CLEAR_FIELD(evalarg);
3454 evalarg.eval_cctx = cctx;
3455 skip_expr(arg, &evalarg);
3456}
3457
3458/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003459 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003460 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 */
3462 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003463compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003464{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003465 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003466
3467 // this works from end to start
3468 while (p > start)
3469 {
3470 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003471 while (VIM_ISWHITE(*p))
3472 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003473 if (*p == '-' || *p == '+')
3474 {
3475 int negate = *p == '-';
3476 isn_T *isn;
3477
3478 // TODO: check type
3479 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3480 {
3481 --p;
3482 if (*p == '-')
3483 negate = !negate;
3484 }
3485 // only '-' has an effect, for '+' we only check the type
3486 if (negate)
3487 isn = generate_instr(cctx, ISN_NEGATENR);
3488 else
3489 isn = generate_instr(cctx, ISN_CHECKNR);
3490 if (isn == NULL)
3491 return FAIL;
3492 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003493 else if (numeric_only)
3494 {
3495 ++p;
3496 break;
3497 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 else
3499 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003500 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003502 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003504 if (p[-1] == '!')
3505 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003506 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507 }
3508 if (generate_2BOOL(cctx, invert) == FAIL)
3509 return FAIL;
3510 }
3511 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003512 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 return OK;
3514}
3515
3516/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003517 * Compile "(expression)": recursive!
3518 * Return FAIL/OK.
3519 */
3520 static int
3521compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3522{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003523 int ret;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003524
3525 *arg = skipwhite(*arg + 1);
3526 if (ppconst->pp_used <= PPSIZE - 10)
3527 {
3528 ret = compile_expr1(arg, cctx, ppconst);
3529 }
3530 else
3531 {
3532 // Not enough space in ppconst, flush constants.
3533 if (generate_ppconst(cctx, ppconst) == FAIL)
3534 return FAIL;
3535 ret = compile_expr0(arg, cctx);
3536 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003537 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3538 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003539 if (**arg == ')')
3540 ++*arg;
3541 else if (ret == OK)
3542 {
3543 emsg(_(e_missing_close));
3544 ret = FAIL;
3545 }
3546 return ret;
3547}
3548
3549/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003551 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552 */
3553 static int
3554compile_subscript(
3555 char_u **arg,
3556 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003557 char_u *start_leader,
3558 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003559 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003561 char_u *name_start = *end_leader;
3562
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003563 for (;;)
3564 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003565 char_u *p = skipwhite(*arg);
3566
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003567 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003568 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003569 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003570
3571 // If a following line starts with "->{" or "->X" advance to that
3572 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003573 // Also if a following line starts with ".x".
3574 if (next != NULL &&
3575 ((next[0] == '-' && next[1] == '>'
3576 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003577 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003578 {
3579 next = next_line_from_context(cctx, TRUE);
3580 if (next == NULL)
3581 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003582 *arg = next;
3583 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003584 }
3585 }
3586
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003587 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003588 // not a function call.
3589 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003591 garray_T *stack = &cctx->ctx_type_stack;
3592 type_T *type;
3593 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003595 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003596 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003597 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003598
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003599 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003600 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3601
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003602 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003603 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3604 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003605 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 return FAIL;
3607 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003608 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003610 char_u *pstart = p;
3611
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003612 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003613 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003614 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003615
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003616 // something->method()
3617 // Apply the '!', '-' and '+' first:
3618 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003619 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003622 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003623 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003624 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003625 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003626 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003627 int argcount = 1;
3628 char_u *expr;
3629 garray_T *stack;
3630 type_T *type;
3631
3632 // Funcref call: list->(Refs[2])(arg)
3633 // or lambda: list->((arg) => expr)(arg)
3634 // Fist compile the arguments.
3635 expr = *arg;
3636 *arg = skipwhite(*arg + 1);
3637 skip_expr_cctx(arg, cctx);
3638 *arg = skipwhite(*arg);
3639 if (**arg != ')')
3640 {
3641 semsg(_(e_missing_paren), *arg);
3642 return FAIL;
3643 }
3644 ++*arg;
3645 if (**arg != '(')
3646 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003647 if (*skipwhite(*arg) == '(')
3648 emsg(_(e_nowhitespace));
3649 else
3650 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01003651 return FAIL;
3652 }
3653
3654 *arg = skipwhite(*arg + 1);
3655 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3656 return FAIL;
3657
3658 // Compile the function expression.
3659 if (compile_parenthesis(&expr, cctx, ppconst) == FAIL)
3660 return FAIL;
3661
3662 stack = &cctx->ctx_type_stack;
3663 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3664 if (generate_PCALL(cctx, argcount,
3665 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 return FAIL;
3667 }
3668 else
3669 {
3670 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003671 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003672 if (!eval_isnamec1(*p))
3673 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003674 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003675 return FAIL;
3676 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003677 if (ASCII_ISALPHA(*p) && p[1] == ':')
3678 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003679 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680 ;
3681 if (*p != '(')
3682 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003683 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684 return FAIL;
3685 }
3686 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003687 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 return FAIL;
3689 }
3690 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003691 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003693 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003694 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003695 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003696 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003697 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003698
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003699 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003700 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003701 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003702 // TODO: blob index
3703 // TODO: more arguments
3704 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003705 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003706 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003707 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003708
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003709 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003710 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003711 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003712 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003713 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003714 // missing first index is equal to zero
3715 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003716 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003717 else
3718 {
3719 if (compile_expr0(arg, cctx) == FAIL)
3720 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003721 if (**arg == ':')
3722 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003723 semsg(_(e_white_space_required_before_and_after_str_at_str),
3724 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003725 return FAIL;
3726 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003727 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003728 return FAIL;
3729 *arg = skipwhite(*arg);
3730 }
3731 if (**arg == ':')
3732 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003733 is_slice = TRUE;
3734 ++*arg;
3735 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
3736 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003737 semsg(_(e_white_space_required_before_and_after_str_at_str),
3738 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003739 return FAIL;
3740 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003741 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003742 return FAIL;
3743 if (**arg == ']')
3744 // missing second index is equal to end of string
3745 generate_PUSHNR(cctx, -1);
3746 else
3747 {
3748 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003749 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003750 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003751 return FAIL;
3752 *arg = skipwhite(*arg);
3753 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003754 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003755
3756 if (**arg != ']')
3757 {
3758 emsg(_(e_missbrac));
3759 return FAIL;
3760 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003761 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003763 // We can index a list and a dict. If we don't know the type
3764 // we can use the index value type.
3765 // TODO: If we don't know use an instruction to figure it out at
3766 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003767 typep = ((type_T **)stack->ga_data) + stack->ga_len
3768 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003769 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003770 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3771 // If the index is a string, the variable must be a Dict.
3772 if (*typep == &t_any && valtype == &t_string)
3773 vtype = VAR_DICT;
3774 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003775 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003776 if (need_type(valtype, &t_number, -1, cctx,
3777 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003778 return FAIL;
3779 if (is_slice)
3780 {
3781 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003782 if (need_type(valtype, &t_number, -2, cctx,
3783 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003784 return FAIL;
3785 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003786 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003787
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003788 if (vtype == VAR_DICT)
3789 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003790 if (is_slice)
3791 {
3792 emsg(_(e_cannot_slice_dictionary));
3793 return FAIL;
3794 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003795 if ((*typep)->tt_type == VAR_DICT)
3796 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003797 else
3798 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003799 if (need_type(*typep, &t_dict_any, -2, cctx,
3800 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003801 return FAIL;
3802 *typep = &t_any;
3803 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003804 if (may_generate_2STRING(-1, cctx) == FAIL)
3805 return FAIL;
3806 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3807 return FAIL;
3808 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003809 else if (vtype == VAR_STRING)
3810 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003811 *typep = &t_string;
3812 if ((is_slice
3813 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3814 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003815 return FAIL;
3816 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003817 else if (vtype == VAR_BLOB)
3818 {
3819 emsg("Sorry, blob index and slice not implemented yet");
3820 return FAIL;
3821 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003822 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003823 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003824 if (is_slice)
3825 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003826 if (generate_instr_drop(cctx,
3827 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3828 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003829 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003830 }
Bram Moolenaared591872020-08-15 22:14:53 +02003831 else
3832 {
3833 if ((*typep)->tt_type == VAR_LIST)
3834 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003835 if (generate_instr_drop(cctx,
3836 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3837 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003838 return FAIL;
3839 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003840 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003841 else
3842 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003843 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003844 return FAIL;
3845 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003846 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003847 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003849 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003850 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003851 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003852 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003853
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003854 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003855 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003856 {
3857 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003858 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003859 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003860 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003861 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862 while (eval_isnamec(*p))
3863 MB_PTR_ADV(p);
3864 if (p == *arg)
3865 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003866 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 return FAIL;
3868 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003869 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 return FAIL;
3871 *arg = p;
3872 }
3873 else
3874 break;
3875 }
3876
3877 // TODO - see handle_subscript():
3878 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3879 // Don't do this when "Func" is already a partial that was bound
3880 // explicitly (pt_auto is FALSE).
3881
3882 return OK;
3883}
3884
3885/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003886 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3887 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003889 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003890 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003891 *
3892 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893 */
3894
3895/*
3896 * number number constant
3897 * 0zFFFFFFFF Blob constant
3898 * "string" string constant
3899 * 'string' literal string constant
3900 * &option-name option value
3901 * @r register contents
3902 * identifier variable value
3903 * function() function call
3904 * $VAR environment variable
3905 * (expression) nested expression
3906 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01003907 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908 *
3909 * Also handle:
3910 * ! in front logical NOT
3911 * - in front unary minus
3912 * + in front unary plus (ignored)
3913 * trailing (arg) funcref/partial call
3914 * trailing [] subscript in String or List
3915 * trailing .name entry in Dictionary
3916 * trailing ->name() method call
3917 */
3918 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003919compile_expr7(
3920 char_u **arg,
3921 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003922 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 char_u *start_leader, *end_leader;
3925 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003926 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003927 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003928
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003929 ppconst->pp_is_const = FALSE;
3930
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931 /*
3932 * Skip '!', '-' and '+' characters. They are handled later.
3933 */
3934 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003935 if (eval_leader(arg, TRUE) == FAIL)
3936 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937 end_leader = *arg;
3938
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003939 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940 switch (**arg)
3941 {
3942 /*
3943 * Number constant.
3944 */
3945 case '0': // also for blob starting with 0z
3946 case '1':
3947 case '2':
3948 case '3':
3949 case '4':
3950 case '5':
3951 case '6':
3952 case '7':
3953 case '8':
3954 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003955 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003956 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003957 // Apply "-" and "+" just before the number now, right to
3958 // left. Matters especially when "->" follows. Stops at
3959 // '!'.
3960 if (apply_leader(rettv, TRUE,
3961 start_leader, &end_leader) == FAIL)
3962 {
3963 clear_tv(rettv);
3964 return FAIL;
3965 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966 break;
3967
3968 /*
3969 * String constant: "string".
3970 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003971 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972 return FAIL;
3973 break;
3974
3975 /*
3976 * Literal string constant: 'str''ing'.
3977 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003978 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979 return FAIL;
3980 break;
3981
3982 /*
3983 * Constant Vim variable.
3984 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003985 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986 ret = NOTDONE;
3987 break;
3988
3989 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003990 * "true" constant
3991 */
3992 case 't': if (STRNCMP(*arg, "true", 4) == 0
3993 && !eval_isnamec((*arg)[4]))
3994 {
3995 *arg += 4;
3996 rettv->v_type = VAR_BOOL;
3997 rettv->vval.v_number = VVAL_TRUE;
3998 }
3999 else
4000 ret = NOTDONE;
4001 break;
4002
4003 /*
4004 * "false" constant
4005 */
4006 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4007 && !eval_isnamec((*arg)[5]))
4008 {
4009 *arg += 5;
4010 rettv->v_type = VAR_BOOL;
4011 rettv->vval.v_number = VVAL_FALSE;
4012 }
4013 else
4014 ret = NOTDONE;
4015 break;
4016
4017 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004018 * "null" constant
4019 */
4020 case 'n': if (STRNCMP(*arg, "null", 4) == 0
4021 && !eval_isnamec((*arg)[5]))
4022 {
4023 *arg += 4;
4024 rettv->v_type = VAR_SPECIAL;
4025 rettv->vval.v_number = VVAL_NULL;
4026 }
4027 else
4028 ret = NOTDONE;
4029 break;
4030
4031 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032 * List: [expr, expr]
4033 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004034 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004035 break;
4036
4037 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004038 * Dictionary: {'key': val, 'key': val}
4039 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004040 case '{': ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004041 break;
4042
4043 /*
4044 * Option value: &name
4045 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004046 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4047 return FAIL;
4048 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049 break;
4050
4051 /*
4052 * Environment variable: $VAR.
4053 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004054 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4055 return FAIL;
4056 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057 break;
4058
4059 /*
4060 * Register contents: @r.
4061 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004062 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4063 return FAIL;
4064 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065 break;
4066 /*
4067 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004068 * lambda: (arg, arg) => expr
4069 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004071 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4072 ret = compile_lambda(arg, cctx);
4073 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004074 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075 break;
4076
4077 default: ret = NOTDONE;
4078 break;
4079 }
4080 if (ret == FAIL)
4081 return FAIL;
4082
Bram Moolenaar1c747212020-05-09 18:28:34 +02004083 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004085 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004086 clear_tv(rettv);
4087 else
4088 // A constant expression can possibly be handled compile time,
4089 // return the value instead of generating code.
4090 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091 }
4092 else if (ret == NOTDONE)
4093 {
4094 char_u *p;
4095 int r;
4096
4097 if (!eval_isnamec1(**arg))
4098 {
Bram Moolenaare4984292020-12-13 14:19:25 +01004099 if (ends_excmd(*skipwhite(*arg)))
4100 semsg(_(e_empty_expression_str), *arg);
4101 else
4102 semsg(_(e_name_expected_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004103 return FAIL;
4104 }
4105
4106 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004107 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004109 {
4110 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4111 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004113 {
4114 if (generate_ppconst(cctx, ppconst) == FAIL)
4115 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004116 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004117 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 if (r == FAIL)
4119 return FAIL;
4120 }
4121
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004122 // Handle following "[]", ".member", etc.
4123 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004124 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004125 ppconst) == FAIL)
4126 return FAIL;
4127 if (ppconst->pp_used > 0)
4128 {
4129 // apply the '!', '-' and '+' before the constant
4130 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004131 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004132 return FAIL;
4133 return OK;
4134 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004135 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004137 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138}
4139
4140/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004141 * Give the "white on both sides" error, taking the operator from "p[len]".
4142 */
4143 void
4144error_white_both(char_u *op, int len)
4145{
4146 char_u buf[10];
4147
4148 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004149 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004150}
4151
4152/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004153 * <type>expr7: runtime type check / conversion
4154 */
4155 static int
4156compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4157{
4158 type_T *want_type = NULL;
4159
4160 // Recognize <type>
4161 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4162 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004163 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004164 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4165 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004166 return FAIL;
4167
4168 if (**arg != '>')
4169 {
4170 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004171 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004172 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004173 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004174 return FAIL;
4175 }
4176 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004177 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004178 return FAIL;
4179 }
4180
4181 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4182 return FAIL;
4183
4184 if (want_type != NULL)
4185 {
4186 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004187 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004188
Bram Moolenaard1103582020-08-14 22:44:25 +02004189 generate_ppconst(cctx, ppconst);
4190 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02004191 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004192 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004193 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004194 return FAIL;
4195 }
4196 }
4197
4198 return OK;
4199}
4200
4201/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202 * * number multiplication
4203 * / number division
4204 * % number modulo
4205 */
4206 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004207compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004208{
4209 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004210 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004211 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004213 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004214 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215 return FAIL;
4216
4217 /*
4218 * Repeat computing, until no "*", "/" or "%" is following.
4219 */
4220 for (;;)
4221 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004222 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004223 if (*op != '*' && *op != '/' && *op != '%')
4224 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004225 if (next != NULL)
4226 {
4227 *arg = next_line_from_context(cctx, TRUE);
4228 op = skipwhite(*arg);
4229 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004230
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004231 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004233 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004234 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004235 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004236 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004237 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004238
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004239 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004240 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004242
4243 if (ppconst->pp_used == ppconst_used + 2
4244 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4245 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004246 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004247 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4248 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004249 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004251 // both are numbers: compute the result
4252 switch (*op)
4253 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004254 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004255 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004256 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004257 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004258 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004259 break;
4260 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004261 tv1->vval.v_number = res;
4262 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004263 }
4264 else
4265 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004266 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004267 generate_two_op(cctx, op);
4268 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269 }
4270
4271 return OK;
4272}
4273
4274/*
4275 * + number addition
4276 * - number subtraction
4277 * .. string concatenation
4278 */
4279 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004280compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004281{
4282 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004283 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004284 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004285 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004286
4287 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004288 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004289 return FAIL;
4290
4291 /*
4292 * Repeat computing, until no "+", "-" or ".." is following.
4293 */
4294 for (;;)
4295 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004296 op = may_peek_next_line(cctx, *arg, &next);
4297 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004298 break;
4299 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004300 if (next != NULL)
4301 {
4302 *arg = next_line_from_context(cctx, TRUE);
4303 op = skipwhite(*arg);
4304 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004306 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004308 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004309 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004310 }
4311
Bram Moolenaare0de1712020-12-02 17:36:54 +01004312 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004313 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004314
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004315 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004316 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317 return FAIL;
4318
Bram Moolenaara5565e42020-05-09 15:44:01 +02004319 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004320 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004321 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4322 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4323 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4324 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004326 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4327 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004328
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004329 // concat/subtract/add constant numbers
4330 if (*op == '+')
4331 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4332 else if (*op == '-')
4333 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4334 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004335 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004336 // concatenate constant strings
4337 char_u *s1 = tv1->vval.v_string;
4338 char_u *s2 = tv2->vval.v_string;
4339 size_t len1 = STRLEN(s1);
4340
4341 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4342 if (tv1->vval.v_string == NULL)
4343 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004344 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004345 return FAIL;
4346 }
4347 mch_memmove(tv1->vval.v_string, s1, len1);
4348 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004349 vim_free(s1);
4350 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004351 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004352 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004353 }
4354 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004355 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004356 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004357 if (*op == '.')
4358 {
4359 if (may_generate_2STRING(-2, cctx) == FAIL
4360 || may_generate_2STRING(-1, cctx) == FAIL)
4361 return FAIL;
4362 generate_instr_drop(cctx, ISN_CONCAT, 1);
4363 }
4364 else
4365 generate_two_op(cctx, op);
4366 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367 }
4368
4369 return OK;
4370}
4371
4372/*
4373 * expr5a == expr5b
4374 * expr5a =~ expr5b
4375 * expr5a != expr5b
4376 * expr5a !~ expr5b
4377 * expr5a > expr5b
4378 * expr5a >= expr5b
4379 * expr5a < expr5b
4380 * expr5a <= expr5b
4381 * expr5a is expr5b
4382 * expr5a isnot expr5b
4383 *
4384 * Produces instructions:
4385 * EVAL expr5a Push result of "expr5a"
4386 * EVAL expr5b Push result of "expr5b"
4387 * COMPARE one of the compare instructions
4388 */
4389 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004390compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004391{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004392 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004394 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004397 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398
4399 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004400 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 return FAIL;
4402
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004403 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004404 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004405
4406 /*
4407 * If there is a comparative operator, use it.
4408 */
4409 if (type != EXPR_UNKNOWN)
4410 {
4411 int ic = FALSE; // Default: do not ignore case
4412
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004413 if (next != NULL)
4414 {
4415 *arg = next_line_from_context(cctx, TRUE);
4416 p = skipwhite(*arg);
4417 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418 if (type_is && (p[len] == '?' || p[len] == '#'))
4419 {
4420 semsg(_(e_invexpr2), *arg);
4421 return FAIL;
4422 }
4423 // extra question mark appended: ignore case
4424 if (p[len] == '?')
4425 {
4426 ic = TRUE;
4427 ++len;
4428 }
4429 // extra '#' appended: match case (ignored)
4430 else if (p[len] == '#')
4431 ++len;
4432 // nothing appended: match case
4433
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004434 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004435 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004436 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004437 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004438 }
4439
4440 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004441 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004442 return FAIL;
4443
Bram Moolenaara5565e42020-05-09 15:44:01 +02004444 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 return FAIL;
4446
Bram Moolenaara5565e42020-05-09 15:44:01 +02004447 if (ppconst->pp_used == ppconst_used + 2)
4448 {
4449 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4450 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4451 int ret;
4452
4453 // Both sides are a constant, compute the result now.
4454 // First check for a valid combination of types, this is more
4455 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004456 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004457 ret = FAIL;
4458 else
4459 {
4460 ret = typval_compare(tv1, tv2, type, ic);
4461 tv1->v_type = VAR_BOOL;
4462 tv1->vval.v_number = tv1->vval.v_number
4463 ? VVAL_TRUE : VVAL_FALSE;
4464 clear_tv(tv2);
4465 --ppconst->pp_used;
4466 }
4467 return ret;
4468 }
4469
4470 generate_ppconst(cctx, ppconst);
4471 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472 }
4473
4474 return OK;
4475}
4476
Bram Moolenaar7f141552020-05-09 17:35:53 +02004477static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004479/*
4480 * Compile || or &&.
4481 */
4482 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004483compile_and_or(
4484 char_u **arg,
4485 cctx_T *cctx,
4486 char *op,
4487 ppconst_T *ppconst,
4488 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004490 char_u *next;
4491 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004492 int opchar = *op;
4493
4494 if (p[0] == opchar && p[1] == opchar)
4495 {
4496 garray_T *instr = &cctx->ctx_instr;
4497 garray_T end_ga;
4498
4499 /*
4500 * Repeat until there is no following "||" or "&&"
4501 */
4502 ga_init2(&end_ga, sizeof(int), 10);
4503 while (p[0] == opchar && p[1] == opchar)
4504 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004505 if (next != NULL)
4506 {
4507 *arg = next_line_from_context(cctx, TRUE);
4508 p = skipwhite(*arg);
4509 }
4510
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004511 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4512 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004513 semsg(_(e_white_space_required_before_and_after_str_at_str),
4514 op, *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004515 return FAIL;
4516 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004517
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004518 // TODO: use ppconst if the value is a constant and check
4519 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004520 generate_ppconst(cctx, ppconst);
4521
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004522 // Every part must evaluate to a bool.
4523 if (bool_on_stack(cctx) == FAIL)
4524 {
4525 ga_clear(&end_ga);
4526 return FAIL;
4527 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004528
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004529 if (ga_grow(&end_ga, 1) == FAIL)
4530 {
4531 ga_clear(&end_ga);
4532 return FAIL;
4533 }
4534 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4535 ++end_ga.ga_len;
4536 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004537 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004538
4539 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004540 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004541 {
4542 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004543 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004544 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004545
Bram Moolenaara5565e42020-05-09 15:44:01 +02004546 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4547 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004548 {
4549 ga_clear(&end_ga);
4550 return FAIL;
4551 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004552
4553 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004554 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004555 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004556
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004557 // Every part must evaluate to a bool.
4558 if (bool_on_stack(cctx) == FAIL)
4559 {
4560 ga_clear(&end_ga);
4561 return FAIL;
4562 }
4563
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564 // Fill in the end label in all jumps.
4565 while (end_ga.ga_len > 0)
4566 {
4567 isn_T *isn;
4568
4569 --end_ga.ga_len;
4570 isn = ((isn_T *)instr->ga_data)
4571 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4572 isn->isn_arg.jump.jump_where = instr->ga_len;
4573 }
4574 ga_clear(&end_ga);
4575 }
4576
4577 return OK;
4578}
4579
4580/*
4581 * expr4a && expr4a && expr4a logical AND
4582 *
4583 * Produces instructions:
4584 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004585 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004586 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004588 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589 * EVAL expr4c Push result of "expr4c"
4590 * end:
4591 */
4592 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004593compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004595 int ppconst_used = ppconst->pp_used;
4596
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004598 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004599 return FAIL;
4600
4601 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004602 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004603}
4604
4605/*
4606 * expr3a || expr3b || expr3c logical OR
4607 *
4608 * Produces instructions:
4609 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004610 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004611 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004612 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004613 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614 * EVAL expr3c Push result of "expr3c"
4615 * end:
4616 */
4617 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004618compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004620 int ppconst_used = ppconst->pp_used;
4621
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004623 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004624 return FAIL;
4625
4626 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004627 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628}
4629
4630/*
4631 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004632 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004633 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634 * JUMP_IF_FALSE alt jump if false
4635 * EVAL expr1a
4636 * JUMP_ALWAYS end
4637 * alt: EVAL expr1b
4638 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004639 *
4640 * Toplevel expression: expr2 ?? expr1
4641 * Produces instructions:
4642 * EVAL expr2 Push result of "expr2"
4643 * JUMP_AND_KEEP_IF_TRUE end jump if true
4644 * EVAL expr1
4645 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004646 */
4647 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004648compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004649{
4650 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004651 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004652 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653
Bram Moolenaar3988f642020-08-27 22:43:03 +02004654 // Ignore all kinds of errors when not producing code.
4655 if (cctx->ctx_skip == SKIP_YES)
4656 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004657 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004658 return OK;
4659 }
4660
Bram Moolenaar61a89812020-05-07 16:58:17 +02004661 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004662 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004663 return FAIL;
4664
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004665 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004666 if (*p == '?')
4667 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004668 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004669 garray_T *instr = &cctx->ctx_instr;
4670 garray_T *stack = &cctx->ctx_type_stack;
4671 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004672 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004673 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004674 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004675 int has_const_expr = FALSE;
4676 int const_value = FALSE;
4677 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004679 if (next != NULL)
4680 {
4681 *arg = next_line_from_context(cctx, TRUE);
4682 p = skipwhite(*arg);
4683 }
4684
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004685 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004686 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004687 semsg(_(e_white_space_required_before_and_after_str_at_str),
4688 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004689 return FAIL;
4690 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004691
Bram Moolenaara5565e42020-05-09 15:44:01 +02004692 if (ppconst->pp_used == ppconst_used + 1)
4693 {
4694 // the condition is a constant, we know whether the ? or the :
4695 // expression is to be evaluated.
4696 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004697 if (op_falsy)
4698 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4699 else
4700 {
4701 int error = FALSE;
4702
4703 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4704 &error);
4705 if (error)
4706 return FAIL;
4707 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004708 cctx->ctx_skip = save_skip == SKIP_YES ||
4709 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4710
4711 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4712 // "left ?? right" and "left" is truthy: produce "left"
4713 generate_ppconst(cctx, ppconst);
4714 else
4715 {
4716 clear_tv(&ppconst->pp_tv[ppconst_used]);
4717 --ppconst->pp_used;
4718 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004719 }
4720 else
4721 {
4722 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004723 if (op_falsy)
4724 end_idx = instr->ga_len;
4725 generate_JUMP(cctx, op_falsy
4726 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4727 if (op_falsy)
4728 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004729 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004730
4731 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01004732 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004733 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004734 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004735 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004736
Bram Moolenaara5565e42020-05-09 15:44:01 +02004737 if (!has_const_expr)
4738 {
4739 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004740
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004741 if (!op_falsy)
4742 {
4743 // remember the type and drop it
4744 --stack->ga_len;
4745 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004746
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004747 end_idx = instr->ga_len;
4748 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004749
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004750 // jump here from JUMP_IF_FALSE
4751 isn = ((isn_T *)instr->ga_data) + alt_idx;
4752 isn->isn_arg.jump.jump_where = instr->ga_len;
4753 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004754 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004755
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004756 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004757 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004758 // Check for the ":".
4759 p = may_peek_next_line(cctx, *arg, &next);
4760 if (*p != ':')
4761 {
4762 emsg(_(e_missing_colon));
4763 return FAIL;
4764 }
4765 if (next != NULL)
4766 {
4767 *arg = next_line_from_context(cctx, TRUE);
4768 p = skipwhite(*arg);
4769 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004770
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004771 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4772 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004773 semsg(_(e_white_space_required_before_and_after_str_at_str),
4774 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004775 return FAIL;
4776 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004777
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004778 // evaluate the third expression
4779 if (has_const_expr)
4780 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004781 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01004782 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004783 return FAIL;
4784 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4785 return FAIL;
4786 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004787
Bram Moolenaara5565e42020-05-09 15:44:01 +02004788 if (!has_const_expr)
4789 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004790 type_T **typep;
4791
Bram Moolenaara5565e42020-05-09 15:44:01 +02004792 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004793
Bram Moolenaara5565e42020-05-09 15:44:01 +02004794 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004795 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4796 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004797
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004798 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004799 isn = ((isn_T *)instr->ga_data) + end_idx;
4800 isn->isn_arg.jump.jump_where = instr->ga_len;
4801 }
4802
4803 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004804 }
4805 return OK;
4806}
4807
4808/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004809 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004810 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4811 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004812 */
4813 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004814compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004815{
4816 ppconst_T ppconst;
4817
4818 CLEAR_FIELD(ppconst);
4819 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4820 {
4821 clear_ppconst(&ppconst);
4822 return FAIL;
4823 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004824 if (is_const != NULL)
4825 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004826 if (generate_ppconst(cctx, &ppconst) == FAIL)
4827 return FAIL;
4828 return OK;
4829}
4830
4831/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004832 * Toplevel expression.
4833 */
4834 static int
4835compile_expr0(char_u **arg, cctx_T *cctx)
4836{
4837 return compile_expr0_ext(arg, cctx, NULL);
4838}
4839
4840/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004841 * compile "return [expr]"
4842 */
4843 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004844compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004845{
4846 char_u *p = arg;
4847 garray_T *stack = &cctx->ctx_type_stack;
4848 type_T *stack_type;
4849
4850 if (*p != NUL && *p != '|' && *p != '\n')
4851 {
4852 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004853 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004854 return NULL;
4855
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004856 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004857 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004858 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01004859 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01004860 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
4861 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004862 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004863 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004864 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004865 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004866 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004867 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4868 && stack_type->tt_type != VAR_VOID
4869 && stack_type->tt_type != VAR_UNKNOWN)
4870 {
4871 emsg(_(e_returning_value_in_function_without_return_type));
4872 return NULL;
4873 }
4874 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004875 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004876 return NULL;
4877 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02004878 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004879 }
4880 else
4881 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004882 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004883 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004884 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4885 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004886 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004887 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004888 return NULL;
4889 }
4890
4891 // No argument, return zero.
4892 generate_PUSHNR(cctx, 0);
4893 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004894 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895 return NULL;
4896
4897 // "return val | endif" is possible
4898 return skipwhite(p);
4899}
4900
4901/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004902 * Get a line from the compilation context, compatible with exarg_T getline().
4903 * Return a pointer to the line in allocated memory.
4904 * Return NULL for end-of-file or some error.
4905 */
4906 static char_u *
4907exarg_getline(
4908 int c UNUSED,
4909 void *cookie,
4910 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004911 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004912{
4913 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004914 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004915
Bram Moolenaar66250c92020-08-20 15:02:42 +02004916 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004917 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004918 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004919 return NULL;
4920 ++cctx->ctx_lnum;
4921 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4922 // Comment lines result in NULL pointers, skip them.
4923 if (p != NULL)
4924 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004925 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004926}
4927
4928/*
4929 * Compile a nested :def command.
4930 */
4931 static char_u *
4932compile_nested_function(exarg_T *eap, cctx_T *cctx)
4933{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004934 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004935 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004936 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004937 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004938 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004939 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004940
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004941 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004942 {
4943 emsg(_(e_cannot_use_bang_with_nested_def));
4944 return NULL;
4945 }
4946
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004947 if (*name_start == '/')
4948 {
4949 name_end = skip_regexp(name_start + 1, '/', TRUE);
4950 if (*name_end == '/')
4951 ++name_end;
4952 eap->nextcmd = check_nextcmd(name_end);
4953 }
4954 if (name_end == name_start || *skipwhite(name_end) != '(')
4955 {
4956 if (!ends_excmd2(name_start, name_end))
4957 {
4958 semsg(_(e_invalid_command_str), eap->cmd);
4959 return NULL;
4960 }
4961
4962 // "def" or "def Name": list functions
4963 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
4964 return NULL;
4965 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
4966 }
4967
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004968 // Only g:Func() can use a namespace.
4969 if (name_start[1] == ':' && !is_global)
4970 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004971 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004972 return NULL;
4973 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004974 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4975 return NULL;
4976
Bram Moolenaar04b12692020-05-04 23:24:44 +02004977 eap->arg = name_end;
4978 eap->getline = exarg_getline;
4979 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004980 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004981 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004982 lambda_name = vim_strsave(get_lambda_name());
4983 if (lambda_name == NULL)
4984 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004985 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004986
Bram Moolenaar822ba242020-05-24 23:00:18 +02004987 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004988 {
4989 r = eap->skip ? OK : FAIL;
4990 goto theend;
4991 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004992 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004993 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004994 {
4995 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004996 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004997 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004998
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004999 if (is_global)
5000 {
5001 char_u *func_name = vim_strnsave(name_start + 2,
5002 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005003
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005004 if (func_name == NULL)
5005 r = FAIL;
5006 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005007 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005008 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005009 lambda_name = NULL;
5010 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005011 }
5012 else
5013 {
5014 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005015 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005016 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005017 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02005018
Bram Moolenaareef21022020-08-01 22:16:43 +02005019 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005020 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005021 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005022 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005023 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005024
5025 // copy over the block scope IDs
5026 if (block_depth > 0)
5027 {
5028 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5029 if (ufunc->uf_block_ids != NULL)
5030 {
5031 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5032 sizeof(int) * block_depth);
5033 ufunc->uf_block_depth = block_depth;
5034 }
5035 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005036 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005037 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005038 r = OK;
5039
5040theend:
5041 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005042 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005043}
5044
5045/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005046 * Return the length of an assignment operator, or zero if there isn't one.
5047 */
5048 int
5049assignment_len(char_u *p, int *heredoc)
5050{
5051 if (*p == '=')
5052 {
5053 if (p[1] == '<' && p[2] == '<')
5054 {
5055 *heredoc = TRUE;
5056 return 3;
5057 }
5058 return 1;
5059 }
5060 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5061 return 2;
5062 if (STRNCMP(p, "..=", 3) == 0)
5063 return 3;
5064 return 0;
5065}
5066
5067// words that cannot be used as a variable
5068static char *reserved[] = {
5069 "true",
5070 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005071 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005072 NULL
5073};
5074
Bram Moolenaar752fc692021-01-04 21:57:11 +01005075// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005076typedef enum {
5077 dest_local,
5078 dest_option,
5079 dest_env,
5080 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005081 dest_buffer,
5082 dest_window,
5083 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005084 dest_vimvar,
5085 dest_script,
5086 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005087 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005088} assign_dest_T;
5089
Bram Moolenaar752fc692021-01-04 21:57:11 +01005090// Used by compile_lhs() to store information about the LHS of an assignment
5091// and one argument of ":unlet" with an index.
5092typedef struct {
5093 assign_dest_T lhs_dest; // type of destination
5094
5095 char_u *lhs_name; // allocated name including
5096 // "[expr]" or ".name".
5097 size_t lhs_varlen; // length of the variable without
5098 // "[expr]" or ".name"
5099 char_u *lhs_dest_end; // end of the destination, including
5100 // "[expr]" or ".name".
5101
5102 int lhs_has_index; // has "[expr]" or ".name"
5103
5104 int lhs_new_local; // create new local variable
5105 int lhs_opt_flags; // for when destination is an option
5106 int lhs_vimvaridx; // for when destination is a v:var
5107
5108 lvar_T lhs_local_lvar; // used for existing local destination
5109 lvar_T lhs_arg_lvar; // used for argument destination
5110 lvar_T *lhs_lvar; // points to destination lvar
5111 int lhs_scriptvar_sid;
5112 int lhs_scriptvar_idx;
5113
5114 int lhs_has_type; // type was specified
5115 type_T *lhs_type;
5116 type_T *lhs_member_type;
5117} lhs_T;
5118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005119/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005120 * Generate the load instruction for "name".
5121 */
5122 static void
5123generate_loadvar(
5124 cctx_T *cctx,
5125 assign_dest_T dest,
5126 char_u *name,
5127 lvar_T *lvar,
5128 type_T *type)
5129{
5130 switch (dest)
5131 {
5132 case dest_option:
5133 // TODO: check the option exists
5134 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5135 break;
5136 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005137 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5138 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5139 else
5140 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005141 break;
5142 case dest_buffer:
5143 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5144 break;
5145 case dest_window:
5146 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5147 break;
5148 case dest_tab:
5149 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5150 break;
5151 case dest_script:
5152 compile_load_scriptvar(cctx,
5153 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5154 break;
5155 case dest_env:
5156 // Include $ in the name here
5157 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5158 break;
5159 case dest_reg:
5160 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5161 break;
5162 case dest_vimvar:
5163 generate_LOADV(cctx, name + 2, TRUE);
5164 break;
5165 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005166 if (lvar->lv_from_outer > 0)
5167 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5168 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005169 else
5170 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5171 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005172 case dest_expr:
5173 // list or dict value should already be on the stack.
5174 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005175 }
5176}
5177
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005178/*
5179 * Skip over "[expr]" or ".member".
5180 * Does not check for any errors.
5181 */
5182 static char_u *
5183skip_index(char_u *start)
5184{
5185 char_u *p = start;
5186
5187 if (*p == '[')
5188 {
5189 p = skipwhite(p + 1);
5190 (void)skip_expr(&p, NULL);
5191 p = skipwhite(p);
5192 if (*p == ']')
5193 return p + 1;
5194 return p;
5195 }
5196 // if (*p == '.')
5197 return to_name_end(p + 1, TRUE);
5198}
5199
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005200 void
5201vim9_declare_error(char_u *name)
5202{
5203 char *scope = "";
5204
5205 switch (*name)
5206 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005207 case 'g': scope = _("global"); break;
5208 case 'b': scope = _("buffer"); break;
5209 case 'w': scope = _("window"); break;
5210 case 't': scope = _("tab"); break;
5211 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005212 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005213 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005214 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005215 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005216 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005217 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005218 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005219 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005220 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005221}
5222
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005223/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005224 * For one assignment figure out the type of destination. Return it in "dest".
5225 * When not recognized "dest" is not set.
5226 * For an option "opt_flags" is set.
5227 * For a v:var "vimvaridx" is set.
5228 * "type" is set to the destination type if known, unchanted otherwise.
5229 * Return FAIL if an error message was given.
5230 */
5231 static int
5232get_var_dest(
5233 char_u *name,
5234 assign_dest_T *dest,
5235 int cmdidx,
5236 int *opt_flags,
5237 int *vimvaridx,
5238 type_T **type,
5239 cctx_T *cctx)
5240{
5241 char_u *p;
5242
5243 if (*name == '&')
5244 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005245 int cc;
5246 long numval;
5247 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005248
5249 *dest = dest_option;
5250 if (cmdidx == CMD_final || cmdidx == CMD_const)
5251 {
5252 emsg(_(e_const_option));
5253 return FAIL;
5254 }
5255 p = name;
5256 p = find_option_end(&p, opt_flags);
5257 if (p == NULL)
5258 {
5259 // cannot happen?
5260 emsg(_(e_letunexp));
5261 return FAIL;
5262 }
5263 cc = *p;
5264 *p = NUL;
5265 opt_type = get_option_value(skip_option_env_lead(name),
5266 &numval, NULL, *opt_flags);
5267 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005268 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005269 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005270 case gov_unknown:
5271 semsg(_(e_unknown_option), name);
5272 return FAIL;
5273 case gov_string:
5274 case gov_hidden_string:
5275 *type = &t_string;
5276 break;
5277 case gov_bool:
5278 case gov_hidden_bool:
5279 *type = &t_bool;
5280 break;
5281 case gov_number:
5282 case gov_hidden_number:
5283 *type = &t_number;
5284 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005285 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005286 }
5287 else if (*name == '$')
5288 {
5289 *dest = dest_env;
5290 *type = &t_string;
5291 }
5292 else if (*name == '@')
5293 {
5294 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5295 {
5296 emsg_invreg(name[1]);
5297 return FAIL;
5298 }
5299 *dest = dest_reg;
5300 *type = &t_string;
5301 }
5302 else if (STRNCMP(name, "g:", 2) == 0)
5303 {
5304 *dest = dest_global;
5305 }
5306 else if (STRNCMP(name, "b:", 2) == 0)
5307 {
5308 *dest = dest_buffer;
5309 }
5310 else if (STRNCMP(name, "w:", 2) == 0)
5311 {
5312 *dest = dest_window;
5313 }
5314 else if (STRNCMP(name, "t:", 2) == 0)
5315 {
5316 *dest = dest_tab;
5317 }
5318 else if (STRNCMP(name, "v:", 2) == 0)
5319 {
5320 typval_T *vtv;
5321 int di_flags;
5322
5323 *vimvaridx = find_vim_var(name + 2, &di_flags);
5324 if (*vimvaridx < 0)
5325 {
5326 semsg(_(e_variable_not_found_str), name);
5327 return FAIL;
5328 }
5329 // We use the current value of "sandbox" here, is that OK?
5330 if (var_check_ro(di_flags, name, FALSE))
5331 return FAIL;
5332 *dest = dest_vimvar;
5333 vtv = get_vim_var_tv(*vimvaridx);
5334 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5335 }
5336 return OK;
5337}
5338
5339/*
5340 * Generate a STORE instruction for "dest", not being "dest_local".
5341 * Return FAIL when out of memory.
5342 */
5343 static int
5344generate_store_var(
5345 cctx_T *cctx,
5346 assign_dest_T dest,
5347 int opt_flags,
5348 int vimvaridx,
5349 int scriptvar_idx,
5350 int scriptvar_sid,
5351 type_T *type,
5352 char_u *name)
5353{
5354 switch (dest)
5355 {
5356 case dest_option:
5357 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5358 opt_flags);
5359 case dest_global:
5360 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005361 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5362 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005363 case dest_buffer:
5364 // include b: with the name, easier to execute that way
5365 return generate_STORE(cctx, ISN_STOREB, 0, name);
5366 case dest_window:
5367 // include w: with the name, easier to execute that way
5368 return generate_STORE(cctx, ISN_STOREW, 0, name);
5369 case dest_tab:
5370 // include t: with the name, easier to execute that way
5371 return generate_STORE(cctx, ISN_STORET, 0, name);
5372 case dest_env:
5373 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5374 case dest_reg:
5375 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5376 case dest_vimvar:
5377 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5378 case dest_script:
5379 if (scriptvar_idx < 0)
5380 {
5381 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005382 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005383
Bram Moolenaar41d61962020-12-06 20:12:43 +01005384 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005385 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5386 scriptvar_sid, type);
5387 if (name_s != name)
5388 vim_free(name_s);
5389 return r;
5390 }
5391 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5392 scriptvar_sid, scriptvar_idx, type);
5393 case dest_local:
5394 case dest_expr:
5395 // cannot happen
5396 break;
5397 }
5398 return FAIL;
5399}
5400
Bram Moolenaar752fc692021-01-04 21:57:11 +01005401 static int
5402is_decl_command(int cmdidx)
5403{
5404 return cmdidx == CMD_let || cmdidx == CMD_var
5405 || cmdidx == CMD_final || cmdidx == CMD_const;
5406}
5407
5408/*
5409 * Figure out the LHS type and other properties for an assignment or one item
5410 * of ":unlet" with an index.
5411 * Returns OK or FAIL.
5412 */
5413 static int
5414compile_lhs(
5415 char_u *var_start,
5416 lhs_T *lhs,
5417 int cmdidx,
5418 int heredoc,
5419 int oplen,
5420 cctx_T *cctx)
5421{
5422 char_u *var_end;
5423 int is_decl = is_decl_command(cmdidx);
5424
5425 CLEAR_POINTER(lhs);
5426 lhs->lhs_dest = dest_local;
5427 lhs->lhs_vimvaridx = -1;
5428 lhs->lhs_scriptvar_idx = -1;
5429
5430 // "dest_end" is the end of the destination, including "[expr]" or
5431 // ".name".
5432 // "var_end" is the end of the variable/option/etc. name.
5433 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5434 if (*var_start == '@')
5435 var_end = var_start + 2;
5436 else
5437 {
5438 // skip over the leading "&", "&l:", "&g:" and "$"
5439 var_end = skip_option_env_lead(var_start);
5440 var_end = to_name_end(var_end, TRUE);
5441 }
5442
5443 // "a: type" is declaring variable "a" with a type, not dict "a:".
5444 if (is_decl && lhs->lhs_dest_end == var_start + 2
5445 && lhs->lhs_dest_end[-1] == ':')
5446 --lhs->lhs_dest_end;
5447 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5448 --var_end;
5449
5450 // compute the length of the destination without "[expr]" or ".name"
5451 lhs->lhs_varlen = var_end - var_start;
5452 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5453 if (lhs->lhs_name == NULL)
5454 return FAIL;
5455 if (heredoc)
5456 lhs->lhs_type = &t_list_string;
5457 else
5458 lhs->lhs_type = &t_any;
5459
5460 if (cctx->ctx_skip != SKIP_YES)
5461 {
5462 int declare_error = FALSE;
5463
5464 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5465 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5466 &lhs->lhs_type, cctx) == FAIL)
5467 return FAIL;
5468 if (lhs->lhs_dest != dest_local)
5469 {
5470 // Specific kind of variable recognized.
5471 declare_error = is_decl;
5472 }
5473 else
5474 {
5475 int idx;
5476
5477 // No specific kind of variable recognized, just a name.
5478 for (idx = 0; reserved[idx] != NULL; ++idx)
5479 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5480 {
5481 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5482 return FAIL;
5483 }
5484
5485
5486 if (lookup_local(var_start, lhs->lhs_varlen,
5487 &lhs->lhs_local_lvar, cctx) == OK)
5488 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5489 else
5490 {
5491 CLEAR_FIELD(lhs->lhs_arg_lvar);
5492 if (arg_exists(var_start, lhs->lhs_varlen,
5493 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5494 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5495 {
5496 if (is_decl)
5497 {
5498 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5499 return FAIL;
5500 }
5501 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5502 }
5503 }
5504 if (lhs->lhs_lvar != NULL)
5505 {
5506 if (is_decl)
5507 {
5508 semsg(_(e_variable_already_declared), lhs->lhs_name);
5509 return FAIL;
5510 }
5511 }
5512 else
5513 {
5514 int script_namespace = lhs->lhs_varlen > 1
5515 && STRNCMP(var_start, "s:", 2) == 0;
5516 int script_var = (script_namespace
5517 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
5518 FALSE, cctx)
5519 : script_var_exists(var_start, lhs->lhs_varlen,
5520 TRUE, cctx)) == OK;
5521 imported_T *import =
5522 find_imported(var_start, lhs->lhs_varlen, cctx);
5523
5524 if (script_namespace || script_var || import != NULL)
5525 {
5526 char_u *rawname = lhs->lhs_name
5527 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5528
5529 if (is_decl)
5530 {
5531 if (script_namespace)
5532 semsg(_(e_cannot_declare_script_variable_in_function),
5533 lhs->lhs_name);
5534 else
5535 semsg(_(e_variable_already_declared_in_script),
5536 lhs->lhs_name);
5537 return FAIL;
5538 }
5539 else if (cctx->ctx_ufunc->uf_script_ctx_version
5540 == SCRIPT_VERSION_VIM9
5541 && script_namespace
5542 && !script_var && import == NULL)
5543 {
5544 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5545 return FAIL;
5546 }
5547
5548 lhs->lhs_dest = dest_script;
5549
5550 // existing script-local variables should have a type
5551 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5552 if (import != NULL)
5553 lhs->lhs_scriptvar_sid = import->imp_sid;
5554 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5555 {
5556 lhs->lhs_scriptvar_idx = get_script_item_idx(
5557 lhs->lhs_scriptvar_sid,
5558 rawname, TRUE, cctx);
5559 if (lhs->lhs_scriptvar_idx >= 0)
5560 {
5561 scriptitem_T *si = SCRIPT_ITEM(
5562 lhs->lhs_scriptvar_sid);
5563 svar_T *sv =
5564 ((svar_T *)si->sn_var_vals.ga_data)
5565 + lhs->lhs_scriptvar_idx;
5566 lhs->lhs_type = sv->sv_type;
5567 }
5568 }
5569 }
5570 else if (check_defined(var_start, lhs->lhs_varlen, cctx)
5571 == FAIL)
5572 return FAIL;
5573 }
5574 }
5575
5576 if (declare_error)
5577 {
5578 vim9_declare_error(lhs->lhs_name);
5579 return FAIL;
5580 }
5581 }
5582
5583 // handle "a:name" as a name, not index "name" on "a"
5584 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5585 var_end = lhs->lhs_dest_end;
5586
5587 if (lhs->lhs_dest != dest_option)
5588 {
5589 if (is_decl && *var_end == ':')
5590 {
5591 char_u *p;
5592
5593 // parse optional type: "let var: type = expr"
5594 if (!VIM_ISWHITE(var_end[1]))
5595 {
5596 semsg(_(e_white_space_required_after_str), ":");
5597 return FAIL;
5598 }
5599 p = skipwhite(var_end + 1);
5600 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5601 if (lhs->lhs_type == NULL)
5602 return FAIL;
5603 lhs->lhs_has_type = TRUE;
5604 }
5605 else if (lhs->lhs_lvar != NULL)
5606 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5607 }
5608
5609 if (oplen == 3 && !heredoc && lhs->lhs_dest != dest_global
5610 && lhs->lhs_type->tt_type != VAR_STRING
5611 && lhs->lhs_type->tt_type != VAR_ANY)
5612 {
5613 emsg(_(e_can_only_concatenate_to_string));
5614 return FAIL;
5615 }
5616
5617 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5618 && cctx->ctx_skip != SKIP_YES)
5619 {
5620 if (oplen > 1 && !heredoc)
5621 {
5622 // +=, /=, etc. require an existing variable
5623 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5624 return FAIL;
5625 }
5626 if (!is_decl)
5627 {
5628 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5629 return FAIL;
5630 }
5631
5632 // new local variable
5633 if ((lhs->lhs_type->tt_type == VAR_FUNC
5634 || lhs->lhs_type->tt_type == VAR_PARTIAL)
5635 && var_wrong_func_name(lhs->lhs_name, TRUE))
5636 return FAIL;
5637 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5638 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5639 if (lhs->lhs_lvar == NULL)
5640 return FAIL;
5641 lhs->lhs_new_local = TRUE;
5642 }
5643
5644 lhs->lhs_member_type = lhs->lhs_type;
5645 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5646 {
5647 // Something follows after the variable: "var[idx]" or "var.key".
5648 // TODO: should we also handle "->func()" here?
5649 if (is_decl)
5650 {
5651 emsg(_(e_cannot_use_index_when_declaring_variable));
5652 return FAIL;
5653 }
5654
5655 if (var_start[lhs->lhs_varlen] == '['
5656 || var_start[lhs->lhs_varlen] == '.')
5657 {
5658 char_u *after = var_start + lhs->lhs_varlen;
5659 char_u *p;
5660
5661 // Only the last index is used below, if there are others
5662 // before it generate code for the expression. Thus for
5663 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5664 for (;;)
5665 {
5666 p = skip_index(after);
5667 if (*p != '[' && *p != '.')
5668 break;
5669 after = p;
5670 }
5671 if (after > var_start + lhs->lhs_varlen)
5672 {
5673 lhs->lhs_varlen = after - var_start;
5674 lhs->lhs_dest = dest_expr;
5675 // We don't know the type before evaluating the expression,
5676 // use "any" until then.
5677 lhs->lhs_type = &t_any;
5678 }
5679
5680 lhs->lhs_has_index = TRUE;
5681 if (lhs->lhs_type->tt_member == NULL)
5682 lhs->lhs_member_type = &t_any;
5683 else
5684 lhs->lhs_member_type = lhs->lhs_type->tt_member;
5685 }
5686 else
5687 {
5688 semsg("Not supported yet: %s", var_start);
5689 return FAIL;
5690 }
5691 }
5692 return OK;
5693}
5694
5695/*
5696 * Assignment to a list or dict member, or ":unlet" for the item, using the
5697 * information in "lhs".
5698 * Returns OK or FAIL.
5699 */
5700 static int
5701compile_assign_unlet(
5702 char_u *var_start,
5703 lhs_T *lhs,
5704 int is_assign,
5705 type_T *rhs_type,
5706 cctx_T *cctx)
5707{
5708 char_u *p;
5709 int r;
5710 vartype_T dest_type;
5711 size_t varlen = lhs->lhs_varlen;
5712 garray_T *stack = &cctx->ctx_type_stack;
5713
5714 // Compile the "idx" in "var[idx]" or "key" in "var.key".
5715 p = var_start + varlen;
5716 if (*p == '[')
5717 {
5718 p = skipwhite(p + 1);
5719 r = compile_expr0(&p, cctx);
5720 if (r == OK && *skipwhite(p) != ']')
5721 {
5722 // this should not happen
5723 emsg(_(e_missbrac));
5724 r = FAIL;
5725 }
5726 }
5727 else // if (*p == '.')
5728 {
5729 char_u *key_end = to_name_end(p + 1, TRUE);
5730 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5731
5732 r = generate_PUSHS(cctx, key);
5733 }
5734 if (r == FAIL)
5735 return FAIL;
5736
5737 if (lhs->lhs_type == &t_any)
5738 {
5739 // Index on variable of unknown type: check at runtime.
5740 dest_type = VAR_ANY;
5741 }
5742 else
5743 {
5744 dest_type = lhs->lhs_type->tt_type;
5745 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
5746 return FAIL;
5747 if (dest_type == VAR_LIST
5748 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
5749 != VAR_NUMBER)
5750 {
5751 emsg(_(e_number_exp));
5752 return FAIL;
5753 }
5754 }
5755
5756 // Load the dict or list. On the stack we then have:
5757 // - value (for assignment, not for :unlet)
5758 // - index
5759 // - variable
5760 if (lhs->lhs_dest == dest_expr)
5761 {
5762 int c = var_start[varlen];
5763
5764 // Evaluate "ll[expr]" of "ll[expr][idx]"
5765 p = var_start;
5766 var_start[varlen] = NUL;
5767 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
5768 {
5769 // this should not happen
5770 emsg(_(e_missbrac));
5771 return FAIL;
5772 }
5773 var_start[varlen] = c;
5774
5775 lhs->lhs_type = stack->ga_len == 0 ? &t_void
5776 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5777 // now we can properly check the type
5778 if (lhs->lhs_type->tt_member != NULL && rhs_type != &t_void
5779 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, cctx,
5780 FALSE, FALSE) == FAIL)
5781 return FAIL;
5782 }
5783 else
5784 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
5785 lhs->lhs_lvar, lhs->lhs_type);
5786
5787 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
5788 {
5789 if (is_assign)
5790 {
5791 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
5792
5793 if (isn == NULL)
5794 return FAIL;
5795 isn->isn_arg.vartype = dest_type;
5796 }
5797 else
5798 {
5799 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
5800 return FAIL;
5801 }
5802 }
5803 else
5804 {
5805 emsg(_(e_indexable_type_required));
5806 return FAIL;
5807 }
5808
5809 return OK;
5810}
5811
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005812/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005813 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005814 * "let name"
5815 * "var name = expr"
5816 * "final name = expr"
5817 * "const name = expr"
5818 * "name = expr"
5819 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005820 * Return NULL for an error.
5821 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005822 */
5823 static char_u *
5824compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5825{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005826 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005827 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005828 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005829 char_u *ret = NULL;
5830 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005831 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005832 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005833 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005834 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005835 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005836 int oplen = 0;
5837 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005838 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005839 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005840 int is_decl = is_decl_command(cmdidx);
5841 lhs_T lhs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005842
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005843 // Skip over the "var" or "[var, var]" to get to any "=".
5844 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5845 if (p == NULL)
5846 return *arg == '[' ? arg : NULL;
5847
5848 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005849 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005850 // TODO: should we allow this, and figure out type inference from list
5851 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005852 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005853 return NULL;
5854 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01005855 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005856
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005857 sp = p;
5858 p = skipwhite(p);
5859 op = p;
5860 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005861
5862 if (var_count > 0 && oplen == 0)
5863 // can be something like "[1, 2]->func()"
5864 return arg;
5865
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005866 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005867 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005868 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005869 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005870 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005871
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005872 if (heredoc)
5873 {
5874 list_T *l;
5875 listitem_T *li;
5876
5877 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005878 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005879 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005880 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005881 if (l == NULL)
5882 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005883
Bram Moolenaar078269b2020-09-21 20:35:55 +02005884 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005885 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005886 // Push each line and the create the list.
5887 FOR_ALL_LIST_ITEMS(l, li)
5888 {
5889 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5890 li->li_tv.vval.v_string = NULL;
5891 }
5892 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005893 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005894 list_free(l);
5895 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005896 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005897 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005898 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005899 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005900 char_u *wp;
5901
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005902 // for "[var, var] = expr" evaluate the expression here, loop over the
5903 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005904 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005905
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005906 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01005907 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005908 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005909 if (compile_expr0(&p, cctx) == FAIL)
5910 return NULL;
5911 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005912
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005913 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005914 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005915 type_T *stacktype;
5916
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005917 stacktype = stack->ga_len == 0 ? &t_void
5918 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005919 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005920 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005921 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005922 goto theend;
5923 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005924 if (need_type(stacktype, &t_list_any, -1, cctx,
5925 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005926 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005927 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005928 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5929 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005930 if (stacktype->tt_member != NULL)
5931 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005932 }
5933 }
5934
5935 /*
5936 * Loop over variables in "[var, var] = expr".
5937 * For "var = expr" and "let var: type" this is done only once.
5938 */
5939 if (var_count > 0)
5940 var_start = skipwhite(arg + 1); // skip over the "["
5941 else
5942 var_start = arg;
5943 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5944 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005945 int instr_count = -1;
5946
Bram Moolenaar752fc692021-01-04 21:57:11 +01005947 vim_free(lhs.lhs_name);
5948
5949 /*
5950 * Figure out the LHS type and other properties.
5951 */
5952 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
5953 goto theend;
5954
5955 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02005956 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01005957 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005958 goto theend;
5959 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01005960 if (!is_decl && lhs.lhs_lvar != NULL
5961 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005962 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01005963 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005964 goto theend;
5965 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005966
5967 if (!heredoc)
5968 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005969 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005970 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005971 if (oplen > 0 && var_count == 0)
5972 {
5973 // skip over the "=" and the expression
5974 p = skipwhite(op + oplen);
5975 compile_expr0(&p, cctx);
5976 }
5977 }
5978 else if (oplen > 0)
5979 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005980 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005981 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005982
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005983 // For "var = expr" evaluate the expression.
5984 if (var_count == 0)
5985 {
5986 int r;
5987
5988 // for "+=", "*=", "..=" etc. first load the current value
5989 if (*op != '=')
5990 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01005991 generate_loadvar(cctx, lhs.lhs_dest, lhs.lhs_name,
5992 lhs.lhs_lvar, lhs.lhs_type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005993
Bram Moolenaar752fc692021-01-04 21:57:11 +01005994 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005995 {
5996 // TODO: get member from list or dict
5997 emsg("Index with operation not supported yet");
5998 goto theend;
5999 }
6000 }
6001
6002 // Compile the expression. Temporarily hide the new local
6003 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006004 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006005 --cctx->ctx_locals.ga_len;
6006 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006007 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006008 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006009 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006010 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006011 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006012 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006013 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006014 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006015 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006016 ++cctx->ctx_locals.ga_len;
6017 if (r == FAIL)
6018 goto theend;
6019 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006020 else if (semicolon && var_idx == var_count - 1)
6021 {
6022 // For "[var; var] = expr" get the rest of the list
6023 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6024 goto theend;
6025 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006026 else
6027 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006028 // For "[var, var] = expr" get the "var_idx" item from the
6029 // list.
6030 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006031 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006032 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006033
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006034 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006035 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006036 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006037 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006038 if ((rhs_type->tt_type == VAR_FUNC
6039 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006040 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006041 goto theend;
6042
Bram Moolenaar752fc692021-01-04 21:57:11 +01006043 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006044 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006045 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006046 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006047 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006048 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006049 }
6050 else
6051 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006052 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006053 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006054 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006055 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006056 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006057 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006058 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006059 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006060 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006061 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006062 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006063 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006064 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006065 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006066 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006067
Bram Moolenaar71abe482020-09-14 22:28:30 +02006068 // without operator check type here, otherwise below
Bram Moolenaar752fc692021-01-04 21:57:11 +01006069 if (lhs.lhs_has_index)
6070 use_type = lhs.lhs_member_type;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006071 if (need_type(rhs_type, use_type, -1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006072 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006073 goto theend;
6074 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006075 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006076 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
6077 -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006078 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006079 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006080 else if (cmdidx == CMD_final)
6081 {
6082 emsg(_(e_final_requires_a_value));
6083 goto theend;
6084 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006085 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006086 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006087 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006088 goto theend;
6089 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006090 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006091 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006092 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006093 goto theend;
6094 }
6095 else
6096 {
6097 // variables are always initialized
6098 if (ga_grow(instr, 1) == FAIL)
6099 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006100 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006101 {
6102 case VAR_BOOL:
6103 generate_PUSHBOOL(cctx, VVAL_FALSE);
6104 break;
6105 case VAR_FLOAT:
6106#ifdef FEAT_FLOAT
6107 generate_PUSHF(cctx, 0.0);
6108#endif
6109 break;
6110 case VAR_STRING:
6111 generate_PUSHS(cctx, NULL);
6112 break;
6113 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006114 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006115 break;
6116 case VAR_FUNC:
6117 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6118 break;
6119 case VAR_LIST:
6120 generate_NEWLIST(cctx, 0);
6121 break;
6122 case VAR_DICT:
6123 generate_NEWDICT(cctx, 0);
6124 break;
6125 case VAR_JOB:
6126 generate_PUSHJOB(cctx, NULL);
6127 break;
6128 case VAR_CHANNEL:
6129 generate_PUSHCHANNEL(cctx, NULL);
6130 break;
6131 case VAR_NUMBER:
6132 case VAR_UNKNOWN:
6133 case VAR_ANY:
6134 case VAR_PARTIAL:
6135 case VAR_VOID:
6136 case VAR_SPECIAL: // cannot happen
6137 generate_PUSHNR(cctx, 0);
6138 break;
6139 }
6140 }
6141 if (var_count == 0)
6142 end = p;
6143 }
6144
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006145 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006146 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006147 break;
6148
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006149 if (oplen > 0 && *op != '=')
6150 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006151 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006152 type_T *stacktype;
6153
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006154 if (*op == '.')
6155 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006156 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006157 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006158 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006159 if (
6160#ifdef FEAT_FLOAT
6161 // If variable is float operation with number is OK.
6162 !(expected == &t_float && stacktype == &t_number) &&
6163#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006164 need_type(stacktype, expected, -1, cctx,
6165 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006166 goto theend;
6167
6168 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006169 {
6170 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6171 goto theend;
6172 }
6173 else if (*op == '+')
6174 {
6175 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006176 operator_type(lhs.lhs_member_type, stacktype),
6177 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006178 goto theend;
6179 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006180 else if (generate_two_op(cctx, op) == FAIL)
6181 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006182 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006183
Bram Moolenaar752fc692021-01-04 21:57:11 +01006184 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006185 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006186 // Use the info in "lhs" to store the value at the index in the
6187 // list or dict.
6188 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6189 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006190 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006191 }
6192 else
6193 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006194 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
6195 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006196 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006197 generate_LOCKCONST(cctx);
6198
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006199 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006200 && (lhs.lhs_type->tt_type == VAR_DICT
6201 || lhs.lhs_type->tt_type == VAR_LIST)
6202 && lhs.lhs_type->tt_member != NULL
6203 && lhs.lhs_type->tt_member != &t_any
6204 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006205 // Set the type in the list or dict, so that it can be checked,
6206 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006207 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006208
Bram Moolenaar752fc692021-01-04 21:57:11 +01006209 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006210 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006211 if (generate_store_var(cctx, lhs.lhs_dest,
6212 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6213 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6214 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006215 goto theend;
6216 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006217 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006218 {
6219 isn_T *isn = ((isn_T *)instr->ga_data)
6220 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006221
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006222 // optimization: turn "var = 123" from ISN_PUSHNR +
6223 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006224 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006225 && instr->ga_len == instr_count + 1
6226 && isn->isn_type == ISN_PUSHNR)
6227 {
6228 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006229
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006230 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006231 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006232 isn->isn_arg.storenr.stnr_val = val;
6233 if (stack->ga_len > 0)
6234 --stack->ga_len;
6235 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006236 else if (lhs.lhs_lvar->lv_from_outer > 0)
6237 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6238 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006239 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006240 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006241 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006242 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006243
6244 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006245 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006246 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006247
6248 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006249 if (var_count > 0 && !semicolon)
6250 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006251 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006252 goto theend;
6253 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006254
Bram Moolenaarb2097502020-07-19 17:17:02 +02006255 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006256
6257theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006258 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006259 return ret;
6260}
6261
6262/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006263 * Check for an assignment at "eap->cmd", compile it if found.
6264 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6265 */
6266 static int
6267may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6268{
6269 char_u *pskip;
6270 char_u *p;
6271
6272 // Assuming the command starts with a variable or function name,
6273 // find what follows.
6274 // Skip over "var.member", "var[idx]" and the like.
6275 // Also "&opt = val", "$ENV = val" and "@r = val".
6276 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6277 ? eap->cmd + 1 : eap->cmd;
6278 p = to_name_end(pskip, TRUE);
6279 if (p > eap->cmd && *p != NUL)
6280 {
6281 char_u *var_end;
6282 int oplen;
6283 int heredoc;
6284
6285 if (eap->cmd[0] == '@')
6286 var_end = eap->cmd + 2;
6287 else
6288 var_end = find_name_end(pskip, NULL, NULL,
6289 FNE_CHECK_START | FNE_INCL_BR);
6290 oplen = assignment_len(skipwhite(var_end), &heredoc);
6291 if (oplen > 0)
6292 {
6293 size_t len = p - eap->cmd;
6294
6295 // Recognize an assignment if we recognize the variable
6296 // name:
6297 // "g:var = expr"
6298 // "local = expr" where "local" is a local var.
6299 // "script = expr" where "script" is a script-local var.
6300 // "import = expr" where "import" is an imported var
6301 // "&opt = expr"
6302 // "$ENV = expr"
6303 // "@r = expr"
6304 if (*eap->cmd == '&'
6305 || *eap->cmd == '$'
6306 || *eap->cmd == '@'
6307 || ((len) > 2 && eap->cmd[1] == ':')
6308 || lookup_local(eap->cmd, len, NULL, cctx) == OK
6309 || arg_exists(eap->cmd, len, NULL, NULL, NULL, cctx) == OK
6310 || script_var_exists(eap->cmd, len, FALSE, cctx) == OK
6311 || find_imported(eap->cmd, len, cctx) != NULL)
6312 {
6313 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6314 if (*line == NULL || *line == eap->cmd)
6315 return FAIL;
6316 return OK;
6317 }
6318 }
6319 }
6320
6321 if (*eap->cmd == '[')
6322 {
6323 // [var, var] = expr
6324 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6325 if (*line == NULL)
6326 return FAIL;
6327 if (*line != eap->cmd)
6328 return OK;
6329 }
6330 return NOTDONE;
6331}
6332
6333/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006334 * Check if "name" can be "unlet".
6335 */
6336 int
6337check_vim9_unlet(char_u *name)
6338{
6339 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6340 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006341 // "unlet s:var" is allowed in legacy script.
6342 if (*name == 's' && !script_is_vim9())
6343 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006344 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006345 return FAIL;
6346 }
6347 return OK;
6348}
6349
6350/*
6351 * Callback passed to ex_unletlock().
6352 */
6353 static int
6354compile_unlet(
6355 lval_T *lvp,
6356 char_u *name_end,
6357 exarg_T *eap,
6358 int deep UNUSED,
6359 void *coookie)
6360{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006361 cctx_T *cctx = coookie;
6362 char_u *p = lvp->ll_name;
6363 int cc = *name_end;
6364 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006365
Bram Moolenaar752fc692021-01-04 21:57:11 +01006366 if (cctx->ctx_skip == SKIP_YES)
6367 return OK;
6368
6369 *name_end = NUL;
6370 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006371 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006372 // :unlet $ENV_VAR
6373 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6374 }
6375 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6376 {
6377 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006378
Bram Moolenaar752fc692021-01-04 21:57:11 +01006379 // This is similar to assigning: lookup the list/dict, compile the
6380 // idx/key. Then instead of storing the value unlet the item.
6381 // unlet {list}[idx]
6382 // unlet {dict}[key] dict.key
6383 //
6384 // Figure out the LHS type and other properties.
6385 //
6386 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6387
6388 // : unlet an indexed item
6389 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006390 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006391 iemsg("called compile_lhs() without an index");
6392 ret = FAIL;
6393 }
6394 else
6395 {
6396 // Use the info in "lhs" to unlet the item at the index in the
6397 // list or dict.
6398 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006399 }
6400
Bram Moolenaar752fc692021-01-04 21:57:11 +01006401 vim_free(lhs.lhs_name);
6402 }
6403 else if (check_vim9_unlet(p) == FAIL)
6404 {
6405 ret = FAIL;
6406 }
6407 else
6408 {
6409 // Normal name. Only supports g:, w:, t: and b: namespaces.
6410 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006411 }
6412
Bram Moolenaar752fc692021-01-04 21:57:11 +01006413 *name_end = cc;
6414 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006415}
6416
6417/*
6418 * compile "unlet var", "lock var" and "unlock var"
6419 * "arg" points to "var".
6420 */
6421 static char_u *
6422compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6423{
6424 char_u *p = arg;
6425
6426 if (eap->cmdidx != CMD_unlet)
6427 {
6428 emsg("Sorry, :lock and unlock not implemented yet");
6429 return NULL;
6430 }
6431
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006432 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6433 compile_unlet, cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006434 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6435}
6436
6437/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006438 * Compile an :import command.
6439 */
6440 static char_u *
6441compile_import(char_u *arg, cctx_T *cctx)
6442{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006443 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006444}
6445
6446/*
6447 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6448 */
6449 static int
6450compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6451{
6452 garray_T *instr = &cctx->ctx_instr;
6453 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6454
6455 if (endlabel == NULL)
6456 return FAIL;
6457 endlabel->el_next = *el;
6458 *el = endlabel;
6459 endlabel->el_end_label = instr->ga_len;
6460
6461 generate_JUMP(cctx, when, 0);
6462 return OK;
6463}
6464
6465 static void
6466compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
6467{
6468 garray_T *instr = &cctx->ctx_instr;
6469
6470 while (*el != NULL)
6471 {
6472 endlabel_T *cur = (*el);
6473 isn_T *isn;
6474
6475 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
6476 isn->isn_arg.jump.jump_where = instr->ga_len;
6477 *el = cur->el_next;
6478 vim_free(cur);
6479 }
6480}
6481
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006482 static void
6483compile_free_jump_to_end(endlabel_T **el)
6484{
6485 while (*el != NULL)
6486 {
6487 endlabel_T *cur = (*el);
6488
6489 *el = cur->el_next;
6490 vim_free(cur);
6491 }
6492}
6493
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006494/*
6495 * Create a new scope and set up the generic items.
6496 */
6497 static scope_T *
6498new_scope(cctx_T *cctx, scopetype_T type)
6499{
6500 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6501
6502 if (scope == NULL)
6503 return NULL;
6504 scope->se_outer = cctx->ctx_scope;
6505 cctx->ctx_scope = scope;
6506 scope->se_type = type;
6507 scope->se_local_count = cctx->ctx_locals.ga_len;
6508 return scope;
6509}
6510
6511/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006512 * Free the current scope and go back to the outer scope.
6513 */
6514 static void
6515drop_scope(cctx_T *cctx)
6516{
6517 scope_T *scope = cctx->ctx_scope;
6518
6519 if (scope == NULL)
6520 {
6521 iemsg("calling drop_scope() without a scope");
6522 return;
6523 }
6524 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006525 switch (scope->se_type)
6526 {
6527 case IF_SCOPE:
6528 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6529 case FOR_SCOPE:
6530 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6531 case WHILE_SCOPE:
6532 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6533 case TRY_SCOPE:
6534 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6535 case NO_SCOPE:
6536 case BLOCK_SCOPE:
6537 break;
6538 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006539 vim_free(scope);
6540}
6541
6542/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006543 * compile "if expr"
6544 *
6545 * "if expr" Produces instructions:
6546 * EVAL expr Push result of "expr"
6547 * JUMP_IF_FALSE end
6548 * ... body ...
6549 * end:
6550 *
6551 * "if expr | else" Produces instructions:
6552 * EVAL expr Push result of "expr"
6553 * JUMP_IF_FALSE else
6554 * ... body ...
6555 * JUMP_ALWAYS end
6556 * else:
6557 * ... body ...
6558 * end:
6559 *
6560 * "if expr1 | elseif expr2 | else" Produces instructions:
6561 * EVAL expr Push result of "expr"
6562 * JUMP_IF_FALSE elseif
6563 * ... body ...
6564 * JUMP_ALWAYS end
6565 * elseif:
6566 * EVAL expr Push result of "expr"
6567 * JUMP_IF_FALSE else
6568 * ... body ...
6569 * JUMP_ALWAYS end
6570 * else:
6571 * ... body ...
6572 * end:
6573 */
6574 static char_u *
6575compile_if(char_u *arg, cctx_T *cctx)
6576{
6577 char_u *p = arg;
6578 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006579 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006580 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006581 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006582 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006583
Bram Moolenaara5565e42020-05-09 15:44:01 +02006584 CLEAR_FIELD(ppconst);
6585 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006586 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006587 clear_ppconst(&ppconst);
6588 return NULL;
6589 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006590 if (cctx->ctx_skip == SKIP_YES)
6591 clear_ppconst(&ppconst);
6592 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006593 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006594 int error = FALSE;
6595 int v;
6596
Bram Moolenaara5565e42020-05-09 15:44:01 +02006597 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006598 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006599 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006600 if (error)
6601 return NULL;
6602 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006603 }
6604 else
6605 {
6606 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006607 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006608 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006609 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006610 if (bool_on_stack(cctx) == FAIL)
6611 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006612 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006613
6614 scope = new_scope(cctx, IF_SCOPE);
6615 if (scope == NULL)
6616 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006617 scope->se_skip_save = skip_save;
6618 // "is_had_return" will be reset if any block does not end in :return
6619 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006620
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006621 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006622 {
6623 // "where" is set when ":elseif", "else" or ":endif" is found
6624 scope->se_u.se_if.is_if_label = instr->ga_len;
6625 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6626 }
6627 else
6628 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006629
6630 return p;
6631}
6632
6633 static char_u *
6634compile_elseif(char_u *arg, cctx_T *cctx)
6635{
6636 char_u *p = arg;
6637 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006638 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006639 isn_T *isn;
6640 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006641 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006642 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006643
6644 if (scope == NULL || scope->se_type != IF_SCOPE)
6645 {
6646 emsg(_(e_elseif_without_if));
6647 return NULL;
6648 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006649 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006650 if (!cctx->ctx_had_return)
6651 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006652
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006653 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006654 {
6655 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006656 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006657 return NULL;
6658 // previous "if" or "elseif" jumps here
6659 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6660 isn->isn_arg.jump.jump_where = instr->ga_len;
6661 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006662
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006663 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006664 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006665 if (cctx->ctx_skip == SKIP_YES)
6666 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006667 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006668 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006669 clear_ppconst(&ppconst);
6670 return NULL;
6671 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006672 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006673 if (scope->se_skip_save == SKIP_YES)
6674 clear_ppconst(&ppconst);
6675 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006676 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006677 int error = FALSE;
6678 int v;
6679
Bram Moolenaar7f141552020-05-09 17:35:53 +02006680 // The expression results in a constant.
6681 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006682 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6683 if (error)
6684 return NULL;
6685 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006686 clear_ppconst(&ppconst);
6687 scope->se_u.se_if.is_if_label = -1;
6688 }
6689 else
6690 {
6691 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006692 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006693 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006694 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006695 if (bool_on_stack(cctx) == FAIL)
6696 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006697
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006698 // "where" is set when ":elseif", "else" or ":endif" is found
6699 scope->se_u.se_if.is_if_label = instr->ga_len;
6700 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6701 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006702
6703 return p;
6704}
6705
6706 static char_u *
6707compile_else(char_u *arg, cctx_T *cctx)
6708{
6709 char_u *p = arg;
6710 garray_T *instr = &cctx->ctx_instr;
6711 isn_T *isn;
6712 scope_T *scope = cctx->ctx_scope;
6713
6714 if (scope == NULL || scope->se_type != IF_SCOPE)
6715 {
6716 emsg(_(e_else_without_if));
6717 return NULL;
6718 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006719 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006720 if (!cctx->ctx_had_return)
6721 scope->se_u.se_if.is_had_return = FALSE;
6722 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006723
Bram Moolenaarefd88552020-06-18 20:50:10 +02006724 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006725 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006726 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006727 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006728 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006729 if (!cctx->ctx_had_return
6730 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6731 JUMP_ALWAYS, cctx) == FAIL)
6732 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006733 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006734
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006735 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006736 {
6737 if (scope->se_u.se_if.is_if_label >= 0)
6738 {
6739 // previous "if" or "elseif" jumps here
6740 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6741 isn->isn_arg.jump.jump_where = instr->ga_len;
6742 scope->se_u.se_if.is_if_label = -1;
6743 }
6744 }
6745
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006746 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006747 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6748 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006749
6750 return p;
6751}
6752
6753 static char_u *
6754compile_endif(char_u *arg, cctx_T *cctx)
6755{
6756 scope_T *scope = cctx->ctx_scope;
6757 ifscope_T *ifscope;
6758 garray_T *instr = &cctx->ctx_instr;
6759 isn_T *isn;
6760
6761 if (scope == NULL || scope->se_type != IF_SCOPE)
6762 {
6763 emsg(_(e_endif_without_if));
6764 return NULL;
6765 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006766 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006767 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006768 if (!cctx->ctx_had_return)
6769 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006770
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006771 if (scope->se_u.se_if.is_if_label >= 0)
6772 {
6773 // previous "if" or "elseif" jumps here
6774 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6775 isn->isn_arg.jump.jump_where = instr->ga_len;
6776 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006777 // Fill in the "end" label in jumps at the end of the blocks.
6778 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006779 cctx->ctx_skip = scope->se_skip_save;
6780
6781 // If all the blocks end in :return and there is an :else then the
6782 // had_return flag is set.
6783 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006784
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006785 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006786 return arg;
6787}
6788
6789/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01006790 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006791 *
6792 * Produces instructions:
6793 * PUSHNR -1
6794 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01006795 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006796 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6797 * - if beyond end, jump to "end"
6798 * - otherwise get item from list and push it
6799 * STORE var Store item in "var"
6800 * ... body ...
6801 * JUMP top Jump back to repeat
6802 * end: DROP Drop the result of "expr"
6803 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006804 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
6805 * UNPACK 2 Split item in 2
6806 * STORE var1 Store item in "var1"
6807 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006808 */
6809 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006810compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006811{
Bram Moolenaar792f7862020-11-23 08:31:18 +01006812 char_u *arg;
6813 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006814 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006815 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006816 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006817 int var_count = 0;
6818 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006819 size_t varlen;
6820 garray_T *instr = &cctx->ctx_instr;
6821 garray_T *stack = &cctx->ctx_type_stack;
6822 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006823 lvar_T *loop_lvar; // loop iteration variable
6824 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006825 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006826 type_T *item_type = &t_any;
6827 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006828
Bram Moolenaar792f7862020-11-23 08:31:18 +01006829 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
6830 if (var_count == 0)
6831 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006832
6833 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006834 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006835 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6836 return NULL;
6837 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006838 {
6839 emsg(_(e_missing_in));
6840 return NULL;
6841 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006842 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006843 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6844 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006845
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006846 scope = new_scope(cctx, FOR_SCOPE);
6847 if (scope == NULL)
6848 return NULL;
6849
Bram Moolenaar792f7862020-11-23 08:31:18 +01006850 // Reserve a variable to store the loop iteration counter and initialize it
6851 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006852 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6853 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006854 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006855 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006856 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006857 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006858 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006859 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006860
6861 // compile "expr", it remains on the stack until "endfor"
6862 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006863 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006864 {
6865 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006866 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006867 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006868 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006869
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006870 // Now that we know the type of "var", check that it is a list, now or at
6871 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006872 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006873 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006874 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006875 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006876 return NULL;
6877 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006878
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006879 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006880 {
6881 if (var_count == 1)
6882 item_type = vartype->tt_member;
6883 else if (vartype->tt_member->tt_type == VAR_LIST
6884 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
6885 item_type = vartype->tt_member->tt_member;
6886 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006887
6888 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006889 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006890 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006891
Bram Moolenaar792f7862020-11-23 08:31:18 +01006892 arg = arg_start;
6893 if (var_count > 1)
6894 {
6895 generate_UNPACK(cctx, var_count, semicolon);
6896 arg = skipwhite(arg + 1); // skip white after '['
6897
6898 // the list item is replaced by a number of items
6899 if (ga_grow(stack, var_count - 1) == FAIL)
6900 {
6901 drop_scope(cctx);
6902 return NULL;
6903 }
6904 --stack->ga_len;
6905 for (idx = 0; idx < var_count; ++idx)
6906 {
6907 ((type_T **)stack->ga_data)[stack->ga_len] =
6908 (semicolon && idx == 0) ? vartype : item_type;
6909 ++stack->ga_len;
6910 }
6911 }
6912
6913 for (idx = 0; idx < var_count; ++idx)
6914 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006915 assign_dest_T dest = dest_local;
6916 int opt_flags = 0;
6917 int vimvaridx = -1;
6918 type_T *type = &t_any;
6919
6920 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006921 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006922 name = vim_strnsave(arg, varlen);
6923 if (name == NULL)
6924 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006925
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006926 // TODO: script var not supported?
6927 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
6928 &vimvaridx, &type, cctx) == FAIL)
6929 goto failed;
6930 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006931 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006932 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
6933 0, 0, type, name) == FAIL)
6934 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006935 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006936 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006937 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01006938 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006939 {
6940 semsg(_(e_variable_already_declared), arg);
6941 goto failed;
6942 }
6943
Bram Moolenaarea870692020-12-02 14:24:30 +01006944 if (STRNCMP(name, "s:", 2) == 0)
6945 {
6946 semsg(_(e_cannot_declare_script_variable_in_function), name);
6947 goto failed;
6948 }
6949
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006950 // Reserve a variable to store "var".
6951 // TODO: check for type
6952 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6953 if (var_lvar == NULL)
6954 // out of memory or used as an argument
6955 goto failed;
6956
6957 if (semicolon && idx == var_count - 1)
6958 var_lvar->lv_type = vartype;
6959 else
6960 var_lvar->lv_type = item_type;
6961 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
6962 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006963
6964 if (*p == ',' || *p == ';')
6965 ++p;
6966 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006967 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006968 }
6969
6970 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006971
6972failed:
6973 vim_free(name);
6974 drop_scope(cctx);
6975 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006976}
6977
6978/*
6979 * compile "endfor"
6980 */
6981 static char_u *
6982compile_endfor(char_u *arg, cctx_T *cctx)
6983{
6984 garray_T *instr = &cctx->ctx_instr;
6985 scope_T *scope = cctx->ctx_scope;
6986 forscope_T *forscope;
6987 isn_T *isn;
6988
6989 if (scope == NULL || scope->se_type != FOR_SCOPE)
6990 {
6991 emsg(_(e_for));
6992 return NULL;
6993 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006994 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006995 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006996 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006997
6998 // At end of ":for" scope jump back to the FOR instruction.
6999 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7000
7001 // Fill in the "end" label in the FOR statement so it can jump here
7002 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7003 isn->isn_arg.forloop.for_end = instr->ga_len;
7004
7005 // Fill in the "end" label any BREAK statements
7006 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
7007
7008 // Below the ":for" scope drop the "expr" list from the stack.
7009 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7010 return NULL;
7011
7012 vim_free(scope);
7013
7014 return arg;
7015}
7016
7017/*
7018 * compile "while expr"
7019 *
7020 * Produces instructions:
7021 * top: EVAL expr Push result of "expr"
7022 * JUMP_IF_FALSE end jump if false
7023 * ... body ...
7024 * JUMP top Jump back to repeat
7025 * end:
7026 *
7027 */
7028 static char_u *
7029compile_while(char_u *arg, cctx_T *cctx)
7030{
7031 char_u *p = arg;
7032 garray_T *instr = &cctx->ctx_instr;
7033 scope_T *scope;
7034
7035 scope = new_scope(cctx, WHILE_SCOPE);
7036 if (scope == NULL)
7037 return NULL;
7038
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007039 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007040
7041 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007042 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007043 return NULL;
7044
Bram Moolenaar13106602020-10-04 16:06:05 +02007045 if (bool_on_stack(cctx) == FAIL)
7046 return FAIL;
7047
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007048 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007049 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007050 JUMP_IF_FALSE, cctx) == FAIL)
7051 return FAIL;
7052
7053 return p;
7054}
7055
7056/*
7057 * compile "endwhile"
7058 */
7059 static char_u *
7060compile_endwhile(char_u *arg, cctx_T *cctx)
7061{
7062 scope_T *scope = cctx->ctx_scope;
7063
7064 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7065 {
7066 emsg(_(e_while));
7067 return NULL;
7068 }
7069 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007070 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007071
7072 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007073 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007074
7075 // Fill in the "end" label in the WHILE statement so it can jump here.
7076 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007077 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007078
7079 vim_free(scope);
7080
7081 return arg;
7082}
7083
7084/*
7085 * compile "continue"
7086 */
7087 static char_u *
7088compile_continue(char_u *arg, cctx_T *cctx)
7089{
7090 scope_T *scope = cctx->ctx_scope;
7091
7092 for (;;)
7093 {
7094 if (scope == NULL)
7095 {
7096 emsg(_(e_continue));
7097 return NULL;
7098 }
7099 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7100 break;
7101 scope = scope->se_outer;
7102 }
7103
7104 // Jump back to the FOR or WHILE instruction.
7105 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007106 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
7107 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007108 return arg;
7109}
7110
7111/*
7112 * compile "break"
7113 */
7114 static char_u *
7115compile_break(char_u *arg, cctx_T *cctx)
7116{
7117 scope_T *scope = cctx->ctx_scope;
7118 endlabel_T **el;
7119
7120 for (;;)
7121 {
7122 if (scope == NULL)
7123 {
7124 emsg(_(e_break));
7125 return NULL;
7126 }
7127 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7128 break;
7129 scope = scope->se_outer;
7130 }
7131
7132 // Jump to the end of the FOR or WHILE loop.
7133 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007134 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007135 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007136 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007137 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7138 return FAIL;
7139
7140 return arg;
7141}
7142
7143/*
7144 * compile "{" start of block
7145 */
7146 static char_u *
7147compile_block(char_u *arg, cctx_T *cctx)
7148{
7149 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7150 return NULL;
7151 return skipwhite(arg + 1);
7152}
7153
7154/*
7155 * compile end of block: drop one scope
7156 */
7157 static void
7158compile_endblock(cctx_T *cctx)
7159{
7160 scope_T *scope = cctx->ctx_scope;
7161
7162 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007163 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007164 vim_free(scope);
7165}
7166
7167/*
7168 * compile "try"
7169 * Creates a new scope for the try-endtry, pointing to the first catch and
7170 * finally.
7171 * Creates another scope for the "try" block itself.
7172 * TRY instruction sets up exception handling at runtime.
7173 *
7174 * "try"
7175 * TRY -> catch1, -> finally push trystack entry
7176 * ... try block
7177 * "throw {exception}"
7178 * EVAL {exception}
7179 * THROW create exception
7180 * ... try block
7181 * " catch {expr}"
7182 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007183 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007184 * EVAL {expr}
7185 * MATCH
7186 * JUMP nomatch -> catch2
7187 * CATCH remove exception
7188 * ... catch block
7189 * " catch"
7190 * JUMP -> finally
7191 * catch2: CATCH remove exception
7192 * ... catch block
7193 * " finally"
7194 * finally:
7195 * ... finally block
7196 * " endtry"
7197 * ENDTRY pop trystack entry, may rethrow
7198 */
7199 static char_u *
7200compile_try(char_u *arg, cctx_T *cctx)
7201{
7202 garray_T *instr = &cctx->ctx_instr;
7203 scope_T *try_scope;
7204 scope_T *scope;
7205
7206 // scope that holds the jumps that go to catch/finally/endtry
7207 try_scope = new_scope(cctx, TRY_SCOPE);
7208 if (try_scope == NULL)
7209 return NULL;
7210
Bram Moolenaar69f70502021-01-01 16:10:46 +01007211 if (cctx->ctx_skip != SKIP_YES)
7212 {
7213 // "catch" is set when the first ":catch" is found.
7214 // "finally" is set when ":finally" or ":endtry" is found
7215 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
7216 if (generate_instr(cctx, ISN_TRY) == NULL)
7217 return NULL;
7218 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007219
7220 // scope for the try block itself
7221 scope = new_scope(cctx, BLOCK_SCOPE);
7222 if (scope == NULL)
7223 return NULL;
7224
7225 return arg;
7226}
7227
7228/*
7229 * compile "catch {expr}"
7230 */
7231 static char_u *
7232compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7233{
7234 scope_T *scope = cctx->ctx_scope;
7235 garray_T *instr = &cctx->ctx_instr;
7236 char_u *p;
7237 isn_T *isn;
7238
7239 // end block scope from :try or :catch
7240 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7241 compile_endblock(cctx);
7242 scope = cctx->ctx_scope;
7243
7244 // Error if not in a :try scope
7245 if (scope == NULL || scope->se_type != TRY_SCOPE)
7246 {
7247 emsg(_(e_catch));
7248 return NULL;
7249 }
7250
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007251 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007252 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007253 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007254 return NULL;
7255 }
7256
Bram Moolenaar69f70502021-01-01 16:10:46 +01007257 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007258 {
Bram Moolenaar69f70502021-01-01 16:10:46 +01007259 // Jump from end of previous block to :finally or :endtry
7260 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7261 JUMP_ALWAYS, cctx) == FAIL)
7262 return NULL;
7263
7264 // End :try or :catch scope: set value in ISN_TRY instruction
7265 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7266 if (isn->isn_arg.try.try_catch == 0)
7267 isn->isn_arg.try.try_catch = instr->ga_len;
7268 if (scope->se_u.se_try.ts_catch_label != 0)
7269 {
7270 // Previous catch without match jumps here
7271 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7272 isn->isn_arg.jump.jump_where = instr->ga_len;
7273 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007274 }
7275
7276 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007277 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007278 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007279 scope->se_u.se_try.ts_caught_all = TRUE;
7280 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007281 }
7282 else
7283 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007284 char_u *end;
7285 char_u *pat;
7286 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007287 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007288 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007290 // Push v:exception, push {expr} and MATCH
7291 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7292
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007293 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007294 if (*end != *p)
7295 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007296 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007297 vim_free(tofree);
7298 return FAIL;
7299 }
7300 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007301 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007302 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007303 len = (int)(end - tofree);
7304 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007305 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007306 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007307 if (pat == NULL)
7308 return FAIL;
7309 if (generate_PUSHS(cctx, pat) == FAIL)
7310 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007311
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007312 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7313 return NULL;
7314
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007315 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007316 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7317 return NULL;
7318 }
7319
Bram Moolenaar69f70502021-01-01 16:10:46 +01007320 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007321 return NULL;
7322
7323 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7324 return NULL;
7325 return p;
7326}
7327
7328 static char_u *
7329compile_finally(char_u *arg, cctx_T *cctx)
7330{
7331 scope_T *scope = cctx->ctx_scope;
7332 garray_T *instr = &cctx->ctx_instr;
7333 isn_T *isn;
7334
7335 // end block scope from :try or :catch
7336 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7337 compile_endblock(cctx);
7338 scope = cctx->ctx_scope;
7339
7340 // Error if not in a :try scope
7341 if (scope == NULL || scope->se_type != TRY_SCOPE)
7342 {
7343 emsg(_(e_finally));
7344 return NULL;
7345 }
7346
7347 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007348 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007349 if (isn->isn_arg.try.try_finally != 0)
7350 {
7351 emsg(_(e_finally_dup));
7352 return NULL;
7353 }
7354
7355 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007356 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007357
Bram Moolenaar585fea72020-04-02 22:33:21 +02007358 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007359 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007360 {
7361 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007362 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007363 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007364 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007365 }
7366
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007367 // TODO: set index in ts_finally_label jumps
7368
7369 return arg;
7370}
7371
7372 static char_u *
7373compile_endtry(char_u *arg, cctx_T *cctx)
7374{
7375 scope_T *scope = cctx->ctx_scope;
7376 garray_T *instr = &cctx->ctx_instr;
7377 isn_T *isn;
7378
7379 // end block scope from :catch or :finally
7380 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7381 compile_endblock(cctx);
7382 scope = cctx->ctx_scope;
7383
7384 // Error if not in a :try scope
7385 if (scope == NULL || scope->se_type != TRY_SCOPE)
7386 {
7387 if (scope == NULL)
7388 emsg(_(e_no_endtry));
7389 else if (scope->se_type == WHILE_SCOPE)
7390 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007391 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007392 emsg(_(e_endfor));
7393 else
7394 emsg(_(e_endif));
7395 return NULL;
7396 }
7397
Bram Moolenaar69f70502021-01-01 16:10:46 +01007398 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007399 {
Bram Moolenaar69f70502021-01-01 16:10:46 +01007400 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7401 if (isn->isn_arg.try.try_catch == 0
7402 && isn->isn_arg.try.try_finally == 0)
7403 {
7404 emsg(_(e_missing_catch_or_finally));
7405 return NULL;
7406 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007407
Bram Moolenaar69f70502021-01-01 16:10:46 +01007408 // Fill in the "end" label in jumps at the end of the blocks, if not
7409 // done by ":finally".
7410 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007411
Bram Moolenaar69f70502021-01-01 16:10:46 +01007412 // End :catch or :finally scope: set value in ISN_TRY instruction
7413 if (isn->isn_arg.try.try_catch == 0)
7414 isn->isn_arg.try.try_catch = instr->ga_len;
7415 if (isn->isn_arg.try.try_finally == 0)
7416 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007417
Bram Moolenaar69f70502021-01-01 16:10:46 +01007418 if (scope->se_u.se_try.ts_catch_label != 0)
7419 {
7420 // Last catch without match jumps here
7421 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7422 isn->isn_arg.jump.jump_where = instr->ga_len;
7423 }
Bram Moolenaare8593122020-07-18 15:17:02 +02007424 }
7425
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007426 compile_endblock(cctx);
7427
Bram Moolenaar69f70502021-01-01 16:10:46 +01007428 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_ENDTRY) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007429 return NULL;
7430 return arg;
7431}
7432
7433/*
7434 * compile "throw {expr}"
7435 */
7436 static char_u *
7437compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7438{
7439 char_u *p = skipwhite(arg);
7440
Bram Moolenaara5565e42020-05-09 15:44:01 +02007441 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007442 return NULL;
7443 if (may_generate_2STRING(-1, cctx) == FAIL)
7444 return NULL;
7445 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7446 return NULL;
7447
7448 return p;
7449}
7450
7451/*
7452 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007453 * compile "echomsg expr"
7454 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007455 * compile "execute expr"
7456 */
7457 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007458compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007459{
7460 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007461 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007462 int count = 0;
7463
7464 for (;;)
7465 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007466 if (ends_excmd2(prev, p))
7467 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007468 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007469 return NULL;
7470 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007471 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007472 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007473 }
7474
Bram Moolenaare4984292020-12-13 14:19:25 +01007475 if (count > 0)
7476 {
7477 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7478 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7479 else if (cmdidx == CMD_execute)
7480 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7481 else if (cmdidx == CMD_echomsg)
7482 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7483 else
7484 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7485 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007486 return p;
7487}
7488
7489/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007490 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01007491 * instruction to compute it and return OK.
7492 * Otherwise return FAIL, the caller must deal with any range.
7493 */
7494 static int
7495compile_variable_range(exarg_T *eap, cctx_T *cctx)
7496{
7497 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7498 char_u *p = skipdigits(eap->cmd);
7499
7500 if (p == range_end)
7501 return FAIL;
7502 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7503}
7504
7505/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007506 * :put r
7507 * :put ={expr}
7508 */
7509 static char_u *
7510compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7511{
7512 char_u *line = arg;
7513 linenr_T lnum;
7514 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007515 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007516
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007517 eap->regname = *line;
7518
7519 if (eap->regname == '=')
7520 {
7521 char_u *p = line + 1;
7522
7523 if (compile_expr0(&p, cctx) == FAIL)
7524 return NULL;
7525 line = p;
7526 }
7527 else if (eap->regname != NUL)
7528 ++line;
7529
Bram Moolenaar08597872020-12-10 19:43:40 +01007530 if (compile_variable_range(eap, cctx) == OK)
7531 {
7532 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
7533 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007534 else
Bram Moolenaar08597872020-12-10 19:43:40 +01007535 {
7536 // Either no range or a number.
7537 // "errormsg" will not be set because the range is ADDR_LINES.
7538 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01007539 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01007540 return NULL;
7541 if (eap->addr_count == 0)
7542 lnum = -1;
7543 else
7544 lnum = eap->line2;
7545 if (above)
7546 --lnum;
7547 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007548
7549 generate_PUT(cctx, eap->regname, lnum);
7550 return line;
7551}
7552
7553/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007554 * A command that is not compiled, execute with legacy code.
7555 */
7556 static char_u *
7557compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7558{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007559 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007560 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007561 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007562
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007563 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007564 goto theend;
7565
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007566 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007567 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007568 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007569 int usefilter = FALSE;
7570
7571 has_expr = argt & (EX_XFILE | EX_EXPAND);
7572
7573 // If the command can be followed by a bar, find the bar and truncate
7574 // it, so that the following command can be compiled.
7575 // The '|' is overwritten with a NUL, it is put back below.
7576 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7577 && *eap->arg == '!')
7578 // :w !filter or :r !filter or :r! filter
7579 usefilter = TRUE;
7580 if ((argt & EX_TRLBAR) && !usefilter)
7581 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007582 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007583 separate_nextcmd(eap);
7584 if (eap->nextcmd != NULL)
7585 nextcmd = eap->nextcmd;
7586 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01007587 else if (eap->cmdidx == CMD_wincmd)
7588 {
7589 p = eap->arg;
7590 if (*p != NUL)
7591 ++p;
7592 if (*p == 'g' || *p == Ctrl_G)
7593 ++p;
7594 p = skipwhite(p);
7595 if (*p == '|')
7596 {
7597 *p = NUL;
7598 nextcmd = p + 1;
7599 }
7600 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007601 }
7602
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007603 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
7604 {
7605 // expand filename in "syntax include [@group] filename"
7606 has_expr = TRUE;
7607 eap->arg = skipwhite(eap->arg + 7);
7608 if (*eap->arg == '@')
7609 eap->arg = skiptowhite(eap->arg);
7610 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007611
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007612 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
7613 && STRLEN(eap->arg) > 4)
7614 {
7615 int delim = *eap->arg;
7616
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007617 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007618 if (*p == delim)
7619 {
7620 eap->arg = p + 1;
7621 has_expr = TRUE;
7622 }
7623 }
7624
Bram Moolenaarecac5912021-01-05 19:23:28 +01007625 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
7626 {
7627 // TODO: should only expand when appropriate for the command
7628 eap->arg = skiptowhite(eap->arg);
7629 has_expr = TRUE;
7630 }
7631
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007632 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007633 {
7634 int count = 0;
7635 char_u *start = skipwhite(line);
7636
7637 // :cmd xxx`=expr1`yyy`=expr2`zzz
7638 // PUSHS ":cmd xxx"
7639 // eval expr1
7640 // PUSHS "yyy"
7641 // eval expr2
7642 // PUSHS "zzz"
7643 // EXECCONCAT 5
7644 for (;;)
7645 {
7646 if (p > start)
7647 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007648 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007649 ++count;
7650 }
7651 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007652 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007653 return NULL;
7654 may_generate_2STRING(-1, cctx);
7655 ++count;
7656 p = skipwhite(p);
7657 if (*p != '`')
7658 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007659 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007660 return NULL;
7661 }
7662 start = p + 1;
7663
7664 p = (char_u *)strstr((char *)start, "`=");
7665 if (p == NULL)
7666 {
7667 if (*skipwhite(start) != NUL)
7668 {
7669 generate_PUSHS(cctx, vim_strsave(start));
7670 ++count;
7671 }
7672 break;
7673 }
7674 }
7675 generate_EXECCONCAT(cctx, count);
7676 }
7677 else
7678 generate_EXEC(cctx, line);
7679
7680theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007681 if (*nextcmd != NUL)
7682 {
7683 // the parser expects a pointer to the bar, put it back
7684 --nextcmd;
7685 *nextcmd = '|';
7686 }
7687
7688 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007689}
7690
7691/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007692 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007693 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007694 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007695 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007696add_def_function(ufunc_T *ufunc)
7697{
7698 dfunc_T *dfunc;
7699
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007700 if (def_functions.ga_len == 0)
7701 {
7702 // The first position is not used, so that a zero uf_dfunc_idx means it
7703 // wasn't set.
7704 if (ga_grow(&def_functions, 1) == FAIL)
7705 return FAIL;
7706 ++def_functions.ga_len;
7707 }
7708
Bram Moolenaar09689a02020-05-09 22:50:08 +02007709 // Add the function to "def_functions".
7710 if (ga_grow(&def_functions, 1) == FAIL)
7711 return FAIL;
7712 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7713 CLEAR_POINTER(dfunc);
7714 dfunc->df_idx = def_functions.ga_len;
7715 ufunc->uf_dfunc_idx = dfunc->df_idx;
7716 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01007717 dfunc->df_name = vim_strsave(ufunc->uf_name);
7718 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007719 ++def_functions.ga_len;
7720 return OK;
7721}
7722
7723/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007724 * After ex_function() has collected all the function lines: parse and compile
7725 * the lines into instructions.
7726 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007727 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
7728 * the return statement (used for lambda). When uf_ret_type is already set
7729 * then check that it matches.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007730 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007731 * This can be used recursively through compile_lambda(), which may reallocate
7732 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007733 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007734 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007735 int
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007736compile_def_function(ufunc_T *ufunc, int check_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007737{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007738 char_u *line = NULL;
7739 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007740 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007741 cctx_T cctx;
7742 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007743 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007744 int ret = FAIL;
7745 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007746 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007747 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007748 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007749
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007750 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01007751 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007752 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007753 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007754 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7755 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01007756 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007757 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007758 else
7759 {
7760 if (add_def_function(ufunc) == FAIL)
7761 return FAIL;
7762 new_def_function = TRUE;
7763 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007764
Bram Moolenaar985116a2020-07-12 17:31:09 +02007765 ufunc->uf_def_status = UF_COMPILING;
7766
Bram Moolenaara80faa82020-04-12 19:37:17 +02007767 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007768 cctx.ctx_ufunc = ufunc;
7769 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007770 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007771 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7772 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7773 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7774 cctx.ctx_type_list = &ufunc->uf_type_list;
7775 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7776 instr = &cctx.ctx_instr;
7777
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007778 // Set the context to the function, it may be compiled when called from
7779 // another script. Set the script version to the most modern one.
7780 // The line number will be set in next_line_from_context().
7781 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007782 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7783
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007784 // Make sure error messages are OK.
7785 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7786 if (do_estack_push)
7787 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007788 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007789
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007790 if (ufunc->uf_def_args.ga_len > 0)
7791 {
7792 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007793 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007794 int i;
7795 char_u *arg;
7796 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007797 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007798
7799 // Produce instructions for the default values of optional arguments.
7800 // Store the instruction index in uf_def_arg_idx[] so that we know
7801 // where to start when the function is called, depending on the number
7802 // of arguments.
7803 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7804 if (ufunc->uf_def_arg_idx == NULL)
7805 goto erret;
7806 for (i = 0; i < count; ++i)
7807 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007808 garray_T *stack = &cctx.ctx_type_stack;
7809 type_T *val_type;
7810 int arg_idx = first_def_arg + i;
7811
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007812 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7813 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007814 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007815 goto erret;
7816
7817 // If no type specified use the type of the default value.
7818 // Otherwise check that the default value type matches the
7819 // specified type.
7820 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7821 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007822 {
7823 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007824 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007825 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007826 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7827 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007828 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007829
7830 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007831 goto erret;
7832 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007833 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007834
7835 if (did_set_arg_type)
7836 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007837 }
7838
7839 /*
7840 * Loop over all the lines of the function and generate instructions.
7841 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007842 for (;;)
7843 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007844 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007845 int starts_with_colon = FALSE;
7846 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007847 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007848
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007849 // Bail out on the first error to avoid a flood of errors and report
7850 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007851 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007852 goto erret;
7853
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007854 if (line != NULL && *line == '|')
7855 // the line continues after a '|'
7856 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007857 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007858 && !(*line == '#' && (line == cctx.ctx_line_start
7859 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007860 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007861 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007862 goto erret;
7863 }
7864 else
7865 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007866 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007867 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007868 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007869 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007870 }
7871
Bram Moolenaara80faa82020-04-12 19:37:17 +02007872 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007873 ea.cmdlinep = &line;
7874 ea.cmd = skipwhite(line);
7875
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007876 // Some things can be recognized by the first character.
7877 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007878 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007879 case '#':
Bram Moolenaare0de1712020-12-02 17:36:54 +01007880 // "#" starts a comment
7881 line = (char_u *)"";
7882 continue;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007883
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007884 case '}':
7885 {
7886 // "}" ends a block scope
7887 scopetype_T stype = cctx.ctx_scope == NULL
7888 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007889
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007890 if (stype == BLOCK_SCOPE)
7891 {
7892 compile_endblock(&cctx);
7893 line = ea.cmd;
7894 }
7895 else
7896 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007897 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007898 goto erret;
7899 }
7900 if (line != NULL)
7901 line = skipwhite(ea.cmd + 1);
7902 continue;
7903 }
7904
7905 case '{':
7906 // "{" starts a block scope
7907 // "{'a': 1}->func() is something else
7908 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7909 {
7910 line = compile_block(ea.cmd, &cctx);
7911 continue;
7912 }
7913 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007914 }
7915
7916 /*
7917 * COMMAND MODIFIERS
7918 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007919 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007920 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7921 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007922 {
7923 if (errormsg != NULL)
7924 goto erret;
7925 // empty line or comment
7926 line = (char_u *)"";
7927 continue;
7928 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007929 generate_cmdmods(&cctx, &local_cmdmod);
7930 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007931
Bram Moolenaare88c8e82020-11-01 17:03:37 +01007932 // Check if there was a colon after the last command modifier or before
7933 // the current position.
7934 for (p = ea.cmd; p >= line; --p)
7935 {
7936 if (*p == ':')
7937 starts_with_colon = TRUE;
7938 if (p < ea.cmd && !VIM_ISWHITE(*p))
7939 break;
7940 }
7941
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007942 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007943 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007944 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007945 {
7946 if (*ea.cmd == '(')
7947 // not for "call()"
7948 ea.cmd = p;
7949 else
7950 ea.cmd = skipwhite(ea.cmd);
7951 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007952
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007953 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007954 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01007955 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007956
Bram Moolenaar17126b12021-01-07 22:03:02 +01007957 // Check for assignment after command modifiers.
7958 assign = may_compile_assignment(&ea, &line, &cctx);
7959 if (assign == OK)
7960 goto nextline;
7961 if (assign == FAIL)
7962 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007963 }
7964
7965 /*
7966 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007967 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007968 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007969 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007970 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007971 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007972 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007973 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007974 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007975 if (!starts_with_colon)
7976 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01007977 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007978 goto erret;
7979 }
7980 if (ends_excmd2(line, ea.cmd))
7981 {
7982 // A range without a command: jump to the line.
7983 // TODO: compile to a more efficient command, possibly
7984 // calling parse_cmd_address().
7985 ea.cmdidx = CMD_SIZE;
7986 line = compile_exec(line, &ea, &cctx);
7987 goto nextline;
7988 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007989 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007990 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007991 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +01007992 : (int (*)(char_u *, size_t, void *, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007993 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007994
Bram Moolenaard1510ee2021-01-04 16:15:58 +01007995 if (p == NULL)
7996 {
7997 if (cctx.ctx_skip != SKIP_YES)
7998 emsg(_(e_ambiguous_use_of_user_defined_command));
7999 goto erret;
8000 }
8001
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008002 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8003 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008004 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008005 {
8006 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008007 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008008 }
8009
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008010 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008011 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008012 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008013 // CMD_var cannot happen, compile_assignment() above would be
8014 // used. Most likely an assignment to a non-existing variable.
8015 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008016 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008017 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008018 }
8019
Bram Moolenaar3988f642020-08-27 22:43:03 +02008020 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008021 && ea.cmdidx != CMD_elseif
8022 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008023 && ea.cmdidx != CMD_endif
8024 && ea.cmdidx != CMD_endfor
8025 && ea.cmdidx != CMD_endwhile
8026 && ea.cmdidx != CMD_catch
8027 && ea.cmdidx != CMD_finally
8028 && ea.cmdidx != CMD_endtry)
8029 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008030 emsg(_(e_unreachable_code_after_return));
8031 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008032 }
8033
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008034 p = skipwhite(p);
8035 if (ea.cmdidx != CMD_SIZE
8036 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8037 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008038 if (ea.cmdidx >= 0)
8039 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008040 if ((ea.argt & EX_BANG) && *p == '!')
8041 {
8042 ea.forceit = TRUE;
8043 p = skipwhite(p + 1);
8044 }
8045 }
8046
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008047 switch (ea.cmdidx)
8048 {
8049 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008050 ea.arg = p;
8051 line = compile_nested_function(&ea, &cctx);
8052 break;
8053
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008054 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008055 // TODO: should we allow this, e.g. to declare a global
8056 // function?
8057 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008058 goto erret;
8059
8060 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008061 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008062 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008063 break;
8064
8065 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008066 emsg(_(e_cannot_use_let_in_vim9_script));
8067 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008068 case CMD_var:
8069 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008070 case CMD_const:
8071 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008072 if (line == p)
8073 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008074 break;
8075
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008076 case CMD_unlet:
8077 case CMD_unlockvar:
8078 case CMD_lockvar:
8079 line = compile_unletlock(p, &ea, &cctx);
8080 break;
8081
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008082 case CMD_import:
8083 line = compile_import(p, &cctx);
8084 break;
8085
8086 case CMD_if:
8087 line = compile_if(p, &cctx);
8088 break;
8089 case CMD_elseif:
8090 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008091 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008092 break;
8093 case CMD_else:
8094 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008095 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008096 break;
8097 case CMD_endif:
8098 line = compile_endif(p, &cctx);
8099 break;
8100
8101 case CMD_while:
8102 line = compile_while(p, &cctx);
8103 break;
8104 case CMD_endwhile:
8105 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008106 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008107 break;
8108
8109 case CMD_for:
8110 line = compile_for(p, &cctx);
8111 break;
8112 case CMD_endfor:
8113 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008114 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008115 break;
8116 case CMD_continue:
8117 line = compile_continue(p, &cctx);
8118 break;
8119 case CMD_break:
8120 line = compile_break(p, &cctx);
8121 break;
8122
8123 case CMD_try:
8124 line = compile_try(p, &cctx);
8125 break;
8126 case CMD_catch:
8127 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008128 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008129 break;
8130 case CMD_finally:
8131 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008132 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008133 break;
8134 case CMD_endtry:
8135 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008136 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008137 break;
8138 case CMD_throw:
8139 line = compile_throw(p, &cctx);
8140 break;
8141
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008142 case CMD_eval:
8143 if (compile_expr0(&p, &cctx) == FAIL)
8144 goto erret;
8145
Bram Moolenaar3988f642020-08-27 22:43:03 +02008146 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008147 generate_instr_drop(&cctx, ISN_DROP, 1);
8148
8149 line = skipwhite(p);
8150 break;
8151
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008152 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008153 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008154 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008155 case CMD_echomsg:
8156 case CMD_echoerr:
8157 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008158 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008159
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008160 case CMD_put:
8161 ea.cmd = cmd;
8162 line = compile_put(p, &ea, &cctx);
8163 break;
8164
Bram Moolenaar3988f642020-08-27 22:43:03 +02008165 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008166
Bram Moolenaarae616492020-07-28 20:07:27 +02008167 case CMD_append:
8168 case CMD_change:
8169 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008170 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008171 case CMD_xit:
8172 not_in_vim9(&ea);
8173 goto erret;
8174
Bram Moolenaar002262f2020-07-08 17:47:57 +02008175 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008176 if (cctx.ctx_skip != SKIP_YES)
8177 {
8178 semsg(_(e_invalid_command_str), ea.cmd);
8179 goto erret;
8180 }
8181 // We don't check for a next command here.
8182 line = (char_u *)"";
8183 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008184
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008185 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008186 if (cctx.ctx_skip == SKIP_YES)
8187 {
8188 // We don't check for a next command here.
8189 line = (char_u *)"";
8190 }
8191 else
8192 {
8193 // Not recognized, execute with do_cmdline_cmd().
8194 ea.arg = p;
8195 line = compile_exec(line, &ea, &cctx);
8196 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008197 break;
8198 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008199nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008200 if (line == NULL)
8201 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008202 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008203
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008204 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008205 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008206
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008207 if (cctx.ctx_type_stack.ga_len < 0)
8208 {
8209 iemsg("Type stack underflow");
8210 goto erret;
8211 }
8212 }
8213
8214 if (cctx.ctx_scope != NULL)
8215 {
8216 if (cctx.ctx_scope->se_type == IF_SCOPE)
8217 emsg(_(e_endif));
8218 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8219 emsg(_(e_endwhile));
8220 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8221 emsg(_(e_endfor));
8222 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008223 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008224 goto erret;
8225 }
8226
Bram Moolenaarefd88552020-06-18 20:50:10 +02008227 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008228 {
8229 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8230 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008231 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008232 goto erret;
8233 }
8234
8235 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008236 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008237 }
8238
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008239 {
8240 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8241 + ufunc->uf_dfunc_idx;
8242 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008243 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008244 dfunc->df_instr = instr->ga_data;
8245 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008246 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008247 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008248 if (cctx.ctx_outer_used)
8249 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008250 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008251 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008252
8253 ret = OK;
8254
8255erret:
8256 if (ret == FAIL)
8257 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008258 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008259 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8260 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008261
8262 for (idx = 0; idx < instr->ga_len; ++idx)
8263 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008264 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008265 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008266
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008267 // If using the last entry in the table and it was added above, we
8268 // might as well remove it.
8269 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008270 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008271 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008272 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008273 ufunc->uf_dfunc_idx = 0;
8274 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008275 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008276
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008277 while (cctx.ctx_scope != NULL)
8278 drop_scope(&cctx);
8279
Bram Moolenaar20431c92020-03-20 18:39:46 +01008280 // Don't execute this function body.
8281 ga_clear_strings(&ufunc->uf_lines);
8282
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008283 if (errormsg != NULL)
8284 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008285 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008286 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008287 }
8288
8289 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008290 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008291 if (do_estack_push)
8292 estack_pop();
8293
Bram Moolenaar20431c92020-03-20 18:39:46 +01008294 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008295 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008296 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008297 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008298}
8299
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008300 void
8301set_function_type(ufunc_T *ufunc)
8302{
8303 int varargs = ufunc->uf_va_name != NULL;
8304 int argcount = ufunc->uf_args.ga_len;
8305
8306 // Create a type for the function, with the return type and any
8307 // argument types.
8308 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8309 // The type is included in "tt_args".
8310 if (argcount > 0 || varargs)
8311 {
8312 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8313 argcount, &ufunc->uf_type_list);
8314 // Add argument types to the function type.
8315 if (func_type_add_arg_types(ufunc->uf_func_type,
8316 argcount + varargs,
8317 &ufunc->uf_type_list) == FAIL)
8318 return;
8319 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8320 ufunc->uf_func_type->tt_min_argcount =
8321 argcount - ufunc->uf_def_args.ga_len;
8322 if (ufunc->uf_arg_types == NULL)
8323 {
8324 int i;
8325
8326 // lambda does not have argument types.
8327 for (i = 0; i < argcount; ++i)
8328 ufunc->uf_func_type->tt_args[i] = &t_any;
8329 }
8330 else
8331 mch_memmove(ufunc->uf_func_type->tt_args,
8332 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8333 if (varargs)
8334 {
8335 ufunc->uf_func_type->tt_args[argcount] =
8336 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8337 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8338 }
8339 }
8340 else
8341 // No arguments, can use a predefined type.
8342 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8343 argcount, &ufunc->uf_type_list);
8344}
8345
8346
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008347/*
8348 * Delete an instruction, free what it contains.
8349 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008350 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008351delete_instr(isn_T *isn)
8352{
8353 switch (isn->isn_type)
8354 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008355 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008356 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008357 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008358 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008359 case ISN_LOADENV:
8360 case ISN_LOADG:
8361 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008362 case ISN_LOADT:
8363 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008364 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008365 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008366 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008367 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008368 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008369 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008370 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008371 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008372 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008373 case ISN_STOREW:
8374 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008375 vim_free(isn->isn_arg.string);
8376 break;
8377
8378 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008379 case ISN_STORES:
8380 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008381 break;
8382
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008383 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008384 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008385 vim_free(isn->isn_arg.unlet.ul_name);
8386 break;
8387
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008388 case ISN_STOREOPT:
8389 vim_free(isn->isn_arg.storeopt.so_name);
8390 break;
8391
8392 case ISN_PUSHBLOB: // push blob isn_arg.blob
8393 blob_unref(isn->isn_arg.blob);
8394 break;
8395
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008396 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008397#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008398 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008399#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008400 break;
8401
8402 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008403#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008404 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008405#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008406 break;
8407
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008408 case ISN_UCALL:
8409 vim_free(isn->isn_arg.ufunc.cuf_name);
8410 break;
8411
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008412 case ISN_FUNCREF:
8413 {
8414 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8415 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01008416 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008417
Bram Moolenaar077a4232020-12-22 18:33:27 +01008418 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
8419 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02008420 }
8421 break;
8422
8423 case ISN_DCALL:
8424 {
8425 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8426 + isn->isn_arg.dfunc.cdf_idx;
8427
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008428 if (dfunc->df_ufunc != NULL
8429 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008430 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008431 }
8432 break;
8433
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008434 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008435 {
8436 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8437 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8438
8439 if (ufunc != NULL)
8440 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008441 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02008442 func_ptr_unref(ufunc);
8443 }
8444
8445 vim_free(lambda);
8446 vim_free(isn->isn_arg.newfunc.nf_global);
8447 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008448 break;
8449
Bram Moolenaar5e654232020-09-16 15:22:00 +02008450 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01008451 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02008452 free_type(isn->isn_arg.type.ct_type);
8453 break;
8454
Bram Moolenaar02194d22020-10-24 23:08:38 +02008455 case ISN_CMDMOD:
8456 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8457 ->cmod_filter_regmatch.regprog);
8458 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8459 break;
8460
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008461 case ISN_LOADSCRIPT:
8462 case ISN_STORESCRIPT:
8463 vim_free(isn->isn_arg.script.scriptref);
8464 break;
8465
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008466 case ISN_2BOOL:
8467 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008468 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008469 case ISN_ADDBLOB:
8470 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008471 case ISN_ANYINDEX:
8472 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008473 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008474 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008475 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008476 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008477 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008478 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008479 case ISN_COMPAREANY:
8480 case ISN_COMPAREBLOB:
8481 case ISN_COMPAREBOOL:
8482 case ISN_COMPAREDICT:
8483 case ISN_COMPAREFLOAT:
8484 case ISN_COMPAREFUNC:
8485 case ISN_COMPARELIST:
8486 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008487 case ISN_COMPARESPECIAL:
8488 case ISN_COMPARESTRING:
8489 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008490 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008491 case ISN_DROP:
8492 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008493 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008494 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008495 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008496 case ISN_EXECCONCAT:
8497 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008498 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008499 case ISN_GETITEM:
8500 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008501 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008502 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008503 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008504 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008505 case ISN_LOADBDICT:
8506 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008507 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008508 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008509 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008510 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008511 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008512 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008513 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008514 case ISN_NEGATENR:
8515 case ISN_NEWDICT:
8516 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008517 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008518 case ISN_OPFLOAT:
8519 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008520 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008521 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008522 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008523 case ISN_PUSHF:
8524 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008525 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008526 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008527 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01008528 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008529 case ISN_SHUFFLE:
8530 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008531 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01008532 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008533 case ISN_STORENR:
8534 case ISN_STOREOUTER:
8535 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008536 case ISN_STOREV:
8537 case ISN_STRINDEX:
8538 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008539 case ISN_THROW:
8540 case ISN_TRY:
Bram Moolenaar752fc692021-01-04 21:57:11 +01008541 case ISN_UNLETINDEX:
Bram Moolenaar792f7862020-11-23 08:31:18 +01008542 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008543 // nothing allocated
8544 break;
8545 }
8546}
8547
8548/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008549 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01008550 */
8551 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008552delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008553{
8554 int idx;
8555
8556 ga_clear(&dfunc->df_def_args_isn);
8557
8558 if (dfunc->df_instr != NULL)
8559 {
8560 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
8561 delete_instr(dfunc->df_instr + idx);
8562 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008563 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008564 }
8565
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008566 if (mark_deleted)
8567 dfunc->df_deleted = TRUE;
8568 if (dfunc->df_ufunc != NULL)
8569 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008570}
8571
8572/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008573 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008574 * function, unless another user function still uses it.
8575 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008576 */
8577 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008578unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008579{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008580 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008581 {
8582 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8583 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008584
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008585 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008586 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008587 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008588 ufunc->uf_dfunc_idx = 0;
8589 if (dfunc->df_ufunc == ufunc)
8590 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008591 }
8592}
8593
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008594/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008595 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008596 */
8597 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008598link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008599{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008600 if (ufunc->uf_dfunc_idx > 0)
8601 {
8602 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8603 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008604
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008605 ++dfunc->df_refcount;
8606 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008607}
8608
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008609#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008610/*
8611 * Free all functions defined with ":def".
8612 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008613 void
8614free_def_functions(void)
8615{
Bram Moolenaar20431c92020-03-20 18:39:46 +01008616 int idx;
8617
8618 for (idx = 0; idx < def_functions.ga_len; ++idx)
8619 {
8620 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
8621
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008622 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008623 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008624 }
8625
8626 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008627}
8628#endif
8629
8630
8631#endif // FEAT_EVAL