blob: 3fae25e1152c6f58227a467e5d894f0998ddf374 [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
111 int lv_from_outer; // when TRUE using ctx_outer scope
112 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 Moolenaar20431c92020-03-20 18:39:46 +0100148static void delete_def_function_contents(dfunc_T *dfunc);
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".
152 * "lvar->lv_from_outer" is set accordingly.
153 * 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;
175 lvar->lv_from_outer = FALSE;
176 }
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;
189 lvar->lv_from_outer = TRUE;
190 }
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 {
261 *gen_load_outer = TRUE;
262 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 Moolenaar84367732020-08-23 15:21:55 +0200321 * Returnd TRUE if the script context is Vim9 script.
322 */
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{
541 isn_T *isn = generate_instr_drop(cctx,
542 vartype == VAR_NUMBER ? ISN_OPNR
543 : vartype == VAR_LIST ? ISN_ADDLIST
544 : vartype == VAR_BLOB ? ISN_ADDBLOB
545#ifdef FEAT_FLOAT
546 : vartype == VAR_FLOAT ? ISN_OPFLOAT
547#endif
548 : ISN_OPANY, 1);
549
550 if (vartype != VAR_LIST && vartype != VAR_BLOB
551 && type1->tt_type != VAR_ANY
552 && type2->tt_type != VAR_ANY
553 && check_number_or_float(
554 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
555 return FAIL;
556
557 if (isn != NULL)
558 isn->isn_arg.op.op_type = EXPR_ADD;
559 return isn == NULL ? FAIL : OK;
560}
561
562/*
563 * Get the type to use for an instruction for an operation on "type1" and
564 * "type2". If they are matching use a type-specific instruction. Otherwise
565 * fall back to runtime type checking.
566 */
567 static vartype_T
568operator_type(type_T *type1, type_T *type2)
569{
570 if (type1->tt_type == type2->tt_type
571 && (type1->tt_type == VAR_NUMBER
572 || type1->tt_type == VAR_LIST
573#ifdef FEAT_FLOAT
574 || type1->tt_type == VAR_FLOAT
575#endif
576 || type1->tt_type == VAR_BLOB))
577 return type1->tt_type;
578 return VAR_ANY;
579}
580
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100581/*
582 * Generate an instruction with two arguments. The instruction depends on the
583 * type of the arguments.
584 */
585 static int
586generate_two_op(cctx_T *cctx, char_u *op)
587{
588 garray_T *stack = &cctx->ctx_type_stack;
589 type_T *type1;
590 type_T *type2;
591 vartype_T vartype;
592 isn_T *isn;
593
Bram Moolenaar080457c2020-03-03 21:53:32 +0100594 RETURN_OK_IF_SKIP(cctx);
595
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200596 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100597 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
598 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200599 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100600
601 switch (*op)
602 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200603 case '+':
604 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100605 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100606 break;
607
608 case '-':
609 case '*':
610 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
611 op) == FAIL)
612 return FAIL;
613 if (vartype == VAR_NUMBER)
614 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
615#ifdef FEAT_FLOAT
616 else if (vartype == VAR_FLOAT)
617 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
618#endif
619 else
620 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
621 if (isn != NULL)
622 isn->isn_arg.op.op_type = *op == '*'
623 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
624 break;
625
Bram Moolenaar4c683752020-04-05 21:38:23 +0200626 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100627 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200628 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100629 && type2->tt_type != VAR_NUMBER))
630 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200631 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100632 return FAIL;
633 }
634 isn = generate_instr_drop(cctx,
635 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
636 if (isn != NULL)
637 isn->isn_arg.op.op_type = EXPR_REM;
638 break;
639 }
640
641 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200642 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100643 {
644 type_T *type = &t_any;
645
646#ifdef FEAT_FLOAT
647 // float+number and number+float results in float
648 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
649 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
650 type = &t_float;
651#endif
652 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
653 }
654
655 return OK;
656}
657
658/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200659 * Get the instruction to use for comparing "type1" with "type2"
660 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100661 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200662 static isntype_T
663get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100664{
665 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100666
Bram Moolenaar4c683752020-04-05 21:38:23 +0200667 if (type1 == VAR_UNKNOWN)
668 type1 = VAR_ANY;
669 if (type2 == VAR_UNKNOWN)
670 type2 = VAR_ANY;
671
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100672 if (type1 == type2)
673 {
674 switch (type1)
675 {
676 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
677 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
678 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
679 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
680 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
681 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
682 case VAR_LIST: isntype = ISN_COMPARELIST; break;
683 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
684 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100685 default: isntype = ISN_COMPAREANY; break;
686 }
687 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200688 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100689 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
690 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
691 isntype = ISN_COMPAREANY;
692
693 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
694 && (isntype == ISN_COMPAREBOOL
695 || isntype == ISN_COMPARESPECIAL
696 || isntype == ISN_COMPARENR
697 || isntype == ISN_COMPAREFLOAT))
698 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200699 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100700 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200701 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100702 }
703 if (isntype == ISN_DROP
704 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
705 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
706 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
707 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
708 && exptype != EXPR_IS && exptype != EXPR_ISNOT
709 && (type1 == VAR_BLOB || type2 == VAR_BLOB
710 || type1 == VAR_LIST || type2 == VAR_LIST))))
711 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200712 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100713 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200714 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100715 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200716 return isntype;
717}
718
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200719 int
720check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
721{
722 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
723 return FAIL;
724 return OK;
725}
726
Bram Moolenaara5565e42020-05-09 15:44:01 +0200727/*
728 * Generate an ISN_COMPARE* instruction with a boolean result.
729 */
730 static int
731generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
732{
733 isntype_T isntype;
734 isn_T *isn;
735 garray_T *stack = &cctx->ctx_type_stack;
736 vartype_T type1;
737 vartype_T type2;
738
739 RETURN_OK_IF_SKIP(cctx);
740
741 // Get the known type of the two items on the stack. If they are matching
742 // use a type-specific instruction. Otherwise fall back to runtime type
743 // checking.
744 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
745 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
746 isntype = get_compare_isn(exptype, type1, type2);
747 if (isntype == ISN_DROP)
748 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749
750 if ((isn = generate_instr(cctx, isntype)) == NULL)
751 return FAIL;
752 isn->isn_arg.op.op_type = exptype;
753 isn->isn_arg.op.op_ic = ic;
754
755 // takes two arguments, puts one bool back
756 if (stack->ga_len >= 2)
757 {
758 --stack->ga_len;
759 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
760 }
761
762 return OK;
763}
764
765/*
766 * Generate an ISN_2BOOL instruction.
767 */
768 static int
769generate_2BOOL(cctx_T *cctx, int invert)
770{
771 isn_T *isn;
772 garray_T *stack = &cctx->ctx_type_stack;
773
Bram Moolenaar080457c2020-03-03 21:53:32 +0100774 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
776 return FAIL;
777 isn->isn_arg.number = invert;
778
779 // type becomes bool
780 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
781
782 return OK;
783}
784
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200785/*
786 * Generate an ISN_COND2BOOL instruction.
787 */
788 static int
789generate_COND2BOOL(cctx_T *cctx)
790{
791 isn_T *isn;
792 garray_T *stack = &cctx->ctx_type_stack;
793
794 RETURN_OK_IF_SKIP(cctx);
795 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
796 return FAIL;
797
798 // type becomes bool
799 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
800
801 return OK;
802}
803
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100804 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200805generate_TYPECHECK(
806 cctx_T *cctx,
807 type_T *expected,
808 int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100809{
810 isn_T *isn;
811 garray_T *stack = &cctx->ctx_type_stack;
812
Bram Moolenaar080457c2020-03-03 21:53:32 +0100813 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100814 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
815 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200816 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817 isn->isn_arg.type.ct_off = offset;
818
Bram Moolenaar5e654232020-09-16 15:22:00 +0200819 // type becomes expected
820 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100821
822 return OK;
823}
824
825/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200826 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
827 * used. Return FALSE if the types will never match.
828 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100829 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200830use_typecheck(type_T *actual, type_T *expected)
831{
832 if (actual->tt_type == VAR_ANY
833 || actual->tt_type == VAR_UNKNOWN
834 || (actual->tt_type == VAR_FUNC
835 && (expected->tt_type == VAR_FUNC
836 || expected->tt_type == VAR_PARTIAL)
837 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
838 return TRUE;
839 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
840 && actual->tt_type == expected->tt_type)
841 // This takes care of a nested list or dict.
842 return use_typecheck(actual->tt_member, expected->tt_member);
843 return FALSE;
844}
845
846/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200847 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200848 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200849 * - "actual" is a type that can be "expected" type: add a runtime check; or
850 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200851 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
852 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200853 */
854 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200855need_type(
856 type_T *actual,
857 type_T *expected,
858 int offset,
859 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200860 int silent,
861 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200862{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200863 if (expected == &t_bool && actual != &t_bool
864 && (actual->tt_flags & TTFLAG_BOOL_OK))
865 {
866 // Using "0", "1" or the result of an expression with "&&" or "||" as a
867 // boolean is OK but requires a conversion.
868 generate_2BOOL(cctx, FALSE);
869 return OK;
870 }
871
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200872 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200873 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200874
875 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200876 // If it's a constant a runtime check makes no sense.
877 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200878 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200879 generate_TYPECHECK(cctx, expected, offset);
880 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200881 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200882
883 if (!silent)
884 type_mismatch(expected, actual);
885 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200886}
887
888/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100889 * Check that the top of the type stack has a type that can be used as a
890 * condition. Give an error and return FAIL if not.
891 */
892 static int
893bool_on_stack(cctx_T *cctx)
894{
895 garray_T *stack = &cctx->ctx_type_stack;
896 type_T *type;
897
898 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
899 if (type == &t_bool)
900 return OK;
901
902 if (type == &t_any || type == &t_number)
903 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
904 // This requires a runtime type check.
905 return generate_COND2BOOL(cctx);
906
907 return need_type(type, &t_bool, -1, cctx, FALSE, FALSE);
908}
909
910/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100911 * Generate an ISN_PUSHNR instruction.
912 */
913 static int
914generate_PUSHNR(cctx_T *cctx, varnumber_T number)
915{
916 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200917 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100918
Bram Moolenaar080457c2020-03-03 21:53:32 +0100919 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100920 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
921 return FAIL;
922 isn->isn_arg.number = number;
923
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200924 if (number == 0 || number == 1)
925 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200926 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200927
928 // A 0 or 1 number can also be used as a bool.
929 if (type != NULL)
930 {
931 type->tt_type = VAR_NUMBER;
932 type->tt_flags = TTFLAG_BOOL_OK;
933 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
934 }
935 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936 return OK;
937}
938
939/*
940 * Generate an ISN_PUSHBOOL instruction.
941 */
942 static int
943generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
944{
945 isn_T *isn;
946
Bram Moolenaar080457c2020-03-03 21:53:32 +0100947 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100948 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
949 return FAIL;
950 isn->isn_arg.number = number;
951
952 return OK;
953}
954
955/*
956 * Generate an ISN_PUSHSPEC instruction.
957 */
958 static int
959generate_PUSHSPEC(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_PUSHSPEC, &t_special)) == NULL)
965 return FAIL;
966 isn->isn_arg.number = number;
967
968 return OK;
969}
970
971#ifdef FEAT_FLOAT
972/*
973 * Generate an ISN_PUSHF instruction.
974 */
975 static int
976generate_PUSHF(cctx_T *cctx, float_T fnumber)
977{
978 isn_T *isn;
979
Bram Moolenaar080457c2020-03-03 21:53:32 +0100980 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
982 return FAIL;
983 isn->isn_arg.fnumber = fnumber;
984
985 return OK;
986}
987#endif
988
989/*
990 * Generate an ISN_PUSHS instruction.
991 * Consumes "str".
992 */
993 static int
994generate_PUSHS(cctx_T *cctx, char_u *str)
995{
996 isn_T *isn;
997
Bram Moolenaar080457c2020-03-03 21:53:32 +0100998 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100999 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1000 return FAIL;
1001 isn->isn_arg.string = str;
1002
1003 return OK;
1004}
1005
1006/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001007 * Generate an ISN_PUSHCHANNEL instruction.
1008 * Consumes "channel".
1009 */
1010 static int
1011generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1012{
1013 isn_T *isn;
1014
Bram Moolenaar080457c2020-03-03 21:53:32 +01001015 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001016 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1017 return FAIL;
1018 isn->isn_arg.channel = channel;
1019
1020 return OK;
1021}
1022
1023/*
1024 * Generate an ISN_PUSHJOB instruction.
1025 * Consumes "job".
1026 */
1027 static int
1028generate_PUSHJOB(cctx_T *cctx, job_T *job)
1029{
1030 isn_T *isn;
1031
Bram Moolenaar080457c2020-03-03 21:53:32 +01001032 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001033 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001034 return FAIL;
1035 isn->isn_arg.job = job;
1036
1037 return OK;
1038}
1039
1040/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001041 * Generate an ISN_PUSHBLOB instruction.
1042 * Consumes "blob".
1043 */
1044 static int
1045generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1046{
1047 isn_T *isn;
1048
Bram Moolenaar080457c2020-03-03 21:53:32 +01001049 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1051 return FAIL;
1052 isn->isn_arg.blob = blob;
1053
1054 return OK;
1055}
1056
1057/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001058 * Generate an ISN_PUSHFUNC instruction with name "name".
1059 * Consumes "name".
1060 */
1061 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001062generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001063{
1064 isn_T *isn;
1065
Bram Moolenaar080457c2020-03-03 21:53:32 +01001066 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001067 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001068 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001069 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001070
1071 return OK;
1072}
1073
1074/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001075 * Generate an ISN_GETITEM instruction with "index".
1076 */
1077 static int
1078generate_GETITEM(cctx_T *cctx, int index)
1079{
1080 isn_T *isn;
1081 garray_T *stack = &cctx->ctx_type_stack;
1082 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1083 type_T *item_type = &t_any;
1084
1085 RETURN_OK_IF_SKIP(cctx);
1086
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001087 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001088 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001089 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001090 emsg(_(e_listreq));
1091 return FAIL;
1092 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001093 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001094 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1095 return FAIL;
1096 isn->isn_arg.number = index;
1097
1098 // add the item type to the type stack
1099 if (ga_grow(stack, 1) == FAIL)
1100 return FAIL;
1101 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1102 ++stack->ga_len;
1103 return OK;
1104}
1105
1106/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001107 * Generate an ISN_SLICE instruction with "count".
1108 */
1109 static int
1110generate_SLICE(cctx_T *cctx, int count)
1111{
1112 isn_T *isn;
1113
1114 RETURN_OK_IF_SKIP(cctx);
1115 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1116 return FAIL;
1117 isn->isn_arg.number = count;
1118 return OK;
1119}
1120
1121/*
1122 * Generate an ISN_CHECKLEN instruction with "min_len".
1123 */
1124 static int
1125generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1126{
1127 isn_T *isn;
1128
1129 RETURN_OK_IF_SKIP(cctx);
1130
1131 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1132 return FAIL;
1133 isn->isn_arg.checklen.cl_min_len = min_len;
1134 isn->isn_arg.checklen.cl_more_OK = more_OK;
1135
1136 return OK;
1137}
1138
1139/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001140 * Generate an ISN_STORE instruction.
1141 */
1142 static int
1143generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1144{
1145 isn_T *isn;
1146
Bram Moolenaar080457c2020-03-03 21:53:32 +01001147 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001148 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1149 return FAIL;
1150 if (name != NULL)
1151 isn->isn_arg.string = vim_strsave(name);
1152 else
1153 isn->isn_arg.number = idx;
1154
1155 return OK;
1156}
1157
1158/*
1159 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1160 */
1161 static int
1162generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1163{
1164 isn_T *isn;
1165
Bram Moolenaar080457c2020-03-03 21:53:32 +01001166 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001167 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1168 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001169 isn->isn_arg.storenr.stnr_idx = idx;
1170 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171
1172 return OK;
1173}
1174
1175/*
1176 * Generate an ISN_STOREOPT instruction
1177 */
1178 static int
1179generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1180{
1181 isn_T *isn;
1182
Bram Moolenaar080457c2020-03-03 21:53:32 +01001183 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001184 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001185 return FAIL;
1186 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1187 isn->isn_arg.storeopt.so_flags = opt_flags;
1188
1189 return OK;
1190}
1191
1192/*
1193 * Generate an ISN_LOAD or similar instruction.
1194 */
1195 static int
1196generate_LOAD(
1197 cctx_T *cctx,
1198 isntype_T isn_type,
1199 int idx,
1200 char_u *name,
1201 type_T *type)
1202{
1203 isn_T *isn;
1204
Bram Moolenaar080457c2020-03-03 21:53:32 +01001205 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1207 return FAIL;
1208 if (name != NULL)
1209 isn->isn_arg.string = vim_strsave(name);
1210 else
1211 isn->isn_arg.number = idx;
1212
1213 return OK;
1214}
1215
1216/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001217 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001218 */
1219 static int
1220generate_LOADV(
1221 cctx_T *cctx,
1222 char_u *name,
1223 int error)
1224{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001225 int di_flags;
1226 int vidx = find_vim_var(name, &di_flags);
1227 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001228
Bram Moolenaar080457c2020-03-03 21:53:32 +01001229 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001230 if (vidx < 0)
1231 {
1232 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001233 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001234 return FAIL;
1235 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001236 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001237
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001238 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001239}
1240
1241/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001242 * Generate an ISN_UNLET instruction.
1243 */
1244 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001245generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001246{
1247 isn_T *isn;
1248
1249 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001250 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001251 return FAIL;
1252 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1253 isn->isn_arg.unlet.ul_forceit = forceit;
1254
1255 return OK;
1256}
1257
1258/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001259 * Generate an ISN_LOCKCONST instruction.
1260 */
1261 static int
1262generate_LOCKCONST(cctx_T *cctx)
1263{
1264 isn_T *isn;
1265
1266 RETURN_OK_IF_SKIP(cctx);
1267 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1268 return FAIL;
1269 return OK;
1270}
1271
1272/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 * Generate an ISN_LOADS instruction.
1274 */
1275 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001276generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001278 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001279 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001280 int sid,
1281 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001282{
1283 isn_T *isn;
1284
Bram Moolenaar080457c2020-03-03 21:53:32 +01001285 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001286 if (isn_type == ISN_LOADS)
1287 isn = generate_instr_type(cctx, isn_type, type);
1288 else
1289 isn = generate_instr_drop(cctx, isn_type, 1);
1290 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001292 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1293 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001294
1295 return OK;
1296}
1297
1298/*
1299 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1300 */
1301 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001302generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303 cctx_T *cctx,
1304 isntype_T isn_type,
1305 int sid,
1306 int idx,
1307 type_T *type)
1308{
1309 isn_T *isn;
1310
Bram Moolenaar080457c2020-03-03 21:53:32 +01001311 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312 if (isn_type == ISN_LOADSCRIPT)
1313 isn = generate_instr_type(cctx, isn_type, type);
1314 else
1315 isn = generate_instr_drop(cctx, isn_type, 1);
1316 if (isn == NULL)
1317 return FAIL;
1318 isn->isn_arg.script.script_sid = sid;
1319 isn->isn_arg.script.script_idx = idx;
1320 return OK;
1321}
1322
1323/*
1324 * Generate an ISN_NEWLIST instruction.
1325 */
1326 static int
1327generate_NEWLIST(cctx_T *cctx, int count)
1328{
1329 isn_T *isn;
1330 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331 type_T *type;
1332 type_T *member;
1333
Bram Moolenaar080457c2020-03-03 21:53:32 +01001334 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001335 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1336 return FAIL;
1337 isn->isn_arg.number = count;
1338
Bram Moolenaar127542b2020-08-09 17:22:04 +02001339 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001340 if (count == 0)
1341 member = &t_void;
1342 else
1343 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001344 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1345 cctx->ctx_type_list);
1346 type = get_list_type(member, cctx->ctx_type_list);
1347
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001348 // drop the value types
1349 stack->ga_len -= count;
1350
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001351 // add the list type to the type stack
1352 if (ga_grow(stack, 1) == FAIL)
1353 return FAIL;
1354 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1355 ++stack->ga_len;
1356
1357 return OK;
1358}
1359
1360/*
1361 * Generate an ISN_NEWDICT instruction.
1362 */
1363 static int
1364generate_NEWDICT(cctx_T *cctx, int count)
1365{
1366 isn_T *isn;
1367 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001368 type_T *type;
1369 type_T *member;
1370
Bram Moolenaar080457c2020-03-03 21:53:32 +01001371 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001372 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1373 return FAIL;
1374 isn->isn_arg.number = count;
1375
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001376 if (count == 0)
1377 member = &t_void;
1378 else
1379 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001380 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1381 cctx->ctx_type_list);
1382 type = get_dict_type(member, cctx->ctx_type_list);
1383
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001384 // drop the key and value types
1385 stack->ga_len -= 2 * count;
1386
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 // add the dict type to the type stack
1388 if (ga_grow(stack, 1) == FAIL)
1389 return FAIL;
1390 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1391 ++stack->ga_len;
1392
1393 return OK;
1394}
1395
1396/*
1397 * Generate an ISN_FUNCREF instruction.
1398 */
1399 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001400generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001401{
1402 isn_T *isn;
1403 garray_T *stack = &cctx->ctx_type_stack;
1404
Bram Moolenaar080457c2020-03-03 21:53:32 +01001405 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1407 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001408 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001409 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001410
1411 if (ga_grow(stack, 1) == FAIL)
1412 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001413 ((type_T **)stack->ga_data)[stack->ga_len] =
1414 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001415 ++stack->ga_len;
1416
1417 return OK;
1418}
1419
1420/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001421 * Generate an ISN_NEWFUNC instruction.
1422 */
1423 static int
1424generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1425{
1426 isn_T *isn;
1427 char_u *name;
1428
1429 RETURN_OK_IF_SKIP(cctx);
1430 name = vim_strsave(lambda_name);
1431 if (name == NULL)
1432 return FAIL;
1433 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1434 return FAIL;
1435 isn->isn_arg.newfunc.nf_lambda = name;
1436 isn->isn_arg.newfunc.nf_global = func_name;
1437
1438 return OK;
1439}
1440
1441/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001442 * Generate an ISN_DEF instruction: list functions
1443 */
1444 static int
1445generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1446{
1447 isn_T *isn;
1448
1449 RETURN_OK_IF_SKIP(cctx);
1450 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1451 return FAIL;
1452 if (len > 0)
1453 {
1454 isn->isn_arg.string = vim_strnsave(name, len);
1455 if (isn->isn_arg.string == NULL)
1456 return FAIL;
1457 }
1458 return OK;
1459}
1460
1461/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001462 * Generate an ISN_JUMP instruction.
1463 */
1464 static int
1465generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1466{
1467 isn_T *isn;
1468 garray_T *stack = &cctx->ctx_type_stack;
1469
Bram Moolenaar080457c2020-03-03 21:53:32 +01001470 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1472 return FAIL;
1473 isn->isn_arg.jump.jump_when = when;
1474 isn->isn_arg.jump.jump_where = where;
1475
1476 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1477 --stack->ga_len;
1478
1479 return OK;
1480}
1481
1482 static int
1483generate_FOR(cctx_T *cctx, int loop_idx)
1484{
1485 isn_T *isn;
1486 garray_T *stack = &cctx->ctx_type_stack;
1487
Bram Moolenaar080457c2020-03-03 21:53:32 +01001488 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001489 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1490 return FAIL;
1491 isn->isn_arg.forloop.for_idx = loop_idx;
1492
1493 if (ga_grow(stack, 1) == FAIL)
1494 return FAIL;
1495 // type doesn't matter, will be stored next
1496 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1497 ++stack->ga_len;
1498
1499 return OK;
1500}
1501
1502/*
1503 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001504 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001505 * Return FAIL if the number of arguments is wrong.
1506 */
1507 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001508generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001509{
1510 isn_T *isn;
1511 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001512 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001513 type_T **argtypes = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001514
Bram Moolenaar080457c2020-03-03 21:53:32 +01001515 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001516 argoff = check_internal_func(func_idx, argcount);
1517 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518 return FAIL;
1519
Bram Moolenaar389df252020-07-09 21:20:47 +02001520 if (method_call && argoff > 1)
1521 {
1522 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1523 return FAIL;
1524 isn->isn_arg.shuffle.shfl_item = argcount;
1525 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1526 }
1527
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001528 if (argcount > 0)
1529 {
1530 // Check the types of the arguments.
1531 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
1532 if (internal_func_check_arg_types(argtypes, func_idx, argcount) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001533 return FAIL;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001534 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001535
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1537 return FAIL;
1538 isn->isn_arg.bfunc.cbf_idx = func_idx;
1539 isn->isn_arg.bfunc.cbf_argcount = argcount;
1540
Bram Moolenaar94738d82020-10-21 14:25:07 +02001541 // Drop the argument types and push the return type.
1542 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001543 if (ga_grow(stack, 1) == FAIL)
1544 return FAIL;
1545 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001546 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001547 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001548
1549 return OK;
1550}
1551
1552/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001553 * Generate an ISN_LISTAPPEND instruction. Works like add().
1554 * Argument count is already checked.
1555 */
1556 static int
1557generate_LISTAPPEND(cctx_T *cctx)
1558{
1559 garray_T *stack = &cctx->ctx_type_stack;
1560 type_T *list_type;
1561 type_T *item_type;
1562 type_T *expected;
1563
1564 // Caller already checked that list_type is a list.
1565 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1566 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1567 expected = list_type->tt_member;
1568 if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL)
1569 return FAIL;
1570
1571 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1572 return FAIL;
1573
1574 --stack->ga_len; // drop the argument
1575 return OK;
1576}
1577
1578/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001579 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1580 * Argument count is already checked.
1581 */
1582 static int
1583generate_BLOBAPPEND(cctx_T *cctx)
1584{
1585 garray_T *stack = &cctx->ctx_type_stack;
1586 type_T *item_type;
1587
1588 // Caller already checked that blob_type is a blob.
1589 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1590 if (need_type(item_type, &t_number, -1, cctx, FALSE, FALSE) == FAIL)
1591 return FAIL;
1592
1593 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1594 return FAIL;
1595
1596 --stack->ga_len; // drop the argument
1597 return OK;
1598}
1599
1600/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601 * Generate an ISN_DCALL or ISN_UCALL instruction.
1602 * Return FAIL if the number of arguments is wrong.
1603 */
1604 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001605generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606{
1607 isn_T *isn;
1608 garray_T *stack = &cctx->ctx_type_stack;
1609 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001610 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611
Bram Moolenaar080457c2020-03-03 21:53:32 +01001612 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 if (argcount > regular_args && !has_varargs(ufunc))
1614 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001615 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001616 return FAIL;
1617 }
1618 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1619 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001620 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621 return FAIL;
1622 }
1623
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001624 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001625 {
1626 int i;
1627
1628 for (i = 0; i < argcount; ++i)
1629 {
1630 type_T *expected;
1631 type_T *actual;
1632
1633 if (i < regular_args)
1634 {
1635 if (ufunc->uf_arg_types == NULL)
1636 continue;
1637 expected = ufunc->uf_arg_types[i];
1638 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001639 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1640 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001641 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001642 else
1643 expected = ufunc->uf_va_type->tt_member;
1644 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001645 if (need_type(actual, expected, -argcount + i, cctx,
1646 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001647 {
1648 arg_type_mismatch(expected, actual, i + 1);
1649 return FAIL;
1650 }
1651 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001652 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001653 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1654 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001655 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001656 }
1657
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001659 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001660 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001661 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001662 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001663 {
1664 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1665 isn->isn_arg.dfunc.cdf_argcount = argcount;
1666 }
1667 else
1668 {
1669 // A user function may be deleted and redefined later, can't use the
1670 // ufunc pointer, need to look it up again at runtime.
1671 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1672 isn->isn_arg.ufunc.cuf_argcount = argcount;
1673 }
1674
1675 stack->ga_len -= argcount; // drop the arguments
1676 if (ga_grow(stack, 1) == FAIL)
1677 return FAIL;
1678 // add return value
1679 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1680 ++stack->ga_len;
1681
1682 return OK;
1683}
1684
1685/*
1686 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1687 */
1688 static int
1689generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1690{
1691 isn_T *isn;
1692 garray_T *stack = &cctx->ctx_type_stack;
1693
Bram Moolenaar080457c2020-03-03 21:53:32 +01001694 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1696 return FAIL;
1697 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1698 isn->isn_arg.ufunc.cuf_argcount = argcount;
1699
1700 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001701 if (ga_grow(stack, 1) == FAIL)
1702 return FAIL;
1703 // add return value
1704 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1705 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001706
1707 return OK;
1708}
1709
1710/*
1711 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001712 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001713 */
1714 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001715generate_PCALL(
1716 cctx_T *cctx,
1717 int argcount,
1718 char_u *name,
1719 type_T *type,
1720 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001721{
1722 isn_T *isn;
1723 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001724 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001725
Bram Moolenaar080457c2020-03-03 21:53:32 +01001726 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001727
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001728 if (type->tt_type == VAR_ANY)
1729 ret_type = &t_any;
1730 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001731 {
1732 if (type->tt_argcount != -1)
1733 {
1734 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1735
1736 if (argcount < type->tt_min_argcount - varargs)
1737 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001738 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001739 return FAIL;
1740 }
1741 if (!varargs && argcount > type->tt_argcount)
1742 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001743 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001744 return FAIL;
1745 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001746 if (type->tt_args != NULL)
1747 {
1748 int i;
1749
1750 for (i = 0; i < argcount; ++i)
1751 {
1752 int offset = -argcount + i - 1;
1753 type_T *actual = ((type_T **)stack->ga_data)[
1754 stack->ga_len + offset];
1755 type_T *expected;
1756
1757 if (varargs && i >= type->tt_min_argcount - 1)
1758 expected = type->tt_args[
1759 type->tt_min_argcount - 1]->tt_member;
1760 else
1761 expected = type->tt_args[i];
1762 if (need_type(actual, expected, offset,
1763 cctx, TRUE, FALSE) == FAIL)
1764 {
1765 arg_type_mismatch(expected, actual, i + 1);
1766 return FAIL;
1767 }
1768 }
1769 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001770 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001771 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001772 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001773 else
1774 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001775 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001776 return FAIL;
1777 }
1778
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001779 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1780 return FAIL;
1781 isn->isn_arg.pfunc.cpf_top = at_top;
1782 isn->isn_arg.pfunc.cpf_argcount = argcount;
1783
1784 stack->ga_len -= argcount; // drop the arguments
1785
1786 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001787 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001789 // If partial is above the arguments it must be cleared and replaced with
1790 // the return value.
1791 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1792 return FAIL;
1793
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001794 return OK;
1795}
1796
1797/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001798 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001799 */
1800 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001801generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802{
1803 isn_T *isn;
1804 garray_T *stack = &cctx->ctx_type_stack;
1805 type_T *type;
1806
Bram Moolenaar080457c2020-03-03 21:53:32 +01001807 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001808 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001810 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001811
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001812 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001813 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001814 if (type->tt_type != VAR_DICT && type != &t_any)
1815 {
1816 emsg(_(e_dictreq));
1817 return FAIL;
1818 }
1819 // change dict type to dict member type
1820 if (type->tt_type == VAR_DICT)
1821 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822
1823 return OK;
1824}
1825
1826/*
1827 * Generate an ISN_ECHO instruction.
1828 */
1829 static int
1830generate_ECHO(cctx_T *cctx, int with_white, int count)
1831{
1832 isn_T *isn;
1833
Bram Moolenaar080457c2020-03-03 21:53:32 +01001834 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1836 return FAIL;
1837 isn->isn_arg.echo.echo_with_white = with_white;
1838 isn->isn_arg.echo.echo_count = count;
1839
1840 return OK;
1841}
1842
Bram Moolenaarad39c092020-02-26 18:23:43 +01001843/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001844 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001845 */
1846 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001847generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001848{
1849 isn_T *isn;
1850
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001851 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001852 return FAIL;
1853 isn->isn_arg.number = count;
1854
1855 return OK;
1856}
1857
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001858/*
1859 * Generate an ISN_PUT instruction.
1860 */
1861 static int
1862generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1863{
1864 isn_T *isn;
1865
1866 RETURN_OK_IF_SKIP(cctx);
1867 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1868 return FAIL;
1869 isn->isn_arg.put.put_regname = regname;
1870 isn->isn_arg.put.put_lnum = lnum;
1871 return OK;
1872}
1873
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874 static int
1875generate_EXEC(cctx_T *cctx, char_u *line)
1876{
1877 isn_T *isn;
1878
Bram Moolenaar080457c2020-03-03 21:53:32 +01001879 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001880 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1881 return FAIL;
1882 isn->isn_arg.string = vim_strsave(line);
1883 return OK;
1884}
1885
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001886 static int
1887generate_EXECCONCAT(cctx_T *cctx, int count)
1888{
1889 isn_T *isn;
1890
1891 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1892 return FAIL;
1893 isn->isn_arg.number = count;
1894 return OK;
1895}
1896
Bram Moolenaar08597872020-12-10 19:43:40 +01001897/*
1898 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1899 */
1900 static int
1901generate_RANGE(cctx_T *cctx, char_u *range)
1902{
1903 isn_T *isn;
1904 garray_T *stack = &cctx->ctx_type_stack;
1905
1906 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1907 return FAIL;
1908 isn->isn_arg.string = range;
1909
1910 if (ga_grow(stack, 1) == FAIL)
1911 return FAIL;
1912 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
1913 ++stack->ga_len;
1914 return OK;
1915}
1916
Bram Moolenaar792f7862020-11-23 08:31:18 +01001917 static int
1918generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
1919{
1920 isn_T *isn;
1921
1922 RETURN_OK_IF_SKIP(cctx);
1923 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
1924 return FAIL;
1925 isn->isn_arg.unpack.unp_count = var_count;
1926 isn->isn_arg.unpack.unp_semicolon = semicolon;
1927 return OK;
1928}
1929
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001930/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02001931 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001932 */
1933 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02001934generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001935{
1936 isn_T *isn;
1937
Bram Moolenaar02194d22020-10-24 23:08:38 +02001938 if (cmod->cmod_flags != 0
1939 || cmod->cmod_split != 0
1940 || cmod->cmod_verbose != 0
1941 || cmod->cmod_tab != 0
1942 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001943 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02001944 cctx->ctx_has_cmdmod = TRUE;
1945
1946 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001947 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001948 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1949 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1950 return FAIL;
1951 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
1952 // filter progam now belongs to the instruction
1953 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001954 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02001955
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001956 return OK;
1957}
1958
1959 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02001960generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001961{
Bram Moolenaarf665e972020-12-05 19:17:16 +01001962 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
1963 return FAIL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001964 return OK;
1965}
1966
1967/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001969 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001970 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001971 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02001972reserve_local(
1973 cctx_T *cctx,
1974 char_u *name,
1975 size_t len,
1976 int isConst,
1977 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979 lvar_T *lvar;
1980
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001981 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001982 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001983 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001984 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985 }
1986
1987 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001988 return NULL;
1989 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02001990 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001991
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001992 // Every local variable uses the next entry on the stack. We could re-use
1993 // the last ones when leaving a scope, but then variables used in a closure
1994 // might get overwritten. To keep things simple do not re-use stack
1995 // entries. This is less efficient, but memory is cheap these days.
1996 lvar->lv_idx = cctx->ctx_locals_count++;
1997
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001998 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001999 lvar->lv_const = isConst;
2000 lvar->lv_type = type;
2001
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002002 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003}
2004
2005/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002006 * Remove local variables above "new_top".
2007 */
2008 static void
2009unwind_locals(cctx_T *cctx, int new_top)
2010{
2011 if (cctx->ctx_locals.ga_len > new_top)
2012 {
2013 int idx;
2014 lvar_T *lvar;
2015
2016 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2017 {
2018 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2019 vim_free(lvar->lv_name);
2020 }
2021 }
2022 cctx->ctx_locals.ga_len = new_top;
2023}
2024
2025/*
2026 * Free all local variables.
2027 */
2028 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002029free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002030{
2031 unwind_locals(cctx, 0);
2032 ga_clear(&cctx->ctx_locals);
2033}
2034
2035/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002036 * Find "name" in script-local items of script "sid".
2037 * Returns the index in "sn_var_vals" if found.
2038 * If found but not in "sn_var_vals" returns -1.
2039 * If not found returns -2.
2040 */
2041 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002042get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002043{
2044 hashtab_T *ht;
2045 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002046 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002047 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048 int idx;
2049
Bram Moolenaare3d46852020-08-29 13:39:17 +02002050 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002051 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002052 if (sid == current_sctx.sc_sid)
2053 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002054 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002055
2056 if (sav == NULL)
2057 return -2;
2058 idx = sav->sav_var_vals_idx;
2059 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2060 if (check_writable && sv->sv_const)
2061 semsg(_(e_readonlyvar), name);
2062 return idx;
2063 }
2064
2065 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002066 ht = &SCRIPT_VARS(sid);
2067 di = find_var_in_ht(ht, 0, name, TRUE);
2068 if (di == NULL)
2069 return -2;
2070
2071 // Now find the svar_T index in sn_var_vals.
2072 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2073 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002074 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002075 if (sv->sv_tv == &di->di_tv)
2076 {
2077 if (check_writable && sv->sv_const)
2078 semsg(_(e_readonlyvar), name);
2079 return idx;
2080 }
2081 }
2082 return -1;
2083}
2084
2085/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002086 * Find "name" in imported items of the current script or in "cctx" if not
2087 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002088 */
2089 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002090find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002091{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002092 int idx;
2093
Bram Moolenaare3d46852020-08-29 13:39:17 +02002094 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002095 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002096 if (cctx != NULL)
2097 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2098 {
2099 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2100 + idx;
2101
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002102 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2103 : STRLEN(import->imp_name) == len
2104 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002105 return import;
2106 }
2107
Bram Moolenaarefa94442020-08-08 22:16:00 +02002108 return find_imported_in_script(name, len, current_sctx.sc_sid);
2109}
2110
2111 imported_T *
2112find_imported_in_script(char_u *name, size_t len, int sid)
2113{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002114 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002115 int idx;
2116
Bram Moolenaare3d46852020-08-29 13:39:17 +02002117 if (!SCRIPT_ID_VALID(sid))
2118 return NULL;
2119 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002120 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2121 {
2122 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2123
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002124 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2125 : STRLEN(import->imp_name) == len
2126 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002127 return import;
2128 }
2129 return NULL;
2130}
2131
2132/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002133 * Free all imported variables.
2134 */
2135 static void
2136free_imported(cctx_T *cctx)
2137{
2138 int idx;
2139
2140 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2141 {
2142 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2143
2144 vim_free(import->imp_name);
2145 }
2146 ga_clear(&cctx->ctx_imports);
2147}
2148
2149/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01002150 * Return TRUE if "p" points at a "#". Does not check for white space.
Bram Moolenaar23c55272020-06-21 16:58:13 +02002151 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002152 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002153vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002154{
Bram Moolenaare0de1712020-12-02 17:36:54 +01002155 return p[0] == '#';
Bram Moolenaar23c55272020-06-21 16:58:13 +02002156}
2157
2158/*
2159 * Return a pointer to the next line that isn't empty or only contains a
2160 * comment. Skips over white space.
2161 * Returns NULL if there is none.
2162 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002163 char_u *
2164peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002165{
2166 int lnum = cctx->ctx_lnum;
2167
2168 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2169 {
2170 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002171 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002172
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002173 // ignore NULLs inserted for continuation lines
2174 if (line != NULL)
2175 {
2176 p = skipwhite(line);
2177 if (*p != NUL && !vim9_comment_start(p))
2178 return p;
2179 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002180 }
2181 return NULL;
2182}
2183
2184/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002185 * Called when checking for a following operator at "arg". When the rest of
2186 * the line is empty or only a comment, peek the next line. If there is a next
2187 * line return a pointer to it and set "nextp".
2188 * Otherwise skip over white space.
2189 */
2190 static char_u *
2191may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2192{
2193 char_u *p = skipwhite(arg);
2194
2195 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002196 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002197 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002198 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002199 if (*nextp != NULL)
2200 return *nextp;
2201 }
2202 return p;
2203}
2204
2205/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002206 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002207 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002208 * Returns NULL when at the end.
2209 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002210 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002211next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002212{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002213 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002214
2215 do
2216 {
2217 ++cctx->ctx_lnum;
2218 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002219 {
2220 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002221 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002222 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002223 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002224 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002225 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002226 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002227 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002228 return line;
2229}
2230
2231/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002232 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002233 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002234 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2235 */
2236 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002237may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002238{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002239 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002240 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002241 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002242
2243 if (next == NULL)
2244 return FAIL;
2245 *arg = skipwhite(next);
2246 }
2247 return OK;
2248}
2249
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002250/*
2251 * Idem, and give an error when failed.
2252 */
2253 static int
2254may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2255{
2256 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2257 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002258 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002259 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002260 return FAIL;
2261 }
2262 return OK;
2263}
2264
2265
Bram Moolenaara5565e42020-05-09 15:44:01 +02002266// Structure passed between the compile_expr* functions to keep track of
2267// constants that have been parsed but for which no code was produced yet. If
2268// possible expressions on these constants are applied at compile time. If
2269// that is not possible, the code to push the constants needs to be generated
2270// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002271// Using 50 should be more than enough of 5 levels of ().
2272#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002273typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002274 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002275 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002276 int pp_is_const; // all generated code was constants, used for a
2277 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002278} ppconst_T;
2279
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002280static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002281static int compile_expr0(char_u **arg, cctx_T *cctx);
2282static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2283
Bram Moolenaara5565e42020-05-09 15:44:01 +02002284/*
2285 * Generate a PUSH instruction for "tv".
2286 * "tv" will be consumed or cleared.
2287 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2288 */
2289 static int
2290generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2291{
2292 if (tv != NULL)
2293 {
2294 switch (tv->v_type)
2295 {
2296 case VAR_UNKNOWN:
2297 break;
2298 case VAR_BOOL:
2299 generate_PUSHBOOL(cctx, tv->vval.v_number);
2300 break;
2301 case VAR_SPECIAL:
2302 generate_PUSHSPEC(cctx, tv->vval.v_number);
2303 break;
2304 case VAR_NUMBER:
2305 generate_PUSHNR(cctx, tv->vval.v_number);
2306 break;
2307#ifdef FEAT_FLOAT
2308 case VAR_FLOAT:
2309 generate_PUSHF(cctx, tv->vval.v_float);
2310 break;
2311#endif
2312 case VAR_BLOB:
2313 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2314 tv->vval.v_blob = NULL;
2315 break;
2316 case VAR_STRING:
2317 generate_PUSHS(cctx, tv->vval.v_string);
2318 tv->vval.v_string = NULL;
2319 break;
2320 default:
2321 iemsg("constant type not supported");
2322 clear_tv(tv);
2323 return FAIL;
2324 }
2325 tv->v_type = VAR_UNKNOWN;
2326 }
2327 return OK;
2328}
2329
2330/*
2331 * Generate code for any ppconst entries.
2332 */
2333 static int
2334generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2335{
2336 int i;
2337 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002338 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002339
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002340 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002341 for (i = 0; i < ppconst->pp_used; ++i)
2342 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2343 ret = FAIL;
2344 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002345 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002346 return ret;
2347}
2348
2349/*
2350 * Clear ppconst constants. Used when failing.
2351 */
2352 static void
2353clear_ppconst(ppconst_T *ppconst)
2354{
2355 int i;
2356
2357 for (i = 0; i < ppconst->pp_used; ++i)
2358 clear_tv(&ppconst->pp_tv[i]);
2359 ppconst->pp_used = 0;
2360}
2361
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002362/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002363 * Generate an instruction to load script-local variable "name", without the
2364 * leading "s:".
2365 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002366 */
2367 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002368compile_load_scriptvar(
2369 cctx_T *cctx,
2370 char_u *name, // variable NUL terminated
2371 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002372 char_u **end, // end of variable
2373 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002374{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002375 scriptitem_T *si;
2376 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002377 imported_T *import;
2378
Bram Moolenaare3d46852020-08-29 13:39:17 +02002379 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2380 return FAIL;
2381 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002382 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002383 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002385 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002386 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2387 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002388 }
2389 if (idx >= 0)
2390 {
2391 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2392
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002393 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394 current_sctx.sc_sid, idx, sv->sv_type);
2395 return OK;
2396 }
2397
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002398 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002399 if (import != NULL)
2400 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002401 if (import->imp_all)
2402 {
2403 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002404 char_u *exp_name;
2405 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002406 ufunc_T *ufunc;
2407 type_T *type;
2408
2409 // Used "import * as Name", need to lookup the member.
2410 if (*p != '.')
2411 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002412 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002413 return FAIL;
2414 }
2415 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002416 if (VIM_ISWHITE(*p))
2417 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002418 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002419 return FAIL;
2420 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002421
Bram Moolenaar1c991142020-07-04 13:15:31 +02002422 // isolate one name
2423 exp_name = p;
2424 while (eval_isnamec(*p))
2425 ++p;
2426 cc = *p;
2427 *p = NUL;
2428
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002429 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002430 *p = cc;
2431 p = skipwhite(p);
2432
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002433 // TODO: what if it is a function?
2434 if (idx < 0)
2435 return FAIL;
2436 *end = p;
2437
2438 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2439 import->imp_sid,
2440 idx,
2441 type);
2442 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002443 else if (import->imp_funcname != NULL)
2444 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002445 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002446 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2447 import->imp_sid,
2448 import->imp_var_vals_idx,
2449 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002450 return OK;
2451 }
2452
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002453 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002454 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002455 return FAIL;
2456}
2457
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002458 static int
2459generate_funcref(cctx_T *cctx, char_u *name)
2460{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002461 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002462
2463 if (ufunc == NULL)
2464 return FAIL;
2465
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002466 // Need to compile any default values to get the argument types.
2467 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2468 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2469 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002470 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002471}
2472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473/*
2474 * Compile a variable name into a load instruction.
2475 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002476 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002477 * When "error" is FALSE do not give an error when not found.
2478 */
2479 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002480compile_load(
2481 char_u **arg,
2482 char_u *end_arg,
2483 cctx_T *cctx,
2484 int is_expr,
2485 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486{
2487 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002488 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002489 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002491 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492
2493 if (*(*arg + 1) == ':')
2494 {
2495 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002496 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002497 {
2498 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002499
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002500 switch (**arg)
2501 {
2502 case 'g': isn_type = ISN_LOADGDICT; break;
2503 case 'w': isn_type = ISN_LOADWDICT; break;
2504 case 't': isn_type = ISN_LOADTDICT; break;
2505 case 'b': isn_type = ISN_LOADBDICT; break;
2506 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002507 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002508 goto theend;
2509 }
2510 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2511 goto theend;
2512 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002513 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 else
2515 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002516 isntype_T isn_type = ISN_DROP;
2517
2518 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2519 if (name == NULL)
2520 return FAIL;
2521
2522 switch (**arg)
2523 {
2524 case 'v': res = generate_LOADV(cctx, name, error);
2525 break;
2526 case 's': res = compile_load_scriptvar(cctx, name,
2527 NULL, NULL, error);
2528 break;
2529 case 'g': isn_type = ISN_LOADG; break;
2530 case 'w': isn_type = ISN_LOADW; break;
2531 case 't': isn_type = ISN_LOADT; break;
2532 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002533 default: // cannot happen, just in case
2534 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002535 goto theend;
2536 }
2537 if (isn_type != ISN_DROP)
2538 {
2539 // Global, Buffer-local, Window-local and Tabpage-local
2540 // variables can be defined later, thus we don't check if it
2541 // exists, give error at runtime.
2542 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2543 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 }
2545 }
2546 else
2547 {
2548 size_t len = end - *arg;
2549 int idx;
2550 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002551 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002552
2553 name = vim_strnsave(*arg, end - *arg);
2554 if (name == NULL)
2555 return FAIL;
2556
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002557 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002559 if (!gen_load_outer)
2560 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561 }
2562 else
2563 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002564 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002565
Bram Moolenaar709664c2020-12-12 14:33:41 +01002566 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002568 type = lvar.lv_type;
2569 idx = lvar.lv_idx;
2570 if (lvar.lv_from_outer)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002571 gen_load_outer = TRUE;
2572 else
2573 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574 }
2575 else
2576 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002577 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002578 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002579 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002580 || find_imported(name, 0, cctx) != NULL)
2581 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002582
Bram Moolenaar0f769812020-09-12 18:32:34 +02002583 // When evaluating an expression and the name starts with an
2584 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002585 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002586 if (res == FAIL && is_expr
2587 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002588 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 }
2590 }
2591 if (gen_load)
2592 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002593 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002594 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002595 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002596 cctx->ctx_outer_used = TRUE;
2597 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002598 }
2599
2600 *arg = end;
2601
2602theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002603 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002604 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605 vim_free(name);
2606 return res;
2607}
2608
2609/*
2610 * Compile the argument expressions.
2611 * "arg" points to just after the "(" and is advanced to after the ")"
2612 */
2613 static int
2614compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2615{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002616 char_u *p = *arg;
2617 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002618 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002619
Bram Moolenaare6085c52020-04-12 20:19:16 +02002620 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002622 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2623 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002624 if (*p == ')')
2625 {
2626 *arg = p + 1;
2627 return OK;
2628 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002629 if (must_end)
2630 {
2631 semsg(_(e_missing_comma_before_argument_str), p);
2632 return FAIL;
2633 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002634
Bram Moolenaara5565e42020-05-09 15:44:01 +02002635 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002636 return FAIL;
2637 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002638
2639 if (*p != ',' && *skipwhite(p) == ',')
2640 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002641 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002642 p = skipwhite(p);
2643 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002645 {
2646 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002647 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002648 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002649 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002650 else
2651 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002652 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002653 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002654 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002655failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002656 emsg(_(e_missing_close));
2657 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658}
2659
2660/*
2661 * Compile a function call: name(arg1, arg2)
2662 * "arg" points to "name", "arg + varlen" to the "(".
2663 * "argcount_init" is 1 for "value->method()"
2664 * Instructions:
2665 * EVAL arg1
2666 * EVAL arg2
2667 * BCALL / DCALL / UCALL
2668 */
2669 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002670compile_call(
2671 char_u **arg,
2672 size_t varlen,
2673 cctx_T *cctx,
2674 ppconst_T *ppconst,
2675 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676{
2677 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002678 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002679 int argcount = argcount_init;
2680 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002681 char_u fname_buf[FLEN_FIXED + 1];
2682 char_u *tofree = NULL;
2683 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002684 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002685 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002686 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002687
Bram Moolenaara5565e42020-05-09 15:44:01 +02002688 // we can evaluate "has('name')" at compile time
2689 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2690 {
2691 char_u *s = skipwhite(*arg + varlen + 1);
2692 typval_T argvars[2];
2693
2694 argvars[0].v_type = VAR_UNKNOWN;
2695 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002696 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002697 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002698 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002699 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002700 if (*s == ')' && argvars[0].v_type == VAR_STRING
2701 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002702 {
2703 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2704
2705 *arg = s + 1;
2706 argvars[1].v_type = VAR_UNKNOWN;
2707 tv->v_type = VAR_NUMBER;
2708 tv->vval.v_number = 0;
2709 f_has(argvars, tv);
2710 clear_tv(&argvars[0]);
2711 ++ppconst->pp_used;
2712 return OK;
2713 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002714 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002715 }
2716
2717 if (generate_ppconst(cctx, ppconst) == FAIL)
2718 return FAIL;
2719
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002720 if (varlen >= sizeof(namebuf))
2721 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002722 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002723 return FAIL;
2724 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002725 vim_strncpy(namebuf, *arg, varlen);
2726 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727
2728 *arg = skipwhite(*arg + varlen + 1);
2729 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002730 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002731
Bram Moolenaara1773442020-08-12 15:21:22 +02002732 is_autoload = vim_strchr(name, '#') != NULL;
2733 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002734 {
2735 int idx;
2736
2737 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002738 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002739 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002740 {
2741 if (STRCMP(name, "add") == 0 && argcount == 2)
2742 {
2743 garray_T *stack = &cctx->ctx_type_stack;
2744 type_T *type = ((type_T **)stack->ga_data)[
2745 stack->ga_len - 2];
2746
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002747 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002748 if (type->tt_type == VAR_LIST)
2749 {
2750 // inline "add(list, item)" so that the type can be checked
2751 res = generate_LISTAPPEND(cctx);
2752 idx = -1;
2753 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002754 else if (type->tt_type == VAR_BLOB)
2755 {
2756 // inline "add(blob, nr)" so that the type can be checked
2757 res = generate_BLOBAPPEND(cctx);
2758 idx = -1;
2759 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002760 }
2761
2762 if (idx >= 0)
2763 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2764 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002765 else
2766 semsg(_(e_unknownfunc), namebuf);
2767 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 }
2769
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002770 // An argument or local variable can be a function reference, this
2771 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002772 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002773 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002774 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002775 // If we can find the function by name generate the right call.
2776 // Skip global functions here, a local funcref takes precedence.
2777 ufunc = find_func(name, FALSE, cctx);
2778 if (ufunc != NULL && !func_is_global(ufunc))
2779 {
2780 res = generate_CALL(cctx, ufunc, argcount);
2781 goto theend;
2782 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002783 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002784
2785 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002786 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002787 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002789 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002790 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002791 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002792 garray_T *stack = &cctx->ctx_type_stack;
2793 type_T *type;
2794
2795 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2796 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002797 goto theend;
2798 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799
Bram Moolenaar0f769812020-09-12 18:32:34 +02002800 // If we can find a global function by name generate the right call.
2801 if (ufunc != NULL)
2802 {
2803 res = generate_CALL(cctx, ufunc, argcount);
2804 goto theend;
2805 }
2806
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002807 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002808 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002809 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002810 res = generate_UCALL(cctx, name, argcount);
2811 else
2812 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002813
2814theend:
2815 vim_free(tofree);
2816 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817}
2818
2819// like NAMESPACE_CHAR but with 'a' and 'l'.
2820#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2821
2822/*
2823 * Find the end of a variable or function name. Unlike find_name_end() this
2824 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002825 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826 * Return a pointer to just after the name. Equal to "arg" if there is no
2827 * valid name.
2828 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01002829 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002830to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831{
2832 char_u *p;
2833
2834 // Quick check for valid starting character.
2835 if (!eval_isnamec1(*arg))
2836 return arg;
2837
2838 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2839 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2840 // and can be used in slice "[n:]".
2841 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002842 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2844 break;
2845 return p;
2846}
2847
2848/*
2849 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002850 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 */
2852 char_u *
2853to_name_const_end(char_u *arg)
2854{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002855 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002856 typval_T rettv;
2857
2858 if (p == arg && *arg == '[')
2859 {
2860
2861 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002862 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 p = arg;
2864 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002865 return p;
2866}
2867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002868/*
2869 * parse a list: [expr, expr]
2870 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002871 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872 */
2873 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002874compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875{
2876 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002877 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002878 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002879 int is_const;
2880 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002882 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002884 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002885 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002886 semsg(_(e_list_end), *arg);
2887 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002888 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002889 if (*p == ',')
2890 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002891 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002892 return FAIL;
2893 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002894 if (*p == ']')
2895 {
2896 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002897 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002898 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002899 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002900 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002901 if (!is_const)
2902 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002903 ++count;
2904 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002905 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002907 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2908 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002909 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002910 return FAIL;
2911 }
2912 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002913 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 p = skipwhite(p);
2915 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002916 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002918 ppconst->pp_is_const = is_all_const;
2919 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920}
2921
2922/*
2923 * parse a lambda: {arg, arg -> expr}
2924 * "*arg" points to the '{'.
2925 */
2926 static int
2927compile_lambda(char_u **arg, cctx_T *cctx)
2928{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002929 typval_T rettv;
2930 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002931 evalarg_T evalarg;
2932
2933 CLEAR_FIELD(evalarg);
2934 evalarg.eval_flags = EVAL_EVALUATE;
2935 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936
2937 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002938 if (get_lambda_tv(arg, &rettv, TRUE, &evalarg) != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002939 {
2940 clear_evalarg(&evalarg, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002941 return FAIL;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002942 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01002943
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002945 ++ufunc->uf_refcount;
2946 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947
2948 // The function will have one line: "return {expr}".
2949 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002950 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002952 clear_evalarg(&evalarg, NULL);
2953
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002954 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002955 {
2956 // The return type will now be known.
2957 set_function_type(ufunc);
2958
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002959 // The function reference count will be 1. When the ISN_FUNCREF
2960 // instruction is deleted the reference count is decremented and the
2961 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002962 return generate_FUNCREF(cctx, ufunc);
2963 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002964
2965 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 return FAIL;
2967}
2968
2969/*
2970 * Compile a lamda call: expr->{lambda}(args)
2971 * "arg" points to the "{".
2972 */
2973 static int
2974compile_lambda_call(char_u **arg, cctx_T *cctx)
2975{
2976 ufunc_T *ufunc;
2977 typval_T rettv;
2978 int argcount = 1;
2979 int ret = FAIL;
2980
2981 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002982 if (get_lambda_tv(arg, &rettv, TRUE, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 return FAIL;
2984
2985 if (**arg != '(')
2986 {
2987 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002988 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 else
2990 semsg(_(e_missing_paren), "lambda");
2991 clear_tv(&rettv);
2992 return FAIL;
2993 }
2994
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 ufunc = rettv.vval.v_partial->pt_func;
2996 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002997 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002998 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002999
Bram Moolenaara05e5242020-09-19 18:19:19 +02003000 // The function will have one line: "return {expr}". Compile it into
3001 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003002 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003
3004 // compile the arguments
3005 *arg = skipwhite(*arg + 1);
3006 if (compile_arguments(arg, cctx, &argcount) == OK)
3007 // call the compiled function
3008 ret = generate_CALL(cctx, ufunc, argcount);
3009
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003010 if (ret == FAIL)
3011 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012 return ret;
3013}
3014
3015/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003016 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003018 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 */
3020 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003021compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022{
3023 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003024 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 int count = 0;
3026 dict_T *d = dict_alloc();
3027 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003028 char_u *whitep = *arg;
3029 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003030 int is_const;
3031 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032
3033 if (d == NULL)
3034 return FAIL;
3035 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003036 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003038 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039
Bram Moolenaar23c55272020-06-21 16:58:13 +02003040 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003041 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003042 *arg = NULL;
3043 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003044 }
3045
3046 if (**arg == '}')
3047 break;
3048
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003049 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003051 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003053 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003054 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003055 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003057 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3058 if (isn->isn_type == ISN_PUSHS)
3059 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003060 else
3061 {
3062 type_T *keytype = ((type_T **)stack->ga_data)
Bram Moolenaar2bede172020-11-19 18:53:18 +01003063 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003064 if (need_type(keytype, &t_string, -1, cctx,
Bram Moolenaar2bede172020-11-19 18:53:18 +01003065 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003066 return FAIL;
3067 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003068 *arg = skipwhite(*arg);
3069 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003070 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003071 emsg(_(e_missing_matching_bracket_after_dict_key));
3072 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003073 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003074 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003076 else
3077 {
3078 // {"name": value},
3079 // {'name': value},
3080 // {name: value} use "name" as a literal key
3081 key = get_literal_key(arg);
3082 if (key == NULL)
3083 return FAIL;
3084 if (generate_PUSHS(cctx, key) == FAIL)
3085 return FAIL;
3086 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087
3088 // Check for duplicate keys, if using string keys.
3089 if (key != NULL)
3090 {
3091 item = dict_find(d, key, -1);
3092 if (item != NULL)
3093 {
3094 semsg(_(e_duplicate_key), key);
3095 goto failret;
3096 }
3097 item = dictitem_alloc(key);
3098 if (item != NULL)
3099 {
3100 item->di_tv.v_type = VAR_UNKNOWN;
3101 item->di_tv.v_lock = 0;
3102 if (dict_add(d, item) == FAIL)
3103 dictitem_free(item);
3104 }
3105 }
3106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 if (**arg != ':')
3108 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003109 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003110 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003111 else
3112 semsg(_(e_missing_dict_colon), *arg);
3113 return FAIL;
3114 }
3115 whitep = *arg + 1;
3116 if (!IS_WHITE_OR_NUL(*whitep))
3117 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003118 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003119 return FAIL;
3120 }
3121
3122 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003123 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003124 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003125 *arg = NULL;
3126 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003127 }
3128
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003129 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003131 if (!is_const)
3132 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 ++count;
3134
Bram Moolenaar2c330432020-04-13 14:41:35 +02003135 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003136 *arg = skipwhite(*arg);
3137 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003138 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003139 *arg = NULL;
3140 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003141 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 if (**arg == '}')
3143 break;
3144 if (**arg != ',')
3145 {
3146 semsg(_(e_missing_dict_comma), *arg);
3147 goto failret;
3148 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003149 if (IS_WHITE_OR_NUL(*whitep))
3150 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003151 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003152 return FAIL;
3153 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003154 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003155 if (!IS_WHITE_OR_NUL(*whitep))
3156 {
3157 semsg(_(e_white_space_required_after_str), ",");
3158 return FAIL;
3159 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160 *arg = skipwhite(*arg + 1);
3161 }
3162
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 *arg = *arg + 1;
3164
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003165 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003166 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003167 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003168 *arg += STRLEN(*arg);
3169
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003171 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 return generate_NEWDICT(cctx, count);
3173
3174failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003175 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003176 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003177 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003178 *arg = (char_u *)"";
3179 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 dict_unref(d);
3181 return FAIL;
3182}
3183
3184/*
3185 * Compile "&option".
3186 */
3187 static int
3188compile_get_option(char_u **arg, cctx_T *cctx)
3189{
3190 typval_T rettv;
3191 char_u *start = *arg;
3192 int ret;
3193
3194 // parse the option and get the current value to get the type.
3195 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003196 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197 if (ret == OK)
3198 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003199 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200 char_u *name = vim_strnsave(start, *arg - start);
3201 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3202
3203 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3204 vim_free(name);
3205 }
3206 clear_tv(&rettv);
3207
3208 return ret;
3209}
3210
3211/*
3212 * Compile "$VAR".
3213 */
3214 static int
3215compile_get_env(char_u **arg, cctx_T *cctx)
3216{
3217 char_u *start = *arg;
3218 int len;
3219 int ret;
3220 char_u *name;
3221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 ++*arg;
3223 len = get_env_len(arg);
3224 if (len == 0)
3225 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003226 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003227 return FAIL;
3228 }
3229
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003230 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231 name = vim_strnsave(start, len + 1);
3232 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3233 vim_free(name);
3234 return ret;
3235}
3236
3237/*
3238 * Compile "@r".
3239 */
3240 static int
3241compile_get_register(char_u **arg, cctx_T *cctx)
3242{
3243 int ret;
3244
3245 ++*arg;
3246 if (**arg == NUL)
3247 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003248 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 return FAIL;
3250 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003251 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252 {
3253 emsg_invreg(**arg);
3254 return FAIL;
3255 }
3256 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3257 ++*arg;
3258 return ret;
3259}
3260
3261/*
3262 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003263 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 */
3265 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003266apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003268 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269
3270 // this works from end to start
3271 while (p > start)
3272 {
3273 --p;
3274 if (*p == '-' || *p == '+')
3275 {
3276 // only '-' has an effect, for '+' we only check the type
3277#ifdef FEAT_FLOAT
3278 if (rettv->v_type == VAR_FLOAT)
3279 {
3280 if (*p == '-')
3281 rettv->vval.v_float = -rettv->vval.v_float;
3282 }
3283 else
3284#endif
3285 {
3286 varnumber_T val;
3287 int error = FALSE;
3288
3289 // tv_get_number_chk() accepts a string, but we don't want that
3290 // here
3291 if (check_not_string(rettv) == FAIL)
3292 return FAIL;
3293 val = tv_get_number_chk(rettv, &error);
3294 clear_tv(rettv);
3295 if (error)
3296 return FAIL;
3297 if (*p == '-')
3298 val = -val;
3299 rettv->v_type = VAR_NUMBER;
3300 rettv->vval.v_number = val;
3301 }
3302 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003303 else if (numeric_only)
3304 {
3305 ++p;
3306 break;
3307 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003308 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 {
3310 int v = tv2bool(rettv);
3311
3312 // '!' is permissive in the type.
3313 clear_tv(rettv);
3314 rettv->v_type = VAR_BOOL;
3315 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3316 }
3317 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003318 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 return OK;
3320}
3321
3322/*
3323 * Recognize v: variables that are constants and set "rettv".
3324 */
3325 static void
3326get_vim_constant(char_u **arg, typval_T *rettv)
3327{
3328 if (STRNCMP(*arg, "v:true", 6) == 0)
3329 {
3330 rettv->v_type = VAR_BOOL;
3331 rettv->vval.v_number = VVAL_TRUE;
3332 *arg += 6;
3333 }
3334 else if (STRNCMP(*arg, "v:false", 7) == 0)
3335 {
3336 rettv->v_type = VAR_BOOL;
3337 rettv->vval.v_number = VVAL_FALSE;
3338 *arg += 7;
3339 }
3340 else if (STRNCMP(*arg, "v:null", 6) == 0)
3341 {
3342 rettv->v_type = VAR_SPECIAL;
3343 rettv->vval.v_number = VVAL_NULL;
3344 *arg += 6;
3345 }
3346 else if (STRNCMP(*arg, "v:none", 6) == 0)
3347 {
3348 rettv->v_type = VAR_SPECIAL;
3349 rettv->vval.v_number = VVAL_NONE;
3350 *arg += 6;
3351 }
3352}
3353
Bram Moolenaar696ba232020-07-29 21:20:41 +02003354 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003355get_compare_type(char_u *p, int *len, int *type_is)
3356{
3357 exptype_T type = EXPR_UNKNOWN;
3358 int i;
3359
3360 switch (p[0])
3361 {
3362 case '=': if (p[1] == '=')
3363 type = EXPR_EQUAL;
3364 else if (p[1] == '~')
3365 type = EXPR_MATCH;
3366 break;
3367 case '!': if (p[1] == '=')
3368 type = EXPR_NEQUAL;
3369 else if (p[1] == '~')
3370 type = EXPR_NOMATCH;
3371 break;
3372 case '>': if (p[1] != '=')
3373 {
3374 type = EXPR_GREATER;
3375 *len = 1;
3376 }
3377 else
3378 type = EXPR_GEQUAL;
3379 break;
3380 case '<': if (p[1] != '=')
3381 {
3382 type = EXPR_SMALLER;
3383 *len = 1;
3384 }
3385 else
3386 type = EXPR_SEQUAL;
3387 break;
3388 case 'i': if (p[1] == 's')
3389 {
3390 // "is" and "isnot"; but not a prefix of a name
3391 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3392 *len = 5;
3393 i = p[*len];
3394 if (!isalnum(i) && i != '_')
3395 {
3396 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3397 *type_is = TRUE;
3398 }
3399 }
3400 break;
3401 }
3402 return type;
3403}
3404
3405/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003407 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408 */
3409 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003410compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003412 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413
3414 // this works from end to start
3415 while (p > start)
3416 {
3417 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003418 while (VIM_ISWHITE(*p))
3419 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 if (*p == '-' || *p == '+')
3421 {
3422 int negate = *p == '-';
3423 isn_T *isn;
3424
3425 // TODO: check type
3426 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3427 {
3428 --p;
3429 if (*p == '-')
3430 negate = !negate;
3431 }
3432 // only '-' has an effect, for '+' we only check the type
3433 if (negate)
3434 isn = generate_instr(cctx, ISN_NEGATENR);
3435 else
3436 isn = generate_instr(cctx, ISN_CHECKNR);
3437 if (isn == NULL)
3438 return FAIL;
3439 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003440 else if (numeric_only)
3441 {
3442 ++p;
3443 break;
3444 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 else
3446 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003447 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003449 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003451 if (p[-1] == '!')
3452 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454 }
3455 if (generate_2BOOL(cctx, invert) == FAIL)
3456 return FAIL;
3457 }
3458 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003459 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460 return OK;
3461}
3462
3463/*
3464 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003465 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003466 */
3467 static int
3468compile_subscript(
3469 char_u **arg,
3470 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003471 char_u *start_leader,
3472 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003473 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003475 char_u *name_start = *end_leader;
3476
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003477 for (;;)
3478 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003479 char_u *p = skipwhite(*arg);
3480
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003481 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003482 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003483 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003484
3485 // If a following line starts with "->{" or "->X" advance to that
3486 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003487 // Also if a following line starts with ".x".
3488 if (next != NULL &&
3489 ((next[0] == '-' && next[1] == '>'
3490 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003491 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003492 {
3493 next = next_line_from_context(cctx, TRUE);
3494 if (next == NULL)
3495 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003496 *arg = next;
3497 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003498 }
3499 }
3500
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003501 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3502 // not a function call.
3503 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003505 garray_T *stack = &cctx->ctx_type_stack;
3506 type_T *type;
3507 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003508
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003509 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003510 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003511 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003512
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003514 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3515
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003516 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003517 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3518 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003519 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 return FAIL;
3521 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003522 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003524 char_u *pstart = p;
3525
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003526 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003527 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003528 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003529
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003530 // something->method()
3531 // Apply the '!', '-' and '+' first:
3532 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003533 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003536 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003537 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003538 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 if (**arg == '{')
3540 {
3541 // lambda call: list->{lambda}
3542 if (compile_lambda_call(arg, cctx) == FAIL)
3543 return FAIL;
3544 }
3545 else
3546 {
3547 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003548 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003549 if (!eval_isnamec1(*p))
3550 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003551 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003552 return FAIL;
3553 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003554 if (ASCII_ISALPHA(*p) && p[1] == ':')
3555 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003556 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003557 ;
3558 if (*p != '(')
3559 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003560 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561 return FAIL;
3562 }
3563 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003564 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565 return FAIL;
3566 }
3567 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003568 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003570 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003571 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003572 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003573 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003574 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003575
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003576 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003577 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003578 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003579 // TODO: blob index
3580 // TODO: more arguments
3581 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003582 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003583 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003584 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003585
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003586 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003587 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003588 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003589 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003590 if (**arg == ':')
3591 // missing first index is equal to zero
3592 generate_PUSHNR(cctx, 0);
3593 else
3594 {
3595 if (compile_expr0(arg, cctx) == FAIL)
3596 return FAIL;
3597 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3598 return FAIL;
3599 *arg = skipwhite(*arg);
3600 }
3601 if (**arg == ':')
3602 {
3603 *arg = skipwhite(*arg + 1);
3604 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3605 return FAIL;
3606 if (**arg == ']')
3607 // missing second index is equal to end of string
3608 generate_PUSHNR(cctx, -1);
3609 else
3610 {
3611 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003612 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003613 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3614 return FAIL;
3615 *arg = skipwhite(*arg);
3616 }
3617 is_slice = TRUE;
3618 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619
3620 if (**arg != ']')
3621 {
3622 emsg(_(e_missbrac));
3623 return FAIL;
3624 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003625 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003627 // We can index a list and a dict. If we don't know the type
3628 // we can use the index value type.
3629 // TODO: If we don't know use an instruction to figure it out at
3630 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003631 typep = ((type_T **)stack->ga_data) + stack->ga_len
3632 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003633 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003634 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3635 // If the index is a string, the variable must be a Dict.
3636 if (*typep == &t_any && valtype == &t_string)
3637 vtype = VAR_DICT;
3638 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003639 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003640 if (need_type(valtype, &t_number, -1, cctx,
3641 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003642 return FAIL;
3643 if (is_slice)
3644 {
3645 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003646 if (need_type(valtype, &t_number, -2, cctx,
3647 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003648 return FAIL;
3649 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003650 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003651
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003652 if (vtype == VAR_DICT)
3653 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003654 if (is_slice)
3655 {
3656 emsg(_(e_cannot_slice_dictionary));
3657 return FAIL;
3658 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003659 if ((*typep)->tt_type == VAR_DICT)
3660 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003661 else
3662 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003663 if (need_type(*typep, &t_dict_any, -2, cctx,
3664 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003665 return FAIL;
3666 *typep = &t_any;
3667 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003668 if (may_generate_2STRING(-1, cctx) == FAIL)
3669 return FAIL;
3670 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3671 return FAIL;
3672 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003673 else if (vtype == VAR_STRING)
3674 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003675 *typep = &t_string;
3676 if ((is_slice
3677 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3678 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003679 return FAIL;
3680 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003681 else if (vtype == VAR_BLOB)
3682 {
3683 emsg("Sorry, blob index and slice not implemented yet");
3684 return FAIL;
3685 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003686 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003687 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003688 if (is_slice)
3689 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003690 if (generate_instr_drop(cctx,
3691 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3692 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003693 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003694 }
Bram Moolenaared591872020-08-15 22:14:53 +02003695 else
3696 {
3697 if ((*typep)->tt_type == VAR_LIST)
3698 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003699 if (generate_instr_drop(cctx,
3700 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3701 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003702 return FAIL;
3703 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003704 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003705 else
3706 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003707 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003708 return FAIL;
3709 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003711 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003713 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003714 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003715 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003716 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003717
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003718 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003719 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003720 {
3721 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003722 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003723 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003724 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003725 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 while (eval_isnamec(*p))
3727 MB_PTR_ADV(p);
3728 if (p == *arg)
3729 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003730 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731 return FAIL;
3732 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003733 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 return FAIL;
3735 *arg = p;
3736 }
3737 else
3738 break;
3739 }
3740
3741 // TODO - see handle_subscript():
3742 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3743 // Don't do this when "Func" is already a partial that was bound
3744 // explicitly (pt_auto is FALSE).
3745
3746 return OK;
3747}
3748
3749/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003750 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3751 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003752 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003753 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003754 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003755 *
3756 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003757 */
3758
3759/*
3760 * number number constant
3761 * 0zFFFFFFFF Blob constant
3762 * "string" string constant
3763 * 'string' literal string constant
3764 * &option-name option value
3765 * @r register contents
3766 * identifier variable value
3767 * function() function call
3768 * $VAR environment variable
3769 * (expression) nested expression
3770 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01003771 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 *
3773 * Also handle:
3774 * ! in front logical NOT
3775 * - in front unary minus
3776 * + in front unary plus (ignored)
3777 * trailing (arg) funcref/partial call
3778 * trailing [] subscript in String or List
3779 * trailing .name entry in Dictionary
3780 * trailing ->name() method call
3781 */
3782 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003783compile_expr7(
3784 char_u **arg,
3785 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003786 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788 char_u *start_leader, *end_leader;
3789 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003790 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003791 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003793 ppconst->pp_is_const = FALSE;
3794
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 /*
3796 * Skip '!', '-' and '+' characters. They are handled later.
3797 */
3798 start_leader = *arg;
3799 while (**arg == '!' || **arg == '-' || **arg == '+')
3800 *arg = skipwhite(*arg + 1);
3801 end_leader = *arg;
3802
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003803 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003804 switch (**arg)
3805 {
3806 /*
3807 * Number constant.
3808 */
3809 case '0': // also for blob starting with 0z
3810 case '1':
3811 case '2':
3812 case '3':
3813 case '4':
3814 case '5':
3815 case '6':
3816 case '7':
3817 case '8':
3818 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003819 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003821 // Apply "-" and "+" just before the number now, right to
3822 // left. Matters especially when "->" follows. Stops at
3823 // '!'.
3824 if (apply_leader(rettv, TRUE,
3825 start_leader, &end_leader) == FAIL)
3826 {
3827 clear_tv(rettv);
3828 return FAIL;
3829 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830 break;
3831
3832 /*
3833 * String constant: "string".
3834 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003835 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836 return FAIL;
3837 break;
3838
3839 /*
3840 * Literal string constant: 'str''ing'.
3841 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003842 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003843 return FAIL;
3844 break;
3845
3846 /*
3847 * Constant Vim variable.
3848 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003849 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 ret = NOTDONE;
3851 break;
3852
3853 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003854 * "true" constant
3855 */
3856 case 't': if (STRNCMP(*arg, "true", 4) == 0
3857 && !eval_isnamec((*arg)[4]))
3858 {
3859 *arg += 4;
3860 rettv->v_type = VAR_BOOL;
3861 rettv->vval.v_number = VVAL_TRUE;
3862 }
3863 else
3864 ret = NOTDONE;
3865 break;
3866
3867 /*
3868 * "false" constant
3869 */
3870 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3871 && !eval_isnamec((*arg)[5]))
3872 {
3873 *arg += 5;
3874 rettv->v_type = VAR_BOOL;
3875 rettv->vval.v_number = VVAL_FALSE;
3876 }
3877 else
3878 ret = NOTDONE;
3879 break;
3880
3881 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003882 * List: [expr, expr]
3883 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003884 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 break;
3886
3887 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 * Lambda: {arg, arg -> expr}
3889 * Dictionary: {'key': val, 'key': val}
3890 */
3891 case '{': {
3892 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003893 garray_T ga_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894
3895 // Find out what comes after the arguments.
3896 ret = get_function_args(&start, '-', NULL,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003897 &ga_arg, TRUE, NULL, NULL,
3898 TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899 if (ret != FAIL && *start == '>')
3900 ret = compile_lambda(arg, cctx);
3901 else
Bram Moolenaare0de1712020-12-02 17:36:54 +01003902 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 }
3904 break;
3905
3906 /*
3907 * Option value: &name
3908 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003909 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3910 return FAIL;
3911 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 break;
3913
3914 /*
3915 * Environment variable: $VAR.
3916 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003917 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3918 return FAIL;
3919 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920 break;
3921
3922 /*
3923 * Register contents: @r.
3924 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003925 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3926 return FAIL;
3927 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003928 break;
3929 /*
3930 * nested expression: (expression).
3931 */
3932 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003933
3934 // recursive!
3935 if (ppconst->pp_used <= PPSIZE - 10)
3936 {
3937 ret = compile_expr1(arg, cctx, ppconst);
3938 }
3939 else
3940 {
3941 // Not enough space in ppconst, flush constants.
3942 if (generate_ppconst(cctx, ppconst) == FAIL)
3943 return FAIL;
3944 ret = compile_expr0(arg, cctx);
3945 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 *arg = skipwhite(*arg);
3947 if (**arg == ')')
3948 ++*arg;
3949 else if (ret == OK)
3950 {
3951 emsg(_(e_missing_close));
3952 ret = FAIL;
3953 }
3954 break;
3955
3956 default: ret = NOTDONE;
3957 break;
3958 }
3959 if (ret == FAIL)
3960 return FAIL;
3961
Bram Moolenaar1c747212020-05-09 18:28:34 +02003962 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003964 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003965 clear_tv(rettv);
3966 else
3967 // A constant expression can possibly be handled compile time,
3968 // return the value instead of generating code.
3969 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 }
3971 else if (ret == NOTDONE)
3972 {
3973 char_u *p;
3974 int r;
3975
3976 if (!eval_isnamec1(**arg))
3977 {
Bram Moolenaare4984292020-12-13 14:19:25 +01003978 if (ends_excmd(*skipwhite(*arg)))
3979 semsg(_(e_empty_expression_str), *arg);
3980 else
3981 semsg(_(e_name_expected_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982 return FAIL;
3983 }
3984
3985 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003986 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003988 {
3989 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3990 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003991 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003992 {
3993 if (generate_ppconst(cctx, ppconst) == FAIL)
3994 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003995 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003996 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003997 if (r == FAIL)
3998 return FAIL;
3999 }
4000
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004001 // Handle following "[]", ".member", etc.
4002 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004003 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004004 ppconst) == FAIL)
4005 return FAIL;
4006 if (ppconst->pp_used > 0)
4007 {
4008 // apply the '!', '-' and '+' before the constant
4009 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004010 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004011 return FAIL;
4012 return OK;
4013 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004014 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004015 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004016 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017}
4018
4019/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004020 * Give the "white on both sides" error, taking the operator from "p[len]".
4021 */
4022 void
4023error_white_both(char_u *op, int len)
4024{
4025 char_u buf[10];
4026
4027 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004028 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004029}
4030
4031/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004032 * <type>expr7: runtime type check / conversion
4033 */
4034 static int
4035compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4036{
4037 type_T *want_type = NULL;
4038
4039 // Recognize <type>
4040 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4041 {
4042 int called_emsg_before = called_emsg;
4043
4044 ++*arg;
4045 want_type = parse_type(arg, cctx->ctx_type_list);
4046 if (called_emsg != called_emsg_before)
4047 return FAIL;
4048
4049 if (**arg != '>')
4050 {
4051 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004052 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004053 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004054 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004055 return FAIL;
4056 }
4057 ++*arg;
4058 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
4059 return FAIL;
4060 }
4061
4062 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4063 return FAIL;
4064
4065 if (want_type != NULL)
4066 {
4067 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004068 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004069
Bram Moolenaard1103582020-08-14 22:44:25 +02004070 generate_ppconst(cctx, ppconst);
4071 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02004072 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004073 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004074 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004075 return FAIL;
4076 }
4077 }
4078
4079 return OK;
4080}
4081
4082/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004083 * * number multiplication
4084 * / number division
4085 * % number modulo
4086 */
4087 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004088compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089{
4090 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004091 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004092 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004094 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004095 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 return FAIL;
4097
4098 /*
4099 * Repeat computing, until no "*", "/" or "%" is following.
4100 */
4101 for (;;)
4102 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004103 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104 if (*op != '*' && *op != '/' && *op != '%')
4105 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004106 if (next != NULL)
4107 {
4108 *arg = next_line_from_context(cctx, TRUE);
4109 op = skipwhite(*arg);
4110 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004111
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004112 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004114 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004115 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004116 }
4117 *arg = skipwhite(op + 1);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004118 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004119 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004121 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004122 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004124
4125 if (ppconst->pp_used == ppconst_used + 2
4126 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4127 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004128 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004129 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4130 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004131 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004132
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004133 // both are numbers: compute the result
4134 switch (*op)
4135 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004136 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004137 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004138 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004139 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004140 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004141 break;
4142 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004143 tv1->vval.v_number = res;
4144 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004145 }
4146 else
4147 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004148 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004149 generate_two_op(cctx, op);
4150 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004151 }
4152
4153 return OK;
4154}
4155
4156/*
4157 * + number addition
4158 * - number subtraction
4159 * .. string concatenation
4160 */
4161 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004162compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163{
4164 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004165 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004166 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004167 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168
4169 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004170 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171 return FAIL;
4172
4173 /*
4174 * Repeat computing, until no "+", "-" or ".." is following.
4175 */
4176 for (;;)
4177 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004178 op = may_peek_next_line(cctx, *arg, &next);
4179 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004180 break;
4181 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004182 if (next != NULL)
4183 {
4184 *arg = next_line_from_context(cctx, TRUE);
4185 op = skipwhite(*arg);
4186 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004187
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004188 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004189 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004190 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004191 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004192 }
4193
4194 *arg = skipwhite(op + oplen);
Bram Moolenaare0de1712020-12-02 17:36:54 +01004195 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004196 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004197
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004198 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004199 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004200 return FAIL;
4201
Bram Moolenaara5565e42020-05-09 15:44:01 +02004202 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004203 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004204 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4205 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4206 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4207 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004208 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004209 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4210 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004211
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004212 // concat/subtract/add constant numbers
4213 if (*op == '+')
4214 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4215 else if (*op == '-')
4216 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4217 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004218 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004219 // concatenate constant strings
4220 char_u *s1 = tv1->vval.v_string;
4221 char_u *s2 = tv2->vval.v_string;
4222 size_t len1 = STRLEN(s1);
4223
4224 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4225 if (tv1->vval.v_string == NULL)
4226 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004227 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004228 return FAIL;
4229 }
4230 mch_memmove(tv1->vval.v_string, s1, len1);
4231 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004232 vim_free(s1);
4233 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004234 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004235 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236 }
4237 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004238 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004239 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004240 if (*op == '.')
4241 {
4242 if (may_generate_2STRING(-2, cctx) == FAIL
4243 || may_generate_2STRING(-1, cctx) == FAIL)
4244 return FAIL;
4245 generate_instr_drop(cctx, ISN_CONCAT, 1);
4246 }
4247 else
4248 generate_two_op(cctx, op);
4249 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250 }
4251
4252 return OK;
4253}
4254
4255/*
4256 * expr5a == expr5b
4257 * expr5a =~ expr5b
4258 * expr5a != expr5b
4259 * expr5a !~ expr5b
4260 * expr5a > expr5b
4261 * expr5a >= expr5b
4262 * expr5a < expr5b
4263 * expr5a <= expr5b
4264 * expr5a is expr5b
4265 * expr5a isnot expr5b
4266 *
4267 * Produces instructions:
4268 * EVAL expr5a Push result of "expr5a"
4269 * EVAL expr5b Push result of "expr5b"
4270 * COMPARE one of the compare instructions
4271 */
4272 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004273compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274{
4275 exptype_T type = EXPR_UNKNOWN;
4276 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004277 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004280 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004281
4282 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004283 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004284 return FAIL;
4285
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004286 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004287 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288
4289 /*
4290 * If there is a comparative operator, use it.
4291 */
4292 if (type != EXPR_UNKNOWN)
4293 {
4294 int ic = FALSE; // Default: do not ignore case
4295
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004296 if (next != NULL)
4297 {
4298 *arg = next_line_from_context(cctx, TRUE);
4299 p = skipwhite(*arg);
4300 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004301 if (type_is && (p[len] == '?' || p[len] == '#'))
4302 {
4303 semsg(_(e_invexpr2), *arg);
4304 return FAIL;
4305 }
4306 // extra question mark appended: ignore case
4307 if (p[len] == '?')
4308 {
4309 ic = TRUE;
4310 ++len;
4311 }
4312 // extra '#' appended: match case (ignored)
4313 else if (p[len] == '#')
4314 ++len;
4315 // nothing appended: match case
4316
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004317 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004319 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004320 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321 }
4322
4323 // get the second variable
4324 *arg = skipwhite(p + len);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004325 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004326 return FAIL;
4327
Bram Moolenaara5565e42020-05-09 15:44:01 +02004328 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004329 return FAIL;
4330
Bram Moolenaara5565e42020-05-09 15:44:01 +02004331 if (ppconst->pp_used == ppconst_used + 2)
4332 {
4333 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4334 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4335 int ret;
4336
4337 // Both sides are a constant, compute the result now.
4338 // First check for a valid combination of types, this is more
4339 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004340 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004341 ret = FAIL;
4342 else
4343 {
4344 ret = typval_compare(tv1, tv2, type, ic);
4345 tv1->v_type = VAR_BOOL;
4346 tv1->vval.v_number = tv1->vval.v_number
4347 ? VVAL_TRUE : VVAL_FALSE;
4348 clear_tv(tv2);
4349 --ppconst->pp_used;
4350 }
4351 return ret;
4352 }
4353
4354 generate_ppconst(cctx, ppconst);
4355 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004356 }
4357
4358 return OK;
4359}
4360
Bram Moolenaar7f141552020-05-09 17:35:53 +02004361static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4362
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363/*
4364 * Compile || or &&.
4365 */
4366 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004367compile_and_or(
4368 char_u **arg,
4369 cctx_T *cctx,
4370 char *op,
4371 ppconst_T *ppconst,
4372 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004374 char_u *next;
4375 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 int opchar = *op;
4377
4378 if (p[0] == opchar && p[1] == opchar)
4379 {
4380 garray_T *instr = &cctx->ctx_instr;
4381 garray_T end_ga;
4382
4383 /*
4384 * Repeat until there is no following "||" or "&&"
4385 */
4386 ga_init2(&end_ga, sizeof(int), 10);
4387 while (p[0] == opchar && p[1] == opchar)
4388 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004389 if (next != NULL)
4390 {
4391 *arg = next_line_from_context(cctx, TRUE);
4392 p = skipwhite(*arg);
4393 }
4394
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004395 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4396 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004397 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004398 return FAIL;
4399 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004401 // TODO: use ppconst if the value is a constant and check
4402 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004403 generate_ppconst(cctx, ppconst);
4404
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004405 // Every part must evaluate to a bool.
4406 if (bool_on_stack(cctx) == FAIL)
4407 {
4408 ga_clear(&end_ga);
4409 return FAIL;
4410 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004411
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412 if (ga_grow(&end_ga, 1) == FAIL)
4413 {
4414 ga_clear(&end_ga);
4415 return FAIL;
4416 }
4417 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4418 ++end_ga.ga_len;
4419 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004420 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421
4422 // eval the next expression
4423 *arg = skipwhite(p + 2);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004424 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004425 {
4426 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004427 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004428 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004429
Bram Moolenaara5565e42020-05-09 15:44:01 +02004430 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4431 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432 {
4433 ga_clear(&end_ga);
4434 return FAIL;
4435 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004436
4437 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004438 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004439 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004441 // Every part must evaluate to a bool.
4442 if (bool_on_stack(cctx) == FAIL)
4443 {
4444 ga_clear(&end_ga);
4445 return FAIL;
4446 }
4447
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 // Fill in the end label in all jumps.
4449 while (end_ga.ga_len > 0)
4450 {
4451 isn_T *isn;
4452
4453 --end_ga.ga_len;
4454 isn = ((isn_T *)instr->ga_data)
4455 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4456 isn->isn_arg.jump.jump_where = instr->ga_len;
4457 }
4458 ga_clear(&end_ga);
4459 }
4460
4461 return OK;
4462}
4463
4464/*
4465 * expr4a && expr4a && expr4a logical AND
4466 *
4467 * Produces instructions:
4468 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004469 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004470 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004471 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004472 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004473 * EVAL expr4c Push result of "expr4c"
4474 * end:
4475 */
4476 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004477compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004479 int ppconst_used = ppconst->pp_used;
4480
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004482 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004483 return FAIL;
4484
4485 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004486 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487}
4488
4489/*
4490 * expr3a || expr3b || expr3c logical OR
4491 *
4492 * Produces instructions:
4493 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004494 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004495 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004496 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004497 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498 * EVAL expr3c Push result of "expr3c"
4499 * end:
4500 */
4501 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004502compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004504 int ppconst_used = ppconst->pp_used;
4505
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004506 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004507 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508 return FAIL;
4509
4510 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004511 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512}
4513
4514/*
4515 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004517 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004518 * JUMP_IF_FALSE alt jump if false
4519 * EVAL expr1a
4520 * JUMP_ALWAYS end
4521 * alt: EVAL expr1b
4522 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004523 *
4524 * Toplevel expression: expr2 ?? expr1
4525 * Produces instructions:
4526 * EVAL expr2 Push result of "expr2"
4527 * JUMP_AND_KEEP_IF_TRUE end jump if true
4528 * EVAL expr1
4529 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004530 */
4531 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004532compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004533{
4534 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004535 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004536 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004537
Bram Moolenaar3988f642020-08-27 22:43:03 +02004538 // Ignore all kinds of errors when not producing code.
4539 if (cctx->ctx_skip == SKIP_YES)
4540 {
Bram Moolenaar683581e2020-10-22 21:22:58 +02004541 evalarg_T evalarg;
4542
4543 CLEAR_FIELD(evalarg);
4544 evalarg.eval_cctx = cctx;
4545 skip_expr(arg, &evalarg);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004546 return OK;
4547 }
4548
Bram Moolenaar61a89812020-05-07 16:58:17 +02004549 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004550 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004551 return FAIL;
4552
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004553 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004554 if (*p == '?')
4555 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004556 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004557 garray_T *instr = &cctx->ctx_instr;
4558 garray_T *stack = &cctx->ctx_type_stack;
4559 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004560 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004561 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004562 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004563 int has_const_expr = FALSE;
4564 int const_value = FALSE;
4565 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004566
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004567 if (next != NULL)
4568 {
4569 *arg = next_line_from_context(cctx, TRUE);
4570 p = skipwhite(*arg);
4571 }
4572
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004573 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004574 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004575 semsg(_(e_white_space_required_before_and_after_str),
4576 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004577 return FAIL;
4578 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004579
Bram Moolenaara5565e42020-05-09 15:44:01 +02004580 if (ppconst->pp_used == ppconst_used + 1)
4581 {
4582 // the condition is a constant, we know whether the ? or the :
4583 // expression is to be evaluated.
4584 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004585 if (op_falsy)
4586 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4587 else
4588 {
4589 int error = FALSE;
4590
4591 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4592 &error);
4593 if (error)
4594 return FAIL;
4595 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004596 cctx->ctx_skip = save_skip == SKIP_YES ||
4597 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4598
4599 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4600 // "left ?? right" and "left" is truthy: produce "left"
4601 generate_ppconst(cctx, ppconst);
4602 else
4603 {
4604 clear_tv(&ppconst->pp_tv[ppconst_used]);
4605 --ppconst->pp_used;
4606 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004607 }
4608 else
4609 {
4610 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004611 if (op_falsy)
4612 end_idx = instr->ga_len;
4613 generate_JUMP(cctx, op_falsy
4614 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4615 if (op_falsy)
4616 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004617 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618
4619 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004620 *arg = skipwhite(p + 1 + op_falsy);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004621 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004622 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004623 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004624 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004625
Bram Moolenaara5565e42020-05-09 15:44:01 +02004626 if (!has_const_expr)
4627 {
4628 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004630 if (!op_falsy)
4631 {
4632 // remember the type and drop it
4633 --stack->ga_len;
4634 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004636 end_idx = instr->ga_len;
4637 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004638
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004639 // jump here from JUMP_IF_FALSE
4640 isn = ((isn_T *)instr->ga_data) + alt_idx;
4641 isn->isn_arg.jump.jump_where = instr->ga_len;
4642 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004643 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004645 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004646 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004647 // Check for the ":".
4648 p = may_peek_next_line(cctx, *arg, &next);
4649 if (*p != ':')
4650 {
4651 emsg(_(e_missing_colon));
4652 return FAIL;
4653 }
4654 if (next != NULL)
4655 {
4656 *arg = next_line_from_context(cctx, TRUE);
4657 p = skipwhite(*arg);
4658 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004659
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004660 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4661 {
4662 semsg(_(e_white_space_required_before_and_after_str), ":");
4663 return FAIL;
4664 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004666 // evaluate the third expression
4667 if (has_const_expr)
4668 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004669 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004670 *arg = skipwhite(p + 1);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004671 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004672 return FAIL;
4673 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4674 return FAIL;
4675 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004676
Bram Moolenaara5565e42020-05-09 15:44:01 +02004677 if (!has_const_expr)
4678 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004679 type_T **typep;
4680
Bram Moolenaara5565e42020-05-09 15:44:01 +02004681 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004682
Bram Moolenaara5565e42020-05-09 15:44:01 +02004683 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004684 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4685 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004686
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004687 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004688 isn = ((isn_T *)instr->ga_data) + end_idx;
4689 isn->isn_arg.jump.jump_where = instr->ga_len;
4690 }
4691
4692 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004693 }
4694 return OK;
4695}
4696
4697/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004698 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004699 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4700 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004701 */
4702 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004703compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004704{
4705 ppconst_T ppconst;
4706
4707 CLEAR_FIELD(ppconst);
4708 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4709 {
4710 clear_ppconst(&ppconst);
4711 return FAIL;
4712 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004713 if (is_const != NULL)
4714 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004715 if (generate_ppconst(cctx, &ppconst) == FAIL)
4716 return FAIL;
4717 return OK;
4718}
4719
4720/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004721 * Toplevel expression.
4722 */
4723 static int
4724compile_expr0(char_u **arg, cctx_T *cctx)
4725{
4726 return compile_expr0_ext(arg, cctx, NULL);
4727}
4728
4729/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004730 * compile "return [expr]"
4731 */
4732 static char_u *
4733compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4734{
4735 char_u *p = arg;
4736 garray_T *stack = &cctx->ctx_type_stack;
4737 type_T *stack_type;
4738
4739 if (*p != NUL && *p != '|' && *p != '\n')
4740 {
4741 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004742 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004743 return NULL;
4744
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004745 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004746 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004747 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4748 if (set_return_type)
4749 cctx->ctx_ufunc->uf_ret_type = stack_type;
4750 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004751 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004752 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4753 && stack_type->tt_type != VAR_VOID
4754 && stack_type->tt_type != VAR_UNKNOWN)
4755 {
4756 emsg(_(e_returning_value_in_function_without_return_type));
4757 return NULL;
4758 }
4759 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004760 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004761 return NULL;
4762 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02004763 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004764 }
4765 else
4766 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004767 // "set_return_type" cannot be TRUE, only used for a lambda which
4768 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004769 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4770 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004771 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004772 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004773 return NULL;
4774 }
4775
4776 // No argument, return zero.
4777 generate_PUSHNR(cctx, 0);
4778 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004779 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004780 return NULL;
4781
4782 // "return val | endif" is possible
4783 return skipwhite(p);
4784}
4785
4786/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004787 * Get a line from the compilation context, compatible with exarg_T getline().
4788 * Return a pointer to the line in allocated memory.
4789 * Return NULL for end-of-file or some error.
4790 */
4791 static char_u *
4792exarg_getline(
4793 int c UNUSED,
4794 void *cookie,
4795 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004796 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004797{
4798 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004799 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004800
Bram Moolenaar66250c92020-08-20 15:02:42 +02004801 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004802 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004803 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004804 return NULL;
4805 ++cctx->ctx_lnum;
4806 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4807 // Comment lines result in NULL pointers, skip them.
4808 if (p != NULL)
4809 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004810 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004811}
4812
4813/*
4814 * Compile a nested :def command.
4815 */
4816 static char_u *
4817compile_nested_function(exarg_T *eap, cctx_T *cctx)
4818{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004819 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004820 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004821 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004822 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004823 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004824 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004825
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004826 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004827 {
4828 emsg(_(e_cannot_use_bang_with_nested_def));
4829 return NULL;
4830 }
4831
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004832 if (*name_start == '/')
4833 {
4834 name_end = skip_regexp(name_start + 1, '/', TRUE);
4835 if (*name_end == '/')
4836 ++name_end;
4837 eap->nextcmd = check_nextcmd(name_end);
4838 }
4839 if (name_end == name_start || *skipwhite(name_end) != '(')
4840 {
4841 if (!ends_excmd2(name_start, name_end))
4842 {
4843 semsg(_(e_invalid_command_str), eap->cmd);
4844 return NULL;
4845 }
4846
4847 // "def" or "def Name": list functions
4848 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
4849 return NULL;
4850 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
4851 }
4852
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004853 // Only g:Func() can use a namespace.
4854 if (name_start[1] == ':' && !is_global)
4855 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004856 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004857 return NULL;
4858 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004859 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4860 return NULL;
4861
Bram Moolenaar04b12692020-05-04 23:24:44 +02004862 eap->arg = name_end;
4863 eap->getline = exarg_getline;
4864 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004865 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004866 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004867 lambda_name = get_lambda_name();
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004868 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004869
Bram Moolenaar822ba242020-05-24 23:00:18 +02004870 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004871 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004872 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004873 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004874 {
4875 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004876 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004877 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004878
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004879 if (is_global)
4880 {
4881 char_u *func_name = vim_strnsave(name_start + 2,
4882 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004883
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004884 if (func_name == NULL)
4885 r = FAIL;
4886 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004887 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004888 }
4889 else
4890 {
4891 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004892 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004893 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004894 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004895
Bram Moolenaareef21022020-08-01 22:16:43 +02004896 if (lvar == NULL)
4897 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004898 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004899 return NULL;
4900 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004901
4902 // copy over the block scope IDs
4903 if (block_depth > 0)
4904 {
4905 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4906 if (ufunc->uf_block_ids != NULL)
4907 {
4908 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4909 sizeof(int) * block_depth);
4910 ufunc->uf_block_depth = block_depth;
4911 }
4912 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004913 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004914
Bram Moolenaar61a89812020-05-07 16:58:17 +02004915 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004916 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004917}
4918
4919/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004920 * Return the length of an assignment operator, or zero if there isn't one.
4921 */
4922 int
4923assignment_len(char_u *p, int *heredoc)
4924{
4925 if (*p == '=')
4926 {
4927 if (p[1] == '<' && p[2] == '<')
4928 {
4929 *heredoc = TRUE;
4930 return 3;
4931 }
4932 return 1;
4933 }
4934 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4935 return 2;
4936 if (STRNCMP(p, "..=", 3) == 0)
4937 return 3;
4938 return 0;
4939}
4940
4941// words that cannot be used as a variable
4942static char *reserved[] = {
4943 "true",
4944 "false",
4945 NULL
4946};
4947
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004948typedef enum {
4949 dest_local,
4950 dest_option,
4951 dest_env,
4952 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004953 dest_buffer,
4954 dest_window,
4955 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004956 dest_vimvar,
4957 dest_script,
4958 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01004959 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004960} assign_dest_T;
4961
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004962/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004963 * Generate the load instruction for "name".
4964 */
4965 static void
4966generate_loadvar(
4967 cctx_T *cctx,
4968 assign_dest_T dest,
4969 char_u *name,
4970 lvar_T *lvar,
4971 type_T *type)
4972{
4973 switch (dest)
4974 {
4975 case dest_option:
4976 // TODO: check the option exists
4977 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4978 break;
4979 case dest_global:
4980 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4981 break;
4982 case dest_buffer:
4983 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4984 break;
4985 case dest_window:
4986 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4987 break;
4988 case dest_tab:
4989 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4990 break;
4991 case dest_script:
4992 compile_load_scriptvar(cctx,
4993 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4994 break;
4995 case dest_env:
4996 // Include $ in the name here
4997 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4998 break;
4999 case dest_reg:
5000 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5001 break;
5002 case dest_vimvar:
5003 generate_LOADV(cctx, name + 2, TRUE);
5004 break;
5005 case dest_local:
5006 if (lvar->lv_from_outer)
5007 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
5008 NULL, type);
5009 else
5010 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5011 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005012 case dest_expr:
5013 // list or dict value should already be on the stack.
5014 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005015 }
5016}
5017
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005018/*
5019 * Skip over "[expr]" or ".member".
5020 * Does not check for any errors.
5021 */
5022 static char_u *
5023skip_index(char_u *start)
5024{
5025 char_u *p = start;
5026
5027 if (*p == '[')
5028 {
5029 p = skipwhite(p + 1);
5030 (void)skip_expr(&p, NULL);
5031 p = skipwhite(p);
5032 if (*p == ']')
5033 return p + 1;
5034 return p;
5035 }
5036 // if (*p == '.')
5037 return to_name_end(p + 1, TRUE);
5038}
5039
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005040 void
5041vim9_declare_error(char_u *name)
5042{
5043 char *scope = "";
5044
5045 switch (*name)
5046 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005047 case 'g': scope = _("global"); break;
5048 case 'b': scope = _("buffer"); break;
5049 case 'w': scope = _("window"); break;
5050 case 't': scope = _("tab"); break;
5051 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005052 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005053 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005054 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005055 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005056 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005057 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005058 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005059 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005060 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005061}
5062
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005063/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005064 * For one assignment figure out the type of destination. Return it in "dest".
5065 * When not recognized "dest" is not set.
5066 * For an option "opt_flags" is set.
5067 * For a v:var "vimvaridx" is set.
5068 * "type" is set to the destination type if known, unchanted otherwise.
5069 * Return FAIL if an error message was given.
5070 */
5071 static int
5072get_var_dest(
5073 char_u *name,
5074 assign_dest_T *dest,
5075 int cmdidx,
5076 int *opt_flags,
5077 int *vimvaridx,
5078 type_T **type,
5079 cctx_T *cctx)
5080{
5081 char_u *p;
5082
5083 if (*name == '&')
5084 {
5085 int cc;
5086 long numval;
5087 int opt_type;
5088
5089 *dest = dest_option;
5090 if (cmdidx == CMD_final || cmdidx == CMD_const)
5091 {
5092 emsg(_(e_const_option));
5093 return FAIL;
5094 }
5095 p = name;
5096 p = find_option_end(&p, opt_flags);
5097 if (p == NULL)
5098 {
5099 // cannot happen?
5100 emsg(_(e_letunexp));
5101 return FAIL;
5102 }
5103 cc = *p;
5104 *p = NUL;
5105 opt_type = get_option_value(skip_option_env_lead(name),
5106 &numval, NULL, *opt_flags);
5107 *p = cc;
5108 if (opt_type == -3)
5109 {
5110 semsg(_(e_unknown_option), name);
5111 return FAIL;
5112 }
5113 if (opt_type == -2 || opt_type == 0)
5114 *type = &t_string;
5115 else
5116 *type = &t_number; // both number and boolean option
5117 }
5118 else if (*name == '$')
5119 {
5120 *dest = dest_env;
5121 *type = &t_string;
5122 }
5123 else if (*name == '@')
5124 {
5125 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5126 {
5127 emsg_invreg(name[1]);
5128 return FAIL;
5129 }
5130 *dest = dest_reg;
5131 *type = &t_string;
5132 }
5133 else if (STRNCMP(name, "g:", 2) == 0)
5134 {
5135 *dest = dest_global;
5136 }
5137 else if (STRNCMP(name, "b:", 2) == 0)
5138 {
5139 *dest = dest_buffer;
5140 }
5141 else if (STRNCMP(name, "w:", 2) == 0)
5142 {
5143 *dest = dest_window;
5144 }
5145 else if (STRNCMP(name, "t:", 2) == 0)
5146 {
5147 *dest = dest_tab;
5148 }
5149 else if (STRNCMP(name, "v:", 2) == 0)
5150 {
5151 typval_T *vtv;
5152 int di_flags;
5153
5154 *vimvaridx = find_vim_var(name + 2, &di_flags);
5155 if (*vimvaridx < 0)
5156 {
5157 semsg(_(e_variable_not_found_str), name);
5158 return FAIL;
5159 }
5160 // We use the current value of "sandbox" here, is that OK?
5161 if (var_check_ro(di_flags, name, FALSE))
5162 return FAIL;
5163 *dest = dest_vimvar;
5164 vtv = get_vim_var_tv(*vimvaridx);
5165 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5166 }
5167 return OK;
5168}
5169
5170/*
5171 * Generate a STORE instruction for "dest", not being "dest_local".
5172 * Return FAIL when out of memory.
5173 */
5174 static int
5175generate_store_var(
5176 cctx_T *cctx,
5177 assign_dest_T dest,
5178 int opt_flags,
5179 int vimvaridx,
5180 int scriptvar_idx,
5181 int scriptvar_sid,
5182 type_T *type,
5183 char_u *name)
5184{
5185 switch (dest)
5186 {
5187 case dest_option:
5188 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5189 opt_flags);
5190 case dest_global:
5191 // include g: with the name, easier to execute that way
5192 return generate_STORE(cctx, ISN_STOREG, 0, name);
5193 case dest_buffer:
5194 // include b: with the name, easier to execute that way
5195 return generate_STORE(cctx, ISN_STOREB, 0, name);
5196 case dest_window:
5197 // include w: with the name, easier to execute that way
5198 return generate_STORE(cctx, ISN_STOREW, 0, name);
5199 case dest_tab:
5200 // include t: with the name, easier to execute that way
5201 return generate_STORE(cctx, ISN_STORET, 0, name);
5202 case dest_env:
5203 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5204 case dest_reg:
5205 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5206 case dest_vimvar:
5207 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5208 case dest_script:
5209 if (scriptvar_idx < 0)
5210 {
5211 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005212 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005213
Bram Moolenaar41d61962020-12-06 20:12:43 +01005214 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005215 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5216 scriptvar_sid, type);
5217 if (name_s != name)
5218 vim_free(name_s);
5219 return r;
5220 }
5221 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5222 scriptvar_sid, scriptvar_idx, type);
5223 case dest_local:
5224 case dest_expr:
5225 // cannot happen
5226 break;
5227 }
5228 return FAIL;
5229}
5230
5231/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005232 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005233 * "let name"
5234 * "var name = expr"
5235 * "final name = expr"
5236 * "const name = expr"
5237 * "name = expr"
5238 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005239 * Return NULL for an error.
5240 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005241 */
5242 static char_u *
5243compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5244{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005245 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005246 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005247 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005248 char_u *ret = NULL;
5249 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005250 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005251 int scriptvar_sid = 0;
5252 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005253 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005254 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005255 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005256 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005257 int oplen = 0;
5258 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005259 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005260 type_T *member_type = &t_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005261 type_T *rhs_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005262 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005263 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005264 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
5265 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005266
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005267 // Skip over the "var" or "[var, var]" to get to any "=".
5268 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5269 if (p == NULL)
5270 return *arg == '[' ? arg : NULL;
5271
5272 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005273 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005274 // TODO: should we allow this, and figure out type inference from list
5275 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005276 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005277 return NULL;
5278 }
5279
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005280 sp = p;
5281 p = skipwhite(p);
5282 op = p;
5283 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005284
5285 if (var_count > 0 && oplen == 0)
5286 // can be something like "[1, 2]->func()"
5287 return arg;
5288
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005289 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005290 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005291 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005292 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005293 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005294
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005295 if (heredoc)
5296 {
5297 list_T *l;
5298 listitem_T *li;
5299
5300 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005301 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005302 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005303 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005304 if (l == NULL)
5305 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005306
Bram Moolenaar078269b2020-09-21 20:35:55 +02005307 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005308 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005309 // Push each line and the create the list.
5310 FOR_ALL_LIST_ITEMS(l, li)
5311 {
5312 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5313 li->li_tv.vval.v_string = NULL;
5314 }
5315 generate_NEWLIST(cctx, l->lv_len);
5316 type = &t_list_string;
5317 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005318 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005319 list_free(l);
5320 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005321 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005322 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005323 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005324 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005325 char_u *wp;
5326
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005327 // for "[var, var] = expr" evaluate the expression here, loop over the
5328 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005329 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005330
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005331 wp = op + oplen;
5332 p = skipwhite(wp);
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01005333 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005334 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005335 if (compile_expr0(&p, cctx) == FAIL)
5336 return NULL;
5337 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005338
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005339 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005340 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005341 type_T *stacktype;
5342
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005343 stacktype = stack->ga_len == 0 ? &t_void
5344 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005345 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005346 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005347 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005348 goto theend;
5349 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005350 if (need_type(stacktype, &t_list_any, -1, cctx,
5351 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005352 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005353 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005354 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5355 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005356 if (stacktype->tt_member != NULL)
5357 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005358 }
5359 }
5360
5361 /*
5362 * Loop over variables in "[var, var] = expr".
5363 * For "var = expr" and "let var: type" this is done only once.
5364 */
5365 if (var_count > 0)
5366 var_start = skipwhite(arg + 1); // skip over the "["
5367 else
5368 var_start = arg;
5369 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5370 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005371 char_u *var_end;
5372 char_u *dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005373 size_t varlen;
5374 int new_local = FALSE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005375 assign_dest_T dest = dest_local;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005376 int opt_flags = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005377 int vimvaridx = -1;
Bram Moolenaar709664c2020-12-12 14:33:41 +01005378 lvar_T local_lvar;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005379 lvar_T *lvar = NULL;
5380 lvar_T arg_lvar;
5381 int has_type = FALSE;
5382 int has_index = FALSE;
5383 int instr_count = -1;
5384
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005385 // "dest_end" is the end of the destination, including "[expr]" or
5386 // ".name".
5387 // "var_end" is the end of the variable/option/etc. name.
5388 dest_end = skip_var_one(var_start, FALSE);
Bram Moolenaar65821722020-08-02 18:58:54 +02005389 if (*var_start == '@')
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005390 var_end = var_start + 2;
Bram Moolenaar65821722020-08-02 18:58:54 +02005391 else
5392 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02005393 // skip over the leading "&", "&l:", "&g:" and "$"
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005394 var_end = skip_option_env_lead(var_start);
5395 var_end = to_name_end(var_end, TRUE);
Bram Moolenaar65821722020-08-02 18:58:54 +02005396 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005397
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005398 // "a: type" is declaring variable "a" with a type, not dict "a:".
5399 if (is_decl && dest_end == var_start + 2 && dest_end[-1] == ':')
5400 --dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005401 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5402 --var_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005403
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005404 // compute the length of the destination without "[expr]" or ".name"
5405 varlen = var_end - var_start;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005406 vim_free(name);
5407 name = vim_strnsave(var_start, varlen);
5408 if (name == NULL)
5409 return NULL;
5410 if (!heredoc)
5411 type = &t_any;
5412
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005413 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005414 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005415 int declare_error = FALSE;
5416
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005417 if (get_var_dest(name, &dest, cmdidx, &opt_flags,
5418 &vimvaridx, &type, cctx) == FAIL)
5419 goto theend;
5420 if (dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005421 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005422 // Specific kind of variable recognized.
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005423 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005424 }
5425 else
5426 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005427 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005428
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005429 // No specific kind of variable recognized, just a name.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005430 for (idx = 0; reserved[idx] != NULL; ++idx)
5431 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005432 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005433 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005434 goto theend;
5435 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005436
Bram Moolenaar709664c2020-12-12 14:33:41 +01005437
5438 if (lookup_local(var_start, varlen, &local_lvar, cctx) == OK)
5439 lvar = &local_lvar;
5440 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005441 {
5442 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005443 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005444 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5445 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005446 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005447 if (is_decl)
5448 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005449 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005450 goto theend;
5451 }
5452 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005453 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005454 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005455 if (lvar != NULL)
5456 {
5457 if (is_decl)
5458 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005459 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005460 goto theend;
5461 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005462 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005463 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005464 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005465 int script_namespace = varlen > 1
5466 && STRNCMP(var_start, "s:", 2) == 0;
5467 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005468 ? script_var_exists(var_start + 2, varlen - 2,
5469 FALSE, cctx)
5470 : script_var_exists(var_start, varlen,
5471 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005472 imported_T *import =
5473 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005474
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005475 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005476 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005477 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5478
5479 if (is_decl)
5480 {
5481 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005482 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005483 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005484 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005485 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005486 name);
5487 goto theend;
5488 }
5489 else if (cctx->ctx_ufunc->uf_script_ctx_version
5490 == SCRIPT_VERSION_VIM9
5491 && script_namespace
5492 && !script_var && import == NULL)
5493 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005494 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005495 goto theend;
5496 }
5497
5498 dest = dest_script;
5499
5500 // existing script-local variables should have a type
5501 scriptvar_sid = current_sctx.sc_sid;
5502 if (import != NULL)
5503 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005504 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005505 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005506 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005507 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005508 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005509 {
5510 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5511 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005512 ((svar_T *)si->sn_var_vals.ga_data)
5513 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005514 type = sv->sv_type;
5515 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005516 }
5517 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005518 else if (check_defined(var_start, varlen, cctx) == FAIL)
5519 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005520 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005521 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005522
5523 if (declare_error)
5524 {
5525 vim9_declare_error(name);
5526 goto theend;
5527 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005528 }
5529
5530 // handle "a:name" as a name, not index "name" on "a"
5531 if (varlen > 1 || var_start[varlen] != ':')
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005532 var_end = dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005533
5534 if (dest != dest_option)
5535 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005536 if (is_decl && *var_end == ':')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005537 {
5538 // parse optional type: "let var: type = expr"
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005539 if (!VIM_ISWHITE(var_end[1]))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005540 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005541 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005542 goto theend;
5543 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005544 p = skipwhite(var_end + 1);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005545 type = parse_type(&p, cctx->ctx_type_list);
5546 has_type = TRUE;
5547 }
5548 else if (lvar != NULL)
5549 type = lvar->lv_type;
5550 }
5551
5552 if (oplen == 3 && !heredoc && dest != dest_global
5553 && type->tt_type != VAR_STRING
5554 && type->tt_type != VAR_ANY)
5555 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005556 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005557 goto theend;
5558 }
5559
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005560 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005561 {
5562 if (oplen > 1 && !heredoc)
5563 {
5564 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005565 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005566 goto theend;
5567 }
5568
5569 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005570 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5571 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005572 goto theend;
5573 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005574 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005575 if (lvar == NULL)
5576 goto theend;
5577 new_local = TRUE;
5578 }
5579
5580 member_type = type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005581 if (dest_end > var_start + varlen)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005582 {
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005583 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005584 // TODO: should we also handle "->func()" here?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005585 if (is_decl)
5586 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005587 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005588 goto theend;
5589 }
5590
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005591 if (var_start[varlen] == '[' || var_start[varlen] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005592 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005593 char_u *after = var_start + varlen;
5594
5595 // Only the last index is used below, if there are others
5596 // before it generate code for the expression. Thus for
5597 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5598 for (;;)
5599 {
5600 p = skip_index(after);
5601 if (*p != '[' && *p != '.')
5602 break;
5603 after = p;
5604 }
5605 if (after > var_start + varlen)
5606 {
5607 varlen = after - var_start;
5608 dest = dest_expr;
5609 // We don't know the type before evaluating the expression,
5610 // use "any" until then.
5611 type = &t_any;
5612 }
5613
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005614 has_index = TRUE;
5615 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005616 member_type = &t_any;
5617 else
5618 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005619 }
5620 else
5621 {
5622 semsg("Not supported yet: %s", var_start);
5623 goto theend;
5624 }
5625 }
5626 else if (lvar == &arg_lvar)
5627 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005628 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005629 goto theend;
5630 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005631 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5632 {
5633 semsg(_(e_cannot_assign_to_constant), name);
5634 goto theend;
5635 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005636
5637 if (!heredoc)
5638 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005639 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005640 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005641 if (oplen > 0 && var_count == 0)
5642 {
5643 // skip over the "=" and the expression
5644 p = skipwhite(op + oplen);
5645 compile_expr0(&p, cctx);
5646 }
5647 }
5648 else if (oplen > 0)
5649 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005650 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005651 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005652
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005653 // For "var = expr" evaluate the expression.
5654 if (var_count == 0)
5655 {
5656 int r;
5657
5658 // for "+=", "*=", "..=" etc. first load the current value
5659 if (*op != '=')
5660 {
5661 generate_loadvar(cctx, dest, name, lvar, type);
5662
5663 if (has_index)
5664 {
5665 // TODO: get member from list or dict
5666 emsg("Index with operation not supported yet");
5667 goto theend;
5668 }
5669 }
5670
5671 // Compile the expression. Temporarily hide the new local
5672 // variable here, it is not available to this expression.
5673 if (new_local)
5674 --cctx->ctx_locals.ga_len;
5675 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005676 wp = op + oplen;
5677 p = skipwhite(wp);
5678 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01005679 {
5680 if (new_local)
5681 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005682 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01005683 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005684 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005685 if (new_local)
5686 ++cctx->ctx_locals.ga_len;
5687 if (r == FAIL)
5688 goto theend;
5689 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005690 else if (semicolon && var_idx == var_count - 1)
5691 {
5692 // For "[var; var] = expr" get the rest of the list
5693 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5694 goto theend;
5695 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005696 else
5697 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005698 // For "[var, var] = expr" get the "var_idx" item from the
5699 // list.
5700 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01005701 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005702 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005703
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005704 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005705 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005706 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005707 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005708 if ((rhs_type->tt_type == VAR_FUNC
5709 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar0f769812020-09-12 18:32:34 +02005710 && var_wrong_func_name(name, TRUE))
5711 goto theend;
5712
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005713 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005714 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005715 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005716 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005717 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005718 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005719 }
5720 else
5721 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005722 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005723 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005724 if (rhs_type == &t_list_empty)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005725 lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005726 else if (rhs_type == &t_dict_empty)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005727 lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005728 else if (rhs_type == &t_unknown)
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005729 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005730 else
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005731 lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005732 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005733 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005734 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005735 {
5736 type_T *use_type = lvar->lv_type;
5737
Bram Moolenaar71abe482020-09-14 22:28:30 +02005738 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005739 if (has_index)
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005740 use_type = member_type;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005741 if (need_type(rhs_type, use_type, -1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005742 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005743 goto theend;
5744 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005745 }
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005746 else if (*p != '=' && need_type(rhs_type, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005747 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005748 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005749 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005750 else if (cmdidx == CMD_final)
5751 {
5752 emsg(_(e_final_requires_a_value));
5753 goto theend;
5754 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005755 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005756 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005757 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005758 goto theend;
5759 }
5760 else if (!has_type || dest == dest_option)
5761 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005762 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005763 goto theend;
5764 }
5765 else
5766 {
5767 // variables are always initialized
5768 if (ga_grow(instr, 1) == FAIL)
5769 goto theend;
5770 switch (member_type->tt_type)
5771 {
5772 case VAR_BOOL:
5773 generate_PUSHBOOL(cctx, VVAL_FALSE);
5774 break;
5775 case VAR_FLOAT:
5776#ifdef FEAT_FLOAT
5777 generate_PUSHF(cctx, 0.0);
5778#endif
5779 break;
5780 case VAR_STRING:
5781 generate_PUSHS(cctx, NULL);
5782 break;
5783 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005784 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005785 break;
5786 case VAR_FUNC:
5787 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5788 break;
5789 case VAR_LIST:
5790 generate_NEWLIST(cctx, 0);
5791 break;
5792 case VAR_DICT:
5793 generate_NEWDICT(cctx, 0);
5794 break;
5795 case VAR_JOB:
5796 generate_PUSHJOB(cctx, NULL);
5797 break;
5798 case VAR_CHANNEL:
5799 generate_PUSHCHANNEL(cctx, NULL);
5800 break;
5801 case VAR_NUMBER:
5802 case VAR_UNKNOWN:
5803 case VAR_ANY:
5804 case VAR_PARTIAL:
5805 case VAR_VOID:
5806 case VAR_SPECIAL: // cannot happen
5807 generate_PUSHNR(cctx, 0);
5808 break;
5809 }
5810 }
5811 if (var_count == 0)
5812 end = p;
5813 }
5814
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005815 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005816 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005817 break;
5818
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005819 if (oplen > 0 && *op != '=')
5820 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005821 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005822 type_T *stacktype;
5823
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005824 if (*op == '.')
5825 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005826 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005827 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005828 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005829 if (
5830#ifdef FEAT_FLOAT
5831 // If variable is float operation with number is OK.
5832 !(expected == &t_float && stacktype == &t_number) &&
5833#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005834 need_type(stacktype, expected, -1, cctx,
5835 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005836 goto theend;
5837
5838 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005839 {
5840 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5841 goto theend;
5842 }
5843 else if (*op == '+')
5844 {
5845 if (generate_add_instr(cctx,
5846 operator_type(member_type, stacktype),
5847 member_type, stacktype) == FAIL)
5848 goto theend;
5849 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005850 else if (generate_two_op(cctx, op) == FAIL)
5851 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005852 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005853
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005854 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005855 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005856 int r;
5857
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005858 // Compile the "idx" in "var[idx]" or "key" in "var.key".
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005859 p = var_start + varlen;
5860 if (*p == '[')
5861 {
5862 p = skipwhite(p + 1);
5863 r = compile_expr0(&p, cctx);
5864 if (r == OK && *skipwhite(p) != ']')
5865 {
5866 // this should not happen
5867 emsg(_(e_missbrac));
5868 r = FAIL;
5869 }
5870 }
5871 else // if (*p == '.')
5872 {
5873 char_u *key_end = to_name_end(p + 1, TRUE);
5874 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5875
5876 r = generate_PUSHS(cctx, key);
5877 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005878 if (r == FAIL)
5879 goto theend;
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005880
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005881 if (type == &t_any)
5882 {
5883 type_T *idx_type = ((type_T **)stack->ga_data)[
5884 stack->ga_len - 1];
5885 // Index on variable of unknown type: guess the type from the
5886 // index type: number is dict, otherwise dict.
5887 // TODO: should do the assignment at runtime
5888 if (idx_type->tt_type == VAR_NUMBER)
5889 type = &t_list_any;
5890 else
5891 type = &t_dict_any;
5892 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005893 if (type->tt_type == VAR_DICT
5894 && may_generate_2STRING(-1, cctx) == FAIL)
5895 goto theend;
5896 if (type->tt_type == VAR_LIST
5897 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005898 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005899 {
5900 emsg(_(e_number_exp));
5901 goto theend;
5902 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005903
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005904 // Load the dict or list. On the stack we then have:
5905 // - value
5906 // - index
5907 // - variable
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005908 if (dest == dest_expr)
5909 {
5910 int c = var_start[varlen];
5911
5912 // Evaluate "ll[expr]" of "ll[expr][idx]"
5913 p = var_start;
5914 var_start[varlen] = NUL;
5915 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
5916 {
5917 // this should not happen
5918 emsg(_(e_missbrac));
5919 goto theend;
5920 }
5921 var_start[varlen] = c;
5922
5923 type = stack->ga_len == 0 ? &t_void
5924 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5925 // now we can properly check the type
5926 if (type->tt_member != NULL
5927 && need_type(rhs_type, type->tt_member, -2, cctx,
5928 FALSE, FALSE) == FAIL)
5929 goto theend;
5930 }
5931 else
5932 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005933
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005934 if (type->tt_type == VAR_LIST)
5935 {
5936 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005937 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005938 }
5939 else if (type->tt_type == VAR_DICT)
5940 {
5941 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005942 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005943 }
5944 else
5945 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005946 emsg(_(e_indexable_type_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005947 goto theend;
5948 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005949 }
5950 else
5951 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005952 if (is_decl && cmdidx == CMD_const
5953 && (dest == dest_script || dest == dest_local))
5954 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005955 generate_LOCKCONST(cctx);
5956
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005957 if (dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005958 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005959 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
5960 scriptvar_idx, scriptvar_sid, type, name) == FAIL)
5961 goto theend;
5962 }
5963 else if (lvar != NULL)
5964 {
5965 isn_T *isn = ((isn_T *)instr->ga_data)
5966 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005967
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005968 // optimization: turn "var = 123" from ISN_PUSHNR +
5969 // ISN_STORE into ISN_STORENR
5970 if (!lvar->lv_from_outer
5971 && instr->ga_len == instr_count + 1
5972 && isn->isn_type == ISN_PUSHNR)
5973 {
5974 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005975
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005976 isn->isn_type = ISN_STORENR;
5977 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5978 isn->isn_arg.storenr.stnr_val = val;
5979 if (stack->ga_len > 0)
5980 --stack->ga_len;
5981 }
5982 else if (lvar->lv_from_outer)
5983 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, NULL);
5984 else
5985 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005986 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005987 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005988
5989 if (var_idx + 1 < var_count)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005990 var_start = skipwhite(dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005991 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005992
5993 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005994 if (var_count > 0 && !semicolon)
5995 {
5996 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5997 goto theend;
5998 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005999
Bram Moolenaarb2097502020-07-19 17:17:02 +02006000 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006001
6002theend:
6003 vim_free(name);
6004 return ret;
6005}
6006
6007/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006008 * Check if "name" can be "unlet".
6009 */
6010 int
6011check_vim9_unlet(char_u *name)
6012{
6013 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6014 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006015 // "unlet s:var" is allowed in legacy script.
6016 if (*name == 's' && !script_is_vim9())
6017 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006018 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006019 return FAIL;
6020 }
6021 return OK;
6022}
6023
6024/*
6025 * Callback passed to ex_unletlock().
6026 */
6027 static int
6028compile_unlet(
6029 lval_T *lvp,
6030 char_u *name_end,
6031 exarg_T *eap,
6032 int deep UNUSED,
6033 void *coookie)
6034{
6035 cctx_T *cctx = coookie;
6036
6037 if (lvp->ll_tv == NULL)
6038 {
6039 char_u *p = lvp->ll_name;
6040 int cc = *name_end;
6041 int ret = OK;
6042
6043 // Normal name. Only supports g:, w:, t: and b: namespaces.
6044 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006045 if (*p == '$')
6046 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6047 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006048 ret = FAIL;
6049 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006050 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006051
6052 *name_end = cc;
6053 return ret;
6054 }
6055
6056 // TODO: unlet {list}[idx]
6057 // TODO: unlet {dict}[key]
6058 emsg("Sorry, :unlet not fully implemented yet");
6059 return FAIL;
6060}
6061
6062/*
6063 * compile "unlet var", "lock var" and "unlock var"
6064 * "arg" points to "var".
6065 */
6066 static char_u *
6067compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6068{
6069 char_u *p = arg;
6070
6071 if (eap->cmdidx != CMD_unlet)
6072 {
6073 emsg("Sorry, :lock and unlock not implemented yet");
6074 return NULL;
6075 }
6076
6077 if (*p == '!')
6078 {
6079 p = skipwhite(p + 1);
6080 eap->forceit = TRUE;
6081 }
6082
6083 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
6084 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6085}
6086
6087/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006088 * Compile an :import command.
6089 */
6090 static char_u *
6091compile_import(char_u *arg, cctx_T *cctx)
6092{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006093 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006094}
6095
6096/*
6097 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6098 */
6099 static int
6100compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6101{
6102 garray_T *instr = &cctx->ctx_instr;
6103 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6104
6105 if (endlabel == NULL)
6106 return FAIL;
6107 endlabel->el_next = *el;
6108 *el = endlabel;
6109 endlabel->el_end_label = instr->ga_len;
6110
6111 generate_JUMP(cctx, when, 0);
6112 return OK;
6113}
6114
6115 static void
6116compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
6117{
6118 garray_T *instr = &cctx->ctx_instr;
6119
6120 while (*el != NULL)
6121 {
6122 endlabel_T *cur = (*el);
6123 isn_T *isn;
6124
6125 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
6126 isn->isn_arg.jump.jump_where = instr->ga_len;
6127 *el = cur->el_next;
6128 vim_free(cur);
6129 }
6130}
6131
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006132 static void
6133compile_free_jump_to_end(endlabel_T **el)
6134{
6135 while (*el != NULL)
6136 {
6137 endlabel_T *cur = (*el);
6138
6139 *el = cur->el_next;
6140 vim_free(cur);
6141 }
6142}
6143
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006144/*
6145 * Create a new scope and set up the generic items.
6146 */
6147 static scope_T *
6148new_scope(cctx_T *cctx, scopetype_T type)
6149{
6150 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6151
6152 if (scope == NULL)
6153 return NULL;
6154 scope->se_outer = cctx->ctx_scope;
6155 cctx->ctx_scope = scope;
6156 scope->se_type = type;
6157 scope->se_local_count = cctx->ctx_locals.ga_len;
6158 return scope;
6159}
6160
6161/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006162 * Free the current scope and go back to the outer scope.
6163 */
6164 static void
6165drop_scope(cctx_T *cctx)
6166{
6167 scope_T *scope = cctx->ctx_scope;
6168
6169 if (scope == NULL)
6170 {
6171 iemsg("calling drop_scope() without a scope");
6172 return;
6173 }
6174 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006175 switch (scope->se_type)
6176 {
6177 case IF_SCOPE:
6178 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6179 case FOR_SCOPE:
6180 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6181 case WHILE_SCOPE:
6182 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6183 case TRY_SCOPE:
6184 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6185 case NO_SCOPE:
6186 case BLOCK_SCOPE:
6187 break;
6188 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006189 vim_free(scope);
6190}
6191
6192/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006193 * compile "if expr"
6194 *
6195 * "if expr" Produces instructions:
6196 * EVAL expr Push result of "expr"
6197 * JUMP_IF_FALSE end
6198 * ... body ...
6199 * end:
6200 *
6201 * "if expr | else" Produces instructions:
6202 * EVAL expr Push result of "expr"
6203 * JUMP_IF_FALSE else
6204 * ... body ...
6205 * JUMP_ALWAYS end
6206 * else:
6207 * ... body ...
6208 * end:
6209 *
6210 * "if expr1 | elseif expr2 | else" Produces instructions:
6211 * EVAL expr Push result of "expr"
6212 * JUMP_IF_FALSE elseif
6213 * ... body ...
6214 * JUMP_ALWAYS end
6215 * elseif:
6216 * EVAL expr Push result of "expr"
6217 * JUMP_IF_FALSE else
6218 * ... body ...
6219 * JUMP_ALWAYS end
6220 * else:
6221 * ... body ...
6222 * end:
6223 */
6224 static char_u *
6225compile_if(char_u *arg, cctx_T *cctx)
6226{
6227 char_u *p = arg;
6228 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006229 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006230 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006231 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006232 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006233
Bram Moolenaara5565e42020-05-09 15:44:01 +02006234 CLEAR_FIELD(ppconst);
6235 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006236 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006237 clear_ppconst(&ppconst);
6238 return NULL;
6239 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006240 if (cctx->ctx_skip == SKIP_YES)
6241 clear_ppconst(&ppconst);
6242 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006243 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006244 int error = FALSE;
6245 int v;
6246
Bram Moolenaara5565e42020-05-09 15:44:01 +02006247 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006248 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006249 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006250 if (error)
6251 return NULL;
6252 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006253 }
6254 else
6255 {
6256 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006257 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006258 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006259 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006260 if (bool_on_stack(cctx) == FAIL)
6261 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006262 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006263
6264 scope = new_scope(cctx, IF_SCOPE);
6265 if (scope == NULL)
6266 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006267 scope->se_skip_save = skip_save;
6268 // "is_had_return" will be reset if any block does not end in :return
6269 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006270
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006271 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006272 {
6273 // "where" is set when ":elseif", "else" or ":endif" is found
6274 scope->se_u.se_if.is_if_label = instr->ga_len;
6275 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6276 }
6277 else
6278 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006279
6280 return p;
6281}
6282
6283 static char_u *
6284compile_elseif(char_u *arg, cctx_T *cctx)
6285{
6286 char_u *p = arg;
6287 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006288 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006289 isn_T *isn;
6290 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006291 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006292 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006293
6294 if (scope == NULL || scope->se_type != IF_SCOPE)
6295 {
6296 emsg(_(e_elseif_without_if));
6297 return NULL;
6298 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006299 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006300 if (!cctx->ctx_had_return)
6301 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006302
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006303 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006304 {
6305 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006306 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006307 return NULL;
6308 // previous "if" or "elseif" jumps here
6309 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6310 isn->isn_arg.jump.jump_where = instr->ga_len;
6311 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006312
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006313 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006314 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006315 if (cctx->ctx_skip == SKIP_YES)
6316 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006317 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006318 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006319 clear_ppconst(&ppconst);
6320 return NULL;
6321 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006322 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006323 if (scope->se_skip_save == SKIP_YES)
6324 clear_ppconst(&ppconst);
6325 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006326 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006327 int error = FALSE;
6328 int v;
6329
Bram Moolenaar7f141552020-05-09 17:35:53 +02006330 // The expression results in a constant.
6331 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006332 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6333 if (error)
6334 return NULL;
6335 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006336 clear_ppconst(&ppconst);
6337 scope->se_u.se_if.is_if_label = -1;
6338 }
6339 else
6340 {
6341 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006342 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006343 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006344 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006345 if (bool_on_stack(cctx) == FAIL)
6346 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006347
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006348 // "where" is set when ":elseif", "else" or ":endif" is found
6349 scope->se_u.se_if.is_if_label = instr->ga_len;
6350 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6351 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006352
6353 return p;
6354}
6355
6356 static char_u *
6357compile_else(char_u *arg, cctx_T *cctx)
6358{
6359 char_u *p = arg;
6360 garray_T *instr = &cctx->ctx_instr;
6361 isn_T *isn;
6362 scope_T *scope = cctx->ctx_scope;
6363
6364 if (scope == NULL || scope->se_type != IF_SCOPE)
6365 {
6366 emsg(_(e_else_without_if));
6367 return NULL;
6368 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006369 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006370 if (!cctx->ctx_had_return)
6371 scope->se_u.se_if.is_had_return = FALSE;
6372 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006373
Bram Moolenaarefd88552020-06-18 20:50:10 +02006374 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006375 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006376 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006377 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006378 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006379 if (!cctx->ctx_had_return
6380 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6381 JUMP_ALWAYS, cctx) == FAIL)
6382 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006383 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006384
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006385 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006386 {
6387 if (scope->se_u.se_if.is_if_label >= 0)
6388 {
6389 // previous "if" or "elseif" jumps here
6390 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6391 isn->isn_arg.jump.jump_where = instr->ga_len;
6392 scope->se_u.se_if.is_if_label = -1;
6393 }
6394 }
6395
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006396 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006397 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6398 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006399
6400 return p;
6401}
6402
6403 static char_u *
6404compile_endif(char_u *arg, cctx_T *cctx)
6405{
6406 scope_T *scope = cctx->ctx_scope;
6407 ifscope_T *ifscope;
6408 garray_T *instr = &cctx->ctx_instr;
6409 isn_T *isn;
6410
6411 if (scope == NULL || scope->se_type != IF_SCOPE)
6412 {
6413 emsg(_(e_endif_without_if));
6414 return NULL;
6415 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006416 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006417 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006418 if (!cctx->ctx_had_return)
6419 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006420
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006421 if (scope->se_u.se_if.is_if_label >= 0)
6422 {
6423 // previous "if" or "elseif" jumps here
6424 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6425 isn->isn_arg.jump.jump_where = instr->ga_len;
6426 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006427 // Fill in the "end" label in jumps at the end of the blocks.
6428 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006429 cctx->ctx_skip = scope->se_skip_save;
6430
6431 // If all the blocks end in :return and there is an :else then the
6432 // had_return flag is set.
6433 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006434
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006435 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006436 return arg;
6437}
6438
6439/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01006440 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006441 *
6442 * Produces instructions:
6443 * PUSHNR -1
6444 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01006445 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006446 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6447 * - if beyond end, jump to "end"
6448 * - otherwise get item from list and push it
6449 * STORE var Store item in "var"
6450 * ... body ...
6451 * JUMP top Jump back to repeat
6452 * end: DROP Drop the result of "expr"
6453 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006454 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
6455 * UNPACK 2 Split item in 2
6456 * STORE var1 Store item in "var1"
6457 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006458 */
6459 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006460compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006461{
Bram Moolenaar792f7862020-11-23 08:31:18 +01006462 char_u *arg;
6463 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006464 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006465 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006466 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006467 int var_count = 0;
6468 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006469 size_t varlen;
6470 garray_T *instr = &cctx->ctx_instr;
6471 garray_T *stack = &cctx->ctx_type_stack;
6472 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006473 lvar_T *loop_lvar; // loop iteration variable
6474 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006475 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006476 type_T *item_type = &t_any;
6477 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006478
Bram Moolenaar792f7862020-11-23 08:31:18 +01006479 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
6480 if (var_count == 0)
6481 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006482
6483 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006484 wp = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006485 p = skipwhite(p);
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006486 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6487 return NULL;
6488 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006489 {
6490 emsg(_(e_missing_in));
6491 return NULL;
6492 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006493 wp = p + 2;
6494 p = skipwhite(wp);
6495 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6496 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006497
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006498 scope = new_scope(cctx, FOR_SCOPE);
6499 if (scope == NULL)
6500 return NULL;
6501
Bram Moolenaar792f7862020-11-23 08:31:18 +01006502 // Reserve a variable to store the loop iteration counter and initialize it
6503 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006504 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6505 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006506 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006507 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006508 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006509 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006510 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006511 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006512
6513 // compile "expr", it remains on the stack until "endfor"
6514 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006515 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006516 {
6517 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006518 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006519 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006520 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006521
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006522 // Now that we know the type of "var", check that it is a list, now or at
6523 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006524 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006525 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006526 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006527 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006528 return NULL;
6529 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006530
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006531 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006532 {
6533 if (var_count == 1)
6534 item_type = vartype->tt_member;
6535 else if (vartype->tt_member->tt_type == VAR_LIST
6536 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
6537 item_type = vartype->tt_member->tt_member;
6538 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006539
6540 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006541 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006542 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006543
Bram Moolenaar792f7862020-11-23 08:31:18 +01006544 arg = arg_start;
6545 if (var_count > 1)
6546 {
6547 generate_UNPACK(cctx, var_count, semicolon);
6548 arg = skipwhite(arg + 1); // skip white after '['
6549
6550 // the list item is replaced by a number of items
6551 if (ga_grow(stack, var_count - 1) == FAIL)
6552 {
6553 drop_scope(cctx);
6554 return NULL;
6555 }
6556 --stack->ga_len;
6557 for (idx = 0; idx < var_count; ++idx)
6558 {
6559 ((type_T **)stack->ga_data)[stack->ga_len] =
6560 (semicolon && idx == 0) ? vartype : item_type;
6561 ++stack->ga_len;
6562 }
6563 }
6564
6565 for (idx = 0; idx < var_count; ++idx)
6566 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006567 assign_dest_T dest = dest_local;
6568 int opt_flags = 0;
6569 int vimvaridx = -1;
6570 type_T *type = &t_any;
6571
6572 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006573 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006574 name = vim_strnsave(arg, varlen);
6575 if (name == NULL)
6576 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006577
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006578 // TODO: script var not supported?
6579 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
6580 &vimvaridx, &type, cctx) == FAIL)
6581 goto failed;
6582 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006583 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006584 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
6585 0, 0, type, name) == FAIL)
6586 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006587 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006588 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006589 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01006590 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006591 {
6592 semsg(_(e_variable_already_declared), arg);
6593 goto failed;
6594 }
6595
Bram Moolenaarea870692020-12-02 14:24:30 +01006596 if (STRNCMP(name, "s:", 2) == 0)
6597 {
6598 semsg(_(e_cannot_declare_script_variable_in_function), name);
6599 goto failed;
6600 }
6601
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006602 // Reserve a variable to store "var".
6603 // TODO: check for type
6604 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6605 if (var_lvar == NULL)
6606 // out of memory or used as an argument
6607 goto failed;
6608
6609 if (semicolon && idx == var_count - 1)
6610 var_lvar->lv_type = vartype;
6611 else
6612 var_lvar->lv_type = item_type;
6613 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
6614 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006615
6616 if (*p == ',' || *p == ';')
6617 ++p;
6618 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006619 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006620 }
6621
6622 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006623
6624failed:
6625 vim_free(name);
6626 drop_scope(cctx);
6627 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006628}
6629
6630/*
6631 * compile "endfor"
6632 */
6633 static char_u *
6634compile_endfor(char_u *arg, cctx_T *cctx)
6635{
6636 garray_T *instr = &cctx->ctx_instr;
6637 scope_T *scope = cctx->ctx_scope;
6638 forscope_T *forscope;
6639 isn_T *isn;
6640
6641 if (scope == NULL || scope->se_type != FOR_SCOPE)
6642 {
6643 emsg(_(e_for));
6644 return NULL;
6645 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006646 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006647 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006648 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006649
6650 // At end of ":for" scope jump back to the FOR instruction.
6651 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6652
6653 // Fill in the "end" label in the FOR statement so it can jump here
6654 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6655 isn->isn_arg.forloop.for_end = instr->ga_len;
6656
6657 // Fill in the "end" label any BREAK statements
6658 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6659
6660 // Below the ":for" scope drop the "expr" list from the stack.
6661 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6662 return NULL;
6663
6664 vim_free(scope);
6665
6666 return arg;
6667}
6668
6669/*
6670 * compile "while expr"
6671 *
6672 * Produces instructions:
6673 * top: EVAL expr Push result of "expr"
6674 * JUMP_IF_FALSE end jump if false
6675 * ... body ...
6676 * JUMP top Jump back to repeat
6677 * end:
6678 *
6679 */
6680 static char_u *
6681compile_while(char_u *arg, cctx_T *cctx)
6682{
6683 char_u *p = arg;
6684 garray_T *instr = &cctx->ctx_instr;
6685 scope_T *scope;
6686
6687 scope = new_scope(cctx, WHILE_SCOPE);
6688 if (scope == NULL)
6689 return NULL;
6690
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006691 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006692
6693 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006694 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006695 return NULL;
6696
Bram Moolenaar13106602020-10-04 16:06:05 +02006697 if (bool_on_stack(cctx) == FAIL)
6698 return FAIL;
6699
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006700 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006701 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006702 JUMP_IF_FALSE, cctx) == FAIL)
6703 return FAIL;
6704
6705 return p;
6706}
6707
6708/*
6709 * compile "endwhile"
6710 */
6711 static char_u *
6712compile_endwhile(char_u *arg, cctx_T *cctx)
6713{
6714 scope_T *scope = cctx->ctx_scope;
6715
6716 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6717 {
6718 emsg(_(e_while));
6719 return NULL;
6720 }
6721 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006722 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006723
6724 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006725 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006726
6727 // Fill in the "end" label in the WHILE statement so it can jump here.
6728 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006729 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006730
6731 vim_free(scope);
6732
6733 return arg;
6734}
6735
6736/*
6737 * compile "continue"
6738 */
6739 static char_u *
6740compile_continue(char_u *arg, cctx_T *cctx)
6741{
6742 scope_T *scope = cctx->ctx_scope;
6743
6744 for (;;)
6745 {
6746 if (scope == NULL)
6747 {
6748 emsg(_(e_continue));
6749 return NULL;
6750 }
6751 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6752 break;
6753 scope = scope->se_outer;
6754 }
6755
6756 // Jump back to the FOR or WHILE instruction.
6757 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006758 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6759 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006760 return arg;
6761}
6762
6763/*
6764 * compile "break"
6765 */
6766 static char_u *
6767compile_break(char_u *arg, cctx_T *cctx)
6768{
6769 scope_T *scope = cctx->ctx_scope;
6770 endlabel_T **el;
6771
6772 for (;;)
6773 {
6774 if (scope == NULL)
6775 {
6776 emsg(_(e_break));
6777 return NULL;
6778 }
6779 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6780 break;
6781 scope = scope->se_outer;
6782 }
6783
6784 // Jump to the end of the FOR or WHILE loop.
6785 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006786 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006787 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006788 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006789 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6790 return FAIL;
6791
6792 return arg;
6793}
6794
6795/*
6796 * compile "{" start of block
6797 */
6798 static char_u *
6799compile_block(char_u *arg, cctx_T *cctx)
6800{
6801 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6802 return NULL;
6803 return skipwhite(arg + 1);
6804}
6805
6806/*
6807 * compile end of block: drop one scope
6808 */
6809 static void
6810compile_endblock(cctx_T *cctx)
6811{
6812 scope_T *scope = cctx->ctx_scope;
6813
6814 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006815 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006816 vim_free(scope);
6817}
6818
6819/*
6820 * compile "try"
6821 * Creates a new scope for the try-endtry, pointing to the first catch and
6822 * finally.
6823 * Creates another scope for the "try" block itself.
6824 * TRY instruction sets up exception handling at runtime.
6825 *
6826 * "try"
6827 * TRY -> catch1, -> finally push trystack entry
6828 * ... try block
6829 * "throw {exception}"
6830 * EVAL {exception}
6831 * THROW create exception
6832 * ... try block
6833 * " catch {expr}"
6834 * JUMP -> finally
6835 * catch1: PUSH exeception
6836 * EVAL {expr}
6837 * MATCH
6838 * JUMP nomatch -> catch2
6839 * CATCH remove exception
6840 * ... catch block
6841 * " catch"
6842 * JUMP -> finally
6843 * catch2: CATCH remove exception
6844 * ... catch block
6845 * " finally"
6846 * finally:
6847 * ... finally block
6848 * " endtry"
6849 * ENDTRY pop trystack entry, may rethrow
6850 */
6851 static char_u *
6852compile_try(char_u *arg, cctx_T *cctx)
6853{
6854 garray_T *instr = &cctx->ctx_instr;
6855 scope_T *try_scope;
6856 scope_T *scope;
6857
6858 // scope that holds the jumps that go to catch/finally/endtry
6859 try_scope = new_scope(cctx, TRY_SCOPE);
6860 if (try_scope == NULL)
6861 return NULL;
6862
6863 // "catch" is set when the first ":catch" is found.
6864 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006865 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006866 if (generate_instr(cctx, ISN_TRY) == NULL)
6867 return NULL;
6868
6869 // scope for the try block itself
6870 scope = new_scope(cctx, BLOCK_SCOPE);
6871 if (scope == NULL)
6872 return NULL;
6873
6874 return arg;
6875}
6876
6877/*
6878 * compile "catch {expr}"
6879 */
6880 static char_u *
6881compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6882{
6883 scope_T *scope = cctx->ctx_scope;
6884 garray_T *instr = &cctx->ctx_instr;
6885 char_u *p;
6886 isn_T *isn;
6887
6888 // end block scope from :try or :catch
6889 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6890 compile_endblock(cctx);
6891 scope = cctx->ctx_scope;
6892
6893 // Error if not in a :try scope
6894 if (scope == NULL || scope->se_type != TRY_SCOPE)
6895 {
6896 emsg(_(e_catch));
6897 return NULL;
6898 }
6899
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006900 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006901 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006902 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006903 return NULL;
6904 }
6905
6906 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006907 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006908 JUMP_ALWAYS, cctx) == FAIL)
6909 return NULL;
6910
6911 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006912 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006913 if (isn->isn_arg.try.try_catch == 0)
6914 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006915 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006916 {
6917 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006918 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006919 isn->isn_arg.jump.jump_where = instr->ga_len;
6920 }
6921
6922 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006923 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006924 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006925 scope->se_u.se_try.ts_caught_all = TRUE;
6926 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006927 }
6928 else
6929 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006930 char_u *end;
6931 char_u *pat;
6932 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006933 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006934 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006935
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006936 // Push v:exception, push {expr} and MATCH
6937 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6938
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006939 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006940 if (*end != *p)
6941 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006942 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006943 vim_free(tofree);
6944 return FAIL;
6945 }
6946 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006947 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006948 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006949 len = (int)(end - tofree);
6950 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006951 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006952 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006953 if (pat == NULL)
6954 return FAIL;
6955 if (generate_PUSHS(cctx, pat) == FAIL)
6956 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006957
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006958 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6959 return NULL;
6960
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006961 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006962 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6963 return NULL;
6964 }
6965
6966 if (generate_instr(cctx, ISN_CATCH) == NULL)
6967 return NULL;
6968
6969 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6970 return NULL;
6971 return p;
6972}
6973
6974 static char_u *
6975compile_finally(char_u *arg, cctx_T *cctx)
6976{
6977 scope_T *scope = cctx->ctx_scope;
6978 garray_T *instr = &cctx->ctx_instr;
6979 isn_T *isn;
6980
6981 // end block scope from :try or :catch
6982 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6983 compile_endblock(cctx);
6984 scope = cctx->ctx_scope;
6985
6986 // Error if not in a :try scope
6987 if (scope == NULL || scope->se_type != TRY_SCOPE)
6988 {
6989 emsg(_(e_finally));
6990 return NULL;
6991 }
6992
6993 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006994 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006995 if (isn->isn_arg.try.try_finally != 0)
6996 {
6997 emsg(_(e_finally_dup));
6998 return NULL;
6999 }
7000
7001 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007002 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007003
Bram Moolenaar585fea72020-04-02 22:33:21 +02007004 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007005 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007006 {
7007 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007008 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007009 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007010 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007011 }
7012
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007013 // TODO: set index in ts_finally_label jumps
7014
7015 return arg;
7016}
7017
7018 static char_u *
7019compile_endtry(char_u *arg, cctx_T *cctx)
7020{
7021 scope_T *scope = cctx->ctx_scope;
7022 garray_T *instr = &cctx->ctx_instr;
7023 isn_T *isn;
7024
7025 // end block scope from :catch or :finally
7026 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7027 compile_endblock(cctx);
7028 scope = cctx->ctx_scope;
7029
7030 // Error if not in a :try scope
7031 if (scope == NULL || scope->se_type != TRY_SCOPE)
7032 {
7033 if (scope == NULL)
7034 emsg(_(e_no_endtry));
7035 else if (scope->se_type == WHILE_SCOPE)
7036 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007037 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007038 emsg(_(e_endfor));
7039 else
7040 emsg(_(e_endif));
7041 return NULL;
7042 }
7043
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007044 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007045 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
7046 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007047 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007048 return NULL;
7049 }
7050
7051 // Fill in the "end" label in jumps at the end of the blocks, if not done
7052 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007053 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007054
7055 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02007056 if (isn->isn_arg.try.try_catch == 0)
7057 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007058 if (isn->isn_arg.try.try_finally == 0)
7059 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007060
7061 if (scope->se_u.se_try.ts_catch_label != 0)
7062 {
7063 // Last catch without match jumps here
7064 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7065 isn->isn_arg.jump.jump_where = instr->ga_len;
7066 }
7067
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007068 compile_endblock(cctx);
7069
7070 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
7071 return NULL;
7072 return arg;
7073}
7074
7075/*
7076 * compile "throw {expr}"
7077 */
7078 static char_u *
7079compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7080{
7081 char_u *p = skipwhite(arg);
7082
Bram Moolenaara5565e42020-05-09 15:44:01 +02007083 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007084 return NULL;
7085 if (may_generate_2STRING(-1, cctx) == FAIL)
7086 return NULL;
7087 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7088 return NULL;
7089
7090 return p;
7091}
7092
7093/*
7094 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007095 * compile "echomsg expr"
7096 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007097 * compile "execute expr"
7098 */
7099 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007100compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007101{
7102 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007103 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007104 int count = 0;
7105
7106 for (;;)
7107 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007108 if (ends_excmd2(prev, p))
7109 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007110 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007111 return NULL;
7112 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007113 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007114 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007115 }
7116
Bram Moolenaare4984292020-12-13 14:19:25 +01007117 if (count > 0)
7118 {
7119 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7120 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7121 else if (cmdidx == CMD_execute)
7122 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7123 else if (cmdidx == CMD_echomsg)
7124 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7125 else
7126 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7127 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007128 return p;
7129}
7130
7131/*
Bram Moolenaar08597872020-12-10 19:43:40 +01007132 * If "eap" has a range that is not a contstant generate an ISN_RANGE
7133 * instruction to compute it and return OK.
7134 * Otherwise return FAIL, the caller must deal with any range.
7135 */
7136 static int
7137compile_variable_range(exarg_T *eap, cctx_T *cctx)
7138{
7139 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7140 char_u *p = skipdigits(eap->cmd);
7141
7142 if (p == range_end)
7143 return FAIL;
7144 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7145}
7146
7147/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007148 * :put r
7149 * :put ={expr}
7150 */
7151 static char_u *
7152compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7153{
7154 char_u *line = arg;
7155 linenr_T lnum;
7156 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007157 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007158
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007159 eap->regname = *line;
7160
7161 if (eap->regname == '=')
7162 {
7163 char_u *p = line + 1;
7164
7165 if (compile_expr0(&p, cctx) == FAIL)
7166 return NULL;
7167 line = p;
7168 }
7169 else if (eap->regname != NUL)
7170 ++line;
7171
Bram Moolenaar08597872020-12-10 19:43:40 +01007172 if (compile_variable_range(eap, cctx) == OK)
7173 {
7174 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
7175 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007176 else
Bram Moolenaar08597872020-12-10 19:43:40 +01007177 {
7178 // Either no range or a number.
7179 // "errormsg" will not be set because the range is ADDR_LINES.
7180 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
7181 return NULL;
7182 if (eap->addr_count == 0)
7183 lnum = -1;
7184 else
7185 lnum = eap->line2;
7186 if (above)
7187 --lnum;
7188 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007189
7190 generate_PUT(cctx, eap->regname, lnum);
7191 return line;
7192}
7193
7194/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007195 * A command that is not compiled, execute with legacy code.
7196 */
7197 static char_u *
7198compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7199{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007200 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007201 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007202 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007203
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007204 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007205 goto theend;
7206
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007207 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007208 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007209 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007210 int usefilter = FALSE;
7211
7212 has_expr = argt & (EX_XFILE | EX_EXPAND);
7213
7214 // If the command can be followed by a bar, find the bar and truncate
7215 // it, so that the following command can be compiled.
7216 // The '|' is overwritten with a NUL, it is put back below.
7217 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7218 && *eap->arg == '!')
7219 // :w !filter or :r !filter or :r! filter
7220 usefilter = TRUE;
7221 if ((argt & EX_TRLBAR) && !usefilter)
7222 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007223 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007224 separate_nextcmd(eap);
7225 if (eap->nextcmd != NULL)
7226 nextcmd = eap->nextcmd;
7227 }
7228 }
7229
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007230 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
7231 {
7232 // expand filename in "syntax include [@group] filename"
7233 has_expr = TRUE;
7234 eap->arg = skipwhite(eap->arg + 7);
7235 if (*eap->arg == '@')
7236 eap->arg = skiptowhite(eap->arg);
7237 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007238
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007239 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007240 {
7241 int count = 0;
7242 char_u *start = skipwhite(line);
7243
7244 // :cmd xxx`=expr1`yyy`=expr2`zzz
7245 // PUSHS ":cmd xxx"
7246 // eval expr1
7247 // PUSHS "yyy"
7248 // eval expr2
7249 // PUSHS "zzz"
7250 // EXECCONCAT 5
7251 for (;;)
7252 {
7253 if (p > start)
7254 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007255 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007256 ++count;
7257 }
7258 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007259 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007260 return NULL;
7261 may_generate_2STRING(-1, cctx);
7262 ++count;
7263 p = skipwhite(p);
7264 if (*p != '`')
7265 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007266 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007267 return NULL;
7268 }
7269 start = p + 1;
7270
7271 p = (char_u *)strstr((char *)start, "`=");
7272 if (p == NULL)
7273 {
7274 if (*skipwhite(start) != NUL)
7275 {
7276 generate_PUSHS(cctx, vim_strsave(start));
7277 ++count;
7278 }
7279 break;
7280 }
7281 }
7282 generate_EXECCONCAT(cctx, count);
7283 }
7284 else
7285 generate_EXEC(cctx, line);
7286
7287theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007288 if (*nextcmd != NUL)
7289 {
7290 // the parser expects a pointer to the bar, put it back
7291 --nextcmd;
7292 *nextcmd = '|';
7293 }
7294
7295 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007296}
7297
7298/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007299 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007300 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007301 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007302 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007303add_def_function(ufunc_T *ufunc)
7304{
7305 dfunc_T *dfunc;
7306
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007307 if (def_functions.ga_len == 0)
7308 {
7309 // The first position is not used, so that a zero uf_dfunc_idx means it
7310 // wasn't set.
7311 if (ga_grow(&def_functions, 1) == FAIL)
7312 return FAIL;
7313 ++def_functions.ga_len;
7314 }
7315
Bram Moolenaar09689a02020-05-09 22:50:08 +02007316 // Add the function to "def_functions".
7317 if (ga_grow(&def_functions, 1) == FAIL)
7318 return FAIL;
7319 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7320 CLEAR_POINTER(dfunc);
7321 dfunc->df_idx = def_functions.ga_len;
7322 ufunc->uf_dfunc_idx = dfunc->df_idx;
7323 dfunc->df_ufunc = ufunc;
7324 ++def_functions.ga_len;
7325 return OK;
7326}
7327
7328/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007329 * After ex_function() has collected all the function lines: parse and compile
7330 * the lines into instructions.
7331 * Adds the function to "def_functions".
7332 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
7333 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007334 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007335 * This can be used recursively through compile_lambda(), which may reallocate
7336 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007337 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007338 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007339 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007340compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007341{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007342 char_u *line = NULL;
7343 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007344 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007345 cctx_T cctx;
7346 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007347 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007348 int ret = FAIL;
7349 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007350 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007351 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007352 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007353
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007354 // When using a function that was compiled before: Free old instructions.
7355 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007356 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007357 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007358 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7359 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007360 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007361 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007362 else
7363 {
7364 if (add_def_function(ufunc) == FAIL)
7365 return FAIL;
7366 new_def_function = TRUE;
7367 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007368
Bram Moolenaar985116a2020-07-12 17:31:09 +02007369 ufunc->uf_def_status = UF_COMPILING;
7370
Bram Moolenaara80faa82020-04-12 19:37:17 +02007371 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007372 cctx.ctx_ufunc = ufunc;
7373 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007374 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007375 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7376 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7377 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7378 cctx.ctx_type_list = &ufunc->uf_type_list;
7379 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7380 instr = &cctx.ctx_instr;
7381
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007382 // Set the context to the function, it may be compiled when called from
7383 // another script. Set the script version to the most modern one.
7384 // The line number will be set in next_line_from_context().
7385 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007386 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7387
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007388 // Make sure error messages are OK.
7389 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7390 if (do_estack_push)
7391 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007392 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007393
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007394 if (ufunc->uf_def_args.ga_len > 0)
7395 {
7396 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007397 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007398 int i;
7399 char_u *arg;
7400 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007401 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007402
7403 // Produce instructions for the default values of optional arguments.
7404 // Store the instruction index in uf_def_arg_idx[] so that we know
7405 // where to start when the function is called, depending on the number
7406 // of arguments.
7407 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7408 if (ufunc->uf_def_arg_idx == NULL)
7409 goto erret;
7410 for (i = 0; i < count; ++i)
7411 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007412 garray_T *stack = &cctx.ctx_type_stack;
7413 type_T *val_type;
7414 int arg_idx = first_def_arg + i;
7415
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007416 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7417 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007418 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007419 goto erret;
7420
7421 // If no type specified use the type of the default value.
7422 // Otherwise check that the default value type matches the
7423 // specified type.
7424 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7425 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007426 {
7427 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007428 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007429 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007430 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7431 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007432 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007433
7434 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007435 goto erret;
7436 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007437 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007438
7439 if (did_set_arg_type)
7440 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007441 }
7442
7443 /*
7444 * Loop over all the lines of the function and generate instructions.
7445 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007446 for (;;)
7447 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007448 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007449 int starts_with_colon = FALSE;
7450 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007451 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007452
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007453 // Bail out on the first error to avoid a flood of errors and report
7454 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007455 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007456 goto erret;
7457
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007458 if (line != NULL && *line == '|')
7459 // the line continues after a '|'
7460 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007461 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007462 && !(*line == '#' && (line == cctx.ctx_line_start
7463 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007464 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007465 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007466 goto erret;
7467 }
7468 else
7469 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007470 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007471 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007472 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007473 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007474 }
7475
Bram Moolenaara80faa82020-04-12 19:37:17 +02007476 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007477 ea.cmdlinep = &line;
7478 ea.cmd = skipwhite(line);
7479
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007480 // Some things can be recognized by the first character.
7481 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007482 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007483 case '#':
Bram Moolenaare0de1712020-12-02 17:36:54 +01007484 // "#" starts a comment
7485 line = (char_u *)"";
7486 continue;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007487
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007488 case '}':
7489 {
7490 // "}" ends a block scope
7491 scopetype_T stype = cctx.ctx_scope == NULL
7492 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007493
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007494 if (stype == BLOCK_SCOPE)
7495 {
7496 compile_endblock(&cctx);
7497 line = ea.cmd;
7498 }
7499 else
7500 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007501 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007502 goto erret;
7503 }
7504 if (line != NULL)
7505 line = skipwhite(ea.cmd + 1);
7506 continue;
7507 }
7508
7509 case '{':
7510 // "{" starts a block scope
7511 // "{'a': 1}->func() is something else
7512 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7513 {
7514 line = compile_block(ea.cmd, &cctx);
7515 continue;
7516 }
7517 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007518 }
7519
7520 /*
7521 * COMMAND MODIFIERS
7522 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007523 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007524 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7525 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007526 {
7527 if (errormsg != NULL)
7528 goto erret;
7529 // empty line or comment
7530 line = (char_u *)"";
7531 continue;
7532 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007533 generate_cmdmods(&cctx, &local_cmdmod);
7534 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007535
Bram Moolenaare88c8e82020-11-01 17:03:37 +01007536 // Check if there was a colon after the last command modifier or before
7537 // the current position.
7538 for (p = ea.cmd; p >= line; --p)
7539 {
7540 if (*p == ':')
7541 starts_with_colon = TRUE;
7542 if (p < ea.cmd && !VIM_ISWHITE(*p))
7543 break;
7544 }
7545
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007546 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007547 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007548 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007549 {
7550 if (*ea.cmd == '(')
7551 // not for "call()"
7552 ea.cmd = p;
7553 else
7554 ea.cmd = skipwhite(ea.cmd);
7555 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007556
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007557 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007558 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007559 char_u *pskip;
7560
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007561 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007562 // find what follows.
7563 // Skip over "var.member", "var[idx]" and the like.
7564 // Also "&opt = val", "$ENV = val" and "@r = val".
7565 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007566 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007567 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007568 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007569 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007570 char_u *var_end;
7571 int oplen;
7572 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007573
Bram Moolenaar65821722020-08-02 18:58:54 +02007574 if (ea.cmd[0] == '@')
7575 var_end = ea.cmd + 2;
7576 else
7577 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007578 FNE_CHECK_START | FNE_INCL_BR);
7579 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007580 if (oplen > 0)
7581 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007582 size_t len = p - ea.cmd;
7583
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007584 // Recognize an assignment if we recognize the variable
7585 // name:
7586 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007587 // "local = expr" where "local" is a local var.
7588 // "script = expr" where "script" is a script-local var.
7589 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007590 // "&opt = expr"
7591 // "$ENV = expr"
7592 // "@r = expr"
7593 if (*ea.cmd == '&'
7594 || *ea.cmd == '$'
7595 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007596 || ((len) > 2 && ea.cmd[1] == ':')
Bram Moolenaar709664c2020-12-12 14:33:41 +01007597 || lookup_local(ea.cmd, len, NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007598 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007599 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007600 || script_var_exists(ea.cmd, len,
7601 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007602 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007603 {
7604 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007605 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007606 goto erret;
Bram Moolenaarf665e972020-12-05 19:17:16 +01007607 goto nextline;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007608 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007609 }
7610 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007611
7612 if (*ea.cmd == '[')
7613 {
7614 // [var, var] = expr
7615 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7616 if (line == NULL)
7617 goto erret;
7618 if (line != ea.cmd)
Bram Moolenaarf665e972020-12-05 19:17:16 +01007619 goto nextline;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007620 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007621 }
7622
7623 /*
7624 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007625 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007626 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007627 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007628 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007629 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007630 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007631 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007632 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007633 if (!starts_with_colon)
7634 {
7635 emsg(_(e_colon_required_before_a_range));
7636 goto erret;
7637 }
7638 if (ends_excmd2(line, ea.cmd))
7639 {
7640 // A range without a command: jump to the line.
7641 // TODO: compile to a more efficient command, possibly
7642 // calling parse_cmd_address().
7643 ea.cmdidx = CMD_SIZE;
7644 line = compile_exec(line, &ea, &cctx);
7645 goto nextline;
7646 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007647 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007648 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007649 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +01007650 : (int (*)(char_u *, size_t, void *, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007651 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007652
7653 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7654 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007655 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007656 {
7657 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01007658 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007659 }
7660
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007661 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007662 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007663 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007664 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007665 iemsg("Command from find_ex_command() not handled");
7666 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007667 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007668 }
7669
Bram Moolenaar3988f642020-08-27 22:43:03 +02007670 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007671 && ea.cmdidx != CMD_elseif
7672 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007673 && ea.cmdidx != CMD_endif
7674 && ea.cmdidx != CMD_endfor
7675 && ea.cmdidx != CMD_endwhile
7676 && ea.cmdidx != CMD_catch
7677 && ea.cmdidx != CMD_finally
7678 && ea.cmdidx != CMD_endtry)
7679 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007680 emsg(_(e_unreachable_code_after_return));
7681 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007682 }
7683
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007684 p = skipwhite(p);
7685 if (ea.cmdidx != CMD_SIZE
7686 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7687 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007688 if (ea.cmdidx >= 0)
7689 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007690 if ((ea.argt & EX_BANG) && *p == '!')
7691 {
7692 ea.forceit = TRUE;
7693 p = skipwhite(p + 1);
7694 }
7695 }
7696
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007697 switch (ea.cmdidx)
7698 {
7699 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007700 ea.arg = p;
7701 line = compile_nested_function(&ea, &cctx);
7702 break;
7703
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007704 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007705 // TODO: should we allow this, e.g. to declare a global
7706 // function?
7707 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007708 goto erret;
7709
7710 case CMD_return:
7711 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007712 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007713 break;
7714
7715 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02007716 emsg(_(e_cannot_use_let_in_vim9_script));
7717 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007718 case CMD_var:
7719 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007720 case CMD_const:
7721 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007722 if (line == p)
7723 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007724 break;
7725
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007726 case CMD_unlet:
7727 case CMD_unlockvar:
7728 case CMD_lockvar:
7729 line = compile_unletlock(p, &ea, &cctx);
7730 break;
7731
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007732 case CMD_import:
7733 line = compile_import(p, &cctx);
7734 break;
7735
7736 case CMD_if:
7737 line = compile_if(p, &cctx);
7738 break;
7739 case CMD_elseif:
7740 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007741 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007742 break;
7743 case CMD_else:
7744 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007745 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007746 break;
7747 case CMD_endif:
7748 line = compile_endif(p, &cctx);
7749 break;
7750
7751 case CMD_while:
7752 line = compile_while(p, &cctx);
7753 break;
7754 case CMD_endwhile:
7755 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007756 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007757 break;
7758
7759 case CMD_for:
7760 line = compile_for(p, &cctx);
7761 break;
7762 case CMD_endfor:
7763 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007764 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007765 break;
7766 case CMD_continue:
7767 line = compile_continue(p, &cctx);
7768 break;
7769 case CMD_break:
7770 line = compile_break(p, &cctx);
7771 break;
7772
7773 case CMD_try:
7774 line = compile_try(p, &cctx);
7775 break;
7776 case CMD_catch:
7777 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007778 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007779 break;
7780 case CMD_finally:
7781 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007782 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007783 break;
7784 case CMD_endtry:
7785 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007786 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007787 break;
7788 case CMD_throw:
7789 line = compile_throw(p, &cctx);
7790 break;
7791
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007792 case CMD_eval:
7793 if (compile_expr0(&p, &cctx) == FAIL)
7794 goto erret;
7795
Bram Moolenaar3988f642020-08-27 22:43:03 +02007796 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007797 generate_instr_drop(&cctx, ISN_DROP, 1);
7798
7799 line = skipwhite(p);
7800 break;
7801
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007802 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007803 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007804 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007805 case CMD_echomsg:
7806 case CMD_echoerr:
7807 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007808 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007809
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007810 case CMD_put:
7811 ea.cmd = cmd;
7812 line = compile_put(p, &ea, &cctx);
7813 break;
7814
Bram Moolenaar3988f642020-08-27 22:43:03 +02007815 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007816
Bram Moolenaarae616492020-07-28 20:07:27 +02007817 case CMD_append:
7818 case CMD_change:
7819 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007820 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007821 case CMD_xit:
7822 not_in_vim9(&ea);
7823 goto erret;
7824
Bram Moolenaar002262f2020-07-08 17:47:57 +02007825 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007826 if (cctx.ctx_skip != SKIP_YES)
7827 {
7828 semsg(_(e_invalid_command_str), ea.cmd);
7829 goto erret;
7830 }
7831 // We don't check for a next command here.
7832 line = (char_u *)"";
7833 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007834
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007835 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007836 if (cctx.ctx_skip == SKIP_YES)
7837 {
7838 // We don't check for a next command here.
7839 line = (char_u *)"";
7840 }
7841 else
7842 {
7843 // Not recognized, execute with do_cmdline_cmd().
7844 ea.arg = p;
7845 line = compile_exec(line, &ea, &cctx);
7846 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007847 break;
7848 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007849nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007850 if (line == NULL)
7851 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007852 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007853
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007854 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02007855 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007856
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007857 if (cctx.ctx_type_stack.ga_len < 0)
7858 {
7859 iemsg("Type stack underflow");
7860 goto erret;
7861 }
7862 }
7863
7864 if (cctx.ctx_scope != NULL)
7865 {
7866 if (cctx.ctx_scope->se_type == IF_SCOPE)
7867 emsg(_(e_endif));
7868 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7869 emsg(_(e_endwhile));
7870 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7871 emsg(_(e_endfor));
7872 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007873 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007874 goto erret;
7875 }
7876
Bram Moolenaarefd88552020-06-18 20:50:10 +02007877 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007878 {
7879 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7880 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007881 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007882 goto erret;
7883 }
7884
7885 // Return zero if there is no return at the end.
7886 generate_PUSHNR(&cctx, 0);
7887 generate_instr(&cctx, ISN_RETURN);
7888 }
7889
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007890 {
7891 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7892 + ufunc->uf_dfunc_idx;
7893 dfunc->df_deleted = FALSE;
7894 dfunc->df_instr = instr->ga_data;
7895 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007896 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007897 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007898 if (cctx.ctx_outer_used)
7899 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007900 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007901 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007902
7903 ret = OK;
7904
7905erret:
7906 if (ret == FAIL)
7907 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007908 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007909 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7910 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007911
7912 for (idx = 0; idx < instr->ga_len; ++idx)
7913 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007914 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007915
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007916 // If using the last entry in the table and it was added above, we
7917 // might as well remove it.
7918 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007919 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007920 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007921 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007922 ufunc->uf_dfunc_idx = 0;
7923 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007924 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007925
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007926 while (cctx.ctx_scope != NULL)
7927 drop_scope(&cctx);
7928
Bram Moolenaar20431c92020-03-20 18:39:46 +01007929 // Don't execute this function body.
7930 ga_clear_strings(&ufunc->uf_lines);
7931
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007932 if (errormsg != NULL)
7933 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01007934 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02007935 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007936 }
7937
7938 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007939 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007940 if (do_estack_push)
7941 estack_pop();
7942
Bram Moolenaar20431c92020-03-20 18:39:46 +01007943 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007944 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007945 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007946 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007947}
7948
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007949 void
7950set_function_type(ufunc_T *ufunc)
7951{
7952 int varargs = ufunc->uf_va_name != NULL;
7953 int argcount = ufunc->uf_args.ga_len;
7954
7955 // Create a type for the function, with the return type and any
7956 // argument types.
7957 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7958 // The type is included in "tt_args".
7959 if (argcount > 0 || varargs)
7960 {
7961 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7962 argcount, &ufunc->uf_type_list);
7963 // Add argument types to the function type.
7964 if (func_type_add_arg_types(ufunc->uf_func_type,
7965 argcount + varargs,
7966 &ufunc->uf_type_list) == FAIL)
7967 return;
7968 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7969 ufunc->uf_func_type->tt_min_argcount =
7970 argcount - ufunc->uf_def_args.ga_len;
7971 if (ufunc->uf_arg_types == NULL)
7972 {
7973 int i;
7974
7975 // lambda does not have argument types.
7976 for (i = 0; i < argcount; ++i)
7977 ufunc->uf_func_type->tt_args[i] = &t_any;
7978 }
7979 else
7980 mch_memmove(ufunc->uf_func_type->tt_args,
7981 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7982 if (varargs)
7983 {
7984 ufunc->uf_func_type->tt_args[argcount] =
7985 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7986 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7987 }
7988 }
7989 else
7990 // No arguments, can use a predefined type.
7991 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7992 argcount, &ufunc->uf_type_list);
7993}
7994
7995
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007996/*
7997 * Delete an instruction, free what it contains.
7998 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007999 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008000delete_instr(isn_T *isn)
8001{
8002 switch (isn->isn_type)
8003 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008004 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008005 case ISN_EXEC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008006 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008007 case ISN_LOADENV:
8008 case ISN_LOADG:
8009 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008010 case ISN_LOADT:
8011 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008012 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008013 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008014 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008015 case ISN_RANGE:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008016 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008017 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008018 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008019 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008020 case ISN_STOREW:
8021 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008022 vim_free(isn->isn_arg.string);
8023 break;
8024
8025 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008026 case ISN_STORES:
8027 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008028 break;
8029
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008030 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008031 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008032 vim_free(isn->isn_arg.unlet.ul_name);
8033 break;
8034
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008035 case ISN_STOREOPT:
8036 vim_free(isn->isn_arg.storeopt.so_name);
8037 break;
8038
8039 case ISN_PUSHBLOB: // push blob isn_arg.blob
8040 blob_unref(isn->isn_arg.blob);
8041 break;
8042
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008043 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008044#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008045 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008046#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008047 break;
8048
8049 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008050#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008051 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008052#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008053 break;
8054
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008055 case ISN_UCALL:
8056 vim_free(isn->isn_arg.ufunc.cuf_name);
8057 break;
8058
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008059 case ISN_FUNCREF:
8060 {
8061 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8062 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008063
8064 if (func_name_refcount(dfunc->df_ufunc->uf_name))
8065 func_ptr_unref(dfunc->df_ufunc);
8066 }
8067 break;
8068
8069 case ISN_DCALL:
8070 {
8071 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8072 + isn->isn_arg.dfunc.cdf_idx;
8073
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008074 if (dfunc->df_ufunc != NULL
8075 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008076 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008077 }
8078 break;
8079
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008080 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008081 {
8082 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8083 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8084
8085 if (ufunc != NULL)
8086 {
8087 // Clear uf_dfunc_idx so that the function is deleted.
8088 clear_def_function(ufunc);
8089 ufunc->uf_dfunc_idx = 0;
8090 func_ptr_unref(ufunc);
8091 }
8092
8093 vim_free(lambda);
8094 vim_free(isn->isn_arg.newfunc.nf_global);
8095 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008096 break;
8097
Bram Moolenaar5e654232020-09-16 15:22:00 +02008098 case ISN_CHECKTYPE:
8099 free_type(isn->isn_arg.type.ct_type);
8100 break;
8101
Bram Moolenaar02194d22020-10-24 23:08:38 +02008102 case ISN_CMDMOD:
8103 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8104 ->cmod_filter_regmatch.regprog);
8105 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8106 break;
8107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008108 case ISN_2BOOL:
8109 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008110 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008111 case ISN_ADDBLOB:
8112 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008113 case ISN_ANYINDEX:
8114 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008115 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008116 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008117 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008118 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008119 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008120 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008121 case ISN_COMPAREANY:
8122 case ISN_COMPAREBLOB:
8123 case ISN_COMPAREBOOL:
8124 case ISN_COMPAREDICT:
8125 case ISN_COMPAREFLOAT:
8126 case ISN_COMPAREFUNC:
8127 case ISN_COMPARELIST:
8128 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008129 case ISN_COMPARESPECIAL:
8130 case ISN_COMPARESTRING:
8131 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008132 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008133 case ISN_DROP:
8134 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008135 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008136 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008137 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008138 case ISN_EXECCONCAT:
8139 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008140 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008141 case ISN_GETITEM:
8142 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008143 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008144 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008145 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008146 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008147 case ISN_LOADBDICT:
8148 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008149 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008150 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008151 case ISN_LOADSCRIPT:
8152 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008153 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008154 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008155 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008156 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008157 case ISN_NEGATENR:
8158 case ISN_NEWDICT:
8159 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008160 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008161 case ISN_OPFLOAT:
8162 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008163 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008164 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008165 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008166 case ISN_PUSHF:
8167 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008168 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008169 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008170 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008171 case ISN_SHUFFLE:
8172 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008173 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008174 case ISN_STOREDICT:
8175 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008176 case ISN_STORENR:
8177 case ISN_STOREOUTER:
8178 case ISN_STOREREG:
8179 case ISN_STORESCRIPT:
8180 case ISN_STOREV:
8181 case ISN_STRINDEX:
8182 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008183 case ISN_THROW:
8184 case ISN_TRY:
Bram Moolenaar792f7862020-11-23 08:31:18 +01008185 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008186 // nothing allocated
8187 break;
8188 }
8189}
8190
8191/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01008192 * Free all instructions for "dfunc".
8193 */
8194 static void
8195delete_def_function_contents(dfunc_T *dfunc)
8196{
8197 int idx;
8198
8199 ga_clear(&dfunc->df_def_args_isn);
8200
8201 if (dfunc->df_instr != NULL)
8202 {
8203 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
8204 delete_instr(dfunc->df_instr + idx);
8205 VIM_CLEAR(dfunc->df_instr);
8206 }
8207
8208 dfunc->df_deleted = TRUE;
8209}
8210
8211/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008212 * When a user function is deleted, clear the contents of any associated def
8213 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008214 */
8215 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008216clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008217{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008218 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008219 {
8220 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8221 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008222
Bram Moolenaar20431c92020-03-20 18:39:46 +01008223 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008224 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008225 }
8226}
8227
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008228/*
8229 * Used when a user function is about to be deleted: remove the pointer to it.
8230 * The entry in def_functions is then unused.
8231 */
8232 void
8233unlink_def_function(ufunc_T *ufunc)
8234{
8235 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
8236
8237 dfunc->df_ufunc = NULL;
8238}
8239
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008240#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008241/*
8242 * Free all functions defined with ":def".
8243 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008244 void
8245free_def_functions(void)
8246{
Bram Moolenaar20431c92020-03-20 18:39:46 +01008247 int idx;
8248
8249 for (idx = 0; idx < def_functions.ga_len; ++idx)
8250 {
8251 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
8252
8253 delete_def_function_contents(dfunc);
8254 }
8255
8256 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008257}
8258#endif
8259
8260
8261#endif // FEAT_EVAL