blob: bd530b1e942572d8e5bb17f39d9abbdf8ac629b5 [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 Moolenaarcdc40c42020-12-26 17:43:08 +0100148static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149
150/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100151 * Lookup variable "name" in the local scope and return it in "lvar".
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 Moolenaar8e7d6222020-12-18 19:49:56 +0100321 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200322 */
323 static int
324script_is_vim9()
325{
326 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
327}
328
329/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200330 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200331 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
332 * without "s:".
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200333 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100334 * Returns OK or FAIL.
335 */
336 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200337script_var_exists(char_u *name, size_t len, int vim9script, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100338{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200339 int is_vim9_script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200341 if (current_sctx.sc_sid <= 0)
342 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200343 is_vim9_script = script_is_vim9();
344 if (vim9script && !is_vim9_script)
Bram Moolenaar53900992020-08-22 19:02:02 +0200345 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200346 if (is_vim9_script)
347 {
348 // Check script variables that were visible where the function was
349 // defined.
350 if (find_script_var(name, len, cctx) != NULL)
351 return OK;
352 }
353 else
354 {
355 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
356 dictitem_T *di;
357 int cc;
358
359 // Check script variables that are currently visible
360 cc = name[len];
361 name[len] = NUL;
362 di = find_var_in_ht(ht, 0, name, TRUE);
363 name[len] = cc;
364 if (di != NULL)
365 return OK;
366 }
367
368 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100369}
370
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100371/*
372 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200373 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200374 * Does not check the global namespace.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100375 * Return FAIL and give an error if it defined.
376 */
377 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200378check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100379{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200380 int c = p[len];
381 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200382
383 p[len] = NUL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200384 if (script_var_exists(p, len, FALSE, cctx) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100385 || (cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100386 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200387 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200388 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200389 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100390 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200391 // A local or script-local function can shadow a global function.
392 if (ufunc == NULL || !func_is_global(ufunc)
393 || (p[0] == 'g' && p[1] == ':'))
394 {
395 p[len] = c;
396 semsg(_(e_name_already_defined_str), p);
397 return FAIL;
398 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100399 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200400 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100401 return OK;
402}
403
Bram Moolenaar65b95452020-07-19 14:03:09 +0200404
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100405/////////////////////////////////////////////////////////////////////
406// Following generate_ functions expect the caller to call ga_grow().
407
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200408#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
409#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100411/*
412 * Generate an instruction without arguments.
413 * Returns a pointer to the new instruction, NULL if failed.
414 */
415 static isn_T *
416generate_instr(cctx_T *cctx, isntype_T isn_type)
417{
418 garray_T *instr = &cctx->ctx_instr;
419 isn_T *isn;
420
Bram Moolenaar080457c2020-03-03 21:53:32 +0100421 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100422 if (ga_grow(instr, 1) == FAIL)
423 return NULL;
424 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
425 isn->isn_type = isn_type;
426 isn->isn_lnum = cctx->ctx_lnum + 1;
427 ++instr->ga_len;
428
429 return isn;
430}
431
432/*
433 * Generate an instruction without arguments.
434 * "drop" will be removed from the stack.
435 * Returns a pointer to the new instruction, NULL if failed.
436 */
437 static isn_T *
438generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
439{
440 garray_T *stack = &cctx->ctx_type_stack;
441
Bram Moolenaar080457c2020-03-03 21:53:32 +0100442 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100443 stack->ga_len -= drop;
444 return generate_instr(cctx, isn_type);
445}
446
447/*
448 * Generate instruction "isn_type" and put "type" on the type stack.
449 */
450 static isn_T *
451generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
452{
453 isn_T *isn;
454 garray_T *stack = &cctx->ctx_type_stack;
455
456 if ((isn = generate_instr(cctx, isn_type)) == NULL)
457 return NULL;
458
459 if (ga_grow(stack, 1) == FAIL)
460 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200461 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462 ++stack->ga_len;
463
464 return isn;
465}
466
467/*
468 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200469 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100470 */
471 static int
472may_generate_2STRING(int offset, cctx_T *cctx)
473{
474 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200475 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100476 garray_T *stack = &cctx->ctx_type_stack;
477 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
478
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200479 switch ((*type)->tt_type)
480 {
481 // nothing to be done
482 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100483
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200484 // conversion possible
485 case VAR_SPECIAL:
486 case VAR_BOOL:
487 case VAR_NUMBER:
488 case VAR_FLOAT:
489 break;
490
491 // conversion possible (with runtime check)
492 case VAR_ANY:
493 case VAR_UNKNOWN:
494 isntype = ISN_2STRING_ANY;
495 break;
496
497 // conversion not possible
498 case VAR_VOID:
499 case VAR_BLOB:
500 case VAR_FUNC:
501 case VAR_PARTIAL:
502 case VAR_LIST:
503 case VAR_DICT:
504 case VAR_JOB:
505 case VAR_CHANNEL:
506 to_string_error((*type)->tt_type);
507 return FAIL;
508 }
509
510 *type = &t_string;
511 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100512 return FAIL;
513 isn->isn_arg.number = offset;
514
515 return OK;
516}
517
518 static int
519check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
520{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200521 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100522 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200523 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524 {
525 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200526 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200528 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 return FAIL;
530 }
531 return OK;
532}
533
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200534 static int
535generate_add_instr(
536 cctx_T *cctx,
537 vartype_T vartype,
538 type_T *type1,
539 type_T *type2)
540{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100541 garray_T *stack = &cctx->ctx_type_stack;
542 isn_T *isn = generate_instr_drop(cctx,
543 vartype == VAR_NUMBER ? ISN_OPNR
544 : vartype == VAR_LIST ? ISN_ADDLIST
545 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200546#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100547 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200548#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100549 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200550
551 if (vartype != VAR_LIST && vartype != VAR_BLOB
552 && type1->tt_type != VAR_ANY
553 && type2->tt_type != VAR_ANY
554 && check_number_or_float(
555 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
556 return FAIL;
557
558 if (isn != NULL)
559 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100560
561 // When concatenating two lists with different member types the member type
562 // becomes "any".
563 if (vartype == VAR_LIST
564 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
565 && type1->tt_member != type2->tt_member)
566 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
567
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200568 return isn == NULL ? FAIL : OK;
569}
570
571/*
572 * Get the type to use for an instruction for an operation on "type1" and
573 * "type2". If they are matching use a type-specific instruction. Otherwise
574 * fall back to runtime type checking.
575 */
576 static vartype_T
577operator_type(type_T *type1, type_T *type2)
578{
579 if (type1->tt_type == type2->tt_type
580 && (type1->tt_type == VAR_NUMBER
581 || type1->tt_type == VAR_LIST
582#ifdef FEAT_FLOAT
583 || type1->tt_type == VAR_FLOAT
584#endif
585 || type1->tt_type == VAR_BLOB))
586 return type1->tt_type;
587 return VAR_ANY;
588}
589
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100590/*
591 * Generate an instruction with two arguments. The instruction depends on the
592 * type of the arguments.
593 */
594 static int
595generate_two_op(cctx_T *cctx, char_u *op)
596{
597 garray_T *stack = &cctx->ctx_type_stack;
598 type_T *type1;
599 type_T *type2;
600 vartype_T vartype;
601 isn_T *isn;
602
Bram Moolenaar080457c2020-03-03 21:53:32 +0100603 RETURN_OK_IF_SKIP(cctx);
604
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200605 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100606 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
607 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200608 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100609
610 switch (*op)
611 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200612 case '+':
613 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615 break;
616
617 case '-':
618 case '*':
619 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
620 op) == FAIL)
621 return FAIL;
622 if (vartype == VAR_NUMBER)
623 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
624#ifdef FEAT_FLOAT
625 else if (vartype == VAR_FLOAT)
626 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
627#endif
628 else
629 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
630 if (isn != NULL)
631 isn->isn_arg.op.op_type = *op == '*'
632 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
633 break;
634
Bram Moolenaar4c683752020-04-05 21:38:23 +0200635 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100636 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200637 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100638 && type2->tt_type != VAR_NUMBER))
639 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200640 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100641 return FAIL;
642 }
643 isn = generate_instr_drop(cctx,
644 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
645 if (isn != NULL)
646 isn->isn_arg.op.op_type = EXPR_REM;
647 break;
648 }
649
650 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200651 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100652 {
653 type_T *type = &t_any;
654
655#ifdef FEAT_FLOAT
656 // float+number and number+float results in float
657 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
658 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
659 type = &t_float;
660#endif
661 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
662 }
663
664 return OK;
665}
666
667/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200668 * Get the instruction to use for comparing "type1" with "type2"
669 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200671 static isntype_T
672get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673{
674 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675
Bram Moolenaar4c683752020-04-05 21:38:23 +0200676 if (type1 == VAR_UNKNOWN)
677 type1 = VAR_ANY;
678 if (type2 == VAR_UNKNOWN)
679 type2 = VAR_ANY;
680
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681 if (type1 == type2)
682 {
683 switch (type1)
684 {
685 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
686 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
687 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
688 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
689 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
690 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
691 case VAR_LIST: isntype = ISN_COMPARELIST; break;
692 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
693 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694 default: isntype = ISN_COMPAREANY; break;
695 }
696 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200697 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100698 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
699 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
700 isntype = ISN_COMPAREANY;
701
702 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
703 && (isntype == ISN_COMPAREBOOL
704 || isntype == ISN_COMPARESPECIAL
705 || isntype == ISN_COMPARENR
706 || isntype == ISN_COMPAREFLOAT))
707 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200708 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100709 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200710 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711 }
712 if (isntype == ISN_DROP
713 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
714 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
715 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
716 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
717 && exptype != EXPR_IS && exptype != EXPR_ISNOT
718 && (type1 == VAR_BLOB || type2 == VAR_BLOB
719 || type1 == VAR_LIST || type2 == VAR_LIST))))
720 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200721 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200723 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200725 return isntype;
726}
727
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200728 int
729check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
730{
731 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
732 return FAIL;
733 return OK;
734}
735
Bram Moolenaara5565e42020-05-09 15:44:01 +0200736/*
737 * Generate an ISN_COMPARE* instruction with a boolean result.
738 */
739 static int
740generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
741{
742 isntype_T isntype;
743 isn_T *isn;
744 garray_T *stack = &cctx->ctx_type_stack;
745 vartype_T type1;
746 vartype_T type2;
747
748 RETURN_OK_IF_SKIP(cctx);
749
750 // Get the known type of the two items on the stack. If they are matching
751 // use a type-specific instruction. Otherwise fall back to runtime type
752 // checking.
753 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
754 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
755 isntype = get_compare_isn(exptype, type1, type2);
756 if (isntype == ISN_DROP)
757 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758
759 if ((isn = generate_instr(cctx, isntype)) == NULL)
760 return FAIL;
761 isn->isn_arg.op.op_type = exptype;
762 isn->isn_arg.op.op_ic = ic;
763
764 // takes two arguments, puts one bool back
765 if (stack->ga_len >= 2)
766 {
767 --stack->ga_len;
768 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
769 }
770
771 return OK;
772}
773
774/*
775 * Generate an ISN_2BOOL instruction.
776 */
777 static int
778generate_2BOOL(cctx_T *cctx, int invert)
779{
780 isn_T *isn;
781 garray_T *stack = &cctx->ctx_type_stack;
782
Bram Moolenaar080457c2020-03-03 21:53:32 +0100783 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
785 return FAIL;
786 isn->isn_arg.number = invert;
787
788 // type becomes bool
789 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
790
791 return OK;
792}
793
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200794/*
795 * Generate an ISN_COND2BOOL instruction.
796 */
797 static int
798generate_COND2BOOL(cctx_T *cctx)
799{
800 isn_T *isn;
801 garray_T *stack = &cctx->ctx_type_stack;
802
803 RETURN_OK_IF_SKIP(cctx);
804 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
805 return FAIL;
806
807 // type becomes bool
808 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
809
810 return OK;
811}
812
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200814generate_TYPECHECK(
815 cctx_T *cctx,
816 type_T *expected,
817 int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100818{
819 isn_T *isn;
820 garray_T *stack = &cctx->ctx_type_stack;
821
Bram Moolenaar080457c2020-03-03 21:53:32 +0100822 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
824 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200825 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826 isn->isn_arg.type.ct_off = offset;
827
Bram Moolenaar5e654232020-09-16 15:22:00 +0200828 // type becomes expected
829 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100830
831 return OK;
832}
833
834/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200835 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
836 * used. Return FALSE if the types will never match.
837 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100838 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200839use_typecheck(type_T *actual, type_T *expected)
840{
841 if (actual->tt_type == VAR_ANY
842 || actual->tt_type == VAR_UNKNOWN
843 || (actual->tt_type == VAR_FUNC
844 && (expected->tt_type == VAR_FUNC
845 || expected->tt_type == VAR_PARTIAL)
846 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
847 return TRUE;
848 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
849 && actual->tt_type == expected->tt_type)
850 // This takes care of a nested list or dict.
851 return use_typecheck(actual->tt_member, expected->tt_member);
852 return FALSE;
853}
854
855/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200856 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200857 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200858 * - "actual" is a type that can be "expected" type: add a runtime check; or
859 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200860 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
861 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200862 */
863 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200864need_type(
865 type_T *actual,
866 type_T *expected,
867 int offset,
868 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200869 int silent,
870 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200871{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200872 if (expected == &t_bool && actual != &t_bool
873 && (actual->tt_flags & TTFLAG_BOOL_OK))
874 {
875 // Using "0", "1" or the result of an expression with "&&" or "||" as a
876 // boolean is OK but requires a conversion.
877 generate_2BOOL(cctx, FALSE);
878 return OK;
879 }
880
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200881 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200882 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200883
884 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200885 // If it's a constant a runtime check makes no sense.
886 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200887 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200888 generate_TYPECHECK(cctx, expected, offset);
889 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200890 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200891
892 if (!silent)
893 type_mismatch(expected, actual);
894 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200895}
896
897/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100898 * Check that the top of the type stack has a type that can be used as a
899 * condition. Give an error and return FAIL if not.
900 */
901 static int
902bool_on_stack(cctx_T *cctx)
903{
904 garray_T *stack = &cctx->ctx_type_stack;
905 type_T *type;
906
907 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
908 if (type == &t_bool)
909 return OK;
910
911 if (type == &t_any || type == &t_number)
912 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
913 // This requires a runtime type check.
914 return generate_COND2BOOL(cctx);
915
916 return need_type(type, &t_bool, -1, cctx, FALSE, FALSE);
917}
918
919/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100920 * Generate an ISN_PUSHNR instruction.
921 */
922 static int
923generate_PUSHNR(cctx_T *cctx, varnumber_T number)
924{
925 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200926 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927
Bram Moolenaar080457c2020-03-03 21:53:32 +0100928 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100929 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
930 return FAIL;
931 isn->isn_arg.number = number;
932
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200933 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200934 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +0100935 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
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;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001310 scriptref_T *sref;
1311 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312
Bram Moolenaar080457c2020-03-03 21:53:32 +01001313 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001314 if (isn_type == ISN_LOADSCRIPT)
1315 isn = generate_instr_type(cctx, isn_type, type);
1316 else
1317 isn = generate_instr_drop(cctx, isn_type, 1);
1318 if (isn == NULL)
1319 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001320
1321 // This requires three arguments, which doesn't fit in an instruction, thus
1322 // we need to allocate a struct for this.
1323 sref = ALLOC_ONE(scriptref_T);
1324 if (sref == NULL)
1325 return FAIL;
1326 isn->isn_arg.script.scriptref = sref;
1327 sref->sref_sid = sid;
1328 sref->sref_idx = idx;
1329 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001330 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331 return OK;
1332}
1333
1334/*
1335 * Generate an ISN_NEWLIST instruction.
1336 */
1337 static int
1338generate_NEWLIST(cctx_T *cctx, int count)
1339{
1340 isn_T *isn;
1341 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342 type_T *type;
1343 type_T *member;
1344
Bram Moolenaar080457c2020-03-03 21:53:32 +01001345 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001346 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1347 return FAIL;
1348 isn->isn_arg.number = count;
1349
Bram Moolenaar127542b2020-08-09 17:22:04 +02001350 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001351 if (count == 0)
1352 member = &t_void;
1353 else
1354 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001355 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1356 cctx->ctx_type_list);
1357 type = get_list_type(member, cctx->ctx_type_list);
1358
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359 // drop the value types
1360 stack->ga_len -= count;
1361
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 // add the list type to the type stack
1363 if (ga_grow(stack, 1) == FAIL)
1364 return FAIL;
1365 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1366 ++stack->ga_len;
1367
1368 return OK;
1369}
1370
1371/*
1372 * Generate an ISN_NEWDICT instruction.
1373 */
1374 static int
1375generate_NEWDICT(cctx_T *cctx, int count)
1376{
1377 isn_T *isn;
1378 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001379 type_T *type;
1380 type_T *member;
1381
Bram Moolenaar080457c2020-03-03 21:53:32 +01001382 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001383 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1384 return FAIL;
1385 isn->isn_arg.number = count;
1386
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001387 if (count == 0)
1388 member = &t_void;
1389 else
1390 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001391 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1392 cctx->ctx_type_list);
1393 type = get_dict_type(member, cctx->ctx_type_list);
1394
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395 // drop the key and value types
1396 stack->ga_len -= 2 * count;
1397
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001398 // add the dict type to the type stack
1399 if (ga_grow(stack, 1) == FAIL)
1400 return FAIL;
1401 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1402 ++stack->ga_len;
1403
1404 return OK;
1405}
1406
1407/*
1408 * Generate an ISN_FUNCREF instruction.
1409 */
1410 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001411generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412{
1413 isn_T *isn;
1414 garray_T *stack = &cctx->ctx_type_stack;
1415
Bram Moolenaar080457c2020-03-03 21:53:32 +01001416 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001417 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1418 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001419 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001420 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421
1422 if (ga_grow(stack, 1) == FAIL)
1423 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001424 ((type_T **)stack->ga_data)[stack->ga_len] =
1425 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001426 ++stack->ga_len;
1427
1428 return OK;
1429}
1430
1431/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001432 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001433 * "lambda_name" and "func_name" must be in allocated memory and will be
1434 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001435 */
1436 static int
1437generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1438{
1439 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001440
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001441 if (cctx->ctx_skip == SKIP_YES)
1442 {
1443 vim_free(lambda_name);
1444 vim_free(func_name);
1445 return OK;
1446 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001447 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001448 {
1449 vim_free(lambda_name);
1450 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001451 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001452 }
1453 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001454 isn->isn_arg.newfunc.nf_global = func_name;
1455
1456 return OK;
1457}
1458
1459/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001460 * Generate an ISN_DEF instruction: list functions
1461 */
1462 static int
1463generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1464{
1465 isn_T *isn;
1466
1467 RETURN_OK_IF_SKIP(cctx);
1468 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1469 return FAIL;
1470 if (len > 0)
1471 {
1472 isn->isn_arg.string = vim_strnsave(name, len);
1473 if (isn->isn_arg.string == NULL)
1474 return FAIL;
1475 }
1476 return OK;
1477}
1478
1479/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001480 * Generate an ISN_JUMP instruction.
1481 */
1482 static int
1483generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
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_JUMP)) == NULL)
1490 return FAIL;
1491 isn->isn_arg.jump.jump_when = when;
1492 isn->isn_arg.jump.jump_where = where;
1493
1494 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1495 --stack->ga_len;
1496
1497 return OK;
1498}
1499
1500 static int
1501generate_FOR(cctx_T *cctx, int loop_idx)
1502{
1503 isn_T *isn;
1504 garray_T *stack = &cctx->ctx_type_stack;
1505
Bram Moolenaar080457c2020-03-03 21:53:32 +01001506 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1508 return FAIL;
1509 isn->isn_arg.forloop.for_idx = loop_idx;
1510
1511 if (ga_grow(stack, 1) == FAIL)
1512 return FAIL;
1513 // type doesn't matter, will be stored next
1514 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1515 ++stack->ga_len;
1516
1517 return OK;
1518}
1519
1520/*
1521 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001522 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001523 * Return FAIL if the number of arguments is wrong.
1524 */
1525 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001526generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001527{
1528 isn_T *isn;
1529 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001530 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001531 type_T **argtypes = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001532
Bram Moolenaar080457c2020-03-03 21:53:32 +01001533 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001534 argoff = check_internal_func(func_idx, argcount);
1535 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536 return FAIL;
1537
Bram Moolenaar389df252020-07-09 21:20:47 +02001538 if (method_call && argoff > 1)
1539 {
1540 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1541 return FAIL;
1542 isn->isn_arg.shuffle.shfl_item = argcount;
1543 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1544 }
1545
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001546 if (argcount > 0)
1547 {
1548 // Check the types of the arguments.
1549 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
1550 if (internal_func_check_arg_types(argtypes, func_idx, argcount) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001551 return FAIL;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001552 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001553
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001554 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1555 return FAIL;
1556 isn->isn_arg.bfunc.cbf_idx = func_idx;
1557 isn->isn_arg.bfunc.cbf_argcount = argcount;
1558
Bram Moolenaar94738d82020-10-21 14:25:07 +02001559 // Drop the argument types and push the return type.
1560 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 if (ga_grow(stack, 1) == FAIL)
1562 return FAIL;
1563 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001564 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001565 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001566
1567 return OK;
1568}
1569
1570/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001571 * Generate an ISN_LISTAPPEND instruction. Works like add().
1572 * Argument count is already checked.
1573 */
1574 static int
1575generate_LISTAPPEND(cctx_T *cctx)
1576{
1577 garray_T *stack = &cctx->ctx_type_stack;
1578 type_T *list_type;
1579 type_T *item_type;
1580 type_T *expected;
1581
1582 // Caller already checked that list_type is a list.
1583 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1584 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1585 expected = list_type->tt_member;
1586 if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL)
1587 return FAIL;
1588
1589 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1590 return FAIL;
1591
1592 --stack->ga_len; // drop the argument
1593 return OK;
1594}
1595
1596/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001597 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1598 * Argument count is already checked.
1599 */
1600 static int
1601generate_BLOBAPPEND(cctx_T *cctx)
1602{
1603 garray_T *stack = &cctx->ctx_type_stack;
1604 type_T *item_type;
1605
1606 // Caller already checked that blob_type is a blob.
1607 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1608 if (need_type(item_type, &t_number, -1, cctx, FALSE, FALSE) == FAIL)
1609 return FAIL;
1610
1611 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1612 return FAIL;
1613
1614 --stack->ga_len; // drop the argument
1615 return OK;
1616}
1617
1618/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001619 * Generate an ISN_DCALL or ISN_UCALL instruction.
1620 * Return FAIL if the number of arguments is wrong.
1621 */
1622 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001623generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001624{
1625 isn_T *isn;
1626 garray_T *stack = &cctx->ctx_type_stack;
1627 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001628 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629
Bram Moolenaar080457c2020-03-03 21:53:32 +01001630 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001631 if (argcount > regular_args && !has_varargs(ufunc))
1632 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001633 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001634 return FAIL;
1635 }
1636 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1637 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001638 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001639 return FAIL;
1640 }
1641
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001642 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001643 {
1644 int i;
1645
1646 for (i = 0; i < argcount; ++i)
1647 {
1648 type_T *expected;
1649 type_T *actual;
1650
1651 if (i < regular_args)
1652 {
1653 if (ufunc->uf_arg_types == NULL)
1654 continue;
1655 expected = ufunc->uf_arg_types[i];
1656 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001657 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1658 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001659 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001660 else
1661 expected = ufunc->uf_va_type->tt_member;
1662 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001663 if (need_type(actual, expected, -argcount + i, cctx,
1664 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001665 {
1666 arg_type_mismatch(expected, actual, i + 1);
1667 return FAIL;
1668 }
1669 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001670 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001671 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1672 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001673 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001674 }
1675
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001676 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001677 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001678 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001679 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001680 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681 {
1682 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1683 isn->isn_arg.dfunc.cdf_argcount = argcount;
1684 }
1685 else
1686 {
1687 // A user function may be deleted and redefined later, can't use the
1688 // ufunc pointer, need to look it up again at runtime.
1689 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1690 isn->isn_arg.ufunc.cuf_argcount = argcount;
1691 }
1692
1693 stack->ga_len -= argcount; // drop the arguments
1694 if (ga_grow(stack, 1) == FAIL)
1695 return FAIL;
1696 // add return value
1697 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1698 ++stack->ga_len;
1699
1700 return OK;
1701}
1702
1703/*
1704 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1705 */
1706 static int
1707generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1708{
1709 isn_T *isn;
1710 garray_T *stack = &cctx->ctx_type_stack;
1711
Bram Moolenaar080457c2020-03-03 21:53:32 +01001712 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001713 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1714 return FAIL;
1715 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1716 isn->isn_arg.ufunc.cuf_argcount = argcount;
1717
1718 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001719 if (ga_grow(stack, 1) == FAIL)
1720 return FAIL;
1721 // add return value
1722 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1723 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001724
1725 return OK;
1726}
1727
1728/*
1729 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001730 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001731 */
1732 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001733generate_PCALL(
1734 cctx_T *cctx,
1735 int argcount,
1736 char_u *name,
1737 type_T *type,
1738 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001739{
1740 isn_T *isn;
1741 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001742 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743
Bram Moolenaar080457c2020-03-03 21:53:32 +01001744 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001745
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001746 if (type->tt_type == VAR_ANY)
1747 ret_type = &t_any;
1748 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001749 {
1750 if (type->tt_argcount != -1)
1751 {
1752 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1753
1754 if (argcount < type->tt_min_argcount - varargs)
1755 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001756 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001757 return FAIL;
1758 }
1759 if (!varargs && argcount > type->tt_argcount)
1760 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001761 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001762 return FAIL;
1763 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001764 if (type->tt_args != NULL)
1765 {
1766 int i;
1767
1768 for (i = 0; i < argcount; ++i)
1769 {
1770 int offset = -argcount + i - 1;
1771 type_T *actual = ((type_T **)stack->ga_data)[
1772 stack->ga_len + offset];
1773 type_T *expected;
1774
1775 if (varargs && i >= type->tt_min_argcount - 1)
1776 expected = type->tt_args[
1777 type->tt_min_argcount - 1]->tt_member;
1778 else
1779 expected = type->tt_args[i];
1780 if (need_type(actual, expected, offset,
1781 cctx, TRUE, FALSE) == FAIL)
1782 {
1783 arg_type_mismatch(expected, actual, i + 1);
1784 return FAIL;
1785 }
1786 }
1787 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001788 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001789 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001790 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001791 else
1792 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001793 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001794 return FAIL;
1795 }
1796
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001797 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1798 return FAIL;
1799 isn->isn_arg.pfunc.cpf_top = at_top;
1800 isn->isn_arg.pfunc.cpf_argcount = argcount;
1801
1802 stack->ga_len -= argcount; // drop the arguments
1803
1804 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001805 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001807 // If partial is above the arguments it must be cleared and replaced with
1808 // the return value.
1809 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1810 return FAIL;
1811
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001812 return OK;
1813}
1814
1815/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001816 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001817 */
1818 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001819generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820{
1821 isn_T *isn;
1822 garray_T *stack = &cctx->ctx_type_stack;
1823 type_T *type;
1824
Bram Moolenaar080457c2020-03-03 21:53:32 +01001825 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001826 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001828 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001830 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001832 if (type->tt_type != VAR_DICT && type != &t_any)
1833 {
1834 emsg(_(e_dictreq));
1835 return FAIL;
1836 }
1837 // change dict type to dict member type
1838 if (type->tt_type == VAR_DICT)
1839 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840
1841 return OK;
1842}
1843
1844/*
1845 * Generate an ISN_ECHO instruction.
1846 */
1847 static int
1848generate_ECHO(cctx_T *cctx, int with_white, int count)
1849{
1850 isn_T *isn;
1851
Bram Moolenaar080457c2020-03-03 21:53:32 +01001852 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001853 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1854 return FAIL;
1855 isn->isn_arg.echo.echo_with_white = with_white;
1856 isn->isn_arg.echo.echo_count = count;
1857
1858 return OK;
1859}
1860
Bram Moolenaarad39c092020-02-26 18:23:43 +01001861/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001862 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001863 */
1864 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001865generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001866{
1867 isn_T *isn;
1868
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001869 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001870 return FAIL;
1871 isn->isn_arg.number = count;
1872
1873 return OK;
1874}
1875
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001876/*
1877 * Generate an ISN_PUT instruction.
1878 */
1879 static int
1880generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1881{
1882 isn_T *isn;
1883
1884 RETURN_OK_IF_SKIP(cctx);
1885 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1886 return FAIL;
1887 isn->isn_arg.put.put_regname = regname;
1888 isn->isn_arg.put.put_lnum = lnum;
1889 return OK;
1890}
1891
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001892 static int
1893generate_EXEC(cctx_T *cctx, char_u *line)
1894{
1895 isn_T *isn;
1896
Bram Moolenaar080457c2020-03-03 21:53:32 +01001897 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001898 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1899 return FAIL;
1900 isn->isn_arg.string = vim_strsave(line);
1901 return OK;
1902}
1903
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001904 static int
1905generate_EXECCONCAT(cctx_T *cctx, int count)
1906{
1907 isn_T *isn;
1908
1909 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1910 return FAIL;
1911 isn->isn_arg.number = count;
1912 return OK;
1913}
1914
Bram Moolenaar08597872020-12-10 19:43:40 +01001915/*
1916 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1917 */
1918 static int
1919generate_RANGE(cctx_T *cctx, char_u *range)
1920{
1921 isn_T *isn;
1922 garray_T *stack = &cctx->ctx_type_stack;
1923
1924 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1925 return FAIL;
1926 isn->isn_arg.string = range;
1927
1928 if (ga_grow(stack, 1) == FAIL)
1929 return FAIL;
1930 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
1931 ++stack->ga_len;
1932 return OK;
1933}
1934
Bram Moolenaar792f7862020-11-23 08:31:18 +01001935 static int
1936generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
1937{
1938 isn_T *isn;
1939
1940 RETURN_OK_IF_SKIP(cctx);
1941 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
1942 return FAIL;
1943 isn->isn_arg.unpack.unp_count = var_count;
1944 isn->isn_arg.unpack.unp_semicolon = semicolon;
1945 return OK;
1946}
1947
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001948/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02001949 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001950 */
1951 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02001952generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001953{
1954 isn_T *isn;
1955
Bram Moolenaar02194d22020-10-24 23:08:38 +02001956 if (cmod->cmod_flags != 0
1957 || cmod->cmod_split != 0
1958 || cmod->cmod_verbose != 0
1959 || cmod->cmod_tab != 0
1960 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001961 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02001962 cctx->ctx_has_cmdmod = TRUE;
1963
1964 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001965 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001966 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1967 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1968 return FAIL;
1969 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001970 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02001971 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001972 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02001973
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001974 return OK;
1975}
1976
1977 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02001978generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001979{
Bram Moolenaarf665e972020-12-05 19:17:16 +01001980 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
1981 return FAIL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001982 return OK;
1983}
1984
1985/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001986 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001987 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001989 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02001990reserve_local(
1991 cctx_T *cctx,
1992 char_u *name,
1993 size_t len,
1994 int isConst,
1995 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001996{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 lvar_T *lvar;
1998
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001999 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002001 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002002 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 }
2004
2005 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002006 return NULL;
2007 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002008 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002009
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002010 // Every local variable uses the next entry on the stack. We could re-use
2011 // the last ones when leaving a scope, but then variables used in a closure
2012 // might get overwritten. To keep things simple do not re-use stack
2013 // entries. This is less efficient, but memory is cheap these days.
2014 lvar->lv_idx = cctx->ctx_locals_count++;
2015
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002016 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017 lvar->lv_const = isConst;
2018 lvar->lv_type = type;
2019
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002020 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021}
2022
2023/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002024 * Remove local variables above "new_top".
2025 */
2026 static void
2027unwind_locals(cctx_T *cctx, int new_top)
2028{
2029 if (cctx->ctx_locals.ga_len > new_top)
2030 {
2031 int idx;
2032 lvar_T *lvar;
2033
2034 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2035 {
2036 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2037 vim_free(lvar->lv_name);
2038 }
2039 }
2040 cctx->ctx_locals.ga_len = new_top;
2041}
2042
2043/*
2044 * Free all local variables.
2045 */
2046 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002047free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002048{
2049 unwind_locals(cctx, 0);
2050 ga_clear(&cctx->ctx_locals);
2051}
2052
2053/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002054 * Find "name" in script-local items of script "sid".
2055 * Returns the index in "sn_var_vals" if found.
2056 * If found but not in "sn_var_vals" returns -1.
2057 * If not found returns -2.
2058 */
2059 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002060get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002061{
2062 hashtab_T *ht;
2063 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002064 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002065 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002066 int idx;
2067
Bram Moolenaare3d46852020-08-29 13:39:17 +02002068 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002069 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002070 if (sid == current_sctx.sc_sid)
2071 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002072 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002073
2074 if (sav == NULL)
2075 return -2;
2076 idx = sav->sav_var_vals_idx;
2077 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2078 if (check_writable && sv->sv_const)
2079 semsg(_(e_readonlyvar), name);
2080 return idx;
2081 }
2082
2083 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002084 ht = &SCRIPT_VARS(sid);
2085 di = find_var_in_ht(ht, 0, name, TRUE);
2086 if (di == NULL)
2087 return -2;
2088
2089 // Now find the svar_T index in sn_var_vals.
2090 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2091 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002092 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002093 if (sv->sv_tv == &di->di_tv)
2094 {
2095 if (check_writable && sv->sv_const)
2096 semsg(_(e_readonlyvar), name);
2097 return idx;
2098 }
2099 }
2100 return -1;
2101}
2102
2103/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002104 * Find "name" in imported items of the current script or in "cctx" if not
2105 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002106 */
2107 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002108find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002109{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002110 int idx;
2111
Bram Moolenaare3d46852020-08-29 13:39:17 +02002112 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002113 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002114 if (cctx != NULL)
2115 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2116 {
2117 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2118 + idx;
2119
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002120 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2121 : STRLEN(import->imp_name) == len
2122 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123 return import;
2124 }
2125
Bram Moolenaarefa94442020-08-08 22:16:00 +02002126 return find_imported_in_script(name, len, current_sctx.sc_sid);
2127}
2128
2129 imported_T *
2130find_imported_in_script(char_u *name, size_t len, int sid)
2131{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002132 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002133 int idx;
2134
Bram Moolenaare3d46852020-08-29 13:39:17 +02002135 if (!SCRIPT_ID_VALID(sid))
2136 return NULL;
2137 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002138 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2139 {
2140 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2141
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002142 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2143 : STRLEN(import->imp_name) == len
2144 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002145 return import;
2146 }
2147 return NULL;
2148}
2149
2150/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002151 * Free all imported variables.
2152 */
2153 static void
2154free_imported(cctx_T *cctx)
2155{
2156 int idx;
2157
2158 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2159 {
2160 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2161
2162 vim_free(import->imp_name);
2163 }
2164 ga_clear(&cctx->ctx_imports);
2165}
2166
2167/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01002168 * Return TRUE if "p" points at a "#". Does not check for white space.
Bram Moolenaar23c55272020-06-21 16:58:13 +02002169 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002170 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002171vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002172{
Bram Moolenaare0de1712020-12-02 17:36:54 +01002173 return p[0] == '#';
Bram Moolenaar23c55272020-06-21 16:58:13 +02002174}
2175
2176/*
2177 * Return a pointer to the next line that isn't empty or only contains a
2178 * comment. Skips over white space.
2179 * Returns NULL if there is none.
2180 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002181 char_u *
2182peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002183{
2184 int lnum = cctx->ctx_lnum;
2185
2186 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2187 {
2188 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002189 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002190
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002191 // ignore NULLs inserted for continuation lines
2192 if (line != NULL)
2193 {
2194 p = skipwhite(line);
2195 if (*p != NUL && !vim9_comment_start(p))
2196 return p;
2197 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002198 }
2199 return NULL;
2200}
2201
2202/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002203 * Called when checking for a following operator at "arg". When the rest of
2204 * the line is empty or only a comment, peek the next line. If there is a next
2205 * line return a pointer to it and set "nextp".
2206 * Otherwise skip over white space.
2207 */
2208 static char_u *
2209may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2210{
2211 char_u *p = skipwhite(arg);
2212
2213 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002214 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002215 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002216 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002217 if (*nextp != NULL)
2218 return *nextp;
2219 }
2220 return p;
2221}
2222
2223/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002224 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002225 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002226 * Returns NULL when at the end.
2227 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002228 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002229next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002230{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002231 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002232
2233 do
2234 {
2235 ++cctx->ctx_lnum;
2236 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002237 {
2238 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002239 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002240 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002241 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002242 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002243 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002244 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002245 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002246 return line;
2247}
2248
2249/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002250 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002251 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002252 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2253 */
2254 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002255may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002256{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002257 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002258 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002259 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002260
2261 if (next == NULL)
2262 return FAIL;
2263 *arg = skipwhite(next);
2264 }
2265 return OK;
2266}
2267
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002268/*
2269 * Idem, and give an error when failed.
2270 */
2271 static int
2272may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2273{
2274 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2275 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002276 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002277 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002278 return FAIL;
2279 }
2280 return OK;
2281}
2282
2283
Bram Moolenaara5565e42020-05-09 15:44:01 +02002284// Structure passed between the compile_expr* functions to keep track of
2285// constants that have been parsed but for which no code was produced yet. If
2286// possible expressions on these constants are applied at compile time. If
2287// that is not possible, the code to push the constants needs to be generated
2288// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002289// Using 50 should be more than enough of 5 levels of ().
2290#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002291typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002292 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002293 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002294 int pp_is_const; // all generated code was constants, used for a
2295 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002296} ppconst_T;
2297
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002298static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002299static int compile_expr0(char_u **arg, cctx_T *cctx);
2300static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2301
Bram Moolenaara5565e42020-05-09 15:44:01 +02002302/*
2303 * Generate a PUSH instruction for "tv".
2304 * "tv" will be consumed or cleared.
2305 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2306 */
2307 static int
2308generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2309{
2310 if (tv != NULL)
2311 {
2312 switch (tv->v_type)
2313 {
2314 case VAR_UNKNOWN:
2315 break;
2316 case VAR_BOOL:
2317 generate_PUSHBOOL(cctx, tv->vval.v_number);
2318 break;
2319 case VAR_SPECIAL:
2320 generate_PUSHSPEC(cctx, tv->vval.v_number);
2321 break;
2322 case VAR_NUMBER:
2323 generate_PUSHNR(cctx, tv->vval.v_number);
2324 break;
2325#ifdef FEAT_FLOAT
2326 case VAR_FLOAT:
2327 generate_PUSHF(cctx, tv->vval.v_float);
2328 break;
2329#endif
2330 case VAR_BLOB:
2331 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2332 tv->vval.v_blob = NULL;
2333 break;
2334 case VAR_STRING:
2335 generate_PUSHS(cctx, tv->vval.v_string);
2336 tv->vval.v_string = NULL;
2337 break;
2338 default:
2339 iemsg("constant type not supported");
2340 clear_tv(tv);
2341 return FAIL;
2342 }
2343 tv->v_type = VAR_UNKNOWN;
2344 }
2345 return OK;
2346}
2347
2348/*
2349 * Generate code for any ppconst entries.
2350 */
2351 static int
2352generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2353{
2354 int i;
2355 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002356 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002357
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002358 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002359 for (i = 0; i < ppconst->pp_used; ++i)
2360 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2361 ret = FAIL;
2362 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002363 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002364 return ret;
2365}
2366
2367/*
2368 * Clear ppconst constants. Used when failing.
2369 */
2370 static void
2371clear_ppconst(ppconst_T *ppconst)
2372{
2373 int i;
2374
2375 for (i = 0; i < ppconst->pp_used; ++i)
2376 clear_tv(&ppconst->pp_tv[i]);
2377 ppconst->pp_used = 0;
2378}
2379
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002380/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002381 * Generate an instruction to load script-local variable "name", without the
2382 * leading "s:".
2383 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384 */
2385 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002386compile_load_scriptvar(
2387 cctx_T *cctx,
2388 char_u *name, // variable NUL terminated
2389 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002390 char_u **end, // end of variable
2391 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002392{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002393 scriptitem_T *si;
2394 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395 imported_T *import;
2396
Bram Moolenaare3d46852020-08-29 13:39:17 +02002397 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2398 return FAIL;
2399 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002400 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002401 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002403 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002404 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2405 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002406 }
2407 if (idx >= 0)
2408 {
2409 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2410
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002411 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002412 current_sctx.sc_sid, idx, sv->sv_type);
2413 return OK;
2414 }
2415
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002416 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417 if (import != NULL)
2418 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002419 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002420 {
2421 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002422 char_u *exp_name;
2423 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002424 ufunc_T *ufunc;
2425 type_T *type;
2426
2427 // Used "import * as Name", need to lookup the member.
2428 if (*p != '.')
2429 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002430 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002431 return FAIL;
2432 }
2433 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002434 if (VIM_ISWHITE(*p))
2435 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002436 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002437 return FAIL;
2438 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002439
Bram Moolenaar1c991142020-07-04 13:15:31 +02002440 // isolate one name
2441 exp_name = p;
2442 while (eval_isnamec(*p))
2443 ++p;
2444 cc = *p;
2445 *p = NUL;
2446
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002447 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002448 *p = cc;
2449 p = skipwhite(p);
2450
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002451 // TODO: what if it is a function?
2452 if (idx < 0)
2453 return FAIL;
2454 *end = p;
2455
2456 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2457 import->imp_sid,
2458 idx,
2459 type);
2460 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002461 else if (import->imp_funcname != NULL)
2462 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002463 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002464 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2465 import->imp_sid,
2466 import->imp_var_vals_idx,
2467 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002468 return OK;
2469 }
2470
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002471 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002472 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 return FAIL;
2474}
2475
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002476 static int
2477generate_funcref(cctx_T *cctx, char_u *name)
2478{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002479 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002480
2481 if (ufunc == NULL)
2482 return FAIL;
2483
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002484 // Need to compile any default values to get the argument types.
2485 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2486 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2487 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002488 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002489}
2490
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002491/*
2492 * Compile a variable name into a load instruction.
2493 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002494 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 * When "error" is FALSE do not give an error when not found.
2496 */
2497 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002498compile_load(
2499 char_u **arg,
2500 char_u *end_arg,
2501 cctx_T *cctx,
2502 int is_expr,
2503 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002504{
2505 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002506 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002507 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002508 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002509 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510
2511 if (*(*arg + 1) == ':')
2512 {
2513 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002514 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002515 {
2516 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002518 switch (**arg)
2519 {
2520 case 'g': isn_type = ISN_LOADGDICT; break;
2521 case 'w': isn_type = ISN_LOADWDICT; break;
2522 case 't': isn_type = ISN_LOADTDICT; break;
2523 case 'b': isn_type = ISN_LOADBDICT; break;
2524 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002525 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002526 goto theend;
2527 }
2528 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2529 goto theend;
2530 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002531 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 else
2533 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002534 isntype_T isn_type = ISN_DROP;
2535
2536 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2537 if (name == NULL)
2538 return FAIL;
2539
2540 switch (**arg)
2541 {
2542 case 'v': res = generate_LOADV(cctx, name, error);
2543 break;
2544 case 's': res = compile_load_scriptvar(cctx, name,
2545 NULL, NULL, error);
2546 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002547 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2548 isn_type = ISN_LOADG;
2549 else
2550 {
2551 isn_type = ISN_LOADAUTO;
2552 vim_free(name);
2553 name = vim_strnsave(*arg, end - *arg);
2554 if (name == NULL)
2555 return FAIL;
2556 }
2557 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002558 case 'w': isn_type = ISN_LOADW; break;
2559 case 't': isn_type = ISN_LOADT; break;
2560 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002561 default: // cannot happen, just in case
2562 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002563 goto theend;
2564 }
2565 if (isn_type != ISN_DROP)
2566 {
2567 // Global, Buffer-local, Window-local and Tabpage-local
2568 // variables can be defined later, thus we don't check if it
2569 // exists, give error at runtime.
2570 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2571 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 }
2573 }
2574 else
2575 {
2576 size_t len = end - *arg;
2577 int idx;
2578 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002579 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002580
2581 name = vim_strnsave(*arg, end - *arg);
2582 if (name == NULL)
2583 return FAIL;
2584
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002585 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002587 if (!gen_load_outer)
2588 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 }
2590 else
2591 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002592 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002593
Bram Moolenaar709664c2020-12-12 14:33:41 +01002594 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002596 type = lvar.lv_type;
2597 idx = lvar.lv_idx;
2598 if (lvar.lv_from_outer)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002599 gen_load_outer = TRUE;
2600 else
2601 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 }
2603 else
2604 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002605 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002606 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002607 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002608 || find_imported(name, 0, cctx) != NULL)
2609 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002610
Bram Moolenaar0f769812020-09-12 18:32:34 +02002611 // When evaluating an expression and the name starts with an
2612 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002613 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002614 if (res == FAIL && is_expr
2615 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002616 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002617 }
2618 }
2619 if (gen_load)
2620 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002621 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002622 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002623 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002624 cctx->ctx_outer_used = TRUE;
2625 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002626 }
2627
2628 *arg = end;
2629
2630theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002631 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002632 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633 vim_free(name);
2634 return res;
2635}
2636
2637/*
2638 * Compile the argument expressions.
2639 * "arg" points to just after the "(" and is advanced to after the ")"
2640 */
2641 static int
2642compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2643{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002644 char_u *p = *arg;
2645 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002646 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002647
Bram Moolenaare6085c52020-04-12 20:19:16 +02002648 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002650 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2651 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002652 if (*p == ')')
2653 {
2654 *arg = p + 1;
2655 return OK;
2656 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002657 if (must_end)
2658 {
2659 semsg(_(e_missing_comma_before_argument_str), p);
2660 return FAIL;
2661 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002662
Bram Moolenaara5565e42020-05-09 15:44:01 +02002663 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002664 return FAIL;
2665 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002666
2667 if (*p != ',' && *skipwhite(p) == ',')
2668 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002669 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002670 p = skipwhite(p);
2671 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002673 {
2674 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002675 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002676 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002677 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002678 else
2679 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002680 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002681 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002682 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002683failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002684 emsg(_(e_missing_close));
2685 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002686}
2687
2688/*
2689 * Compile a function call: name(arg1, arg2)
2690 * "arg" points to "name", "arg + varlen" to the "(".
2691 * "argcount_init" is 1 for "value->method()"
2692 * Instructions:
2693 * EVAL arg1
2694 * EVAL arg2
2695 * BCALL / DCALL / UCALL
2696 */
2697 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002698compile_call(
2699 char_u **arg,
2700 size_t varlen,
2701 cctx_T *cctx,
2702 ppconst_T *ppconst,
2703 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704{
2705 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002706 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707 int argcount = argcount_init;
2708 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002709 char_u fname_buf[FLEN_FIXED + 1];
2710 char_u *tofree = NULL;
2711 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002712 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002713 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002714 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002715
Bram Moolenaara5565e42020-05-09 15:44:01 +02002716 // we can evaluate "has('name')" at compile time
2717 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2718 {
2719 char_u *s = skipwhite(*arg + varlen + 1);
2720 typval_T argvars[2];
2721
2722 argvars[0].v_type = VAR_UNKNOWN;
2723 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002724 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002725 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002726 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002727 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002728 if (*s == ')' && argvars[0].v_type == VAR_STRING
2729 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002730 {
2731 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2732
2733 *arg = s + 1;
2734 argvars[1].v_type = VAR_UNKNOWN;
2735 tv->v_type = VAR_NUMBER;
2736 tv->vval.v_number = 0;
2737 f_has(argvars, tv);
2738 clear_tv(&argvars[0]);
2739 ++ppconst->pp_used;
2740 return OK;
2741 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002742 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002743 }
2744
2745 if (generate_ppconst(cctx, ppconst) == FAIL)
2746 return FAIL;
2747
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002748 if (varlen >= sizeof(namebuf))
2749 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002750 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 return FAIL;
2752 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002753 vim_strncpy(namebuf, *arg, varlen);
2754 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002755
2756 *arg = skipwhite(*arg + varlen + 1);
2757 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002758 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002759
Bram Moolenaar03290b82020-12-19 16:30:44 +01002760 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002761 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 {
2763 int idx;
2764
2765 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002766 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002767 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002768 {
2769 if (STRCMP(name, "add") == 0 && argcount == 2)
2770 {
2771 garray_T *stack = &cctx->ctx_type_stack;
2772 type_T *type = ((type_T **)stack->ga_data)[
2773 stack->ga_len - 2];
2774
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002775 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002776 if (type->tt_type == VAR_LIST)
2777 {
2778 // inline "add(list, item)" so that the type can be checked
2779 res = generate_LISTAPPEND(cctx);
2780 idx = -1;
2781 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002782 else if (type->tt_type == VAR_BLOB)
2783 {
2784 // inline "add(blob, nr)" so that the type can be checked
2785 res = generate_BLOBAPPEND(cctx);
2786 idx = -1;
2787 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002788 }
2789
2790 if (idx >= 0)
2791 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2792 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002793 else
2794 semsg(_(e_unknownfunc), namebuf);
2795 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796 }
2797
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002798 // An argument or local variable can be a function reference, this
2799 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002800 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002801 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002802 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002803 // If we can find the function by name generate the right call.
2804 // Skip global functions here, a local funcref takes precedence.
2805 ufunc = find_func(name, FALSE, cctx);
2806 if (ufunc != NULL && !func_is_global(ufunc))
2807 {
2808 res = generate_CALL(cctx, ufunc, argcount);
2809 goto theend;
2810 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002811 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002812
2813 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002814 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002815 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002817 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002818 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002819 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002820 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01002821 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002822
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002823 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002824 goto theend;
2825 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826
Bram Moolenaar0f769812020-09-12 18:32:34 +02002827 // If we can find a global function by name generate the right call.
2828 if (ufunc != NULL)
2829 {
2830 res = generate_CALL(cctx, ufunc, argcount);
2831 goto theend;
2832 }
2833
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002834 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002835 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002836 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002837 res = generate_UCALL(cctx, name, argcount);
2838 else
2839 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002840
2841theend:
2842 vim_free(tofree);
2843 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002844}
2845
2846// like NAMESPACE_CHAR but with 'a' and 'l'.
2847#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2848
2849/*
2850 * Find the end of a variable or function name. Unlike find_name_end() this
2851 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002852 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 * Return a pointer to just after the name. Equal to "arg" if there is no
2854 * valid name.
2855 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01002856 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002857to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858{
2859 char_u *p;
2860
2861 // Quick check for valid starting character.
2862 if (!eval_isnamec1(*arg))
2863 return arg;
2864
2865 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2866 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2867 // and can be used in slice "[n:]".
2868 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002869 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2871 break;
2872 return p;
2873}
2874
2875/*
2876 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002877 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002878 */
2879 char_u *
2880to_name_const_end(char_u *arg)
2881{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002882 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883 typval_T rettv;
2884
2885 if (p == arg && *arg == '[')
2886 {
2887
2888 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002889 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890 p = arg;
2891 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 return p;
2893}
2894
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002895/*
2896 * parse a list: [expr, expr]
2897 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002898 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899 */
2900 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002901compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902{
2903 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002904 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002905 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002906 int is_const;
2907 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002908
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002909 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002911 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002912 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002913 semsg(_(e_list_end), *arg);
2914 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002915 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002916 if (*p == ',')
2917 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002918 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002919 return FAIL;
2920 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002921 if (*p == ']')
2922 {
2923 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002924 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002925 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002926 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002927 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002928 if (!is_const)
2929 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 ++count;
2931 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002932 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002934 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2935 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002936 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002937 return FAIL;
2938 }
2939 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002940 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002941 p = skipwhite(p);
2942 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002943 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002945 ppconst->pp_is_const = is_all_const;
2946 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947}
2948
2949/*
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01002950 * parse a lambda: "{arg, arg -> expr}" or "(arg, arg) => expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951 * "*arg" points to the '{'.
2952 */
2953 static int
2954compile_lambda(char_u **arg, cctx_T *cctx)
2955{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956 typval_T rettv;
2957 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002958 evalarg_T evalarg;
2959
2960 CLEAR_FIELD(evalarg);
2961 evalarg.eval_flags = EVAL_EVALUATE;
2962 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963
2964 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002965 if (get_lambda_tv(arg, &rettv, TRUE, &evalarg) != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002966 {
2967 clear_evalarg(&evalarg, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 return FAIL;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002969 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01002970
Bram Moolenaar65c44152020-12-24 15:14:01 +01002971 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002973 ++ufunc->uf_refcount;
2974 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975
Bram Moolenaar65c44152020-12-24 15:14:01 +01002976 // Compile the function into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002977 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002979 clear_evalarg(&evalarg, NULL);
2980
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002981 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002982 {
2983 // The return type will now be known.
2984 set_function_type(ufunc);
2985
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002986 // The function reference count will be 1. When the ISN_FUNCREF
2987 // instruction is deleted the reference count is decremented and the
2988 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002989 return generate_FUNCREF(cctx, ufunc);
2990 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002991
2992 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 return FAIL;
2994}
2995
2996/*
2997 * Compile a lamda call: expr->{lambda}(args)
2998 * "arg" points to the "{".
2999 */
3000 static int
3001compile_lambda_call(char_u **arg, cctx_T *cctx)
3002{
3003 ufunc_T *ufunc;
3004 typval_T rettv;
3005 int argcount = 1;
3006 int ret = FAIL;
3007
3008 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003009 if (get_lambda_tv(arg, &rettv, TRUE, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 return FAIL;
3011
3012 if (**arg != '(')
3013 {
3014 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003015 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 else
3017 semsg(_(e_missing_paren), "lambda");
3018 clear_tv(&rettv);
3019 return FAIL;
3020 }
3021
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 ufunc = rettv.vval.v_partial->pt_func;
3023 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003024 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003025 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003026
Bram Moolenaara05e5242020-09-19 18:19:19 +02003027 // The function will have one line: "return {expr}". Compile it into
3028 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003029 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030
3031 // compile the arguments
3032 *arg = skipwhite(*arg + 1);
3033 if (compile_arguments(arg, cctx, &argcount) == OK)
3034 // call the compiled function
3035 ret = generate_CALL(cctx, ufunc, argcount);
3036
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003037 if (ret == FAIL)
3038 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 return ret;
3040}
3041
3042/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003043 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003045 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 */
3047 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003048compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049{
3050 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003051 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052 int count = 0;
3053 dict_T *d = dict_alloc();
3054 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003055 char_u *whitep = *arg;
3056 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003057 int is_const;
3058 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059
3060 if (d == NULL)
3061 return FAIL;
3062 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003063 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003065 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066
Bram Moolenaar23c55272020-06-21 16:58:13 +02003067 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003068 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003069 *arg = NULL;
3070 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003071 }
3072
3073 if (**arg == '}')
3074 break;
3075
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003076 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003077 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003078 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003080 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003081 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003082 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3085 if (isn->isn_type == ISN_PUSHS)
3086 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003087 else
3088 {
3089 type_T *keytype = ((type_T **)stack->ga_data)
Bram Moolenaar2bede172020-11-19 18:53:18 +01003090 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003091 if (need_type(keytype, &t_string, -1, cctx,
Bram Moolenaar2bede172020-11-19 18:53:18 +01003092 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003093 return FAIL;
3094 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003095 *arg = skipwhite(*arg);
3096 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003097 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003098 emsg(_(e_missing_matching_bracket_after_dict_key));
3099 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003100 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003101 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003103 else
3104 {
3105 // {"name": value},
3106 // {'name': value},
3107 // {name: value} use "name" as a literal key
3108 key = get_literal_key(arg);
3109 if (key == NULL)
3110 return FAIL;
3111 if (generate_PUSHS(cctx, key) == FAIL)
3112 return FAIL;
3113 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114
3115 // Check for duplicate keys, if using string keys.
3116 if (key != NULL)
3117 {
3118 item = dict_find(d, key, -1);
3119 if (item != NULL)
3120 {
3121 semsg(_(e_duplicate_key), key);
3122 goto failret;
3123 }
3124 item = dictitem_alloc(key);
3125 if (item != NULL)
3126 {
3127 item->di_tv.v_type = VAR_UNKNOWN;
3128 item->di_tv.v_lock = 0;
3129 if (dict_add(d, item) == FAIL)
3130 dictitem_free(item);
3131 }
3132 }
3133
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 if (**arg != ':')
3135 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003136 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003137 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003138 else
3139 semsg(_(e_missing_dict_colon), *arg);
3140 return FAIL;
3141 }
3142 whitep = *arg + 1;
3143 if (!IS_WHITE_OR_NUL(*whitep))
3144 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003145 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 return FAIL;
3147 }
3148
3149 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003150 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003151 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003152 *arg = NULL;
3153 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003154 }
3155
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003156 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003158 if (!is_const)
3159 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160 ++count;
3161
Bram Moolenaar2c330432020-04-13 14:41:35 +02003162 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003163 *arg = skipwhite(*arg);
3164 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003165 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003166 *arg = NULL;
3167 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003168 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 if (**arg == '}')
3170 break;
3171 if (**arg != ',')
3172 {
3173 semsg(_(e_missing_dict_comma), *arg);
3174 goto failret;
3175 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003176 if (IS_WHITE_OR_NUL(*whitep))
3177 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003178 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003179 return FAIL;
3180 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003181 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003182 if (!IS_WHITE_OR_NUL(*whitep))
3183 {
3184 semsg(_(e_white_space_required_after_str), ",");
3185 return FAIL;
3186 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 *arg = skipwhite(*arg + 1);
3188 }
3189
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003190 *arg = *arg + 1;
3191
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003192 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003193 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003194 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003195 *arg += STRLEN(*arg);
3196
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003198 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 return generate_NEWDICT(cctx, count);
3200
3201failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003202 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003203 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003204 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003205 *arg = (char_u *)"";
3206 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207 dict_unref(d);
3208 return FAIL;
3209}
3210
3211/*
3212 * Compile "&option".
3213 */
3214 static int
3215compile_get_option(char_u **arg, cctx_T *cctx)
3216{
3217 typval_T rettv;
3218 char_u *start = *arg;
3219 int ret;
3220
3221 // parse the option and get the current value to get the type.
3222 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003223 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 if (ret == OK)
3225 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003226 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003227 char_u *name = vim_strnsave(start, *arg - start);
3228 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3229
3230 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3231 vim_free(name);
3232 }
3233 clear_tv(&rettv);
3234
3235 return ret;
3236}
3237
3238/*
3239 * Compile "$VAR".
3240 */
3241 static int
3242compile_get_env(char_u **arg, cctx_T *cctx)
3243{
3244 char_u *start = *arg;
3245 int len;
3246 int ret;
3247 char_u *name;
3248
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 ++*arg;
3250 len = get_env_len(arg);
3251 if (len == 0)
3252 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003253 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 return FAIL;
3255 }
3256
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003257 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 name = vim_strnsave(start, len + 1);
3259 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3260 vim_free(name);
3261 return ret;
3262}
3263
3264/*
3265 * Compile "@r".
3266 */
3267 static int
3268compile_get_register(char_u **arg, cctx_T *cctx)
3269{
3270 int ret;
3271
3272 ++*arg;
3273 if (**arg == NUL)
3274 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003275 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 return FAIL;
3277 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003278 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279 {
3280 emsg_invreg(**arg);
3281 return FAIL;
3282 }
3283 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3284 ++*arg;
3285 return ret;
3286}
3287
3288/*
3289 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003290 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291 */
3292 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003293apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003295 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296
3297 // this works from end to start
3298 while (p > start)
3299 {
3300 --p;
3301 if (*p == '-' || *p == '+')
3302 {
3303 // only '-' has an effect, for '+' we only check the type
3304#ifdef FEAT_FLOAT
3305 if (rettv->v_type == VAR_FLOAT)
3306 {
3307 if (*p == '-')
3308 rettv->vval.v_float = -rettv->vval.v_float;
3309 }
3310 else
3311#endif
3312 {
3313 varnumber_T val;
3314 int error = FALSE;
3315
3316 // tv_get_number_chk() accepts a string, but we don't want that
3317 // here
3318 if (check_not_string(rettv) == FAIL)
3319 return FAIL;
3320 val = tv_get_number_chk(rettv, &error);
3321 clear_tv(rettv);
3322 if (error)
3323 return FAIL;
3324 if (*p == '-')
3325 val = -val;
3326 rettv->v_type = VAR_NUMBER;
3327 rettv->vval.v_number = val;
3328 }
3329 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003330 else if (numeric_only)
3331 {
3332 ++p;
3333 break;
3334 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003335 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003336 {
3337 int v = tv2bool(rettv);
3338
3339 // '!' is permissive in the type.
3340 clear_tv(rettv);
3341 rettv->v_type = VAR_BOOL;
3342 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3343 }
3344 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003345 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 return OK;
3347}
3348
3349/*
3350 * Recognize v: variables that are constants and set "rettv".
3351 */
3352 static void
3353get_vim_constant(char_u **arg, typval_T *rettv)
3354{
3355 if (STRNCMP(*arg, "v:true", 6) == 0)
3356 {
3357 rettv->v_type = VAR_BOOL;
3358 rettv->vval.v_number = VVAL_TRUE;
3359 *arg += 6;
3360 }
3361 else if (STRNCMP(*arg, "v:false", 7) == 0)
3362 {
3363 rettv->v_type = VAR_BOOL;
3364 rettv->vval.v_number = VVAL_FALSE;
3365 *arg += 7;
3366 }
3367 else if (STRNCMP(*arg, "v:null", 6) == 0)
3368 {
3369 rettv->v_type = VAR_SPECIAL;
3370 rettv->vval.v_number = VVAL_NULL;
3371 *arg += 6;
3372 }
3373 else if (STRNCMP(*arg, "v:none", 6) == 0)
3374 {
3375 rettv->v_type = VAR_SPECIAL;
3376 rettv->vval.v_number = VVAL_NONE;
3377 *arg += 6;
3378 }
3379}
3380
Bram Moolenaar696ba232020-07-29 21:20:41 +02003381 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003382get_compare_type(char_u *p, int *len, int *type_is)
3383{
3384 exptype_T type = EXPR_UNKNOWN;
3385 int i;
3386
3387 switch (p[0])
3388 {
3389 case '=': if (p[1] == '=')
3390 type = EXPR_EQUAL;
3391 else if (p[1] == '~')
3392 type = EXPR_MATCH;
3393 break;
3394 case '!': if (p[1] == '=')
3395 type = EXPR_NEQUAL;
3396 else if (p[1] == '~')
3397 type = EXPR_NOMATCH;
3398 break;
3399 case '>': if (p[1] != '=')
3400 {
3401 type = EXPR_GREATER;
3402 *len = 1;
3403 }
3404 else
3405 type = EXPR_GEQUAL;
3406 break;
3407 case '<': if (p[1] != '=')
3408 {
3409 type = EXPR_SMALLER;
3410 *len = 1;
3411 }
3412 else
3413 type = EXPR_SEQUAL;
3414 break;
3415 case 'i': if (p[1] == 's')
3416 {
3417 // "is" and "isnot"; but not a prefix of a name
3418 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3419 *len = 5;
3420 i = p[*len];
3421 if (!isalnum(i) && i != '_')
3422 {
3423 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3424 *type_is = TRUE;
3425 }
3426 }
3427 break;
3428 }
3429 return type;
3430}
3431
3432/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003433 * Skip over an expression, ignoring most errors.
3434 */
3435 static void
3436skip_expr_cctx(char_u **arg, cctx_T *cctx)
3437{
3438 evalarg_T evalarg;
3439
3440 CLEAR_FIELD(evalarg);
3441 evalarg.eval_cctx = cctx;
3442 skip_expr(arg, &evalarg);
3443}
3444
3445/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003447 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448 */
3449 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003450compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003452 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453
3454 // this works from end to start
3455 while (p > start)
3456 {
3457 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003458 while (VIM_ISWHITE(*p))
3459 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460 if (*p == '-' || *p == '+')
3461 {
3462 int negate = *p == '-';
3463 isn_T *isn;
3464
3465 // TODO: check type
3466 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3467 {
3468 --p;
3469 if (*p == '-')
3470 negate = !negate;
3471 }
3472 // only '-' has an effect, for '+' we only check the type
3473 if (negate)
3474 isn = generate_instr(cctx, ISN_NEGATENR);
3475 else
3476 isn = generate_instr(cctx, ISN_CHECKNR);
3477 if (isn == NULL)
3478 return FAIL;
3479 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003480 else if (numeric_only)
3481 {
3482 ++p;
3483 break;
3484 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 else
3486 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003487 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003488
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003489 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003490 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003491 if (p[-1] == '!')
3492 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494 }
3495 if (generate_2BOOL(cctx, invert) == FAIL)
3496 return FAIL;
3497 }
3498 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003499 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 return OK;
3501}
3502
3503/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003504 * Compile "(expression)": recursive!
3505 * Return FAIL/OK.
3506 */
3507 static int
3508compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3509{
3510 int ret;
3511
3512 *arg = skipwhite(*arg + 1);
3513 if (ppconst->pp_used <= PPSIZE - 10)
3514 {
3515 ret = compile_expr1(arg, cctx, ppconst);
3516 }
3517 else
3518 {
3519 // Not enough space in ppconst, flush constants.
3520 if (generate_ppconst(cctx, ppconst) == FAIL)
3521 return FAIL;
3522 ret = compile_expr0(arg, cctx);
3523 }
3524 *arg = skipwhite(*arg);
3525 if (**arg == ')')
3526 ++*arg;
3527 else if (ret == OK)
3528 {
3529 emsg(_(e_missing_close));
3530 ret = FAIL;
3531 }
3532 return ret;
3533}
3534
3535/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003536 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003537 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538 */
3539 static int
3540compile_subscript(
3541 char_u **arg,
3542 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003543 char_u *start_leader,
3544 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003545 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003546{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003547 char_u *name_start = *end_leader;
3548
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549 for (;;)
3550 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003551 char_u *p = skipwhite(*arg);
3552
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003553 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003554 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003555 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003556
3557 // If a following line starts with "->{" or "->X" advance to that
3558 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003559 // Also if a following line starts with ".x".
3560 if (next != NULL &&
3561 ((next[0] == '-' && next[1] == '>'
3562 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003563 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003564 {
3565 next = next_line_from_context(cctx, TRUE);
3566 if (next == NULL)
3567 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003568 *arg = next;
3569 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003570 }
3571 }
3572
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003573 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003574 // not a function call.
3575 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003577 garray_T *stack = &cctx->ctx_type_stack;
3578 type_T *type;
3579 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003580
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003581 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003582 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003583 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003584
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003586 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3587
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003588 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3590 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003591 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003592 return FAIL;
3593 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003594 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003596 char_u *pstart = p;
3597
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003598 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003599 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003600 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003601
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003602 // something->method()
3603 // Apply the '!', '-' and '+' first:
3604 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003605 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003608 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003609 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003610 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 if (**arg == '{')
3612 {
3613 // lambda call: list->{lambda}
Bram Moolenaar65c44152020-12-24 15:14:01 +01003614 // TODO: remove this
3615 if (compile_lambda_call(arg, cctx) == FAIL)
3616 return FAIL;
3617 }
3618 else if (**arg == '(')
3619 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003620 int argcount = 1;
3621 char_u *expr;
3622 garray_T *stack;
3623 type_T *type;
3624
3625 // Funcref call: list->(Refs[2])(arg)
3626 // or lambda: list->((arg) => expr)(arg)
3627 // Fist compile the arguments.
3628 expr = *arg;
3629 *arg = skipwhite(*arg + 1);
3630 skip_expr_cctx(arg, cctx);
3631 *arg = skipwhite(*arg);
3632 if (**arg != ')')
3633 {
3634 semsg(_(e_missing_paren), *arg);
3635 return FAIL;
3636 }
3637 ++*arg;
3638 if (**arg != '(')
3639 {
3640 semsg(_(e_missing_paren), *arg);
3641 return FAIL;
3642 }
3643
3644 *arg = skipwhite(*arg + 1);
3645 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3646 return FAIL;
3647
3648 // Compile the function expression.
3649 if (compile_parenthesis(&expr, cctx, ppconst) == FAIL)
3650 return FAIL;
3651
3652 stack = &cctx->ctx_type_stack;
3653 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3654 if (generate_PCALL(cctx, argcount,
3655 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656 return FAIL;
3657 }
3658 else
3659 {
3660 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003661 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003662 if (!eval_isnamec1(*p))
3663 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003664 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003665 return FAIL;
3666 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003667 if (ASCII_ISALPHA(*p) && p[1] == ':')
3668 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003669 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670 ;
3671 if (*p != '(')
3672 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003673 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003674 return FAIL;
3675 }
3676 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003677 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678 return FAIL;
3679 }
3680 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003681 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003683 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003684 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003685 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003686 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003687 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003688
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003689 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003690 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003691 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003692 // TODO: blob index
3693 // TODO: more arguments
3694 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003695 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003696 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003697 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003698
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003699 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003700 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003701 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003702 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003703 if (**arg == ':')
3704 // missing first index is equal to zero
3705 generate_PUSHNR(cctx, 0);
3706 else
3707 {
3708 if (compile_expr0(arg, cctx) == FAIL)
3709 return FAIL;
3710 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3711 return FAIL;
3712 *arg = skipwhite(*arg);
3713 }
3714 if (**arg == ':')
3715 {
3716 *arg = skipwhite(*arg + 1);
3717 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3718 return FAIL;
3719 if (**arg == ']')
3720 // missing second index is equal to end of string
3721 generate_PUSHNR(cctx, -1);
3722 else
3723 {
3724 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003725 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003726 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3727 return FAIL;
3728 *arg = skipwhite(*arg);
3729 }
3730 is_slice = TRUE;
3731 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732
3733 if (**arg != ']')
3734 {
3735 emsg(_(e_missbrac));
3736 return FAIL;
3737 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003738 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003739
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003740 // We can index a list and a dict. If we don't know the type
3741 // we can use the index value type.
3742 // TODO: If we don't know use an instruction to figure it out at
3743 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003744 typep = ((type_T **)stack->ga_data) + stack->ga_len
3745 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003746 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003747 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3748 // If the index is a string, the variable must be a Dict.
3749 if (*typep == &t_any && valtype == &t_string)
3750 vtype = VAR_DICT;
3751 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003752 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003753 if (need_type(valtype, &t_number, -1, cctx,
3754 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003755 return FAIL;
3756 if (is_slice)
3757 {
3758 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003759 if (need_type(valtype, &t_number, -2, cctx,
3760 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003761 return FAIL;
3762 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003763 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003764
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003765 if (vtype == VAR_DICT)
3766 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003767 if (is_slice)
3768 {
3769 emsg(_(e_cannot_slice_dictionary));
3770 return FAIL;
3771 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003772 if ((*typep)->tt_type == VAR_DICT)
3773 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003774 else
3775 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003776 if (need_type(*typep, &t_dict_any, -2, cctx,
3777 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003778 return FAIL;
3779 *typep = &t_any;
3780 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003781 if (may_generate_2STRING(-1, cctx) == FAIL)
3782 return FAIL;
3783 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3784 return FAIL;
3785 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003786 else if (vtype == VAR_STRING)
3787 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003788 *typep = &t_string;
3789 if ((is_slice
3790 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3791 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003792 return FAIL;
3793 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003794 else if (vtype == VAR_BLOB)
3795 {
3796 emsg("Sorry, blob index and slice not implemented yet");
3797 return FAIL;
3798 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003799 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003800 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003801 if (is_slice)
3802 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003803 if (generate_instr_drop(cctx,
3804 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3805 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003806 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003807 }
Bram Moolenaared591872020-08-15 22:14:53 +02003808 else
3809 {
3810 if ((*typep)->tt_type == VAR_LIST)
3811 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003812 if (generate_instr_drop(cctx,
3813 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3814 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003815 return FAIL;
3816 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003817 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003818 else
3819 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003820 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003821 return FAIL;
3822 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003824 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003826 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003827 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003828 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003829 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003830
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003831 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003832 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003833 {
3834 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003835 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003836 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003837 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003838 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839 while (eval_isnamec(*p))
3840 MB_PTR_ADV(p);
3841 if (p == *arg)
3842 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003843 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844 return FAIL;
3845 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003846 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 return FAIL;
3848 *arg = p;
3849 }
3850 else
3851 break;
3852 }
3853
3854 // TODO - see handle_subscript():
3855 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3856 // Don't do this when "Func" is already a partial that was bound
3857 // explicitly (pt_auto is FALSE).
3858
3859 return OK;
3860}
3861
3862/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003863 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3864 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003866 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003867 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003868 *
3869 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 */
3871
3872/*
3873 * number number constant
3874 * 0zFFFFFFFF Blob constant
3875 * "string" string constant
3876 * 'string' literal string constant
3877 * &option-name option value
3878 * @r register contents
3879 * identifier variable value
3880 * function() function call
3881 * $VAR environment variable
3882 * (expression) nested expression
3883 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01003884 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 *
3886 * Also handle:
3887 * ! in front logical NOT
3888 * - in front unary minus
3889 * + in front unary plus (ignored)
3890 * trailing (arg) funcref/partial call
3891 * trailing [] subscript in String or List
3892 * trailing .name entry in Dictionary
3893 * trailing ->name() method call
3894 */
3895 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003896compile_expr7(
3897 char_u **arg,
3898 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003899 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901 char_u *start_leader, *end_leader;
3902 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003903 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003904 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003906 ppconst->pp_is_const = FALSE;
3907
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908 /*
3909 * Skip '!', '-' and '+' characters. They are handled later.
3910 */
3911 start_leader = *arg;
3912 while (**arg == '!' || **arg == '-' || **arg == '+')
3913 *arg = skipwhite(*arg + 1);
3914 end_leader = *arg;
3915
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003916 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 switch (**arg)
3918 {
3919 /*
3920 * Number constant.
3921 */
3922 case '0': // also for blob starting with 0z
3923 case '1':
3924 case '2':
3925 case '3':
3926 case '4':
3927 case '5':
3928 case '6':
3929 case '7':
3930 case '8':
3931 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003932 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003934 // Apply "-" and "+" just before the number now, right to
3935 // left. Matters especially when "->" follows. Stops at
3936 // '!'.
3937 if (apply_leader(rettv, TRUE,
3938 start_leader, &end_leader) == FAIL)
3939 {
3940 clear_tv(rettv);
3941 return FAIL;
3942 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943 break;
3944
3945 /*
3946 * String constant: "string".
3947 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003948 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 return FAIL;
3950 break;
3951
3952 /*
3953 * Literal string constant: 'str''ing'.
3954 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003955 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003956 return FAIL;
3957 break;
3958
3959 /*
3960 * Constant Vim variable.
3961 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003962 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 ret = NOTDONE;
3964 break;
3965
3966 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003967 * "true" constant
3968 */
3969 case 't': if (STRNCMP(*arg, "true", 4) == 0
3970 && !eval_isnamec((*arg)[4]))
3971 {
3972 *arg += 4;
3973 rettv->v_type = VAR_BOOL;
3974 rettv->vval.v_number = VVAL_TRUE;
3975 }
3976 else
3977 ret = NOTDONE;
3978 break;
3979
3980 /*
3981 * "false" constant
3982 */
3983 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3984 && !eval_isnamec((*arg)[5]))
3985 {
3986 *arg += 5;
3987 rettv->v_type = VAR_BOOL;
3988 rettv->vval.v_number = VVAL_FALSE;
3989 }
3990 else
3991 ret = NOTDONE;
3992 break;
3993
3994 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003995 * List: [expr, expr]
3996 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003997 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003998 break;
3999
4000 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001 * Lambda: {arg, arg -> expr}
4002 * Dictionary: {'key': val, 'key': val}
4003 */
4004 case '{': {
Bram Moolenaar27bf7af2020-12-23 20:27:31 +01004005 char_u *start = skipwhite(*arg + 1);
4006 char_u *after = start;
4007 garray_T ga_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008
4009 // Find out what comes after the arguments.
Bram Moolenaar27bf7af2020-12-23 20:27:31 +01004010 ret = get_function_args(&after, '-', NULL,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004011 &ga_arg, TRUE, NULL, NULL,
4012 TRUE, NULL, NULL);
Bram Moolenaar27bf7af2020-12-23 20:27:31 +01004013 if (ret != FAIL && after[0] == '>'
4014 && ((after > start + 2
4015 && VIM_ISWHITE(after[-2]))
4016 || after == start + 1)
4017 && IS_WHITE_OR_NUL(after[1]))
Bram Moolenaar65c44152020-12-24 15:14:01 +01004018 // TODO: if we go with the "(arg) => expr" syntax
4019 // remove this
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 ret = compile_lambda(arg, cctx);
4021 else
Bram Moolenaare0de1712020-12-02 17:36:54 +01004022 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023 }
4024 break;
4025
4026 /*
4027 * Option value: &name
4028 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004029 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4030 return FAIL;
4031 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032 break;
4033
4034 /*
4035 * Environment variable: $VAR.
4036 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004037 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4038 return FAIL;
4039 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 break;
4041
4042 /*
4043 * Register contents: @r.
4044 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004045 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4046 return FAIL;
4047 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048 break;
4049 /*
4050 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004051 * lambda: (arg, arg) => expr
4052 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053 */
Bram Moolenaar65c44152020-12-24 15:14:01 +01004054 case '(': {
4055 char_u *start = skipwhite(*arg + 1);
4056 char_u *after = start;
4057 garray_T ga_arg;
Bram Moolenaar1c747212020-05-09 18:28:34 +02004058
Bram Moolenaar65c44152020-12-24 15:14:01 +01004059 // Find out if "=>" comes after the ().
4060 ret = get_function_args(&after, ')', NULL,
4061 &ga_arg, TRUE, NULL, NULL,
4062 TRUE, NULL, NULL);
4063 if (ret == OK && VIM_ISWHITE(
4064 *after == ':' ? after[1] : *after))
4065 {
4066 if (*after == ':')
4067 // Skip over type in "(arg): type".
4068 after = skip_type(skipwhite(after + 1), TRUE);
4069
4070 after = skipwhite(after);
4071 if (after[0] == '=' && after[1] == '>'
4072 && IS_WHITE_OR_NUL(after[2]))
4073 {
4074 ret = compile_lambda(arg, cctx);
4075 break;
4076 }
4077 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004078 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004079 }
4080 break;
4081
4082 default: ret = NOTDONE;
4083 break;
4084 }
4085 if (ret == FAIL)
4086 return FAIL;
4087
Bram Moolenaar1c747212020-05-09 18:28:34 +02004088 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004090 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004091 clear_tv(rettv);
4092 else
4093 // A constant expression can possibly be handled compile time,
4094 // return the value instead of generating code.
4095 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 }
4097 else if (ret == NOTDONE)
4098 {
4099 char_u *p;
4100 int r;
4101
4102 if (!eval_isnamec1(**arg))
4103 {
Bram Moolenaare4984292020-12-13 14:19:25 +01004104 if (ends_excmd(*skipwhite(*arg)))
4105 semsg(_(e_empty_expression_str), *arg);
4106 else
4107 semsg(_(e_name_expected_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108 return FAIL;
4109 }
4110
4111 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004112 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004114 {
4115 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4116 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004117 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004118 {
4119 if (generate_ppconst(cctx, ppconst) == FAIL)
4120 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004121 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004122 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123 if (r == FAIL)
4124 return FAIL;
4125 }
4126
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004127 // Handle following "[]", ".member", etc.
4128 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004129 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004130 ppconst) == FAIL)
4131 return FAIL;
4132 if (ppconst->pp_used > 0)
4133 {
4134 // apply the '!', '-' and '+' before the constant
4135 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004136 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004137 return FAIL;
4138 return OK;
4139 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004140 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004141 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004142 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004143}
4144
4145/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004146 * Give the "white on both sides" error, taking the operator from "p[len]".
4147 */
4148 void
4149error_white_both(char_u *op, int len)
4150{
4151 char_u buf[10];
4152
4153 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004154 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004155}
4156
4157/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004158 * <type>expr7: runtime type check / conversion
4159 */
4160 static int
4161compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4162{
4163 type_T *want_type = NULL;
4164
4165 // Recognize <type>
4166 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4167 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004168 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004169 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4170 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004171 return FAIL;
4172
4173 if (**arg != '>')
4174 {
4175 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004176 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004177 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004178 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004179 return FAIL;
4180 }
4181 ++*arg;
4182 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
4183 return FAIL;
4184 }
4185
4186 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4187 return FAIL;
4188
4189 if (want_type != NULL)
4190 {
4191 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004192 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004193
Bram Moolenaard1103582020-08-14 22:44:25 +02004194 generate_ppconst(cctx, ppconst);
4195 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02004196 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004197 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004198 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004199 return FAIL;
4200 }
4201 }
4202
4203 return OK;
4204}
4205
4206/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207 * * number multiplication
4208 * / number division
4209 * % number modulo
4210 */
4211 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004212compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213{
4214 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004215 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004216 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004218 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004219 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 return FAIL;
4221
4222 /*
4223 * Repeat computing, until no "*", "/" or "%" is following.
4224 */
4225 for (;;)
4226 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004227 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 if (*op != '*' && *op != '/' && *op != '%')
4229 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004230 if (next != NULL)
4231 {
4232 *arg = next_line_from_context(cctx, TRUE);
4233 op = skipwhite(*arg);
4234 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004235
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004236 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004237 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004238 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004239 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240 }
4241 *arg = skipwhite(op + 1);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004242 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004243 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004245 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004246 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004247 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004248
4249 if (ppconst->pp_used == ppconst_used + 2
4250 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4251 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004252 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004253 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4254 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004255 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004256
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004257 // both are numbers: compute the result
4258 switch (*op)
4259 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004260 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004261 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004262 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004263 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004264 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004265 break;
4266 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004267 tv1->vval.v_number = res;
4268 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004269 }
4270 else
4271 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004272 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004273 generate_two_op(cctx, op);
4274 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004275 }
4276
4277 return OK;
4278}
4279
4280/*
4281 * + number addition
4282 * - number subtraction
4283 * .. string concatenation
4284 */
4285 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004286compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287{
4288 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004289 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004291 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004292
4293 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004294 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004295 return FAIL;
4296
4297 /*
4298 * Repeat computing, until no "+", "-" or ".." is following.
4299 */
4300 for (;;)
4301 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004302 op = may_peek_next_line(cctx, *arg, &next);
4303 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 break;
4305 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004306 if (next != NULL)
4307 {
4308 *arg = next_line_from_context(cctx, TRUE);
4309 op = skipwhite(*arg);
4310 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004312 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004314 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004315 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 }
4317
4318 *arg = skipwhite(op + oplen);
Bram Moolenaare0de1712020-12-02 17:36:54 +01004319 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004320 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004322 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004323 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 return FAIL;
4325
Bram Moolenaara5565e42020-05-09 15:44:01 +02004326 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004327 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004328 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4329 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4330 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4331 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004333 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4334 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004335
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004336 // concat/subtract/add constant numbers
4337 if (*op == '+')
4338 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4339 else if (*op == '-')
4340 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4341 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004342 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004343 // concatenate constant strings
4344 char_u *s1 = tv1->vval.v_string;
4345 char_u *s2 = tv2->vval.v_string;
4346 size_t len1 = STRLEN(s1);
4347
4348 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4349 if (tv1->vval.v_string == NULL)
4350 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004351 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004352 return FAIL;
4353 }
4354 mch_memmove(tv1->vval.v_string, s1, len1);
4355 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004356 vim_free(s1);
4357 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004358 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004359 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004360 }
4361 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004362 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004363 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004364 if (*op == '.')
4365 {
4366 if (may_generate_2STRING(-2, cctx) == FAIL
4367 || may_generate_2STRING(-1, cctx) == FAIL)
4368 return FAIL;
4369 generate_instr_drop(cctx, ISN_CONCAT, 1);
4370 }
4371 else
4372 generate_two_op(cctx, op);
4373 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004374 }
4375
4376 return OK;
4377}
4378
4379/*
4380 * expr5a == expr5b
4381 * expr5a =~ expr5b
4382 * expr5a != expr5b
4383 * expr5a !~ expr5b
4384 * expr5a > expr5b
4385 * expr5a >= expr5b
4386 * expr5a < expr5b
4387 * expr5a <= expr5b
4388 * expr5a is expr5b
4389 * expr5a isnot expr5b
4390 *
4391 * Produces instructions:
4392 * EVAL expr5a Push result of "expr5a"
4393 * EVAL expr5b Push result of "expr5b"
4394 * COMPARE one of the compare instructions
4395 */
4396 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004397compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398{
4399 exptype_T type = EXPR_UNKNOWN;
4400 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004401 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004402 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004404 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004405
4406 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004407 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408 return FAIL;
4409
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004410 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004411 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412
4413 /*
4414 * If there is a comparative operator, use it.
4415 */
4416 if (type != EXPR_UNKNOWN)
4417 {
4418 int ic = FALSE; // Default: do not ignore case
4419
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004420 if (next != NULL)
4421 {
4422 *arg = next_line_from_context(cctx, TRUE);
4423 p = skipwhite(*arg);
4424 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 if (type_is && (p[len] == '?' || p[len] == '#'))
4426 {
4427 semsg(_(e_invexpr2), *arg);
4428 return FAIL;
4429 }
4430 // extra question mark appended: ignore case
4431 if (p[len] == '?')
4432 {
4433 ic = TRUE;
4434 ++len;
4435 }
4436 // extra '#' appended: match case (ignored)
4437 else if (p[len] == '#')
4438 ++len;
4439 // nothing appended: match case
4440
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004441 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004443 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004444 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 }
4446
4447 // get the second variable
4448 *arg = skipwhite(p + len);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004449 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004450 return FAIL;
4451
Bram Moolenaara5565e42020-05-09 15:44:01 +02004452 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004453 return FAIL;
4454
Bram Moolenaara5565e42020-05-09 15:44:01 +02004455 if (ppconst->pp_used == ppconst_used + 2)
4456 {
4457 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4458 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4459 int ret;
4460
4461 // Both sides are a constant, compute the result now.
4462 // First check for a valid combination of types, this is more
4463 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004464 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004465 ret = FAIL;
4466 else
4467 {
4468 ret = typval_compare(tv1, tv2, type, ic);
4469 tv1->v_type = VAR_BOOL;
4470 tv1->vval.v_number = tv1->vval.v_number
4471 ? VVAL_TRUE : VVAL_FALSE;
4472 clear_tv(tv2);
4473 --ppconst->pp_used;
4474 }
4475 return ret;
4476 }
4477
4478 generate_ppconst(cctx, ppconst);
4479 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004480 }
4481
4482 return OK;
4483}
4484
Bram Moolenaar7f141552020-05-09 17:35:53 +02004485static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487/*
4488 * Compile || or &&.
4489 */
4490 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004491compile_and_or(
4492 char_u **arg,
4493 cctx_T *cctx,
4494 char *op,
4495 ppconst_T *ppconst,
4496 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004497{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004498 char_u *next;
4499 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004500 int opchar = *op;
4501
4502 if (p[0] == opchar && p[1] == opchar)
4503 {
4504 garray_T *instr = &cctx->ctx_instr;
4505 garray_T end_ga;
4506
4507 /*
4508 * Repeat until there is no following "||" or "&&"
4509 */
4510 ga_init2(&end_ga, sizeof(int), 10);
4511 while (p[0] == opchar && p[1] == opchar)
4512 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004513 if (next != NULL)
4514 {
4515 *arg = next_line_from_context(cctx, TRUE);
4516 p = skipwhite(*arg);
4517 }
4518
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004519 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4520 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004521 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004522 return FAIL;
4523 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004524
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004525 // TODO: use ppconst if the value is a constant and check
4526 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004527 generate_ppconst(cctx, ppconst);
4528
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004529 // Every part must evaluate to a bool.
4530 if (bool_on_stack(cctx) == FAIL)
4531 {
4532 ga_clear(&end_ga);
4533 return FAIL;
4534 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004535
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536 if (ga_grow(&end_ga, 1) == FAIL)
4537 {
4538 ga_clear(&end_ga);
4539 return FAIL;
4540 }
4541 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4542 ++end_ga.ga_len;
4543 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004544 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545
4546 // eval the next expression
4547 *arg = skipwhite(p + 2);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004548 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004549 {
4550 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004551 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004552 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004553
Bram Moolenaara5565e42020-05-09 15:44:01 +02004554 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4555 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004556 {
4557 ga_clear(&end_ga);
4558 return FAIL;
4559 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004560
4561 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004562 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004563 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004565 // Every part must evaluate to a bool.
4566 if (bool_on_stack(cctx) == FAIL)
4567 {
4568 ga_clear(&end_ga);
4569 return FAIL;
4570 }
4571
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004572 // Fill in the end label in all jumps.
4573 while (end_ga.ga_len > 0)
4574 {
4575 isn_T *isn;
4576
4577 --end_ga.ga_len;
4578 isn = ((isn_T *)instr->ga_data)
4579 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4580 isn->isn_arg.jump.jump_where = instr->ga_len;
4581 }
4582 ga_clear(&end_ga);
4583 }
4584
4585 return OK;
4586}
4587
4588/*
4589 * expr4a && expr4a && expr4a logical AND
4590 *
4591 * Produces instructions:
4592 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004593 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004594 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004596 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597 * EVAL expr4c Push result of "expr4c"
4598 * end:
4599 */
4600 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004601compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004603 int ppconst_used = ppconst->pp_used;
4604
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004605 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004606 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607 return FAIL;
4608
4609 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004610 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004611}
4612
4613/*
4614 * expr3a || expr3b || expr3c logical OR
4615 *
4616 * Produces instructions:
4617 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004618 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004619 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004620 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004621 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622 * EVAL expr3c Push result of "expr3c"
4623 * end:
4624 */
4625 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004626compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004628 int ppconst_used = ppconst->pp_used;
4629
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004630 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004631 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004632 return FAIL;
4633
4634 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004635 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636}
4637
4638/*
4639 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004640 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004641 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642 * JUMP_IF_FALSE alt jump if false
4643 * EVAL expr1a
4644 * JUMP_ALWAYS end
4645 * alt: EVAL expr1b
4646 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004647 *
4648 * Toplevel expression: expr2 ?? expr1
4649 * Produces instructions:
4650 * EVAL expr2 Push result of "expr2"
4651 * JUMP_AND_KEEP_IF_TRUE end jump if true
4652 * EVAL expr1
4653 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004654 */
4655 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004656compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004657{
4658 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004659 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004660 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004661
Bram Moolenaar3988f642020-08-27 22:43:03 +02004662 // Ignore all kinds of errors when not producing code.
4663 if (cctx->ctx_skip == SKIP_YES)
4664 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004665 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004666 return OK;
4667 }
4668
Bram Moolenaar61a89812020-05-07 16:58:17 +02004669 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004670 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004671 return FAIL;
4672
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004673 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004674 if (*p == '?')
4675 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004676 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004677 garray_T *instr = &cctx->ctx_instr;
4678 garray_T *stack = &cctx->ctx_type_stack;
4679 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004680 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004681 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004682 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004683 int has_const_expr = FALSE;
4684 int const_value = FALSE;
4685 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004686
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004687 if (next != NULL)
4688 {
4689 *arg = next_line_from_context(cctx, TRUE);
4690 p = skipwhite(*arg);
4691 }
4692
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004693 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004694 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004695 semsg(_(e_white_space_required_before_and_after_str),
4696 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004697 return FAIL;
4698 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004699
Bram Moolenaara5565e42020-05-09 15:44:01 +02004700 if (ppconst->pp_used == ppconst_used + 1)
4701 {
4702 // the condition is a constant, we know whether the ? or the :
4703 // expression is to be evaluated.
4704 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004705 if (op_falsy)
4706 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4707 else
4708 {
4709 int error = FALSE;
4710
4711 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4712 &error);
4713 if (error)
4714 return FAIL;
4715 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004716 cctx->ctx_skip = save_skip == SKIP_YES ||
4717 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4718
4719 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4720 // "left ?? right" and "left" is truthy: produce "left"
4721 generate_ppconst(cctx, ppconst);
4722 else
4723 {
4724 clear_tv(&ppconst->pp_tv[ppconst_used]);
4725 --ppconst->pp_used;
4726 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004727 }
4728 else
4729 {
4730 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004731 if (op_falsy)
4732 end_idx = instr->ga_len;
4733 generate_JUMP(cctx, op_falsy
4734 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4735 if (op_falsy)
4736 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004737 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738
4739 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004740 *arg = skipwhite(p + 1 + op_falsy);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004741 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004742 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004743 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004744 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004745
Bram Moolenaara5565e42020-05-09 15:44:01 +02004746 if (!has_const_expr)
4747 {
4748 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004750 if (!op_falsy)
4751 {
4752 // remember the type and drop it
4753 --stack->ga_len;
4754 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004755
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004756 end_idx = instr->ga_len;
4757 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004758
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004759 // jump here from JUMP_IF_FALSE
4760 isn = ((isn_T *)instr->ga_data) + alt_idx;
4761 isn->isn_arg.jump.jump_where = instr->ga_len;
4762 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004763 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004764
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004765 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004766 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004767 // Check for the ":".
4768 p = may_peek_next_line(cctx, *arg, &next);
4769 if (*p != ':')
4770 {
4771 emsg(_(e_missing_colon));
4772 return FAIL;
4773 }
4774 if (next != NULL)
4775 {
4776 *arg = next_line_from_context(cctx, TRUE);
4777 p = skipwhite(*arg);
4778 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004779
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004780 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4781 {
4782 semsg(_(e_white_space_required_before_and_after_str), ":");
4783 return FAIL;
4784 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004785
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004786 // evaluate the third expression
4787 if (has_const_expr)
4788 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004789 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004790 *arg = skipwhite(p + 1);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004791 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004792 return FAIL;
4793 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4794 return FAIL;
4795 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004796
Bram Moolenaara5565e42020-05-09 15:44:01 +02004797 if (!has_const_expr)
4798 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004799 type_T **typep;
4800
Bram Moolenaara5565e42020-05-09 15:44:01 +02004801 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004802
Bram Moolenaara5565e42020-05-09 15:44:01 +02004803 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004804 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4805 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004806
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004807 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004808 isn = ((isn_T *)instr->ga_data) + end_idx;
4809 isn->isn_arg.jump.jump_where = instr->ga_len;
4810 }
4811
4812 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004813 }
4814 return OK;
4815}
4816
4817/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004818 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004819 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4820 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004821 */
4822 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004823compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004824{
4825 ppconst_T ppconst;
4826
4827 CLEAR_FIELD(ppconst);
4828 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4829 {
4830 clear_ppconst(&ppconst);
4831 return FAIL;
4832 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004833 if (is_const != NULL)
4834 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004835 if (generate_ppconst(cctx, &ppconst) == FAIL)
4836 return FAIL;
4837 return OK;
4838}
4839
4840/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004841 * Toplevel expression.
4842 */
4843 static int
4844compile_expr0(char_u **arg, cctx_T *cctx)
4845{
4846 return compile_expr0_ext(arg, cctx, NULL);
4847}
4848
4849/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004850 * compile "return [expr]"
4851 */
4852 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004853compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004854{
4855 char_u *p = arg;
4856 garray_T *stack = &cctx->ctx_type_stack;
4857 type_T *stack_type;
4858
4859 if (*p != NUL && *p != '|' && *p != '\n')
4860 {
4861 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004862 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004863 return NULL;
4864
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004865 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004866 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004867 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004868 if (check_return_type && cctx->ctx_ufunc->uf_ret_type == NULL)
4869 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004870 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004871 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004872 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004873 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004874 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4875 && stack_type->tt_type != VAR_VOID
4876 && stack_type->tt_type != VAR_UNKNOWN)
4877 {
4878 emsg(_(e_returning_value_in_function_without_return_type));
4879 return NULL;
4880 }
4881 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004882 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004883 return NULL;
4884 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02004885 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004886 }
4887 else
4888 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004889 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004890 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004891 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4892 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004894 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895 return NULL;
4896 }
4897
4898 // No argument, return zero.
4899 generate_PUSHNR(cctx, 0);
4900 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004901 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004902 return NULL;
4903
4904 // "return val | endif" is possible
4905 return skipwhite(p);
4906}
4907
4908/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004909 * Get a line from the compilation context, compatible with exarg_T getline().
4910 * Return a pointer to the line in allocated memory.
4911 * Return NULL for end-of-file or some error.
4912 */
4913 static char_u *
4914exarg_getline(
4915 int c UNUSED,
4916 void *cookie,
4917 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004918 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004919{
4920 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004921 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004922
Bram Moolenaar66250c92020-08-20 15:02:42 +02004923 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004924 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004925 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004926 return NULL;
4927 ++cctx->ctx_lnum;
4928 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4929 // Comment lines result in NULL pointers, skip them.
4930 if (p != NULL)
4931 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004932 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004933}
4934
4935/*
4936 * Compile a nested :def command.
4937 */
4938 static char_u *
4939compile_nested_function(exarg_T *eap, cctx_T *cctx)
4940{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004941 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004942 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004943 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004944 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004945 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004946 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004947
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004948 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004949 {
4950 emsg(_(e_cannot_use_bang_with_nested_def));
4951 return NULL;
4952 }
4953
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004954 if (*name_start == '/')
4955 {
4956 name_end = skip_regexp(name_start + 1, '/', TRUE);
4957 if (*name_end == '/')
4958 ++name_end;
4959 eap->nextcmd = check_nextcmd(name_end);
4960 }
4961 if (name_end == name_start || *skipwhite(name_end) != '(')
4962 {
4963 if (!ends_excmd2(name_start, name_end))
4964 {
4965 semsg(_(e_invalid_command_str), eap->cmd);
4966 return NULL;
4967 }
4968
4969 // "def" or "def Name": list functions
4970 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
4971 return NULL;
4972 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
4973 }
4974
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004975 // Only g:Func() can use a namespace.
4976 if (name_start[1] == ':' && !is_global)
4977 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004978 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004979 return NULL;
4980 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004981 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4982 return NULL;
4983
Bram Moolenaar04b12692020-05-04 23:24:44 +02004984 eap->arg = name_end;
4985 eap->getline = exarg_getline;
4986 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004987 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004988 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004989 lambda_name = vim_strsave(get_lambda_name());
4990 if (lambda_name == NULL)
4991 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004992 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004993
Bram Moolenaar822ba242020-05-24 23:00:18 +02004994 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004995 {
4996 r = eap->skip ? OK : FAIL;
4997 goto theend;
4998 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004999 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02005000 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005001 {
5002 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005003 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005004 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005005
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005006 if (is_global)
5007 {
5008 char_u *func_name = vim_strnsave(name_start + 2,
5009 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005010
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005011 if (func_name == NULL)
5012 r = FAIL;
5013 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005014 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005015 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005016 lambda_name = NULL;
5017 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005018 }
5019 else
5020 {
5021 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005022 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005023 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005024 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02005025
Bram Moolenaareef21022020-08-01 22:16:43 +02005026 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005027 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005028 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005029 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005030 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005031
5032 // copy over the block scope IDs
5033 if (block_depth > 0)
5034 {
5035 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5036 if (ufunc->uf_block_ids != NULL)
5037 {
5038 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5039 sizeof(int) * block_depth);
5040 ufunc->uf_block_depth = block_depth;
5041 }
5042 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005043 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005044 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005045 r = OK;
5046
5047theend:
5048 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005049 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005050}
5051
5052/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005053 * Return the length of an assignment operator, or zero if there isn't one.
5054 */
5055 int
5056assignment_len(char_u *p, int *heredoc)
5057{
5058 if (*p == '=')
5059 {
5060 if (p[1] == '<' && p[2] == '<')
5061 {
5062 *heredoc = TRUE;
5063 return 3;
5064 }
5065 return 1;
5066 }
5067 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5068 return 2;
5069 if (STRNCMP(p, "..=", 3) == 0)
5070 return 3;
5071 return 0;
5072}
5073
5074// words that cannot be used as a variable
5075static char *reserved[] = {
5076 "true",
5077 "false",
5078 NULL
5079};
5080
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005081typedef enum {
5082 dest_local,
5083 dest_option,
5084 dest_env,
5085 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005086 dest_buffer,
5087 dest_window,
5088 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005089 dest_vimvar,
5090 dest_script,
5091 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005092 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005093} assign_dest_T;
5094
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005095/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005096 * Generate the load instruction for "name".
5097 */
5098 static void
5099generate_loadvar(
5100 cctx_T *cctx,
5101 assign_dest_T dest,
5102 char_u *name,
5103 lvar_T *lvar,
5104 type_T *type)
5105{
5106 switch (dest)
5107 {
5108 case dest_option:
5109 // TODO: check the option exists
5110 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5111 break;
5112 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005113 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5114 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5115 else
5116 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005117 break;
5118 case dest_buffer:
5119 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5120 break;
5121 case dest_window:
5122 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5123 break;
5124 case dest_tab:
5125 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5126 break;
5127 case dest_script:
5128 compile_load_scriptvar(cctx,
5129 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5130 break;
5131 case dest_env:
5132 // Include $ in the name here
5133 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5134 break;
5135 case dest_reg:
5136 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5137 break;
5138 case dest_vimvar:
5139 generate_LOADV(cctx, name + 2, TRUE);
5140 break;
5141 case dest_local:
5142 if (lvar->lv_from_outer)
5143 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
5144 NULL, type);
5145 else
5146 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5147 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005148 case dest_expr:
5149 // list or dict value should already be on the stack.
5150 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005151 }
5152}
5153
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005154/*
5155 * Skip over "[expr]" or ".member".
5156 * Does not check for any errors.
5157 */
5158 static char_u *
5159skip_index(char_u *start)
5160{
5161 char_u *p = start;
5162
5163 if (*p == '[')
5164 {
5165 p = skipwhite(p + 1);
5166 (void)skip_expr(&p, NULL);
5167 p = skipwhite(p);
5168 if (*p == ']')
5169 return p + 1;
5170 return p;
5171 }
5172 // if (*p == '.')
5173 return to_name_end(p + 1, TRUE);
5174}
5175
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005176 void
5177vim9_declare_error(char_u *name)
5178{
5179 char *scope = "";
5180
5181 switch (*name)
5182 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005183 case 'g': scope = _("global"); break;
5184 case 'b': scope = _("buffer"); break;
5185 case 'w': scope = _("window"); break;
5186 case 't': scope = _("tab"); break;
5187 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005188 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005189 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005190 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005191 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005192 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005193 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005194 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005195 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005196 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005197}
5198
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005199/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005200 * For one assignment figure out the type of destination. Return it in "dest".
5201 * When not recognized "dest" is not set.
5202 * For an option "opt_flags" is set.
5203 * For a v:var "vimvaridx" is set.
5204 * "type" is set to the destination type if known, unchanted otherwise.
5205 * Return FAIL if an error message was given.
5206 */
5207 static int
5208get_var_dest(
5209 char_u *name,
5210 assign_dest_T *dest,
5211 int cmdidx,
5212 int *opt_flags,
5213 int *vimvaridx,
5214 type_T **type,
5215 cctx_T *cctx)
5216{
5217 char_u *p;
5218
5219 if (*name == '&')
5220 {
5221 int cc;
5222 long numval;
5223 int opt_type;
5224
5225 *dest = dest_option;
5226 if (cmdidx == CMD_final || cmdidx == CMD_const)
5227 {
5228 emsg(_(e_const_option));
5229 return FAIL;
5230 }
5231 p = name;
5232 p = find_option_end(&p, opt_flags);
5233 if (p == NULL)
5234 {
5235 // cannot happen?
5236 emsg(_(e_letunexp));
5237 return FAIL;
5238 }
5239 cc = *p;
5240 *p = NUL;
5241 opt_type = get_option_value(skip_option_env_lead(name),
5242 &numval, NULL, *opt_flags);
5243 *p = cc;
5244 if (opt_type == -3)
5245 {
5246 semsg(_(e_unknown_option), name);
5247 return FAIL;
5248 }
5249 if (opt_type == -2 || opt_type == 0)
5250 *type = &t_string;
5251 else
5252 *type = &t_number; // both number and boolean option
5253 }
5254 else if (*name == '$')
5255 {
5256 *dest = dest_env;
5257 *type = &t_string;
5258 }
5259 else if (*name == '@')
5260 {
5261 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5262 {
5263 emsg_invreg(name[1]);
5264 return FAIL;
5265 }
5266 *dest = dest_reg;
5267 *type = &t_string;
5268 }
5269 else if (STRNCMP(name, "g:", 2) == 0)
5270 {
5271 *dest = dest_global;
5272 }
5273 else if (STRNCMP(name, "b:", 2) == 0)
5274 {
5275 *dest = dest_buffer;
5276 }
5277 else if (STRNCMP(name, "w:", 2) == 0)
5278 {
5279 *dest = dest_window;
5280 }
5281 else if (STRNCMP(name, "t:", 2) == 0)
5282 {
5283 *dest = dest_tab;
5284 }
5285 else if (STRNCMP(name, "v:", 2) == 0)
5286 {
5287 typval_T *vtv;
5288 int di_flags;
5289
5290 *vimvaridx = find_vim_var(name + 2, &di_flags);
5291 if (*vimvaridx < 0)
5292 {
5293 semsg(_(e_variable_not_found_str), name);
5294 return FAIL;
5295 }
5296 // We use the current value of "sandbox" here, is that OK?
5297 if (var_check_ro(di_flags, name, FALSE))
5298 return FAIL;
5299 *dest = dest_vimvar;
5300 vtv = get_vim_var_tv(*vimvaridx);
5301 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5302 }
5303 return OK;
5304}
5305
5306/*
5307 * Generate a STORE instruction for "dest", not being "dest_local".
5308 * Return FAIL when out of memory.
5309 */
5310 static int
5311generate_store_var(
5312 cctx_T *cctx,
5313 assign_dest_T dest,
5314 int opt_flags,
5315 int vimvaridx,
5316 int scriptvar_idx,
5317 int scriptvar_sid,
5318 type_T *type,
5319 char_u *name)
5320{
5321 switch (dest)
5322 {
5323 case dest_option:
5324 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5325 opt_flags);
5326 case dest_global:
5327 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005328 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5329 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005330 case dest_buffer:
5331 // include b: with the name, easier to execute that way
5332 return generate_STORE(cctx, ISN_STOREB, 0, name);
5333 case dest_window:
5334 // include w: with the name, easier to execute that way
5335 return generate_STORE(cctx, ISN_STOREW, 0, name);
5336 case dest_tab:
5337 // include t: with the name, easier to execute that way
5338 return generate_STORE(cctx, ISN_STORET, 0, name);
5339 case dest_env:
5340 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5341 case dest_reg:
5342 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5343 case dest_vimvar:
5344 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5345 case dest_script:
5346 if (scriptvar_idx < 0)
5347 {
5348 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005349 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005350
Bram Moolenaar41d61962020-12-06 20:12:43 +01005351 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005352 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5353 scriptvar_sid, type);
5354 if (name_s != name)
5355 vim_free(name_s);
5356 return r;
5357 }
5358 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5359 scriptvar_sid, scriptvar_idx, type);
5360 case dest_local:
5361 case dest_expr:
5362 // cannot happen
5363 break;
5364 }
5365 return FAIL;
5366}
5367
5368/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005369 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005370 * "let name"
5371 * "var name = expr"
5372 * "final name = expr"
5373 * "const name = expr"
5374 * "name = expr"
5375 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005376 * Return NULL for an error.
5377 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005378 */
5379 static char_u *
5380compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5381{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005382 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005383 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005384 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005385 char_u *ret = NULL;
5386 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005387 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005388 int scriptvar_sid = 0;
5389 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005390 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005391 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005392 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005393 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005394 int oplen = 0;
5395 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005396 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005397 type_T *member_type = &t_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005398 type_T *rhs_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005399 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005400 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005401 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
5402 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005403
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005404 // Skip over the "var" or "[var, var]" to get to any "=".
5405 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5406 if (p == NULL)
5407 return *arg == '[' ? arg : NULL;
5408
5409 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005410 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005411 // TODO: should we allow this, and figure out type inference from list
5412 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005413 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005414 return NULL;
5415 }
5416
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005417 sp = p;
5418 p = skipwhite(p);
5419 op = p;
5420 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005421
5422 if (var_count > 0 && oplen == 0)
5423 // can be something like "[1, 2]->func()"
5424 return arg;
5425
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005426 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005427 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005428 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005429 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005430 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005431
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005432 if (heredoc)
5433 {
5434 list_T *l;
5435 listitem_T *li;
5436
5437 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005438 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005439 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005440 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005441 if (l == NULL)
5442 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005443
Bram Moolenaar078269b2020-09-21 20:35:55 +02005444 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005445 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005446 // Push each line and the create the list.
5447 FOR_ALL_LIST_ITEMS(l, li)
5448 {
5449 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5450 li->li_tv.vval.v_string = NULL;
5451 }
5452 generate_NEWLIST(cctx, l->lv_len);
5453 type = &t_list_string;
5454 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005455 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005456 list_free(l);
5457 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005458 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005459 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005460 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005461 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005462 char_u *wp;
5463
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005464 // for "[var, var] = expr" evaluate the expression here, loop over the
5465 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005466 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005467
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005468 wp = op + oplen;
5469 p = skipwhite(wp);
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01005470 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005471 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005472 if (compile_expr0(&p, cctx) == FAIL)
5473 return NULL;
5474 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005475
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005476 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005477 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005478 type_T *stacktype;
5479
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005480 stacktype = stack->ga_len == 0 ? &t_void
5481 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005482 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005483 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005484 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005485 goto theend;
5486 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005487 if (need_type(stacktype, &t_list_any, -1, cctx,
5488 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005489 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005490 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005491 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5492 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005493 if (stacktype->tt_member != NULL)
5494 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005495 }
5496 }
5497
5498 /*
5499 * Loop over variables in "[var, var] = expr".
5500 * For "var = expr" and "let var: type" this is done only once.
5501 */
5502 if (var_count > 0)
5503 var_start = skipwhite(arg + 1); // skip over the "["
5504 else
5505 var_start = arg;
5506 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5507 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005508 char_u *var_end;
5509 char_u *dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005510 size_t varlen;
5511 int new_local = FALSE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005512 assign_dest_T dest = dest_local;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005513 int opt_flags = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005514 int vimvaridx = -1;
Bram Moolenaar709664c2020-12-12 14:33:41 +01005515 lvar_T local_lvar;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005516 lvar_T *lvar = NULL;
5517 lvar_T arg_lvar;
5518 int has_type = FALSE;
5519 int has_index = FALSE;
5520 int instr_count = -1;
5521
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005522 // "dest_end" is the end of the destination, including "[expr]" or
5523 // ".name".
5524 // "var_end" is the end of the variable/option/etc. name.
5525 dest_end = skip_var_one(var_start, FALSE);
Bram Moolenaar65821722020-08-02 18:58:54 +02005526 if (*var_start == '@')
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005527 var_end = var_start + 2;
Bram Moolenaar65821722020-08-02 18:58:54 +02005528 else
5529 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02005530 // skip over the leading "&", "&l:", "&g:" and "$"
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005531 var_end = skip_option_env_lead(var_start);
5532 var_end = to_name_end(var_end, TRUE);
Bram Moolenaar65821722020-08-02 18:58:54 +02005533 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005534
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005535 // "a: type" is declaring variable "a" with a type, not dict "a:".
5536 if (is_decl && dest_end == var_start + 2 && dest_end[-1] == ':')
5537 --dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005538 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5539 --var_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005540
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005541 // compute the length of the destination without "[expr]" or ".name"
5542 varlen = var_end - var_start;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005543 vim_free(name);
5544 name = vim_strnsave(var_start, varlen);
5545 if (name == NULL)
5546 return NULL;
5547 if (!heredoc)
5548 type = &t_any;
5549
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005550 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005551 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005552 int declare_error = FALSE;
5553
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005554 if (get_var_dest(name, &dest, cmdidx, &opt_flags,
5555 &vimvaridx, &type, cctx) == FAIL)
5556 goto theend;
5557 if (dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005558 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005559 // Specific kind of variable recognized.
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005560 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005561 }
5562 else
5563 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005564 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005565
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005566 // No specific kind of variable recognized, just a name.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005567 for (idx = 0; reserved[idx] != NULL; ++idx)
5568 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005569 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005570 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005571 goto theend;
5572 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005573
Bram Moolenaar709664c2020-12-12 14:33:41 +01005574
5575 if (lookup_local(var_start, varlen, &local_lvar, cctx) == OK)
5576 lvar = &local_lvar;
5577 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005578 {
5579 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005580 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005581 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5582 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005583 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005584 if (is_decl)
5585 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005586 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005587 goto theend;
5588 }
5589 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005590 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005591 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005592 if (lvar != NULL)
5593 {
5594 if (is_decl)
5595 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005596 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005597 goto theend;
5598 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005599 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005600 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005601 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005602 int script_namespace = varlen > 1
5603 && STRNCMP(var_start, "s:", 2) == 0;
5604 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005605 ? script_var_exists(var_start + 2, varlen - 2,
5606 FALSE, cctx)
5607 : script_var_exists(var_start, varlen,
5608 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005609 imported_T *import =
5610 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005611
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005612 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005613 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005614 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5615
5616 if (is_decl)
5617 {
5618 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005619 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005620 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005621 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005622 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005623 name);
5624 goto theend;
5625 }
5626 else if (cctx->ctx_ufunc->uf_script_ctx_version
5627 == SCRIPT_VERSION_VIM9
5628 && script_namespace
5629 && !script_var && import == NULL)
5630 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005631 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005632 goto theend;
5633 }
5634
5635 dest = dest_script;
5636
5637 // existing script-local variables should have a type
5638 scriptvar_sid = current_sctx.sc_sid;
5639 if (import != NULL)
5640 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005641 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005642 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005643 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005644 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005645 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005646 {
5647 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5648 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005649 ((svar_T *)si->sn_var_vals.ga_data)
5650 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005651 type = sv->sv_type;
5652 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005653 }
5654 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005655 else if (check_defined(var_start, varlen, cctx) == FAIL)
5656 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005657 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005658 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005659
5660 if (declare_error)
5661 {
5662 vim9_declare_error(name);
5663 goto theend;
5664 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005665 }
5666
5667 // handle "a:name" as a name, not index "name" on "a"
5668 if (varlen > 1 || var_start[varlen] != ':')
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005669 var_end = dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005670
5671 if (dest != dest_option)
5672 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005673 if (is_decl && *var_end == ':')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005674 {
5675 // parse optional type: "let var: type = expr"
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005676 if (!VIM_ISWHITE(var_end[1]))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005677 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005678 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005679 goto theend;
5680 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005681 p = skipwhite(var_end + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005682 type = parse_type(&p, cctx->ctx_type_list, TRUE);
5683 if (type == NULL)
5684 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005685 has_type = TRUE;
5686 }
5687 else if (lvar != NULL)
5688 type = lvar->lv_type;
5689 }
5690
5691 if (oplen == 3 && !heredoc && dest != dest_global
5692 && type->tt_type != VAR_STRING
5693 && type->tt_type != VAR_ANY)
5694 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005695 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005696 goto theend;
5697 }
5698
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005699 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005700 {
5701 if (oplen > 1 && !heredoc)
5702 {
5703 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005704 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005705 goto theend;
5706 }
5707
5708 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005709 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5710 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005711 goto theend;
5712 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005713 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005714 if (lvar == NULL)
5715 goto theend;
5716 new_local = TRUE;
5717 }
5718
5719 member_type = type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005720 if (dest_end > var_start + varlen)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005721 {
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005722 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005723 // TODO: should we also handle "->func()" here?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005724 if (is_decl)
5725 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005726 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005727 goto theend;
5728 }
5729
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005730 if (var_start[varlen] == '[' || var_start[varlen] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005731 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005732 char_u *after = var_start + varlen;
5733
5734 // Only the last index is used below, if there are others
5735 // before it generate code for the expression. Thus for
5736 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5737 for (;;)
5738 {
5739 p = skip_index(after);
5740 if (*p != '[' && *p != '.')
5741 break;
5742 after = p;
5743 }
5744 if (after > var_start + varlen)
5745 {
5746 varlen = after - var_start;
5747 dest = dest_expr;
5748 // We don't know the type before evaluating the expression,
5749 // use "any" until then.
5750 type = &t_any;
5751 }
5752
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005753 has_index = TRUE;
5754 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005755 member_type = &t_any;
5756 else
5757 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005758 }
5759 else
5760 {
5761 semsg("Not supported yet: %s", var_start);
5762 goto theend;
5763 }
5764 }
5765 else if (lvar == &arg_lvar)
5766 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005767 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005768 goto theend;
5769 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005770 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5771 {
5772 semsg(_(e_cannot_assign_to_constant), name);
5773 goto theend;
5774 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005775
5776 if (!heredoc)
5777 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005778 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005779 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005780 if (oplen > 0 && var_count == 0)
5781 {
5782 // skip over the "=" and the expression
5783 p = skipwhite(op + oplen);
5784 compile_expr0(&p, cctx);
5785 }
5786 }
5787 else if (oplen > 0)
5788 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005789 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005790 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005791
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005792 // For "var = expr" evaluate the expression.
5793 if (var_count == 0)
5794 {
5795 int r;
5796
5797 // for "+=", "*=", "..=" etc. first load the current value
5798 if (*op != '=')
5799 {
5800 generate_loadvar(cctx, dest, name, lvar, type);
5801
5802 if (has_index)
5803 {
5804 // TODO: get member from list or dict
5805 emsg("Index with operation not supported yet");
5806 goto theend;
5807 }
5808 }
5809
5810 // Compile the expression. Temporarily hide the new local
5811 // variable here, it is not available to this expression.
5812 if (new_local)
5813 --cctx->ctx_locals.ga_len;
5814 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005815 wp = op + oplen;
5816 p = skipwhite(wp);
5817 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01005818 {
5819 if (new_local)
5820 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005821 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01005822 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005823 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005824 if (new_local)
5825 ++cctx->ctx_locals.ga_len;
5826 if (r == FAIL)
5827 goto theend;
5828 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005829 else if (semicolon && var_idx == var_count - 1)
5830 {
5831 // For "[var; var] = expr" get the rest of the list
5832 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5833 goto theend;
5834 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005835 else
5836 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005837 // For "[var, var] = expr" get the "var_idx" item from the
5838 // list.
5839 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01005840 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005841 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005842
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005843 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005844 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005845 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005846 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005847 if ((rhs_type->tt_type == VAR_FUNC
5848 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar0f769812020-09-12 18:32:34 +02005849 && var_wrong_func_name(name, TRUE))
5850 goto theend;
5851
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005852 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005853 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005854 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005855 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005856 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005857 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005858 }
5859 else
5860 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005861 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005862 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005863 if (rhs_type == &t_list_empty)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005864 lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005865 else if (rhs_type == &t_dict_empty)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005866 lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005867 else if (rhs_type == &t_unknown)
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005868 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005869 else
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005870 lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005871 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005872 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005873 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005874 {
5875 type_T *use_type = lvar->lv_type;
5876
Bram Moolenaar71abe482020-09-14 22:28:30 +02005877 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005878 if (has_index)
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005879 use_type = member_type;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005880 if (need_type(rhs_type, use_type, -1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005881 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005882 goto theend;
5883 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005884 }
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005885 else if (*p != '=' && need_type(rhs_type, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005886 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005887 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005888 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005889 else if (cmdidx == CMD_final)
5890 {
5891 emsg(_(e_final_requires_a_value));
5892 goto theend;
5893 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005894 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005895 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005896 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005897 goto theend;
5898 }
5899 else if (!has_type || dest == dest_option)
5900 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005901 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005902 goto theend;
5903 }
5904 else
5905 {
5906 // variables are always initialized
5907 if (ga_grow(instr, 1) == FAIL)
5908 goto theend;
5909 switch (member_type->tt_type)
5910 {
5911 case VAR_BOOL:
5912 generate_PUSHBOOL(cctx, VVAL_FALSE);
5913 break;
5914 case VAR_FLOAT:
5915#ifdef FEAT_FLOAT
5916 generate_PUSHF(cctx, 0.0);
5917#endif
5918 break;
5919 case VAR_STRING:
5920 generate_PUSHS(cctx, NULL);
5921 break;
5922 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005923 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005924 break;
5925 case VAR_FUNC:
5926 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5927 break;
5928 case VAR_LIST:
5929 generate_NEWLIST(cctx, 0);
5930 break;
5931 case VAR_DICT:
5932 generate_NEWDICT(cctx, 0);
5933 break;
5934 case VAR_JOB:
5935 generate_PUSHJOB(cctx, NULL);
5936 break;
5937 case VAR_CHANNEL:
5938 generate_PUSHCHANNEL(cctx, NULL);
5939 break;
5940 case VAR_NUMBER:
5941 case VAR_UNKNOWN:
5942 case VAR_ANY:
5943 case VAR_PARTIAL:
5944 case VAR_VOID:
5945 case VAR_SPECIAL: // cannot happen
5946 generate_PUSHNR(cctx, 0);
5947 break;
5948 }
5949 }
5950 if (var_count == 0)
5951 end = p;
5952 }
5953
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005954 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005955 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005956 break;
5957
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005958 if (oplen > 0 && *op != '=')
5959 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005960 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005961 type_T *stacktype;
5962
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005963 if (*op == '.')
5964 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005965 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005966 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005967 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005968 if (
5969#ifdef FEAT_FLOAT
5970 // If variable is float operation with number is OK.
5971 !(expected == &t_float && stacktype == &t_number) &&
5972#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005973 need_type(stacktype, expected, -1, cctx,
5974 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005975 goto theend;
5976
5977 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005978 {
5979 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5980 goto theend;
5981 }
5982 else if (*op == '+')
5983 {
5984 if (generate_add_instr(cctx,
5985 operator_type(member_type, stacktype),
5986 member_type, stacktype) == FAIL)
5987 goto theend;
5988 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005989 else if (generate_two_op(cctx, op) == FAIL)
5990 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005991 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005992
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005993 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005994 {
Bram Moolenaard24602f2020-12-20 15:20:56 +01005995 int r;
5996 vartype_T dest_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005997
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005998 // Compile the "idx" in "var[idx]" or "key" in "var.key".
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005999 p = var_start + varlen;
6000 if (*p == '[')
6001 {
6002 p = skipwhite(p + 1);
6003 r = compile_expr0(&p, cctx);
6004 if (r == OK && *skipwhite(p) != ']')
6005 {
6006 // this should not happen
6007 emsg(_(e_missbrac));
6008 r = FAIL;
6009 }
6010 }
6011 else // if (*p == '.')
6012 {
6013 char_u *key_end = to_name_end(p + 1, TRUE);
6014 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6015
6016 r = generate_PUSHS(cctx, key);
6017 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006018 if (r == FAIL)
6019 goto theend;
Bram Moolenaarfc74d032020-11-16 22:11:49 +01006020
Bram Moolenaar2caa1592020-08-01 15:53:19 +02006021 if (type == &t_any)
6022 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01006023 // Index on variable of unknown type: check at runtime.
6024 dest_type = VAR_ANY;
Bram Moolenaar2caa1592020-08-01 15:53:19 +02006025 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01006026 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006027 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01006028 dest_type = type->tt_type;
6029 if (dest_type == VAR_DICT
6030 && may_generate_2STRING(-1, cctx) == FAIL)
6031 goto theend;
6032 if (dest_type == VAR_LIST
6033 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
6034 != VAR_NUMBER)
6035 {
6036 emsg(_(e_number_exp));
6037 goto theend;
6038 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006039 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006040
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006041 // Load the dict or list. On the stack we then have:
6042 // - value
6043 // - index
6044 // - variable
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006045 if (dest == dest_expr)
6046 {
6047 int c = var_start[varlen];
6048
6049 // Evaluate "ll[expr]" of "ll[expr][idx]"
6050 p = var_start;
6051 var_start[varlen] = NUL;
6052 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6053 {
6054 // this should not happen
6055 emsg(_(e_missbrac));
6056 goto theend;
6057 }
6058 var_start[varlen] = c;
6059
6060 type = stack->ga_len == 0 ? &t_void
6061 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6062 // now we can properly check the type
6063 if (type->tt_member != NULL
6064 && need_type(rhs_type, type->tt_member, -2, cctx,
6065 FALSE, FALSE) == FAIL)
6066 goto theend;
6067 }
6068 else
6069 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006070
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01006071 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6072 || dest_type == VAR_ANY)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006073 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01006074 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
6075
6076 if (isn == NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006077 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01006078 isn->isn_arg.vartype = dest_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006079 }
6080 else
6081 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006082 emsg(_(e_indexable_type_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006083 goto theend;
6084 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006085 }
6086 else
6087 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006088 if (is_decl && cmdidx == CMD_const
6089 && (dest == dest_script || dest == dest_local))
6090 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006091 generate_LOCKCONST(cctx);
6092
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006093 if (dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006094 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006095 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
6096 scriptvar_idx, scriptvar_sid, type, name) == FAIL)
6097 goto theend;
6098 }
6099 else if (lvar != NULL)
6100 {
6101 isn_T *isn = ((isn_T *)instr->ga_data)
6102 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006103
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006104 // optimization: turn "var = 123" from ISN_PUSHNR +
6105 // ISN_STORE into ISN_STORENR
6106 if (!lvar->lv_from_outer
6107 && instr->ga_len == instr_count + 1
6108 && isn->isn_type == ISN_PUSHNR)
6109 {
6110 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006111
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006112 isn->isn_type = ISN_STORENR;
6113 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
6114 isn->isn_arg.storenr.stnr_val = val;
6115 if (stack->ga_len > 0)
6116 --stack->ga_len;
6117 }
6118 else if (lvar->lv_from_outer)
6119 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, NULL);
6120 else
6121 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006122 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006123 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006124
6125 if (var_idx + 1 < var_count)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006126 var_start = skipwhite(dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006127 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006128
6129 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006130 if (var_count > 0 && !semicolon)
6131 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006132 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006133 goto theend;
6134 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006135
Bram Moolenaarb2097502020-07-19 17:17:02 +02006136 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006137
6138theend:
6139 vim_free(name);
6140 return ret;
6141}
6142
6143/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006144 * Check if "name" can be "unlet".
6145 */
6146 int
6147check_vim9_unlet(char_u *name)
6148{
6149 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6150 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006151 // "unlet s:var" is allowed in legacy script.
6152 if (*name == 's' && !script_is_vim9())
6153 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006154 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006155 return FAIL;
6156 }
6157 return OK;
6158}
6159
6160/*
6161 * Callback passed to ex_unletlock().
6162 */
6163 static int
6164compile_unlet(
6165 lval_T *lvp,
6166 char_u *name_end,
6167 exarg_T *eap,
6168 int deep UNUSED,
6169 void *coookie)
6170{
6171 cctx_T *cctx = coookie;
6172
6173 if (lvp->ll_tv == NULL)
6174 {
6175 char_u *p = lvp->ll_name;
6176 int cc = *name_end;
6177 int ret = OK;
6178
6179 // Normal name. Only supports g:, w:, t: and b: namespaces.
6180 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006181 if (*p == '$')
6182 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6183 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006184 ret = FAIL;
6185 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006186 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006187
6188 *name_end = cc;
6189 return ret;
6190 }
6191
6192 // TODO: unlet {list}[idx]
6193 // TODO: unlet {dict}[key]
6194 emsg("Sorry, :unlet not fully implemented yet");
6195 return FAIL;
6196}
6197
6198/*
6199 * compile "unlet var", "lock var" and "unlock var"
6200 * "arg" points to "var".
6201 */
6202 static char_u *
6203compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6204{
6205 char_u *p = arg;
6206
6207 if (eap->cmdidx != CMD_unlet)
6208 {
6209 emsg("Sorry, :lock and unlock not implemented yet");
6210 return NULL;
6211 }
6212
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006213 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
6214 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6215}
6216
6217/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006218 * Compile an :import command.
6219 */
6220 static char_u *
6221compile_import(char_u *arg, cctx_T *cctx)
6222{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006223 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006224}
6225
6226/*
6227 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6228 */
6229 static int
6230compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6231{
6232 garray_T *instr = &cctx->ctx_instr;
6233 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6234
6235 if (endlabel == NULL)
6236 return FAIL;
6237 endlabel->el_next = *el;
6238 *el = endlabel;
6239 endlabel->el_end_label = instr->ga_len;
6240
6241 generate_JUMP(cctx, when, 0);
6242 return OK;
6243}
6244
6245 static void
6246compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
6247{
6248 garray_T *instr = &cctx->ctx_instr;
6249
6250 while (*el != NULL)
6251 {
6252 endlabel_T *cur = (*el);
6253 isn_T *isn;
6254
6255 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
6256 isn->isn_arg.jump.jump_where = instr->ga_len;
6257 *el = cur->el_next;
6258 vim_free(cur);
6259 }
6260}
6261
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006262 static void
6263compile_free_jump_to_end(endlabel_T **el)
6264{
6265 while (*el != NULL)
6266 {
6267 endlabel_T *cur = (*el);
6268
6269 *el = cur->el_next;
6270 vim_free(cur);
6271 }
6272}
6273
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006274/*
6275 * Create a new scope and set up the generic items.
6276 */
6277 static scope_T *
6278new_scope(cctx_T *cctx, scopetype_T type)
6279{
6280 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6281
6282 if (scope == NULL)
6283 return NULL;
6284 scope->se_outer = cctx->ctx_scope;
6285 cctx->ctx_scope = scope;
6286 scope->se_type = type;
6287 scope->se_local_count = cctx->ctx_locals.ga_len;
6288 return scope;
6289}
6290
6291/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006292 * Free the current scope and go back to the outer scope.
6293 */
6294 static void
6295drop_scope(cctx_T *cctx)
6296{
6297 scope_T *scope = cctx->ctx_scope;
6298
6299 if (scope == NULL)
6300 {
6301 iemsg("calling drop_scope() without a scope");
6302 return;
6303 }
6304 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006305 switch (scope->se_type)
6306 {
6307 case IF_SCOPE:
6308 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6309 case FOR_SCOPE:
6310 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6311 case WHILE_SCOPE:
6312 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6313 case TRY_SCOPE:
6314 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6315 case NO_SCOPE:
6316 case BLOCK_SCOPE:
6317 break;
6318 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006319 vim_free(scope);
6320}
6321
6322/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006323 * compile "if expr"
6324 *
6325 * "if expr" Produces instructions:
6326 * EVAL expr Push result of "expr"
6327 * JUMP_IF_FALSE end
6328 * ... body ...
6329 * end:
6330 *
6331 * "if expr | else" Produces instructions:
6332 * EVAL expr Push result of "expr"
6333 * JUMP_IF_FALSE else
6334 * ... body ...
6335 * JUMP_ALWAYS end
6336 * else:
6337 * ... body ...
6338 * end:
6339 *
6340 * "if expr1 | elseif expr2 | else" Produces instructions:
6341 * EVAL expr Push result of "expr"
6342 * JUMP_IF_FALSE elseif
6343 * ... body ...
6344 * JUMP_ALWAYS end
6345 * elseif:
6346 * EVAL expr Push result of "expr"
6347 * JUMP_IF_FALSE else
6348 * ... body ...
6349 * JUMP_ALWAYS end
6350 * else:
6351 * ... body ...
6352 * end:
6353 */
6354 static char_u *
6355compile_if(char_u *arg, cctx_T *cctx)
6356{
6357 char_u *p = arg;
6358 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006359 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006360 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006361 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006362 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006363
Bram Moolenaara5565e42020-05-09 15:44:01 +02006364 CLEAR_FIELD(ppconst);
6365 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006366 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006367 clear_ppconst(&ppconst);
6368 return NULL;
6369 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006370 if (cctx->ctx_skip == SKIP_YES)
6371 clear_ppconst(&ppconst);
6372 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006373 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006374 int error = FALSE;
6375 int v;
6376
Bram Moolenaara5565e42020-05-09 15:44:01 +02006377 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006378 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006379 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006380 if (error)
6381 return NULL;
6382 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006383 }
6384 else
6385 {
6386 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006387 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006388 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006389 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006390 if (bool_on_stack(cctx) == FAIL)
6391 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006392 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006393
6394 scope = new_scope(cctx, IF_SCOPE);
6395 if (scope == NULL)
6396 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006397 scope->se_skip_save = skip_save;
6398 // "is_had_return" will be reset if any block does not end in :return
6399 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006400
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006401 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006402 {
6403 // "where" is set when ":elseif", "else" or ":endif" is found
6404 scope->se_u.se_if.is_if_label = instr->ga_len;
6405 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6406 }
6407 else
6408 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006409
6410 return p;
6411}
6412
6413 static char_u *
6414compile_elseif(char_u *arg, cctx_T *cctx)
6415{
6416 char_u *p = arg;
6417 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006418 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006419 isn_T *isn;
6420 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006421 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006422 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006423
6424 if (scope == NULL || scope->se_type != IF_SCOPE)
6425 {
6426 emsg(_(e_elseif_without_if));
6427 return NULL;
6428 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006429 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006430 if (!cctx->ctx_had_return)
6431 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006432
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006433 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006434 {
6435 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006436 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006437 return NULL;
6438 // previous "if" or "elseif" jumps here
6439 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6440 isn->isn_arg.jump.jump_where = instr->ga_len;
6441 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006442
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006443 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006444 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006445 if (cctx->ctx_skip == SKIP_YES)
6446 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006447 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006448 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006449 clear_ppconst(&ppconst);
6450 return NULL;
6451 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006452 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006453 if (scope->se_skip_save == SKIP_YES)
6454 clear_ppconst(&ppconst);
6455 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006456 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006457 int error = FALSE;
6458 int v;
6459
Bram Moolenaar7f141552020-05-09 17:35:53 +02006460 // The expression results in a constant.
6461 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006462 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6463 if (error)
6464 return NULL;
6465 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006466 clear_ppconst(&ppconst);
6467 scope->se_u.se_if.is_if_label = -1;
6468 }
6469 else
6470 {
6471 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006472 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006473 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006474 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006475 if (bool_on_stack(cctx) == FAIL)
6476 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006477
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006478 // "where" is set when ":elseif", "else" or ":endif" is found
6479 scope->se_u.se_if.is_if_label = instr->ga_len;
6480 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6481 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006482
6483 return p;
6484}
6485
6486 static char_u *
6487compile_else(char_u *arg, cctx_T *cctx)
6488{
6489 char_u *p = arg;
6490 garray_T *instr = &cctx->ctx_instr;
6491 isn_T *isn;
6492 scope_T *scope = cctx->ctx_scope;
6493
6494 if (scope == NULL || scope->se_type != IF_SCOPE)
6495 {
6496 emsg(_(e_else_without_if));
6497 return NULL;
6498 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006499 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006500 if (!cctx->ctx_had_return)
6501 scope->se_u.se_if.is_had_return = FALSE;
6502 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006503
Bram Moolenaarefd88552020-06-18 20:50:10 +02006504 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006505 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006506 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006507 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006508 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006509 if (!cctx->ctx_had_return
6510 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6511 JUMP_ALWAYS, cctx) == FAIL)
6512 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006513 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006514
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006515 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006516 {
6517 if (scope->se_u.se_if.is_if_label >= 0)
6518 {
6519 // previous "if" or "elseif" jumps here
6520 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6521 isn->isn_arg.jump.jump_where = instr->ga_len;
6522 scope->se_u.se_if.is_if_label = -1;
6523 }
6524 }
6525
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006526 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006527 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6528 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006529
6530 return p;
6531}
6532
6533 static char_u *
6534compile_endif(char_u *arg, cctx_T *cctx)
6535{
6536 scope_T *scope = cctx->ctx_scope;
6537 ifscope_T *ifscope;
6538 garray_T *instr = &cctx->ctx_instr;
6539 isn_T *isn;
6540
6541 if (scope == NULL || scope->se_type != IF_SCOPE)
6542 {
6543 emsg(_(e_endif_without_if));
6544 return NULL;
6545 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006546 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006547 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006548 if (!cctx->ctx_had_return)
6549 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006550
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006551 if (scope->se_u.se_if.is_if_label >= 0)
6552 {
6553 // previous "if" or "elseif" jumps here
6554 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6555 isn->isn_arg.jump.jump_where = instr->ga_len;
6556 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006557 // Fill in the "end" label in jumps at the end of the blocks.
6558 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006559 cctx->ctx_skip = scope->se_skip_save;
6560
6561 // If all the blocks end in :return and there is an :else then the
6562 // had_return flag is set.
6563 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006564
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006565 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006566 return arg;
6567}
6568
6569/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01006570 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006571 *
6572 * Produces instructions:
6573 * PUSHNR -1
6574 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01006575 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006576 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6577 * - if beyond end, jump to "end"
6578 * - otherwise get item from list and push it
6579 * STORE var Store item in "var"
6580 * ... body ...
6581 * JUMP top Jump back to repeat
6582 * end: DROP Drop the result of "expr"
6583 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006584 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
6585 * UNPACK 2 Split item in 2
6586 * STORE var1 Store item in "var1"
6587 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006588 */
6589 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006590compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006591{
Bram Moolenaar792f7862020-11-23 08:31:18 +01006592 char_u *arg;
6593 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006594 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006595 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006596 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006597 int var_count = 0;
6598 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006599 size_t varlen;
6600 garray_T *instr = &cctx->ctx_instr;
6601 garray_T *stack = &cctx->ctx_type_stack;
6602 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006603 lvar_T *loop_lvar; // loop iteration variable
6604 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006605 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006606 type_T *item_type = &t_any;
6607 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006608
Bram Moolenaar792f7862020-11-23 08:31:18 +01006609 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
6610 if (var_count == 0)
6611 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006612
6613 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006614 wp = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006615 p = skipwhite(p);
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006616 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6617 return NULL;
6618 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006619 {
6620 emsg(_(e_missing_in));
6621 return NULL;
6622 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006623 wp = p + 2;
6624 p = skipwhite(wp);
6625 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6626 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006627
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006628 scope = new_scope(cctx, FOR_SCOPE);
6629 if (scope == NULL)
6630 return NULL;
6631
Bram Moolenaar792f7862020-11-23 08:31:18 +01006632 // Reserve a variable to store the loop iteration counter and initialize it
6633 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006634 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6635 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006636 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006637 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006638 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006639 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006640 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006641 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006642
6643 // compile "expr", it remains on the stack until "endfor"
6644 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006645 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006646 {
6647 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006648 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006649 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006650 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006651
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006652 // Now that we know the type of "var", check that it is a list, now or at
6653 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006654 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006655 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006656 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006657 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006658 return NULL;
6659 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006660
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006661 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006662 {
6663 if (var_count == 1)
6664 item_type = vartype->tt_member;
6665 else if (vartype->tt_member->tt_type == VAR_LIST
6666 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
6667 item_type = vartype->tt_member->tt_member;
6668 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006669
6670 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006671 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006672 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006673
Bram Moolenaar792f7862020-11-23 08:31:18 +01006674 arg = arg_start;
6675 if (var_count > 1)
6676 {
6677 generate_UNPACK(cctx, var_count, semicolon);
6678 arg = skipwhite(arg + 1); // skip white after '['
6679
6680 // the list item is replaced by a number of items
6681 if (ga_grow(stack, var_count - 1) == FAIL)
6682 {
6683 drop_scope(cctx);
6684 return NULL;
6685 }
6686 --stack->ga_len;
6687 for (idx = 0; idx < var_count; ++idx)
6688 {
6689 ((type_T **)stack->ga_data)[stack->ga_len] =
6690 (semicolon && idx == 0) ? vartype : item_type;
6691 ++stack->ga_len;
6692 }
6693 }
6694
6695 for (idx = 0; idx < var_count; ++idx)
6696 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006697 assign_dest_T dest = dest_local;
6698 int opt_flags = 0;
6699 int vimvaridx = -1;
6700 type_T *type = &t_any;
6701
6702 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006703 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006704 name = vim_strnsave(arg, varlen);
6705 if (name == NULL)
6706 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006707
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006708 // TODO: script var not supported?
6709 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
6710 &vimvaridx, &type, cctx) == FAIL)
6711 goto failed;
6712 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006713 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006714 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
6715 0, 0, type, name) == FAIL)
6716 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006717 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006718 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006719 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01006720 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006721 {
6722 semsg(_(e_variable_already_declared), arg);
6723 goto failed;
6724 }
6725
Bram Moolenaarea870692020-12-02 14:24:30 +01006726 if (STRNCMP(name, "s:", 2) == 0)
6727 {
6728 semsg(_(e_cannot_declare_script_variable_in_function), name);
6729 goto failed;
6730 }
6731
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006732 // Reserve a variable to store "var".
6733 // TODO: check for type
6734 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6735 if (var_lvar == NULL)
6736 // out of memory or used as an argument
6737 goto failed;
6738
6739 if (semicolon && idx == var_count - 1)
6740 var_lvar->lv_type = vartype;
6741 else
6742 var_lvar->lv_type = item_type;
6743 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
6744 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006745
6746 if (*p == ',' || *p == ';')
6747 ++p;
6748 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006749 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006750 }
6751
6752 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006753
6754failed:
6755 vim_free(name);
6756 drop_scope(cctx);
6757 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006758}
6759
6760/*
6761 * compile "endfor"
6762 */
6763 static char_u *
6764compile_endfor(char_u *arg, cctx_T *cctx)
6765{
6766 garray_T *instr = &cctx->ctx_instr;
6767 scope_T *scope = cctx->ctx_scope;
6768 forscope_T *forscope;
6769 isn_T *isn;
6770
6771 if (scope == NULL || scope->se_type != FOR_SCOPE)
6772 {
6773 emsg(_(e_for));
6774 return NULL;
6775 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006776 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006777 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006778 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006779
6780 // At end of ":for" scope jump back to the FOR instruction.
6781 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6782
6783 // Fill in the "end" label in the FOR statement so it can jump here
6784 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6785 isn->isn_arg.forloop.for_end = instr->ga_len;
6786
6787 // Fill in the "end" label any BREAK statements
6788 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6789
6790 // Below the ":for" scope drop the "expr" list from the stack.
6791 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6792 return NULL;
6793
6794 vim_free(scope);
6795
6796 return arg;
6797}
6798
6799/*
6800 * compile "while expr"
6801 *
6802 * Produces instructions:
6803 * top: EVAL expr Push result of "expr"
6804 * JUMP_IF_FALSE end jump if false
6805 * ... body ...
6806 * JUMP top Jump back to repeat
6807 * end:
6808 *
6809 */
6810 static char_u *
6811compile_while(char_u *arg, cctx_T *cctx)
6812{
6813 char_u *p = arg;
6814 garray_T *instr = &cctx->ctx_instr;
6815 scope_T *scope;
6816
6817 scope = new_scope(cctx, WHILE_SCOPE);
6818 if (scope == NULL)
6819 return NULL;
6820
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006821 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006822
6823 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006824 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006825 return NULL;
6826
Bram Moolenaar13106602020-10-04 16:06:05 +02006827 if (bool_on_stack(cctx) == FAIL)
6828 return FAIL;
6829
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006830 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006831 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006832 JUMP_IF_FALSE, cctx) == FAIL)
6833 return FAIL;
6834
6835 return p;
6836}
6837
6838/*
6839 * compile "endwhile"
6840 */
6841 static char_u *
6842compile_endwhile(char_u *arg, cctx_T *cctx)
6843{
6844 scope_T *scope = cctx->ctx_scope;
6845
6846 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6847 {
6848 emsg(_(e_while));
6849 return NULL;
6850 }
6851 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006852 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006853
6854 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006855 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006856
6857 // Fill in the "end" label in the WHILE statement so it can jump here.
6858 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006859 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006860
6861 vim_free(scope);
6862
6863 return arg;
6864}
6865
6866/*
6867 * compile "continue"
6868 */
6869 static char_u *
6870compile_continue(char_u *arg, cctx_T *cctx)
6871{
6872 scope_T *scope = cctx->ctx_scope;
6873
6874 for (;;)
6875 {
6876 if (scope == NULL)
6877 {
6878 emsg(_(e_continue));
6879 return NULL;
6880 }
6881 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6882 break;
6883 scope = scope->se_outer;
6884 }
6885
6886 // Jump back to the FOR or WHILE instruction.
6887 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006888 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6889 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006890 return arg;
6891}
6892
6893/*
6894 * compile "break"
6895 */
6896 static char_u *
6897compile_break(char_u *arg, cctx_T *cctx)
6898{
6899 scope_T *scope = cctx->ctx_scope;
6900 endlabel_T **el;
6901
6902 for (;;)
6903 {
6904 if (scope == NULL)
6905 {
6906 emsg(_(e_break));
6907 return NULL;
6908 }
6909 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6910 break;
6911 scope = scope->se_outer;
6912 }
6913
6914 // Jump to the end of the FOR or WHILE loop.
6915 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006916 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006917 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006918 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006919 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6920 return FAIL;
6921
6922 return arg;
6923}
6924
6925/*
6926 * compile "{" start of block
6927 */
6928 static char_u *
6929compile_block(char_u *arg, cctx_T *cctx)
6930{
6931 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6932 return NULL;
6933 return skipwhite(arg + 1);
6934}
6935
6936/*
6937 * compile end of block: drop one scope
6938 */
6939 static void
6940compile_endblock(cctx_T *cctx)
6941{
6942 scope_T *scope = cctx->ctx_scope;
6943
6944 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006945 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006946 vim_free(scope);
6947}
6948
6949/*
6950 * compile "try"
6951 * Creates a new scope for the try-endtry, pointing to the first catch and
6952 * finally.
6953 * Creates another scope for the "try" block itself.
6954 * TRY instruction sets up exception handling at runtime.
6955 *
6956 * "try"
6957 * TRY -> catch1, -> finally push trystack entry
6958 * ... try block
6959 * "throw {exception}"
6960 * EVAL {exception}
6961 * THROW create exception
6962 * ... try block
6963 * " catch {expr}"
6964 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006965 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006966 * EVAL {expr}
6967 * MATCH
6968 * JUMP nomatch -> catch2
6969 * CATCH remove exception
6970 * ... catch block
6971 * " catch"
6972 * JUMP -> finally
6973 * catch2: CATCH remove exception
6974 * ... catch block
6975 * " finally"
6976 * finally:
6977 * ... finally block
6978 * " endtry"
6979 * ENDTRY pop trystack entry, may rethrow
6980 */
6981 static char_u *
6982compile_try(char_u *arg, cctx_T *cctx)
6983{
6984 garray_T *instr = &cctx->ctx_instr;
6985 scope_T *try_scope;
6986 scope_T *scope;
6987
6988 // scope that holds the jumps that go to catch/finally/endtry
6989 try_scope = new_scope(cctx, TRY_SCOPE);
6990 if (try_scope == NULL)
6991 return NULL;
6992
6993 // "catch" is set when the first ":catch" is found.
6994 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006995 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006996 if (generate_instr(cctx, ISN_TRY) == NULL)
6997 return NULL;
6998
6999 // scope for the try block itself
7000 scope = new_scope(cctx, BLOCK_SCOPE);
7001 if (scope == NULL)
7002 return NULL;
7003
7004 return arg;
7005}
7006
7007/*
7008 * compile "catch {expr}"
7009 */
7010 static char_u *
7011compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7012{
7013 scope_T *scope = cctx->ctx_scope;
7014 garray_T *instr = &cctx->ctx_instr;
7015 char_u *p;
7016 isn_T *isn;
7017
7018 // end block scope from :try or :catch
7019 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7020 compile_endblock(cctx);
7021 scope = cctx->ctx_scope;
7022
7023 // Error if not in a :try scope
7024 if (scope == NULL || scope->se_type != TRY_SCOPE)
7025 {
7026 emsg(_(e_catch));
7027 return NULL;
7028 }
7029
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007030 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007031 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007032 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007033 return NULL;
7034 }
7035
7036 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007037 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007038 JUMP_ALWAYS, cctx) == FAIL)
7039 return NULL;
7040
7041 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007042 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007043 if (isn->isn_arg.try.try_catch == 0)
7044 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007045 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007046 {
7047 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007048 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007049 isn->isn_arg.jump.jump_where = instr->ga_len;
7050 }
7051
7052 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007053 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007054 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007055 scope->se_u.se_try.ts_caught_all = TRUE;
7056 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007057 }
7058 else
7059 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007060 char_u *end;
7061 char_u *pat;
7062 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007063 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007064 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007065
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007066 // Push v:exception, push {expr} and MATCH
7067 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7068
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007069 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007070 if (*end != *p)
7071 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007072 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007073 vim_free(tofree);
7074 return FAIL;
7075 }
7076 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007077 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007078 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007079 len = (int)(end - tofree);
7080 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007081 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007082 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007083 if (pat == NULL)
7084 return FAIL;
7085 if (generate_PUSHS(cctx, pat) == FAIL)
7086 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007088 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7089 return NULL;
7090
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007091 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007092 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7093 return NULL;
7094 }
7095
7096 if (generate_instr(cctx, ISN_CATCH) == NULL)
7097 return NULL;
7098
7099 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7100 return NULL;
7101 return p;
7102}
7103
7104 static char_u *
7105compile_finally(char_u *arg, cctx_T *cctx)
7106{
7107 scope_T *scope = cctx->ctx_scope;
7108 garray_T *instr = &cctx->ctx_instr;
7109 isn_T *isn;
7110
7111 // end block scope from :try or :catch
7112 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7113 compile_endblock(cctx);
7114 scope = cctx->ctx_scope;
7115
7116 // Error if not in a :try scope
7117 if (scope == NULL || scope->se_type != TRY_SCOPE)
7118 {
7119 emsg(_(e_finally));
7120 return NULL;
7121 }
7122
7123 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007124 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007125 if (isn->isn_arg.try.try_finally != 0)
7126 {
7127 emsg(_(e_finally_dup));
7128 return NULL;
7129 }
7130
7131 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007132 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007133
Bram Moolenaar585fea72020-04-02 22:33:21 +02007134 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007135 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007136 {
7137 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007138 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007139 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007140 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007141 }
7142
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007143 // TODO: set index in ts_finally_label jumps
7144
7145 return arg;
7146}
7147
7148 static char_u *
7149compile_endtry(char_u *arg, cctx_T *cctx)
7150{
7151 scope_T *scope = cctx->ctx_scope;
7152 garray_T *instr = &cctx->ctx_instr;
7153 isn_T *isn;
7154
7155 // end block scope from :catch or :finally
7156 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7157 compile_endblock(cctx);
7158 scope = cctx->ctx_scope;
7159
7160 // Error if not in a :try scope
7161 if (scope == NULL || scope->se_type != TRY_SCOPE)
7162 {
7163 if (scope == NULL)
7164 emsg(_(e_no_endtry));
7165 else if (scope->se_type == WHILE_SCOPE)
7166 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007167 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007168 emsg(_(e_endfor));
7169 else
7170 emsg(_(e_endif));
7171 return NULL;
7172 }
7173
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007174 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007175 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
7176 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007177 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007178 return NULL;
7179 }
7180
7181 // Fill in the "end" label in jumps at the end of the blocks, if not done
7182 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007183 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007184
7185 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02007186 if (isn->isn_arg.try.try_catch == 0)
7187 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007188 if (isn->isn_arg.try.try_finally == 0)
7189 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007190
7191 if (scope->se_u.se_try.ts_catch_label != 0)
7192 {
7193 // Last catch without match jumps here
7194 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7195 isn->isn_arg.jump.jump_where = instr->ga_len;
7196 }
7197
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007198 compile_endblock(cctx);
7199
7200 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
7201 return NULL;
7202 return arg;
7203}
7204
7205/*
7206 * compile "throw {expr}"
7207 */
7208 static char_u *
7209compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7210{
7211 char_u *p = skipwhite(arg);
7212
Bram Moolenaara5565e42020-05-09 15:44:01 +02007213 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007214 return NULL;
7215 if (may_generate_2STRING(-1, cctx) == FAIL)
7216 return NULL;
7217 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7218 return NULL;
7219
7220 return p;
7221}
7222
7223/*
7224 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007225 * compile "echomsg expr"
7226 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007227 * compile "execute expr"
7228 */
7229 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007230compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007231{
7232 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007233 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007234 int count = 0;
7235
7236 for (;;)
7237 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007238 if (ends_excmd2(prev, p))
7239 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007240 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007241 return NULL;
7242 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007243 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007244 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007245 }
7246
Bram Moolenaare4984292020-12-13 14:19:25 +01007247 if (count > 0)
7248 {
7249 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7250 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7251 else if (cmdidx == CMD_execute)
7252 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7253 else if (cmdidx == CMD_echomsg)
7254 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7255 else
7256 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7257 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007258 return p;
7259}
7260
7261/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007262 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01007263 * instruction to compute it and return OK.
7264 * Otherwise return FAIL, the caller must deal with any range.
7265 */
7266 static int
7267compile_variable_range(exarg_T *eap, cctx_T *cctx)
7268{
7269 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7270 char_u *p = skipdigits(eap->cmd);
7271
7272 if (p == range_end)
7273 return FAIL;
7274 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7275}
7276
7277/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007278 * :put r
7279 * :put ={expr}
7280 */
7281 static char_u *
7282compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7283{
7284 char_u *line = arg;
7285 linenr_T lnum;
7286 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007287 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007288
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007289 eap->regname = *line;
7290
7291 if (eap->regname == '=')
7292 {
7293 char_u *p = line + 1;
7294
7295 if (compile_expr0(&p, cctx) == FAIL)
7296 return NULL;
7297 line = p;
7298 }
7299 else if (eap->regname != NUL)
7300 ++line;
7301
Bram Moolenaar08597872020-12-10 19:43:40 +01007302 if (compile_variable_range(eap, cctx) == OK)
7303 {
7304 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
7305 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007306 else
Bram Moolenaar08597872020-12-10 19:43:40 +01007307 {
7308 // Either no range or a number.
7309 // "errormsg" will not be set because the range is ADDR_LINES.
7310 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01007311 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01007312 return NULL;
7313 if (eap->addr_count == 0)
7314 lnum = -1;
7315 else
7316 lnum = eap->line2;
7317 if (above)
7318 --lnum;
7319 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007320
7321 generate_PUT(cctx, eap->regname, lnum);
7322 return line;
7323}
7324
7325/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007326 * A command that is not compiled, execute with legacy code.
7327 */
7328 static char_u *
7329compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7330{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007331 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007332 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007333 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007334
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007335 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007336 goto theend;
7337
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007338 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007339 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007340 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007341 int usefilter = FALSE;
7342
7343 has_expr = argt & (EX_XFILE | EX_EXPAND);
7344
7345 // If the command can be followed by a bar, find the bar and truncate
7346 // it, so that the following command can be compiled.
7347 // The '|' is overwritten with a NUL, it is put back below.
7348 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7349 && *eap->arg == '!')
7350 // :w !filter or :r !filter or :r! filter
7351 usefilter = TRUE;
7352 if ((argt & EX_TRLBAR) && !usefilter)
7353 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007354 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007355 separate_nextcmd(eap);
7356 if (eap->nextcmd != NULL)
7357 nextcmd = eap->nextcmd;
7358 }
7359 }
7360
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007361 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
7362 {
7363 // expand filename in "syntax include [@group] filename"
7364 has_expr = TRUE;
7365 eap->arg = skipwhite(eap->arg + 7);
7366 if (*eap->arg == '@')
7367 eap->arg = skiptowhite(eap->arg);
7368 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007369
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007370 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
7371 && STRLEN(eap->arg) > 4)
7372 {
7373 int delim = *eap->arg;
7374
7375 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL);
7376 if (*p == delim)
7377 {
7378 eap->arg = p + 1;
7379 has_expr = TRUE;
7380 }
7381 }
7382
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007383 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007384 {
7385 int count = 0;
7386 char_u *start = skipwhite(line);
7387
7388 // :cmd xxx`=expr1`yyy`=expr2`zzz
7389 // PUSHS ":cmd xxx"
7390 // eval expr1
7391 // PUSHS "yyy"
7392 // eval expr2
7393 // PUSHS "zzz"
7394 // EXECCONCAT 5
7395 for (;;)
7396 {
7397 if (p > start)
7398 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007399 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007400 ++count;
7401 }
7402 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007403 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007404 return NULL;
7405 may_generate_2STRING(-1, cctx);
7406 ++count;
7407 p = skipwhite(p);
7408 if (*p != '`')
7409 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007410 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007411 return NULL;
7412 }
7413 start = p + 1;
7414
7415 p = (char_u *)strstr((char *)start, "`=");
7416 if (p == NULL)
7417 {
7418 if (*skipwhite(start) != NUL)
7419 {
7420 generate_PUSHS(cctx, vim_strsave(start));
7421 ++count;
7422 }
7423 break;
7424 }
7425 }
7426 generate_EXECCONCAT(cctx, count);
7427 }
7428 else
7429 generate_EXEC(cctx, line);
7430
7431theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007432 if (*nextcmd != NUL)
7433 {
7434 // the parser expects a pointer to the bar, put it back
7435 --nextcmd;
7436 *nextcmd = '|';
7437 }
7438
7439 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007440}
7441
7442/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007443 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007444 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007445 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007446 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007447add_def_function(ufunc_T *ufunc)
7448{
7449 dfunc_T *dfunc;
7450
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007451 if (def_functions.ga_len == 0)
7452 {
7453 // The first position is not used, so that a zero uf_dfunc_idx means it
7454 // wasn't set.
7455 if (ga_grow(&def_functions, 1) == FAIL)
7456 return FAIL;
7457 ++def_functions.ga_len;
7458 }
7459
Bram Moolenaar09689a02020-05-09 22:50:08 +02007460 // Add the function to "def_functions".
7461 if (ga_grow(&def_functions, 1) == FAIL)
7462 return FAIL;
7463 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7464 CLEAR_POINTER(dfunc);
7465 dfunc->df_idx = def_functions.ga_len;
7466 ufunc->uf_dfunc_idx = dfunc->df_idx;
7467 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01007468 dfunc->df_name = vim_strsave(ufunc->uf_name);
7469 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007470 ++def_functions.ga_len;
7471 return OK;
7472}
7473
7474/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007475 * After ex_function() has collected all the function lines: parse and compile
7476 * the lines into instructions.
7477 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007478 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
7479 * the return statement (used for lambda). When uf_ret_type is already set
7480 * then check that it matches.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007481 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007482 * This can be used recursively through compile_lambda(), which may reallocate
7483 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007484 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007485 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007486 int
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007487compile_def_function(ufunc_T *ufunc, int check_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007488{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007489 char_u *line = NULL;
7490 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007491 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007492 cctx_T cctx;
7493 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007494 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007495 int ret = FAIL;
7496 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007497 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007498 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007499 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007500
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007501 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01007502 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007503 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007504 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007505 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7506 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01007507 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007508 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007509 else
7510 {
7511 if (add_def_function(ufunc) == FAIL)
7512 return FAIL;
7513 new_def_function = TRUE;
7514 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007515
Bram Moolenaar985116a2020-07-12 17:31:09 +02007516 ufunc->uf_def_status = UF_COMPILING;
7517
Bram Moolenaara80faa82020-04-12 19:37:17 +02007518 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007519 cctx.ctx_ufunc = ufunc;
7520 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007521 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007522 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7523 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7524 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7525 cctx.ctx_type_list = &ufunc->uf_type_list;
7526 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7527 instr = &cctx.ctx_instr;
7528
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007529 // Set the context to the function, it may be compiled when called from
7530 // another script. Set the script version to the most modern one.
7531 // The line number will be set in next_line_from_context().
7532 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007533 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7534
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007535 // Make sure error messages are OK.
7536 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7537 if (do_estack_push)
7538 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007539 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007540
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007541 if (ufunc->uf_def_args.ga_len > 0)
7542 {
7543 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007544 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007545 int i;
7546 char_u *arg;
7547 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007548 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007549
7550 // Produce instructions for the default values of optional arguments.
7551 // Store the instruction index in uf_def_arg_idx[] so that we know
7552 // where to start when the function is called, depending on the number
7553 // of arguments.
7554 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7555 if (ufunc->uf_def_arg_idx == NULL)
7556 goto erret;
7557 for (i = 0; i < count; ++i)
7558 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007559 garray_T *stack = &cctx.ctx_type_stack;
7560 type_T *val_type;
7561 int arg_idx = first_def_arg + i;
7562
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007563 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7564 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007565 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007566 goto erret;
7567
7568 // If no type specified use the type of the default value.
7569 // Otherwise check that the default value type matches the
7570 // specified type.
7571 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7572 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007573 {
7574 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007575 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007576 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007577 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7578 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007579 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007580
7581 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007582 goto erret;
7583 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007584 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007585
7586 if (did_set_arg_type)
7587 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007588 }
7589
7590 /*
7591 * Loop over all the lines of the function and generate instructions.
7592 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007593 for (;;)
7594 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007595 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007596 int starts_with_colon = FALSE;
7597 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007598 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007599
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007600 // Bail out on the first error to avoid a flood of errors and report
7601 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007602 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007603 goto erret;
7604
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007605 if (line != NULL && *line == '|')
7606 // the line continues after a '|'
7607 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007608 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007609 && !(*line == '#' && (line == cctx.ctx_line_start
7610 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007611 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007612 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007613 goto erret;
7614 }
7615 else
7616 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007617 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007618 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007619 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007620 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007621 }
7622
Bram Moolenaara80faa82020-04-12 19:37:17 +02007623 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007624 ea.cmdlinep = &line;
7625 ea.cmd = skipwhite(line);
7626
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007627 // Some things can be recognized by the first character.
7628 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007629 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007630 case '#':
Bram Moolenaare0de1712020-12-02 17:36:54 +01007631 // "#" starts a comment
7632 line = (char_u *)"";
7633 continue;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007634
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007635 case '}':
7636 {
7637 // "}" ends a block scope
7638 scopetype_T stype = cctx.ctx_scope == NULL
7639 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007640
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007641 if (stype == BLOCK_SCOPE)
7642 {
7643 compile_endblock(&cctx);
7644 line = ea.cmd;
7645 }
7646 else
7647 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007648 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007649 goto erret;
7650 }
7651 if (line != NULL)
7652 line = skipwhite(ea.cmd + 1);
7653 continue;
7654 }
7655
7656 case '{':
7657 // "{" starts a block scope
7658 // "{'a': 1}->func() is something else
7659 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7660 {
7661 line = compile_block(ea.cmd, &cctx);
7662 continue;
7663 }
7664 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007665 }
7666
7667 /*
7668 * COMMAND MODIFIERS
7669 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007670 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007671 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7672 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007673 {
7674 if (errormsg != NULL)
7675 goto erret;
7676 // empty line or comment
7677 line = (char_u *)"";
7678 continue;
7679 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007680 generate_cmdmods(&cctx, &local_cmdmod);
7681 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007682
Bram Moolenaare88c8e82020-11-01 17:03:37 +01007683 // Check if there was a colon after the last command modifier or before
7684 // the current position.
7685 for (p = ea.cmd; p >= line; --p)
7686 {
7687 if (*p == ':')
7688 starts_with_colon = TRUE;
7689 if (p < ea.cmd && !VIM_ISWHITE(*p))
7690 break;
7691 }
7692
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007693 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007694 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007695 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007696 {
7697 if (*ea.cmd == '(')
7698 // not for "call()"
7699 ea.cmd = p;
7700 else
7701 ea.cmd = skipwhite(ea.cmd);
7702 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007703
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007704 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007705 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007706 char_u *pskip;
7707
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007708 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007709 // find what follows.
7710 // Skip over "var.member", "var[idx]" and the like.
7711 // Also "&opt = val", "$ENV = val" and "@r = val".
7712 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007713 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007714 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007715 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007716 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007717 char_u *var_end;
7718 int oplen;
7719 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007720
Bram Moolenaar65821722020-08-02 18:58:54 +02007721 if (ea.cmd[0] == '@')
7722 var_end = ea.cmd + 2;
7723 else
7724 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007725 FNE_CHECK_START | FNE_INCL_BR);
7726 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007727 if (oplen > 0)
7728 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007729 size_t len = p - ea.cmd;
7730
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007731 // Recognize an assignment if we recognize the variable
7732 // name:
7733 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007734 // "local = expr" where "local" is a local var.
7735 // "script = expr" where "script" is a script-local var.
7736 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007737 // "&opt = expr"
7738 // "$ENV = expr"
7739 // "@r = expr"
7740 if (*ea.cmd == '&'
7741 || *ea.cmd == '$'
7742 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007743 || ((len) > 2 && ea.cmd[1] == ':')
Bram Moolenaar709664c2020-12-12 14:33:41 +01007744 || lookup_local(ea.cmd, len, NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007745 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007746 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007747 || script_var_exists(ea.cmd, len,
7748 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007749 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007750 {
7751 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007752 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007753 goto erret;
Bram Moolenaarf665e972020-12-05 19:17:16 +01007754 goto nextline;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007755 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007756 }
7757 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007758
7759 if (*ea.cmd == '[')
7760 {
7761 // [var, var] = expr
7762 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7763 if (line == NULL)
7764 goto erret;
7765 if (line != ea.cmd)
Bram Moolenaarf665e972020-12-05 19:17:16 +01007766 goto nextline;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007767 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007768 }
7769
7770 /*
7771 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007772 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007773 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007774 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007775 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007776 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007777 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007778 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007779 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007780 if (!starts_with_colon)
7781 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01007782 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007783 goto erret;
7784 }
7785 if (ends_excmd2(line, ea.cmd))
7786 {
7787 // A range without a command: jump to the line.
7788 // TODO: compile to a more efficient command, possibly
7789 // calling parse_cmd_address().
7790 ea.cmdidx = CMD_SIZE;
7791 line = compile_exec(line, &ea, &cctx);
7792 goto nextline;
7793 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007794 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007795 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007796 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +01007797 : (int (*)(char_u *, size_t, void *, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007798 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007799
7800 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7801 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007802 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007803 {
7804 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01007805 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007806 }
7807
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007808 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007809 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007810 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01007811 // CMD_var cannot happen, compile_assignment() above would be
7812 // used. Most likely an assignment to a non-existing variable.
7813 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007814 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007815 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007816 }
7817
Bram Moolenaar3988f642020-08-27 22:43:03 +02007818 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007819 && ea.cmdidx != CMD_elseif
7820 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007821 && ea.cmdidx != CMD_endif
7822 && ea.cmdidx != CMD_endfor
7823 && ea.cmdidx != CMD_endwhile
7824 && ea.cmdidx != CMD_catch
7825 && ea.cmdidx != CMD_finally
7826 && ea.cmdidx != CMD_endtry)
7827 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007828 emsg(_(e_unreachable_code_after_return));
7829 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007830 }
7831
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007832 p = skipwhite(p);
7833 if (ea.cmdidx != CMD_SIZE
7834 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7835 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007836 if (ea.cmdidx >= 0)
7837 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007838 if ((ea.argt & EX_BANG) && *p == '!')
7839 {
7840 ea.forceit = TRUE;
7841 p = skipwhite(p + 1);
7842 }
7843 }
7844
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007845 switch (ea.cmdidx)
7846 {
7847 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007848 ea.arg = p;
7849 line = compile_nested_function(&ea, &cctx);
7850 break;
7851
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007852 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007853 // TODO: should we allow this, e.g. to declare a global
7854 // function?
7855 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007856 goto erret;
7857
7858 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007859 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007860 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007861 break;
7862
7863 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02007864 emsg(_(e_cannot_use_let_in_vim9_script));
7865 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007866 case CMD_var:
7867 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007868 case CMD_const:
7869 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007870 if (line == p)
7871 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007872 break;
7873
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007874 case CMD_unlet:
7875 case CMD_unlockvar:
7876 case CMD_lockvar:
7877 line = compile_unletlock(p, &ea, &cctx);
7878 break;
7879
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007880 case CMD_import:
7881 line = compile_import(p, &cctx);
7882 break;
7883
7884 case CMD_if:
7885 line = compile_if(p, &cctx);
7886 break;
7887 case CMD_elseif:
7888 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007889 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007890 break;
7891 case CMD_else:
7892 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007893 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007894 break;
7895 case CMD_endif:
7896 line = compile_endif(p, &cctx);
7897 break;
7898
7899 case CMD_while:
7900 line = compile_while(p, &cctx);
7901 break;
7902 case CMD_endwhile:
7903 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007904 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007905 break;
7906
7907 case CMD_for:
7908 line = compile_for(p, &cctx);
7909 break;
7910 case CMD_endfor:
7911 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007912 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007913 break;
7914 case CMD_continue:
7915 line = compile_continue(p, &cctx);
7916 break;
7917 case CMD_break:
7918 line = compile_break(p, &cctx);
7919 break;
7920
7921 case CMD_try:
7922 line = compile_try(p, &cctx);
7923 break;
7924 case CMD_catch:
7925 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007926 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007927 break;
7928 case CMD_finally:
7929 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007930 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007931 break;
7932 case CMD_endtry:
7933 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007934 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007935 break;
7936 case CMD_throw:
7937 line = compile_throw(p, &cctx);
7938 break;
7939
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007940 case CMD_eval:
7941 if (compile_expr0(&p, &cctx) == FAIL)
7942 goto erret;
7943
Bram Moolenaar3988f642020-08-27 22:43:03 +02007944 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007945 generate_instr_drop(&cctx, ISN_DROP, 1);
7946
7947 line = skipwhite(p);
7948 break;
7949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007950 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007951 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007952 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007953 case CMD_echomsg:
7954 case CMD_echoerr:
7955 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007956 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007957
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007958 case CMD_put:
7959 ea.cmd = cmd;
7960 line = compile_put(p, &ea, &cctx);
7961 break;
7962
Bram Moolenaar3988f642020-08-27 22:43:03 +02007963 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007964
Bram Moolenaarae616492020-07-28 20:07:27 +02007965 case CMD_append:
7966 case CMD_change:
7967 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007968 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007969 case CMD_xit:
7970 not_in_vim9(&ea);
7971 goto erret;
7972
Bram Moolenaar002262f2020-07-08 17:47:57 +02007973 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007974 if (cctx.ctx_skip != SKIP_YES)
7975 {
7976 semsg(_(e_invalid_command_str), ea.cmd);
7977 goto erret;
7978 }
7979 // We don't check for a next command here.
7980 line = (char_u *)"";
7981 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007982
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007983 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007984 if (cctx.ctx_skip == SKIP_YES)
7985 {
7986 // We don't check for a next command here.
7987 line = (char_u *)"";
7988 }
7989 else
7990 {
7991 // Not recognized, execute with do_cmdline_cmd().
7992 ea.arg = p;
7993 line = compile_exec(line, &ea, &cctx);
7994 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007995 break;
7996 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007997nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007998 if (line == NULL)
7999 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008000 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008001
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008002 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008003 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008004
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008005 if (cctx.ctx_type_stack.ga_len < 0)
8006 {
8007 iemsg("Type stack underflow");
8008 goto erret;
8009 }
8010 }
8011
8012 if (cctx.ctx_scope != NULL)
8013 {
8014 if (cctx.ctx_scope->se_type == IF_SCOPE)
8015 emsg(_(e_endif));
8016 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8017 emsg(_(e_endwhile));
8018 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8019 emsg(_(e_endfor));
8020 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008021 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008022 goto erret;
8023 }
8024
Bram Moolenaarefd88552020-06-18 20:50:10 +02008025 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008026 {
8027 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8028 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008029 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008030 goto erret;
8031 }
8032
8033 // Return zero if there is no return at the end.
8034 generate_PUSHNR(&cctx, 0);
8035 generate_instr(&cctx, ISN_RETURN);
8036 }
8037
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008038 {
8039 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8040 + ufunc->uf_dfunc_idx;
8041 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008042 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008043 dfunc->df_instr = instr->ga_data;
8044 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008045 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008046 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008047 if (cctx.ctx_outer_used)
8048 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008049 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008050 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008051
8052 ret = OK;
8053
8054erret:
8055 if (ret == FAIL)
8056 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008057 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008058 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8059 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008060
8061 for (idx = 0; idx < instr->ga_len; ++idx)
8062 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008063 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008064 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008065
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008066 // If using the last entry in the table and it was added above, we
8067 // might as well remove it.
8068 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008069 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008070 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008071 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008072 ufunc->uf_dfunc_idx = 0;
8073 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008074 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008075
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008076 while (cctx.ctx_scope != NULL)
8077 drop_scope(&cctx);
8078
Bram Moolenaar20431c92020-03-20 18:39:46 +01008079 // Don't execute this function body.
8080 ga_clear_strings(&ufunc->uf_lines);
8081
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008082 if (errormsg != NULL)
8083 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008084 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008085 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008086 }
8087
8088 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008089 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008090 if (do_estack_push)
8091 estack_pop();
8092
Bram Moolenaar20431c92020-03-20 18:39:46 +01008093 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008094 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008095 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008096 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008097}
8098
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008099 void
8100set_function_type(ufunc_T *ufunc)
8101{
8102 int varargs = ufunc->uf_va_name != NULL;
8103 int argcount = ufunc->uf_args.ga_len;
8104
8105 // Create a type for the function, with the return type and any
8106 // argument types.
8107 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8108 // The type is included in "tt_args".
8109 if (argcount > 0 || varargs)
8110 {
8111 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8112 argcount, &ufunc->uf_type_list);
8113 // Add argument types to the function type.
8114 if (func_type_add_arg_types(ufunc->uf_func_type,
8115 argcount + varargs,
8116 &ufunc->uf_type_list) == FAIL)
8117 return;
8118 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8119 ufunc->uf_func_type->tt_min_argcount =
8120 argcount - ufunc->uf_def_args.ga_len;
8121 if (ufunc->uf_arg_types == NULL)
8122 {
8123 int i;
8124
8125 // lambda does not have argument types.
8126 for (i = 0; i < argcount; ++i)
8127 ufunc->uf_func_type->tt_args[i] = &t_any;
8128 }
8129 else
8130 mch_memmove(ufunc->uf_func_type->tt_args,
8131 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8132 if (varargs)
8133 {
8134 ufunc->uf_func_type->tt_args[argcount] =
8135 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8136 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8137 }
8138 }
8139 else
8140 // No arguments, can use a predefined type.
8141 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8142 argcount, &ufunc->uf_type_list);
8143}
8144
8145
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008146/*
8147 * Delete an instruction, free what it contains.
8148 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008149 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008150delete_instr(isn_T *isn)
8151{
8152 switch (isn->isn_type)
8153 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008154 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008155 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008156 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008157 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008158 case ISN_LOADENV:
8159 case ISN_LOADG:
8160 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008161 case ISN_LOADT:
8162 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008163 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008164 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008165 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008166 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008167 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008168 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008169 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008170 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008171 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008172 case ISN_STOREW:
8173 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008174 vim_free(isn->isn_arg.string);
8175 break;
8176
8177 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008178 case ISN_STORES:
8179 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008180 break;
8181
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008182 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008183 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008184 vim_free(isn->isn_arg.unlet.ul_name);
8185 break;
8186
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008187 case ISN_STOREOPT:
8188 vim_free(isn->isn_arg.storeopt.so_name);
8189 break;
8190
8191 case ISN_PUSHBLOB: // push blob isn_arg.blob
8192 blob_unref(isn->isn_arg.blob);
8193 break;
8194
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008195 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008196#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008197 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008198#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008199 break;
8200
8201 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008202#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008203 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008204#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008205 break;
8206
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008207 case ISN_UCALL:
8208 vim_free(isn->isn_arg.ufunc.cuf_name);
8209 break;
8210
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008211 case ISN_FUNCREF:
8212 {
8213 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8214 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01008215 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008216
Bram Moolenaar077a4232020-12-22 18:33:27 +01008217 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
8218 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02008219 }
8220 break;
8221
8222 case ISN_DCALL:
8223 {
8224 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8225 + isn->isn_arg.dfunc.cdf_idx;
8226
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008227 if (dfunc->df_ufunc != NULL
8228 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008229 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008230 }
8231 break;
8232
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008233 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008234 {
8235 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8236 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8237
8238 if (ufunc != NULL)
8239 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008240 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02008241 func_ptr_unref(ufunc);
8242 }
8243
8244 vim_free(lambda);
8245 vim_free(isn->isn_arg.newfunc.nf_global);
8246 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008247 break;
8248
Bram Moolenaar5e654232020-09-16 15:22:00 +02008249 case ISN_CHECKTYPE:
8250 free_type(isn->isn_arg.type.ct_type);
8251 break;
8252
Bram Moolenaar02194d22020-10-24 23:08:38 +02008253 case ISN_CMDMOD:
8254 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8255 ->cmod_filter_regmatch.regprog);
8256 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8257 break;
8258
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008259 case ISN_LOADSCRIPT:
8260 case ISN_STORESCRIPT:
8261 vim_free(isn->isn_arg.script.scriptref);
8262 break;
8263
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008264 case ISN_2BOOL:
8265 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008266 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008267 case ISN_ADDBLOB:
8268 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008269 case ISN_ANYINDEX:
8270 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008271 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008272 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008273 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008274 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008275 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008276 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008277 case ISN_COMPAREANY:
8278 case ISN_COMPAREBLOB:
8279 case ISN_COMPAREBOOL:
8280 case ISN_COMPAREDICT:
8281 case ISN_COMPAREFLOAT:
8282 case ISN_COMPAREFUNC:
8283 case ISN_COMPARELIST:
8284 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008285 case ISN_COMPARESPECIAL:
8286 case ISN_COMPARESTRING:
8287 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008288 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008289 case ISN_DROP:
8290 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008291 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008292 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008293 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008294 case ISN_EXECCONCAT:
8295 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008296 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008297 case ISN_GETITEM:
8298 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008299 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008300 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008301 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008302 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008303 case ISN_LOADBDICT:
8304 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008305 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008306 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008307 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008308 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008309 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008310 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008311 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008312 case ISN_NEGATENR:
8313 case ISN_NEWDICT:
8314 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008315 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008316 case ISN_OPFLOAT:
8317 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008318 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008319 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008320 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008321 case ISN_PUSHF:
8322 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008323 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008324 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008325 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008326 case ISN_SHUFFLE:
8327 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008328 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01008329 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008330 case ISN_STORENR:
8331 case ISN_STOREOUTER:
8332 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008333 case ISN_STOREV:
8334 case ISN_STRINDEX:
8335 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008336 case ISN_THROW:
8337 case ISN_TRY:
Bram Moolenaar792f7862020-11-23 08:31:18 +01008338 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008339 // nothing allocated
8340 break;
8341 }
8342}
8343
8344/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008345 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01008346 */
8347 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008348delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008349{
8350 int idx;
8351
8352 ga_clear(&dfunc->df_def_args_isn);
8353
8354 if (dfunc->df_instr != NULL)
8355 {
8356 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
8357 delete_instr(dfunc->df_instr + idx);
8358 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008359 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008360 }
8361
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008362 if (mark_deleted)
8363 dfunc->df_deleted = TRUE;
8364 if (dfunc->df_ufunc != NULL)
8365 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008366}
8367
8368/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008369 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008370 * function, unless another user function still uses it.
8371 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008372 */
8373 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008374unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008375{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008376 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008377 {
8378 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8379 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008380
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008381 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008382 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008383 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008384 ufunc->uf_dfunc_idx = 0;
8385 if (dfunc->df_ufunc == ufunc)
8386 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008387 }
8388}
8389
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008390/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008391 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008392 */
8393 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008394link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008395{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008396 if (ufunc->uf_dfunc_idx > 0)
8397 {
8398 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8399 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008400
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008401 ++dfunc->df_refcount;
8402 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008403}
8404
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008405#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008406/*
8407 * Free all functions defined with ":def".
8408 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008409 void
8410free_def_functions(void)
8411{
Bram Moolenaar20431c92020-03-20 18:39:46 +01008412 int idx;
8413
8414 for (idx = 0; idx < def_functions.ga_len; ++idx)
8415 {
8416 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
8417
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008418 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008419 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008420 }
8421
8422 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008423}
8424#endif
8425
8426
8427#endif // FEAT_EVAL