blob: e8a3a21dbd27c889800c29fa658bdb16f260e5a2 [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 Moolenaarbf67ea12020-05-02 17:52:42 +0200129 int ctx_closure_count; // number of closures created in the
130 // function
131
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 Moolenaar8a7d6542020-01-26 15:56:19 +0100144};
145
Bram Moolenaar20431c92020-03-20 18:39:46 +0100146static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100147
148/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200149 * Lookup variable "name" in the local scope and return it.
150 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200152 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153lookup_local(char_u *name, size_t len, cctx_T *cctx)
154{
155 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200156 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100158 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200159 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200160
161 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
163 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200164 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100165 if (STRNCMP(name, lvar->lv_name, len) == 0
166 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167 {
168 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200169 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200170 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200172
173 // Find local in outer function scope.
174 if (cctx->ctx_outer != NULL)
175 {
176 lvar = lookup_local(name, len, cctx->ctx_outer);
177 if (lvar != NULL)
178 {
179 // TODO: are there situations we should not mark the outer scope as
180 // used?
181 cctx->ctx_outer_used = TRUE;
182 lvar->lv_from_outer = TRUE;
183 return lvar;
184 }
185 }
186
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200187 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100188}
189
190/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200191 * Lookup an argument in the current function and an enclosing function.
192 * Returns the argument index in "idxp"
193 * Returns the argument type in "type"
194 * Sets "gen_load_outer" to TRUE if found in outer scope.
195 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196 */
197 static int
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200198lookup_arg(
199 char_u *name,
200 size_t len,
201 int *idxp,
202 type_T **type,
203 int *gen_load_outer,
204 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100205{
206 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200207 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100209 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200210 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
212 {
213 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
214
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200215 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
216 {
217 if (idxp != NULL)
218 {
219 // Arguments are located above the frame pointer. One further
220 // if there is a vararg argument
221 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
222 + STACK_FRAME_SIZE)
223 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
224
225 if (cctx->ctx_ufunc->uf_arg_types != NULL)
226 *type = cctx->ctx_ufunc->uf_arg_types[idx];
227 else
228 *type = &t_any;
229 }
230 return OK;
231 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100232 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100233
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200234 va_name = cctx->ctx_ufunc->uf_va_name;
235 if (va_name != NULL
236 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
237 {
238 if (idxp != NULL)
239 {
240 // varargs is always the last argument
241 *idxp = -STACK_FRAME_SIZE - 1;
242 *type = cctx->ctx_ufunc->uf_va_type;
243 }
244 return OK;
245 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200247 if (cctx->ctx_outer != NULL)
248 {
249 // Lookup the name for an argument of the outer function.
250 if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
251 == OK)
252 {
253 *gen_load_outer = TRUE;
254 return OK;
255 }
256 }
257
258 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100259}
260
261/*
Bram Moolenaar84367732020-08-23 15:21:55 +0200262 * Returnd TRUE if the script context is Vim9 script.
263 */
264 static int
265script_is_vim9()
266{
267 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
268}
269
270/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 * Lookup a variable in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200272 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
273 * without "s:".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274 * Returns OK or FAIL.
275 */
276 static int
Bram Moolenaar53900992020-08-22 19:02:02 +0200277lookup_script(char_u *name, size_t len, int vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278{
279 int cc;
280 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
281 dictitem_T *di;
282
Bram Moolenaar84367732020-08-23 15:21:55 +0200283 if (vim9script && !script_is_vim9())
Bram Moolenaar53900992020-08-22 19:02:02 +0200284 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100285 cc = name[len];
286 name[len] = NUL;
287 di = find_var_in_ht(ht, 0, name, TRUE);
288 name[len] = cc;
289 return di == NULL ? FAIL: OK;
290}
291
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100292/*
293 * Check if "p[len]" is already defined, either in script "import_sid" or in
294 * compilation context "cctx".
Bram Moolenaar0f769812020-09-12 18:32:34 +0200295 * Does not check the global namespace.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100296 * Return FAIL and give an error if it defined.
297 */
298 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200299check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100300{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200301 int c = p[len];
302 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200303
304 p[len] = NUL;
Bram Moolenaar53900992020-08-22 19:02:02 +0200305 if (lookup_script(p, len, FALSE) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100306 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200307 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaareef21022020-08-01 22:16:43 +0200308 || lookup_arg(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200309 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200310 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100311 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200312 // A local or script-local function can shadow a global function.
313 if (ufunc == NULL || !func_is_global(ufunc)
314 || (p[0] == 'g' && p[1] == ':'))
315 {
316 p[len] = c;
317 semsg(_(e_name_already_defined_str), p);
318 return FAIL;
319 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100320 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200321 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100322 return OK;
323}
324
Bram Moolenaar65b95452020-07-19 14:03:09 +0200325
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100326/////////////////////////////////////////////////////////////////////
327// Following generate_ functions expect the caller to call ga_grow().
328
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200329#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
330#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100332/*
333 * Generate an instruction without arguments.
334 * Returns a pointer to the new instruction, NULL if failed.
335 */
336 static isn_T *
337generate_instr(cctx_T *cctx, isntype_T isn_type)
338{
339 garray_T *instr = &cctx->ctx_instr;
340 isn_T *isn;
341
Bram Moolenaar080457c2020-03-03 21:53:32 +0100342 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100343 if (ga_grow(instr, 1) == FAIL)
344 return NULL;
345 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
346 isn->isn_type = isn_type;
347 isn->isn_lnum = cctx->ctx_lnum + 1;
348 ++instr->ga_len;
349
350 return isn;
351}
352
353/*
354 * Generate an instruction without arguments.
355 * "drop" will be removed from the stack.
356 * Returns a pointer to the new instruction, NULL if failed.
357 */
358 static isn_T *
359generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
360{
361 garray_T *stack = &cctx->ctx_type_stack;
362
Bram Moolenaar080457c2020-03-03 21:53:32 +0100363 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100364 stack->ga_len -= drop;
365 return generate_instr(cctx, isn_type);
366}
367
368/*
369 * Generate instruction "isn_type" and put "type" on the type stack.
370 */
371 static isn_T *
372generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
373{
374 isn_T *isn;
375 garray_T *stack = &cctx->ctx_type_stack;
376
377 if ((isn = generate_instr(cctx, isn_type)) == NULL)
378 return NULL;
379
380 if (ga_grow(stack, 1) == FAIL)
381 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200382 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100383 ++stack->ga_len;
384
385 return isn;
386}
387
388/*
389 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200390 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100391 */
392 static int
393may_generate_2STRING(int offset, cctx_T *cctx)
394{
395 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200396 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100397 garray_T *stack = &cctx->ctx_type_stack;
398 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
399
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200400 switch ((*type)->tt_type)
401 {
402 // nothing to be done
403 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100404
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200405 // conversion possible
406 case VAR_SPECIAL:
407 case VAR_BOOL:
408 case VAR_NUMBER:
409 case VAR_FLOAT:
410 break;
411
412 // conversion possible (with runtime check)
413 case VAR_ANY:
414 case VAR_UNKNOWN:
415 isntype = ISN_2STRING_ANY;
416 break;
417
418 // conversion not possible
419 case VAR_VOID:
420 case VAR_BLOB:
421 case VAR_FUNC:
422 case VAR_PARTIAL:
423 case VAR_LIST:
424 case VAR_DICT:
425 case VAR_JOB:
426 case VAR_CHANNEL:
427 to_string_error((*type)->tt_type);
428 return FAIL;
429 }
430
431 *type = &t_string;
432 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100433 return FAIL;
434 isn->isn_arg.number = offset;
435
436 return OK;
437}
438
439 static int
440check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
441{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200442 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100443 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200444 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100445 {
446 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200447 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100448 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200449 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100450 return FAIL;
451 }
452 return OK;
453}
454
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200455 static int
456generate_add_instr(
457 cctx_T *cctx,
458 vartype_T vartype,
459 type_T *type1,
460 type_T *type2)
461{
462 isn_T *isn = generate_instr_drop(cctx,
463 vartype == VAR_NUMBER ? ISN_OPNR
464 : vartype == VAR_LIST ? ISN_ADDLIST
465 : vartype == VAR_BLOB ? ISN_ADDBLOB
466#ifdef FEAT_FLOAT
467 : vartype == VAR_FLOAT ? ISN_OPFLOAT
468#endif
469 : ISN_OPANY, 1);
470
471 if (vartype != VAR_LIST && vartype != VAR_BLOB
472 && type1->tt_type != VAR_ANY
473 && type2->tt_type != VAR_ANY
474 && check_number_or_float(
475 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
476 return FAIL;
477
478 if (isn != NULL)
479 isn->isn_arg.op.op_type = EXPR_ADD;
480 return isn == NULL ? FAIL : OK;
481}
482
483/*
484 * Get the type to use for an instruction for an operation on "type1" and
485 * "type2". If they are matching use a type-specific instruction. Otherwise
486 * fall back to runtime type checking.
487 */
488 static vartype_T
489operator_type(type_T *type1, type_T *type2)
490{
491 if (type1->tt_type == type2->tt_type
492 && (type1->tt_type == VAR_NUMBER
493 || type1->tt_type == VAR_LIST
494#ifdef FEAT_FLOAT
495 || type1->tt_type == VAR_FLOAT
496#endif
497 || type1->tt_type == VAR_BLOB))
498 return type1->tt_type;
499 return VAR_ANY;
500}
501
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100502/*
503 * Generate an instruction with two arguments. The instruction depends on the
504 * type of the arguments.
505 */
506 static int
507generate_two_op(cctx_T *cctx, char_u *op)
508{
509 garray_T *stack = &cctx->ctx_type_stack;
510 type_T *type1;
511 type_T *type2;
512 vartype_T vartype;
513 isn_T *isn;
514
Bram Moolenaar080457c2020-03-03 21:53:32 +0100515 RETURN_OK_IF_SKIP(cctx);
516
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200517 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100518 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
519 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200520 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521
522 switch (*op)
523 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200524 case '+':
525 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100526 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527 break;
528
529 case '-':
530 case '*':
531 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
532 op) == FAIL)
533 return FAIL;
534 if (vartype == VAR_NUMBER)
535 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
536#ifdef FEAT_FLOAT
537 else if (vartype == VAR_FLOAT)
538 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
539#endif
540 else
541 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
542 if (isn != NULL)
543 isn->isn_arg.op.op_type = *op == '*'
544 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
545 break;
546
Bram Moolenaar4c683752020-04-05 21:38:23 +0200547 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100548 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200549 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100550 && type2->tt_type != VAR_NUMBER))
551 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200552 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100553 return FAIL;
554 }
555 isn = generate_instr_drop(cctx,
556 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
557 if (isn != NULL)
558 isn->isn_arg.op.op_type = EXPR_REM;
559 break;
560 }
561
562 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200563 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100564 {
565 type_T *type = &t_any;
566
567#ifdef FEAT_FLOAT
568 // float+number and number+float results in float
569 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
570 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
571 type = &t_float;
572#endif
573 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
574 }
575
576 return OK;
577}
578
579/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200580 * Get the instruction to use for comparing "type1" with "type2"
581 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100582 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200583 static isntype_T
584get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100585{
586 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100587
Bram Moolenaar4c683752020-04-05 21:38:23 +0200588 if (type1 == VAR_UNKNOWN)
589 type1 = VAR_ANY;
590 if (type2 == VAR_UNKNOWN)
591 type2 = VAR_ANY;
592
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100593 if (type1 == type2)
594 {
595 switch (type1)
596 {
597 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
598 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
599 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
600 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
601 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
602 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
603 case VAR_LIST: isntype = ISN_COMPARELIST; break;
604 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
605 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100606 default: isntype = ISN_COMPAREANY; break;
607 }
608 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200609 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100610 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
611 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
612 isntype = ISN_COMPAREANY;
613
614 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
615 && (isntype == ISN_COMPAREBOOL
616 || isntype == ISN_COMPARESPECIAL
617 || isntype == ISN_COMPARENR
618 || isntype == ISN_COMPAREFLOAT))
619 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200620 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200622 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100623 }
624 if (isntype == ISN_DROP
625 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
626 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
627 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
628 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
629 && exptype != EXPR_IS && exptype != EXPR_ISNOT
630 && (type1 == VAR_BLOB || type2 == VAR_BLOB
631 || type1 == VAR_LIST || type2 == VAR_LIST))))
632 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200633 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100634 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200635 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100636 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200637 return isntype;
638}
639
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200640 int
641check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
642{
643 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
644 return FAIL;
645 return OK;
646}
647
Bram Moolenaara5565e42020-05-09 15:44:01 +0200648/*
649 * Generate an ISN_COMPARE* instruction with a boolean result.
650 */
651 static int
652generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
653{
654 isntype_T isntype;
655 isn_T *isn;
656 garray_T *stack = &cctx->ctx_type_stack;
657 vartype_T type1;
658 vartype_T type2;
659
660 RETURN_OK_IF_SKIP(cctx);
661
662 // Get the known type of the two items on the stack. If they are matching
663 // use a type-specific instruction. Otherwise fall back to runtime type
664 // checking.
665 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
666 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
667 isntype = get_compare_isn(exptype, type1, type2);
668 if (isntype == ISN_DROP)
669 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670
671 if ((isn = generate_instr(cctx, isntype)) == NULL)
672 return FAIL;
673 isn->isn_arg.op.op_type = exptype;
674 isn->isn_arg.op.op_ic = ic;
675
676 // takes two arguments, puts one bool back
677 if (stack->ga_len >= 2)
678 {
679 --stack->ga_len;
680 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
681 }
682
683 return OK;
684}
685
686/*
687 * Generate an ISN_2BOOL instruction.
688 */
689 static int
690generate_2BOOL(cctx_T *cctx, int invert)
691{
692 isn_T *isn;
693 garray_T *stack = &cctx->ctx_type_stack;
694
Bram Moolenaar080457c2020-03-03 21:53:32 +0100695 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100696 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
697 return FAIL;
698 isn->isn_arg.number = invert;
699
700 // type becomes bool
701 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
702
703 return OK;
704}
705
706 static int
707generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
708{
709 isn_T *isn;
710 garray_T *stack = &cctx->ctx_type_stack;
711
Bram Moolenaar080457c2020-03-03 21:53:32 +0100712 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100713 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
714 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200715 // TODO: whole type, e.g. for a function also arg and return types
716 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100717 isn->isn_arg.type.ct_off = offset;
718
719 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200720 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100721
722 return OK;
723}
724
725/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200726 * Check that
727 * - "actual" is "expected" type or
728 * - "actual" is a type that can be "expected" type: add a runtime check; or
729 * - return FAIL.
730 */
731 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200732need_type(
733 type_T *actual,
734 type_T *expected,
735 int offset,
736 cctx_T *cctx,
737 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200738{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200739 if (expected == &t_bool && actual != &t_bool
740 && (actual->tt_flags & TTFLAG_BOOL_OK))
741 {
742 // Using "0", "1" or the result of an expression with "&&" or "||" as a
743 // boolean is OK but requires a conversion.
744 generate_2BOOL(cctx, FALSE);
745 return OK;
746 }
747
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200748 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200749 return OK;
750 if (actual->tt_type != VAR_ANY
751 && actual->tt_type != VAR_UNKNOWN
752 && !(actual->tt_type == VAR_FUNC
753 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
754 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200755 if (!silent)
756 type_mismatch(expected, actual);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200757 return FAIL;
758 }
759 generate_TYPECHECK(cctx, expected, offset);
760 return OK;
761}
762
763/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764 * Generate an ISN_PUSHNR instruction.
765 */
766 static int
767generate_PUSHNR(cctx_T *cctx, varnumber_T number)
768{
769 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200770 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100771
Bram Moolenaar080457c2020-03-03 21:53:32 +0100772 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100773 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
774 return FAIL;
775 isn->isn_arg.number = number;
776
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200777 if (number == 0 || number == 1)
778 {
779 type_T *type = alloc_type(cctx->ctx_type_list);
780
781 // A 0 or 1 number can also be used as a bool.
782 if (type != NULL)
783 {
784 type->tt_type = VAR_NUMBER;
785 type->tt_flags = TTFLAG_BOOL_OK;
786 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
787 }
788 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100789 return OK;
790}
791
792/*
793 * Generate an ISN_PUSHBOOL instruction.
794 */
795 static int
796generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
797{
798 isn_T *isn;
799
Bram Moolenaar080457c2020-03-03 21:53:32 +0100800 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100801 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
802 return FAIL;
803 isn->isn_arg.number = number;
804
805 return OK;
806}
807
808/*
809 * Generate an ISN_PUSHSPEC instruction.
810 */
811 static int
812generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
813{
814 isn_T *isn;
815
Bram Moolenaar080457c2020-03-03 21:53:32 +0100816 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
818 return FAIL;
819 isn->isn_arg.number = number;
820
821 return OK;
822}
823
824#ifdef FEAT_FLOAT
825/*
826 * Generate an ISN_PUSHF instruction.
827 */
828 static int
829generate_PUSHF(cctx_T *cctx, float_T fnumber)
830{
831 isn_T *isn;
832
Bram Moolenaar080457c2020-03-03 21:53:32 +0100833 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100834 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
835 return FAIL;
836 isn->isn_arg.fnumber = fnumber;
837
838 return OK;
839}
840#endif
841
842/*
843 * Generate an ISN_PUSHS instruction.
844 * Consumes "str".
845 */
846 static int
847generate_PUSHS(cctx_T *cctx, char_u *str)
848{
849 isn_T *isn;
850
Bram Moolenaar080457c2020-03-03 21:53:32 +0100851 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100852 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
853 return FAIL;
854 isn->isn_arg.string = str;
855
856 return OK;
857}
858
859/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100860 * Generate an ISN_PUSHCHANNEL instruction.
861 * Consumes "channel".
862 */
863 static int
864generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
865{
866 isn_T *isn;
867
Bram Moolenaar080457c2020-03-03 21:53:32 +0100868 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100869 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
870 return FAIL;
871 isn->isn_arg.channel = channel;
872
873 return OK;
874}
875
876/*
877 * Generate an ISN_PUSHJOB instruction.
878 * Consumes "job".
879 */
880 static int
881generate_PUSHJOB(cctx_T *cctx, job_T *job)
882{
883 isn_T *isn;
884
Bram Moolenaar080457c2020-03-03 21:53:32 +0100885 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100886 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100887 return FAIL;
888 isn->isn_arg.job = job;
889
890 return OK;
891}
892
893/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100894 * Generate an ISN_PUSHBLOB instruction.
895 * Consumes "blob".
896 */
897 static int
898generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
899{
900 isn_T *isn;
901
Bram Moolenaar080457c2020-03-03 21:53:32 +0100902 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100903 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
904 return FAIL;
905 isn->isn_arg.blob = blob;
906
907 return OK;
908}
909
910/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100911 * Generate an ISN_PUSHFUNC instruction with name "name".
912 * Consumes "name".
913 */
914 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200915generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100916{
917 isn_T *isn;
918
Bram Moolenaar080457c2020-03-03 21:53:32 +0100919 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200920 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100921 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200922 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100923
924 return OK;
925}
926
927/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200928 * Generate an ISN_GETITEM instruction with "index".
929 */
930 static int
931generate_GETITEM(cctx_T *cctx, int index)
932{
933 isn_T *isn;
934 garray_T *stack = &cctx->ctx_type_stack;
935 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
936 type_T *item_type = &t_any;
937
938 RETURN_OK_IF_SKIP(cctx);
939
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200940 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200941 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200942 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200943 emsg(_(e_listreq));
944 return FAIL;
945 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200946 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200947 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
948 return FAIL;
949 isn->isn_arg.number = index;
950
951 // add the item type to the type stack
952 if (ga_grow(stack, 1) == FAIL)
953 return FAIL;
954 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
955 ++stack->ga_len;
956 return OK;
957}
958
959/*
Bram Moolenaar9af78762020-06-16 11:34:42 +0200960 * Generate an ISN_SLICE instruction with "count".
961 */
962 static int
963generate_SLICE(cctx_T *cctx, int count)
964{
965 isn_T *isn;
966
967 RETURN_OK_IF_SKIP(cctx);
968 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
969 return FAIL;
970 isn->isn_arg.number = count;
971 return OK;
972}
973
974/*
975 * Generate an ISN_CHECKLEN instruction with "min_len".
976 */
977 static int
978generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
979{
980 isn_T *isn;
981
982 RETURN_OK_IF_SKIP(cctx);
983
984 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
985 return FAIL;
986 isn->isn_arg.checklen.cl_min_len = min_len;
987 isn->isn_arg.checklen.cl_more_OK = more_OK;
988
989 return OK;
990}
991
992/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100993 * Generate an ISN_STORE instruction.
994 */
995 static int
996generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
997{
998 isn_T *isn;
999
Bram Moolenaar080457c2020-03-03 21:53:32 +01001000 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001001 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1002 return FAIL;
1003 if (name != NULL)
1004 isn->isn_arg.string = vim_strsave(name);
1005 else
1006 isn->isn_arg.number = idx;
1007
1008 return OK;
1009}
1010
1011/*
1012 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1013 */
1014 static int
1015generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1016{
1017 isn_T *isn;
1018
Bram Moolenaar080457c2020-03-03 21:53:32 +01001019 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001020 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1021 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001022 isn->isn_arg.storenr.stnr_idx = idx;
1023 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001024
1025 return OK;
1026}
1027
1028/*
1029 * Generate an ISN_STOREOPT instruction
1030 */
1031 static int
1032generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1033{
1034 isn_T *isn;
1035
Bram Moolenaar080457c2020-03-03 21:53:32 +01001036 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001037 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001038 return FAIL;
1039 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1040 isn->isn_arg.storeopt.so_flags = opt_flags;
1041
1042 return OK;
1043}
1044
1045/*
1046 * Generate an ISN_LOAD or similar instruction.
1047 */
1048 static int
1049generate_LOAD(
1050 cctx_T *cctx,
1051 isntype_T isn_type,
1052 int idx,
1053 char_u *name,
1054 type_T *type)
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_type, type)) == NULL)
1060 return FAIL;
1061 if (name != NULL)
1062 isn->isn_arg.string = vim_strsave(name);
1063 else
1064 isn->isn_arg.number = idx;
1065
1066 return OK;
1067}
1068
1069/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001070 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001071 */
1072 static int
1073generate_LOADV(
1074 cctx_T *cctx,
1075 char_u *name,
1076 int error)
1077{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001078 int di_flags;
1079 int vidx = find_vim_var(name, &di_flags);
1080 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001081
Bram Moolenaar080457c2020-03-03 21:53:32 +01001082 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001083 if (vidx < 0)
1084 {
1085 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001086 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001087 return FAIL;
1088 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001089 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001090
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001091 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001092}
1093
1094/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001095 * Generate an ISN_UNLET instruction.
1096 */
1097 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001098generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001099{
1100 isn_T *isn;
1101
1102 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001103 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001104 return FAIL;
1105 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1106 isn->isn_arg.unlet.ul_forceit = forceit;
1107
1108 return OK;
1109}
1110
1111/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001112 * Generate an ISN_LOCKCONST instruction.
1113 */
1114 static int
1115generate_LOCKCONST(cctx_T *cctx)
1116{
1117 isn_T *isn;
1118
1119 RETURN_OK_IF_SKIP(cctx);
1120 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1121 return FAIL;
1122 return OK;
1123}
1124
1125/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001126 * Generate an ISN_LOADS instruction.
1127 */
1128 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001129generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001130 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001131 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001133 int sid,
1134 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135{
1136 isn_T *isn;
1137
Bram Moolenaar080457c2020-03-03 21:53:32 +01001138 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001139 if (isn_type == ISN_LOADS)
1140 isn = generate_instr_type(cctx, isn_type, type);
1141 else
1142 isn = generate_instr_drop(cctx, isn_type, 1);
1143 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001144 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001145 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1146 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001147
1148 return OK;
1149}
1150
1151/*
1152 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1153 */
1154 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001155generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001156 cctx_T *cctx,
1157 isntype_T isn_type,
1158 int sid,
1159 int idx,
1160 type_T *type)
1161{
1162 isn_T *isn;
1163
Bram Moolenaar080457c2020-03-03 21:53:32 +01001164 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001165 if (isn_type == ISN_LOADSCRIPT)
1166 isn = generate_instr_type(cctx, isn_type, type);
1167 else
1168 isn = generate_instr_drop(cctx, isn_type, 1);
1169 if (isn == NULL)
1170 return FAIL;
1171 isn->isn_arg.script.script_sid = sid;
1172 isn->isn_arg.script.script_idx = idx;
1173 return OK;
1174}
1175
1176/*
1177 * Generate an ISN_NEWLIST instruction.
1178 */
1179 static int
1180generate_NEWLIST(cctx_T *cctx, int count)
1181{
1182 isn_T *isn;
1183 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001184 type_T *type;
1185 type_T *member;
1186
Bram Moolenaar080457c2020-03-03 21:53:32 +01001187 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001188 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1189 return FAIL;
1190 isn->isn_arg.number = count;
1191
Bram Moolenaar127542b2020-08-09 17:22:04 +02001192 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001193 if (count == 0)
1194 member = &t_void;
1195 else
1196 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001197 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1198 cctx->ctx_type_list);
1199 type = get_list_type(member, cctx->ctx_type_list);
1200
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001201 // drop the value types
1202 stack->ga_len -= count;
1203
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204 // add the list type to the type stack
1205 if (ga_grow(stack, 1) == FAIL)
1206 return FAIL;
1207 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1208 ++stack->ga_len;
1209
1210 return OK;
1211}
1212
1213/*
1214 * Generate an ISN_NEWDICT instruction.
1215 */
1216 static int
1217generate_NEWDICT(cctx_T *cctx, int count)
1218{
1219 isn_T *isn;
1220 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001221 type_T *type;
1222 type_T *member;
1223
Bram Moolenaar080457c2020-03-03 21:53:32 +01001224 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001225 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1226 return FAIL;
1227 isn->isn_arg.number = count;
1228
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001229 if (count == 0)
1230 member = &t_void;
1231 else
1232 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001233 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1234 cctx->ctx_type_list);
1235 type = get_dict_type(member, cctx->ctx_type_list);
1236
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001237 // drop the key and value types
1238 stack->ga_len -= 2 * count;
1239
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240 // add the dict type to the type stack
1241 if (ga_grow(stack, 1) == FAIL)
1242 return FAIL;
1243 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1244 ++stack->ga_len;
1245
1246 return OK;
1247}
1248
1249/*
1250 * Generate an ISN_FUNCREF instruction.
1251 */
1252 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001253generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001254{
1255 isn_T *isn;
1256 garray_T *stack = &cctx->ctx_type_stack;
1257
Bram Moolenaar080457c2020-03-03 21:53:32 +01001258 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001259 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1260 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001261 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001262 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263
1264 if (ga_grow(stack, 1) == FAIL)
1265 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001266 ((type_T **)stack->ga_data)[stack->ga_len] =
1267 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268 ++stack->ga_len;
1269
1270 return OK;
1271}
1272
1273/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001274 * Generate an ISN_NEWFUNC instruction.
1275 */
1276 static int
1277generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1278{
1279 isn_T *isn;
1280 char_u *name;
1281
1282 RETURN_OK_IF_SKIP(cctx);
1283 name = vim_strsave(lambda_name);
1284 if (name == NULL)
1285 return FAIL;
1286 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1287 return FAIL;
1288 isn->isn_arg.newfunc.nf_lambda = name;
1289 isn->isn_arg.newfunc.nf_global = func_name;
1290
1291 return OK;
1292}
1293
1294/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001295 * Generate an ISN_JUMP instruction.
1296 */
1297 static int
1298generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1299{
1300 isn_T *isn;
1301 garray_T *stack = &cctx->ctx_type_stack;
1302
Bram Moolenaar080457c2020-03-03 21:53:32 +01001303 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001304 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1305 return FAIL;
1306 isn->isn_arg.jump.jump_when = when;
1307 isn->isn_arg.jump.jump_where = where;
1308
1309 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1310 --stack->ga_len;
1311
1312 return OK;
1313}
1314
1315 static int
1316generate_FOR(cctx_T *cctx, int loop_idx)
1317{
1318 isn_T *isn;
1319 garray_T *stack = &cctx->ctx_type_stack;
1320
Bram Moolenaar080457c2020-03-03 21:53:32 +01001321 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001322 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1323 return FAIL;
1324 isn->isn_arg.forloop.for_idx = loop_idx;
1325
1326 if (ga_grow(stack, 1) == FAIL)
1327 return FAIL;
1328 // type doesn't matter, will be stored next
1329 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1330 ++stack->ga_len;
1331
1332 return OK;
1333}
1334
1335/*
1336 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001337 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338 * Return FAIL if the number of arguments is wrong.
1339 */
1340 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001341generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342{
1343 isn_T *isn;
1344 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001345 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001346 type_T *argtypes[MAX_FUNC_ARGS];
1347 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001348
Bram Moolenaar080457c2020-03-03 21:53:32 +01001349 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001350 argoff = check_internal_func(func_idx, argcount);
1351 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 return FAIL;
1353
Bram Moolenaar389df252020-07-09 21:20:47 +02001354 if (method_call && argoff > 1)
1355 {
1356 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1357 return FAIL;
1358 isn->isn_arg.shuffle.shfl_item = argcount;
1359 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1360 }
1361
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1363 return FAIL;
1364 isn->isn_arg.bfunc.cbf_idx = func_idx;
1365 isn->isn_arg.bfunc.cbf_argcount = argcount;
1366
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001367 for (i = 0; i < argcount; ++i)
1368 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1369
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001370 stack->ga_len -= argcount; // drop the arguments
1371 if (ga_grow(stack, 1) == FAIL)
1372 return FAIL;
1373 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001374 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001375 ++stack->ga_len; // add return value
1376
1377 return OK;
1378}
1379
1380/*
1381 * Generate an ISN_DCALL or ISN_UCALL instruction.
1382 * Return FAIL if the number of arguments is wrong.
1383 */
1384 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001385generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001386{
1387 isn_T *isn;
1388 garray_T *stack = &cctx->ctx_type_stack;
1389 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001390 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001391
Bram Moolenaar080457c2020-03-03 21:53:32 +01001392 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393 if (argcount > regular_args && !has_varargs(ufunc))
1394 {
1395 semsg(_(e_toomanyarg), ufunc->uf_name);
1396 return FAIL;
1397 }
1398 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1399 {
1400 semsg(_(e_toofewarg), ufunc->uf_name);
1401 return FAIL;
1402 }
1403
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001404 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001405 {
1406 int i;
1407
1408 for (i = 0; i < argcount; ++i)
1409 {
1410 type_T *expected;
1411 type_T *actual;
1412
1413 if (i < regular_args)
1414 {
1415 if (ufunc->uf_arg_types == NULL)
1416 continue;
1417 expected = ufunc->uf_arg_types[i];
1418 }
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001419 else if (ufunc->uf_va_type == NULL)
1420 // possibly a lambda
1421 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001422 else
1423 expected = ufunc->uf_va_type->tt_member;
1424 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001425 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001426 {
1427 arg_type_mismatch(expected, actual, i + 1);
1428 return FAIL;
1429 }
1430 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001431 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001432 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1433 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001434 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001435 }
1436
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001437 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001438 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001439 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001440 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001441 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442 {
1443 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1444 isn->isn_arg.dfunc.cdf_argcount = argcount;
1445 }
1446 else
1447 {
1448 // A user function may be deleted and redefined later, can't use the
1449 // ufunc pointer, need to look it up again at runtime.
1450 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1451 isn->isn_arg.ufunc.cuf_argcount = argcount;
1452 }
1453
1454 stack->ga_len -= argcount; // drop the arguments
1455 if (ga_grow(stack, 1) == FAIL)
1456 return FAIL;
1457 // add return value
1458 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1459 ++stack->ga_len;
1460
1461 return OK;
1462}
1463
1464/*
1465 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1466 */
1467 static int
1468generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1469{
1470 isn_T *isn;
1471 garray_T *stack = &cctx->ctx_type_stack;
1472
Bram Moolenaar080457c2020-03-03 21:53:32 +01001473 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001474 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1475 return FAIL;
1476 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1477 isn->isn_arg.ufunc.cuf_argcount = argcount;
1478
1479 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001480 if (ga_grow(stack, 1) == FAIL)
1481 return FAIL;
1482 // add return value
1483 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1484 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485
1486 return OK;
1487}
1488
1489/*
1490 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001491 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492 */
1493 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001494generate_PCALL(
1495 cctx_T *cctx,
1496 int argcount,
1497 char_u *name,
1498 type_T *type,
1499 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001500{
1501 isn_T *isn;
1502 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001503 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504
Bram Moolenaar080457c2020-03-03 21:53:32 +01001505 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001506
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001507 if (type->tt_type == VAR_ANY)
1508 ret_type = &t_any;
1509 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001510 {
1511 if (type->tt_argcount != -1)
1512 {
1513 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1514
1515 if (argcount < type->tt_min_argcount - varargs)
1516 {
1517 semsg(_(e_toofewarg), "[reference]");
1518 return FAIL;
1519 }
1520 if (!varargs && argcount > type->tt_argcount)
1521 {
1522 semsg(_(e_toomanyarg), "[reference]");
1523 return FAIL;
1524 }
1525 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001526 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001527 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001528 else
1529 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001530 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001531 return FAIL;
1532 }
1533
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001534 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1535 return FAIL;
1536 isn->isn_arg.pfunc.cpf_top = at_top;
1537 isn->isn_arg.pfunc.cpf_argcount = argcount;
1538
1539 stack->ga_len -= argcount; // drop the arguments
1540
1541 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001542 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001543
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001544 // If partial is above the arguments it must be cleared and replaced with
1545 // the return value.
1546 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1547 return FAIL;
1548
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001549 return OK;
1550}
1551
1552/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001553 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001554 */
1555 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001556generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557{
1558 isn_T *isn;
1559 garray_T *stack = &cctx->ctx_type_stack;
1560 type_T *type;
1561
Bram Moolenaar080457c2020-03-03 21:53:32 +01001562 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001563 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001564 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001565 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001566
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001567 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001569 if (type->tt_type != VAR_DICT && type != &t_any)
1570 {
1571 emsg(_(e_dictreq));
1572 return FAIL;
1573 }
1574 // change dict type to dict member type
1575 if (type->tt_type == VAR_DICT)
1576 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577
1578 return OK;
1579}
1580
1581/*
1582 * Generate an ISN_ECHO instruction.
1583 */
1584 static int
1585generate_ECHO(cctx_T *cctx, int with_white, int count)
1586{
1587 isn_T *isn;
1588
Bram Moolenaar080457c2020-03-03 21:53:32 +01001589 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1591 return FAIL;
1592 isn->isn_arg.echo.echo_with_white = with_white;
1593 isn->isn_arg.echo.echo_count = count;
1594
1595 return OK;
1596}
1597
Bram Moolenaarad39c092020-02-26 18:23:43 +01001598/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001599 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001600 */
1601 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001602generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001603{
1604 isn_T *isn;
1605
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001606 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001607 return FAIL;
1608 isn->isn_arg.number = count;
1609
1610 return OK;
1611}
1612
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001613/*
1614 * Generate an ISN_PUT instruction.
1615 */
1616 static int
1617generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1618{
1619 isn_T *isn;
1620
1621 RETURN_OK_IF_SKIP(cctx);
1622 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1623 return FAIL;
1624 isn->isn_arg.put.put_regname = regname;
1625 isn->isn_arg.put.put_lnum = lnum;
1626 return OK;
1627}
1628
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629 static int
1630generate_EXEC(cctx_T *cctx, char_u *line)
1631{
1632 isn_T *isn;
1633
Bram Moolenaar080457c2020-03-03 21:53:32 +01001634 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001635 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1636 return FAIL;
1637 isn->isn_arg.string = vim_strsave(line);
1638 return OK;
1639}
1640
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001641 static int
1642generate_EXECCONCAT(cctx_T *cctx, int count)
1643{
1644 isn_T *isn;
1645
1646 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1647 return FAIL;
1648 isn->isn_arg.number = count;
1649 return OK;
1650}
1651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652/*
1653 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001654 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001656 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001657reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1658{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001659 lvar_T *lvar;
1660
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001661 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001662 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001663 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001664 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665 }
1666
1667 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001668 return NULL;
1669 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001670
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001671 // Every local variable uses the next entry on the stack. We could re-use
1672 // the last ones when leaving a scope, but then variables used in a closure
1673 // might get overwritten. To keep things simple do not re-use stack
1674 // entries. This is less efficient, but memory is cheap these days.
1675 lvar->lv_idx = cctx->ctx_locals_count++;
1676
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001677 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001678 lvar->lv_const = isConst;
1679 lvar->lv_type = type;
1680
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001681 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001682}
1683
1684/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001685 * Remove local variables above "new_top".
1686 */
1687 static void
1688unwind_locals(cctx_T *cctx, int new_top)
1689{
1690 if (cctx->ctx_locals.ga_len > new_top)
1691 {
1692 int idx;
1693 lvar_T *lvar;
1694
1695 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1696 {
1697 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1698 vim_free(lvar->lv_name);
1699 }
1700 }
1701 cctx->ctx_locals.ga_len = new_top;
1702}
1703
1704/*
1705 * Free all local variables.
1706 */
1707 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001708free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001709{
1710 unwind_locals(cctx, 0);
1711 ga_clear(&cctx->ctx_locals);
1712}
1713
1714/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001715 * Find "name" in script-local items of script "sid".
1716 * Returns the index in "sn_var_vals" if found.
1717 * If found but not in "sn_var_vals" returns -1.
1718 * If not found returns -2.
1719 */
1720 int
1721get_script_item_idx(int sid, char_u *name, int check_writable)
1722{
1723 hashtab_T *ht;
1724 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001725 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001726 int idx;
1727
1728 // First look the name up in the hashtable.
Bram Moolenaare3d46852020-08-29 13:39:17 +02001729 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730 return -1;
1731 ht = &SCRIPT_VARS(sid);
1732 di = find_var_in_ht(ht, 0, name, TRUE);
1733 if (di == NULL)
1734 return -2;
1735
1736 // Now find the svar_T index in sn_var_vals.
1737 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1738 {
1739 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1740
1741 if (sv->sv_tv == &di->di_tv)
1742 {
1743 if (check_writable && sv->sv_const)
1744 semsg(_(e_readonlyvar), name);
1745 return idx;
1746 }
1747 }
1748 return -1;
1749}
1750
1751/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001752 * Find "name" in imported items of the current script or in "cctx" if not
1753 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001754 */
1755 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001756find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001757{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001758 int idx;
1759
Bram Moolenaare3d46852020-08-29 13:39:17 +02001760 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001761 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762 if (cctx != NULL)
1763 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1764 {
1765 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1766 + idx;
1767
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001768 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1769 : STRLEN(import->imp_name) == len
1770 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771 return import;
1772 }
1773
Bram Moolenaarefa94442020-08-08 22:16:00 +02001774 return find_imported_in_script(name, len, current_sctx.sc_sid);
1775}
1776
1777 imported_T *
1778find_imported_in_script(char_u *name, size_t len, int sid)
1779{
Bram Moolenaare3d46852020-08-29 13:39:17 +02001780 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001781 int idx;
1782
Bram Moolenaare3d46852020-08-29 13:39:17 +02001783 if (!SCRIPT_ID_VALID(sid))
1784 return NULL;
1785 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001786 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1787 {
1788 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1789
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001790 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1791 : STRLEN(import->imp_name) == len
1792 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793 return import;
1794 }
1795 return NULL;
1796}
1797
1798/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001799 * Free all imported variables.
1800 */
1801 static void
1802free_imported(cctx_T *cctx)
1803{
1804 int idx;
1805
1806 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1807 {
1808 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1809
1810 vim_free(import->imp_name);
1811 }
1812 ga_clear(&cctx->ctx_imports);
1813}
1814
1815/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02001816 * Return TRUE if "p" points at a "#" but not at "#{".
1817 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001818 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001819vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001820{
1821 return p[0] == '#' && p[1] != '{';
1822}
1823
1824/*
1825 * Return a pointer to the next line that isn't empty or only contains a
1826 * comment. Skips over white space.
1827 * Returns NULL if there is none.
1828 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001829 char_u *
1830peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001831{
1832 int lnum = cctx->ctx_lnum;
1833
1834 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
1835 {
1836 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001837 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001838
Bram Moolenaarba60cc42020-08-12 19:15:33 +02001839 // ignore NULLs inserted for continuation lines
1840 if (line != NULL)
1841 {
1842 p = skipwhite(line);
1843 if (*p != NUL && !vim9_comment_start(p))
1844 return p;
1845 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02001846 }
1847 return NULL;
1848}
1849
1850/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001851 * Called when checking for a following operator at "arg". When the rest of
1852 * the line is empty or only a comment, peek the next line. If there is a next
1853 * line return a pointer to it and set "nextp".
1854 * Otherwise skip over white space.
1855 */
1856 static char_u *
1857may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
1858{
1859 char_u *p = skipwhite(arg);
1860
1861 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001862 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001863 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001864 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001865 if (*nextp != NULL)
1866 return *nextp;
1867 }
1868 return p;
1869}
1870
1871/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02001872 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02001873 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02001874 * Returns NULL when at the end.
1875 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001876 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02001877next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02001878{
Bram Moolenaar7a092242020-04-16 22:10:49 +02001879 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001880
1881 do
1882 {
1883 ++cctx->ctx_lnum;
1884 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02001885 {
1886 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001887 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02001888 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02001889 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02001890 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001891 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001892 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02001893 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02001894 return line;
1895}
1896
1897/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001898 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02001899 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001900 * Return FAIL if beyond the last line, "*arg" is unmodified then.
1901 */
1902 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02001903may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001904{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001905 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001906 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02001907 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001908
1909 if (next == NULL)
1910 return FAIL;
1911 *arg = skipwhite(next);
1912 }
1913 return OK;
1914}
1915
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001916/*
1917 * Idem, and give an error when failed.
1918 */
1919 static int
1920may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
1921{
1922 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1923 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001924 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001925 return FAIL;
1926 }
1927 return OK;
1928}
1929
1930
Bram Moolenaara5565e42020-05-09 15:44:01 +02001931// Structure passed between the compile_expr* functions to keep track of
1932// constants that have been parsed but for which no code was produced yet. If
1933// possible expressions on these constants are applied at compile time. If
1934// that is not possible, the code to push the constants needs to be generated
1935// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02001936// Using 50 should be more than enough of 5 levels of ().
1937#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02001938typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02001939 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02001940 int pp_used; // active entries in pp_tv[]
1941} ppconst_T;
1942
Bram Moolenaar1c747212020-05-09 18:28:34 +02001943static int compile_expr0(char_u **arg, cctx_T *cctx);
1944static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1945
Bram Moolenaara5565e42020-05-09 15:44:01 +02001946/*
1947 * Generate a PUSH instruction for "tv".
1948 * "tv" will be consumed or cleared.
1949 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
1950 */
1951 static int
1952generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
1953{
1954 if (tv != NULL)
1955 {
1956 switch (tv->v_type)
1957 {
1958 case VAR_UNKNOWN:
1959 break;
1960 case VAR_BOOL:
1961 generate_PUSHBOOL(cctx, tv->vval.v_number);
1962 break;
1963 case VAR_SPECIAL:
1964 generate_PUSHSPEC(cctx, tv->vval.v_number);
1965 break;
1966 case VAR_NUMBER:
1967 generate_PUSHNR(cctx, tv->vval.v_number);
1968 break;
1969#ifdef FEAT_FLOAT
1970 case VAR_FLOAT:
1971 generate_PUSHF(cctx, tv->vval.v_float);
1972 break;
1973#endif
1974 case VAR_BLOB:
1975 generate_PUSHBLOB(cctx, tv->vval.v_blob);
1976 tv->vval.v_blob = NULL;
1977 break;
1978 case VAR_STRING:
1979 generate_PUSHS(cctx, tv->vval.v_string);
1980 tv->vval.v_string = NULL;
1981 break;
1982 default:
1983 iemsg("constant type not supported");
1984 clear_tv(tv);
1985 return FAIL;
1986 }
1987 tv->v_type = VAR_UNKNOWN;
1988 }
1989 return OK;
1990}
1991
1992/*
1993 * Generate code for any ppconst entries.
1994 */
1995 static int
1996generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
1997{
1998 int i;
1999 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002000 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002001
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002002 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002003 for (i = 0; i < ppconst->pp_used; ++i)
2004 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2005 ret = FAIL;
2006 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002007 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002008 return ret;
2009}
2010
2011/*
2012 * Clear ppconst constants. Used when failing.
2013 */
2014 static void
2015clear_ppconst(ppconst_T *ppconst)
2016{
2017 int i;
2018
2019 for (i = 0; i < ppconst->pp_used; ++i)
2020 clear_tv(&ppconst->pp_tv[i]);
2021 ppconst->pp_used = 0;
2022}
2023
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002024/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002025 * Generate an instruction to load script-local variable "name", without the
2026 * leading "s:".
2027 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002028 */
2029 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002030compile_load_scriptvar(
2031 cctx_T *cctx,
2032 char_u *name, // variable NUL terminated
2033 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002034 char_u **end, // end of variable
2035 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002036{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002037 scriptitem_T *si;
2038 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002039 imported_T *import;
2040
Bram Moolenaare3d46852020-08-29 13:39:17 +02002041 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2042 return FAIL;
2043 si = SCRIPT_ITEM(current_sctx.sc_sid);
2044 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002045 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002046 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002047 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002048 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2049 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002050 }
2051 if (idx >= 0)
2052 {
2053 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2054
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002055 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002056 current_sctx.sc_sid, idx, sv->sv_type);
2057 return OK;
2058 }
2059
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002060 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002061 if (import != NULL)
2062 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002063 if (import->imp_all)
2064 {
2065 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002066 char_u *exp_name;
2067 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002068 ufunc_T *ufunc;
2069 type_T *type;
2070
2071 // Used "import * as Name", need to lookup the member.
2072 if (*p != '.')
2073 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002074 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002075 return FAIL;
2076 }
2077 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002078 if (VIM_ISWHITE(*p))
2079 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002080 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002081 return FAIL;
2082 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002083
Bram Moolenaar1c991142020-07-04 13:15:31 +02002084 // isolate one name
2085 exp_name = p;
2086 while (eval_isnamec(*p))
2087 ++p;
2088 cc = *p;
2089 *p = NUL;
2090
2091 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2092 *p = cc;
2093 p = skipwhite(p);
2094
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002095 // TODO: what if it is a function?
2096 if (idx < 0)
2097 return FAIL;
2098 *end = p;
2099
2100 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2101 import->imp_sid,
2102 idx,
2103 type);
2104 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002105 else if (import->imp_funcname != NULL)
2106 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002107 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002108 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2109 import->imp_sid,
2110 import->imp_var_vals_idx,
2111 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112 return OK;
2113 }
2114
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002115 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002116 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002117 return FAIL;
2118}
2119
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002120 static int
2121generate_funcref(cctx_T *cctx, char_u *name)
2122{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002123 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002124
2125 if (ufunc == NULL)
2126 return FAIL;
2127
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002128 // Need to compile any default values to get the argument types.
2129 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2130 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2131 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002132 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002133}
2134
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135/*
2136 * Compile a variable name into a load instruction.
2137 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002138 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002139 * When "error" is FALSE do not give an error when not found.
2140 */
2141 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002142compile_load(
2143 char_u **arg,
2144 char_u *end_arg,
2145 cctx_T *cctx,
2146 int is_expr,
2147 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002148{
2149 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002150 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002151 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002152 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002153 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002154
2155 if (*(*arg + 1) == ':')
2156 {
2157 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002158 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002159 {
2160 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002161
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002162 switch (**arg)
2163 {
2164 case 'g': isn_type = ISN_LOADGDICT; break;
2165 case 'w': isn_type = ISN_LOADWDICT; break;
2166 case 't': isn_type = ISN_LOADTDICT; break;
2167 case 'b': isn_type = ISN_LOADBDICT; break;
2168 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002169 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002170 goto theend;
2171 }
2172 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2173 goto theend;
2174 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002175 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002176 else
2177 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002178 isntype_T isn_type = ISN_DROP;
2179
2180 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2181 if (name == NULL)
2182 return FAIL;
2183
2184 switch (**arg)
2185 {
2186 case 'v': res = generate_LOADV(cctx, name, error);
2187 break;
2188 case 's': res = compile_load_scriptvar(cctx, name,
2189 NULL, NULL, error);
2190 break;
2191 case 'g': isn_type = ISN_LOADG; break;
2192 case 'w': isn_type = ISN_LOADW; break;
2193 case 't': isn_type = ISN_LOADT; break;
2194 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002195 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002196 goto theend;
2197 }
2198 if (isn_type != ISN_DROP)
2199 {
2200 // Global, Buffer-local, Window-local and Tabpage-local
2201 // variables can be defined later, thus we don't check if it
2202 // exists, give error at runtime.
2203 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2204 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002205 }
2206 }
2207 else
2208 {
2209 size_t len = end - *arg;
2210 int idx;
2211 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002212 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002213
2214 name = vim_strnsave(*arg, end - *arg);
2215 if (name == NULL)
2216 return FAIL;
2217
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002218 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002219 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002220 if (!gen_load_outer)
2221 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002222 }
2223 else
2224 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002225 lvar_T *lvar = lookup_local(*arg, len, cctx);
2226
2227 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002228 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002229 type = lvar->lv_type;
2230 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002231 if (lvar->lv_from_outer)
2232 gen_load_outer = TRUE;
2233 else
2234 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002235 }
2236 else
2237 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002238 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002239 // already exists in a Vim9 script or when it's imported.
2240 if (lookup_script(*arg, len, TRUE) == OK
2241 || find_imported(name, 0, cctx) != NULL)
2242 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002243
Bram Moolenaar0f769812020-09-12 18:32:34 +02002244 // When evaluating an expression and the name starts with an
2245 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002246 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002247 if (res == FAIL && is_expr
2248 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002249 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002250 }
2251 }
2252 if (gen_load)
2253 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002254 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002255 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002256 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002257 cctx->ctx_outer_used = TRUE;
2258 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002259 }
2260
2261 *arg = end;
2262
2263theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002264 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002265 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002266 vim_free(name);
2267 return res;
2268}
2269
2270/*
2271 * Compile the argument expressions.
2272 * "arg" points to just after the "(" and is advanced to after the ")"
2273 */
2274 static int
2275compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2276{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002277 char_u *p = *arg;
2278 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002279
Bram Moolenaare6085c52020-04-12 20:19:16 +02002280 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002281 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002282 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2283 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002284 if (*p == ')')
2285 {
2286 *arg = p + 1;
2287 return OK;
2288 }
2289
Bram Moolenaara5565e42020-05-09 15:44:01 +02002290 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002291 return FAIL;
2292 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002293
2294 if (*p != ',' && *skipwhite(p) == ',')
2295 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002296 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002297 p = skipwhite(p);
2298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002300 {
2301 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002302 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002303 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002304 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002305 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002306 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002307 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002308failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002309 emsg(_(e_missing_close));
2310 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311}
2312
2313/*
2314 * Compile a function call: name(arg1, arg2)
2315 * "arg" points to "name", "arg + varlen" to the "(".
2316 * "argcount_init" is 1 for "value->method()"
2317 * Instructions:
2318 * EVAL arg1
2319 * EVAL arg2
2320 * BCALL / DCALL / UCALL
2321 */
2322 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002323compile_call(
2324 char_u **arg,
2325 size_t varlen,
2326 cctx_T *cctx,
2327 ppconst_T *ppconst,
2328 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002329{
2330 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002331 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002332 int argcount = argcount_init;
2333 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002334 char_u fname_buf[FLEN_FIXED + 1];
2335 char_u *tofree = NULL;
2336 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002337 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002338 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002339 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002340
Bram Moolenaara5565e42020-05-09 15:44:01 +02002341 // we can evaluate "has('name')" at compile time
2342 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2343 {
2344 char_u *s = skipwhite(*arg + varlen + 1);
2345 typval_T argvars[2];
2346
2347 argvars[0].v_type = VAR_UNKNOWN;
2348 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002349 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002350 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002351 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002352 s = skipwhite(s);
2353 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2354 {
2355 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2356
2357 *arg = s + 1;
2358 argvars[1].v_type = VAR_UNKNOWN;
2359 tv->v_type = VAR_NUMBER;
2360 tv->vval.v_number = 0;
2361 f_has(argvars, tv);
2362 clear_tv(&argvars[0]);
2363 ++ppconst->pp_used;
2364 return OK;
2365 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002366 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002367 }
2368
2369 if (generate_ppconst(cctx, ppconst) == FAIL)
2370 return FAIL;
2371
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372 if (varlen >= sizeof(namebuf))
2373 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002374 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002375 return FAIL;
2376 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002377 vim_strncpy(namebuf, *arg, varlen);
2378 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002379
2380 *arg = skipwhite(*arg + varlen + 1);
2381 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002382 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002383
Bram Moolenaara1773442020-08-12 15:21:22 +02002384 is_autoload = vim_strchr(name, '#') != NULL;
2385 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002386 {
2387 int idx;
2388
2389 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002390 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002392 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002393 else
2394 semsg(_(e_unknownfunc), namebuf);
2395 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002396 }
2397
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002398 // If we can find the function by name generate the right call.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002399 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002400 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar0f769812020-09-12 18:32:34 +02002401 if (ufunc != NULL && !func_is_global(ufunc))
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002402 {
2403 res = generate_CALL(cctx, ufunc, argcount);
2404 goto theend;
2405 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002406
2407 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002408 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002409 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002410 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002411 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002412 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002413 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002414 garray_T *stack = &cctx->ctx_type_stack;
2415 type_T *type;
2416
2417 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2418 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002419 goto theend;
2420 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002421
Bram Moolenaar0f769812020-09-12 18:32:34 +02002422 // If we can find a global function by name generate the right call.
2423 if (ufunc != NULL)
2424 {
2425 res = generate_CALL(cctx, ufunc, argcount);
2426 goto theend;
2427 }
2428
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002429 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002430 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002431 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002432 res = generate_UCALL(cctx, name, argcount);
2433 else
2434 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002435
2436theend:
2437 vim_free(tofree);
2438 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002439}
2440
2441// like NAMESPACE_CHAR but with 'a' and 'l'.
2442#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2443
2444/*
2445 * Find the end of a variable or function name. Unlike find_name_end() this
2446 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002447 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448 * Return a pointer to just after the name. Equal to "arg" if there is no
2449 * valid name.
2450 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002451 static char_u *
2452to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002453{
2454 char_u *p;
2455
2456 // Quick check for valid starting character.
2457 if (!eval_isnamec1(*arg))
2458 return arg;
2459
2460 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2461 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2462 // and can be used in slice "[n:]".
2463 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002464 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002465 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2466 break;
2467 return p;
2468}
2469
2470/*
2471 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002472 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 */
2474 char_u *
2475to_name_const_end(char_u *arg)
2476{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002477 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002478 typval_T rettv;
2479
2480 if (p == arg && *arg == '[')
2481 {
2482
2483 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002484 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002485 p = arg;
2486 }
2487 else if (p == arg && *arg == '#' && arg[1] == '{')
2488 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002489 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002491 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 p = arg;
2493 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002494
2495 return p;
2496}
2497
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498/*
2499 * parse a list: [expr, expr]
2500 * "*arg" points to the '['.
2501 */
2502 static int
2503compile_list(char_u **arg, cctx_T *cctx)
2504{
2505 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002506 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507 int count = 0;
2508
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002509 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002511 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002512 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002513 semsg(_(e_list_end), *arg);
2514 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002515 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002516 if (*p == ',')
2517 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002518 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002519 return FAIL;
2520 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002521 if (*p == ']')
2522 {
2523 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002524 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002525 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002526 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 break;
2528 ++count;
2529 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002530 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002532 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2533 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002534 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002535 return FAIL;
2536 }
2537 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002538 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539 p = skipwhite(p);
2540 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002541 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542
2543 generate_NEWLIST(cctx, count);
2544 return OK;
2545}
2546
2547/*
2548 * parse a lambda: {arg, arg -> expr}
2549 * "*arg" points to the '{'.
2550 */
2551 static int
2552compile_lambda(char_u **arg, cctx_T *cctx)
2553{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554 typval_T rettv;
2555 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002556 evalarg_T evalarg;
2557
2558 CLEAR_FIELD(evalarg);
2559 evalarg.eval_flags = EVAL_EVALUATE;
2560 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561
2562 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002563 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002565
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002567 ++ufunc->uf_refcount;
2568 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002569 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002570
2571 // The function will have one line: "return {expr}".
2572 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002573 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002575 clear_evalarg(&evalarg, NULL);
2576
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002577 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002578 {
2579 // The return type will now be known.
2580 set_function_type(ufunc);
2581
2582 return generate_FUNCREF(cctx, ufunc);
2583 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002584
2585 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586 return FAIL;
2587}
2588
2589/*
2590 * Compile a lamda call: expr->{lambda}(args)
2591 * "arg" points to the "{".
2592 */
2593 static int
2594compile_lambda_call(char_u **arg, cctx_T *cctx)
2595{
2596 ufunc_T *ufunc;
2597 typval_T rettv;
2598 int argcount = 1;
2599 int ret = FAIL;
2600
2601 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002602 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603 return FAIL;
2604
2605 if (**arg != '(')
2606 {
2607 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002608 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609 else
2610 semsg(_(e_missing_paren), "lambda");
2611 clear_tv(&rettv);
2612 return FAIL;
2613 }
2614
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002615 ufunc = rettv.vval.v_partial->pt_func;
2616 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002617 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002618 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002619
2620 // The function will have one line: "return {expr}".
2621 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002622 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002623
2624 // compile the arguments
2625 *arg = skipwhite(*arg + 1);
2626 if (compile_arguments(arg, cctx, &argcount) == OK)
2627 // call the compiled function
2628 ret = generate_CALL(cctx, ufunc, argcount);
2629
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002630 if (ret == FAIL)
2631 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632 return ret;
2633}
2634
2635/*
2636 * parse a dict: {'key': val} or #{key: val}
2637 * "*arg" points to the '{'.
2638 */
2639 static int
2640compile_dict(char_u **arg, cctx_T *cctx, int literal)
2641{
2642 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002643 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 int count = 0;
2645 dict_T *d = dict_alloc();
2646 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002647 char_u *whitep = *arg;
2648 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649
2650 if (d == NULL)
2651 return FAIL;
2652 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002653 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002654 {
2655 char_u *key = NULL;
2656
Bram Moolenaar23c55272020-06-21 16:58:13 +02002657 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002658 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002659 *arg = NULL;
2660 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002661 }
2662
2663 if (**arg == '}')
2664 break;
2665
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666 if (literal)
2667 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002668 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002669
Bram Moolenaar2c330432020-04-13 14:41:35 +02002670 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002671 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002672 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002673 return FAIL;
2674 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002675 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676 if (generate_PUSHS(cctx, key) == FAIL)
2677 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002678 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002679 }
2680 else
2681 {
2682 isn_T *isn;
2683
Bram Moolenaara5565e42020-05-09 15:44:01 +02002684 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002685 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002686 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2687 if (isn->isn_type == ISN_PUSHS)
2688 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002689 else
2690 {
2691 type_T *keytype = ((type_T **)stack->ga_data)
2692 [stack->ga_len - 1];
2693 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2694 return FAIL;
2695 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002696 }
2697
2698 // Check for duplicate keys, if using string keys.
2699 if (key != NULL)
2700 {
2701 item = dict_find(d, key, -1);
2702 if (item != NULL)
2703 {
2704 semsg(_(e_duplicate_key), key);
2705 goto failret;
2706 }
2707 item = dictitem_alloc(key);
2708 if (item != NULL)
2709 {
2710 item->di_tv.v_type = VAR_UNKNOWN;
2711 item->di_tv.v_lock = 0;
2712 if (dict_add(d, item) == FAIL)
2713 dictitem_free(item);
2714 }
2715 }
2716
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002717 if (**arg != ':')
2718 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002719 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002720 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002721 else
2722 semsg(_(e_missing_dict_colon), *arg);
2723 return FAIL;
2724 }
2725 whitep = *arg + 1;
2726 if (!IS_WHITE_OR_NUL(*whitep))
2727 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002728 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002729 return FAIL;
2730 }
2731
2732 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002733 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002734 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002735 *arg = NULL;
2736 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002737 }
2738
Bram Moolenaara5565e42020-05-09 15:44:01 +02002739 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002740 return FAIL;
2741 ++count;
2742
Bram Moolenaar2c330432020-04-13 14:41:35 +02002743 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002744 *arg = skipwhite(*arg);
2745 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002746 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002747 *arg = NULL;
2748 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002749 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002750 if (**arg == '}')
2751 break;
2752 if (**arg != ',')
2753 {
2754 semsg(_(e_missing_dict_comma), *arg);
2755 goto failret;
2756 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002757 if (IS_WHITE_OR_NUL(*whitep))
2758 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002759 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002760 return FAIL;
2761 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002762 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002763 *arg = skipwhite(*arg + 1);
2764 }
2765
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766 *arg = *arg + 1;
2767
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002768 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002769 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002770 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002771 *arg += STRLEN(*arg);
2772
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 dict_unref(d);
2774 return generate_NEWDICT(cctx, count);
2775
2776failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002777 if (*arg == NULL)
2778 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002779 dict_unref(d);
2780 return FAIL;
2781}
2782
2783/*
2784 * Compile "&option".
2785 */
2786 static int
2787compile_get_option(char_u **arg, cctx_T *cctx)
2788{
2789 typval_T rettv;
2790 char_u *start = *arg;
2791 int ret;
2792
2793 // parse the option and get the current value to get the type.
2794 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002795 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796 if (ret == OK)
2797 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002798 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799 char_u *name = vim_strnsave(start, *arg - start);
2800 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2801
2802 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2803 vim_free(name);
2804 }
2805 clear_tv(&rettv);
2806
2807 return ret;
2808}
2809
2810/*
2811 * Compile "$VAR".
2812 */
2813 static int
2814compile_get_env(char_u **arg, cctx_T *cctx)
2815{
2816 char_u *start = *arg;
2817 int len;
2818 int ret;
2819 char_u *name;
2820
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821 ++*arg;
2822 len = get_env_len(arg);
2823 if (len == 0)
2824 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002825 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826 return FAIL;
2827 }
2828
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002829 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830 name = vim_strnsave(start, len + 1);
2831 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2832 vim_free(name);
2833 return ret;
2834}
2835
2836/*
2837 * Compile "@r".
2838 */
2839 static int
2840compile_get_register(char_u **arg, cctx_T *cctx)
2841{
2842 int ret;
2843
2844 ++*arg;
2845 if (**arg == NUL)
2846 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002847 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848 return FAIL;
2849 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002850 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 {
2852 emsg_invreg(**arg);
2853 return FAIL;
2854 }
2855 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2856 ++*arg;
2857 return ret;
2858}
2859
2860/*
2861 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002862 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 */
2864 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002865apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002867 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002868
2869 // this works from end to start
2870 while (p > start)
2871 {
2872 --p;
2873 if (*p == '-' || *p == '+')
2874 {
2875 // only '-' has an effect, for '+' we only check the type
2876#ifdef FEAT_FLOAT
2877 if (rettv->v_type == VAR_FLOAT)
2878 {
2879 if (*p == '-')
2880 rettv->vval.v_float = -rettv->vval.v_float;
2881 }
2882 else
2883#endif
2884 {
2885 varnumber_T val;
2886 int error = FALSE;
2887
2888 // tv_get_number_chk() accepts a string, but we don't want that
2889 // here
2890 if (check_not_string(rettv) == FAIL)
2891 return FAIL;
2892 val = tv_get_number_chk(rettv, &error);
2893 clear_tv(rettv);
2894 if (error)
2895 return FAIL;
2896 if (*p == '-')
2897 val = -val;
2898 rettv->v_type = VAR_NUMBER;
2899 rettv->vval.v_number = val;
2900 }
2901 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002902 else if (numeric_only)
2903 {
2904 ++p;
2905 break;
2906 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907 else
2908 {
2909 int v = tv2bool(rettv);
2910
2911 // '!' is permissive in the type.
2912 clear_tv(rettv);
2913 rettv->v_type = VAR_BOOL;
2914 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2915 }
2916 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002917 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 return OK;
2919}
2920
2921/*
2922 * Recognize v: variables that are constants and set "rettv".
2923 */
2924 static void
2925get_vim_constant(char_u **arg, typval_T *rettv)
2926{
2927 if (STRNCMP(*arg, "v:true", 6) == 0)
2928 {
2929 rettv->v_type = VAR_BOOL;
2930 rettv->vval.v_number = VVAL_TRUE;
2931 *arg += 6;
2932 }
2933 else if (STRNCMP(*arg, "v:false", 7) == 0)
2934 {
2935 rettv->v_type = VAR_BOOL;
2936 rettv->vval.v_number = VVAL_FALSE;
2937 *arg += 7;
2938 }
2939 else if (STRNCMP(*arg, "v:null", 6) == 0)
2940 {
2941 rettv->v_type = VAR_SPECIAL;
2942 rettv->vval.v_number = VVAL_NULL;
2943 *arg += 6;
2944 }
2945 else if (STRNCMP(*arg, "v:none", 6) == 0)
2946 {
2947 rettv->v_type = VAR_SPECIAL;
2948 rettv->vval.v_number = VVAL_NONE;
2949 *arg += 6;
2950 }
2951}
2952
Bram Moolenaar696ba232020-07-29 21:20:41 +02002953 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02002954get_compare_type(char_u *p, int *len, int *type_is)
2955{
2956 exptype_T type = EXPR_UNKNOWN;
2957 int i;
2958
2959 switch (p[0])
2960 {
2961 case '=': if (p[1] == '=')
2962 type = EXPR_EQUAL;
2963 else if (p[1] == '~')
2964 type = EXPR_MATCH;
2965 break;
2966 case '!': if (p[1] == '=')
2967 type = EXPR_NEQUAL;
2968 else if (p[1] == '~')
2969 type = EXPR_NOMATCH;
2970 break;
2971 case '>': if (p[1] != '=')
2972 {
2973 type = EXPR_GREATER;
2974 *len = 1;
2975 }
2976 else
2977 type = EXPR_GEQUAL;
2978 break;
2979 case '<': if (p[1] != '=')
2980 {
2981 type = EXPR_SMALLER;
2982 *len = 1;
2983 }
2984 else
2985 type = EXPR_SEQUAL;
2986 break;
2987 case 'i': if (p[1] == 's')
2988 {
2989 // "is" and "isnot"; but not a prefix of a name
2990 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2991 *len = 5;
2992 i = p[*len];
2993 if (!isalnum(i) && i != '_')
2994 {
2995 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2996 *type_is = TRUE;
2997 }
2998 }
2999 break;
3000 }
3001 return type;
3002}
3003
3004/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003006 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 */
3008 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003009compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003011 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012
3013 // this works from end to start
3014 while (p > start)
3015 {
3016 --p;
3017 if (*p == '-' || *p == '+')
3018 {
3019 int negate = *p == '-';
3020 isn_T *isn;
3021
3022 // TODO: check type
3023 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3024 {
3025 --p;
3026 if (*p == '-')
3027 negate = !negate;
3028 }
3029 // only '-' has an effect, for '+' we only check the type
3030 if (negate)
3031 isn = generate_instr(cctx, ISN_NEGATENR);
3032 else
3033 isn = generate_instr(cctx, ISN_CHECKNR);
3034 if (isn == NULL)
3035 return FAIL;
3036 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003037 else if (numeric_only)
3038 {
3039 ++p;
3040 break;
3041 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042 else
3043 {
3044 int invert = TRUE;
3045
3046 while (p > start && p[-1] == '!')
3047 {
3048 --p;
3049 invert = !invert;
3050 }
3051 if (generate_2BOOL(cctx, invert) == FAIL)
3052 return FAIL;
3053 }
3054 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003055 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 return OK;
3057}
3058
3059/*
3060 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003061 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062 */
3063 static int
3064compile_subscript(
3065 char_u **arg,
3066 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003067 char_u *start_leader,
3068 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003069 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003071 char_u *name_start = *end_leader;
3072
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003073 for (;;)
3074 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003075 char_u *p = skipwhite(*arg);
3076
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003077 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003078 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003079 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003080
3081 // If a following line starts with "->{" or "->X" advance to that
3082 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003083 // Also if a following line starts with ".x".
3084 if (next != NULL &&
3085 ((next[0] == '-' && next[1] == '>'
3086 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003087 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003088 {
3089 next = next_line_from_context(cctx, TRUE);
3090 if (next == NULL)
3091 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003092 *arg = next;
3093 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003094 }
3095 }
3096
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003097 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3098 // not a function call.
3099 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003101 garray_T *stack = &cctx->ctx_type_stack;
3102 type_T *type;
3103 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003105 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003106 return FAIL;
3107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003109 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3110
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003111 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3113 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003114 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 return FAIL;
3116 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003117 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003119 char_u *pstart = p;
3120
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003121 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003122 return FAIL;
3123
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003124 // something->method()
3125 // Apply the '!', '-' and '+' first:
3126 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003127 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003130 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003131 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003132 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 if (**arg == '{')
3134 {
3135 // lambda call: list->{lambda}
3136 if (compile_lambda_call(arg, cctx) == FAIL)
3137 return FAIL;
3138 }
3139 else
3140 {
3141 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003142 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003143 if (!eval_isnamec1(*p))
3144 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003145 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003146 return FAIL;
3147 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003148 if (ASCII_ISALPHA(*p) && p[1] == ':')
3149 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003150 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151 ;
3152 if (*p != '(')
3153 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003154 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155 return FAIL;
3156 }
3157 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003158 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 return FAIL;
3160 }
3161 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003162 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003164 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003165 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003166 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003167 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003168 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003169
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003170 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003171 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003172 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003173 // TODO: blob index
3174 // TODO: more arguments
3175 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003176 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003177 return FAIL;
3178
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003179 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003180 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003181 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003182 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003183 if (**arg == ':')
3184 // missing first index is equal to zero
3185 generate_PUSHNR(cctx, 0);
3186 else
3187 {
3188 if (compile_expr0(arg, cctx) == FAIL)
3189 return FAIL;
3190 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3191 return FAIL;
3192 *arg = skipwhite(*arg);
3193 }
3194 if (**arg == ':')
3195 {
3196 *arg = skipwhite(*arg + 1);
3197 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3198 return FAIL;
3199 if (**arg == ']')
3200 // missing second index is equal to end of string
3201 generate_PUSHNR(cctx, -1);
3202 else
3203 {
3204 if (compile_expr0(arg, cctx) == FAIL)
3205 return FAIL;
3206 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3207 return FAIL;
3208 *arg = skipwhite(*arg);
3209 }
3210 is_slice = TRUE;
3211 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212
3213 if (**arg != ']')
3214 {
3215 emsg(_(e_missbrac));
3216 return FAIL;
3217 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003218 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003220 // We can index a list and a dict. If we don't know the type
3221 // we can use the index value type.
3222 // TODO: If we don't know use an instruction to figure it out at
3223 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003224 typep = ((type_T **)stack->ga_data) + stack->ga_len
3225 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003226 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003227 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3228 // If the index is a string, the variable must be a Dict.
3229 if (*typep == &t_any && valtype == &t_string)
3230 vtype = VAR_DICT;
3231 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003232 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003233 if (need_type(valtype, &t_number, -1, cctx, FALSE) == FAIL)
3234 return FAIL;
3235 if (is_slice)
3236 {
3237 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
3238 if (need_type(valtype, &t_number, -2, cctx, FALSE) == FAIL)
3239 return FAIL;
3240 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003241 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003242
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003243 if (vtype == VAR_DICT)
3244 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003245 if (is_slice)
3246 {
3247 emsg(_(e_cannot_slice_dictionary));
3248 return FAIL;
3249 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003250 if ((*typep)->tt_type == VAR_DICT)
3251 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003252 else
3253 {
3254 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3255 return FAIL;
3256 *typep = &t_any;
3257 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003258 if (may_generate_2STRING(-1, cctx) == FAIL)
3259 return FAIL;
3260 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3261 return FAIL;
3262 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003263 else if (vtype == VAR_STRING)
3264 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003265 *typep = &t_string;
3266 if ((is_slice
3267 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3268 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003269 return FAIL;
3270 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003271 else if (vtype == VAR_BLOB)
3272 {
3273 emsg("Sorry, blob index and slice not implemented yet");
3274 return FAIL;
3275 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003276 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003277 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003278 if (is_slice)
3279 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003280 if (generate_instr_drop(cctx,
3281 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3282 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003283 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003284 }
Bram Moolenaared591872020-08-15 22:14:53 +02003285 else
3286 {
3287 if ((*typep)->tt_type == VAR_LIST)
3288 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003289 if (generate_instr_drop(cctx,
3290 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3291 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003292 return FAIL;
3293 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003294 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003295 else
3296 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003297 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003298 return FAIL;
3299 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003301 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003303 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003304 return FAIL;
3305
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003306 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003307 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3308 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003310 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003311 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 while (eval_isnamec(*p))
3313 MB_PTR_ADV(p);
3314 if (p == *arg)
3315 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003316 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317 return FAIL;
3318 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003319 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320 return FAIL;
3321 *arg = p;
3322 }
3323 else
3324 break;
3325 }
3326
3327 // TODO - see handle_subscript():
3328 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3329 // Don't do this when "Func" is already a partial that was bound
3330 // explicitly (pt_auto is FALSE).
3331
3332 return OK;
3333}
3334
3335/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003336 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3337 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003338 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003339 * If the value is a constant "ppconst->pp_ret" will be set.
3340 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003341 *
3342 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343 */
3344
3345/*
3346 * number number constant
3347 * 0zFFFFFFFF Blob constant
3348 * "string" string constant
3349 * 'string' literal string constant
3350 * &option-name option value
3351 * @r register contents
3352 * identifier variable value
3353 * function() function call
3354 * $VAR environment variable
3355 * (expression) nested expression
3356 * [expr, expr] List
3357 * {key: val, key: val} Dictionary
3358 * #{key: val, key: val} Dictionary with literal keys
3359 *
3360 * Also handle:
3361 * ! in front logical NOT
3362 * - in front unary minus
3363 * + in front unary plus (ignored)
3364 * trailing (arg) funcref/partial call
3365 * trailing [] subscript in String or List
3366 * trailing .name entry in Dictionary
3367 * trailing ->name() method call
3368 */
3369 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003370compile_expr7(
3371 char_u **arg,
3372 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003373 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 char_u *start_leader, *end_leader;
3376 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003377 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003378 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379
3380 /*
3381 * Skip '!', '-' and '+' characters. They are handled later.
3382 */
3383 start_leader = *arg;
3384 while (**arg == '!' || **arg == '-' || **arg == '+')
3385 *arg = skipwhite(*arg + 1);
3386 end_leader = *arg;
3387
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003388 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389 switch (**arg)
3390 {
3391 /*
3392 * Number constant.
3393 */
3394 case '0': // also for blob starting with 0z
3395 case '1':
3396 case '2':
3397 case '3':
3398 case '4':
3399 case '5':
3400 case '6':
3401 case '7':
3402 case '8':
3403 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003404 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003406 // Apply "-" and "+" just before the number now, right to
3407 // left. Matters especially when "->" follows. Stops at
3408 // '!'.
3409 if (apply_leader(rettv, TRUE,
3410 start_leader, &end_leader) == FAIL)
3411 {
3412 clear_tv(rettv);
3413 return FAIL;
3414 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415 break;
3416
3417 /*
3418 * String constant: "string".
3419 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003420 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421 return FAIL;
3422 break;
3423
3424 /*
3425 * Literal string constant: 'str''ing'.
3426 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003427 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428 return FAIL;
3429 break;
3430
3431 /*
3432 * Constant Vim variable.
3433 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003434 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 ret = NOTDONE;
3436 break;
3437
3438 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003439 * "true" constant
3440 */
3441 case 't': if (STRNCMP(*arg, "true", 4) == 0
3442 && !eval_isnamec((*arg)[4]))
3443 {
3444 *arg += 4;
3445 rettv->v_type = VAR_BOOL;
3446 rettv->vval.v_number = VVAL_TRUE;
3447 }
3448 else
3449 ret = NOTDONE;
3450 break;
3451
3452 /*
3453 * "false" constant
3454 */
3455 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3456 && !eval_isnamec((*arg)[5]))
3457 {
3458 *arg += 5;
3459 rettv->v_type = VAR_BOOL;
3460 rettv->vval.v_number = VVAL_FALSE;
3461 }
3462 else
3463 ret = NOTDONE;
3464 break;
3465
3466 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 * List: [expr, expr]
3468 */
3469 case '[': ret = compile_list(arg, cctx);
3470 break;
3471
3472 /*
3473 * Dictionary: #{key: val, key: val}
3474 */
3475 case '#': if ((*arg)[1] == '{')
3476 {
3477 ++*arg;
3478 ret = compile_dict(arg, cctx, TRUE);
3479 }
3480 else
3481 ret = NOTDONE;
3482 break;
3483
3484 /*
3485 * Lambda: {arg, arg -> expr}
3486 * Dictionary: {'key': val, 'key': val}
3487 */
3488 case '{': {
3489 char_u *start = skipwhite(*arg + 1);
3490
3491 // Find out what comes after the arguments.
3492 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003493 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494 if (ret != FAIL && *start == '>')
3495 ret = compile_lambda(arg, cctx);
3496 else
3497 ret = compile_dict(arg, cctx, FALSE);
3498 }
3499 break;
3500
3501 /*
3502 * Option value: &name
3503 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003504 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3505 return FAIL;
3506 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507 break;
3508
3509 /*
3510 * Environment variable: $VAR.
3511 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003512 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3513 return FAIL;
3514 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515 break;
3516
3517 /*
3518 * Register contents: @r.
3519 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003520 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3521 return FAIL;
3522 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 break;
3524 /*
3525 * nested expression: (expression).
3526 */
3527 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003528
3529 // recursive!
3530 if (ppconst->pp_used <= PPSIZE - 10)
3531 {
3532 ret = compile_expr1(arg, cctx, ppconst);
3533 }
3534 else
3535 {
3536 // Not enough space in ppconst, flush constants.
3537 if (generate_ppconst(cctx, ppconst) == FAIL)
3538 return FAIL;
3539 ret = compile_expr0(arg, cctx);
3540 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003541 *arg = skipwhite(*arg);
3542 if (**arg == ')')
3543 ++*arg;
3544 else if (ret == OK)
3545 {
3546 emsg(_(e_missing_close));
3547 ret = FAIL;
3548 }
3549 break;
3550
3551 default: ret = NOTDONE;
3552 break;
3553 }
3554 if (ret == FAIL)
3555 return FAIL;
3556
Bram Moolenaar1c747212020-05-09 18:28:34 +02003557 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003559 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003560 clear_tv(rettv);
3561 else
3562 // A constant expression can possibly be handled compile time,
3563 // return the value instead of generating code.
3564 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565 }
3566 else if (ret == NOTDONE)
3567 {
3568 char_u *p;
3569 int r;
3570
3571 if (!eval_isnamec1(**arg))
3572 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003573 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003574 return FAIL;
3575 }
3576
3577 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003578 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003579 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003580 {
3581 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3582 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003584 {
3585 if (generate_ppconst(cctx, ppconst) == FAIL)
3586 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003587 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003588 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 if (r == FAIL)
3590 return FAIL;
3591 }
3592
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003593 // Handle following "[]", ".member", etc.
3594 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003595 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003596 ppconst) == FAIL)
3597 return FAIL;
3598 if (ppconst->pp_used > 0)
3599 {
3600 // apply the '!', '-' and '+' before the constant
3601 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003602 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003603 return FAIL;
3604 return OK;
3605 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003606 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003608 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609}
3610
3611/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003612 * Give the "white on both sides" error, taking the operator from "p[len]".
3613 */
3614 void
3615error_white_both(char_u *op, int len)
3616{
3617 char_u buf[10];
3618
3619 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003620 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003621}
3622
3623/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003624 * <type>expr7: runtime type check / conversion
3625 */
3626 static int
3627compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3628{
3629 type_T *want_type = NULL;
3630
3631 // Recognize <type>
3632 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3633 {
3634 int called_emsg_before = called_emsg;
3635
3636 ++*arg;
3637 want_type = parse_type(arg, cctx->ctx_type_list);
3638 if (called_emsg != called_emsg_before)
3639 return FAIL;
3640
3641 if (**arg != '>')
3642 {
3643 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003644 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003645 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003646 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003647 return FAIL;
3648 }
3649 ++*arg;
3650 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3651 return FAIL;
3652 }
3653
3654 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3655 return FAIL;
3656
3657 if (want_type != NULL)
3658 {
3659 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003660 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003661
Bram Moolenaard1103582020-08-14 22:44:25 +02003662 generate_ppconst(cctx, ppconst);
3663 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003664 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003665 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003666 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3667 return FAIL;
3668 }
3669 }
3670
3671 return OK;
3672}
3673
3674/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 * * number multiplication
3676 * / number division
3677 * % number modulo
3678 */
3679 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003680compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681{
3682 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003683 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003684 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003686 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003687 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 return FAIL;
3689
3690 /*
3691 * Repeat computing, until no "*", "/" or "%" is following.
3692 */
3693 for (;;)
3694 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003695 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 if (*op != '*' && *op != '/' && *op != '%')
3697 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003698 if (next != NULL)
3699 {
3700 *arg = next_line_from_context(cctx, TRUE);
3701 op = skipwhite(*arg);
3702 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003703
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003704 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003706 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003707 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 }
3709 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003710 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003711 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003713 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003714 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003716
3717 if (ppconst->pp_used == ppconst_used + 2
3718 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3719 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003720 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003721 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3722 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003723 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003725 // both are numbers: compute the result
3726 switch (*op)
3727 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003728 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003729 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003730 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003731 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003732 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003733 break;
3734 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003735 tv1->vval.v_number = res;
3736 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003737 }
3738 else
3739 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003740 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003741 generate_two_op(cctx, op);
3742 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003743 }
3744
3745 return OK;
3746}
3747
3748/*
3749 * + number addition
3750 * - number subtraction
3751 * .. string concatenation
3752 */
3753 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003754compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003755{
3756 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003757 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003758 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003759 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760
3761 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003762 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003763 return FAIL;
3764
3765 /*
3766 * Repeat computing, until no "+", "-" or ".." is following.
3767 */
3768 for (;;)
3769 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003770 op = may_peek_next_line(cctx, *arg, &next);
3771 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 break;
3773 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003774 if (next != NULL)
3775 {
3776 *arg = next_line_from_context(cctx, TRUE);
3777 op = skipwhite(*arg);
3778 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003779
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003780 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003781 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003782 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003783 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 }
3785
3786 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003787 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003788 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003790 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003791 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792 return FAIL;
3793
Bram Moolenaara5565e42020-05-09 15:44:01 +02003794 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003795 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003796 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3797 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3798 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3799 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003801 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3802 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003803
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003804 // concat/subtract/add constant numbers
3805 if (*op == '+')
3806 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3807 else if (*op == '-')
3808 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3809 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003810 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003811 // concatenate constant strings
3812 char_u *s1 = tv1->vval.v_string;
3813 char_u *s2 = tv2->vval.v_string;
3814 size_t len1 = STRLEN(s1);
3815
3816 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3817 if (tv1->vval.v_string == NULL)
3818 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003819 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003820 return FAIL;
3821 }
3822 mch_memmove(tv1->vval.v_string, s1, len1);
3823 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003824 vim_free(s1);
3825 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003826 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003827 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 }
3829 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003830 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003831 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003832 if (*op == '.')
3833 {
3834 if (may_generate_2STRING(-2, cctx) == FAIL
3835 || may_generate_2STRING(-1, cctx) == FAIL)
3836 return FAIL;
3837 generate_instr_drop(cctx, ISN_CONCAT, 1);
3838 }
3839 else
3840 generate_two_op(cctx, op);
3841 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842 }
3843
3844 return OK;
3845}
3846
3847/*
3848 * expr5a == expr5b
3849 * expr5a =~ expr5b
3850 * expr5a != expr5b
3851 * expr5a !~ expr5b
3852 * expr5a > expr5b
3853 * expr5a >= expr5b
3854 * expr5a < expr5b
3855 * expr5a <= expr5b
3856 * expr5a is expr5b
3857 * expr5a isnot expr5b
3858 *
3859 * Produces instructions:
3860 * EVAL expr5a Push result of "expr5a"
3861 * EVAL expr5b Push result of "expr5b"
3862 * COMPARE one of the compare instructions
3863 */
3864 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003865compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003866{
3867 exptype_T type = EXPR_UNKNOWN;
3868 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003869 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003872 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873
3874 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003875 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003876 return FAIL;
3877
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003878 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003879 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880
3881 /*
3882 * If there is a comparative operator, use it.
3883 */
3884 if (type != EXPR_UNKNOWN)
3885 {
3886 int ic = FALSE; // Default: do not ignore case
3887
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003888 if (next != NULL)
3889 {
3890 *arg = next_line_from_context(cctx, TRUE);
3891 p = skipwhite(*arg);
3892 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893 if (type_is && (p[len] == '?' || p[len] == '#'))
3894 {
3895 semsg(_(e_invexpr2), *arg);
3896 return FAIL;
3897 }
3898 // extra question mark appended: ignore case
3899 if (p[len] == '?')
3900 {
3901 ic = TRUE;
3902 ++len;
3903 }
3904 // extra '#' appended: match case (ignored)
3905 else if (p[len] == '#')
3906 ++len;
3907 // nothing appended: match case
3908
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003909 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003910 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003911 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003912 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913 }
3914
3915 // get the second variable
3916 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003917 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003918 return FAIL;
3919
Bram Moolenaara5565e42020-05-09 15:44:01 +02003920 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 return FAIL;
3922
Bram Moolenaara5565e42020-05-09 15:44:01 +02003923 if (ppconst->pp_used == ppconst_used + 2)
3924 {
3925 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3926 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3927 int ret;
3928
3929 // Both sides are a constant, compute the result now.
3930 // First check for a valid combination of types, this is more
3931 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003932 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003933 ret = FAIL;
3934 else
3935 {
3936 ret = typval_compare(tv1, tv2, type, ic);
3937 tv1->v_type = VAR_BOOL;
3938 tv1->vval.v_number = tv1->vval.v_number
3939 ? VVAL_TRUE : VVAL_FALSE;
3940 clear_tv(tv2);
3941 --ppconst->pp_used;
3942 }
3943 return ret;
3944 }
3945
3946 generate_ppconst(cctx, ppconst);
3947 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948 }
3949
3950 return OK;
3951}
3952
Bram Moolenaar7f141552020-05-09 17:35:53 +02003953static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3954
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955/*
3956 * Compile || or &&.
3957 */
3958 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003959compile_and_or(
3960 char_u **arg,
3961 cctx_T *cctx,
3962 char *op,
3963 ppconst_T *ppconst,
3964 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003966 char_u *next;
3967 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 int opchar = *op;
3969
3970 if (p[0] == opchar && p[1] == opchar)
3971 {
3972 garray_T *instr = &cctx->ctx_instr;
3973 garray_T end_ga;
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02003974 garray_T *stack = &cctx->ctx_type_stack;
3975 type_T **typep;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976
3977 /*
3978 * Repeat until there is no following "||" or "&&"
3979 */
3980 ga_init2(&end_ga, sizeof(int), 10);
3981 while (p[0] == opchar && p[1] == opchar)
3982 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003983 if (next != NULL)
3984 {
3985 *arg = next_line_from_context(cctx, TRUE);
3986 p = skipwhite(*arg);
3987 }
3988
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003989 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3990 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003991 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003992 return FAIL;
3993 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994
Bram Moolenaara5565e42020-05-09 15:44:01 +02003995 // TODO: use ppconst if the value is a constant
3996 generate_ppconst(cctx, ppconst);
3997
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003998 if (ga_grow(&end_ga, 1) == FAIL)
3999 {
4000 ga_clear(&end_ga);
4001 return FAIL;
4002 }
4003 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4004 ++end_ga.ga_len;
4005 generate_JUMP(cctx, opchar == '|'
4006 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4007
4008 // eval the next expression
4009 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004010 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004011 return FAIL;
4012
Bram Moolenaara5565e42020-05-09 15:44:01 +02004013 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4014 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004015 {
4016 ga_clear(&end_ga);
4017 return FAIL;
4018 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004019
4020 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004022 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023
4024 // Fill in the end label in all jumps.
4025 while (end_ga.ga_len > 0)
4026 {
4027 isn_T *isn;
4028
4029 --end_ga.ga_len;
4030 isn = ((isn_T *)instr->ga_data)
4031 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4032 isn->isn_arg.jump.jump_where = instr->ga_len;
4033 }
4034 ga_clear(&end_ga);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004035
4036 // The resulting type can be used as a bool.
4037 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4038 if (*typep != &t_bool)
4039 {
4040 type_T *type = alloc_type(cctx->ctx_type_list);
4041
4042 if (type != NULL)
4043 {
4044 *type = **typep;
4045 type->tt_flags |= TTFLAG_BOOL_OK;
4046 *typep = type;
4047 }
4048 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049 }
4050
4051 return OK;
4052}
4053
4054/*
4055 * expr4a && expr4a && expr4a logical AND
4056 *
4057 * Produces instructions:
4058 * EVAL expr4a Push result of "expr4a"
4059 * JUMP_AND_KEEP_IF_FALSE end
4060 * EVAL expr4b Push result of "expr4b"
4061 * JUMP_AND_KEEP_IF_FALSE end
4062 * EVAL expr4c Push result of "expr4c"
4063 * end:
4064 */
4065 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004066compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004067{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004068 int ppconst_used = ppconst->pp_used;
4069
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004071 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004072 return FAIL;
4073
4074 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004075 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004076}
4077
4078/*
4079 * expr3a || expr3b || expr3c logical OR
4080 *
4081 * Produces instructions:
4082 * EVAL expr3a Push result of "expr3a"
4083 * JUMP_AND_KEEP_IF_TRUE end
4084 * EVAL expr3b Push result of "expr3b"
4085 * JUMP_AND_KEEP_IF_TRUE end
4086 * EVAL expr3c Push result of "expr3c"
4087 * end:
4088 */
4089 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004090compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004092 int ppconst_used = ppconst->pp_used;
4093
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004094 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004095 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 return FAIL;
4097
4098 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004099 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004100}
4101
4102/*
4103 * Toplevel expression: expr2 ? expr1a : expr1b
4104 *
4105 * Produces instructions:
4106 * EVAL expr2 Push result of "expr"
4107 * JUMP_IF_FALSE alt jump if false
4108 * EVAL expr1a
4109 * JUMP_ALWAYS end
4110 * alt: EVAL expr1b
4111 * end:
4112 */
4113 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004114compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115{
4116 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004117 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004118 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119
Bram Moolenaar3988f642020-08-27 22:43:03 +02004120 // Ignore all kinds of errors when not producing code.
4121 if (cctx->ctx_skip == SKIP_YES)
4122 {
4123 skip_expr(arg);
4124 return OK;
4125 }
4126
Bram Moolenaar61a89812020-05-07 16:58:17 +02004127 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004128 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 return FAIL;
4130
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004131 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004132 if (*p == '?')
4133 {
4134 garray_T *instr = &cctx->ctx_instr;
4135 garray_T *stack = &cctx->ctx_type_stack;
4136 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004137 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004139 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004141 int has_const_expr = FALSE;
4142 int const_value = FALSE;
4143 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004145 if (next != NULL)
4146 {
4147 *arg = next_line_from_context(cctx, TRUE);
4148 p = skipwhite(*arg);
4149 }
4150
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004151 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4152 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004153 semsg(_(e_white_space_required_before_and_after_str), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004154 return FAIL;
4155 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004156
Bram Moolenaara5565e42020-05-09 15:44:01 +02004157 if (ppconst->pp_used == ppconst_used + 1)
4158 {
4159 // the condition is a constant, we know whether the ? or the :
4160 // expression is to be evaluated.
4161 has_const_expr = TRUE;
4162 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4163 clear_tv(&ppconst->pp_tv[ppconst_used]);
4164 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004165 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4166 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004167 }
4168 else
4169 {
4170 generate_ppconst(cctx, ppconst);
4171 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4172 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004173
4174 // evaluate the second expression; any type is accepted
4175 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004176 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004177 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004178 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004179 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004180
Bram Moolenaara5565e42020-05-09 15:44:01 +02004181 if (!has_const_expr)
4182 {
4183 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184
Bram Moolenaara5565e42020-05-09 15:44:01 +02004185 // remember the type and drop it
4186 --stack->ga_len;
4187 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004188
Bram Moolenaara5565e42020-05-09 15:44:01 +02004189 end_idx = instr->ga_len;
4190 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4191
4192 // jump here from JUMP_IF_FALSE
4193 isn = ((isn_T *)instr->ga_data) + alt_idx;
4194 isn->isn_arg.jump.jump_where = instr->ga_len;
4195 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196
4197 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004198 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004199 if (*p != ':')
4200 {
4201 emsg(_(e_missing_colon));
4202 return FAIL;
4203 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004204 if (next != NULL)
4205 {
4206 *arg = next_line_from_context(cctx, TRUE);
4207 p = skipwhite(*arg);
4208 }
4209
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004210 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4211 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004212 semsg(_(e_white_space_required_before_and_after_str), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004213 return FAIL;
4214 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215
4216 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004217 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004218 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4219 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004221 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004222 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004223 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004224 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004225
Bram Moolenaara5565e42020-05-09 15:44:01 +02004226 if (!has_const_expr)
4227 {
4228 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229
Bram Moolenaara5565e42020-05-09 15:44:01 +02004230 // If the types differ, the result has a more generic type.
4231 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4232 common_type(type1, type2, &type2, cctx->ctx_type_list);
4233
4234 // jump here from JUMP_ALWAYS
4235 isn = ((isn_T *)instr->ga_data) + end_idx;
4236 isn->isn_arg.jump.jump_where = instr->ga_len;
4237 }
4238
4239 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240 }
4241 return OK;
4242}
4243
4244/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004245 * Toplevel expression.
4246 */
4247 static int
4248compile_expr0(char_u **arg, cctx_T *cctx)
4249{
4250 ppconst_T ppconst;
4251
4252 CLEAR_FIELD(ppconst);
4253 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4254 {
4255 clear_ppconst(&ppconst);
4256 return FAIL;
4257 }
4258 if (generate_ppconst(cctx, &ppconst) == FAIL)
4259 return FAIL;
4260 return OK;
4261}
4262
4263/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264 * compile "return [expr]"
4265 */
4266 static char_u *
4267compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4268{
4269 char_u *p = arg;
4270 garray_T *stack = &cctx->ctx_type_stack;
4271 type_T *stack_type;
4272
4273 if (*p != NUL && *p != '|' && *p != '\n')
4274 {
4275 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004276 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277 return NULL;
4278
4279 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4280 if (set_return_type)
4281 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004282 else
4283 {
4284 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4285 && stack_type->tt_type != VAR_VOID
4286 && stack_type->tt_type != VAR_UNKNOWN)
4287 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004288 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004289 return NULL;
4290 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004291 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4292 cctx, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004293 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004294 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004295 }
4296 else
4297 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004298 // "set_return_type" cannot be TRUE, only used for a lambda which
4299 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004300 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4301 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004303 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 return NULL;
4305 }
4306
4307 // No argument, return zero.
4308 generate_PUSHNR(cctx, 0);
4309 }
4310
4311 if (generate_instr(cctx, ISN_RETURN) == NULL)
4312 return NULL;
4313
4314 // "return val | endif" is possible
4315 return skipwhite(p);
4316}
4317
4318/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004319 * Get a line from the compilation context, compatible with exarg_T getline().
4320 * Return a pointer to the line in allocated memory.
4321 * Return NULL for end-of-file or some error.
4322 */
4323 static char_u *
4324exarg_getline(
4325 int c UNUSED,
4326 void *cookie,
4327 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004328 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004329{
4330 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004331 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004332
Bram Moolenaar66250c92020-08-20 15:02:42 +02004333 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004334 {
Bram Moolenaar66250c92020-08-20 15:02:42 +02004335 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4336 return NULL;
4337 ++cctx->ctx_lnum;
4338 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4339 // Comment lines result in NULL pointers, skip them.
4340 if (p != NULL)
4341 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004342 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004343}
4344
4345/*
4346 * Compile a nested :def command.
4347 */
4348 static char_u *
4349compile_nested_function(exarg_T *eap, cctx_T *cctx)
4350{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004351 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004352 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004353 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004354 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004355 lvar_T *lvar;
4356 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004357 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004358
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004359 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004360 {
4361 emsg(_(e_cannot_use_bang_with_nested_def));
4362 return NULL;
4363 }
4364
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004365 // Only g:Func() can use a namespace.
4366 if (name_start[1] == ':' && !is_global)
4367 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004368 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004369 return NULL;
4370 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004371 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4372 return NULL;
4373
Bram Moolenaar04b12692020-05-04 23:24:44 +02004374 eap->arg = name_end;
4375 eap->getline = exarg_getline;
4376 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004377 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004378 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004379 lambda_name = get_lambda_name();
4380 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004381
Bram Moolenaar822ba242020-05-24 23:00:18 +02004382 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004383 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004384 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004385 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004386 return NULL;
4387
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004388 if (is_global)
4389 {
4390 char_u *func_name = vim_strnsave(name_start + 2,
4391 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004392
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004393 if (func_name == NULL)
4394 r = FAIL;
4395 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004396 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004397 }
4398 else
4399 {
4400 // Define a local variable for the function reference.
4401 lvar = reserve_local(cctx, name_start, name_end - name_start,
4402 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004403 if (lvar == NULL)
4404 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004405 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004406 return NULL;
4407 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4408 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004409
Bram Moolenaar61a89812020-05-07 16:58:17 +02004410 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004411 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004412}
4413
4414/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004415 * Return the length of an assignment operator, or zero if there isn't one.
4416 */
4417 int
4418assignment_len(char_u *p, int *heredoc)
4419{
4420 if (*p == '=')
4421 {
4422 if (p[1] == '<' && p[2] == '<')
4423 {
4424 *heredoc = TRUE;
4425 return 3;
4426 }
4427 return 1;
4428 }
4429 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4430 return 2;
4431 if (STRNCMP(p, "..=", 3) == 0)
4432 return 3;
4433 return 0;
4434}
4435
4436// words that cannot be used as a variable
4437static char *reserved[] = {
4438 "true",
4439 "false",
4440 NULL
4441};
4442
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004443typedef enum {
4444 dest_local,
4445 dest_option,
4446 dest_env,
4447 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004448 dest_buffer,
4449 dest_window,
4450 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004451 dest_vimvar,
4452 dest_script,
4453 dest_reg,
4454} assign_dest_T;
4455
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004456/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004457 * Generate the load instruction for "name".
4458 */
4459 static void
4460generate_loadvar(
4461 cctx_T *cctx,
4462 assign_dest_T dest,
4463 char_u *name,
4464 lvar_T *lvar,
4465 type_T *type)
4466{
4467 switch (dest)
4468 {
4469 case dest_option:
4470 // TODO: check the option exists
4471 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4472 break;
4473 case dest_global:
4474 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4475 break;
4476 case dest_buffer:
4477 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4478 break;
4479 case dest_window:
4480 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4481 break;
4482 case dest_tab:
4483 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4484 break;
4485 case dest_script:
4486 compile_load_scriptvar(cctx,
4487 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4488 break;
4489 case dest_env:
4490 // Include $ in the name here
4491 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4492 break;
4493 case dest_reg:
4494 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4495 break;
4496 case dest_vimvar:
4497 generate_LOADV(cctx, name + 2, TRUE);
4498 break;
4499 case dest_local:
4500 if (lvar->lv_from_outer)
4501 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4502 NULL, type);
4503 else
4504 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4505 break;
4506 }
4507}
4508
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004509 void
4510vim9_declare_error(char_u *name)
4511{
4512 char *scope = "";
4513
4514 switch (*name)
4515 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004516 case 'g': scope = _("global"); break;
4517 case 'b': scope = _("buffer"); break;
4518 case 'w': scope = _("window"); break;
4519 case 't': scope = _("tab"); break;
4520 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004521 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004522 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004523 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004524 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004525 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004526 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004527 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004528 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004529 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004530}
4531
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004532/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004533 * Compile declaration and assignment:
4534 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004535 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004536 * Return NULL for an error.
4537 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004538 */
4539 static char_u *
4540compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4541{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004542 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004543 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004544 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 char_u *ret = NULL;
4546 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004547 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004548 int scriptvar_sid = 0;
4549 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004550 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004551 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004552 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004554 int oplen = 0;
4555 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004556 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004557 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004558 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004559 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004560 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004561
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004562 // Skip over the "var" or "[var, var]" to get to any "=".
4563 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4564 if (p == NULL)
4565 return *arg == '[' ? arg : NULL;
4566
4567 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004568 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004569 // TODO: should we allow this, and figure out type inference from list
4570 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004571 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004572 return NULL;
4573 }
4574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575 sp = p;
4576 p = skipwhite(p);
4577 op = p;
4578 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004579
4580 if (var_count > 0 && oplen == 0)
4581 // can be something like "[1, 2]->func()"
4582 return arg;
4583
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4585 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004586 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004587 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004588 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004589
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004590 if (heredoc)
4591 {
4592 list_T *l;
4593 listitem_T *li;
4594
4595 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004596 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004598 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004599
4600 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004601 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602 {
4603 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4604 li->li_tv.vval.v_string = NULL;
4605 }
4606 generate_NEWLIST(cctx, l->lv_len);
4607 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004608 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004609 list_free(l);
4610 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004611 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004612 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004613 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004615 // for "[var, var] = expr" evaluate the expression here, loop over the
4616 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004617
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004618 p = skipwhite(op + oplen);
4619 if (compile_expr0(&p, cctx) == FAIL)
4620 return NULL;
4621 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004623 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004624 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004625 type_T *stacktype;
4626
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004627 stacktype = stack->ga_len == 0 ? &t_void
4628 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004629 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004630 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004631 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004632 goto theend;
4633 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004634 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004635 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004636 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004637 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4638 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004639 }
4640 }
4641
4642 /*
4643 * Loop over variables in "[var, var] = expr".
4644 * For "var = expr" and "let var: type" this is done only once.
4645 */
4646 if (var_count > 0)
4647 var_start = skipwhite(arg + 1); // skip over the "["
4648 else
4649 var_start = arg;
4650 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4651 {
4652 char_u *var_end = skip_var_one(var_start, FALSE);
4653 size_t varlen;
4654 int new_local = FALSE;
4655 int opt_type;
4656 int opt_flags = 0;
4657 assign_dest_T dest = dest_local;
4658 int vimvaridx = -1;
4659 lvar_T *lvar = NULL;
4660 lvar_T arg_lvar;
4661 int has_type = FALSE;
4662 int has_index = FALSE;
4663 int instr_count = -1;
4664
Bram Moolenaar65821722020-08-02 18:58:54 +02004665 if (*var_start == '@')
4666 p = var_start + 2;
4667 else
4668 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02004669 // skip over the leading "&", "&l:", "&g:" and "$"
4670 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02004671 p = to_name_end(p, TRUE);
4672 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004673
4674 // "a: type" is declaring variable "a" with a type, not "a:".
4675 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4676 --var_end;
4677 if (is_decl && p == var_start + 2 && p[-1] == ':')
4678 --p;
4679
4680 varlen = p - var_start;
4681 vim_free(name);
4682 name = vim_strnsave(var_start, varlen);
4683 if (name == NULL)
4684 return NULL;
4685 if (!heredoc)
4686 type = &t_any;
4687
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004688 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004689 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004690 int declare_error = FALSE;
4691
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004692 if (*var_start == '&')
4693 {
4694 int cc;
4695 long numval;
4696
4697 dest = dest_option;
4698 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004699 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004700 emsg(_(e_const_option));
4701 goto theend;
4702 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004703 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004704 p = var_start;
4705 p = find_option_end(&p, &opt_flags);
4706 if (p == NULL)
4707 {
4708 // cannot happen?
4709 emsg(_(e_letunexp));
4710 goto theend;
4711 }
4712 cc = *p;
4713 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02004714 opt_type = get_option_value(skip_option_env_lead(var_start),
4715 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004716 *p = cc;
4717 if (opt_type == -3)
4718 {
4719 semsg(_(e_unknown_option), var_start);
4720 goto theend;
4721 }
4722 if (opt_type == -2 || opt_type == 0)
4723 type = &t_string;
4724 else
4725 type = &t_number; // both number and boolean option
4726 }
4727 else if (*var_start == '$')
4728 {
4729 dest = dest_env;
4730 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004731 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004732 }
4733 else if (*var_start == '@')
4734 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004735 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004736 {
4737 emsg_invreg(var_start[1]);
4738 goto theend;
4739 }
4740 dest = dest_reg;
4741 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004742 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004743 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004744 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004745 {
4746 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004747 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004748 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004749 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004750 {
4751 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004752 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004753 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004754 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004755 {
4756 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004757 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004758 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004759 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004760 {
4761 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004762 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004763 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004764 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004765 {
4766 typval_T *vtv;
4767 int di_flags;
4768
4769 vimvaridx = find_vim_var(name + 2, &di_flags);
4770 if (vimvaridx < 0)
4771 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004772 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004773 goto theend;
4774 }
4775 // We use the current value of "sandbox" here, is that OK?
4776 if (var_check_ro(di_flags, name, FALSE))
4777 goto theend;
4778 dest = dest_vimvar;
4779 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004780 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004781 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004782 }
4783 else
4784 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004785 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004786
4787 for (idx = 0; reserved[idx] != NULL; ++idx)
4788 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004789 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004790 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004791 goto theend;
4792 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004793
4794 lvar = lookup_local(var_start, varlen, cctx);
4795 if (lvar == NULL)
4796 {
4797 CLEAR_FIELD(arg_lvar);
4798 if (lookup_arg(var_start, varlen,
4799 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4800 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004801 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004802 if (is_decl)
4803 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004804 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004805 goto theend;
4806 }
4807 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004808 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004809 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004810 if (lvar != NULL)
4811 {
4812 if (is_decl)
4813 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004814 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004815 goto theend;
4816 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004817 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004818 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004819 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004820 int script_namespace = varlen > 1
4821 && STRNCMP(var_start, "s:", 2) == 0;
4822 int script_var = (script_namespace
Bram Moolenaar53900992020-08-22 19:02:02 +02004823 ? lookup_script(var_start + 2, varlen - 2, FALSE)
4824 : lookup_script(var_start, varlen, TRUE)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004825 imported_T *import =
4826 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004827
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004828 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004829 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004830 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4831
4832 if (is_decl)
4833 {
4834 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004835 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004836 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004837 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004838 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004839 name);
4840 goto theend;
4841 }
4842 else if (cctx->ctx_ufunc->uf_script_ctx_version
4843 == SCRIPT_VERSION_VIM9
4844 && script_namespace
4845 && !script_var && import == NULL)
4846 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004847 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004848 goto theend;
4849 }
4850
4851 dest = dest_script;
4852
4853 // existing script-local variables should have a type
4854 scriptvar_sid = current_sctx.sc_sid;
4855 if (import != NULL)
4856 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004857 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004858 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02004859 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4860 rawname, TRUE);
4861 if (scriptvar_idx > 0)
4862 {
4863 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4864 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004865 ((svar_T *)si->sn_var_vals.ga_data)
4866 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004867 type = sv->sv_type;
4868 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004869 }
4870 }
4871 else if (name[1] == ':' && name[2] != NUL)
4872 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004873 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004874 goto theend;
4875 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004876 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004877 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004878 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004879 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004880 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004881 else if (check_defined(var_start, varlen, cctx) == FAIL)
4882 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004883 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004884 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004885
4886 if (declare_error)
4887 {
4888 vim9_declare_error(name);
4889 goto theend;
4890 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004891 }
4892
4893 // handle "a:name" as a name, not index "name" on "a"
4894 if (varlen > 1 || var_start[varlen] != ':')
4895 p = var_end;
4896
4897 if (dest != dest_option)
4898 {
4899 if (is_decl && *p == ':')
4900 {
4901 // parse optional type: "let var: type = expr"
4902 if (!VIM_ISWHITE(p[1]))
4903 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004904 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004905 goto theend;
4906 }
4907 p = skipwhite(p + 1);
4908 type = parse_type(&p, cctx->ctx_type_list);
4909 has_type = TRUE;
4910 }
4911 else if (lvar != NULL)
4912 type = lvar->lv_type;
4913 }
4914
4915 if (oplen == 3 && !heredoc && dest != dest_global
4916 && type->tt_type != VAR_STRING
4917 && type->tt_type != VAR_ANY)
4918 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004919 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004920 goto theend;
4921 }
4922
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004923 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004924 {
4925 if (oplen > 1 && !heredoc)
4926 {
4927 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004928 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004929 goto theend;
4930 }
4931
4932 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004933 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
4934 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004935 goto theend;
4936 lvar = reserve_local(cctx, var_start, varlen,
4937 cmdidx == CMD_const, type);
4938 if (lvar == NULL)
4939 goto theend;
4940 new_local = TRUE;
4941 }
4942
4943 member_type = type;
4944 if (var_end > var_start + varlen)
4945 {
4946 // Something follows after the variable: "var[idx]".
4947 if (is_decl)
4948 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004949 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004950 goto theend;
4951 }
4952
4953 if (var_start[varlen] == '[')
4954 {
4955 has_index = TRUE;
4956 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004957 member_type = &t_any;
4958 else
4959 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004960 }
4961 else
4962 {
4963 semsg("Not supported yet: %s", var_start);
4964 goto theend;
4965 }
4966 }
4967 else if (lvar == &arg_lvar)
4968 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004969 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004970 goto theend;
4971 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02004972 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
4973 {
4974 semsg(_(e_cannot_assign_to_constant), name);
4975 goto theend;
4976 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004977
4978 if (!heredoc)
4979 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004980 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004981 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004982 if (oplen > 0 && var_count == 0)
4983 {
4984 // skip over the "=" and the expression
4985 p = skipwhite(op + oplen);
4986 compile_expr0(&p, cctx);
4987 }
4988 }
4989 else if (oplen > 0)
4990 {
4991 type_T *stacktype;
4992
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004993 // For "var = expr" evaluate the expression.
4994 if (var_count == 0)
4995 {
4996 int r;
4997
4998 // for "+=", "*=", "..=" etc. first load the current value
4999 if (*op != '=')
5000 {
5001 generate_loadvar(cctx, dest, name, lvar, type);
5002
5003 if (has_index)
5004 {
5005 // TODO: get member from list or dict
5006 emsg("Index with operation not supported yet");
5007 goto theend;
5008 }
5009 }
5010
5011 // Compile the expression. Temporarily hide the new local
5012 // variable here, it is not available to this expression.
5013 if (new_local)
5014 --cctx->ctx_locals.ga_len;
5015 instr_count = instr->ga_len;
5016 p = skipwhite(op + oplen);
5017 r = compile_expr0(&p, cctx);
5018 if (new_local)
5019 ++cctx->ctx_locals.ga_len;
5020 if (r == FAIL)
5021 goto theend;
5022 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005023 else if (semicolon && var_idx == var_count - 1)
5024 {
5025 // For "[var; var] = expr" get the rest of the list
5026 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5027 goto theend;
5028 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005029 else
5030 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005031 // For "[var, var] = expr" get the "var_idx" item from the
5032 // list.
5033 if (generate_GETITEM(cctx, var_idx) == FAIL)
5034 return FAIL;
5035 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005036
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005037 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005038 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005039 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005040 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005041 if ((stacktype->tt_type == VAR_FUNC
5042 || stacktype->tt_type == VAR_PARTIAL)
5043 && var_wrong_func_name(name, TRUE))
5044 goto theend;
5045
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005046 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005047 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005048 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005049 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005050 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005051 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005052 }
5053 else
5054 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005055 // An empty list or dict has a &t_void member,
5056 // for a variable that implies &t_any.
5057 if (stacktype == &t_list_empty)
5058 lvar->lv_type = &t_list_any;
5059 else if (stacktype == &t_dict_empty)
5060 lvar->lv_type = &t_dict_any;
5061 else
5062 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005063 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005064 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005065 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005066 {
5067 type_T *use_type = lvar->lv_type;
5068
Bram Moolenaar71abe482020-09-14 22:28:30 +02005069 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005070 if (has_index)
5071 {
5072 use_type = use_type->tt_member;
5073 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005074 // could be indexing "any"
5075 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005076 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005077 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005078 == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005079 goto theend;
5080 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005081 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005082 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005083 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005084 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005085 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005086 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005087 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005088 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005089 goto theend;
5090 }
5091 else if (!has_type || dest == dest_option)
5092 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005093 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005094 goto theend;
5095 }
5096 else
5097 {
5098 // variables are always initialized
5099 if (ga_grow(instr, 1) == FAIL)
5100 goto theend;
5101 switch (member_type->tt_type)
5102 {
5103 case VAR_BOOL:
5104 generate_PUSHBOOL(cctx, VVAL_FALSE);
5105 break;
5106 case VAR_FLOAT:
5107#ifdef FEAT_FLOAT
5108 generate_PUSHF(cctx, 0.0);
5109#endif
5110 break;
5111 case VAR_STRING:
5112 generate_PUSHS(cctx, NULL);
5113 break;
5114 case VAR_BLOB:
5115 generate_PUSHBLOB(cctx, NULL);
5116 break;
5117 case VAR_FUNC:
5118 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5119 break;
5120 case VAR_LIST:
5121 generate_NEWLIST(cctx, 0);
5122 break;
5123 case VAR_DICT:
5124 generate_NEWDICT(cctx, 0);
5125 break;
5126 case VAR_JOB:
5127 generate_PUSHJOB(cctx, NULL);
5128 break;
5129 case VAR_CHANNEL:
5130 generate_PUSHCHANNEL(cctx, NULL);
5131 break;
5132 case VAR_NUMBER:
5133 case VAR_UNKNOWN:
5134 case VAR_ANY:
5135 case VAR_PARTIAL:
5136 case VAR_VOID:
5137 case VAR_SPECIAL: // cannot happen
5138 generate_PUSHNR(cctx, 0);
5139 break;
5140 }
5141 }
5142 if (var_count == 0)
5143 end = p;
5144 }
5145
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005146 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005147 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005148 break;
5149
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005150 if (oplen > 0 && *op != '=')
5151 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005152 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005153 type_T *stacktype;
5154
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005155 if (*op == '.')
5156 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005157 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005158 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005159 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005160 if (
5161#ifdef FEAT_FLOAT
5162 // If variable is float operation with number is OK.
5163 !(expected == &t_float && stacktype == &t_number) &&
5164#endif
5165 need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005166 goto theend;
5167
5168 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005169 {
5170 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5171 goto theend;
5172 }
5173 else if (*op == '+')
5174 {
5175 if (generate_add_instr(cctx,
5176 operator_type(member_type, stacktype),
5177 member_type, stacktype) == FAIL)
5178 goto theend;
5179 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005180 else if (generate_two_op(cctx, op) == FAIL)
5181 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005182 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005183
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005184 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005185 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005186 int r;
5187
5188 // Compile the "idx" in "var[idx]".
5189 if (new_local)
5190 --cctx->ctx_locals.ga_len;
5191 p = skipwhite(var_start + varlen + 1);
5192 r = compile_expr0(&p, cctx);
5193 if (new_local)
5194 ++cctx->ctx_locals.ga_len;
5195 if (r == FAIL)
5196 goto theend;
5197 if (*skipwhite(p) != ']')
5198 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005199 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005200 emsg(_(e_missbrac));
5201 goto theend;
5202 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005203 if (type == &t_any)
5204 {
5205 type_T *idx_type = ((type_T **)stack->ga_data)[
5206 stack->ga_len - 1];
5207 // Index on variable of unknown type: guess the type from the
5208 // index type: number is dict, otherwise dict.
5209 // TODO: should do the assignment at runtime
5210 if (idx_type->tt_type == VAR_NUMBER)
5211 type = &t_list_any;
5212 else
5213 type = &t_dict_any;
5214 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005215 if (type->tt_type == VAR_DICT
5216 && may_generate_2STRING(-1, cctx) == FAIL)
5217 goto theend;
5218 if (type->tt_type == VAR_LIST
5219 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005220 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005221 {
5222 emsg(_(e_number_exp));
5223 goto theend;
5224 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005225
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005226 // Load the dict or list. On the stack we then have:
5227 // - value
5228 // - index
5229 // - variable
5230 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005231
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005232 if (type->tt_type == VAR_LIST)
5233 {
5234 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5235 return FAIL;
5236 }
5237 else if (type->tt_type == VAR_DICT)
5238 {
5239 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5240 return FAIL;
5241 }
5242 else
5243 {
5244 emsg(_(e_listreq));
5245 goto theend;
5246 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005247 }
5248 else
5249 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005250 if (is_decl && eap->forceit && cmdidx == CMD_const
5251 && (dest == dest_script || dest == dest_local))
5252 // ":const! var": lock the value, but not referenced variables
5253 generate_LOCKCONST(cctx);
5254
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005255 switch (dest)
5256 {
5257 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005258 generate_STOREOPT(cctx, skip_option_env_lead(name),
5259 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005260 break;
5261 case dest_global:
5262 // include g: with the name, easier to execute that way
5263 generate_STORE(cctx, ISN_STOREG, 0, name);
5264 break;
5265 case dest_buffer:
5266 // include b: with the name, easier to execute that way
5267 generate_STORE(cctx, ISN_STOREB, 0, name);
5268 break;
5269 case dest_window:
5270 // include w: with the name, easier to execute that way
5271 generate_STORE(cctx, ISN_STOREW, 0, name);
5272 break;
5273 case dest_tab:
5274 // include t: with the name, easier to execute that way
5275 generate_STORE(cctx, ISN_STORET, 0, name);
5276 break;
5277 case dest_env:
5278 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5279 break;
5280 case dest_reg:
5281 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5282 break;
5283 case dest_vimvar:
5284 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5285 break;
5286 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005287 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005288 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005289 {
5290 char_u *name_s = name;
5291
5292 // Include s: in the name for store_var()
5293 if (name[1] != ':')
5294 {
5295 int len = (int)STRLEN(name) + 3;
5296
5297 name_s = alloc(len);
5298 if (name_s == NULL)
5299 name_s = name;
5300 else
5301 vim_snprintf((char *)name_s, len,
5302 "s:%s", name);
5303 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005304 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5305 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005306 if (name_s != name)
5307 vim_free(name_s);
5308 }
5309 else
5310 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005311 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005312 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005313 break;
5314 case dest_local:
5315 if (lvar != NULL)
5316 {
5317 isn_T *isn = ((isn_T *)instr->ga_data)
5318 + instr->ga_len - 1;
5319
5320 // optimization: turn "var = 123" from ISN_PUSHNR +
5321 // ISN_STORE into ISN_STORENR
5322 if (!lvar->lv_from_outer
5323 && instr->ga_len == instr_count + 1
5324 && isn->isn_type == ISN_PUSHNR)
5325 {
5326 varnumber_T val = isn->isn_arg.number;
5327
5328 isn->isn_type = ISN_STORENR;
5329 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5330 isn->isn_arg.storenr.stnr_val = val;
5331 if (stack->ga_len > 0)
5332 --stack->ga_len;
5333 }
5334 else if (lvar->lv_from_outer)
5335 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005336 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005337 else
5338 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5339 }
5340 break;
5341 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005342 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005343
5344 if (var_idx + 1 < var_count)
5345 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005346 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005347
5348 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005349 if (var_count > 0 && !semicolon)
5350 {
5351 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5352 goto theend;
5353 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005354
Bram Moolenaarb2097502020-07-19 17:17:02 +02005355 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005356
5357theend:
5358 vim_free(name);
5359 return ret;
5360}
5361
5362/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005363 * Check if "name" can be "unlet".
5364 */
5365 int
5366check_vim9_unlet(char_u *name)
5367{
5368 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5369 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005370 // "unlet s:var" is allowed in legacy script.
5371 if (*name == 's' && !script_is_vim9())
5372 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005373 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005374 return FAIL;
5375 }
5376 return OK;
5377}
5378
5379/*
5380 * Callback passed to ex_unletlock().
5381 */
5382 static int
5383compile_unlet(
5384 lval_T *lvp,
5385 char_u *name_end,
5386 exarg_T *eap,
5387 int deep UNUSED,
5388 void *coookie)
5389{
5390 cctx_T *cctx = coookie;
5391
5392 if (lvp->ll_tv == NULL)
5393 {
5394 char_u *p = lvp->ll_name;
5395 int cc = *name_end;
5396 int ret = OK;
5397
5398 // Normal name. Only supports g:, w:, t: and b: namespaces.
5399 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005400 if (*p == '$')
5401 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5402 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005403 ret = FAIL;
5404 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005405 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005406
5407 *name_end = cc;
5408 return ret;
5409 }
5410
5411 // TODO: unlet {list}[idx]
5412 // TODO: unlet {dict}[key]
5413 emsg("Sorry, :unlet not fully implemented yet");
5414 return FAIL;
5415}
5416
5417/*
5418 * compile "unlet var", "lock var" and "unlock var"
5419 * "arg" points to "var".
5420 */
5421 static char_u *
5422compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5423{
5424 char_u *p = arg;
5425
5426 if (eap->cmdidx != CMD_unlet)
5427 {
5428 emsg("Sorry, :lock and unlock not implemented yet");
5429 return NULL;
5430 }
5431
5432 if (*p == '!')
5433 {
5434 p = skipwhite(p + 1);
5435 eap->forceit = TRUE;
5436 }
5437
5438 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5439 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5440}
5441
5442/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005443 * Compile an :import command.
5444 */
5445 static char_u *
5446compile_import(char_u *arg, cctx_T *cctx)
5447{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005448 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005449}
5450
5451/*
5452 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5453 */
5454 static int
5455compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5456{
5457 garray_T *instr = &cctx->ctx_instr;
5458 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5459
5460 if (endlabel == NULL)
5461 return FAIL;
5462 endlabel->el_next = *el;
5463 *el = endlabel;
5464 endlabel->el_end_label = instr->ga_len;
5465
5466 generate_JUMP(cctx, when, 0);
5467 return OK;
5468}
5469
5470 static void
5471compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5472{
5473 garray_T *instr = &cctx->ctx_instr;
5474
5475 while (*el != NULL)
5476 {
5477 endlabel_T *cur = (*el);
5478 isn_T *isn;
5479
5480 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5481 isn->isn_arg.jump.jump_where = instr->ga_len;
5482 *el = cur->el_next;
5483 vim_free(cur);
5484 }
5485}
5486
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005487 static void
5488compile_free_jump_to_end(endlabel_T **el)
5489{
5490 while (*el != NULL)
5491 {
5492 endlabel_T *cur = (*el);
5493
5494 *el = cur->el_next;
5495 vim_free(cur);
5496 }
5497}
5498
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005499/*
5500 * Create a new scope and set up the generic items.
5501 */
5502 static scope_T *
5503new_scope(cctx_T *cctx, scopetype_T type)
5504{
5505 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5506
5507 if (scope == NULL)
5508 return NULL;
5509 scope->se_outer = cctx->ctx_scope;
5510 cctx->ctx_scope = scope;
5511 scope->se_type = type;
5512 scope->se_local_count = cctx->ctx_locals.ga_len;
5513 return scope;
5514}
5515
5516/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005517 * Free the current scope and go back to the outer scope.
5518 */
5519 static void
5520drop_scope(cctx_T *cctx)
5521{
5522 scope_T *scope = cctx->ctx_scope;
5523
5524 if (scope == NULL)
5525 {
5526 iemsg("calling drop_scope() without a scope");
5527 return;
5528 }
5529 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005530 switch (scope->se_type)
5531 {
5532 case IF_SCOPE:
5533 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5534 case FOR_SCOPE:
5535 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5536 case WHILE_SCOPE:
5537 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5538 case TRY_SCOPE:
5539 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5540 case NO_SCOPE:
5541 case BLOCK_SCOPE:
5542 break;
5543 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005544 vim_free(scope);
5545}
5546
5547/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005548 * compile "if expr"
5549 *
5550 * "if expr" Produces instructions:
5551 * EVAL expr Push result of "expr"
5552 * JUMP_IF_FALSE end
5553 * ... body ...
5554 * end:
5555 *
5556 * "if expr | else" Produces instructions:
5557 * EVAL expr Push result of "expr"
5558 * JUMP_IF_FALSE else
5559 * ... body ...
5560 * JUMP_ALWAYS end
5561 * else:
5562 * ... body ...
5563 * end:
5564 *
5565 * "if expr1 | elseif expr2 | else" Produces instructions:
5566 * EVAL expr Push result of "expr"
5567 * JUMP_IF_FALSE elseif
5568 * ... body ...
5569 * JUMP_ALWAYS end
5570 * elseif:
5571 * EVAL expr Push result of "expr"
5572 * JUMP_IF_FALSE else
5573 * ... body ...
5574 * JUMP_ALWAYS end
5575 * else:
5576 * ... body ...
5577 * end:
5578 */
5579 static char_u *
5580compile_if(char_u *arg, cctx_T *cctx)
5581{
5582 char_u *p = arg;
5583 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005584 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005585 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005586 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005587 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005588
Bram Moolenaara5565e42020-05-09 15:44:01 +02005589 CLEAR_FIELD(ppconst);
5590 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005591 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005592 clear_ppconst(&ppconst);
5593 return NULL;
5594 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005595 if (cctx->ctx_skip == SKIP_YES)
5596 clear_ppconst(&ppconst);
5597 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005598 {
5599 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005600 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005601 clear_ppconst(&ppconst);
5602 }
5603 else
5604 {
5605 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005606 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005607 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005608 return NULL;
5609 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005610
5611 scope = new_scope(cctx, IF_SCOPE);
5612 if (scope == NULL)
5613 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005614 scope->se_skip_save = skip_save;
5615 // "is_had_return" will be reset if any block does not end in :return
5616 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005617
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005618 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005619 {
5620 // "where" is set when ":elseif", "else" or ":endif" is found
5621 scope->se_u.se_if.is_if_label = instr->ga_len;
5622 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5623 }
5624 else
5625 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005626
5627 return p;
5628}
5629
5630 static char_u *
5631compile_elseif(char_u *arg, cctx_T *cctx)
5632{
5633 char_u *p = arg;
5634 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005635 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005636 isn_T *isn;
5637 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005638 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02005639 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005640
5641 if (scope == NULL || scope->se_type != IF_SCOPE)
5642 {
5643 emsg(_(e_elseif_without_if));
5644 return NULL;
5645 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005646 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005647 if (!cctx->ctx_had_return)
5648 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005649
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005650 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005651 {
5652 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005653 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005654 return NULL;
5655 // previous "if" or "elseif" jumps here
5656 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5657 isn->isn_arg.jump.jump_where = instr->ga_len;
5658 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005659
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005660 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005661 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02005662 if (cctx->ctx_skip == SKIP_YES)
5663 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005664 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005665 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005666 clear_ppconst(&ppconst);
5667 return NULL;
5668 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02005669 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005670 if (scope->se_skip_save == SKIP_YES)
5671 clear_ppconst(&ppconst);
5672 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005673 {
5674 // The expression results in a constant.
5675 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005676 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005677 clear_ppconst(&ppconst);
5678 scope->se_u.se_if.is_if_label = -1;
5679 }
5680 else
5681 {
5682 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005683 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005684 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005685 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005686
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005687 // "where" is set when ":elseif", "else" or ":endif" is found
5688 scope->se_u.se_if.is_if_label = instr->ga_len;
5689 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5690 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005691
5692 return p;
5693}
5694
5695 static char_u *
5696compile_else(char_u *arg, cctx_T *cctx)
5697{
5698 char_u *p = arg;
5699 garray_T *instr = &cctx->ctx_instr;
5700 isn_T *isn;
5701 scope_T *scope = cctx->ctx_scope;
5702
5703 if (scope == NULL || scope->se_type != IF_SCOPE)
5704 {
5705 emsg(_(e_else_without_if));
5706 return NULL;
5707 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005708 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005709 if (!cctx->ctx_had_return)
5710 scope->se_u.se_if.is_had_return = FALSE;
5711 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005712
Bram Moolenaarefd88552020-06-18 20:50:10 +02005713 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005714 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005715 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005716 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005717 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005718 if (!cctx->ctx_had_return
5719 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5720 JUMP_ALWAYS, cctx) == FAIL)
5721 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005722 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005723
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005724 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005725 {
5726 if (scope->se_u.se_if.is_if_label >= 0)
5727 {
5728 // previous "if" or "elseif" jumps here
5729 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5730 isn->isn_arg.jump.jump_where = instr->ga_len;
5731 scope->se_u.se_if.is_if_label = -1;
5732 }
5733 }
5734
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005735 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005736 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5737 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005738
5739 return p;
5740}
5741
5742 static char_u *
5743compile_endif(char_u *arg, cctx_T *cctx)
5744{
5745 scope_T *scope = cctx->ctx_scope;
5746 ifscope_T *ifscope;
5747 garray_T *instr = &cctx->ctx_instr;
5748 isn_T *isn;
5749
5750 if (scope == NULL || scope->se_type != IF_SCOPE)
5751 {
5752 emsg(_(e_endif_without_if));
5753 return NULL;
5754 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005755 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005756 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005757 if (!cctx->ctx_had_return)
5758 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005759
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005760 if (scope->se_u.se_if.is_if_label >= 0)
5761 {
5762 // previous "if" or "elseif" jumps here
5763 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5764 isn->isn_arg.jump.jump_where = instr->ga_len;
5765 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005766 // Fill in the "end" label in jumps at the end of the blocks.
5767 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005768 cctx->ctx_skip = scope->se_skip_save;
5769
5770 // If all the blocks end in :return and there is an :else then the
5771 // had_return flag is set.
5772 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005773
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005774 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005775 return arg;
5776}
5777
5778/*
5779 * compile "for var in expr"
5780 *
5781 * Produces instructions:
5782 * PUSHNR -1
5783 * STORE loop-idx Set index to -1
5784 * EVAL expr Push result of "expr"
5785 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5786 * - if beyond end, jump to "end"
5787 * - otherwise get item from list and push it
5788 * STORE var Store item in "var"
5789 * ... body ...
5790 * JUMP top Jump back to repeat
5791 * end: DROP Drop the result of "expr"
5792 *
5793 */
5794 static char_u *
5795compile_for(char_u *arg, cctx_T *cctx)
5796{
5797 char_u *p;
5798 size_t varlen;
5799 garray_T *instr = &cctx->ctx_instr;
5800 garray_T *stack = &cctx->ctx_type_stack;
5801 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005802 lvar_T *loop_lvar; // loop iteration variable
5803 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005804 type_T *vartype;
5805
5806 // TODO: list of variables: "for [key, value] in dict"
5807 // parse "var"
5808 for (p = arg; eval_isnamec1(*p); ++p)
5809 ;
5810 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005811 var_lvar = lookup_local(arg, varlen, cctx);
5812 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005813 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005814 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005815 return NULL;
5816 }
5817
5818 // consume "in"
5819 p = skipwhite(p);
5820 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5821 {
5822 emsg(_(e_missing_in));
5823 return NULL;
5824 }
5825 p = skipwhite(p + 2);
5826
5827
5828 scope = new_scope(cctx, FOR_SCOPE);
5829 if (scope == NULL)
5830 return NULL;
5831
5832 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005833 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5834 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005835 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005836 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005837 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005838 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005839 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005840
5841 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005842 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5843 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005844 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005845 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005846 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005847 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005848 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005849
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005850 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005851
5852 // compile "expr", it remains on the stack until "endfor"
5853 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005854 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005855 {
5856 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005857 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005858 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005859
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005860 // Now that we know the type of "var", check that it is a list, now or at
5861 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005862 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005863 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005864 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005865 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005866 return NULL;
5867 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005868 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005869 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005870
5871 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005872 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005873
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005874 generate_FOR(cctx, loop_lvar->lv_idx);
5875 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005876
5877 return arg;
5878}
5879
5880/*
5881 * compile "endfor"
5882 */
5883 static char_u *
5884compile_endfor(char_u *arg, cctx_T *cctx)
5885{
5886 garray_T *instr = &cctx->ctx_instr;
5887 scope_T *scope = cctx->ctx_scope;
5888 forscope_T *forscope;
5889 isn_T *isn;
5890
5891 if (scope == NULL || scope->se_type != FOR_SCOPE)
5892 {
5893 emsg(_(e_for));
5894 return NULL;
5895 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005896 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005897 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005898 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005899
5900 // At end of ":for" scope jump back to the FOR instruction.
5901 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5902
5903 // Fill in the "end" label in the FOR statement so it can jump here
5904 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5905 isn->isn_arg.forloop.for_end = instr->ga_len;
5906
5907 // Fill in the "end" label any BREAK statements
5908 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5909
5910 // Below the ":for" scope drop the "expr" list from the stack.
5911 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5912 return NULL;
5913
5914 vim_free(scope);
5915
5916 return arg;
5917}
5918
5919/*
5920 * compile "while expr"
5921 *
5922 * Produces instructions:
5923 * top: EVAL expr Push result of "expr"
5924 * JUMP_IF_FALSE end jump if false
5925 * ... body ...
5926 * JUMP top Jump back to repeat
5927 * end:
5928 *
5929 */
5930 static char_u *
5931compile_while(char_u *arg, cctx_T *cctx)
5932{
5933 char_u *p = arg;
5934 garray_T *instr = &cctx->ctx_instr;
5935 scope_T *scope;
5936
5937 scope = new_scope(cctx, WHILE_SCOPE);
5938 if (scope == NULL)
5939 return NULL;
5940
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005941 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005942
5943 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005944 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005945 return NULL;
5946
5947 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005948 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005949 JUMP_IF_FALSE, cctx) == FAIL)
5950 return FAIL;
5951
5952 return p;
5953}
5954
5955/*
5956 * compile "endwhile"
5957 */
5958 static char_u *
5959compile_endwhile(char_u *arg, cctx_T *cctx)
5960{
5961 scope_T *scope = cctx->ctx_scope;
5962
5963 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5964 {
5965 emsg(_(e_while));
5966 return NULL;
5967 }
5968 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005969 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005970
5971 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005972 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005973
5974 // Fill in the "end" label in the WHILE statement so it can jump here.
5975 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005976 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005977
5978 vim_free(scope);
5979
5980 return arg;
5981}
5982
5983/*
5984 * compile "continue"
5985 */
5986 static char_u *
5987compile_continue(char_u *arg, cctx_T *cctx)
5988{
5989 scope_T *scope = cctx->ctx_scope;
5990
5991 for (;;)
5992 {
5993 if (scope == NULL)
5994 {
5995 emsg(_(e_continue));
5996 return NULL;
5997 }
5998 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5999 break;
6000 scope = scope->se_outer;
6001 }
6002
6003 // Jump back to the FOR or WHILE instruction.
6004 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006005 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6006 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006007 return arg;
6008}
6009
6010/*
6011 * compile "break"
6012 */
6013 static char_u *
6014compile_break(char_u *arg, cctx_T *cctx)
6015{
6016 scope_T *scope = cctx->ctx_scope;
6017 endlabel_T **el;
6018
6019 for (;;)
6020 {
6021 if (scope == NULL)
6022 {
6023 emsg(_(e_break));
6024 return NULL;
6025 }
6026 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6027 break;
6028 scope = scope->se_outer;
6029 }
6030
6031 // Jump to the end of the FOR or WHILE loop.
6032 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006033 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006034 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006035 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006036 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6037 return FAIL;
6038
6039 return arg;
6040}
6041
6042/*
6043 * compile "{" start of block
6044 */
6045 static char_u *
6046compile_block(char_u *arg, cctx_T *cctx)
6047{
6048 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6049 return NULL;
6050 return skipwhite(arg + 1);
6051}
6052
6053/*
6054 * compile end of block: drop one scope
6055 */
6056 static void
6057compile_endblock(cctx_T *cctx)
6058{
6059 scope_T *scope = cctx->ctx_scope;
6060
6061 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006062 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006063 vim_free(scope);
6064}
6065
6066/*
6067 * compile "try"
6068 * Creates a new scope for the try-endtry, pointing to the first catch and
6069 * finally.
6070 * Creates another scope for the "try" block itself.
6071 * TRY instruction sets up exception handling at runtime.
6072 *
6073 * "try"
6074 * TRY -> catch1, -> finally push trystack entry
6075 * ... try block
6076 * "throw {exception}"
6077 * EVAL {exception}
6078 * THROW create exception
6079 * ... try block
6080 * " catch {expr}"
6081 * JUMP -> finally
6082 * catch1: PUSH exeception
6083 * EVAL {expr}
6084 * MATCH
6085 * JUMP nomatch -> catch2
6086 * CATCH remove exception
6087 * ... catch block
6088 * " catch"
6089 * JUMP -> finally
6090 * catch2: CATCH remove exception
6091 * ... catch block
6092 * " finally"
6093 * finally:
6094 * ... finally block
6095 * " endtry"
6096 * ENDTRY pop trystack entry, may rethrow
6097 */
6098 static char_u *
6099compile_try(char_u *arg, cctx_T *cctx)
6100{
6101 garray_T *instr = &cctx->ctx_instr;
6102 scope_T *try_scope;
6103 scope_T *scope;
6104
6105 // scope that holds the jumps that go to catch/finally/endtry
6106 try_scope = new_scope(cctx, TRY_SCOPE);
6107 if (try_scope == NULL)
6108 return NULL;
6109
6110 // "catch" is set when the first ":catch" is found.
6111 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006112 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006113 if (generate_instr(cctx, ISN_TRY) == NULL)
6114 return NULL;
6115
6116 // scope for the try block itself
6117 scope = new_scope(cctx, BLOCK_SCOPE);
6118 if (scope == NULL)
6119 return NULL;
6120
6121 return arg;
6122}
6123
6124/*
6125 * compile "catch {expr}"
6126 */
6127 static char_u *
6128compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6129{
6130 scope_T *scope = cctx->ctx_scope;
6131 garray_T *instr = &cctx->ctx_instr;
6132 char_u *p;
6133 isn_T *isn;
6134
6135 // end block scope from :try or :catch
6136 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6137 compile_endblock(cctx);
6138 scope = cctx->ctx_scope;
6139
6140 // Error if not in a :try scope
6141 if (scope == NULL || scope->se_type != TRY_SCOPE)
6142 {
6143 emsg(_(e_catch));
6144 return NULL;
6145 }
6146
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006147 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006148 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006149 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006150 return NULL;
6151 }
6152
6153 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006154 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006155 JUMP_ALWAYS, cctx) == FAIL)
6156 return NULL;
6157
6158 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006159 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006160 if (isn->isn_arg.try.try_catch == 0)
6161 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006162 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006163 {
6164 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006165 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006166 isn->isn_arg.jump.jump_where = instr->ga_len;
6167 }
6168
6169 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006170 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006171 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006172 scope->se_u.se_try.ts_caught_all = TRUE;
6173 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006174 }
6175 else
6176 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006177 char_u *end;
6178 char_u *pat;
6179 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006180 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006181 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006182
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006183 // Push v:exception, push {expr} and MATCH
6184 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6185
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006186 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006187 if (*end != *p)
6188 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006189 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006190 vim_free(tofree);
6191 return FAIL;
6192 }
6193 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006194 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006195 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006196 len = (int)(end - tofree);
6197 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006198 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006199 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006200 if (pat == NULL)
6201 return FAIL;
6202 if (generate_PUSHS(cctx, pat) == FAIL)
6203 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006204
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006205 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6206 return NULL;
6207
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006208 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006209 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6210 return NULL;
6211 }
6212
6213 if (generate_instr(cctx, ISN_CATCH) == NULL)
6214 return NULL;
6215
6216 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6217 return NULL;
6218 return p;
6219}
6220
6221 static char_u *
6222compile_finally(char_u *arg, cctx_T *cctx)
6223{
6224 scope_T *scope = cctx->ctx_scope;
6225 garray_T *instr = &cctx->ctx_instr;
6226 isn_T *isn;
6227
6228 // end block scope from :try or :catch
6229 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6230 compile_endblock(cctx);
6231 scope = cctx->ctx_scope;
6232
6233 // Error if not in a :try scope
6234 if (scope == NULL || scope->se_type != TRY_SCOPE)
6235 {
6236 emsg(_(e_finally));
6237 return NULL;
6238 }
6239
6240 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006241 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006242 if (isn->isn_arg.try.try_finally != 0)
6243 {
6244 emsg(_(e_finally_dup));
6245 return NULL;
6246 }
6247
6248 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006249 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006250
Bram Moolenaar585fea72020-04-02 22:33:21 +02006251 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006252 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006253 {
6254 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006255 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006256 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006257 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006258 }
6259
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006260 // TODO: set index in ts_finally_label jumps
6261
6262 return arg;
6263}
6264
6265 static char_u *
6266compile_endtry(char_u *arg, cctx_T *cctx)
6267{
6268 scope_T *scope = cctx->ctx_scope;
6269 garray_T *instr = &cctx->ctx_instr;
6270 isn_T *isn;
6271
6272 // end block scope from :catch or :finally
6273 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6274 compile_endblock(cctx);
6275 scope = cctx->ctx_scope;
6276
6277 // Error if not in a :try scope
6278 if (scope == NULL || scope->se_type != TRY_SCOPE)
6279 {
6280 if (scope == NULL)
6281 emsg(_(e_no_endtry));
6282 else if (scope->se_type == WHILE_SCOPE)
6283 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006284 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006285 emsg(_(e_endfor));
6286 else
6287 emsg(_(e_endif));
6288 return NULL;
6289 }
6290
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006291 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006292 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6293 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006294 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006295 return NULL;
6296 }
6297
6298 // Fill in the "end" label in jumps at the end of the blocks, if not done
6299 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006300 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006301
6302 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006303 if (isn->isn_arg.try.try_catch == 0)
6304 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006305 if (isn->isn_arg.try.try_finally == 0)
6306 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006307
6308 if (scope->se_u.se_try.ts_catch_label != 0)
6309 {
6310 // Last catch without match jumps here
6311 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6312 isn->isn_arg.jump.jump_where = instr->ga_len;
6313 }
6314
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006315 compile_endblock(cctx);
6316
6317 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6318 return NULL;
6319 return arg;
6320}
6321
6322/*
6323 * compile "throw {expr}"
6324 */
6325 static char_u *
6326compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6327{
6328 char_u *p = skipwhite(arg);
6329
Bram Moolenaara5565e42020-05-09 15:44:01 +02006330 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006331 return NULL;
6332 if (may_generate_2STRING(-1, cctx) == FAIL)
6333 return NULL;
6334 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6335 return NULL;
6336
6337 return p;
6338}
6339
6340/*
6341 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006342 * compile "echomsg expr"
6343 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006344 * compile "execute expr"
6345 */
6346 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006347compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006348{
6349 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006350 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006351 int count = 0;
6352
6353 for (;;)
6354 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006355 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006356 return NULL;
6357 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006358 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006359 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006360 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006361 break;
6362 }
6363
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006364 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6365 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6366 else if (cmdidx == CMD_execute)
6367 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6368 else if (cmdidx == CMD_echomsg)
6369 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6370 else
6371 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006372 return p;
6373}
6374
6375/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006376 * :put r
6377 * :put ={expr}
6378 */
6379 static char_u *
6380compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6381{
6382 char_u *line = arg;
6383 linenr_T lnum;
6384 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006385 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006386
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006387 eap->regname = *line;
6388
6389 if (eap->regname == '=')
6390 {
6391 char_u *p = line + 1;
6392
6393 if (compile_expr0(&p, cctx) == FAIL)
6394 return NULL;
6395 line = p;
6396 }
6397 else if (eap->regname != NUL)
6398 ++line;
6399
6400 // TODO: if the range is something like "$" need to evaluate at runtime
6401 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6402 return NULL;
6403 if (eap->addr_count == 0)
6404 lnum = -1;
6405 else
6406 lnum = eap->line2;
6407 if (above)
6408 --lnum;
6409
6410 generate_PUT(cctx, eap->regname, lnum);
6411 return line;
6412}
6413
6414/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006415 * A command that is not compiled, execute with legacy code.
6416 */
6417 static char_u *
6418compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6419{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006420 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006421 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006422 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006423
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006424 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006425 goto theend;
6426
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006427 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006428 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006429 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006430 int usefilter = FALSE;
6431
6432 has_expr = argt & (EX_XFILE | EX_EXPAND);
6433
6434 // If the command can be followed by a bar, find the bar and truncate
6435 // it, so that the following command can be compiled.
6436 // The '|' is overwritten with a NUL, it is put back below.
6437 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6438 && *eap->arg == '!')
6439 // :w !filter or :r !filter or :r! filter
6440 usefilter = TRUE;
6441 if ((argt & EX_TRLBAR) && !usefilter)
6442 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006443 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006444 separate_nextcmd(eap);
6445 if (eap->nextcmd != NULL)
6446 nextcmd = eap->nextcmd;
6447 }
6448 }
6449
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006450 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6451 {
6452 // expand filename in "syntax include [@group] filename"
6453 has_expr = TRUE;
6454 eap->arg = skipwhite(eap->arg + 7);
6455 if (*eap->arg == '@')
6456 eap->arg = skiptowhite(eap->arg);
6457 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006458
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006459 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006460 {
6461 int count = 0;
6462 char_u *start = skipwhite(line);
6463
6464 // :cmd xxx`=expr1`yyy`=expr2`zzz
6465 // PUSHS ":cmd xxx"
6466 // eval expr1
6467 // PUSHS "yyy"
6468 // eval expr2
6469 // PUSHS "zzz"
6470 // EXECCONCAT 5
6471 for (;;)
6472 {
6473 if (p > start)
6474 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006475 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006476 ++count;
6477 }
6478 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006479 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006480 return NULL;
6481 may_generate_2STRING(-1, cctx);
6482 ++count;
6483 p = skipwhite(p);
6484 if (*p != '`')
6485 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006486 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006487 return NULL;
6488 }
6489 start = p + 1;
6490
6491 p = (char_u *)strstr((char *)start, "`=");
6492 if (p == NULL)
6493 {
6494 if (*skipwhite(start) != NUL)
6495 {
6496 generate_PUSHS(cctx, vim_strsave(start));
6497 ++count;
6498 }
6499 break;
6500 }
6501 }
6502 generate_EXECCONCAT(cctx, count);
6503 }
6504 else
6505 generate_EXEC(cctx, line);
6506
6507theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006508 if (*nextcmd != NUL)
6509 {
6510 // the parser expects a pointer to the bar, put it back
6511 --nextcmd;
6512 *nextcmd = '|';
6513 }
6514
6515 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006516}
6517
6518/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006519 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006520 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006521 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006522 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006523add_def_function(ufunc_T *ufunc)
6524{
6525 dfunc_T *dfunc;
6526
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006527 if (def_functions.ga_len == 0)
6528 {
6529 // The first position is not used, so that a zero uf_dfunc_idx means it
6530 // wasn't set.
6531 if (ga_grow(&def_functions, 1) == FAIL)
6532 return FAIL;
6533 ++def_functions.ga_len;
6534 }
6535
Bram Moolenaar09689a02020-05-09 22:50:08 +02006536 // Add the function to "def_functions".
6537 if (ga_grow(&def_functions, 1) == FAIL)
6538 return FAIL;
6539 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6540 CLEAR_POINTER(dfunc);
6541 dfunc->df_idx = def_functions.ga_len;
6542 ufunc->uf_dfunc_idx = dfunc->df_idx;
6543 dfunc->df_ufunc = ufunc;
6544 ++def_functions.ga_len;
6545 return OK;
6546}
6547
6548/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006549 * After ex_function() has collected all the function lines: parse and compile
6550 * the lines into instructions.
6551 * Adds the function to "def_functions".
6552 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6553 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006554 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006555 * This can be used recursively through compile_lambda(), which may reallocate
6556 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006557 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006558 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006559 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006560compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006561{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006562 char_u *line = NULL;
6563 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006564 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006565 cctx_T cctx;
6566 garray_T *instr;
6567 int called_emsg_before = called_emsg;
6568 int ret = FAIL;
6569 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006570 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006571 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006572 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006573
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006574 // When using a function that was compiled before: Free old instructions.
6575 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006576 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006577 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006578 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6579 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006580 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006581 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006582 else
6583 {
6584 if (add_def_function(ufunc) == FAIL)
6585 return FAIL;
6586 new_def_function = TRUE;
6587 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006588
Bram Moolenaar985116a2020-07-12 17:31:09 +02006589 ufunc->uf_def_status = UF_COMPILING;
6590
Bram Moolenaara80faa82020-04-12 19:37:17 +02006591 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006592 cctx.ctx_ufunc = ufunc;
6593 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006594 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006595 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6596 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6597 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6598 cctx.ctx_type_list = &ufunc->uf_type_list;
6599 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6600 instr = &cctx.ctx_instr;
6601
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006602 // Set the context to the function, it may be compiled when called from
6603 // another script. Set the script version to the most modern one.
6604 // The line number will be set in next_line_from_context().
6605 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006606 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6607
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006608 // Make sure error messages are OK.
6609 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6610 if (do_estack_push)
6611 estack_push_ufunc(ufunc, 1);
6612
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006613 if (ufunc->uf_def_args.ga_len > 0)
6614 {
6615 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006616 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006617 int i;
6618 char_u *arg;
6619 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006620 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006621
6622 // Produce instructions for the default values of optional arguments.
6623 // Store the instruction index in uf_def_arg_idx[] so that we know
6624 // where to start when the function is called, depending on the number
6625 // of arguments.
6626 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6627 if (ufunc->uf_def_arg_idx == NULL)
6628 goto erret;
6629 for (i = 0; i < count; ++i)
6630 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006631 garray_T *stack = &cctx.ctx_type_stack;
6632 type_T *val_type;
6633 int arg_idx = first_def_arg + i;
6634
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006635 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6636 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006637 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006638 goto erret;
6639
6640 // If no type specified use the type of the default value.
6641 // Otherwise check that the default value type matches the
6642 // specified type.
6643 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6644 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006645 {
6646 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006647 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006648 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02006649 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
6650 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006651 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006652
6653 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006654 goto erret;
6655 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006656 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006657
6658 if (did_set_arg_type)
6659 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006660 }
6661
6662 /*
6663 * Loop over all the lines of the function and generate instructions.
6664 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006665 for (;;)
6666 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006667 exarg_T ea;
6668 cmdmod_T save_cmdmod;
6669 int starts_with_colon = FALSE;
6670 char_u *cmd;
6671 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006672
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006673 // Bail out on the first error to avoid a flood of errors and report
6674 // the right line number when inside try/catch.
6675 if (emsg_before != called_emsg)
6676 goto erret;
6677
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006678 if (line != NULL && *line == '|')
6679 // the line continues after a '|'
6680 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006681 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006682 && !(*line == '#' && (line == cctx.ctx_line_start
6683 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006684 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006685 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006686 goto erret;
6687 }
6688 else
6689 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006690 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006691 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006692 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006693 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006694 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006695 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006696
Bram Moolenaara80faa82020-04-12 19:37:17 +02006697 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006698 ea.cmdlinep = &line;
6699 ea.cmd = skipwhite(line);
6700
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006701 // Some things can be recognized by the first character.
6702 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006703 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006704 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006705 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006706 if (ea.cmd[1] != '{')
6707 {
6708 line = (char_u *)"";
6709 continue;
6710 }
6711 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006712
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006713 case '}':
6714 {
6715 // "}" ends a block scope
6716 scopetype_T stype = cctx.ctx_scope == NULL
6717 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006718
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006719 if (stype == BLOCK_SCOPE)
6720 {
6721 compile_endblock(&cctx);
6722 line = ea.cmd;
6723 }
6724 else
6725 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006726 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006727 goto erret;
6728 }
6729 if (line != NULL)
6730 line = skipwhite(ea.cmd + 1);
6731 continue;
6732 }
6733
6734 case '{':
6735 // "{" starts a block scope
6736 // "{'a': 1}->func() is something else
6737 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6738 {
6739 line = compile_block(ea.cmd, &cctx);
6740 continue;
6741 }
6742 break;
6743
6744 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006745 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006746 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006747 }
6748
6749 /*
6750 * COMMAND MODIFIERS
6751 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006752 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006753 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6754 {
6755 if (errormsg != NULL)
6756 goto erret;
6757 // empty line or comment
6758 line = (char_u *)"";
6759 continue;
6760 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006761 // TODO: use modifiers in the command
6762 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006763 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006764
6765 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006766 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006767 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006768 {
6769 if (*ea.cmd == '(')
6770 // not for "call()"
6771 ea.cmd = p;
6772 else
6773 ea.cmd = skipwhite(ea.cmd);
6774 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006775
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006776 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006777 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006778 char_u *pskip;
6779
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006780 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006781 // find what follows.
6782 // Skip over "var.member", "var[idx]" and the like.
6783 // Also "&opt = val", "$ENV = val" and "@r = val".
6784 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006785 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006786 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006787 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006788 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006789 char_u *var_end;
6790 int oplen;
6791 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006792
Bram Moolenaar65821722020-08-02 18:58:54 +02006793 if (ea.cmd[0] == '@')
6794 var_end = ea.cmd + 2;
6795 else
6796 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006797 FNE_CHECK_START | FNE_INCL_BR);
6798 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006799 if (oplen > 0)
6800 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006801 size_t len = p - ea.cmd;
6802
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006803 // Recognize an assignment if we recognize the variable
6804 // name:
6805 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006806 // "local = expr" where "local" is a local var.
6807 // "script = expr" where "script" is a script-local var.
6808 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006809 // "&opt = expr"
6810 // "$ENV = expr"
6811 // "@r = expr"
6812 if (*ea.cmd == '&'
6813 || *ea.cmd == '$'
6814 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006815 || ((len) > 2 && ea.cmd[1] == ':')
6816 || lookup_local(ea.cmd, len, &cctx) != NULL
6817 || lookup_arg(ea.cmd, len, NULL, NULL,
6818 NULL, &cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02006819 || lookup_script(ea.cmd, len, FALSE) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006820 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006821 {
6822 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006823 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006824 goto erret;
6825 continue;
6826 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006827 }
6828 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006829
6830 if (*ea.cmd == '[')
6831 {
6832 // [var, var] = expr
6833 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6834 if (line == NULL)
6835 goto erret;
6836 if (line != ea.cmd)
6837 continue;
6838 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006839 }
6840
6841 /*
6842 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006843 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006844 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006845 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006846 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006847 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02006848 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006849 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006850 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006851 if (!starts_with_colon)
6852 {
6853 emsg(_(e_colon_required_before_a_range));
6854 goto erret;
6855 }
6856 if (ends_excmd2(line, ea.cmd))
6857 {
6858 // A range without a command: jump to the line.
6859 // TODO: compile to a more efficient command, possibly
6860 // calling parse_cmd_address().
6861 ea.cmdidx = CMD_SIZE;
6862 line = compile_exec(line, &ea, &cctx);
6863 goto nextline;
6864 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006865 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006866 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006867 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006868 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006869 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006870
6871 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6872 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006873 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006874 {
6875 line += STRLEN(line);
6876 continue;
6877 }
6878
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006879 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006880 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006881 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006882 // CMD_let cannot happen, compile_assignment() above is used
6883 iemsg("Command from find_ex_command() not handled");
6884 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006885 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006886 }
6887
Bram Moolenaar3988f642020-08-27 22:43:03 +02006888 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006889 && ea.cmdidx != CMD_elseif
6890 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02006891 && ea.cmdidx != CMD_endif
6892 && ea.cmdidx != CMD_endfor
6893 && ea.cmdidx != CMD_endwhile
6894 && ea.cmdidx != CMD_catch
6895 && ea.cmdidx != CMD_finally
6896 && ea.cmdidx != CMD_endtry)
6897 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02006898 emsg(_(e_unreachable_code_after_return));
6899 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006900 }
6901
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006902 p = skipwhite(p);
6903 if (ea.cmdidx != CMD_SIZE
6904 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
6905 {
6906 ea.argt = excmd_get_argt(ea.cmdidx);
6907 if ((ea.argt & EX_BANG) && *p == '!')
6908 {
6909 ea.forceit = TRUE;
6910 p = skipwhite(p + 1);
6911 }
6912 }
6913
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006914 switch (ea.cmdidx)
6915 {
6916 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006917 ea.arg = p;
6918 line = compile_nested_function(&ea, &cctx);
6919 break;
6920
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006921 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006922 // TODO: should we allow this, e.g. to declare a global
6923 // function?
6924 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006925 goto erret;
6926
6927 case CMD_return:
6928 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006929 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006930 break;
6931
6932 case CMD_let:
6933 case CMD_const:
6934 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006935 if (line == p)
6936 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006937 break;
6938
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006939 case CMD_unlet:
6940 case CMD_unlockvar:
6941 case CMD_lockvar:
6942 line = compile_unletlock(p, &ea, &cctx);
6943 break;
6944
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006945 case CMD_import:
6946 line = compile_import(p, &cctx);
6947 break;
6948
6949 case CMD_if:
6950 line = compile_if(p, &cctx);
6951 break;
6952 case CMD_elseif:
6953 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006954 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006955 break;
6956 case CMD_else:
6957 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006958 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006959 break;
6960 case CMD_endif:
6961 line = compile_endif(p, &cctx);
6962 break;
6963
6964 case CMD_while:
6965 line = compile_while(p, &cctx);
6966 break;
6967 case CMD_endwhile:
6968 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006969 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006970 break;
6971
6972 case CMD_for:
6973 line = compile_for(p, &cctx);
6974 break;
6975 case CMD_endfor:
6976 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006977 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006978 break;
6979 case CMD_continue:
6980 line = compile_continue(p, &cctx);
6981 break;
6982 case CMD_break:
6983 line = compile_break(p, &cctx);
6984 break;
6985
6986 case CMD_try:
6987 line = compile_try(p, &cctx);
6988 break;
6989 case CMD_catch:
6990 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006991 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006992 break;
6993 case CMD_finally:
6994 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006995 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006996 break;
6997 case CMD_endtry:
6998 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006999 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007000 break;
7001 case CMD_throw:
7002 line = compile_throw(p, &cctx);
7003 break;
7004
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007005 case CMD_eval:
7006 if (compile_expr0(&p, &cctx) == FAIL)
7007 goto erret;
7008
Bram Moolenaar3988f642020-08-27 22:43:03 +02007009 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007010 generate_instr_drop(&cctx, ISN_DROP, 1);
7011
7012 line = skipwhite(p);
7013 break;
7014
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007015 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007016 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007017 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007018 case CMD_echomsg:
7019 case CMD_echoerr:
7020 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007021 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007022
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007023 case CMD_put:
7024 ea.cmd = cmd;
7025 line = compile_put(p, &ea, &cctx);
7026 break;
7027
Bram Moolenaar3988f642020-08-27 22:43:03 +02007028 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007029
Bram Moolenaarae616492020-07-28 20:07:27 +02007030 case CMD_append:
7031 case CMD_change:
7032 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007033 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007034 case CMD_xit:
7035 not_in_vim9(&ea);
7036 goto erret;
7037
Bram Moolenaar002262f2020-07-08 17:47:57 +02007038 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007039 if (cctx.ctx_skip != SKIP_YES)
7040 {
7041 semsg(_(e_invalid_command_str), ea.cmd);
7042 goto erret;
7043 }
7044 // We don't check for a next command here.
7045 line = (char_u *)"";
7046 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007047
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007048 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007049 if (cctx.ctx_skip == SKIP_YES)
7050 {
7051 // We don't check for a next command here.
7052 line = (char_u *)"";
7053 }
7054 else
7055 {
7056 // Not recognized, execute with do_cmdline_cmd().
7057 ea.arg = p;
7058 line = compile_exec(line, &ea, &cctx);
7059 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007060 break;
7061 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007062nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007063 if (line == NULL)
7064 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007065 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007066
7067 if (cctx.ctx_type_stack.ga_len < 0)
7068 {
7069 iemsg("Type stack underflow");
7070 goto erret;
7071 }
7072 }
7073
7074 if (cctx.ctx_scope != NULL)
7075 {
7076 if (cctx.ctx_scope->se_type == IF_SCOPE)
7077 emsg(_(e_endif));
7078 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7079 emsg(_(e_endwhile));
7080 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7081 emsg(_(e_endfor));
7082 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007083 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007084 goto erret;
7085 }
7086
Bram Moolenaarefd88552020-06-18 20:50:10 +02007087 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007088 {
7089 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7090 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007091 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007092 goto erret;
7093 }
7094
7095 // Return zero if there is no return at the end.
7096 generate_PUSHNR(&cctx, 0);
7097 generate_instr(&cctx, ISN_RETURN);
7098 }
7099
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007100 {
7101 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7102 + ufunc->uf_dfunc_idx;
7103 dfunc->df_deleted = FALSE;
7104 dfunc->df_instr = instr->ga_data;
7105 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007106 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007107 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007108 if (cctx.ctx_outer_used)
7109 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007110 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007111 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007112
7113 ret = OK;
7114
7115erret:
7116 if (ret == FAIL)
7117 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007118 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007119 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7120 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007121
7122 for (idx = 0; idx < instr->ga_len; ++idx)
7123 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007124 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007125
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007126 // If using the last entry in the table and it was added above, we
7127 // might as well remove it.
7128 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007129 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007130 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007131 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007132 ufunc->uf_dfunc_idx = 0;
7133 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007134 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007135
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007136 while (cctx.ctx_scope != NULL)
7137 drop_scope(&cctx);
7138
Bram Moolenaar20431c92020-03-20 18:39:46 +01007139 // Don't execute this function body.
7140 ga_clear_strings(&ufunc->uf_lines);
7141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007142 if (errormsg != NULL)
7143 emsg(errormsg);
7144 else if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007145 emsg(_(e_compile_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007146 }
7147
7148 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007149 if (do_estack_push)
7150 estack_pop();
7151
Bram Moolenaar20431c92020-03-20 18:39:46 +01007152 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007153 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007154 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007155 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007156}
7157
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007158 void
7159set_function_type(ufunc_T *ufunc)
7160{
7161 int varargs = ufunc->uf_va_name != NULL;
7162 int argcount = ufunc->uf_args.ga_len;
7163
7164 // Create a type for the function, with the return type and any
7165 // argument types.
7166 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7167 // The type is included in "tt_args".
7168 if (argcount > 0 || varargs)
7169 {
7170 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7171 argcount, &ufunc->uf_type_list);
7172 // Add argument types to the function type.
7173 if (func_type_add_arg_types(ufunc->uf_func_type,
7174 argcount + varargs,
7175 &ufunc->uf_type_list) == FAIL)
7176 return;
7177 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7178 ufunc->uf_func_type->tt_min_argcount =
7179 argcount - ufunc->uf_def_args.ga_len;
7180 if (ufunc->uf_arg_types == NULL)
7181 {
7182 int i;
7183
7184 // lambda does not have argument types.
7185 for (i = 0; i < argcount; ++i)
7186 ufunc->uf_func_type->tt_args[i] = &t_any;
7187 }
7188 else
7189 mch_memmove(ufunc->uf_func_type->tt_args,
7190 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7191 if (varargs)
7192 {
7193 ufunc->uf_func_type->tt_args[argcount] =
7194 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7195 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7196 }
7197 }
7198 else
7199 // No arguments, can use a predefined type.
7200 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7201 argcount, &ufunc->uf_type_list);
7202}
7203
7204
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007205/*
7206 * Delete an instruction, free what it contains.
7207 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007208 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007209delete_instr(isn_T *isn)
7210{
7211 switch (isn->isn_type)
7212 {
7213 case ISN_EXEC:
7214 case ISN_LOADENV:
7215 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007216 case ISN_LOADB:
7217 case ISN_LOADW:
7218 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007219 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007220 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007221 case ISN_PUSHEXC:
7222 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007223 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007224 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007225 case ISN_STOREB:
7226 case ISN_STOREW:
7227 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007228 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007229 vim_free(isn->isn_arg.string);
7230 break;
7231
7232 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007233 case ISN_STORES:
7234 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007235 break;
7236
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007237 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007238 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007239 vim_free(isn->isn_arg.unlet.ul_name);
7240 break;
7241
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007242 case ISN_STOREOPT:
7243 vim_free(isn->isn_arg.storeopt.so_name);
7244 break;
7245
7246 case ISN_PUSHBLOB: // push blob isn_arg.blob
7247 blob_unref(isn->isn_arg.blob);
7248 break;
7249
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007250 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007251#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007252 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007253#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007254 break;
7255
7256 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007257#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007258 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007259#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007260 break;
7261
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007262 case ISN_UCALL:
7263 vim_free(isn->isn_arg.ufunc.cuf_name);
7264 break;
7265
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007266 case ISN_FUNCREF:
7267 {
7268 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7269 + isn->isn_arg.funcref.fr_func;
7270 func_ptr_unref(dfunc->df_ufunc);
7271 }
7272 break;
7273
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007274 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007275 {
7276 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7277 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7278
7279 if (ufunc != NULL)
7280 {
7281 // Clear uf_dfunc_idx so that the function is deleted.
7282 clear_def_function(ufunc);
7283 ufunc->uf_dfunc_idx = 0;
7284 func_ptr_unref(ufunc);
7285 }
7286
7287 vim_free(lambda);
7288 vim_free(isn->isn_arg.newfunc.nf_global);
7289 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007290 break;
7291
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007292 case ISN_2BOOL:
7293 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007294 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007295 case ISN_ADDBLOB:
7296 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007297 case ISN_ANYINDEX:
7298 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007299 case ISN_BCALL:
7300 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007301 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007302 case ISN_CHECKNR:
7303 case ISN_CHECKTYPE:
7304 case ISN_COMPAREANY:
7305 case ISN_COMPAREBLOB:
7306 case ISN_COMPAREBOOL:
7307 case ISN_COMPAREDICT:
7308 case ISN_COMPAREFLOAT:
7309 case ISN_COMPAREFUNC:
7310 case ISN_COMPARELIST:
7311 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007312 case ISN_COMPARESPECIAL:
7313 case ISN_COMPARESTRING:
7314 case ISN_CONCAT:
7315 case ISN_DCALL:
7316 case ISN_DROP:
7317 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007318 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007319 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007320 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007321 case ISN_EXECCONCAT:
7322 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007323 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007324 case ISN_GETITEM:
7325 case ISN_JUMP:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007326 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007327 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007328 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007329 case ISN_LOADBDICT:
7330 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007331 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007332 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007333 case ISN_LOADSCRIPT:
7334 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007335 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007336 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007337 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007338 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007339 case ISN_NEGATENR:
7340 case ISN_NEWDICT:
7341 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007342 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007343 case ISN_OPFLOAT:
7344 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007345 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007346 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007347 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007348 case ISN_PUSHF:
7349 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007350 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007351 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007352 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007353 case ISN_SHUFFLE:
7354 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007355 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007356 case ISN_STOREDICT:
7357 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007358 case ISN_STORENR:
7359 case ISN_STOREOUTER:
7360 case ISN_STOREREG:
7361 case ISN_STORESCRIPT:
7362 case ISN_STOREV:
7363 case ISN_STRINDEX:
7364 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007365 case ISN_THROW:
7366 case ISN_TRY:
7367 // nothing allocated
7368 break;
7369 }
7370}
7371
7372/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007373 * Free all instructions for "dfunc".
7374 */
7375 static void
7376delete_def_function_contents(dfunc_T *dfunc)
7377{
7378 int idx;
7379
7380 ga_clear(&dfunc->df_def_args_isn);
7381
7382 if (dfunc->df_instr != NULL)
7383 {
7384 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7385 delete_instr(dfunc->df_instr + idx);
7386 VIM_CLEAR(dfunc->df_instr);
7387 }
7388
7389 dfunc->df_deleted = TRUE;
7390}
7391
7392/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007393 * When a user function is deleted, clear the contents of any associated def
7394 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007395 */
7396 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007397clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007398{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007399 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007400 {
7401 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7402 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007403
Bram Moolenaar20431c92020-03-20 18:39:46 +01007404 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007405 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007406 }
7407}
7408
7409#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007410/*
7411 * Free all functions defined with ":def".
7412 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007413 void
7414free_def_functions(void)
7415{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007416 int idx;
7417
7418 for (idx = 0; idx < def_functions.ga_len; ++idx)
7419 {
7420 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7421
7422 delete_def_function_contents(dfunc);
7423 }
7424
7425 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007426}
7427#endif
7428
7429
7430#endif // FEAT_EVAL