blob: fdc805b48e06f54ec8733786ea096800faf19ec6 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
111 int lv_from_outer; // when TRUE using ctx_outer scope
112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200129 int ctx_has_closure; // set to one if a closures was created in
130 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200144
Bram Moolenaar02194d22020-10-24 23:08:38 +0200145 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100146};
147
Bram Moolenaar20431c92020-03-20 18:39:46 +0100148static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149
150/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100151 * Lookup variable "name" in the local scope and return it in "lvar".
152 * "lvar->lv_from_outer" is set accordingly.
153 * If "lvar" is NULL only check if the variable can be found.
154 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100155 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100156 static int
157lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158{
159 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100160 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100161
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100162 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100163 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200164
165 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100166 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
167 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100168 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
169 if (STRNCMP(name, lvp->lv_name, len) == 0
170 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200171 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100172 if (lvar != NULL)
173 {
174 *lvar = *lvp;
175 lvar->lv_from_outer = FALSE;
176 }
177 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200180
181 // Find local in outer function scope.
182 if (cctx->ctx_outer != NULL)
183 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100184 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200185 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100186 if (lvar != NULL)
187 {
188 cctx->ctx_outer_used = TRUE;
189 lvar->lv_from_outer = TRUE;
190 }
191 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200192 }
193 }
194
Bram Moolenaar709664c2020-12-12 14:33:41 +0100195 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196}
197
198/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200199 * Lookup an argument in the current function and an enclosing function.
200 * Returns the argument index in "idxp"
201 * Returns the argument type in "type"
202 * Sets "gen_load_outer" to TRUE if found in outer scope.
203 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100204 */
205 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200206arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200207 char_u *name,
208 size_t len,
209 int *idxp,
210 type_T **type,
211 int *gen_load_outer,
212 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213{
214 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200215 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100217 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200218 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
220 {
221 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
222
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200223 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
224 {
225 if (idxp != NULL)
226 {
227 // Arguments are located above the frame pointer. One further
228 // if there is a vararg argument
229 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
230 + STACK_FRAME_SIZE)
231 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
232
233 if (cctx->ctx_ufunc->uf_arg_types != NULL)
234 *type = cctx->ctx_ufunc->uf_arg_types[idx];
235 else
236 *type = &t_any;
237 }
238 return OK;
239 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100240 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100241
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200242 va_name = cctx->ctx_ufunc->uf_va_name;
243 if (va_name != NULL
244 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
245 {
246 if (idxp != NULL)
247 {
248 // varargs is always the last argument
249 *idxp = -STACK_FRAME_SIZE - 1;
250 *type = cctx->ctx_ufunc->uf_va_type;
251 }
252 return OK;
253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200255 if (cctx->ctx_outer != NULL)
256 {
257 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200258 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200259 == OK)
260 {
261 *gen_load_outer = TRUE;
262 return OK;
263 }
264 }
265
266 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100267}
268
269/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200270 * Lookup a script-local variable in the current script, possibly defined in a
271 * block that contains the function "cctx->ctx_ufunc".
272 * "cctx" is NULL at the script level.
273 * if "len" is <= 0 "name" must be NUL terminated.
274 * Return NULL when not found.
275 */
276 static sallvar_T *
277find_script_var(char_u *name, size_t len, cctx_T *cctx)
278{
279 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
280 hashitem_T *hi;
281 int cc;
282 sallvar_T *sav;
283 ufunc_T *ufunc;
284
285 // Find the list of all script variables with the right name.
286 if (len > 0)
287 {
288 cc = name[len];
289 name[len] = NUL;
290 }
291 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
292 if (len > 0)
293 name[len] = cc;
294 if (HASHITEM_EMPTY(hi))
295 return NULL;
296
297 sav = HI2SAV(hi);
298 if (sav->sav_block_id == 0 || cctx == NULL)
299 // variable defined in the script scope or not in a function.
300 return sav;
301
302 // Go over the variables with this name and find one that was visible
303 // from the function.
304 ufunc = cctx->ctx_ufunc;
305 while (sav != NULL)
306 {
307 int idx;
308
309 // Go over the blocks that this function was defined in. If the
310 // variable block ID matches it was visible to the function.
311 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
312 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
313 return sav;
314 sav = sav->sav_next;
315 }
316
317 return NULL;
318}
319
320/*
Bram 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)
934 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200935 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200936
937 // A 0 or 1 number can also be used as a bool.
938 if (type != NULL)
939 {
940 type->tt_type = VAR_NUMBER;
941 type->tt_flags = TTFLAG_BOOL_OK;
942 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
943 }
944 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100945 return OK;
946}
947
948/*
949 * Generate an ISN_PUSHBOOL instruction.
950 */
951 static int
952generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
953{
954 isn_T *isn;
955
Bram Moolenaar080457c2020-03-03 21:53:32 +0100956 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100957 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
958 return FAIL;
959 isn->isn_arg.number = number;
960
961 return OK;
962}
963
964/*
965 * Generate an ISN_PUSHSPEC instruction.
966 */
967 static int
968generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
969{
970 isn_T *isn;
971
Bram Moolenaar080457c2020-03-03 21:53:32 +0100972 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100973 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
974 return FAIL;
975 isn->isn_arg.number = number;
976
977 return OK;
978}
979
980#ifdef FEAT_FLOAT
981/*
982 * Generate an ISN_PUSHF instruction.
983 */
984 static int
985generate_PUSHF(cctx_T *cctx, float_T fnumber)
986{
987 isn_T *isn;
988
Bram Moolenaar080457c2020-03-03 21:53:32 +0100989 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
991 return FAIL;
992 isn->isn_arg.fnumber = fnumber;
993
994 return OK;
995}
996#endif
997
998/*
999 * Generate an ISN_PUSHS instruction.
1000 * Consumes "str".
1001 */
1002 static int
1003generate_PUSHS(cctx_T *cctx, char_u *str)
1004{
1005 isn_T *isn;
1006
Bram Moolenaar080457c2020-03-03 21:53:32 +01001007 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001008 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1009 return FAIL;
1010 isn->isn_arg.string = str;
1011
1012 return OK;
1013}
1014
1015/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001016 * Generate an ISN_PUSHCHANNEL instruction.
1017 * Consumes "channel".
1018 */
1019 static int
1020generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1021{
1022 isn_T *isn;
1023
Bram Moolenaar080457c2020-03-03 21:53:32 +01001024 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001025 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1026 return FAIL;
1027 isn->isn_arg.channel = channel;
1028
1029 return OK;
1030}
1031
1032/*
1033 * Generate an ISN_PUSHJOB instruction.
1034 * Consumes "job".
1035 */
1036 static int
1037generate_PUSHJOB(cctx_T *cctx, job_T *job)
1038{
1039 isn_T *isn;
1040
Bram Moolenaar080457c2020-03-03 21:53:32 +01001041 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001042 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001043 return FAIL;
1044 isn->isn_arg.job = job;
1045
1046 return OK;
1047}
1048
1049/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050 * Generate an ISN_PUSHBLOB instruction.
1051 * Consumes "blob".
1052 */
1053 static int
1054generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1055{
1056 isn_T *isn;
1057
Bram Moolenaar080457c2020-03-03 21:53:32 +01001058 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1060 return FAIL;
1061 isn->isn_arg.blob = blob;
1062
1063 return OK;
1064}
1065
1066/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001067 * Generate an ISN_PUSHFUNC instruction with name "name".
1068 * Consumes "name".
1069 */
1070 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001071generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001072{
1073 isn_T *isn;
1074
Bram Moolenaar080457c2020-03-03 21:53:32 +01001075 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001076 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001077 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001078 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001079
1080 return OK;
1081}
1082
1083/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001084 * Generate an ISN_GETITEM instruction with "index".
1085 */
1086 static int
1087generate_GETITEM(cctx_T *cctx, int index)
1088{
1089 isn_T *isn;
1090 garray_T *stack = &cctx->ctx_type_stack;
1091 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1092 type_T *item_type = &t_any;
1093
1094 RETURN_OK_IF_SKIP(cctx);
1095
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001096 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001097 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001098 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001099 emsg(_(e_listreq));
1100 return FAIL;
1101 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001102 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001103 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1104 return FAIL;
1105 isn->isn_arg.number = index;
1106
1107 // add the item type to the type stack
1108 if (ga_grow(stack, 1) == FAIL)
1109 return FAIL;
1110 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1111 ++stack->ga_len;
1112 return OK;
1113}
1114
1115/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001116 * Generate an ISN_SLICE instruction with "count".
1117 */
1118 static int
1119generate_SLICE(cctx_T *cctx, int count)
1120{
1121 isn_T *isn;
1122
1123 RETURN_OK_IF_SKIP(cctx);
1124 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1125 return FAIL;
1126 isn->isn_arg.number = count;
1127 return OK;
1128}
1129
1130/*
1131 * Generate an ISN_CHECKLEN instruction with "min_len".
1132 */
1133 static int
1134generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1135{
1136 isn_T *isn;
1137
1138 RETURN_OK_IF_SKIP(cctx);
1139
1140 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1141 return FAIL;
1142 isn->isn_arg.checklen.cl_min_len = min_len;
1143 isn->isn_arg.checklen.cl_more_OK = more_OK;
1144
1145 return OK;
1146}
1147
1148/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149 * Generate an ISN_STORE instruction.
1150 */
1151 static int
1152generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1153{
1154 isn_T *isn;
1155
Bram Moolenaar080457c2020-03-03 21:53:32 +01001156 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001157 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1158 return FAIL;
1159 if (name != NULL)
1160 isn->isn_arg.string = vim_strsave(name);
1161 else
1162 isn->isn_arg.number = idx;
1163
1164 return OK;
1165}
1166
1167/*
1168 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1169 */
1170 static int
1171generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1172{
1173 isn_T *isn;
1174
Bram Moolenaar080457c2020-03-03 21:53:32 +01001175 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001176 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1177 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001178 isn->isn_arg.storenr.stnr_idx = idx;
1179 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001180
1181 return OK;
1182}
1183
1184/*
1185 * Generate an ISN_STOREOPT instruction
1186 */
1187 static int
1188generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1189{
1190 isn_T *isn;
1191
Bram Moolenaar080457c2020-03-03 21:53:32 +01001192 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001193 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194 return FAIL;
1195 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1196 isn->isn_arg.storeopt.so_flags = opt_flags;
1197
1198 return OK;
1199}
1200
1201/*
1202 * Generate an ISN_LOAD or similar instruction.
1203 */
1204 static int
1205generate_LOAD(
1206 cctx_T *cctx,
1207 isntype_T isn_type,
1208 int idx,
1209 char_u *name,
1210 type_T *type)
1211{
1212 isn_T *isn;
1213
Bram Moolenaar080457c2020-03-03 21:53:32 +01001214 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1216 return FAIL;
1217 if (name != NULL)
1218 isn->isn_arg.string = vim_strsave(name);
1219 else
1220 isn->isn_arg.number = idx;
1221
1222 return OK;
1223}
1224
1225/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001226 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001227 */
1228 static int
1229generate_LOADV(
1230 cctx_T *cctx,
1231 char_u *name,
1232 int error)
1233{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001234 int di_flags;
1235 int vidx = find_vim_var(name, &di_flags);
1236 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001237
Bram Moolenaar080457c2020-03-03 21:53:32 +01001238 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001239 if (vidx < 0)
1240 {
1241 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001242 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001243 return FAIL;
1244 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001245 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001246
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001247 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001248}
1249
1250/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001251 * Generate an ISN_UNLET instruction.
1252 */
1253 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001254generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001255{
1256 isn_T *isn;
1257
1258 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001259 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001260 return FAIL;
1261 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1262 isn->isn_arg.unlet.ul_forceit = forceit;
1263
1264 return OK;
1265}
1266
1267/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001268 * Generate an ISN_LOCKCONST instruction.
1269 */
1270 static int
1271generate_LOCKCONST(cctx_T *cctx)
1272{
1273 isn_T *isn;
1274
1275 RETURN_OK_IF_SKIP(cctx);
1276 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1277 return FAIL;
1278 return OK;
1279}
1280
1281/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001282 * Generate an ISN_LOADS instruction.
1283 */
1284 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001285generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001286 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001287 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001289 int sid,
1290 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291{
1292 isn_T *isn;
1293
Bram Moolenaar080457c2020-03-03 21:53:32 +01001294 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001295 if (isn_type == ISN_LOADS)
1296 isn = generate_instr_type(cctx, isn_type, type);
1297 else
1298 isn = generate_instr_drop(cctx, isn_type, 1);
1299 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001300 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001301 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1302 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303
1304 return OK;
1305}
1306
1307/*
1308 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1309 */
1310 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001311generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312 cctx_T *cctx,
1313 isntype_T isn_type,
1314 int sid,
1315 int idx,
1316 type_T *type)
1317{
1318 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001319 scriptref_T *sref;
1320 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321
Bram Moolenaar080457c2020-03-03 21:53:32 +01001322 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001323 if (isn_type == ISN_LOADSCRIPT)
1324 isn = generate_instr_type(cctx, isn_type, type);
1325 else
1326 isn = generate_instr_drop(cctx, isn_type, 1);
1327 if (isn == NULL)
1328 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001329
1330 // This requires three arguments, which doesn't fit in an instruction, thus
1331 // we need to allocate a struct for this.
1332 sref = ALLOC_ONE(scriptref_T);
1333 if (sref == NULL)
1334 return FAIL;
1335 isn->isn_arg.script.scriptref = sref;
1336 sref->sref_sid = sid;
1337 sref->sref_idx = idx;
1338 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339 return OK;
1340}
1341
1342/*
1343 * Generate an ISN_NEWLIST instruction.
1344 */
1345 static int
1346generate_NEWLIST(cctx_T *cctx, int count)
1347{
1348 isn_T *isn;
1349 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001350 type_T *type;
1351 type_T *member;
1352
Bram Moolenaar080457c2020-03-03 21:53:32 +01001353 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1355 return FAIL;
1356 isn->isn_arg.number = count;
1357
Bram Moolenaar127542b2020-08-09 17:22:04 +02001358 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001359 if (count == 0)
1360 member = &t_void;
1361 else
1362 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001363 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1364 cctx->ctx_type_list);
1365 type = get_list_type(member, cctx->ctx_type_list);
1366
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001367 // drop the value types
1368 stack->ga_len -= count;
1369
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001370 // add the list type to the type stack
1371 if (ga_grow(stack, 1) == FAIL)
1372 return FAIL;
1373 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1374 ++stack->ga_len;
1375
1376 return OK;
1377}
1378
1379/*
1380 * Generate an ISN_NEWDICT instruction.
1381 */
1382 static int
1383generate_NEWDICT(cctx_T *cctx, int count)
1384{
1385 isn_T *isn;
1386 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 type_T *type;
1388 type_T *member;
1389
Bram Moolenaar080457c2020-03-03 21:53:32 +01001390 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001391 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1392 return FAIL;
1393 isn->isn_arg.number = count;
1394
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001395 if (count == 0)
1396 member = &t_void;
1397 else
1398 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001399 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1400 cctx->ctx_type_list);
1401 type = get_dict_type(member, cctx->ctx_type_list);
1402
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403 // drop the key and value types
1404 stack->ga_len -= 2 * count;
1405
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406 // add the dict type to the type stack
1407 if (ga_grow(stack, 1) == FAIL)
1408 return FAIL;
1409 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1410 ++stack->ga_len;
1411
1412 return OK;
1413}
1414
1415/*
1416 * Generate an ISN_FUNCREF instruction.
1417 */
1418 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001419generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001420{
1421 isn_T *isn;
1422 garray_T *stack = &cctx->ctx_type_stack;
1423
Bram Moolenaar080457c2020-03-03 21:53:32 +01001424 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1426 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001427 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001428 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429
1430 if (ga_grow(stack, 1) == FAIL)
1431 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001432 ((type_T **)stack->ga_data)[stack->ga_len] =
1433 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434 ++stack->ga_len;
1435
1436 return OK;
1437}
1438
1439/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001440 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001441 * "lambda_name" and "func_name" must be in allocated memory and will be
1442 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001443 */
1444 static int
1445generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1446{
1447 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001448
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001449 if (cctx->ctx_skip == SKIP_YES)
1450 {
1451 vim_free(lambda_name);
1452 vim_free(func_name);
1453 return OK;
1454 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001455 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001456 {
1457 vim_free(lambda_name);
1458 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001459 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001460 }
1461 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001462 isn->isn_arg.newfunc.nf_global = func_name;
1463
1464 return OK;
1465}
1466
1467/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001468 * Generate an ISN_DEF instruction: list functions
1469 */
1470 static int
1471generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1472{
1473 isn_T *isn;
1474
1475 RETURN_OK_IF_SKIP(cctx);
1476 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1477 return FAIL;
1478 if (len > 0)
1479 {
1480 isn->isn_arg.string = vim_strnsave(name, len);
1481 if (isn->isn_arg.string == NULL)
1482 return FAIL;
1483 }
1484 return OK;
1485}
1486
1487/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 * Generate an ISN_JUMP instruction.
1489 */
1490 static int
1491generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1492{
1493 isn_T *isn;
1494 garray_T *stack = &cctx->ctx_type_stack;
1495
Bram Moolenaar080457c2020-03-03 21:53:32 +01001496 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001497 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1498 return FAIL;
1499 isn->isn_arg.jump.jump_when = when;
1500 isn->isn_arg.jump.jump_where = where;
1501
1502 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1503 --stack->ga_len;
1504
1505 return OK;
1506}
1507
1508 static int
1509generate_FOR(cctx_T *cctx, int loop_idx)
1510{
1511 isn_T *isn;
1512 garray_T *stack = &cctx->ctx_type_stack;
1513
Bram Moolenaar080457c2020-03-03 21:53:32 +01001514 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001515 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1516 return FAIL;
1517 isn->isn_arg.forloop.for_idx = loop_idx;
1518
1519 if (ga_grow(stack, 1) == FAIL)
1520 return FAIL;
1521 // type doesn't matter, will be stored next
1522 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1523 ++stack->ga_len;
1524
1525 return OK;
1526}
1527
1528/*
1529 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001530 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001531 * Return FAIL if the number of arguments is wrong.
1532 */
1533 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001534generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001535{
1536 isn_T *isn;
1537 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001538 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001539 type_T **argtypes = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001540
Bram Moolenaar080457c2020-03-03 21:53:32 +01001541 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001542 argoff = check_internal_func(func_idx, argcount);
1543 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 return FAIL;
1545
Bram Moolenaar389df252020-07-09 21:20:47 +02001546 if (method_call && argoff > 1)
1547 {
1548 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1549 return FAIL;
1550 isn->isn_arg.shuffle.shfl_item = argcount;
1551 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1552 }
1553
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001554 if (argcount > 0)
1555 {
1556 // Check the types of the arguments.
1557 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
1558 if (internal_func_check_arg_types(argtypes, func_idx, argcount) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001559 return FAIL;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001560 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001561
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001562 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1563 return FAIL;
1564 isn->isn_arg.bfunc.cbf_idx = func_idx;
1565 isn->isn_arg.bfunc.cbf_argcount = argcount;
1566
Bram Moolenaar94738d82020-10-21 14:25:07 +02001567 // Drop the argument types and push the return type.
1568 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569 if (ga_grow(stack, 1) == FAIL)
1570 return FAIL;
1571 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001572 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001573 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001574
1575 return OK;
1576}
1577
1578/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001579 * Generate an ISN_LISTAPPEND instruction. Works like add().
1580 * Argument count is already checked.
1581 */
1582 static int
1583generate_LISTAPPEND(cctx_T *cctx)
1584{
1585 garray_T *stack = &cctx->ctx_type_stack;
1586 type_T *list_type;
1587 type_T *item_type;
1588 type_T *expected;
1589
1590 // Caller already checked that list_type is a list.
1591 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1592 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1593 expected = list_type->tt_member;
1594 if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL)
1595 return FAIL;
1596
1597 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1598 return FAIL;
1599
1600 --stack->ga_len; // drop the argument
1601 return OK;
1602}
1603
1604/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001605 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1606 * Argument count is already checked.
1607 */
1608 static int
1609generate_BLOBAPPEND(cctx_T *cctx)
1610{
1611 garray_T *stack = &cctx->ctx_type_stack;
1612 type_T *item_type;
1613
1614 // Caller already checked that blob_type is a blob.
1615 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1616 if (need_type(item_type, &t_number, -1, cctx, FALSE, FALSE) == FAIL)
1617 return FAIL;
1618
1619 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1620 return FAIL;
1621
1622 --stack->ga_len; // drop the argument
1623 return OK;
1624}
1625
1626/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001627 * Generate an ISN_DCALL or ISN_UCALL instruction.
1628 * Return FAIL if the number of arguments is wrong.
1629 */
1630 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001631generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001632{
1633 isn_T *isn;
1634 garray_T *stack = &cctx->ctx_type_stack;
1635 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001636 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001637
Bram Moolenaar080457c2020-03-03 21:53:32 +01001638 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001639 if (argcount > regular_args && !has_varargs(ufunc))
1640 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001641 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001642 return FAIL;
1643 }
1644 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1645 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001646 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001647 return FAIL;
1648 }
1649
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001650 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001651 {
1652 int i;
1653
1654 for (i = 0; i < argcount; ++i)
1655 {
1656 type_T *expected;
1657 type_T *actual;
1658
1659 if (i < regular_args)
1660 {
1661 if (ufunc->uf_arg_types == NULL)
1662 continue;
1663 expected = ufunc->uf_arg_types[i];
1664 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001665 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1666 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001667 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001668 else
1669 expected = ufunc->uf_va_type->tt_member;
1670 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001671 if (need_type(actual, expected, -argcount + i, cctx,
1672 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001673 {
1674 arg_type_mismatch(expected, actual, i + 1);
1675 return FAIL;
1676 }
1677 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001678 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001679 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1680 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001681 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001682 }
1683
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001685 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001686 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001688 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689 {
1690 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1691 isn->isn_arg.dfunc.cdf_argcount = argcount;
1692 }
1693 else
1694 {
1695 // A user function may be deleted and redefined later, can't use the
1696 // ufunc pointer, need to look it up again at runtime.
1697 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1698 isn->isn_arg.ufunc.cuf_argcount = argcount;
1699 }
1700
1701 stack->ga_len -= argcount; // drop the arguments
1702 if (ga_grow(stack, 1) == FAIL)
1703 return FAIL;
1704 // add return value
1705 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1706 ++stack->ga_len;
1707
1708 return OK;
1709}
1710
1711/*
1712 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1713 */
1714 static int
1715generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1716{
1717 isn_T *isn;
1718 garray_T *stack = &cctx->ctx_type_stack;
1719
Bram Moolenaar080457c2020-03-03 21:53:32 +01001720 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001721 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1722 return FAIL;
1723 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1724 isn->isn_arg.ufunc.cuf_argcount = argcount;
1725
1726 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001727 if (ga_grow(stack, 1) == FAIL)
1728 return FAIL;
1729 // add return value
1730 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1731 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001732
1733 return OK;
1734}
1735
1736/*
1737 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001738 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001739 */
1740 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001741generate_PCALL(
1742 cctx_T *cctx,
1743 int argcount,
1744 char_u *name,
1745 type_T *type,
1746 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001747{
1748 isn_T *isn;
1749 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001750 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751
Bram Moolenaar080457c2020-03-03 21:53:32 +01001752 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001753
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001754 if (type->tt_type == VAR_ANY)
1755 ret_type = &t_any;
1756 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001757 {
1758 if (type->tt_argcount != -1)
1759 {
1760 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1761
1762 if (argcount < type->tt_min_argcount - varargs)
1763 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001764 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001765 return FAIL;
1766 }
1767 if (!varargs && argcount > type->tt_argcount)
1768 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001769 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001770 return FAIL;
1771 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001772 if (type->tt_args != NULL)
1773 {
1774 int i;
1775
1776 for (i = 0; i < argcount; ++i)
1777 {
1778 int offset = -argcount + i - 1;
1779 type_T *actual = ((type_T **)stack->ga_data)[
1780 stack->ga_len + offset];
1781 type_T *expected;
1782
1783 if (varargs && i >= type->tt_min_argcount - 1)
1784 expected = type->tt_args[
1785 type->tt_min_argcount - 1]->tt_member;
1786 else
1787 expected = type->tt_args[i];
1788 if (need_type(actual, expected, offset,
1789 cctx, TRUE, FALSE) == FAIL)
1790 {
1791 arg_type_mismatch(expected, actual, i + 1);
1792 return FAIL;
1793 }
1794 }
1795 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001796 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001797 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001798 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001799 else
1800 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001801 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001802 return FAIL;
1803 }
1804
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1806 return FAIL;
1807 isn->isn_arg.pfunc.cpf_top = at_top;
1808 isn->isn_arg.pfunc.cpf_argcount = argcount;
1809
1810 stack->ga_len -= argcount; // drop the arguments
1811
1812 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001813 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001814
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001815 // If partial is above the arguments it must be cleared and replaced with
1816 // the return value.
1817 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1818 return FAIL;
1819
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820 return OK;
1821}
1822
1823/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001824 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825 */
1826 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001827generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828{
1829 isn_T *isn;
1830 garray_T *stack = &cctx->ctx_type_stack;
1831 type_T *type;
1832
Bram Moolenaar080457c2020-03-03 21:53:32 +01001833 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001834 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001836 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001837
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001838 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001840 if (type->tt_type != VAR_DICT && type != &t_any)
1841 {
1842 emsg(_(e_dictreq));
1843 return FAIL;
1844 }
1845 // change dict type to dict member type
1846 if (type->tt_type == VAR_DICT)
1847 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001848
1849 return OK;
1850}
1851
1852/*
1853 * Generate an ISN_ECHO instruction.
1854 */
1855 static int
1856generate_ECHO(cctx_T *cctx, int with_white, int count)
1857{
1858 isn_T *isn;
1859
Bram Moolenaar080457c2020-03-03 21:53:32 +01001860 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001861 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1862 return FAIL;
1863 isn->isn_arg.echo.echo_with_white = with_white;
1864 isn->isn_arg.echo.echo_count = count;
1865
1866 return OK;
1867}
1868
Bram Moolenaarad39c092020-02-26 18:23:43 +01001869/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001870 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001871 */
1872 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001873generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001874{
1875 isn_T *isn;
1876
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001877 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001878 return FAIL;
1879 isn->isn_arg.number = count;
1880
1881 return OK;
1882}
1883
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001884/*
1885 * Generate an ISN_PUT instruction.
1886 */
1887 static int
1888generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1889{
1890 isn_T *isn;
1891
1892 RETURN_OK_IF_SKIP(cctx);
1893 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1894 return FAIL;
1895 isn->isn_arg.put.put_regname = regname;
1896 isn->isn_arg.put.put_lnum = lnum;
1897 return OK;
1898}
1899
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001900 static int
1901generate_EXEC(cctx_T *cctx, char_u *line)
1902{
1903 isn_T *isn;
1904
Bram Moolenaar080457c2020-03-03 21:53:32 +01001905 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1907 return FAIL;
1908 isn->isn_arg.string = vim_strsave(line);
1909 return OK;
1910}
1911
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001912 static int
1913generate_EXECCONCAT(cctx_T *cctx, int count)
1914{
1915 isn_T *isn;
1916
1917 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1918 return FAIL;
1919 isn->isn_arg.number = count;
1920 return OK;
1921}
1922
Bram Moolenaar08597872020-12-10 19:43:40 +01001923/*
1924 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1925 */
1926 static int
1927generate_RANGE(cctx_T *cctx, char_u *range)
1928{
1929 isn_T *isn;
1930 garray_T *stack = &cctx->ctx_type_stack;
1931
1932 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1933 return FAIL;
1934 isn->isn_arg.string = range;
1935
1936 if (ga_grow(stack, 1) == FAIL)
1937 return FAIL;
1938 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
1939 ++stack->ga_len;
1940 return OK;
1941}
1942
Bram Moolenaar792f7862020-11-23 08:31:18 +01001943 static int
1944generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
1945{
1946 isn_T *isn;
1947
1948 RETURN_OK_IF_SKIP(cctx);
1949 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
1950 return FAIL;
1951 isn->isn_arg.unpack.unp_count = var_count;
1952 isn->isn_arg.unpack.unp_semicolon = semicolon;
1953 return OK;
1954}
1955
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02001957 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001958 */
1959 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02001960generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001961{
1962 isn_T *isn;
1963
Bram Moolenaar02194d22020-10-24 23:08:38 +02001964 if (cmod->cmod_flags != 0
1965 || cmod->cmod_split != 0
1966 || cmod->cmod_verbose != 0
1967 || cmod->cmod_tab != 0
1968 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001969 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02001970 cctx->ctx_has_cmdmod = TRUE;
1971
1972 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001973 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001974 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1975 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1976 return FAIL;
1977 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001978 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02001979 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001980 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02001981
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001982 return OK;
1983}
1984
1985 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02001986generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001987{
Bram Moolenaarf665e972020-12-05 19:17:16 +01001988 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
1989 return FAIL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001990 return OK;
1991}
1992
1993/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001994 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001995 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001996 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001997 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02001998reserve_local(
1999 cctx_T *cctx,
2000 char_u *name,
2001 size_t len,
2002 int isConst,
2003 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002004{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002005 lvar_T *lvar;
2006
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002007 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002009 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002010 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011 }
2012
2013 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002014 return NULL;
2015 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002016 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002018 // Every local variable uses the next entry on the stack. We could re-use
2019 // the last ones when leaving a scope, but then variables used in a closure
2020 // might get overwritten. To keep things simple do not re-use stack
2021 // entries. This is less efficient, but memory is cheap these days.
2022 lvar->lv_idx = cctx->ctx_locals_count++;
2023
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002024 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 lvar->lv_const = isConst;
2026 lvar->lv_type = type;
2027
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002028 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029}
2030
2031/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002032 * Remove local variables above "new_top".
2033 */
2034 static void
2035unwind_locals(cctx_T *cctx, int new_top)
2036{
2037 if (cctx->ctx_locals.ga_len > new_top)
2038 {
2039 int idx;
2040 lvar_T *lvar;
2041
2042 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2043 {
2044 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2045 vim_free(lvar->lv_name);
2046 }
2047 }
2048 cctx->ctx_locals.ga_len = new_top;
2049}
2050
2051/*
2052 * Free all local variables.
2053 */
2054 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002055free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002056{
2057 unwind_locals(cctx, 0);
2058 ga_clear(&cctx->ctx_locals);
2059}
2060
2061/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002062 * Find "name" in script-local items of script "sid".
2063 * Returns the index in "sn_var_vals" if found.
2064 * If found but not in "sn_var_vals" returns -1.
2065 * If not found returns -2.
2066 */
2067 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002068get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002069{
2070 hashtab_T *ht;
2071 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002072 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002073 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002074 int idx;
2075
Bram Moolenaare3d46852020-08-29 13:39:17 +02002076 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002077 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002078 if (sid == current_sctx.sc_sid)
2079 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002080 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002081
2082 if (sav == NULL)
2083 return -2;
2084 idx = sav->sav_var_vals_idx;
2085 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2086 if (check_writable && sv->sv_const)
2087 semsg(_(e_readonlyvar), name);
2088 return idx;
2089 }
2090
2091 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002092 ht = &SCRIPT_VARS(sid);
2093 di = find_var_in_ht(ht, 0, name, TRUE);
2094 if (di == NULL)
2095 return -2;
2096
2097 // Now find the svar_T index in sn_var_vals.
2098 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2099 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002100 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002101 if (sv->sv_tv == &di->di_tv)
2102 {
2103 if (check_writable && sv->sv_const)
2104 semsg(_(e_readonlyvar), name);
2105 return idx;
2106 }
2107 }
2108 return -1;
2109}
2110
2111/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002112 * Find "name" in imported items of the current script or in "cctx" if not
2113 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002114 */
2115 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002116find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002117{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002118 int idx;
2119
Bram Moolenaare3d46852020-08-29 13:39:17 +02002120 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002121 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122 if (cctx != NULL)
2123 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2124 {
2125 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2126 + idx;
2127
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002128 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2129 : STRLEN(import->imp_name) == len
2130 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131 return import;
2132 }
2133
Bram Moolenaarefa94442020-08-08 22:16:00 +02002134 return find_imported_in_script(name, len, current_sctx.sc_sid);
2135}
2136
2137 imported_T *
2138find_imported_in_script(char_u *name, size_t len, int sid)
2139{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002140 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002141 int idx;
2142
Bram Moolenaare3d46852020-08-29 13:39:17 +02002143 if (!SCRIPT_ID_VALID(sid))
2144 return NULL;
2145 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002146 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2147 {
2148 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2149
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002150 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2151 : STRLEN(import->imp_name) == len
2152 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002153 return import;
2154 }
2155 return NULL;
2156}
2157
2158/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002159 * Free all imported variables.
2160 */
2161 static void
2162free_imported(cctx_T *cctx)
2163{
2164 int idx;
2165
2166 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2167 {
2168 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2169
2170 vim_free(import->imp_name);
2171 }
2172 ga_clear(&cctx->ctx_imports);
2173}
2174
2175/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01002176 * Return TRUE if "p" points at a "#". Does not check for white space.
Bram Moolenaar23c55272020-06-21 16:58:13 +02002177 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002178 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002179vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002180{
Bram Moolenaare0de1712020-12-02 17:36:54 +01002181 return p[0] == '#';
Bram Moolenaar23c55272020-06-21 16:58:13 +02002182}
2183
2184/*
2185 * Return a pointer to the next line that isn't empty or only contains a
2186 * comment. Skips over white space.
2187 * Returns NULL if there is none.
2188 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002189 char_u *
2190peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002191{
2192 int lnum = cctx->ctx_lnum;
2193
2194 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2195 {
2196 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002197 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002198
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002199 // ignore NULLs inserted for continuation lines
2200 if (line != NULL)
2201 {
2202 p = skipwhite(line);
2203 if (*p != NUL && !vim9_comment_start(p))
2204 return p;
2205 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002206 }
2207 return NULL;
2208}
2209
2210/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002211 * Called when checking for a following operator at "arg". When the rest of
2212 * the line is empty or only a comment, peek the next line. If there is a next
2213 * line return a pointer to it and set "nextp".
2214 * Otherwise skip over white space.
2215 */
2216 static char_u *
2217may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2218{
2219 char_u *p = skipwhite(arg);
2220
2221 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002222 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002223 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002224 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002225 if (*nextp != NULL)
2226 return *nextp;
2227 }
2228 return p;
2229}
2230
2231/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002232 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002233 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002234 * Returns NULL when at the end.
2235 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002236 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002237next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002238{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002239 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002240
2241 do
2242 {
2243 ++cctx->ctx_lnum;
2244 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002245 {
2246 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002247 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002248 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002249 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002250 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002251 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002252 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002253 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002254 return line;
2255}
2256
2257/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002258 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002259 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002260 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2261 */
2262 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002263may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002264{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002265 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002266 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002267 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002268
2269 if (next == NULL)
2270 return FAIL;
2271 *arg = skipwhite(next);
2272 }
2273 return OK;
2274}
2275
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002276/*
2277 * Idem, and give an error when failed.
2278 */
2279 static int
2280may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2281{
2282 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2283 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002284 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002285 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002286 return FAIL;
2287 }
2288 return OK;
2289}
2290
2291
Bram Moolenaara5565e42020-05-09 15:44:01 +02002292// Structure passed between the compile_expr* functions to keep track of
2293// constants that have been parsed but for which no code was produced yet. If
2294// possible expressions on these constants are applied at compile time. If
2295// that is not possible, the code to push the constants needs to be generated
2296// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002297// Using 50 should be more than enough of 5 levels of ().
2298#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002299typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002300 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002301 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002302 int pp_is_const; // all generated code was constants, used for a
2303 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002304} ppconst_T;
2305
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002306static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002307static int compile_expr0(char_u **arg, cctx_T *cctx);
2308static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2309
Bram Moolenaara5565e42020-05-09 15:44:01 +02002310/*
2311 * Generate a PUSH instruction for "tv".
2312 * "tv" will be consumed or cleared.
2313 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2314 */
2315 static int
2316generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2317{
2318 if (tv != NULL)
2319 {
2320 switch (tv->v_type)
2321 {
2322 case VAR_UNKNOWN:
2323 break;
2324 case VAR_BOOL:
2325 generate_PUSHBOOL(cctx, tv->vval.v_number);
2326 break;
2327 case VAR_SPECIAL:
2328 generate_PUSHSPEC(cctx, tv->vval.v_number);
2329 break;
2330 case VAR_NUMBER:
2331 generate_PUSHNR(cctx, tv->vval.v_number);
2332 break;
2333#ifdef FEAT_FLOAT
2334 case VAR_FLOAT:
2335 generate_PUSHF(cctx, tv->vval.v_float);
2336 break;
2337#endif
2338 case VAR_BLOB:
2339 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2340 tv->vval.v_blob = NULL;
2341 break;
2342 case VAR_STRING:
2343 generate_PUSHS(cctx, tv->vval.v_string);
2344 tv->vval.v_string = NULL;
2345 break;
2346 default:
2347 iemsg("constant type not supported");
2348 clear_tv(tv);
2349 return FAIL;
2350 }
2351 tv->v_type = VAR_UNKNOWN;
2352 }
2353 return OK;
2354}
2355
2356/*
2357 * Generate code for any ppconst entries.
2358 */
2359 static int
2360generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2361{
2362 int i;
2363 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002364 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002365
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002366 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002367 for (i = 0; i < ppconst->pp_used; ++i)
2368 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2369 ret = FAIL;
2370 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002371 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002372 return ret;
2373}
2374
2375/*
2376 * Clear ppconst constants. Used when failing.
2377 */
2378 static void
2379clear_ppconst(ppconst_T *ppconst)
2380{
2381 int i;
2382
2383 for (i = 0; i < ppconst->pp_used; ++i)
2384 clear_tv(&ppconst->pp_tv[i]);
2385 ppconst->pp_used = 0;
2386}
2387
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002388/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002389 * Generate an instruction to load script-local variable "name", without the
2390 * leading "s:".
2391 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002392 */
2393 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002394compile_load_scriptvar(
2395 cctx_T *cctx,
2396 char_u *name, // variable NUL terminated
2397 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002398 char_u **end, // end of variable
2399 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002400{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002401 scriptitem_T *si;
2402 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403 imported_T *import;
2404
Bram Moolenaare3d46852020-08-29 13:39:17 +02002405 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2406 return FAIL;
2407 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002408 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002409 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002410 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002411 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002412 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2413 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002414 }
2415 if (idx >= 0)
2416 {
2417 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2418
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002419 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002420 current_sctx.sc_sid, idx, sv->sv_type);
2421 return OK;
2422 }
2423
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002424 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002425 if (import != NULL)
2426 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002427 if (import->imp_all)
2428 {
2429 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002430 char_u *exp_name;
2431 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002432 ufunc_T *ufunc;
2433 type_T *type;
2434
2435 // Used "import * as Name", need to lookup the member.
2436 if (*p != '.')
2437 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002438 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002439 return FAIL;
2440 }
2441 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002442 if (VIM_ISWHITE(*p))
2443 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002444 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002445 return FAIL;
2446 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002447
Bram Moolenaar1c991142020-07-04 13:15:31 +02002448 // isolate one name
2449 exp_name = p;
2450 while (eval_isnamec(*p))
2451 ++p;
2452 cc = *p;
2453 *p = NUL;
2454
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002455 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002456 *p = cc;
2457 p = skipwhite(p);
2458
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002459 // TODO: what if it is a function?
2460 if (idx < 0)
2461 return FAIL;
2462 *end = p;
2463
2464 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2465 import->imp_sid,
2466 idx,
2467 type);
2468 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002469 else if (import->imp_funcname != NULL)
2470 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002471 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002472 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2473 import->imp_sid,
2474 import->imp_var_vals_idx,
2475 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002476 return OK;
2477 }
2478
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002479 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002480 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481 return FAIL;
2482}
2483
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002484 static int
2485generate_funcref(cctx_T *cctx, char_u *name)
2486{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002487 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002488
2489 if (ufunc == NULL)
2490 return FAIL;
2491
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002492 // Need to compile any default values to get the argument types.
2493 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2494 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2495 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002496 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002497}
2498
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002499/*
2500 * Compile a variable name into a load instruction.
2501 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002502 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 * When "error" is FALSE do not give an error when not found.
2504 */
2505 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002506compile_load(
2507 char_u **arg,
2508 char_u *end_arg,
2509 cctx_T *cctx,
2510 int is_expr,
2511 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512{
2513 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002514 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002515 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002517 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518
2519 if (*(*arg + 1) == ':')
2520 {
2521 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002522 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002523 {
2524 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002526 switch (**arg)
2527 {
2528 case 'g': isn_type = ISN_LOADGDICT; break;
2529 case 'w': isn_type = ISN_LOADWDICT; break;
2530 case 't': isn_type = ISN_LOADTDICT; break;
2531 case 'b': isn_type = ISN_LOADBDICT; break;
2532 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002533 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002534 goto theend;
2535 }
2536 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2537 goto theend;
2538 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002539 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 else
2541 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002542 isntype_T isn_type = ISN_DROP;
2543
2544 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2545 if (name == NULL)
2546 return FAIL;
2547
2548 switch (**arg)
2549 {
2550 case 'v': res = generate_LOADV(cctx, name, error);
2551 break;
2552 case 's': res = compile_load_scriptvar(cctx, name,
2553 NULL, NULL, error);
2554 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002555 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2556 isn_type = ISN_LOADG;
2557 else
2558 {
2559 isn_type = ISN_LOADAUTO;
2560 vim_free(name);
2561 name = vim_strnsave(*arg, end - *arg);
2562 if (name == NULL)
2563 return FAIL;
2564 }
2565 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002566 case 'w': isn_type = ISN_LOADW; break;
2567 case 't': isn_type = ISN_LOADT; break;
2568 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002569 default: // cannot happen, just in case
2570 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002571 goto theend;
2572 }
2573 if (isn_type != ISN_DROP)
2574 {
2575 // Global, Buffer-local, Window-local and Tabpage-local
2576 // variables can be defined later, thus we don't check if it
2577 // exists, give error at runtime.
2578 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2579 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002580 }
2581 }
2582 else
2583 {
2584 size_t len = end - *arg;
2585 int idx;
2586 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002587 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588
2589 name = vim_strnsave(*arg, end - *arg);
2590 if (name == NULL)
2591 return FAIL;
2592
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002593 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002594 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002595 if (!gen_load_outer)
2596 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002597 }
2598 else
2599 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002600 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002601
Bram Moolenaar709664c2020-12-12 14:33:41 +01002602 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002604 type = lvar.lv_type;
2605 idx = lvar.lv_idx;
2606 if (lvar.lv_from_outer)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002607 gen_load_outer = TRUE;
2608 else
2609 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002610 }
2611 else
2612 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002613 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002614 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002615 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002616 || find_imported(name, 0, cctx) != NULL)
2617 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002618
Bram Moolenaar0f769812020-09-12 18:32:34 +02002619 // When evaluating an expression and the name starts with an
2620 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002621 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002622 if (res == FAIL && is_expr
2623 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002624 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002625 }
2626 }
2627 if (gen_load)
2628 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002629 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002630 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002631 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002632 cctx->ctx_outer_used = TRUE;
2633 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 }
2635
2636 *arg = end;
2637
2638theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002639 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002640 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002641 vim_free(name);
2642 return res;
2643}
2644
2645/*
2646 * Compile the argument expressions.
2647 * "arg" points to just after the "(" and is advanced to after the ")"
2648 */
2649 static int
2650compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2651{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002652 char_u *p = *arg;
2653 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002654 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655
Bram Moolenaare6085c52020-04-12 20:19:16 +02002656 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002658 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2659 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002660 if (*p == ')')
2661 {
2662 *arg = p + 1;
2663 return OK;
2664 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002665 if (must_end)
2666 {
2667 semsg(_(e_missing_comma_before_argument_str), p);
2668 return FAIL;
2669 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002670
Bram Moolenaara5565e42020-05-09 15:44:01 +02002671 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672 return FAIL;
2673 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002674
2675 if (*p != ',' && *skipwhite(p) == ',')
2676 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002677 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002678 p = skipwhite(p);
2679 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002680 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002681 {
2682 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002683 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002684 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002685 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002686 else
2687 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002688 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002689 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002690 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002691failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002692 emsg(_(e_missing_close));
2693 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002694}
2695
2696/*
2697 * Compile a function call: name(arg1, arg2)
2698 * "arg" points to "name", "arg + varlen" to the "(".
2699 * "argcount_init" is 1 for "value->method()"
2700 * Instructions:
2701 * EVAL arg1
2702 * EVAL arg2
2703 * BCALL / DCALL / UCALL
2704 */
2705 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002706compile_call(
2707 char_u **arg,
2708 size_t varlen,
2709 cctx_T *cctx,
2710 ppconst_T *ppconst,
2711 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002712{
2713 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002714 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002715 int argcount = argcount_init;
2716 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002717 char_u fname_buf[FLEN_FIXED + 1];
2718 char_u *tofree = NULL;
2719 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002720 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002721 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002722 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002723
Bram Moolenaara5565e42020-05-09 15:44:01 +02002724 // we can evaluate "has('name')" at compile time
2725 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2726 {
2727 char_u *s = skipwhite(*arg + varlen + 1);
2728 typval_T argvars[2];
2729
2730 argvars[0].v_type = VAR_UNKNOWN;
2731 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002732 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002733 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002734 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002735 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002736 if (*s == ')' && argvars[0].v_type == VAR_STRING
2737 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002738 {
2739 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2740
2741 *arg = s + 1;
2742 argvars[1].v_type = VAR_UNKNOWN;
2743 tv->v_type = VAR_NUMBER;
2744 tv->vval.v_number = 0;
2745 f_has(argvars, tv);
2746 clear_tv(&argvars[0]);
2747 ++ppconst->pp_used;
2748 return OK;
2749 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002750 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002751 }
2752
2753 if (generate_ppconst(cctx, ppconst) == FAIL)
2754 return FAIL;
2755
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756 if (varlen >= sizeof(namebuf))
2757 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002758 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002759 return FAIL;
2760 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002761 vim_strncpy(namebuf, *arg, varlen);
2762 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002763
2764 *arg = skipwhite(*arg + varlen + 1);
2765 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002766 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002767
Bram Moolenaar03290b82020-12-19 16:30:44 +01002768 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002769 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002770 {
2771 int idx;
2772
2773 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002774 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002776 {
2777 if (STRCMP(name, "add") == 0 && argcount == 2)
2778 {
2779 garray_T *stack = &cctx->ctx_type_stack;
2780 type_T *type = ((type_T **)stack->ga_data)[
2781 stack->ga_len - 2];
2782
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002783 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002784 if (type->tt_type == VAR_LIST)
2785 {
2786 // inline "add(list, item)" so that the type can be checked
2787 res = generate_LISTAPPEND(cctx);
2788 idx = -1;
2789 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002790 else if (type->tt_type == VAR_BLOB)
2791 {
2792 // inline "add(blob, nr)" so that the type can be checked
2793 res = generate_BLOBAPPEND(cctx);
2794 idx = -1;
2795 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002796 }
2797
2798 if (idx >= 0)
2799 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2800 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002801 else
2802 semsg(_(e_unknownfunc), namebuf);
2803 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 }
2805
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002806 // An argument or local variable can be a function reference, this
2807 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002808 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002809 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002810 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002811 // If we can find the function by name generate the right call.
2812 // Skip global functions here, a local funcref takes precedence.
2813 ufunc = find_func(name, FALSE, cctx);
2814 if (ufunc != NULL && !func_is_global(ufunc))
2815 {
2816 res = generate_CALL(cctx, ufunc, argcount);
2817 goto theend;
2818 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002819 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820
2821 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002822 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002823 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002825 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002826 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002827 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002828 garray_T *stack = &cctx->ctx_type_stack;
2829 type_T *type;
2830
2831 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2832 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002833 goto theend;
2834 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835
Bram Moolenaar0f769812020-09-12 18:32:34 +02002836 // If we can find a global function by name generate the right call.
2837 if (ufunc != NULL)
2838 {
2839 res = generate_CALL(cctx, ufunc, argcount);
2840 goto theend;
2841 }
2842
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002843 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002844 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002845 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002846 res = generate_UCALL(cctx, name, argcount);
2847 else
2848 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002849
2850theend:
2851 vim_free(tofree);
2852 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853}
2854
2855// like NAMESPACE_CHAR but with 'a' and 'l'.
2856#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2857
2858/*
2859 * Find the end of a variable or function name. Unlike find_name_end() this
2860 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002861 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 * Return a pointer to just after the name. Equal to "arg" if there is no
2863 * valid name.
2864 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01002865 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002866to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867{
2868 char_u *p;
2869
2870 // Quick check for valid starting character.
2871 if (!eval_isnamec1(*arg))
2872 return arg;
2873
2874 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2875 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2876 // and can be used in slice "[n:]".
2877 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002878 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002879 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2880 break;
2881 return p;
2882}
2883
2884/*
2885 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002886 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887 */
2888 char_u *
2889to_name_const_end(char_u *arg)
2890{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002891 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 typval_T rettv;
2893
2894 if (p == arg && *arg == '[')
2895 {
2896
2897 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002898 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899 p = arg;
2900 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901 return p;
2902}
2903
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904/*
2905 * parse a list: [expr, expr]
2906 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002907 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002908 */
2909 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002910compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911{
2912 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002913 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002915 int is_const;
2916 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002918 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002920 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002921 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002922 semsg(_(e_list_end), *arg);
2923 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002924 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002925 if (*p == ',')
2926 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002927 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002928 return FAIL;
2929 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002930 if (*p == ']')
2931 {
2932 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002933 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002934 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002935 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002936 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002937 if (!is_const)
2938 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939 ++count;
2940 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002941 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002943 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2944 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002945 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002946 return FAIL;
2947 }
2948 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002949 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002950 p = skipwhite(p);
2951 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002952 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002954 ppconst->pp_is_const = is_all_const;
2955 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956}
2957
2958/*
2959 * parse a lambda: {arg, arg -> expr}
2960 * "*arg" points to the '{'.
2961 */
2962 static int
2963compile_lambda(char_u **arg, cctx_T *cctx)
2964{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 typval_T rettv;
2966 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002967 evalarg_T evalarg;
2968
2969 CLEAR_FIELD(evalarg);
2970 evalarg.eval_flags = EVAL_EVALUATE;
2971 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972
2973 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002974 if (get_lambda_tv(arg, &rettv, TRUE, &evalarg) != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002975 {
2976 clear_evalarg(&evalarg, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 return FAIL;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002978 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01002979
Bram Moolenaar65c44152020-12-24 15:14:01 +01002980 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002982 ++ufunc->uf_refcount;
2983 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984
Bram Moolenaar65c44152020-12-24 15:14:01 +01002985 // Compile the function into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002986 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002988 clear_evalarg(&evalarg, NULL);
2989
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002990 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002991 {
2992 // The return type will now be known.
2993 set_function_type(ufunc);
2994
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002995 // The function reference count will be 1. When the ISN_FUNCREF
2996 // instruction is deleted the reference count is decremented and the
2997 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002998 return generate_FUNCREF(cctx, ufunc);
2999 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003000
3001 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002 return FAIL;
3003}
3004
3005/*
3006 * Compile a lamda call: expr->{lambda}(args)
3007 * "arg" points to the "{".
3008 */
3009 static int
3010compile_lambda_call(char_u **arg, cctx_T *cctx)
3011{
3012 ufunc_T *ufunc;
3013 typval_T rettv;
3014 int argcount = 1;
3015 int ret = FAIL;
3016
3017 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003018 if (get_lambda_tv(arg, &rettv, TRUE, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 return FAIL;
3020
3021 if (**arg != '(')
3022 {
3023 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003024 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 else
3026 semsg(_(e_missing_paren), "lambda");
3027 clear_tv(&rettv);
3028 return FAIL;
3029 }
3030
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 ufunc = rettv.vval.v_partial->pt_func;
3032 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003033 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003034 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003035
Bram Moolenaara05e5242020-09-19 18:19:19 +02003036 // The function will have one line: "return {expr}". Compile it into
3037 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003038 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039
3040 // compile the arguments
3041 *arg = skipwhite(*arg + 1);
3042 if (compile_arguments(arg, cctx, &argcount) == OK)
3043 // call the compiled function
3044 ret = generate_CALL(cctx, ufunc, argcount);
3045
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003046 if (ret == FAIL)
3047 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 return ret;
3049}
3050
3051/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003052 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003054 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 */
3056 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003057compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058{
3059 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003060 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061 int count = 0;
3062 dict_T *d = dict_alloc();
3063 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003064 char_u *whitep = *arg;
3065 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003066 int is_const;
3067 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068
3069 if (d == NULL)
3070 return FAIL;
3071 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003072 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003073 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003074 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075
Bram Moolenaar23c55272020-06-21 16:58:13 +02003076 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003077 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003078 *arg = NULL;
3079 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003080 }
3081
3082 if (**arg == '}')
3083 break;
3084
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003085 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003087 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003089 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003090 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003091 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3094 if (isn->isn_type == ISN_PUSHS)
3095 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003096 else
3097 {
3098 type_T *keytype = ((type_T **)stack->ga_data)
Bram Moolenaar2bede172020-11-19 18:53:18 +01003099 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003100 if (need_type(keytype, &t_string, -1, cctx,
Bram Moolenaar2bede172020-11-19 18:53:18 +01003101 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003102 return FAIL;
3103 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003104 *arg = skipwhite(*arg);
3105 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003106 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003107 emsg(_(e_missing_matching_bracket_after_dict_key));
3108 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003109 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003110 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003112 else
3113 {
3114 // {"name": value},
3115 // {'name': value},
3116 // {name: value} use "name" as a literal key
3117 key = get_literal_key(arg);
3118 if (key == NULL)
3119 return FAIL;
3120 if (generate_PUSHS(cctx, key) == FAIL)
3121 return FAIL;
3122 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123
3124 // Check for duplicate keys, if using string keys.
3125 if (key != NULL)
3126 {
3127 item = dict_find(d, key, -1);
3128 if (item != NULL)
3129 {
3130 semsg(_(e_duplicate_key), key);
3131 goto failret;
3132 }
3133 item = dictitem_alloc(key);
3134 if (item != NULL)
3135 {
3136 item->di_tv.v_type = VAR_UNKNOWN;
3137 item->di_tv.v_lock = 0;
3138 if (dict_add(d, item) == FAIL)
3139 dictitem_free(item);
3140 }
3141 }
3142
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 if (**arg != ':')
3144 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003145 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003146 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003147 else
3148 semsg(_(e_missing_dict_colon), *arg);
3149 return FAIL;
3150 }
3151 whitep = *arg + 1;
3152 if (!IS_WHITE_OR_NUL(*whitep))
3153 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003154 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155 return FAIL;
3156 }
3157
3158 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003159 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003160 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003161 *arg = NULL;
3162 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003163 }
3164
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003165 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003167 if (!is_const)
3168 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 ++count;
3170
Bram Moolenaar2c330432020-04-13 14:41:35 +02003171 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003172 *arg = skipwhite(*arg);
3173 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003174 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003175 *arg = NULL;
3176 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003177 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 if (**arg == '}')
3179 break;
3180 if (**arg != ',')
3181 {
3182 semsg(_(e_missing_dict_comma), *arg);
3183 goto failret;
3184 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003185 if (IS_WHITE_OR_NUL(*whitep))
3186 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003187 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003188 return FAIL;
3189 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003190 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003191 if (!IS_WHITE_OR_NUL(*whitep))
3192 {
3193 semsg(_(e_white_space_required_after_str), ",");
3194 return FAIL;
3195 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 *arg = skipwhite(*arg + 1);
3197 }
3198
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 *arg = *arg + 1;
3200
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003201 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003202 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003203 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003204 *arg += STRLEN(*arg);
3205
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003207 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 return generate_NEWDICT(cctx, count);
3209
3210failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003211 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003212 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003213 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003214 *arg = (char_u *)"";
3215 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216 dict_unref(d);
3217 return FAIL;
3218}
3219
3220/*
3221 * Compile "&option".
3222 */
3223 static int
3224compile_get_option(char_u **arg, cctx_T *cctx)
3225{
3226 typval_T rettv;
3227 char_u *start = *arg;
3228 int ret;
3229
3230 // parse the option and get the current value to get the type.
3231 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003232 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233 if (ret == OK)
3234 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003235 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 char_u *name = vim_strnsave(start, *arg - start);
3237 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3238
3239 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3240 vim_free(name);
3241 }
3242 clear_tv(&rettv);
3243
3244 return ret;
3245}
3246
3247/*
3248 * Compile "$VAR".
3249 */
3250 static int
3251compile_get_env(char_u **arg, cctx_T *cctx)
3252{
3253 char_u *start = *arg;
3254 int len;
3255 int ret;
3256 char_u *name;
3257
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 ++*arg;
3259 len = get_env_len(arg);
3260 if (len == 0)
3261 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003262 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263 return FAIL;
3264 }
3265
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003266 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 name = vim_strnsave(start, len + 1);
3268 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3269 vim_free(name);
3270 return ret;
3271}
3272
3273/*
3274 * Compile "@r".
3275 */
3276 static int
3277compile_get_register(char_u **arg, cctx_T *cctx)
3278{
3279 int ret;
3280
3281 ++*arg;
3282 if (**arg == NUL)
3283 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003284 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 return FAIL;
3286 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003287 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003288 {
3289 emsg_invreg(**arg);
3290 return FAIL;
3291 }
3292 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3293 ++*arg;
3294 return ret;
3295}
3296
3297/*
3298 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003299 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 */
3301 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003302apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003304 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305
3306 // this works from end to start
3307 while (p > start)
3308 {
3309 --p;
3310 if (*p == '-' || *p == '+')
3311 {
3312 // only '-' has an effect, for '+' we only check the type
3313#ifdef FEAT_FLOAT
3314 if (rettv->v_type == VAR_FLOAT)
3315 {
3316 if (*p == '-')
3317 rettv->vval.v_float = -rettv->vval.v_float;
3318 }
3319 else
3320#endif
3321 {
3322 varnumber_T val;
3323 int error = FALSE;
3324
3325 // tv_get_number_chk() accepts a string, but we don't want that
3326 // here
3327 if (check_not_string(rettv) == FAIL)
3328 return FAIL;
3329 val = tv_get_number_chk(rettv, &error);
3330 clear_tv(rettv);
3331 if (error)
3332 return FAIL;
3333 if (*p == '-')
3334 val = -val;
3335 rettv->v_type = VAR_NUMBER;
3336 rettv->vval.v_number = val;
3337 }
3338 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003339 else if (numeric_only)
3340 {
3341 ++p;
3342 break;
3343 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003344 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003345 {
3346 int v = tv2bool(rettv);
3347
3348 // '!' is permissive in the type.
3349 clear_tv(rettv);
3350 rettv->v_type = VAR_BOOL;
3351 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3352 }
3353 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003354 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 return OK;
3356}
3357
3358/*
3359 * Recognize v: variables that are constants and set "rettv".
3360 */
3361 static void
3362get_vim_constant(char_u **arg, typval_T *rettv)
3363{
3364 if (STRNCMP(*arg, "v:true", 6) == 0)
3365 {
3366 rettv->v_type = VAR_BOOL;
3367 rettv->vval.v_number = VVAL_TRUE;
3368 *arg += 6;
3369 }
3370 else if (STRNCMP(*arg, "v:false", 7) == 0)
3371 {
3372 rettv->v_type = VAR_BOOL;
3373 rettv->vval.v_number = VVAL_FALSE;
3374 *arg += 7;
3375 }
3376 else if (STRNCMP(*arg, "v:null", 6) == 0)
3377 {
3378 rettv->v_type = VAR_SPECIAL;
3379 rettv->vval.v_number = VVAL_NULL;
3380 *arg += 6;
3381 }
3382 else if (STRNCMP(*arg, "v:none", 6) == 0)
3383 {
3384 rettv->v_type = VAR_SPECIAL;
3385 rettv->vval.v_number = VVAL_NONE;
3386 *arg += 6;
3387 }
3388}
3389
Bram Moolenaar696ba232020-07-29 21:20:41 +02003390 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003391get_compare_type(char_u *p, int *len, int *type_is)
3392{
3393 exptype_T type = EXPR_UNKNOWN;
3394 int i;
3395
3396 switch (p[0])
3397 {
3398 case '=': if (p[1] == '=')
3399 type = EXPR_EQUAL;
3400 else if (p[1] == '~')
3401 type = EXPR_MATCH;
3402 break;
3403 case '!': if (p[1] == '=')
3404 type = EXPR_NEQUAL;
3405 else if (p[1] == '~')
3406 type = EXPR_NOMATCH;
3407 break;
3408 case '>': if (p[1] != '=')
3409 {
3410 type = EXPR_GREATER;
3411 *len = 1;
3412 }
3413 else
3414 type = EXPR_GEQUAL;
3415 break;
3416 case '<': if (p[1] != '=')
3417 {
3418 type = EXPR_SMALLER;
3419 *len = 1;
3420 }
3421 else
3422 type = EXPR_SEQUAL;
3423 break;
3424 case 'i': if (p[1] == 's')
3425 {
3426 // "is" and "isnot"; but not a prefix of a name
3427 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3428 *len = 5;
3429 i = p[*len];
3430 if (!isalnum(i) && i != '_')
3431 {
3432 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3433 *type_is = TRUE;
3434 }
3435 }
3436 break;
3437 }
3438 return type;
3439}
3440
3441/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003443 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003444 */
3445 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003446compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003448 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449
3450 // this works from end to start
3451 while (p > start)
3452 {
3453 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003454 while (VIM_ISWHITE(*p))
3455 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 if (*p == '-' || *p == '+')
3457 {
3458 int negate = *p == '-';
3459 isn_T *isn;
3460
3461 // TODO: check type
3462 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3463 {
3464 --p;
3465 if (*p == '-')
3466 negate = !negate;
3467 }
3468 // only '-' has an effect, for '+' we only check the type
3469 if (negate)
3470 isn = generate_instr(cctx, ISN_NEGATENR);
3471 else
3472 isn = generate_instr(cctx, ISN_CHECKNR);
3473 if (isn == NULL)
3474 return FAIL;
3475 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003476 else if (numeric_only)
3477 {
3478 ++p;
3479 break;
3480 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481 else
3482 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003483 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003485 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003486 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003487 if (p[-1] == '!')
3488 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003490 }
3491 if (generate_2BOOL(cctx, invert) == FAIL)
3492 return FAIL;
3493 }
3494 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003495 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496 return OK;
3497}
3498
3499/*
3500 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003501 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502 */
3503 static int
3504compile_subscript(
3505 char_u **arg,
3506 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003507 char_u *start_leader,
3508 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003509 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003511 char_u *name_start = *end_leader;
3512
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 for (;;)
3514 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003515 char_u *p = skipwhite(*arg);
3516
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003517 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003518 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003519 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003520
3521 // If a following line starts with "->{" or "->X" advance to that
3522 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003523 // Also if a following line starts with ".x".
3524 if (next != NULL &&
3525 ((next[0] == '-' && next[1] == '>'
3526 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003527 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003528 {
3529 next = next_line_from_context(cctx, TRUE);
3530 if (next == NULL)
3531 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003532 *arg = next;
3533 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003534 }
3535 }
3536
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003537 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003538 // not a function call.
3539 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003541 garray_T *stack = &cctx->ctx_type_stack;
3542 type_T *type;
3543 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003544
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003545 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003546 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003547 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003548
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003550 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3551
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003552 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3554 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003555 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556 return FAIL;
3557 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003558 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003560 char_u *pstart = p;
3561
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003562 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003563 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003564 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003565
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 // something->method()
3567 // Apply the '!', '-' and '+' first:
3568 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003569 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003572 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003573 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003574 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 if (**arg == '{')
3576 {
3577 // lambda call: list->{lambda}
Bram Moolenaar65c44152020-12-24 15:14:01 +01003578 // TODO: remove this
3579 if (compile_lambda_call(arg, cctx) == FAIL)
3580 return FAIL;
3581 }
3582 else if (**arg == '(')
3583 {
3584 // Funcref call: list->(Refs[2])()
3585 // or lambda: list->((arg) => expr)()
3586 // TODO: make this work
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003587 if (compile_lambda_call(arg, cctx) == FAIL)
3588 return FAIL;
3589 }
3590 else
3591 {
3592 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003593 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003594 if (!eval_isnamec1(*p))
3595 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003596 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003597 return FAIL;
3598 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003599 if (ASCII_ISALPHA(*p) && p[1] == ':')
3600 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003601 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003602 ;
3603 if (*p != '(')
3604 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003605 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 return FAIL;
3607 }
3608 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003609 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 return FAIL;
3611 }
3612 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003613 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003615 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003616 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003617 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003618 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003619 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003620
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003621 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003622 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003623 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003624 // TODO: blob index
3625 // TODO: more arguments
3626 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003627 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003628 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003629 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003630
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003631 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003632 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003633 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003634 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003635 if (**arg == ':')
3636 // missing first index is equal to zero
3637 generate_PUSHNR(cctx, 0);
3638 else
3639 {
3640 if (compile_expr0(arg, cctx) == FAIL)
3641 return FAIL;
3642 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3643 return FAIL;
3644 *arg = skipwhite(*arg);
3645 }
3646 if (**arg == ':')
3647 {
3648 *arg = skipwhite(*arg + 1);
3649 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3650 return FAIL;
3651 if (**arg == ']')
3652 // missing second index is equal to end of string
3653 generate_PUSHNR(cctx, -1);
3654 else
3655 {
3656 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003657 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003658 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3659 return FAIL;
3660 *arg = skipwhite(*arg);
3661 }
3662 is_slice = TRUE;
3663 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003664
3665 if (**arg != ']')
3666 {
3667 emsg(_(e_missbrac));
3668 return FAIL;
3669 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003670 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003672 // We can index a list and a dict. If we don't know the type
3673 // we can use the index value type.
3674 // TODO: If we don't know use an instruction to figure it out at
3675 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003676 typep = ((type_T **)stack->ga_data) + stack->ga_len
3677 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003678 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003679 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3680 // If the index is a string, the variable must be a Dict.
3681 if (*typep == &t_any && valtype == &t_string)
3682 vtype = VAR_DICT;
3683 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003684 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003685 if (need_type(valtype, &t_number, -1, cctx,
3686 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003687 return FAIL;
3688 if (is_slice)
3689 {
3690 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003691 if (need_type(valtype, &t_number, -2, cctx,
3692 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003693 return FAIL;
3694 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003695 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003696
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003697 if (vtype == VAR_DICT)
3698 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003699 if (is_slice)
3700 {
3701 emsg(_(e_cannot_slice_dictionary));
3702 return FAIL;
3703 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003704 if ((*typep)->tt_type == VAR_DICT)
3705 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003706 else
3707 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003708 if (need_type(*typep, &t_dict_any, -2, cctx,
3709 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003710 return FAIL;
3711 *typep = &t_any;
3712 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003713 if (may_generate_2STRING(-1, cctx) == FAIL)
3714 return FAIL;
3715 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3716 return FAIL;
3717 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003718 else if (vtype == VAR_STRING)
3719 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003720 *typep = &t_string;
3721 if ((is_slice
3722 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3723 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003724 return FAIL;
3725 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003726 else if (vtype == VAR_BLOB)
3727 {
3728 emsg("Sorry, blob index and slice not implemented yet");
3729 return FAIL;
3730 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003731 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003732 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003733 if (is_slice)
3734 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003735 if (generate_instr_drop(cctx,
3736 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3737 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003738 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003739 }
Bram Moolenaared591872020-08-15 22:14:53 +02003740 else
3741 {
3742 if ((*typep)->tt_type == VAR_LIST)
3743 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003744 if (generate_instr_drop(cctx,
3745 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3746 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003747 return FAIL;
3748 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003749 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003750 else
3751 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003752 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003753 return FAIL;
3754 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003755 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003756 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003757 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003758 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003759 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003760 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003761 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003762
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003763 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003764 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003765 {
3766 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003767 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003768 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003769 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003770 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771 while (eval_isnamec(*p))
3772 MB_PTR_ADV(p);
3773 if (p == *arg)
3774 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003775 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 return FAIL;
3777 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003778 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003779 return FAIL;
3780 *arg = p;
3781 }
3782 else
3783 break;
3784 }
3785
3786 // TODO - see handle_subscript():
3787 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3788 // Don't do this when "Func" is already a partial that was bound
3789 // explicitly (pt_auto is FALSE).
3790
3791 return OK;
3792}
3793
3794/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003795 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3796 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003798 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003799 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003800 *
3801 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 */
3803
3804/*
3805 * number number constant
3806 * 0zFFFFFFFF Blob constant
3807 * "string" string constant
3808 * 'string' literal string constant
3809 * &option-name option value
3810 * @r register contents
3811 * identifier variable value
3812 * function() function call
3813 * $VAR environment variable
3814 * (expression) nested expression
3815 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01003816 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 *
3818 * Also handle:
3819 * ! in front logical NOT
3820 * - in front unary minus
3821 * + in front unary plus (ignored)
3822 * trailing (arg) funcref/partial call
3823 * trailing [] subscript in String or List
3824 * trailing .name entry in Dictionary
3825 * trailing ->name() method call
3826 */
3827 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003828compile_expr7(
3829 char_u **arg,
3830 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003831 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003832{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833 char_u *start_leader, *end_leader;
3834 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003835 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003836 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003838 ppconst->pp_is_const = FALSE;
3839
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 /*
3841 * Skip '!', '-' and '+' characters. They are handled later.
3842 */
3843 start_leader = *arg;
3844 while (**arg == '!' || **arg == '-' || **arg == '+')
3845 *arg = skipwhite(*arg + 1);
3846 end_leader = *arg;
3847
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003848 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 switch (**arg)
3850 {
3851 /*
3852 * Number constant.
3853 */
3854 case '0': // also for blob starting with 0z
3855 case '1':
3856 case '2':
3857 case '3':
3858 case '4':
3859 case '5':
3860 case '6':
3861 case '7':
3862 case '8':
3863 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003864 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003866 // Apply "-" and "+" just before the number now, right to
3867 // left. Matters especially when "->" follows. Stops at
3868 // '!'.
3869 if (apply_leader(rettv, TRUE,
3870 start_leader, &end_leader) == FAIL)
3871 {
3872 clear_tv(rettv);
3873 return FAIL;
3874 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 break;
3876
3877 /*
3878 * String constant: "string".
3879 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003880 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003881 return FAIL;
3882 break;
3883
3884 /*
3885 * Literal string constant: 'str''ing'.
3886 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003887 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 return FAIL;
3889 break;
3890
3891 /*
3892 * Constant Vim variable.
3893 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003894 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003895 ret = NOTDONE;
3896 break;
3897
3898 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003899 * "true" constant
3900 */
3901 case 't': if (STRNCMP(*arg, "true", 4) == 0
3902 && !eval_isnamec((*arg)[4]))
3903 {
3904 *arg += 4;
3905 rettv->v_type = VAR_BOOL;
3906 rettv->vval.v_number = VVAL_TRUE;
3907 }
3908 else
3909 ret = NOTDONE;
3910 break;
3911
3912 /*
3913 * "false" constant
3914 */
3915 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3916 && !eval_isnamec((*arg)[5]))
3917 {
3918 *arg += 5;
3919 rettv->v_type = VAR_BOOL;
3920 rettv->vval.v_number = VVAL_FALSE;
3921 }
3922 else
3923 ret = NOTDONE;
3924 break;
3925
3926 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927 * List: [expr, expr]
3928 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003929 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003930 break;
3931
3932 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933 * Lambda: {arg, arg -> expr}
3934 * Dictionary: {'key': val, 'key': val}
3935 */
3936 case '{': {
Bram Moolenaar27bf7af2020-12-23 20:27:31 +01003937 char_u *start = skipwhite(*arg + 1);
3938 char_u *after = start;
3939 garray_T ga_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940
3941 // Find out what comes after the arguments.
Bram Moolenaar27bf7af2020-12-23 20:27:31 +01003942 ret = get_function_args(&after, '-', NULL,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003943 &ga_arg, TRUE, NULL, NULL,
3944 TRUE, NULL, NULL);
Bram Moolenaar27bf7af2020-12-23 20:27:31 +01003945 if (ret != FAIL && after[0] == '>'
3946 && ((after > start + 2
3947 && VIM_ISWHITE(after[-2]))
3948 || after == start + 1)
3949 && IS_WHITE_OR_NUL(after[1]))
Bram Moolenaar65c44152020-12-24 15:14:01 +01003950 // TODO: if we go with the "(arg) => expr" syntax
3951 // remove this
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 ret = compile_lambda(arg, cctx);
3953 else
Bram Moolenaare0de1712020-12-02 17:36:54 +01003954 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955 }
3956 break;
3957
3958 /*
3959 * Option value: &name
3960 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003961 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3962 return FAIL;
3963 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003964 break;
3965
3966 /*
3967 * Environment variable: $VAR.
3968 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003969 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3970 return FAIL;
3971 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972 break;
3973
3974 /*
3975 * Register contents: @r.
3976 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003977 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3978 return FAIL;
3979 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980 break;
3981 /*
3982 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01003983 * lambda: (arg, arg) => expr
3984 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003985 */
Bram Moolenaar65c44152020-12-24 15:14:01 +01003986 case '(': {
3987 char_u *start = skipwhite(*arg + 1);
3988 char_u *after = start;
3989 garray_T ga_arg;
Bram Moolenaar1c747212020-05-09 18:28:34 +02003990
Bram Moolenaar65c44152020-12-24 15:14:01 +01003991 // Find out if "=>" comes after the ().
3992 ret = get_function_args(&after, ')', NULL,
3993 &ga_arg, TRUE, NULL, NULL,
3994 TRUE, NULL, NULL);
3995 if (ret == OK && VIM_ISWHITE(
3996 *after == ':' ? after[1] : *after))
3997 {
3998 if (*after == ':')
3999 // Skip over type in "(arg): type".
4000 after = skip_type(skipwhite(after + 1), TRUE);
4001
4002 after = skipwhite(after);
4003 if (after[0] == '=' && after[1] == '>'
4004 && IS_WHITE_OR_NUL(after[2]))
4005 {
4006 ret = compile_lambda(arg, cctx);
4007 break;
4008 }
4009 }
4010
4011 // (expression): recursive!
4012 *arg = skipwhite(*arg + 1);
4013 if (ppconst->pp_used <= PPSIZE - 10)
4014 {
4015 ret = compile_expr1(arg, cctx, ppconst);
4016 }
4017 else
4018 {
4019 // Not enough space in ppconst, flush constants.
4020 if (generate_ppconst(cctx, ppconst) == FAIL)
4021 return FAIL;
4022 ret = compile_expr0(arg, cctx);
4023 }
4024 *arg = skipwhite(*arg);
4025 if (**arg == ')')
4026 ++*arg;
4027 else if (ret == OK)
4028 {
4029 emsg(_(e_missing_close));
4030 ret = FAIL;
4031 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032 }
4033 break;
4034
4035 default: ret = NOTDONE;
4036 break;
4037 }
4038 if (ret == FAIL)
4039 return FAIL;
4040
Bram Moolenaar1c747212020-05-09 18:28:34 +02004041 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004043 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004044 clear_tv(rettv);
4045 else
4046 // A constant expression can possibly be handled compile time,
4047 // return the value instead of generating code.
4048 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049 }
4050 else if (ret == NOTDONE)
4051 {
4052 char_u *p;
4053 int r;
4054
4055 if (!eval_isnamec1(**arg))
4056 {
Bram Moolenaare4984292020-12-13 14:19:25 +01004057 if (ends_excmd(*skipwhite(*arg)))
4058 semsg(_(e_empty_expression_str), *arg);
4059 else
4060 semsg(_(e_name_expected_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061 return FAIL;
4062 }
4063
4064 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004065 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004066 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004067 {
4068 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4069 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004071 {
4072 if (generate_ppconst(cctx, ppconst) == FAIL)
4073 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004074 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004075 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004076 if (r == FAIL)
4077 return FAIL;
4078 }
4079
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004080 // Handle following "[]", ".member", etc.
4081 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004082 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004083 ppconst) == FAIL)
4084 return FAIL;
4085 if (ppconst->pp_used > 0)
4086 {
4087 // apply the '!', '-' and '+' before the constant
4088 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004089 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004090 return FAIL;
4091 return OK;
4092 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004093 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004094 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004095 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096}
4097
4098/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004099 * Give the "white on both sides" error, taking the operator from "p[len]".
4100 */
4101 void
4102error_white_both(char_u *op, int len)
4103{
4104 char_u buf[10];
4105
4106 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004107 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004108}
4109
4110/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004111 * <type>expr7: runtime type check / conversion
4112 */
4113 static int
4114compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4115{
4116 type_T *want_type = NULL;
4117
4118 // Recognize <type>
4119 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4120 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004121 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004122 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4123 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004124 return FAIL;
4125
4126 if (**arg != '>')
4127 {
4128 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004129 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004130 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004131 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004132 return FAIL;
4133 }
4134 ++*arg;
4135 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
4136 return FAIL;
4137 }
4138
4139 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4140 return FAIL;
4141
4142 if (want_type != NULL)
4143 {
4144 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004145 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004146
Bram Moolenaard1103582020-08-14 22:44:25 +02004147 generate_ppconst(cctx, ppconst);
4148 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02004149 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004150 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004151 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004152 return FAIL;
4153 }
4154 }
4155
4156 return OK;
4157}
4158
4159/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 * * number multiplication
4161 * / number division
4162 * % number modulo
4163 */
4164 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004165compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004166{
4167 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004168 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004169 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004171 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004172 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004173 return FAIL;
4174
4175 /*
4176 * Repeat computing, until no "*", "/" or "%" is following.
4177 */
4178 for (;;)
4179 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004180 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181 if (*op != '*' && *op != '/' && *op != '%')
4182 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004183 if (next != NULL)
4184 {
4185 *arg = next_line_from_context(cctx, TRUE);
4186 op = skipwhite(*arg);
4187 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004188
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004189 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004190 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004191 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004192 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004193 }
4194 *arg = skipwhite(op + 1);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004195 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004196 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004197
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004198 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004199 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004200 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004201
4202 if (ppconst->pp_used == ppconst_used + 2
4203 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4204 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004205 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004206 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4207 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004208 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004210 // both are numbers: compute the result
4211 switch (*op)
4212 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004213 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004214 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004215 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004216 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004217 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004218 break;
4219 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004220 tv1->vval.v_number = res;
4221 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004222 }
4223 else
4224 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004225 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004226 generate_two_op(cctx, op);
4227 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 }
4229
4230 return OK;
4231}
4232
4233/*
4234 * + number addition
4235 * - number subtraction
4236 * .. string concatenation
4237 */
4238 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004239compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240{
4241 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004242 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004243 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004244 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245
4246 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004247 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 return FAIL;
4249
4250 /*
4251 * Repeat computing, until no "+", "-" or ".." is following.
4252 */
4253 for (;;)
4254 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004255 op = may_peek_next_line(cctx, *arg, &next);
4256 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004257 break;
4258 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004259 if (next != NULL)
4260 {
4261 *arg = next_line_from_context(cctx, TRUE);
4262 op = skipwhite(*arg);
4263 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004265 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004267 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004268 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269 }
4270
4271 *arg = skipwhite(op + oplen);
Bram Moolenaare0de1712020-12-02 17:36:54 +01004272 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004273 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004275 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004276 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277 return FAIL;
4278
Bram Moolenaara5565e42020-05-09 15:44:01 +02004279 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004280 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004281 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4282 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4283 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4284 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004285 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004286 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4287 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004288
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004289 // concat/subtract/add constant numbers
4290 if (*op == '+')
4291 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4292 else if (*op == '-')
4293 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4294 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004295 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004296 // concatenate constant strings
4297 char_u *s1 = tv1->vval.v_string;
4298 char_u *s2 = tv2->vval.v_string;
4299 size_t len1 = STRLEN(s1);
4300
4301 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4302 if (tv1->vval.v_string == NULL)
4303 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004304 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004305 return FAIL;
4306 }
4307 mch_memmove(tv1->vval.v_string, s1, len1);
4308 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004309 vim_free(s1);
4310 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004311 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004312 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 }
4314 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004315 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004316 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004317 if (*op == '.')
4318 {
4319 if (may_generate_2STRING(-2, cctx) == FAIL
4320 || may_generate_2STRING(-1, cctx) == FAIL)
4321 return FAIL;
4322 generate_instr_drop(cctx, ISN_CONCAT, 1);
4323 }
4324 else
4325 generate_two_op(cctx, op);
4326 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 }
4328
4329 return OK;
4330}
4331
4332/*
4333 * expr5a == expr5b
4334 * expr5a =~ expr5b
4335 * expr5a != expr5b
4336 * expr5a !~ expr5b
4337 * expr5a > expr5b
4338 * expr5a >= expr5b
4339 * expr5a < expr5b
4340 * expr5a <= expr5b
4341 * expr5a is expr5b
4342 * expr5a isnot expr5b
4343 *
4344 * Produces instructions:
4345 * EVAL expr5a Push result of "expr5a"
4346 * EVAL expr5b Push result of "expr5b"
4347 * COMPARE one of the compare instructions
4348 */
4349 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004350compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004351{
4352 exptype_T type = EXPR_UNKNOWN;
4353 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004354 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004356 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004357 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004358
4359 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004360 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004361 return FAIL;
4362
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004363 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004364 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004365
4366 /*
4367 * If there is a comparative operator, use it.
4368 */
4369 if (type != EXPR_UNKNOWN)
4370 {
4371 int ic = FALSE; // Default: do not ignore case
4372
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004373 if (next != NULL)
4374 {
4375 *arg = next_line_from_context(cctx, TRUE);
4376 p = skipwhite(*arg);
4377 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378 if (type_is && (p[len] == '?' || p[len] == '#'))
4379 {
4380 semsg(_(e_invexpr2), *arg);
4381 return FAIL;
4382 }
4383 // extra question mark appended: ignore case
4384 if (p[len] == '?')
4385 {
4386 ic = TRUE;
4387 ++len;
4388 }
4389 // extra '#' appended: match case (ignored)
4390 else if (p[len] == '#')
4391 ++len;
4392 // nothing appended: match case
4393
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004394 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004396 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004397 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 }
4399
4400 // get the second variable
4401 *arg = skipwhite(p + len);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004402 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004403 return FAIL;
4404
Bram Moolenaara5565e42020-05-09 15:44:01 +02004405 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406 return FAIL;
4407
Bram Moolenaara5565e42020-05-09 15:44:01 +02004408 if (ppconst->pp_used == ppconst_used + 2)
4409 {
4410 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4411 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4412 int ret;
4413
4414 // Both sides are a constant, compute the result now.
4415 // First check for a valid combination of types, this is more
4416 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004417 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004418 ret = FAIL;
4419 else
4420 {
4421 ret = typval_compare(tv1, tv2, type, ic);
4422 tv1->v_type = VAR_BOOL;
4423 tv1->vval.v_number = tv1->vval.v_number
4424 ? VVAL_TRUE : VVAL_FALSE;
4425 clear_tv(tv2);
4426 --ppconst->pp_used;
4427 }
4428 return ret;
4429 }
4430
4431 generate_ppconst(cctx, ppconst);
4432 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433 }
4434
4435 return OK;
4436}
4437
Bram Moolenaar7f141552020-05-09 17:35:53 +02004438static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4439
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440/*
4441 * Compile || or &&.
4442 */
4443 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004444compile_and_or(
4445 char_u **arg,
4446 cctx_T *cctx,
4447 char *op,
4448 ppconst_T *ppconst,
4449 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004451 char_u *next;
4452 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004453 int opchar = *op;
4454
4455 if (p[0] == opchar && p[1] == opchar)
4456 {
4457 garray_T *instr = &cctx->ctx_instr;
4458 garray_T end_ga;
4459
4460 /*
4461 * Repeat until there is no following "||" or "&&"
4462 */
4463 ga_init2(&end_ga, sizeof(int), 10);
4464 while (p[0] == opchar && p[1] == opchar)
4465 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004466 if (next != NULL)
4467 {
4468 *arg = next_line_from_context(cctx, TRUE);
4469 p = skipwhite(*arg);
4470 }
4471
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004472 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4473 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004474 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004475 return FAIL;
4476 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004477
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004478 // TODO: use ppconst if the value is a constant and check
4479 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004480 generate_ppconst(cctx, ppconst);
4481
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004482 // Every part must evaluate to a bool.
4483 if (bool_on_stack(cctx) == FAIL)
4484 {
4485 ga_clear(&end_ga);
4486 return FAIL;
4487 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004488
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489 if (ga_grow(&end_ga, 1) == FAIL)
4490 {
4491 ga_clear(&end_ga);
4492 return FAIL;
4493 }
4494 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4495 ++end_ga.ga_len;
4496 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004497 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498
4499 // eval the next expression
4500 *arg = skipwhite(p + 2);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004501 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004502 {
4503 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004504 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004505 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004506
Bram Moolenaara5565e42020-05-09 15:44:01 +02004507 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4508 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004509 {
4510 ga_clear(&end_ga);
4511 return FAIL;
4512 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004513
4514 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004515 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004516 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004517
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004518 // Every part must evaluate to a bool.
4519 if (bool_on_stack(cctx) == FAIL)
4520 {
4521 ga_clear(&end_ga);
4522 return FAIL;
4523 }
4524
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525 // Fill in the end label in all jumps.
4526 while (end_ga.ga_len > 0)
4527 {
4528 isn_T *isn;
4529
4530 --end_ga.ga_len;
4531 isn = ((isn_T *)instr->ga_data)
4532 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4533 isn->isn_arg.jump.jump_where = instr->ga_len;
4534 }
4535 ga_clear(&end_ga);
4536 }
4537
4538 return OK;
4539}
4540
4541/*
4542 * expr4a && expr4a && expr4a logical AND
4543 *
4544 * Produces instructions:
4545 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004546 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004547 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004548 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004549 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004550 * EVAL expr4c Push result of "expr4c"
4551 * end:
4552 */
4553 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004554compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004556 int ppconst_used = ppconst->pp_used;
4557
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004558 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004559 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004560 return FAIL;
4561
4562 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004563 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564}
4565
4566/*
4567 * expr3a || expr3b || expr3c logical OR
4568 *
4569 * Produces instructions:
4570 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004571 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004572 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004574 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575 * EVAL expr3c Push result of "expr3c"
4576 * end:
4577 */
4578 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004579compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004581 int ppconst_used = ppconst->pp_used;
4582
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004583 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004584 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 return FAIL;
4586
4587 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004588 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589}
4590
4591/*
4592 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004593 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004594 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595 * JUMP_IF_FALSE alt jump if false
4596 * EVAL expr1a
4597 * JUMP_ALWAYS end
4598 * alt: EVAL expr1b
4599 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004600 *
4601 * Toplevel expression: expr2 ?? expr1
4602 * Produces instructions:
4603 * EVAL expr2 Push result of "expr2"
4604 * JUMP_AND_KEEP_IF_TRUE end jump if true
4605 * EVAL expr1
4606 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607 */
4608 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004609compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610{
4611 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004612 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004613 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614
Bram Moolenaar3988f642020-08-27 22:43:03 +02004615 // Ignore all kinds of errors when not producing code.
4616 if (cctx->ctx_skip == SKIP_YES)
4617 {
Bram Moolenaar683581e2020-10-22 21:22:58 +02004618 evalarg_T evalarg;
4619
4620 CLEAR_FIELD(evalarg);
4621 evalarg.eval_cctx = cctx;
4622 skip_expr(arg, &evalarg);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004623 return OK;
4624 }
4625
Bram Moolenaar61a89812020-05-07 16:58:17 +02004626 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004627 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628 return FAIL;
4629
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004630 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004631 if (*p == '?')
4632 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004633 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634 garray_T *instr = &cctx->ctx_instr;
4635 garray_T *stack = &cctx->ctx_type_stack;
4636 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004637 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004639 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004640 int has_const_expr = FALSE;
4641 int const_value = FALSE;
4642 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004644 if (next != NULL)
4645 {
4646 *arg = next_line_from_context(cctx, TRUE);
4647 p = skipwhite(*arg);
4648 }
4649
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004650 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004651 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004652 semsg(_(e_white_space_required_before_and_after_str),
4653 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004654 return FAIL;
4655 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004656
Bram Moolenaara5565e42020-05-09 15:44:01 +02004657 if (ppconst->pp_used == ppconst_used + 1)
4658 {
4659 // the condition is a constant, we know whether the ? or the :
4660 // expression is to be evaluated.
4661 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004662 if (op_falsy)
4663 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4664 else
4665 {
4666 int error = FALSE;
4667
4668 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4669 &error);
4670 if (error)
4671 return FAIL;
4672 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004673 cctx->ctx_skip = save_skip == SKIP_YES ||
4674 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4675
4676 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4677 // "left ?? right" and "left" is truthy: produce "left"
4678 generate_ppconst(cctx, ppconst);
4679 else
4680 {
4681 clear_tv(&ppconst->pp_tv[ppconst_used]);
4682 --ppconst->pp_used;
4683 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004684 }
4685 else
4686 {
4687 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004688 if (op_falsy)
4689 end_idx = instr->ga_len;
4690 generate_JUMP(cctx, op_falsy
4691 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4692 if (op_falsy)
4693 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004694 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004695
4696 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004697 *arg = skipwhite(p + 1 + op_falsy);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004698 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004699 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004700 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004701 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004702
Bram Moolenaara5565e42020-05-09 15:44:01 +02004703 if (!has_const_expr)
4704 {
4705 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004707 if (!op_falsy)
4708 {
4709 // remember the type and drop it
4710 --stack->ga_len;
4711 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004712
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004713 end_idx = instr->ga_len;
4714 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004715
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004716 // jump here from JUMP_IF_FALSE
4717 isn = ((isn_T *)instr->ga_data) + alt_idx;
4718 isn->isn_arg.jump.jump_where = instr->ga_len;
4719 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004720 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004722 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004723 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004724 // Check for the ":".
4725 p = may_peek_next_line(cctx, *arg, &next);
4726 if (*p != ':')
4727 {
4728 emsg(_(e_missing_colon));
4729 return FAIL;
4730 }
4731 if (next != NULL)
4732 {
4733 *arg = next_line_from_context(cctx, TRUE);
4734 p = skipwhite(*arg);
4735 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004736
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004737 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4738 {
4739 semsg(_(e_white_space_required_before_and_after_str), ":");
4740 return FAIL;
4741 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004742
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004743 // evaluate the third expression
4744 if (has_const_expr)
4745 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004746 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004747 *arg = skipwhite(p + 1);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004748 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004749 return FAIL;
4750 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4751 return FAIL;
4752 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004753
Bram Moolenaara5565e42020-05-09 15:44:01 +02004754 if (!has_const_expr)
4755 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004756 type_T **typep;
4757
Bram Moolenaara5565e42020-05-09 15:44:01 +02004758 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004759
Bram Moolenaara5565e42020-05-09 15:44:01 +02004760 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004761 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4762 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004763
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004764 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004765 isn = ((isn_T *)instr->ga_data) + end_idx;
4766 isn->isn_arg.jump.jump_where = instr->ga_len;
4767 }
4768
4769 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004770 }
4771 return OK;
4772}
4773
4774/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004775 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004776 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4777 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004778 */
4779 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004780compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004781{
4782 ppconst_T ppconst;
4783
4784 CLEAR_FIELD(ppconst);
4785 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4786 {
4787 clear_ppconst(&ppconst);
4788 return FAIL;
4789 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004790 if (is_const != NULL)
4791 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004792 if (generate_ppconst(cctx, &ppconst) == FAIL)
4793 return FAIL;
4794 return OK;
4795}
4796
4797/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004798 * Toplevel expression.
4799 */
4800 static int
4801compile_expr0(char_u **arg, cctx_T *cctx)
4802{
4803 return compile_expr0_ext(arg, cctx, NULL);
4804}
4805
4806/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004807 * compile "return [expr]"
4808 */
4809 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004810compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004811{
4812 char_u *p = arg;
4813 garray_T *stack = &cctx->ctx_type_stack;
4814 type_T *stack_type;
4815
4816 if (*p != NUL && *p != '|' && *p != '\n')
4817 {
4818 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004819 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004820 return NULL;
4821
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004822 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004823 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004824 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004825 if (check_return_type && cctx->ctx_ufunc->uf_ret_type == NULL)
4826 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004827 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004828 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004829 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004830 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004831 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4832 && stack_type->tt_type != VAR_VOID
4833 && stack_type->tt_type != VAR_UNKNOWN)
4834 {
4835 emsg(_(e_returning_value_in_function_without_return_type));
4836 return NULL;
4837 }
4838 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004839 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004840 return NULL;
4841 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02004842 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004843 }
4844 else
4845 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004846 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004847 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004848 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4849 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004850 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004851 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004852 return NULL;
4853 }
4854
4855 // No argument, return zero.
4856 generate_PUSHNR(cctx, 0);
4857 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004858 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004859 return NULL;
4860
4861 // "return val | endif" is possible
4862 return skipwhite(p);
4863}
4864
4865/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004866 * Get a line from the compilation context, compatible with exarg_T getline().
4867 * Return a pointer to the line in allocated memory.
4868 * Return NULL for end-of-file or some error.
4869 */
4870 static char_u *
4871exarg_getline(
4872 int c UNUSED,
4873 void *cookie,
4874 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004875 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004876{
4877 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004878 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004879
Bram Moolenaar66250c92020-08-20 15:02:42 +02004880 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004881 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004882 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004883 return NULL;
4884 ++cctx->ctx_lnum;
4885 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4886 // Comment lines result in NULL pointers, skip them.
4887 if (p != NULL)
4888 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004889 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004890}
4891
4892/*
4893 * Compile a nested :def command.
4894 */
4895 static char_u *
4896compile_nested_function(exarg_T *eap, cctx_T *cctx)
4897{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004898 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004899 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004900 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004901 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004902 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004903 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004904
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004905 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004906 {
4907 emsg(_(e_cannot_use_bang_with_nested_def));
4908 return NULL;
4909 }
4910
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004911 if (*name_start == '/')
4912 {
4913 name_end = skip_regexp(name_start + 1, '/', TRUE);
4914 if (*name_end == '/')
4915 ++name_end;
4916 eap->nextcmd = check_nextcmd(name_end);
4917 }
4918 if (name_end == name_start || *skipwhite(name_end) != '(')
4919 {
4920 if (!ends_excmd2(name_start, name_end))
4921 {
4922 semsg(_(e_invalid_command_str), eap->cmd);
4923 return NULL;
4924 }
4925
4926 // "def" or "def Name": list functions
4927 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
4928 return NULL;
4929 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
4930 }
4931
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004932 // Only g:Func() can use a namespace.
4933 if (name_start[1] == ':' && !is_global)
4934 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004935 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004936 return NULL;
4937 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004938 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4939 return NULL;
4940
Bram Moolenaar04b12692020-05-04 23:24:44 +02004941 eap->arg = name_end;
4942 eap->getline = exarg_getline;
4943 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004944 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004945 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004946 lambda_name = vim_strsave(get_lambda_name());
4947 if (lambda_name == NULL)
4948 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004949 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004950
Bram Moolenaar822ba242020-05-24 23:00:18 +02004951 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004952 {
4953 r = eap->skip ? OK : FAIL;
4954 goto theend;
4955 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004956 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004957 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004958 {
4959 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004960 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004961 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004962
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004963 if (is_global)
4964 {
4965 char_u *func_name = vim_strnsave(name_start + 2,
4966 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004967
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004968 if (func_name == NULL)
4969 r = FAIL;
4970 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004971 {
Bram Moolenaareef21022020-08-01 22:16:43 +02004972 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004973 lambda_name = NULL;
4974 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004975 }
4976 else
4977 {
4978 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004979 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004980 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004981 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004982
Bram Moolenaareef21022020-08-01 22:16:43 +02004983 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004984 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004985 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004986 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004987 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004988
4989 // copy over the block scope IDs
4990 if (block_depth > 0)
4991 {
4992 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4993 if (ufunc->uf_block_ids != NULL)
4994 {
4995 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4996 sizeof(int) * block_depth);
4997 ufunc->uf_block_depth = block_depth;
4998 }
4999 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005000 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005001 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005002 r = OK;
5003
5004theend:
5005 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005006 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005007}
5008
5009/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005010 * Return the length of an assignment operator, or zero if there isn't one.
5011 */
5012 int
5013assignment_len(char_u *p, int *heredoc)
5014{
5015 if (*p == '=')
5016 {
5017 if (p[1] == '<' && p[2] == '<')
5018 {
5019 *heredoc = TRUE;
5020 return 3;
5021 }
5022 return 1;
5023 }
5024 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5025 return 2;
5026 if (STRNCMP(p, "..=", 3) == 0)
5027 return 3;
5028 return 0;
5029}
5030
5031// words that cannot be used as a variable
5032static char *reserved[] = {
5033 "true",
5034 "false",
5035 NULL
5036};
5037
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005038typedef enum {
5039 dest_local,
5040 dest_option,
5041 dest_env,
5042 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005043 dest_buffer,
5044 dest_window,
5045 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005046 dest_vimvar,
5047 dest_script,
5048 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005049 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005050} assign_dest_T;
5051
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005052/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005053 * Generate the load instruction for "name".
5054 */
5055 static void
5056generate_loadvar(
5057 cctx_T *cctx,
5058 assign_dest_T dest,
5059 char_u *name,
5060 lvar_T *lvar,
5061 type_T *type)
5062{
5063 switch (dest)
5064 {
5065 case dest_option:
5066 // TODO: check the option exists
5067 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5068 break;
5069 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005070 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5071 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5072 else
5073 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005074 break;
5075 case dest_buffer:
5076 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5077 break;
5078 case dest_window:
5079 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5080 break;
5081 case dest_tab:
5082 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5083 break;
5084 case dest_script:
5085 compile_load_scriptvar(cctx,
5086 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5087 break;
5088 case dest_env:
5089 // Include $ in the name here
5090 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5091 break;
5092 case dest_reg:
5093 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5094 break;
5095 case dest_vimvar:
5096 generate_LOADV(cctx, name + 2, TRUE);
5097 break;
5098 case dest_local:
5099 if (lvar->lv_from_outer)
5100 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
5101 NULL, type);
5102 else
5103 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5104 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005105 case dest_expr:
5106 // list or dict value should already be on the stack.
5107 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005108 }
5109}
5110
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005111/*
5112 * Skip over "[expr]" or ".member".
5113 * Does not check for any errors.
5114 */
5115 static char_u *
5116skip_index(char_u *start)
5117{
5118 char_u *p = start;
5119
5120 if (*p == '[')
5121 {
5122 p = skipwhite(p + 1);
5123 (void)skip_expr(&p, NULL);
5124 p = skipwhite(p);
5125 if (*p == ']')
5126 return p + 1;
5127 return p;
5128 }
5129 // if (*p == '.')
5130 return to_name_end(p + 1, TRUE);
5131}
5132
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005133 void
5134vim9_declare_error(char_u *name)
5135{
5136 char *scope = "";
5137
5138 switch (*name)
5139 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005140 case 'g': scope = _("global"); break;
5141 case 'b': scope = _("buffer"); break;
5142 case 'w': scope = _("window"); break;
5143 case 't': scope = _("tab"); break;
5144 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005145 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005146 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005147 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005148 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005149 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005150 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005151 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005152 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005153 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005154}
5155
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005156/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005157 * For one assignment figure out the type of destination. Return it in "dest".
5158 * When not recognized "dest" is not set.
5159 * For an option "opt_flags" is set.
5160 * For a v:var "vimvaridx" is set.
5161 * "type" is set to the destination type if known, unchanted otherwise.
5162 * Return FAIL if an error message was given.
5163 */
5164 static int
5165get_var_dest(
5166 char_u *name,
5167 assign_dest_T *dest,
5168 int cmdidx,
5169 int *opt_flags,
5170 int *vimvaridx,
5171 type_T **type,
5172 cctx_T *cctx)
5173{
5174 char_u *p;
5175
5176 if (*name == '&')
5177 {
5178 int cc;
5179 long numval;
5180 int opt_type;
5181
5182 *dest = dest_option;
5183 if (cmdidx == CMD_final || cmdidx == CMD_const)
5184 {
5185 emsg(_(e_const_option));
5186 return FAIL;
5187 }
5188 p = name;
5189 p = find_option_end(&p, opt_flags);
5190 if (p == NULL)
5191 {
5192 // cannot happen?
5193 emsg(_(e_letunexp));
5194 return FAIL;
5195 }
5196 cc = *p;
5197 *p = NUL;
5198 opt_type = get_option_value(skip_option_env_lead(name),
5199 &numval, NULL, *opt_flags);
5200 *p = cc;
5201 if (opt_type == -3)
5202 {
5203 semsg(_(e_unknown_option), name);
5204 return FAIL;
5205 }
5206 if (opt_type == -2 || opt_type == 0)
5207 *type = &t_string;
5208 else
5209 *type = &t_number; // both number and boolean option
5210 }
5211 else if (*name == '$')
5212 {
5213 *dest = dest_env;
5214 *type = &t_string;
5215 }
5216 else if (*name == '@')
5217 {
5218 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5219 {
5220 emsg_invreg(name[1]);
5221 return FAIL;
5222 }
5223 *dest = dest_reg;
5224 *type = &t_string;
5225 }
5226 else if (STRNCMP(name, "g:", 2) == 0)
5227 {
5228 *dest = dest_global;
5229 }
5230 else if (STRNCMP(name, "b:", 2) == 0)
5231 {
5232 *dest = dest_buffer;
5233 }
5234 else if (STRNCMP(name, "w:", 2) == 0)
5235 {
5236 *dest = dest_window;
5237 }
5238 else if (STRNCMP(name, "t:", 2) == 0)
5239 {
5240 *dest = dest_tab;
5241 }
5242 else if (STRNCMP(name, "v:", 2) == 0)
5243 {
5244 typval_T *vtv;
5245 int di_flags;
5246
5247 *vimvaridx = find_vim_var(name + 2, &di_flags);
5248 if (*vimvaridx < 0)
5249 {
5250 semsg(_(e_variable_not_found_str), name);
5251 return FAIL;
5252 }
5253 // We use the current value of "sandbox" here, is that OK?
5254 if (var_check_ro(di_flags, name, FALSE))
5255 return FAIL;
5256 *dest = dest_vimvar;
5257 vtv = get_vim_var_tv(*vimvaridx);
5258 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5259 }
5260 return OK;
5261}
5262
5263/*
5264 * Generate a STORE instruction for "dest", not being "dest_local".
5265 * Return FAIL when out of memory.
5266 */
5267 static int
5268generate_store_var(
5269 cctx_T *cctx,
5270 assign_dest_T dest,
5271 int opt_flags,
5272 int vimvaridx,
5273 int scriptvar_idx,
5274 int scriptvar_sid,
5275 type_T *type,
5276 char_u *name)
5277{
5278 switch (dest)
5279 {
5280 case dest_option:
5281 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5282 opt_flags);
5283 case dest_global:
5284 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005285 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5286 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005287 case dest_buffer:
5288 // include b: with the name, easier to execute that way
5289 return generate_STORE(cctx, ISN_STOREB, 0, name);
5290 case dest_window:
5291 // include w: with the name, easier to execute that way
5292 return generate_STORE(cctx, ISN_STOREW, 0, name);
5293 case dest_tab:
5294 // include t: with the name, easier to execute that way
5295 return generate_STORE(cctx, ISN_STORET, 0, name);
5296 case dest_env:
5297 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5298 case dest_reg:
5299 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5300 case dest_vimvar:
5301 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5302 case dest_script:
5303 if (scriptvar_idx < 0)
5304 {
5305 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005306 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005307
Bram Moolenaar41d61962020-12-06 20:12:43 +01005308 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005309 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5310 scriptvar_sid, type);
5311 if (name_s != name)
5312 vim_free(name_s);
5313 return r;
5314 }
5315 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5316 scriptvar_sid, scriptvar_idx, type);
5317 case dest_local:
5318 case dest_expr:
5319 // cannot happen
5320 break;
5321 }
5322 return FAIL;
5323}
5324
5325/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005326 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005327 * "let name"
5328 * "var name = expr"
5329 * "final name = expr"
5330 * "const name = expr"
5331 * "name = expr"
5332 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005333 * Return NULL for an error.
5334 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005335 */
5336 static char_u *
5337compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5338{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005339 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005340 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005341 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005342 char_u *ret = NULL;
5343 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005344 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005345 int scriptvar_sid = 0;
5346 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005347 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005348 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005349 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005350 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005351 int oplen = 0;
5352 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005353 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005354 type_T *member_type = &t_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005355 type_T *rhs_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005356 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005357 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005358 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
5359 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005360
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005361 // Skip over the "var" or "[var, var]" to get to any "=".
5362 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5363 if (p == NULL)
5364 return *arg == '[' ? arg : NULL;
5365
5366 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005367 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005368 // TODO: should we allow this, and figure out type inference from list
5369 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005370 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005371 return NULL;
5372 }
5373
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005374 sp = p;
5375 p = skipwhite(p);
5376 op = p;
5377 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005378
5379 if (var_count > 0 && oplen == 0)
5380 // can be something like "[1, 2]->func()"
5381 return arg;
5382
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005383 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005384 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005385 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005386 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005387 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005388
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005389 if (heredoc)
5390 {
5391 list_T *l;
5392 listitem_T *li;
5393
5394 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005395 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005396 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005397 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005398 if (l == NULL)
5399 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005400
Bram Moolenaar078269b2020-09-21 20:35:55 +02005401 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005402 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005403 // Push each line and the create the list.
5404 FOR_ALL_LIST_ITEMS(l, li)
5405 {
5406 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5407 li->li_tv.vval.v_string = NULL;
5408 }
5409 generate_NEWLIST(cctx, l->lv_len);
5410 type = &t_list_string;
5411 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005412 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005413 list_free(l);
5414 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005415 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005416 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005417 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005418 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005419 char_u *wp;
5420
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005421 // for "[var, var] = expr" evaluate the expression here, loop over the
5422 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005423 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005424
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005425 wp = op + oplen;
5426 p = skipwhite(wp);
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01005427 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005428 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005429 if (compile_expr0(&p, cctx) == FAIL)
5430 return NULL;
5431 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005432
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005433 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005434 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005435 type_T *stacktype;
5436
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005437 stacktype = stack->ga_len == 0 ? &t_void
5438 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005439 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005440 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005441 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005442 goto theend;
5443 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005444 if (need_type(stacktype, &t_list_any, -1, cctx,
5445 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005446 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005447 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005448 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5449 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005450 if (stacktype->tt_member != NULL)
5451 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005452 }
5453 }
5454
5455 /*
5456 * Loop over variables in "[var, var] = expr".
5457 * For "var = expr" and "let var: type" this is done only once.
5458 */
5459 if (var_count > 0)
5460 var_start = skipwhite(arg + 1); // skip over the "["
5461 else
5462 var_start = arg;
5463 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5464 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005465 char_u *var_end;
5466 char_u *dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005467 size_t varlen;
5468 int new_local = FALSE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005469 assign_dest_T dest = dest_local;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005470 int opt_flags = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005471 int vimvaridx = -1;
Bram Moolenaar709664c2020-12-12 14:33:41 +01005472 lvar_T local_lvar;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005473 lvar_T *lvar = NULL;
5474 lvar_T arg_lvar;
5475 int has_type = FALSE;
5476 int has_index = FALSE;
5477 int instr_count = -1;
5478
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005479 // "dest_end" is the end of the destination, including "[expr]" or
5480 // ".name".
5481 // "var_end" is the end of the variable/option/etc. name.
5482 dest_end = skip_var_one(var_start, FALSE);
Bram Moolenaar65821722020-08-02 18:58:54 +02005483 if (*var_start == '@')
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005484 var_end = var_start + 2;
Bram Moolenaar65821722020-08-02 18:58:54 +02005485 else
5486 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02005487 // skip over the leading "&", "&l:", "&g:" and "$"
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005488 var_end = skip_option_env_lead(var_start);
5489 var_end = to_name_end(var_end, TRUE);
Bram Moolenaar65821722020-08-02 18:58:54 +02005490 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005491
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005492 // "a: type" is declaring variable "a" with a type, not dict "a:".
5493 if (is_decl && dest_end == var_start + 2 && dest_end[-1] == ':')
5494 --dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005495 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5496 --var_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005497
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005498 // compute the length of the destination without "[expr]" or ".name"
5499 varlen = var_end - var_start;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005500 vim_free(name);
5501 name = vim_strnsave(var_start, varlen);
5502 if (name == NULL)
5503 return NULL;
5504 if (!heredoc)
5505 type = &t_any;
5506
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005507 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005508 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005509 int declare_error = FALSE;
5510
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005511 if (get_var_dest(name, &dest, cmdidx, &opt_flags,
5512 &vimvaridx, &type, cctx) == FAIL)
5513 goto theend;
5514 if (dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005515 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005516 // Specific kind of variable recognized.
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005517 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005518 }
5519 else
5520 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005521 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005522
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005523 // No specific kind of variable recognized, just a name.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005524 for (idx = 0; reserved[idx] != NULL; ++idx)
5525 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005526 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005527 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005528 goto theend;
5529 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005530
Bram Moolenaar709664c2020-12-12 14:33:41 +01005531
5532 if (lookup_local(var_start, varlen, &local_lvar, cctx) == OK)
5533 lvar = &local_lvar;
5534 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005535 {
5536 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005537 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005538 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5539 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005540 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005541 if (is_decl)
5542 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005543 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005544 goto theend;
5545 }
5546 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005547 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005548 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005549 if (lvar != NULL)
5550 {
5551 if (is_decl)
5552 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005553 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005554 goto theend;
5555 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005556 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005557 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005558 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005559 int script_namespace = varlen > 1
5560 && STRNCMP(var_start, "s:", 2) == 0;
5561 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005562 ? script_var_exists(var_start + 2, varlen - 2,
5563 FALSE, cctx)
5564 : script_var_exists(var_start, varlen,
5565 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005566 imported_T *import =
5567 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005568
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005569 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005570 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005571 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5572
5573 if (is_decl)
5574 {
5575 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005576 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005577 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005578 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005579 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005580 name);
5581 goto theend;
5582 }
5583 else if (cctx->ctx_ufunc->uf_script_ctx_version
5584 == SCRIPT_VERSION_VIM9
5585 && script_namespace
5586 && !script_var && import == NULL)
5587 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005588 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005589 goto theend;
5590 }
5591
5592 dest = dest_script;
5593
5594 // existing script-local variables should have a type
5595 scriptvar_sid = current_sctx.sc_sid;
5596 if (import != NULL)
5597 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005598 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005599 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005600 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005601 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005602 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005603 {
5604 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5605 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005606 ((svar_T *)si->sn_var_vals.ga_data)
5607 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005608 type = sv->sv_type;
5609 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005610 }
5611 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005612 else if (check_defined(var_start, varlen, cctx) == FAIL)
5613 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005614 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005615 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005616
5617 if (declare_error)
5618 {
5619 vim9_declare_error(name);
5620 goto theend;
5621 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005622 }
5623
5624 // handle "a:name" as a name, not index "name" on "a"
5625 if (varlen > 1 || var_start[varlen] != ':')
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005626 var_end = dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005627
5628 if (dest != dest_option)
5629 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005630 if (is_decl && *var_end == ':')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005631 {
5632 // parse optional type: "let var: type = expr"
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005633 if (!VIM_ISWHITE(var_end[1]))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005634 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005635 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005636 goto theend;
5637 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005638 p = skipwhite(var_end + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005639 type = parse_type(&p, cctx->ctx_type_list, TRUE);
5640 if (type == NULL)
5641 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005642 has_type = TRUE;
5643 }
5644 else if (lvar != NULL)
5645 type = lvar->lv_type;
5646 }
5647
5648 if (oplen == 3 && !heredoc && dest != dest_global
5649 && type->tt_type != VAR_STRING
5650 && type->tt_type != VAR_ANY)
5651 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005652 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005653 goto theend;
5654 }
5655
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005656 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005657 {
5658 if (oplen > 1 && !heredoc)
5659 {
5660 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005661 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005662 goto theend;
5663 }
5664
5665 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005666 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5667 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005668 goto theend;
5669 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005670 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005671 if (lvar == NULL)
5672 goto theend;
5673 new_local = TRUE;
5674 }
5675
5676 member_type = type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005677 if (dest_end > var_start + varlen)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005678 {
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005679 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005680 // TODO: should we also handle "->func()" here?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005681 if (is_decl)
5682 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005683 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005684 goto theend;
5685 }
5686
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005687 if (var_start[varlen] == '[' || var_start[varlen] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005688 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005689 char_u *after = var_start + varlen;
5690
5691 // Only the last index is used below, if there are others
5692 // before it generate code for the expression. Thus for
5693 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5694 for (;;)
5695 {
5696 p = skip_index(after);
5697 if (*p != '[' && *p != '.')
5698 break;
5699 after = p;
5700 }
5701 if (after > var_start + varlen)
5702 {
5703 varlen = after - var_start;
5704 dest = dest_expr;
5705 // We don't know the type before evaluating the expression,
5706 // use "any" until then.
5707 type = &t_any;
5708 }
5709
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005710 has_index = TRUE;
5711 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005712 member_type = &t_any;
5713 else
5714 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005715 }
5716 else
5717 {
5718 semsg("Not supported yet: %s", var_start);
5719 goto theend;
5720 }
5721 }
5722 else if (lvar == &arg_lvar)
5723 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005724 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005725 goto theend;
5726 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005727 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5728 {
5729 semsg(_(e_cannot_assign_to_constant), name);
5730 goto theend;
5731 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005732
5733 if (!heredoc)
5734 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005735 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005736 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005737 if (oplen > 0 && var_count == 0)
5738 {
5739 // skip over the "=" and the expression
5740 p = skipwhite(op + oplen);
5741 compile_expr0(&p, cctx);
5742 }
5743 }
5744 else if (oplen > 0)
5745 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005746 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005747 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005748
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005749 // For "var = expr" evaluate the expression.
5750 if (var_count == 0)
5751 {
5752 int r;
5753
5754 // for "+=", "*=", "..=" etc. first load the current value
5755 if (*op != '=')
5756 {
5757 generate_loadvar(cctx, dest, name, lvar, type);
5758
5759 if (has_index)
5760 {
5761 // TODO: get member from list or dict
5762 emsg("Index with operation not supported yet");
5763 goto theend;
5764 }
5765 }
5766
5767 // Compile the expression. Temporarily hide the new local
5768 // variable here, it is not available to this expression.
5769 if (new_local)
5770 --cctx->ctx_locals.ga_len;
5771 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005772 wp = op + oplen;
5773 p = skipwhite(wp);
5774 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01005775 {
5776 if (new_local)
5777 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005778 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01005779 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005780 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005781 if (new_local)
5782 ++cctx->ctx_locals.ga_len;
5783 if (r == FAIL)
5784 goto theend;
5785 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005786 else if (semicolon && var_idx == var_count - 1)
5787 {
5788 // For "[var; var] = expr" get the rest of the list
5789 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5790 goto theend;
5791 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005792 else
5793 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005794 // For "[var, var] = expr" get the "var_idx" item from the
5795 // list.
5796 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01005797 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005798 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005799
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005800 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005801 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005802 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005803 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005804 if ((rhs_type->tt_type == VAR_FUNC
5805 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar0f769812020-09-12 18:32:34 +02005806 && var_wrong_func_name(name, TRUE))
5807 goto theend;
5808
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005809 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005810 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005811 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005812 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005813 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005814 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005815 }
5816 else
5817 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005818 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005819 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005820 if (rhs_type == &t_list_empty)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005821 lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005822 else if (rhs_type == &t_dict_empty)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005823 lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005824 else if (rhs_type == &t_unknown)
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005825 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005826 else
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005827 lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005828 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005829 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005830 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005831 {
5832 type_T *use_type = lvar->lv_type;
5833
Bram Moolenaar71abe482020-09-14 22:28:30 +02005834 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005835 if (has_index)
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005836 use_type = member_type;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005837 if (need_type(rhs_type, use_type, -1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005838 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005839 goto theend;
5840 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005841 }
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005842 else if (*p != '=' && need_type(rhs_type, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005843 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005844 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005845 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005846 else if (cmdidx == CMD_final)
5847 {
5848 emsg(_(e_final_requires_a_value));
5849 goto theend;
5850 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005851 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005852 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005853 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005854 goto theend;
5855 }
5856 else if (!has_type || dest == dest_option)
5857 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005858 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005859 goto theend;
5860 }
5861 else
5862 {
5863 // variables are always initialized
5864 if (ga_grow(instr, 1) == FAIL)
5865 goto theend;
5866 switch (member_type->tt_type)
5867 {
5868 case VAR_BOOL:
5869 generate_PUSHBOOL(cctx, VVAL_FALSE);
5870 break;
5871 case VAR_FLOAT:
5872#ifdef FEAT_FLOAT
5873 generate_PUSHF(cctx, 0.0);
5874#endif
5875 break;
5876 case VAR_STRING:
5877 generate_PUSHS(cctx, NULL);
5878 break;
5879 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005880 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005881 break;
5882 case VAR_FUNC:
5883 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5884 break;
5885 case VAR_LIST:
5886 generate_NEWLIST(cctx, 0);
5887 break;
5888 case VAR_DICT:
5889 generate_NEWDICT(cctx, 0);
5890 break;
5891 case VAR_JOB:
5892 generate_PUSHJOB(cctx, NULL);
5893 break;
5894 case VAR_CHANNEL:
5895 generate_PUSHCHANNEL(cctx, NULL);
5896 break;
5897 case VAR_NUMBER:
5898 case VAR_UNKNOWN:
5899 case VAR_ANY:
5900 case VAR_PARTIAL:
5901 case VAR_VOID:
5902 case VAR_SPECIAL: // cannot happen
5903 generate_PUSHNR(cctx, 0);
5904 break;
5905 }
5906 }
5907 if (var_count == 0)
5908 end = p;
5909 }
5910
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005911 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005912 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005913 break;
5914
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005915 if (oplen > 0 && *op != '=')
5916 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005917 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005918 type_T *stacktype;
5919
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005920 if (*op == '.')
5921 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005922 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005923 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005924 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005925 if (
5926#ifdef FEAT_FLOAT
5927 // If variable is float operation with number is OK.
5928 !(expected == &t_float && stacktype == &t_number) &&
5929#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005930 need_type(stacktype, expected, -1, cctx,
5931 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005932 goto theend;
5933
5934 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005935 {
5936 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5937 goto theend;
5938 }
5939 else if (*op == '+')
5940 {
5941 if (generate_add_instr(cctx,
5942 operator_type(member_type, stacktype),
5943 member_type, stacktype) == FAIL)
5944 goto theend;
5945 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005946 else if (generate_two_op(cctx, op) == FAIL)
5947 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005948 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005949
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005950 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005951 {
Bram Moolenaard24602f2020-12-20 15:20:56 +01005952 int r;
5953 vartype_T dest_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005954
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005955 // Compile the "idx" in "var[idx]" or "key" in "var.key".
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005956 p = var_start + varlen;
5957 if (*p == '[')
5958 {
5959 p = skipwhite(p + 1);
5960 r = compile_expr0(&p, cctx);
5961 if (r == OK && *skipwhite(p) != ']')
5962 {
5963 // this should not happen
5964 emsg(_(e_missbrac));
5965 r = FAIL;
5966 }
5967 }
5968 else // if (*p == '.')
5969 {
5970 char_u *key_end = to_name_end(p + 1, TRUE);
5971 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5972
5973 r = generate_PUSHS(cctx, key);
5974 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005975 if (r == FAIL)
5976 goto theend;
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005977
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005978 if (type == &t_any)
5979 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005980 // Index on variable of unknown type: check at runtime.
5981 dest_type = VAR_ANY;
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005982 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005983 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005984 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005985 dest_type = type->tt_type;
5986 if (dest_type == VAR_DICT
5987 && may_generate_2STRING(-1, cctx) == FAIL)
5988 goto theend;
5989 if (dest_type == VAR_LIST
5990 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
5991 != VAR_NUMBER)
5992 {
5993 emsg(_(e_number_exp));
5994 goto theend;
5995 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005996 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005997
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005998 // Load the dict or list. On the stack we then have:
5999 // - value
6000 // - index
6001 // - variable
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006002 if (dest == dest_expr)
6003 {
6004 int c = var_start[varlen];
6005
6006 // Evaluate "ll[expr]" of "ll[expr][idx]"
6007 p = var_start;
6008 var_start[varlen] = NUL;
6009 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6010 {
6011 // this should not happen
6012 emsg(_(e_missbrac));
6013 goto theend;
6014 }
6015 var_start[varlen] = c;
6016
6017 type = stack->ga_len == 0 ? &t_void
6018 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6019 // now we can properly check the type
6020 if (type->tt_member != NULL
6021 && need_type(rhs_type, type->tt_member, -2, cctx,
6022 FALSE, FALSE) == FAIL)
6023 goto theend;
6024 }
6025 else
6026 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006027
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01006028 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6029 || dest_type == VAR_ANY)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006030 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01006031 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
6032
6033 if (isn == NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006034 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01006035 isn->isn_arg.vartype = dest_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006036 }
6037 else
6038 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006039 emsg(_(e_indexable_type_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006040 goto theend;
6041 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006042 }
6043 else
6044 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006045 if (is_decl && cmdidx == CMD_const
6046 && (dest == dest_script || dest == dest_local))
6047 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006048 generate_LOCKCONST(cctx);
6049
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006050 if (dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006051 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006052 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
6053 scriptvar_idx, scriptvar_sid, type, name) == FAIL)
6054 goto theend;
6055 }
6056 else if (lvar != NULL)
6057 {
6058 isn_T *isn = ((isn_T *)instr->ga_data)
6059 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006060
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006061 // optimization: turn "var = 123" from ISN_PUSHNR +
6062 // ISN_STORE into ISN_STORENR
6063 if (!lvar->lv_from_outer
6064 && instr->ga_len == instr_count + 1
6065 && isn->isn_type == ISN_PUSHNR)
6066 {
6067 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006068
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006069 isn->isn_type = ISN_STORENR;
6070 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
6071 isn->isn_arg.storenr.stnr_val = val;
6072 if (stack->ga_len > 0)
6073 --stack->ga_len;
6074 }
6075 else if (lvar->lv_from_outer)
6076 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, NULL);
6077 else
6078 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006079 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006080 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006081
6082 if (var_idx + 1 < var_count)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006083 var_start = skipwhite(dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006084 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006085
6086 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006087 if (var_count > 0 && !semicolon)
6088 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006089 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006090 goto theend;
6091 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006092
Bram Moolenaarb2097502020-07-19 17:17:02 +02006093 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006094
6095theend:
6096 vim_free(name);
6097 return ret;
6098}
6099
6100/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006101 * Check if "name" can be "unlet".
6102 */
6103 int
6104check_vim9_unlet(char_u *name)
6105{
6106 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6107 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006108 // "unlet s:var" is allowed in legacy script.
6109 if (*name == 's' && !script_is_vim9())
6110 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006111 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006112 return FAIL;
6113 }
6114 return OK;
6115}
6116
6117/*
6118 * Callback passed to ex_unletlock().
6119 */
6120 static int
6121compile_unlet(
6122 lval_T *lvp,
6123 char_u *name_end,
6124 exarg_T *eap,
6125 int deep UNUSED,
6126 void *coookie)
6127{
6128 cctx_T *cctx = coookie;
6129
6130 if (lvp->ll_tv == NULL)
6131 {
6132 char_u *p = lvp->ll_name;
6133 int cc = *name_end;
6134 int ret = OK;
6135
6136 // Normal name. Only supports g:, w:, t: and b: namespaces.
6137 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006138 if (*p == '$')
6139 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6140 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006141 ret = FAIL;
6142 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006143 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006144
6145 *name_end = cc;
6146 return ret;
6147 }
6148
6149 // TODO: unlet {list}[idx]
6150 // TODO: unlet {dict}[key]
6151 emsg("Sorry, :unlet not fully implemented yet");
6152 return FAIL;
6153}
6154
6155/*
6156 * compile "unlet var", "lock var" and "unlock var"
6157 * "arg" points to "var".
6158 */
6159 static char_u *
6160compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6161{
6162 char_u *p = arg;
6163
6164 if (eap->cmdidx != CMD_unlet)
6165 {
6166 emsg("Sorry, :lock and unlock not implemented yet");
6167 return NULL;
6168 }
6169
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006170 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
6171 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6172}
6173
6174/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006175 * Compile an :import command.
6176 */
6177 static char_u *
6178compile_import(char_u *arg, cctx_T *cctx)
6179{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006180 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006181}
6182
6183/*
6184 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6185 */
6186 static int
6187compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6188{
6189 garray_T *instr = &cctx->ctx_instr;
6190 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6191
6192 if (endlabel == NULL)
6193 return FAIL;
6194 endlabel->el_next = *el;
6195 *el = endlabel;
6196 endlabel->el_end_label = instr->ga_len;
6197
6198 generate_JUMP(cctx, when, 0);
6199 return OK;
6200}
6201
6202 static void
6203compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
6204{
6205 garray_T *instr = &cctx->ctx_instr;
6206
6207 while (*el != NULL)
6208 {
6209 endlabel_T *cur = (*el);
6210 isn_T *isn;
6211
6212 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
6213 isn->isn_arg.jump.jump_where = instr->ga_len;
6214 *el = cur->el_next;
6215 vim_free(cur);
6216 }
6217}
6218
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006219 static void
6220compile_free_jump_to_end(endlabel_T **el)
6221{
6222 while (*el != NULL)
6223 {
6224 endlabel_T *cur = (*el);
6225
6226 *el = cur->el_next;
6227 vim_free(cur);
6228 }
6229}
6230
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006231/*
6232 * Create a new scope and set up the generic items.
6233 */
6234 static scope_T *
6235new_scope(cctx_T *cctx, scopetype_T type)
6236{
6237 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6238
6239 if (scope == NULL)
6240 return NULL;
6241 scope->se_outer = cctx->ctx_scope;
6242 cctx->ctx_scope = scope;
6243 scope->se_type = type;
6244 scope->se_local_count = cctx->ctx_locals.ga_len;
6245 return scope;
6246}
6247
6248/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006249 * Free the current scope and go back to the outer scope.
6250 */
6251 static void
6252drop_scope(cctx_T *cctx)
6253{
6254 scope_T *scope = cctx->ctx_scope;
6255
6256 if (scope == NULL)
6257 {
6258 iemsg("calling drop_scope() without a scope");
6259 return;
6260 }
6261 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006262 switch (scope->se_type)
6263 {
6264 case IF_SCOPE:
6265 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6266 case FOR_SCOPE:
6267 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6268 case WHILE_SCOPE:
6269 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6270 case TRY_SCOPE:
6271 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6272 case NO_SCOPE:
6273 case BLOCK_SCOPE:
6274 break;
6275 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006276 vim_free(scope);
6277}
6278
6279/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006280 * compile "if expr"
6281 *
6282 * "if expr" Produces instructions:
6283 * EVAL expr Push result of "expr"
6284 * JUMP_IF_FALSE end
6285 * ... body ...
6286 * end:
6287 *
6288 * "if expr | else" Produces instructions:
6289 * EVAL expr Push result of "expr"
6290 * JUMP_IF_FALSE else
6291 * ... body ...
6292 * JUMP_ALWAYS end
6293 * else:
6294 * ... body ...
6295 * end:
6296 *
6297 * "if expr1 | elseif expr2 | else" Produces instructions:
6298 * EVAL expr Push result of "expr"
6299 * JUMP_IF_FALSE elseif
6300 * ... body ...
6301 * JUMP_ALWAYS end
6302 * elseif:
6303 * EVAL expr Push result of "expr"
6304 * JUMP_IF_FALSE else
6305 * ... body ...
6306 * JUMP_ALWAYS end
6307 * else:
6308 * ... body ...
6309 * end:
6310 */
6311 static char_u *
6312compile_if(char_u *arg, cctx_T *cctx)
6313{
6314 char_u *p = arg;
6315 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006316 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006317 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006318 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006319 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006320
Bram Moolenaara5565e42020-05-09 15:44:01 +02006321 CLEAR_FIELD(ppconst);
6322 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006323 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006324 clear_ppconst(&ppconst);
6325 return NULL;
6326 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006327 if (cctx->ctx_skip == SKIP_YES)
6328 clear_ppconst(&ppconst);
6329 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006330 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006331 int error = FALSE;
6332 int v;
6333
Bram Moolenaara5565e42020-05-09 15:44:01 +02006334 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006335 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006336 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006337 if (error)
6338 return NULL;
6339 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006340 }
6341 else
6342 {
6343 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006344 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006345 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006346 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006347 if (bool_on_stack(cctx) == FAIL)
6348 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006349 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006350
6351 scope = new_scope(cctx, IF_SCOPE);
6352 if (scope == NULL)
6353 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006354 scope->se_skip_save = skip_save;
6355 // "is_had_return" will be reset if any block does not end in :return
6356 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006357
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006358 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006359 {
6360 // "where" is set when ":elseif", "else" or ":endif" is found
6361 scope->se_u.se_if.is_if_label = instr->ga_len;
6362 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6363 }
6364 else
6365 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006366
6367 return p;
6368}
6369
6370 static char_u *
6371compile_elseif(char_u *arg, cctx_T *cctx)
6372{
6373 char_u *p = arg;
6374 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006375 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006376 isn_T *isn;
6377 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006378 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006379 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006380
6381 if (scope == NULL || scope->se_type != IF_SCOPE)
6382 {
6383 emsg(_(e_elseif_without_if));
6384 return NULL;
6385 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006386 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006387 if (!cctx->ctx_had_return)
6388 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006389
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006390 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006391 {
6392 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006393 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006394 return NULL;
6395 // previous "if" or "elseif" jumps here
6396 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6397 isn->isn_arg.jump.jump_where = instr->ga_len;
6398 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006399
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006400 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006401 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006402 if (cctx->ctx_skip == SKIP_YES)
6403 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006404 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006405 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006406 clear_ppconst(&ppconst);
6407 return NULL;
6408 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006409 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006410 if (scope->se_skip_save == SKIP_YES)
6411 clear_ppconst(&ppconst);
6412 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006413 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006414 int error = FALSE;
6415 int v;
6416
Bram Moolenaar7f141552020-05-09 17:35:53 +02006417 // The expression results in a constant.
6418 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006419 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6420 if (error)
6421 return NULL;
6422 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006423 clear_ppconst(&ppconst);
6424 scope->se_u.se_if.is_if_label = -1;
6425 }
6426 else
6427 {
6428 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006429 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006430 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006431 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006432 if (bool_on_stack(cctx) == FAIL)
6433 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006434
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006435 // "where" is set when ":elseif", "else" or ":endif" is found
6436 scope->se_u.se_if.is_if_label = instr->ga_len;
6437 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6438 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006439
6440 return p;
6441}
6442
6443 static char_u *
6444compile_else(char_u *arg, cctx_T *cctx)
6445{
6446 char_u *p = arg;
6447 garray_T *instr = &cctx->ctx_instr;
6448 isn_T *isn;
6449 scope_T *scope = cctx->ctx_scope;
6450
6451 if (scope == NULL || scope->se_type != IF_SCOPE)
6452 {
6453 emsg(_(e_else_without_if));
6454 return NULL;
6455 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006456 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006457 if (!cctx->ctx_had_return)
6458 scope->se_u.se_if.is_had_return = FALSE;
6459 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006460
Bram Moolenaarefd88552020-06-18 20:50:10 +02006461 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006462 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006463 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006464 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006465 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006466 if (!cctx->ctx_had_return
6467 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6468 JUMP_ALWAYS, cctx) == FAIL)
6469 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006470 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006471
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006472 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006473 {
6474 if (scope->se_u.se_if.is_if_label >= 0)
6475 {
6476 // previous "if" or "elseif" jumps here
6477 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6478 isn->isn_arg.jump.jump_where = instr->ga_len;
6479 scope->se_u.se_if.is_if_label = -1;
6480 }
6481 }
6482
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006483 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006484 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6485 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006486
6487 return p;
6488}
6489
6490 static char_u *
6491compile_endif(char_u *arg, cctx_T *cctx)
6492{
6493 scope_T *scope = cctx->ctx_scope;
6494 ifscope_T *ifscope;
6495 garray_T *instr = &cctx->ctx_instr;
6496 isn_T *isn;
6497
6498 if (scope == NULL || scope->se_type != IF_SCOPE)
6499 {
6500 emsg(_(e_endif_without_if));
6501 return NULL;
6502 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006503 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006504 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006505 if (!cctx->ctx_had_return)
6506 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006507
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006508 if (scope->se_u.se_if.is_if_label >= 0)
6509 {
6510 // previous "if" or "elseif" jumps here
6511 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6512 isn->isn_arg.jump.jump_where = instr->ga_len;
6513 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006514 // Fill in the "end" label in jumps at the end of the blocks.
6515 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006516 cctx->ctx_skip = scope->se_skip_save;
6517
6518 // If all the blocks end in :return and there is an :else then the
6519 // had_return flag is set.
6520 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006521
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006522 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006523 return arg;
6524}
6525
6526/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01006527 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006528 *
6529 * Produces instructions:
6530 * PUSHNR -1
6531 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01006532 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006533 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6534 * - if beyond end, jump to "end"
6535 * - otherwise get item from list and push it
6536 * STORE var Store item in "var"
6537 * ... body ...
6538 * JUMP top Jump back to repeat
6539 * end: DROP Drop the result of "expr"
6540 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006541 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
6542 * UNPACK 2 Split item in 2
6543 * STORE var1 Store item in "var1"
6544 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006545 */
6546 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006547compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006548{
Bram Moolenaar792f7862020-11-23 08:31:18 +01006549 char_u *arg;
6550 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006551 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006552 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006553 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006554 int var_count = 0;
6555 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006556 size_t varlen;
6557 garray_T *instr = &cctx->ctx_instr;
6558 garray_T *stack = &cctx->ctx_type_stack;
6559 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006560 lvar_T *loop_lvar; // loop iteration variable
6561 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006562 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006563 type_T *item_type = &t_any;
6564 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006565
Bram Moolenaar792f7862020-11-23 08:31:18 +01006566 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
6567 if (var_count == 0)
6568 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006569
6570 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006571 wp = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006572 p = skipwhite(p);
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006573 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6574 return NULL;
6575 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006576 {
6577 emsg(_(e_missing_in));
6578 return NULL;
6579 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006580 wp = p + 2;
6581 p = skipwhite(wp);
6582 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6583 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006584
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006585 scope = new_scope(cctx, FOR_SCOPE);
6586 if (scope == NULL)
6587 return NULL;
6588
Bram Moolenaar792f7862020-11-23 08:31:18 +01006589 // Reserve a variable to store the loop iteration counter and initialize it
6590 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006591 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6592 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006593 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006594 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006595 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006596 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006597 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006598 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006599
6600 // compile "expr", it remains on the stack until "endfor"
6601 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006602 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006603 {
6604 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006605 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006606 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006607 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006608
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006609 // Now that we know the type of "var", check that it is a list, now or at
6610 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006611 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006612 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006613 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006614 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006615 return NULL;
6616 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006617
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006618 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006619 {
6620 if (var_count == 1)
6621 item_type = vartype->tt_member;
6622 else if (vartype->tt_member->tt_type == VAR_LIST
6623 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
6624 item_type = vartype->tt_member->tt_member;
6625 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006626
6627 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006628 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006629 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006630
Bram Moolenaar792f7862020-11-23 08:31:18 +01006631 arg = arg_start;
6632 if (var_count > 1)
6633 {
6634 generate_UNPACK(cctx, var_count, semicolon);
6635 arg = skipwhite(arg + 1); // skip white after '['
6636
6637 // the list item is replaced by a number of items
6638 if (ga_grow(stack, var_count - 1) == FAIL)
6639 {
6640 drop_scope(cctx);
6641 return NULL;
6642 }
6643 --stack->ga_len;
6644 for (idx = 0; idx < var_count; ++idx)
6645 {
6646 ((type_T **)stack->ga_data)[stack->ga_len] =
6647 (semicolon && idx == 0) ? vartype : item_type;
6648 ++stack->ga_len;
6649 }
6650 }
6651
6652 for (idx = 0; idx < var_count; ++idx)
6653 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006654 assign_dest_T dest = dest_local;
6655 int opt_flags = 0;
6656 int vimvaridx = -1;
6657 type_T *type = &t_any;
6658
6659 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006660 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006661 name = vim_strnsave(arg, varlen);
6662 if (name == NULL)
6663 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006664
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006665 // TODO: script var not supported?
6666 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
6667 &vimvaridx, &type, cctx) == FAIL)
6668 goto failed;
6669 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006670 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006671 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
6672 0, 0, type, name) == FAIL)
6673 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006674 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006675 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006676 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01006677 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006678 {
6679 semsg(_(e_variable_already_declared), arg);
6680 goto failed;
6681 }
6682
Bram Moolenaarea870692020-12-02 14:24:30 +01006683 if (STRNCMP(name, "s:", 2) == 0)
6684 {
6685 semsg(_(e_cannot_declare_script_variable_in_function), name);
6686 goto failed;
6687 }
6688
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006689 // Reserve a variable to store "var".
6690 // TODO: check for type
6691 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6692 if (var_lvar == NULL)
6693 // out of memory or used as an argument
6694 goto failed;
6695
6696 if (semicolon && idx == var_count - 1)
6697 var_lvar->lv_type = vartype;
6698 else
6699 var_lvar->lv_type = item_type;
6700 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
6701 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006702
6703 if (*p == ',' || *p == ';')
6704 ++p;
6705 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006706 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006707 }
6708
6709 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006710
6711failed:
6712 vim_free(name);
6713 drop_scope(cctx);
6714 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006715}
6716
6717/*
6718 * compile "endfor"
6719 */
6720 static char_u *
6721compile_endfor(char_u *arg, cctx_T *cctx)
6722{
6723 garray_T *instr = &cctx->ctx_instr;
6724 scope_T *scope = cctx->ctx_scope;
6725 forscope_T *forscope;
6726 isn_T *isn;
6727
6728 if (scope == NULL || scope->se_type != FOR_SCOPE)
6729 {
6730 emsg(_(e_for));
6731 return NULL;
6732 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006733 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006734 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006735 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006736
6737 // At end of ":for" scope jump back to the FOR instruction.
6738 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6739
6740 // Fill in the "end" label in the FOR statement so it can jump here
6741 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6742 isn->isn_arg.forloop.for_end = instr->ga_len;
6743
6744 // Fill in the "end" label any BREAK statements
6745 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6746
6747 // Below the ":for" scope drop the "expr" list from the stack.
6748 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6749 return NULL;
6750
6751 vim_free(scope);
6752
6753 return arg;
6754}
6755
6756/*
6757 * compile "while expr"
6758 *
6759 * Produces instructions:
6760 * top: EVAL expr Push result of "expr"
6761 * JUMP_IF_FALSE end jump if false
6762 * ... body ...
6763 * JUMP top Jump back to repeat
6764 * end:
6765 *
6766 */
6767 static char_u *
6768compile_while(char_u *arg, cctx_T *cctx)
6769{
6770 char_u *p = arg;
6771 garray_T *instr = &cctx->ctx_instr;
6772 scope_T *scope;
6773
6774 scope = new_scope(cctx, WHILE_SCOPE);
6775 if (scope == NULL)
6776 return NULL;
6777
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006778 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006779
6780 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006781 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006782 return NULL;
6783
Bram Moolenaar13106602020-10-04 16:06:05 +02006784 if (bool_on_stack(cctx) == FAIL)
6785 return FAIL;
6786
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006787 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006788 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006789 JUMP_IF_FALSE, cctx) == FAIL)
6790 return FAIL;
6791
6792 return p;
6793}
6794
6795/*
6796 * compile "endwhile"
6797 */
6798 static char_u *
6799compile_endwhile(char_u *arg, cctx_T *cctx)
6800{
6801 scope_T *scope = cctx->ctx_scope;
6802
6803 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6804 {
6805 emsg(_(e_while));
6806 return NULL;
6807 }
6808 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006809 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006810
6811 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006812 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006813
6814 // Fill in the "end" label in the WHILE statement so it can jump here.
6815 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006816 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006817
6818 vim_free(scope);
6819
6820 return arg;
6821}
6822
6823/*
6824 * compile "continue"
6825 */
6826 static char_u *
6827compile_continue(char_u *arg, cctx_T *cctx)
6828{
6829 scope_T *scope = cctx->ctx_scope;
6830
6831 for (;;)
6832 {
6833 if (scope == NULL)
6834 {
6835 emsg(_(e_continue));
6836 return NULL;
6837 }
6838 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6839 break;
6840 scope = scope->se_outer;
6841 }
6842
6843 // Jump back to the FOR or WHILE instruction.
6844 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006845 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6846 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006847 return arg;
6848}
6849
6850/*
6851 * compile "break"
6852 */
6853 static char_u *
6854compile_break(char_u *arg, cctx_T *cctx)
6855{
6856 scope_T *scope = cctx->ctx_scope;
6857 endlabel_T **el;
6858
6859 for (;;)
6860 {
6861 if (scope == NULL)
6862 {
6863 emsg(_(e_break));
6864 return NULL;
6865 }
6866 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6867 break;
6868 scope = scope->se_outer;
6869 }
6870
6871 // Jump to the end of the FOR or WHILE loop.
6872 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006873 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006874 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006875 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006876 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6877 return FAIL;
6878
6879 return arg;
6880}
6881
6882/*
6883 * compile "{" start of block
6884 */
6885 static char_u *
6886compile_block(char_u *arg, cctx_T *cctx)
6887{
6888 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6889 return NULL;
6890 return skipwhite(arg + 1);
6891}
6892
6893/*
6894 * compile end of block: drop one scope
6895 */
6896 static void
6897compile_endblock(cctx_T *cctx)
6898{
6899 scope_T *scope = cctx->ctx_scope;
6900
6901 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006902 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006903 vim_free(scope);
6904}
6905
6906/*
6907 * compile "try"
6908 * Creates a new scope for the try-endtry, pointing to the first catch and
6909 * finally.
6910 * Creates another scope for the "try" block itself.
6911 * TRY instruction sets up exception handling at runtime.
6912 *
6913 * "try"
6914 * TRY -> catch1, -> finally push trystack entry
6915 * ... try block
6916 * "throw {exception}"
6917 * EVAL {exception}
6918 * THROW create exception
6919 * ... try block
6920 * " catch {expr}"
6921 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006922 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006923 * EVAL {expr}
6924 * MATCH
6925 * JUMP nomatch -> catch2
6926 * CATCH remove exception
6927 * ... catch block
6928 * " catch"
6929 * JUMP -> finally
6930 * catch2: CATCH remove exception
6931 * ... catch block
6932 * " finally"
6933 * finally:
6934 * ... finally block
6935 * " endtry"
6936 * ENDTRY pop trystack entry, may rethrow
6937 */
6938 static char_u *
6939compile_try(char_u *arg, cctx_T *cctx)
6940{
6941 garray_T *instr = &cctx->ctx_instr;
6942 scope_T *try_scope;
6943 scope_T *scope;
6944
6945 // scope that holds the jumps that go to catch/finally/endtry
6946 try_scope = new_scope(cctx, TRY_SCOPE);
6947 if (try_scope == NULL)
6948 return NULL;
6949
6950 // "catch" is set when the first ":catch" is found.
6951 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006952 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006953 if (generate_instr(cctx, ISN_TRY) == NULL)
6954 return NULL;
6955
6956 // scope for the try block itself
6957 scope = new_scope(cctx, BLOCK_SCOPE);
6958 if (scope == NULL)
6959 return NULL;
6960
6961 return arg;
6962}
6963
6964/*
6965 * compile "catch {expr}"
6966 */
6967 static char_u *
6968compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6969{
6970 scope_T *scope = cctx->ctx_scope;
6971 garray_T *instr = &cctx->ctx_instr;
6972 char_u *p;
6973 isn_T *isn;
6974
6975 // end block scope from :try or :catch
6976 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6977 compile_endblock(cctx);
6978 scope = cctx->ctx_scope;
6979
6980 // Error if not in a :try scope
6981 if (scope == NULL || scope->se_type != TRY_SCOPE)
6982 {
6983 emsg(_(e_catch));
6984 return NULL;
6985 }
6986
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006987 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006988 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006989 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006990 return NULL;
6991 }
6992
6993 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006994 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006995 JUMP_ALWAYS, cctx) == FAIL)
6996 return NULL;
6997
6998 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006999 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007000 if (isn->isn_arg.try.try_catch == 0)
7001 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007002 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007003 {
7004 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007005 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007006 isn->isn_arg.jump.jump_where = instr->ga_len;
7007 }
7008
7009 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007010 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007011 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007012 scope->se_u.se_try.ts_caught_all = TRUE;
7013 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007014 }
7015 else
7016 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007017 char_u *end;
7018 char_u *pat;
7019 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007020 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007021 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007022
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007023 // Push v:exception, push {expr} and MATCH
7024 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7025
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007026 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007027 if (*end != *p)
7028 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007029 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007030 vim_free(tofree);
7031 return FAIL;
7032 }
7033 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007034 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007035 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007036 len = (int)(end - tofree);
7037 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007038 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007039 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007040 if (pat == NULL)
7041 return FAIL;
7042 if (generate_PUSHS(cctx, pat) == FAIL)
7043 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007044
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007045 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7046 return NULL;
7047
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007048 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007049 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7050 return NULL;
7051 }
7052
7053 if (generate_instr(cctx, ISN_CATCH) == NULL)
7054 return NULL;
7055
7056 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7057 return NULL;
7058 return p;
7059}
7060
7061 static char_u *
7062compile_finally(char_u *arg, cctx_T *cctx)
7063{
7064 scope_T *scope = cctx->ctx_scope;
7065 garray_T *instr = &cctx->ctx_instr;
7066 isn_T *isn;
7067
7068 // end block scope from :try or :catch
7069 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7070 compile_endblock(cctx);
7071 scope = cctx->ctx_scope;
7072
7073 // Error if not in a :try scope
7074 if (scope == NULL || scope->se_type != TRY_SCOPE)
7075 {
7076 emsg(_(e_finally));
7077 return NULL;
7078 }
7079
7080 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007081 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007082 if (isn->isn_arg.try.try_finally != 0)
7083 {
7084 emsg(_(e_finally_dup));
7085 return NULL;
7086 }
7087
7088 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007089 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007090
Bram Moolenaar585fea72020-04-02 22:33:21 +02007091 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007092 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007093 {
7094 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007095 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007096 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007097 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007098 }
7099
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007100 // TODO: set index in ts_finally_label jumps
7101
7102 return arg;
7103}
7104
7105 static char_u *
7106compile_endtry(char_u *arg, cctx_T *cctx)
7107{
7108 scope_T *scope = cctx->ctx_scope;
7109 garray_T *instr = &cctx->ctx_instr;
7110 isn_T *isn;
7111
7112 // end block scope from :catch or :finally
7113 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7114 compile_endblock(cctx);
7115 scope = cctx->ctx_scope;
7116
7117 // Error if not in a :try scope
7118 if (scope == NULL || scope->se_type != TRY_SCOPE)
7119 {
7120 if (scope == NULL)
7121 emsg(_(e_no_endtry));
7122 else if (scope->se_type == WHILE_SCOPE)
7123 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007124 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007125 emsg(_(e_endfor));
7126 else
7127 emsg(_(e_endif));
7128 return NULL;
7129 }
7130
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007131 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007132 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
7133 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007134 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007135 return NULL;
7136 }
7137
7138 // Fill in the "end" label in jumps at the end of the blocks, if not done
7139 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007140 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007141
7142 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02007143 if (isn->isn_arg.try.try_catch == 0)
7144 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007145 if (isn->isn_arg.try.try_finally == 0)
7146 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007147
7148 if (scope->se_u.se_try.ts_catch_label != 0)
7149 {
7150 // Last catch without match jumps here
7151 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7152 isn->isn_arg.jump.jump_where = instr->ga_len;
7153 }
7154
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007155 compile_endblock(cctx);
7156
7157 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
7158 return NULL;
7159 return arg;
7160}
7161
7162/*
7163 * compile "throw {expr}"
7164 */
7165 static char_u *
7166compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7167{
7168 char_u *p = skipwhite(arg);
7169
Bram Moolenaara5565e42020-05-09 15:44:01 +02007170 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007171 return NULL;
7172 if (may_generate_2STRING(-1, cctx) == FAIL)
7173 return NULL;
7174 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7175 return NULL;
7176
7177 return p;
7178}
7179
7180/*
7181 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007182 * compile "echomsg expr"
7183 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007184 * compile "execute expr"
7185 */
7186 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007187compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007188{
7189 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007190 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007191 int count = 0;
7192
7193 for (;;)
7194 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007195 if (ends_excmd2(prev, p))
7196 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007197 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007198 return NULL;
7199 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007200 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007201 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007202 }
7203
Bram Moolenaare4984292020-12-13 14:19:25 +01007204 if (count > 0)
7205 {
7206 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7207 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7208 else if (cmdidx == CMD_execute)
7209 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7210 else if (cmdidx == CMD_echomsg)
7211 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7212 else
7213 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7214 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007215 return p;
7216}
7217
7218/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007219 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01007220 * instruction to compute it and return OK.
7221 * Otherwise return FAIL, the caller must deal with any range.
7222 */
7223 static int
7224compile_variable_range(exarg_T *eap, cctx_T *cctx)
7225{
7226 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7227 char_u *p = skipdigits(eap->cmd);
7228
7229 if (p == range_end)
7230 return FAIL;
7231 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7232}
7233
7234/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007235 * :put r
7236 * :put ={expr}
7237 */
7238 static char_u *
7239compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7240{
7241 char_u *line = arg;
7242 linenr_T lnum;
7243 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007244 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007245
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007246 eap->regname = *line;
7247
7248 if (eap->regname == '=')
7249 {
7250 char_u *p = line + 1;
7251
7252 if (compile_expr0(&p, cctx) == FAIL)
7253 return NULL;
7254 line = p;
7255 }
7256 else if (eap->regname != NUL)
7257 ++line;
7258
Bram Moolenaar08597872020-12-10 19:43:40 +01007259 if (compile_variable_range(eap, cctx) == OK)
7260 {
7261 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
7262 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007263 else
Bram Moolenaar08597872020-12-10 19:43:40 +01007264 {
7265 // Either no range or a number.
7266 // "errormsg" will not be set because the range is ADDR_LINES.
7267 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01007268 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01007269 return NULL;
7270 if (eap->addr_count == 0)
7271 lnum = -1;
7272 else
7273 lnum = eap->line2;
7274 if (above)
7275 --lnum;
7276 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007277
7278 generate_PUT(cctx, eap->regname, lnum);
7279 return line;
7280}
7281
7282/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007283 * A command that is not compiled, execute with legacy code.
7284 */
7285 static char_u *
7286compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7287{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007288 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007289 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007290 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007291
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007292 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007293 goto theend;
7294
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007295 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007296 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007297 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007298 int usefilter = FALSE;
7299
7300 has_expr = argt & (EX_XFILE | EX_EXPAND);
7301
7302 // If the command can be followed by a bar, find the bar and truncate
7303 // it, so that the following command can be compiled.
7304 // The '|' is overwritten with a NUL, it is put back below.
7305 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7306 && *eap->arg == '!')
7307 // :w !filter or :r !filter or :r! filter
7308 usefilter = TRUE;
7309 if ((argt & EX_TRLBAR) && !usefilter)
7310 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007311 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007312 separate_nextcmd(eap);
7313 if (eap->nextcmd != NULL)
7314 nextcmd = eap->nextcmd;
7315 }
7316 }
7317
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007318 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
7319 {
7320 // expand filename in "syntax include [@group] filename"
7321 has_expr = TRUE;
7322 eap->arg = skipwhite(eap->arg + 7);
7323 if (*eap->arg == '@')
7324 eap->arg = skiptowhite(eap->arg);
7325 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007326
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007327 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007328 {
7329 int count = 0;
7330 char_u *start = skipwhite(line);
7331
7332 // :cmd xxx`=expr1`yyy`=expr2`zzz
7333 // PUSHS ":cmd xxx"
7334 // eval expr1
7335 // PUSHS "yyy"
7336 // eval expr2
7337 // PUSHS "zzz"
7338 // EXECCONCAT 5
7339 for (;;)
7340 {
7341 if (p > start)
7342 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007343 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007344 ++count;
7345 }
7346 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007347 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007348 return NULL;
7349 may_generate_2STRING(-1, cctx);
7350 ++count;
7351 p = skipwhite(p);
7352 if (*p != '`')
7353 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007354 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007355 return NULL;
7356 }
7357 start = p + 1;
7358
7359 p = (char_u *)strstr((char *)start, "`=");
7360 if (p == NULL)
7361 {
7362 if (*skipwhite(start) != NUL)
7363 {
7364 generate_PUSHS(cctx, vim_strsave(start));
7365 ++count;
7366 }
7367 break;
7368 }
7369 }
7370 generate_EXECCONCAT(cctx, count);
7371 }
7372 else
7373 generate_EXEC(cctx, line);
7374
7375theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007376 if (*nextcmd != NUL)
7377 {
7378 // the parser expects a pointer to the bar, put it back
7379 --nextcmd;
7380 *nextcmd = '|';
7381 }
7382
7383 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007384}
7385
7386/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007387 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007388 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007389 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007390 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007391add_def_function(ufunc_T *ufunc)
7392{
7393 dfunc_T *dfunc;
7394
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007395 if (def_functions.ga_len == 0)
7396 {
7397 // The first position is not used, so that a zero uf_dfunc_idx means it
7398 // wasn't set.
7399 if (ga_grow(&def_functions, 1) == FAIL)
7400 return FAIL;
7401 ++def_functions.ga_len;
7402 }
7403
Bram Moolenaar09689a02020-05-09 22:50:08 +02007404 // Add the function to "def_functions".
7405 if (ga_grow(&def_functions, 1) == FAIL)
7406 return FAIL;
7407 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7408 CLEAR_POINTER(dfunc);
7409 dfunc->df_idx = def_functions.ga_len;
7410 ufunc->uf_dfunc_idx = dfunc->df_idx;
7411 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01007412 dfunc->df_name = vim_strsave(ufunc->uf_name);
7413 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007414 ++def_functions.ga_len;
7415 return OK;
7416}
7417
7418/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007419 * After ex_function() has collected all the function lines: parse and compile
7420 * the lines into instructions.
7421 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007422 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
7423 * the return statement (used for lambda). When uf_ret_type is already set
7424 * then check that it matches.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007425 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007426 * This can be used recursively through compile_lambda(), which may reallocate
7427 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007428 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007429 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007430 int
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007431compile_def_function(ufunc_T *ufunc, int check_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007432{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007433 char_u *line = NULL;
7434 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007435 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007436 cctx_T cctx;
7437 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007438 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007439 int ret = FAIL;
7440 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007441 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007442 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007443 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007444
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007445 // When using a function that was compiled before: Free old instructions.
7446 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007447 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007448 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007449 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7450 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007451 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007452 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007453 else
7454 {
7455 if (add_def_function(ufunc) == FAIL)
7456 return FAIL;
7457 new_def_function = TRUE;
7458 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007459
Bram Moolenaar985116a2020-07-12 17:31:09 +02007460 ufunc->uf_def_status = UF_COMPILING;
7461
Bram Moolenaara80faa82020-04-12 19:37:17 +02007462 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007463 cctx.ctx_ufunc = ufunc;
7464 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007465 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007466 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7467 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7468 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7469 cctx.ctx_type_list = &ufunc->uf_type_list;
7470 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7471 instr = &cctx.ctx_instr;
7472
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007473 // Set the context to the function, it may be compiled when called from
7474 // another script. Set the script version to the most modern one.
7475 // The line number will be set in next_line_from_context().
7476 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007477 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7478
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007479 // Make sure error messages are OK.
7480 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7481 if (do_estack_push)
7482 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007483 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007484
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007485 if (ufunc->uf_def_args.ga_len > 0)
7486 {
7487 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007488 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007489 int i;
7490 char_u *arg;
7491 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007492 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007493
7494 // Produce instructions for the default values of optional arguments.
7495 // Store the instruction index in uf_def_arg_idx[] so that we know
7496 // where to start when the function is called, depending on the number
7497 // of arguments.
7498 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7499 if (ufunc->uf_def_arg_idx == NULL)
7500 goto erret;
7501 for (i = 0; i < count; ++i)
7502 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007503 garray_T *stack = &cctx.ctx_type_stack;
7504 type_T *val_type;
7505 int arg_idx = first_def_arg + i;
7506
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007507 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7508 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007509 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007510 goto erret;
7511
7512 // If no type specified use the type of the default value.
7513 // Otherwise check that the default value type matches the
7514 // specified type.
7515 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7516 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007517 {
7518 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007519 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007520 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007521 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7522 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007523 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007524
7525 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007526 goto erret;
7527 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007528 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007529
7530 if (did_set_arg_type)
7531 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007532 }
7533
7534 /*
7535 * Loop over all the lines of the function and generate instructions.
7536 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007537 for (;;)
7538 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007539 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007540 int starts_with_colon = FALSE;
7541 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007542 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007543
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007544 // Bail out on the first error to avoid a flood of errors and report
7545 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007546 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007547 goto erret;
7548
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007549 if (line != NULL && *line == '|')
7550 // the line continues after a '|'
7551 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007552 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007553 && !(*line == '#' && (line == cctx.ctx_line_start
7554 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007555 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007556 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007557 goto erret;
7558 }
7559 else
7560 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007561 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007562 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007563 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007564 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007565 }
7566
Bram Moolenaara80faa82020-04-12 19:37:17 +02007567 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007568 ea.cmdlinep = &line;
7569 ea.cmd = skipwhite(line);
7570
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007571 // Some things can be recognized by the first character.
7572 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007573 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007574 case '#':
Bram Moolenaare0de1712020-12-02 17:36:54 +01007575 // "#" starts a comment
7576 line = (char_u *)"";
7577 continue;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007578
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007579 case '}':
7580 {
7581 // "}" ends a block scope
7582 scopetype_T stype = cctx.ctx_scope == NULL
7583 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007584
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007585 if (stype == BLOCK_SCOPE)
7586 {
7587 compile_endblock(&cctx);
7588 line = ea.cmd;
7589 }
7590 else
7591 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007592 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007593 goto erret;
7594 }
7595 if (line != NULL)
7596 line = skipwhite(ea.cmd + 1);
7597 continue;
7598 }
7599
7600 case '{':
7601 // "{" starts a block scope
7602 // "{'a': 1}->func() is something else
7603 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7604 {
7605 line = compile_block(ea.cmd, &cctx);
7606 continue;
7607 }
7608 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007609 }
7610
7611 /*
7612 * COMMAND MODIFIERS
7613 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007614 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007615 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7616 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007617 {
7618 if (errormsg != NULL)
7619 goto erret;
7620 // empty line or comment
7621 line = (char_u *)"";
7622 continue;
7623 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007624 generate_cmdmods(&cctx, &local_cmdmod);
7625 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007626
Bram Moolenaare88c8e82020-11-01 17:03:37 +01007627 // Check if there was a colon after the last command modifier or before
7628 // the current position.
7629 for (p = ea.cmd; p >= line; --p)
7630 {
7631 if (*p == ':')
7632 starts_with_colon = TRUE;
7633 if (p < ea.cmd && !VIM_ISWHITE(*p))
7634 break;
7635 }
7636
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007637 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007638 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007639 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007640 {
7641 if (*ea.cmd == '(')
7642 // not for "call()"
7643 ea.cmd = p;
7644 else
7645 ea.cmd = skipwhite(ea.cmd);
7646 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007647
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007648 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007649 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007650 char_u *pskip;
7651
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007652 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007653 // find what follows.
7654 // Skip over "var.member", "var[idx]" and the like.
7655 // Also "&opt = val", "$ENV = val" and "@r = val".
7656 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007657 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007658 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007659 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007660 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007661 char_u *var_end;
7662 int oplen;
7663 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007664
Bram Moolenaar65821722020-08-02 18:58:54 +02007665 if (ea.cmd[0] == '@')
7666 var_end = ea.cmd + 2;
7667 else
7668 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007669 FNE_CHECK_START | FNE_INCL_BR);
7670 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007671 if (oplen > 0)
7672 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007673 size_t len = p - ea.cmd;
7674
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007675 // Recognize an assignment if we recognize the variable
7676 // name:
7677 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007678 // "local = expr" where "local" is a local var.
7679 // "script = expr" where "script" is a script-local var.
7680 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007681 // "&opt = expr"
7682 // "$ENV = expr"
7683 // "@r = expr"
7684 if (*ea.cmd == '&'
7685 || *ea.cmd == '$'
7686 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007687 || ((len) > 2 && ea.cmd[1] == ':')
Bram Moolenaar709664c2020-12-12 14:33:41 +01007688 || lookup_local(ea.cmd, len, NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007689 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007690 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007691 || script_var_exists(ea.cmd, len,
7692 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007693 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007694 {
7695 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007696 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007697 goto erret;
Bram Moolenaarf665e972020-12-05 19:17:16 +01007698 goto nextline;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007699 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007700 }
7701 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007702
7703 if (*ea.cmd == '[')
7704 {
7705 // [var, var] = expr
7706 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7707 if (line == NULL)
7708 goto erret;
7709 if (line != ea.cmd)
Bram Moolenaarf665e972020-12-05 19:17:16 +01007710 goto nextline;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007711 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007712 }
7713
7714 /*
7715 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007716 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007717 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007718 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007719 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007720 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007721 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007722 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007723 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007724 if (!starts_with_colon)
7725 {
7726 emsg(_(e_colon_required_before_a_range));
7727 goto erret;
7728 }
7729 if (ends_excmd2(line, ea.cmd))
7730 {
7731 // A range without a command: jump to the line.
7732 // TODO: compile to a more efficient command, possibly
7733 // calling parse_cmd_address().
7734 ea.cmdidx = CMD_SIZE;
7735 line = compile_exec(line, &ea, &cctx);
7736 goto nextline;
7737 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007738 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007739 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007740 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +01007741 : (int (*)(char_u *, size_t, void *, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007742 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007743
7744 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7745 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007746 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007747 {
7748 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01007749 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007750 }
7751
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007752 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007753 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007754 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01007755 // CMD_var cannot happen, compile_assignment() above would be
7756 // used. Most likely an assignment to a non-existing variable.
7757 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007758 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007759 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007760 }
7761
Bram Moolenaar3988f642020-08-27 22:43:03 +02007762 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007763 && ea.cmdidx != CMD_elseif
7764 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007765 && ea.cmdidx != CMD_endif
7766 && ea.cmdidx != CMD_endfor
7767 && ea.cmdidx != CMD_endwhile
7768 && ea.cmdidx != CMD_catch
7769 && ea.cmdidx != CMD_finally
7770 && ea.cmdidx != CMD_endtry)
7771 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007772 emsg(_(e_unreachable_code_after_return));
7773 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007774 }
7775
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007776 p = skipwhite(p);
7777 if (ea.cmdidx != CMD_SIZE
7778 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7779 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007780 if (ea.cmdidx >= 0)
7781 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007782 if ((ea.argt & EX_BANG) && *p == '!')
7783 {
7784 ea.forceit = TRUE;
7785 p = skipwhite(p + 1);
7786 }
7787 }
7788
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007789 switch (ea.cmdidx)
7790 {
7791 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007792 ea.arg = p;
7793 line = compile_nested_function(&ea, &cctx);
7794 break;
7795
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007796 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007797 // TODO: should we allow this, e.g. to declare a global
7798 // function?
7799 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007800 goto erret;
7801
7802 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007803 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007804 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007805 break;
7806
7807 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02007808 emsg(_(e_cannot_use_let_in_vim9_script));
7809 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007810 case CMD_var:
7811 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007812 case CMD_const:
7813 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007814 if (line == p)
7815 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007816 break;
7817
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007818 case CMD_unlet:
7819 case CMD_unlockvar:
7820 case CMD_lockvar:
7821 line = compile_unletlock(p, &ea, &cctx);
7822 break;
7823
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007824 case CMD_import:
7825 line = compile_import(p, &cctx);
7826 break;
7827
7828 case CMD_if:
7829 line = compile_if(p, &cctx);
7830 break;
7831 case CMD_elseif:
7832 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007833 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007834 break;
7835 case CMD_else:
7836 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007837 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007838 break;
7839 case CMD_endif:
7840 line = compile_endif(p, &cctx);
7841 break;
7842
7843 case CMD_while:
7844 line = compile_while(p, &cctx);
7845 break;
7846 case CMD_endwhile:
7847 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007848 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007849 break;
7850
7851 case CMD_for:
7852 line = compile_for(p, &cctx);
7853 break;
7854 case CMD_endfor:
7855 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007856 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007857 break;
7858 case CMD_continue:
7859 line = compile_continue(p, &cctx);
7860 break;
7861 case CMD_break:
7862 line = compile_break(p, &cctx);
7863 break;
7864
7865 case CMD_try:
7866 line = compile_try(p, &cctx);
7867 break;
7868 case CMD_catch:
7869 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007870 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007871 break;
7872 case CMD_finally:
7873 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007874 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007875 break;
7876 case CMD_endtry:
7877 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007878 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007879 break;
7880 case CMD_throw:
7881 line = compile_throw(p, &cctx);
7882 break;
7883
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007884 case CMD_eval:
7885 if (compile_expr0(&p, &cctx) == FAIL)
7886 goto erret;
7887
Bram Moolenaar3988f642020-08-27 22:43:03 +02007888 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007889 generate_instr_drop(&cctx, ISN_DROP, 1);
7890
7891 line = skipwhite(p);
7892 break;
7893
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007894 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007895 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007896 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007897 case CMD_echomsg:
7898 case CMD_echoerr:
7899 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007900 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007901
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007902 case CMD_put:
7903 ea.cmd = cmd;
7904 line = compile_put(p, &ea, &cctx);
7905 break;
7906
Bram Moolenaar3988f642020-08-27 22:43:03 +02007907 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007908
Bram Moolenaarae616492020-07-28 20:07:27 +02007909 case CMD_append:
7910 case CMD_change:
7911 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007912 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007913 case CMD_xit:
7914 not_in_vim9(&ea);
7915 goto erret;
7916
Bram Moolenaar002262f2020-07-08 17:47:57 +02007917 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007918 if (cctx.ctx_skip != SKIP_YES)
7919 {
7920 semsg(_(e_invalid_command_str), ea.cmd);
7921 goto erret;
7922 }
7923 // We don't check for a next command here.
7924 line = (char_u *)"";
7925 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007926
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007927 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007928 if (cctx.ctx_skip == SKIP_YES)
7929 {
7930 // We don't check for a next command here.
7931 line = (char_u *)"";
7932 }
7933 else
7934 {
7935 // Not recognized, execute with do_cmdline_cmd().
7936 ea.arg = p;
7937 line = compile_exec(line, &ea, &cctx);
7938 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007939 break;
7940 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007941nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007942 if (line == NULL)
7943 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007944 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007945
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007946 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02007947 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007948
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007949 if (cctx.ctx_type_stack.ga_len < 0)
7950 {
7951 iemsg("Type stack underflow");
7952 goto erret;
7953 }
7954 }
7955
7956 if (cctx.ctx_scope != NULL)
7957 {
7958 if (cctx.ctx_scope->se_type == IF_SCOPE)
7959 emsg(_(e_endif));
7960 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7961 emsg(_(e_endwhile));
7962 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7963 emsg(_(e_endfor));
7964 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007965 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007966 goto erret;
7967 }
7968
Bram Moolenaarefd88552020-06-18 20:50:10 +02007969 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007970 {
7971 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7972 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007973 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007974 goto erret;
7975 }
7976
7977 // Return zero if there is no return at the end.
7978 generate_PUSHNR(&cctx, 0);
7979 generate_instr(&cctx, ISN_RETURN);
7980 }
7981
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007982 {
7983 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7984 + ufunc->uf_dfunc_idx;
7985 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01007986 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007987 dfunc->df_instr = instr->ga_data;
7988 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007989 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007990 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007991 if (cctx.ctx_outer_used)
7992 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007993 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007994 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007995
7996 ret = OK;
7997
7998erret:
7999 if (ret == FAIL)
8000 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008001 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008002 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8003 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008004
8005 for (idx = 0; idx < instr->ga_len; ++idx)
8006 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008007 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008008 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008009
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008010 // If using the last entry in the table and it was added above, we
8011 // might as well remove it.
8012 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008013 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008014 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008015 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008016 ufunc->uf_dfunc_idx = 0;
8017 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008018 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008019
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008020 while (cctx.ctx_scope != NULL)
8021 drop_scope(&cctx);
8022
Bram Moolenaar20431c92020-03-20 18:39:46 +01008023 // Don't execute this function body.
8024 ga_clear_strings(&ufunc->uf_lines);
8025
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008026 if (errormsg != NULL)
8027 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008028 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008029 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008030 }
8031
8032 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008033 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008034 if (do_estack_push)
8035 estack_pop();
8036
Bram Moolenaar20431c92020-03-20 18:39:46 +01008037 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008038 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008039 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008040 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008041}
8042
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008043 void
8044set_function_type(ufunc_T *ufunc)
8045{
8046 int varargs = ufunc->uf_va_name != NULL;
8047 int argcount = ufunc->uf_args.ga_len;
8048
8049 // Create a type for the function, with the return type and any
8050 // argument types.
8051 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8052 // The type is included in "tt_args".
8053 if (argcount > 0 || varargs)
8054 {
8055 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8056 argcount, &ufunc->uf_type_list);
8057 // Add argument types to the function type.
8058 if (func_type_add_arg_types(ufunc->uf_func_type,
8059 argcount + varargs,
8060 &ufunc->uf_type_list) == FAIL)
8061 return;
8062 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8063 ufunc->uf_func_type->tt_min_argcount =
8064 argcount - ufunc->uf_def_args.ga_len;
8065 if (ufunc->uf_arg_types == NULL)
8066 {
8067 int i;
8068
8069 // lambda does not have argument types.
8070 for (i = 0; i < argcount; ++i)
8071 ufunc->uf_func_type->tt_args[i] = &t_any;
8072 }
8073 else
8074 mch_memmove(ufunc->uf_func_type->tt_args,
8075 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8076 if (varargs)
8077 {
8078 ufunc->uf_func_type->tt_args[argcount] =
8079 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8080 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8081 }
8082 }
8083 else
8084 // No arguments, can use a predefined type.
8085 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8086 argcount, &ufunc->uf_type_list);
8087}
8088
8089
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008090/*
8091 * Delete an instruction, free what it contains.
8092 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008093 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008094delete_instr(isn_T *isn)
8095{
8096 switch (isn->isn_type)
8097 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008098 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008099 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008100 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008101 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008102 case ISN_LOADENV:
8103 case ISN_LOADG:
8104 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008105 case ISN_LOADT:
8106 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008107 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008108 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008109 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008110 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008111 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008112 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008113 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008114 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008115 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008116 case ISN_STOREW:
8117 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008118 vim_free(isn->isn_arg.string);
8119 break;
8120
8121 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008122 case ISN_STORES:
8123 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008124 break;
8125
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008126 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008127 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008128 vim_free(isn->isn_arg.unlet.ul_name);
8129 break;
8130
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008131 case ISN_STOREOPT:
8132 vim_free(isn->isn_arg.storeopt.so_name);
8133 break;
8134
8135 case ISN_PUSHBLOB: // push blob isn_arg.blob
8136 blob_unref(isn->isn_arg.blob);
8137 break;
8138
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008139 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008140#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008141 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008142#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008143 break;
8144
8145 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008146#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008147 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008148#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008149 break;
8150
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008151 case ISN_UCALL:
8152 vim_free(isn->isn_arg.ufunc.cuf_name);
8153 break;
8154
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008155 case ISN_FUNCREF:
8156 {
8157 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8158 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01008159 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008160
Bram Moolenaar077a4232020-12-22 18:33:27 +01008161 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
8162 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02008163 }
8164 break;
8165
8166 case ISN_DCALL:
8167 {
8168 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8169 + isn->isn_arg.dfunc.cdf_idx;
8170
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008171 if (dfunc->df_ufunc != NULL
8172 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008173 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008174 }
8175 break;
8176
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008177 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008178 {
8179 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8180 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8181
8182 if (ufunc != NULL)
8183 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008184 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02008185 func_ptr_unref(ufunc);
8186 }
8187
8188 vim_free(lambda);
8189 vim_free(isn->isn_arg.newfunc.nf_global);
8190 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008191 break;
8192
Bram Moolenaar5e654232020-09-16 15:22:00 +02008193 case ISN_CHECKTYPE:
8194 free_type(isn->isn_arg.type.ct_type);
8195 break;
8196
Bram Moolenaar02194d22020-10-24 23:08:38 +02008197 case ISN_CMDMOD:
8198 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8199 ->cmod_filter_regmatch.regprog);
8200 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8201 break;
8202
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008203 case ISN_LOADSCRIPT:
8204 case ISN_STORESCRIPT:
8205 vim_free(isn->isn_arg.script.scriptref);
8206 break;
8207
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008208 case ISN_2BOOL:
8209 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008210 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008211 case ISN_ADDBLOB:
8212 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008213 case ISN_ANYINDEX:
8214 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008215 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008216 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008217 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008218 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008219 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008220 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008221 case ISN_COMPAREANY:
8222 case ISN_COMPAREBLOB:
8223 case ISN_COMPAREBOOL:
8224 case ISN_COMPAREDICT:
8225 case ISN_COMPAREFLOAT:
8226 case ISN_COMPAREFUNC:
8227 case ISN_COMPARELIST:
8228 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008229 case ISN_COMPARESPECIAL:
8230 case ISN_COMPARESTRING:
8231 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008232 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008233 case ISN_DROP:
8234 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008235 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008236 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008237 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008238 case ISN_EXECCONCAT:
8239 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008240 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008241 case ISN_GETITEM:
8242 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008243 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008244 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008245 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008246 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008247 case ISN_LOADBDICT:
8248 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008249 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008250 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008251 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008252 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008253 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008254 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008255 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008256 case ISN_NEGATENR:
8257 case ISN_NEWDICT:
8258 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008259 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008260 case ISN_OPFLOAT:
8261 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008262 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008263 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008264 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008265 case ISN_PUSHF:
8266 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008267 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008268 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008269 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008270 case ISN_SHUFFLE:
8271 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008272 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01008273 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008274 case ISN_STORENR:
8275 case ISN_STOREOUTER:
8276 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008277 case ISN_STOREV:
8278 case ISN_STRINDEX:
8279 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008280 case ISN_THROW:
8281 case ISN_TRY:
Bram Moolenaar792f7862020-11-23 08:31:18 +01008282 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008283 // nothing allocated
8284 break;
8285 }
8286}
8287
8288/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008289 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01008290 */
8291 static void
8292delete_def_function_contents(dfunc_T *dfunc)
8293{
8294 int idx;
8295
8296 ga_clear(&dfunc->df_def_args_isn);
8297
8298 if (dfunc->df_instr != NULL)
8299 {
8300 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
8301 delete_instr(dfunc->df_instr + idx);
8302 VIM_CLEAR(dfunc->df_instr);
8303 }
8304
8305 dfunc->df_deleted = TRUE;
8306}
8307
8308/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008309 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008310 * function, unless another user function still uses it.
8311 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008312 */
8313 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008314unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008315{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008316 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008317 {
8318 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8319 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008320
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008321 if (--dfunc->df_refcount <= 0)
8322 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008323 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008324 ufunc->uf_dfunc_idx = 0;
8325 if (dfunc->df_ufunc == ufunc)
8326 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008327 }
8328}
8329
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008330/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008331 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008332 */
8333 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008334link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008335{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008336 if (ufunc->uf_dfunc_idx > 0)
8337 {
8338 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8339 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008340
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008341 ++dfunc->df_refcount;
8342 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008343}
8344
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008345#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008346/*
8347 * Free all functions defined with ":def".
8348 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008349 void
8350free_def_functions(void)
8351{
Bram Moolenaar20431c92020-03-20 18:39:46 +01008352 int idx;
8353
8354 for (idx = 0; idx < def_functions.ga_len; ++idx)
8355 {
8356 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
8357
8358 delete_def_function_contents(dfunc);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008359 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008360 }
8361
8362 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008363}
8364#endif
8365
8366
8367#endif // FEAT_EVAL