blob: 103e696bf03ce0609b4710f5fe3f72794a284d4b [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 Moolenaar93ad1472020-08-19 22:02:41 +02005069 // without operator type is 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)
5074 use_type = &t_void;
5075 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005076 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005077 == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005078 goto theend;
5079 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005080 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005081 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005082 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005083 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005084 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005085 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005086 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005087 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005088 goto theend;
5089 }
5090 else if (!has_type || dest == dest_option)
5091 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005092 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005093 goto theend;
5094 }
5095 else
5096 {
5097 // variables are always initialized
5098 if (ga_grow(instr, 1) == FAIL)
5099 goto theend;
5100 switch (member_type->tt_type)
5101 {
5102 case VAR_BOOL:
5103 generate_PUSHBOOL(cctx, VVAL_FALSE);
5104 break;
5105 case VAR_FLOAT:
5106#ifdef FEAT_FLOAT
5107 generate_PUSHF(cctx, 0.0);
5108#endif
5109 break;
5110 case VAR_STRING:
5111 generate_PUSHS(cctx, NULL);
5112 break;
5113 case VAR_BLOB:
5114 generate_PUSHBLOB(cctx, NULL);
5115 break;
5116 case VAR_FUNC:
5117 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5118 break;
5119 case VAR_LIST:
5120 generate_NEWLIST(cctx, 0);
5121 break;
5122 case VAR_DICT:
5123 generate_NEWDICT(cctx, 0);
5124 break;
5125 case VAR_JOB:
5126 generate_PUSHJOB(cctx, NULL);
5127 break;
5128 case VAR_CHANNEL:
5129 generate_PUSHCHANNEL(cctx, NULL);
5130 break;
5131 case VAR_NUMBER:
5132 case VAR_UNKNOWN:
5133 case VAR_ANY:
5134 case VAR_PARTIAL:
5135 case VAR_VOID:
5136 case VAR_SPECIAL: // cannot happen
5137 generate_PUSHNR(cctx, 0);
5138 break;
5139 }
5140 }
5141 if (var_count == 0)
5142 end = p;
5143 }
5144
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005145 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005146 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005147 break;
5148
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005149 if (oplen > 0 && *op != '=')
5150 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005151 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005152 type_T *stacktype;
5153
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005154 if (*op == '.')
5155 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005156 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005157 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005158 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005159 if (
5160#ifdef FEAT_FLOAT
5161 // If variable is float operation with number is OK.
5162 !(expected == &t_float && stacktype == &t_number) &&
5163#endif
5164 need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005165 goto theend;
5166
5167 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005168 {
5169 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5170 goto theend;
5171 }
5172 else if (*op == '+')
5173 {
5174 if (generate_add_instr(cctx,
5175 operator_type(member_type, stacktype),
5176 member_type, stacktype) == FAIL)
5177 goto theend;
5178 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005179 else if (generate_two_op(cctx, op) == FAIL)
5180 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005181 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005182
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005183 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005184 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005185 int r;
5186
5187 // Compile the "idx" in "var[idx]".
5188 if (new_local)
5189 --cctx->ctx_locals.ga_len;
5190 p = skipwhite(var_start + varlen + 1);
5191 r = compile_expr0(&p, cctx);
5192 if (new_local)
5193 ++cctx->ctx_locals.ga_len;
5194 if (r == FAIL)
5195 goto theend;
5196 if (*skipwhite(p) != ']')
5197 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005198 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005199 emsg(_(e_missbrac));
5200 goto theend;
5201 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005202 if (type == &t_any)
5203 {
5204 type_T *idx_type = ((type_T **)stack->ga_data)[
5205 stack->ga_len - 1];
5206 // Index on variable of unknown type: guess the type from the
5207 // index type: number is dict, otherwise dict.
5208 // TODO: should do the assignment at runtime
5209 if (idx_type->tt_type == VAR_NUMBER)
5210 type = &t_list_any;
5211 else
5212 type = &t_dict_any;
5213 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005214 if (type->tt_type == VAR_DICT
5215 && may_generate_2STRING(-1, cctx) == FAIL)
5216 goto theend;
5217 if (type->tt_type == VAR_LIST
5218 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005219 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005220 {
5221 emsg(_(e_number_exp));
5222 goto theend;
5223 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005224
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005225 // Load the dict or list. On the stack we then have:
5226 // - value
5227 // - index
5228 // - variable
5229 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005230
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005231 if (type->tt_type == VAR_LIST)
5232 {
5233 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5234 return FAIL;
5235 }
5236 else if (type->tt_type == VAR_DICT)
5237 {
5238 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5239 return FAIL;
5240 }
5241 else
5242 {
5243 emsg(_(e_listreq));
5244 goto theend;
5245 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005246 }
5247 else
5248 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005249 if (is_decl && eap->forceit && cmdidx == CMD_const
5250 && (dest == dest_script || dest == dest_local))
5251 // ":const! var": lock the value, but not referenced variables
5252 generate_LOCKCONST(cctx);
5253
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005254 switch (dest)
5255 {
5256 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005257 generate_STOREOPT(cctx, skip_option_env_lead(name),
5258 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005259 break;
5260 case dest_global:
5261 // include g: with the name, easier to execute that way
5262 generate_STORE(cctx, ISN_STOREG, 0, name);
5263 break;
5264 case dest_buffer:
5265 // include b: with the name, easier to execute that way
5266 generate_STORE(cctx, ISN_STOREB, 0, name);
5267 break;
5268 case dest_window:
5269 // include w: with the name, easier to execute that way
5270 generate_STORE(cctx, ISN_STOREW, 0, name);
5271 break;
5272 case dest_tab:
5273 // include t: with the name, easier to execute that way
5274 generate_STORE(cctx, ISN_STORET, 0, name);
5275 break;
5276 case dest_env:
5277 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5278 break;
5279 case dest_reg:
5280 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5281 break;
5282 case dest_vimvar:
5283 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5284 break;
5285 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005286 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005287 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005288 {
5289 char_u *name_s = name;
5290
5291 // Include s: in the name for store_var()
5292 if (name[1] != ':')
5293 {
5294 int len = (int)STRLEN(name) + 3;
5295
5296 name_s = alloc(len);
5297 if (name_s == NULL)
5298 name_s = name;
5299 else
5300 vim_snprintf((char *)name_s, len,
5301 "s:%s", name);
5302 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005303 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5304 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005305 if (name_s != name)
5306 vim_free(name_s);
5307 }
5308 else
5309 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005310 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005311 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005312 break;
5313 case dest_local:
5314 if (lvar != NULL)
5315 {
5316 isn_T *isn = ((isn_T *)instr->ga_data)
5317 + instr->ga_len - 1;
5318
5319 // optimization: turn "var = 123" from ISN_PUSHNR +
5320 // ISN_STORE into ISN_STORENR
5321 if (!lvar->lv_from_outer
5322 && instr->ga_len == instr_count + 1
5323 && isn->isn_type == ISN_PUSHNR)
5324 {
5325 varnumber_T val = isn->isn_arg.number;
5326
5327 isn->isn_type = ISN_STORENR;
5328 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5329 isn->isn_arg.storenr.stnr_val = val;
5330 if (stack->ga_len > 0)
5331 --stack->ga_len;
5332 }
5333 else if (lvar->lv_from_outer)
5334 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005335 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005336 else
5337 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5338 }
5339 break;
5340 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005341 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005342
5343 if (var_idx + 1 < var_count)
5344 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005345 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005346
5347 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005348 if (var_count > 0 && !semicolon)
5349 {
5350 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5351 goto theend;
5352 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005353
Bram Moolenaarb2097502020-07-19 17:17:02 +02005354 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005355
5356theend:
5357 vim_free(name);
5358 return ret;
5359}
5360
5361/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005362 * Check if "name" can be "unlet".
5363 */
5364 int
5365check_vim9_unlet(char_u *name)
5366{
5367 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5368 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005369 // "unlet s:var" is allowed in legacy script.
5370 if (*name == 's' && !script_is_vim9())
5371 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005372 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005373 return FAIL;
5374 }
5375 return OK;
5376}
5377
5378/*
5379 * Callback passed to ex_unletlock().
5380 */
5381 static int
5382compile_unlet(
5383 lval_T *lvp,
5384 char_u *name_end,
5385 exarg_T *eap,
5386 int deep UNUSED,
5387 void *coookie)
5388{
5389 cctx_T *cctx = coookie;
5390
5391 if (lvp->ll_tv == NULL)
5392 {
5393 char_u *p = lvp->ll_name;
5394 int cc = *name_end;
5395 int ret = OK;
5396
5397 // Normal name. Only supports g:, w:, t: and b: namespaces.
5398 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005399 if (*p == '$')
5400 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5401 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005402 ret = FAIL;
5403 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005404 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005405
5406 *name_end = cc;
5407 return ret;
5408 }
5409
5410 // TODO: unlet {list}[idx]
5411 // TODO: unlet {dict}[key]
5412 emsg("Sorry, :unlet not fully implemented yet");
5413 return FAIL;
5414}
5415
5416/*
5417 * compile "unlet var", "lock var" and "unlock var"
5418 * "arg" points to "var".
5419 */
5420 static char_u *
5421compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5422{
5423 char_u *p = arg;
5424
5425 if (eap->cmdidx != CMD_unlet)
5426 {
5427 emsg("Sorry, :lock and unlock not implemented yet");
5428 return NULL;
5429 }
5430
5431 if (*p == '!')
5432 {
5433 p = skipwhite(p + 1);
5434 eap->forceit = TRUE;
5435 }
5436
5437 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5438 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5439}
5440
5441/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005442 * Compile an :import command.
5443 */
5444 static char_u *
5445compile_import(char_u *arg, cctx_T *cctx)
5446{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005447 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005448}
5449
5450/*
5451 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5452 */
5453 static int
5454compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5455{
5456 garray_T *instr = &cctx->ctx_instr;
5457 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5458
5459 if (endlabel == NULL)
5460 return FAIL;
5461 endlabel->el_next = *el;
5462 *el = endlabel;
5463 endlabel->el_end_label = instr->ga_len;
5464
5465 generate_JUMP(cctx, when, 0);
5466 return OK;
5467}
5468
5469 static void
5470compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5471{
5472 garray_T *instr = &cctx->ctx_instr;
5473
5474 while (*el != NULL)
5475 {
5476 endlabel_T *cur = (*el);
5477 isn_T *isn;
5478
5479 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5480 isn->isn_arg.jump.jump_where = instr->ga_len;
5481 *el = cur->el_next;
5482 vim_free(cur);
5483 }
5484}
5485
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005486 static void
5487compile_free_jump_to_end(endlabel_T **el)
5488{
5489 while (*el != NULL)
5490 {
5491 endlabel_T *cur = (*el);
5492
5493 *el = cur->el_next;
5494 vim_free(cur);
5495 }
5496}
5497
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005498/*
5499 * Create a new scope and set up the generic items.
5500 */
5501 static scope_T *
5502new_scope(cctx_T *cctx, scopetype_T type)
5503{
5504 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5505
5506 if (scope == NULL)
5507 return NULL;
5508 scope->se_outer = cctx->ctx_scope;
5509 cctx->ctx_scope = scope;
5510 scope->se_type = type;
5511 scope->se_local_count = cctx->ctx_locals.ga_len;
5512 return scope;
5513}
5514
5515/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005516 * Free the current scope and go back to the outer scope.
5517 */
5518 static void
5519drop_scope(cctx_T *cctx)
5520{
5521 scope_T *scope = cctx->ctx_scope;
5522
5523 if (scope == NULL)
5524 {
5525 iemsg("calling drop_scope() without a scope");
5526 return;
5527 }
5528 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005529 switch (scope->se_type)
5530 {
5531 case IF_SCOPE:
5532 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5533 case FOR_SCOPE:
5534 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5535 case WHILE_SCOPE:
5536 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5537 case TRY_SCOPE:
5538 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5539 case NO_SCOPE:
5540 case BLOCK_SCOPE:
5541 break;
5542 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005543 vim_free(scope);
5544}
5545
5546/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005547 * compile "if expr"
5548 *
5549 * "if expr" Produces instructions:
5550 * EVAL expr Push result of "expr"
5551 * JUMP_IF_FALSE end
5552 * ... body ...
5553 * end:
5554 *
5555 * "if expr | else" Produces instructions:
5556 * EVAL expr Push result of "expr"
5557 * JUMP_IF_FALSE else
5558 * ... body ...
5559 * JUMP_ALWAYS end
5560 * else:
5561 * ... body ...
5562 * end:
5563 *
5564 * "if expr1 | elseif expr2 | else" Produces instructions:
5565 * EVAL expr Push result of "expr"
5566 * JUMP_IF_FALSE elseif
5567 * ... body ...
5568 * JUMP_ALWAYS end
5569 * elseif:
5570 * EVAL expr Push result of "expr"
5571 * JUMP_IF_FALSE else
5572 * ... body ...
5573 * JUMP_ALWAYS end
5574 * else:
5575 * ... body ...
5576 * end:
5577 */
5578 static char_u *
5579compile_if(char_u *arg, cctx_T *cctx)
5580{
5581 char_u *p = arg;
5582 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005583 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005584 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005585 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005586 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005587
Bram Moolenaara5565e42020-05-09 15:44:01 +02005588 CLEAR_FIELD(ppconst);
5589 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005590 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005591 clear_ppconst(&ppconst);
5592 return NULL;
5593 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005594 if (cctx->ctx_skip == SKIP_YES)
5595 clear_ppconst(&ppconst);
5596 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005597 {
5598 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005599 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005600 clear_ppconst(&ppconst);
5601 }
5602 else
5603 {
5604 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005605 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005606 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005607 return NULL;
5608 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005609
5610 scope = new_scope(cctx, IF_SCOPE);
5611 if (scope == NULL)
5612 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005613 scope->se_skip_save = skip_save;
5614 // "is_had_return" will be reset if any block does not end in :return
5615 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005616
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005617 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005618 {
5619 // "where" is set when ":elseif", "else" or ":endif" is found
5620 scope->se_u.se_if.is_if_label = instr->ga_len;
5621 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5622 }
5623 else
5624 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005625
5626 return p;
5627}
5628
5629 static char_u *
5630compile_elseif(char_u *arg, cctx_T *cctx)
5631{
5632 char_u *p = arg;
5633 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005634 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005635 isn_T *isn;
5636 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005637 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02005638 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005639
5640 if (scope == NULL || scope->se_type != IF_SCOPE)
5641 {
5642 emsg(_(e_elseif_without_if));
5643 return NULL;
5644 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005645 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005646 if (!cctx->ctx_had_return)
5647 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005648
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005649 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005650 {
5651 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005652 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005653 return NULL;
5654 // previous "if" or "elseif" jumps here
5655 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5656 isn->isn_arg.jump.jump_where = instr->ga_len;
5657 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005658
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005659 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005660 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02005661 if (cctx->ctx_skip == SKIP_YES)
5662 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005663 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005664 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005665 clear_ppconst(&ppconst);
5666 return NULL;
5667 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02005668 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005669 if (scope->se_skip_save == SKIP_YES)
5670 clear_ppconst(&ppconst);
5671 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005672 {
5673 // The expression results in a constant.
5674 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005675 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005676 clear_ppconst(&ppconst);
5677 scope->se_u.se_if.is_if_label = -1;
5678 }
5679 else
5680 {
5681 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005682 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005683 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005684 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005685
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005686 // "where" is set when ":elseif", "else" or ":endif" is found
5687 scope->se_u.se_if.is_if_label = instr->ga_len;
5688 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5689 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005690
5691 return p;
5692}
5693
5694 static char_u *
5695compile_else(char_u *arg, cctx_T *cctx)
5696{
5697 char_u *p = arg;
5698 garray_T *instr = &cctx->ctx_instr;
5699 isn_T *isn;
5700 scope_T *scope = cctx->ctx_scope;
5701
5702 if (scope == NULL || scope->se_type != IF_SCOPE)
5703 {
5704 emsg(_(e_else_without_if));
5705 return NULL;
5706 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005707 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005708 if (!cctx->ctx_had_return)
5709 scope->se_u.se_if.is_had_return = FALSE;
5710 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005711
Bram Moolenaarefd88552020-06-18 20:50:10 +02005712 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005713 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005714 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005715 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005716 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005717 if (!cctx->ctx_had_return
5718 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5719 JUMP_ALWAYS, cctx) == FAIL)
5720 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005721 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005722
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005723 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005724 {
5725 if (scope->se_u.se_if.is_if_label >= 0)
5726 {
5727 // previous "if" or "elseif" jumps here
5728 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5729 isn->isn_arg.jump.jump_where = instr->ga_len;
5730 scope->se_u.se_if.is_if_label = -1;
5731 }
5732 }
5733
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005734 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005735 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5736 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005737
5738 return p;
5739}
5740
5741 static char_u *
5742compile_endif(char_u *arg, cctx_T *cctx)
5743{
5744 scope_T *scope = cctx->ctx_scope;
5745 ifscope_T *ifscope;
5746 garray_T *instr = &cctx->ctx_instr;
5747 isn_T *isn;
5748
5749 if (scope == NULL || scope->se_type != IF_SCOPE)
5750 {
5751 emsg(_(e_endif_without_if));
5752 return NULL;
5753 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005754 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005755 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005756 if (!cctx->ctx_had_return)
5757 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005758
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005759 if (scope->se_u.se_if.is_if_label >= 0)
5760 {
5761 // previous "if" or "elseif" jumps here
5762 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5763 isn->isn_arg.jump.jump_where = instr->ga_len;
5764 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005765 // Fill in the "end" label in jumps at the end of the blocks.
5766 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005767 cctx->ctx_skip = scope->se_skip_save;
5768
5769 // If all the blocks end in :return and there is an :else then the
5770 // had_return flag is set.
5771 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005772
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005773 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005774 return arg;
5775}
5776
5777/*
5778 * compile "for var in expr"
5779 *
5780 * Produces instructions:
5781 * PUSHNR -1
5782 * STORE loop-idx Set index to -1
5783 * EVAL expr Push result of "expr"
5784 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5785 * - if beyond end, jump to "end"
5786 * - otherwise get item from list and push it
5787 * STORE var Store item in "var"
5788 * ... body ...
5789 * JUMP top Jump back to repeat
5790 * end: DROP Drop the result of "expr"
5791 *
5792 */
5793 static char_u *
5794compile_for(char_u *arg, cctx_T *cctx)
5795{
5796 char_u *p;
5797 size_t varlen;
5798 garray_T *instr = &cctx->ctx_instr;
5799 garray_T *stack = &cctx->ctx_type_stack;
5800 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005801 lvar_T *loop_lvar; // loop iteration variable
5802 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005803 type_T *vartype;
5804
5805 // TODO: list of variables: "for [key, value] in dict"
5806 // parse "var"
5807 for (p = arg; eval_isnamec1(*p); ++p)
5808 ;
5809 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005810 var_lvar = lookup_local(arg, varlen, cctx);
5811 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005812 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005813 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005814 return NULL;
5815 }
5816
5817 // consume "in"
5818 p = skipwhite(p);
5819 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5820 {
5821 emsg(_(e_missing_in));
5822 return NULL;
5823 }
5824 p = skipwhite(p + 2);
5825
5826
5827 scope = new_scope(cctx, FOR_SCOPE);
5828 if (scope == NULL)
5829 return NULL;
5830
5831 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005832 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5833 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005834 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005835 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005836 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005837 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005838 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005839
5840 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005841 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5842 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005843 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005844 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005845 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005846 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005847 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005848
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005849 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005850
5851 // compile "expr", it remains on the stack until "endfor"
5852 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005853 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005854 {
5855 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005856 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005857 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005858
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005859 // Now that we know the type of "var", check that it is a list, now or at
5860 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005861 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005862 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005863 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005864 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005865 return NULL;
5866 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005867 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005868 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005869
5870 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005871 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005872
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005873 generate_FOR(cctx, loop_lvar->lv_idx);
5874 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005875
5876 return arg;
5877}
5878
5879/*
5880 * compile "endfor"
5881 */
5882 static char_u *
5883compile_endfor(char_u *arg, cctx_T *cctx)
5884{
5885 garray_T *instr = &cctx->ctx_instr;
5886 scope_T *scope = cctx->ctx_scope;
5887 forscope_T *forscope;
5888 isn_T *isn;
5889
5890 if (scope == NULL || scope->se_type != FOR_SCOPE)
5891 {
5892 emsg(_(e_for));
5893 return NULL;
5894 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005895 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005896 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005897 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005898
5899 // At end of ":for" scope jump back to the FOR instruction.
5900 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5901
5902 // Fill in the "end" label in the FOR statement so it can jump here
5903 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5904 isn->isn_arg.forloop.for_end = instr->ga_len;
5905
5906 // Fill in the "end" label any BREAK statements
5907 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5908
5909 // Below the ":for" scope drop the "expr" list from the stack.
5910 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5911 return NULL;
5912
5913 vim_free(scope);
5914
5915 return arg;
5916}
5917
5918/*
5919 * compile "while expr"
5920 *
5921 * Produces instructions:
5922 * top: EVAL expr Push result of "expr"
5923 * JUMP_IF_FALSE end jump if false
5924 * ... body ...
5925 * JUMP top Jump back to repeat
5926 * end:
5927 *
5928 */
5929 static char_u *
5930compile_while(char_u *arg, cctx_T *cctx)
5931{
5932 char_u *p = arg;
5933 garray_T *instr = &cctx->ctx_instr;
5934 scope_T *scope;
5935
5936 scope = new_scope(cctx, WHILE_SCOPE);
5937 if (scope == NULL)
5938 return NULL;
5939
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005940 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005941
5942 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005943 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005944 return NULL;
5945
5946 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005947 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005948 JUMP_IF_FALSE, cctx) == FAIL)
5949 return FAIL;
5950
5951 return p;
5952}
5953
5954/*
5955 * compile "endwhile"
5956 */
5957 static char_u *
5958compile_endwhile(char_u *arg, cctx_T *cctx)
5959{
5960 scope_T *scope = cctx->ctx_scope;
5961
5962 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5963 {
5964 emsg(_(e_while));
5965 return NULL;
5966 }
5967 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005968 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005969
5970 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005971 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005972
5973 // Fill in the "end" label in the WHILE statement so it can jump here.
5974 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005975 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005976
5977 vim_free(scope);
5978
5979 return arg;
5980}
5981
5982/*
5983 * compile "continue"
5984 */
5985 static char_u *
5986compile_continue(char_u *arg, cctx_T *cctx)
5987{
5988 scope_T *scope = cctx->ctx_scope;
5989
5990 for (;;)
5991 {
5992 if (scope == NULL)
5993 {
5994 emsg(_(e_continue));
5995 return NULL;
5996 }
5997 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5998 break;
5999 scope = scope->se_outer;
6000 }
6001
6002 // Jump back to the FOR or WHILE instruction.
6003 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006004 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6005 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006006 return arg;
6007}
6008
6009/*
6010 * compile "break"
6011 */
6012 static char_u *
6013compile_break(char_u *arg, cctx_T *cctx)
6014{
6015 scope_T *scope = cctx->ctx_scope;
6016 endlabel_T **el;
6017
6018 for (;;)
6019 {
6020 if (scope == NULL)
6021 {
6022 emsg(_(e_break));
6023 return NULL;
6024 }
6025 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6026 break;
6027 scope = scope->se_outer;
6028 }
6029
6030 // Jump to the end of the FOR or WHILE loop.
6031 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006032 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006033 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006034 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006035 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6036 return FAIL;
6037
6038 return arg;
6039}
6040
6041/*
6042 * compile "{" start of block
6043 */
6044 static char_u *
6045compile_block(char_u *arg, cctx_T *cctx)
6046{
6047 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6048 return NULL;
6049 return skipwhite(arg + 1);
6050}
6051
6052/*
6053 * compile end of block: drop one scope
6054 */
6055 static void
6056compile_endblock(cctx_T *cctx)
6057{
6058 scope_T *scope = cctx->ctx_scope;
6059
6060 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006061 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006062 vim_free(scope);
6063}
6064
6065/*
6066 * compile "try"
6067 * Creates a new scope for the try-endtry, pointing to the first catch and
6068 * finally.
6069 * Creates another scope for the "try" block itself.
6070 * TRY instruction sets up exception handling at runtime.
6071 *
6072 * "try"
6073 * TRY -> catch1, -> finally push trystack entry
6074 * ... try block
6075 * "throw {exception}"
6076 * EVAL {exception}
6077 * THROW create exception
6078 * ... try block
6079 * " catch {expr}"
6080 * JUMP -> finally
6081 * catch1: PUSH exeception
6082 * EVAL {expr}
6083 * MATCH
6084 * JUMP nomatch -> catch2
6085 * CATCH remove exception
6086 * ... catch block
6087 * " catch"
6088 * JUMP -> finally
6089 * catch2: CATCH remove exception
6090 * ... catch block
6091 * " finally"
6092 * finally:
6093 * ... finally block
6094 * " endtry"
6095 * ENDTRY pop trystack entry, may rethrow
6096 */
6097 static char_u *
6098compile_try(char_u *arg, cctx_T *cctx)
6099{
6100 garray_T *instr = &cctx->ctx_instr;
6101 scope_T *try_scope;
6102 scope_T *scope;
6103
6104 // scope that holds the jumps that go to catch/finally/endtry
6105 try_scope = new_scope(cctx, TRY_SCOPE);
6106 if (try_scope == NULL)
6107 return NULL;
6108
6109 // "catch" is set when the first ":catch" is found.
6110 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006111 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006112 if (generate_instr(cctx, ISN_TRY) == NULL)
6113 return NULL;
6114
6115 // scope for the try block itself
6116 scope = new_scope(cctx, BLOCK_SCOPE);
6117 if (scope == NULL)
6118 return NULL;
6119
6120 return arg;
6121}
6122
6123/*
6124 * compile "catch {expr}"
6125 */
6126 static char_u *
6127compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6128{
6129 scope_T *scope = cctx->ctx_scope;
6130 garray_T *instr = &cctx->ctx_instr;
6131 char_u *p;
6132 isn_T *isn;
6133
6134 // end block scope from :try or :catch
6135 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6136 compile_endblock(cctx);
6137 scope = cctx->ctx_scope;
6138
6139 // Error if not in a :try scope
6140 if (scope == NULL || scope->se_type != TRY_SCOPE)
6141 {
6142 emsg(_(e_catch));
6143 return NULL;
6144 }
6145
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006146 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006147 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006148 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006149 return NULL;
6150 }
6151
6152 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006153 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006154 JUMP_ALWAYS, cctx) == FAIL)
6155 return NULL;
6156
6157 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006158 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006159 if (isn->isn_arg.try.try_catch == 0)
6160 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006161 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006162 {
6163 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006164 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006165 isn->isn_arg.jump.jump_where = instr->ga_len;
6166 }
6167
6168 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006169 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006170 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006171 scope->se_u.se_try.ts_caught_all = TRUE;
6172 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006173 }
6174 else
6175 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006176 char_u *end;
6177 char_u *pat;
6178 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006179 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006180 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006181
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006182 // Push v:exception, push {expr} and MATCH
6183 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6184
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006185 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006186 if (*end != *p)
6187 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006188 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006189 vim_free(tofree);
6190 return FAIL;
6191 }
6192 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006193 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006194 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006195 len = (int)(end - tofree);
6196 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006197 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006198 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006199 if (pat == NULL)
6200 return FAIL;
6201 if (generate_PUSHS(cctx, pat) == FAIL)
6202 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006203
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006204 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6205 return NULL;
6206
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006207 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006208 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6209 return NULL;
6210 }
6211
6212 if (generate_instr(cctx, ISN_CATCH) == NULL)
6213 return NULL;
6214
6215 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6216 return NULL;
6217 return p;
6218}
6219
6220 static char_u *
6221compile_finally(char_u *arg, cctx_T *cctx)
6222{
6223 scope_T *scope = cctx->ctx_scope;
6224 garray_T *instr = &cctx->ctx_instr;
6225 isn_T *isn;
6226
6227 // end block scope from :try or :catch
6228 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6229 compile_endblock(cctx);
6230 scope = cctx->ctx_scope;
6231
6232 // Error if not in a :try scope
6233 if (scope == NULL || scope->se_type != TRY_SCOPE)
6234 {
6235 emsg(_(e_finally));
6236 return NULL;
6237 }
6238
6239 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006240 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006241 if (isn->isn_arg.try.try_finally != 0)
6242 {
6243 emsg(_(e_finally_dup));
6244 return NULL;
6245 }
6246
6247 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006248 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006249
Bram Moolenaar585fea72020-04-02 22:33:21 +02006250 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006251 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006252 {
6253 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006254 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006255 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006256 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006257 }
6258
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006259 // TODO: set index in ts_finally_label jumps
6260
6261 return arg;
6262}
6263
6264 static char_u *
6265compile_endtry(char_u *arg, cctx_T *cctx)
6266{
6267 scope_T *scope = cctx->ctx_scope;
6268 garray_T *instr = &cctx->ctx_instr;
6269 isn_T *isn;
6270
6271 // end block scope from :catch or :finally
6272 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6273 compile_endblock(cctx);
6274 scope = cctx->ctx_scope;
6275
6276 // Error if not in a :try scope
6277 if (scope == NULL || scope->se_type != TRY_SCOPE)
6278 {
6279 if (scope == NULL)
6280 emsg(_(e_no_endtry));
6281 else if (scope->se_type == WHILE_SCOPE)
6282 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006283 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006284 emsg(_(e_endfor));
6285 else
6286 emsg(_(e_endif));
6287 return NULL;
6288 }
6289
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006290 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006291 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6292 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006293 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006294 return NULL;
6295 }
6296
6297 // Fill in the "end" label in jumps at the end of the blocks, if not done
6298 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006299 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006300
6301 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006302 if (isn->isn_arg.try.try_catch == 0)
6303 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006304 if (isn->isn_arg.try.try_finally == 0)
6305 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006306
6307 if (scope->se_u.se_try.ts_catch_label != 0)
6308 {
6309 // Last catch without match jumps here
6310 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6311 isn->isn_arg.jump.jump_where = instr->ga_len;
6312 }
6313
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006314 compile_endblock(cctx);
6315
6316 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6317 return NULL;
6318 return arg;
6319}
6320
6321/*
6322 * compile "throw {expr}"
6323 */
6324 static char_u *
6325compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6326{
6327 char_u *p = skipwhite(arg);
6328
Bram Moolenaara5565e42020-05-09 15:44:01 +02006329 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006330 return NULL;
6331 if (may_generate_2STRING(-1, cctx) == FAIL)
6332 return NULL;
6333 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6334 return NULL;
6335
6336 return p;
6337}
6338
6339/*
6340 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006341 * compile "echomsg expr"
6342 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006343 * compile "execute expr"
6344 */
6345 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006346compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006347{
6348 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006349 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006350 int count = 0;
6351
6352 for (;;)
6353 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006354 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006355 return NULL;
6356 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006357 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006358 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006359 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006360 break;
6361 }
6362
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006363 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6364 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6365 else if (cmdidx == CMD_execute)
6366 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6367 else if (cmdidx == CMD_echomsg)
6368 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6369 else
6370 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006371 return p;
6372}
6373
6374/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006375 * :put r
6376 * :put ={expr}
6377 */
6378 static char_u *
6379compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6380{
6381 char_u *line = arg;
6382 linenr_T lnum;
6383 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006384 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006385
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006386 eap->regname = *line;
6387
6388 if (eap->regname == '=')
6389 {
6390 char_u *p = line + 1;
6391
6392 if (compile_expr0(&p, cctx) == FAIL)
6393 return NULL;
6394 line = p;
6395 }
6396 else if (eap->regname != NUL)
6397 ++line;
6398
6399 // TODO: if the range is something like "$" need to evaluate at runtime
6400 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6401 return NULL;
6402 if (eap->addr_count == 0)
6403 lnum = -1;
6404 else
6405 lnum = eap->line2;
6406 if (above)
6407 --lnum;
6408
6409 generate_PUT(cctx, eap->regname, lnum);
6410 return line;
6411}
6412
6413/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006414 * A command that is not compiled, execute with legacy code.
6415 */
6416 static char_u *
6417compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6418{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006419 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006420 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006421 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006422
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006423 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006424 goto theend;
6425
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006426 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006427 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006428 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006429 int usefilter = FALSE;
6430
6431 has_expr = argt & (EX_XFILE | EX_EXPAND);
6432
6433 // If the command can be followed by a bar, find the bar and truncate
6434 // it, so that the following command can be compiled.
6435 // The '|' is overwritten with a NUL, it is put back below.
6436 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6437 && *eap->arg == '!')
6438 // :w !filter or :r !filter or :r! filter
6439 usefilter = TRUE;
6440 if ((argt & EX_TRLBAR) && !usefilter)
6441 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006442 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006443 separate_nextcmd(eap);
6444 if (eap->nextcmd != NULL)
6445 nextcmd = eap->nextcmd;
6446 }
6447 }
6448
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006449 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6450 {
6451 // expand filename in "syntax include [@group] filename"
6452 has_expr = TRUE;
6453 eap->arg = skipwhite(eap->arg + 7);
6454 if (*eap->arg == '@')
6455 eap->arg = skiptowhite(eap->arg);
6456 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006457
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006458 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006459 {
6460 int count = 0;
6461 char_u *start = skipwhite(line);
6462
6463 // :cmd xxx`=expr1`yyy`=expr2`zzz
6464 // PUSHS ":cmd xxx"
6465 // eval expr1
6466 // PUSHS "yyy"
6467 // eval expr2
6468 // PUSHS "zzz"
6469 // EXECCONCAT 5
6470 for (;;)
6471 {
6472 if (p > start)
6473 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006474 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006475 ++count;
6476 }
6477 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006478 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006479 return NULL;
6480 may_generate_2STRING(-1, cctx);
6481 ++count;
6482 p = skipwhite(p);
6483 if (*p != '`')
6484 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006485 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006486 return NULL;
6487 }
6488 start = p + 1;
6489
6490 p = (char_u *)strstr((char *)start, "`=");
6491 if (p == NULL)
6492 {
6493 if (*skipwhite(start) != NUL)
6494 {
6495 generate_PUSHS(cctx, vim_strsave(start));
6496 ++count;
6497 }
6498 break;
6499 }
6500 }
6501 generate_EXECCONCAT(cctx, count);
6502 }
6503 else
6504 generate_EXEC(cctx, line);
6505
6506theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006507 if (*nextcmd != NUL)
6508 {
6509 // the parser expects a pointer to the bar, put it back
6510 --nextcmd;
6511 *nextcmd = '|';
6512 }
6513
6514 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006515}
6516
6517/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006518 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006519 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006520 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006521 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006522add_def_function(ufunc_T *ufunc)
6523{
6524 dfunc_T *dfunc;
6525
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006526 if (def_functions.ga_len == 0)
6527 {
6528 // The first position is not used, so that a zero uf_dfunc_idx means it
6529 // wasn't set.
6530 if (ga_grow(&def_functions, 1) == FAIL)
6531 return FAIL;
6532 ++def_functions.ga_len;
6533 }
6534
Bram Moolenaar09689a02020-05-09 22:50:08 +02006535 // Add the function to "def_functions".
6536 if (ga_grow(&def_functions, 1) == FAIL)
6537 return FAIL;
6538 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6539 CLEAR_POINTER(dfunc);
6540 dfunc->df_idx = def_functions.ga_len;
6541 ufunc->uf_dfunc_idx = dfunc->df_idx;
6542 dfunc->df_ufunc = ufunc;
6543 ++def_functions.ga_len;
6544 return OK;
6545}
6546
6547/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006548 * After ex_function() has collected all the function lines: parse and compile
6549 * the lines into instructions.
6550 * Adds the function to "def_functions".
6551 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6552 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006553 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006554 * This can be used recursively through compile_lambda(), which may reallocate
6555 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006556 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006557 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006558 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006559compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006560{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006561 char_u *line = NULL;
6562 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006563 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006564 cctx_T cctx;
6565 garray_T *instr;
6566 int called_emsg_before = called_emsg;
6567 int ret = FAIL;
6568 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006569 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006570 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006571 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006572
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006573 // When using a function that was compiled before: Free old instructions.
6574 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006575 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006576 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006577 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6578 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006579 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006580 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006581 else
6582 {
6583 if (add_def_function(ufunc) == FAIL)
6584 return FAIL;
6585 new_def_function = TRUE;
6586 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006587
Bram Moolenaar985116a2020-07-12 17:31:09 +02006588 ufunc->uf_def_status = UF_COMPILING;
6589
Bram Moolenaara80faa82020-04-12 19:37:17 +02006590 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006591 cctx.ctx_ufunc = ufunc;
6592 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006593 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006594 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6595 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6596 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6597 cctx.ctx_type_list = &ufunc->uf_type_list;
6598 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6599 instr = &cctx.ctx_instr;
6600
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006601 // Set the context to the function, it may be compiled when called from
6602 // another script. Set the script version to the most modern one.
6603 // The line number will be set in next_line_from_context().
6604 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006605 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6606
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006607 // Make sure error messages are OK.
6608 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6609 if (do_estack_push)
6610 estack_push_ufunc(ufunc, 1);
6611
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006612 if (ufunc->uf_def_args.ga_len > 0)
6613 {
6614 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006615 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006616 int i;
6617 char_u *arg;
6618 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006619 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006620
6621 // Produce instructions for the default values of optional arguments.
6622 // Store the instruction index in uf_def_arg_idx[] so that we know
6623 // where to start when the function is called, depending on the number
6624 // of arguments.
6625 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6626 if (ufunc->uf_def_arg_idx == NULL)
6627 goto erret;
6628 for (i = 0; i < count; ++i)
6629 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006630 garray_T *stack = &cctx.ctx_type_stack;
6631 type_T *val_type;
6632 int arg_idx = first_def_arg + i;
6633
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006634 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6635 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006636 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006637 goto erret;
6638
6639 // If no type specified use the type of the default value.
6640 // Otherwise check that the default value type matches the
6641 // specified type.
6642 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6643 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006644 {
6645 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006646 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006647 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02006648 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
6649 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006650 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006651
6652 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006653 goto erret;
6654 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006655 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006656
6657 if (did_set_arg_type)
6658 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006659 }
6660
6661 /*
6662 * Loop over all the lines of the function and generate instructions.
6663 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006664 for (;;)
6665 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006666 exarg_T ea;
6667 cmdmod_T save_cmdmod;
6668 int starts_with_colon = FALSE;
6669 char_u *cmd;
6670 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006671
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006672 // Bail out on the first error to avoid a flood of errors and report
6673 // the right line number when inside try/catch.
6674 if (emsg_before != called_emsg)
6675 goto erret;
6676
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006677 if (line != NULL && *line == '|')
6678 // the line continues after a '|'
6679 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006680 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006681 && !(*line == '#' && (line == cctx.ctx_line_start
6682 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006683 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006684 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006685 goto erret;
6686 }
6687 else
6688 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006689 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006690 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006691 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006692 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006693 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006694 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006695
Bram Moolenaara80faa82020-04-12 19:37:17 +02006696 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006697 ea.cmdlinep = &line;
6698 ea.cmd = skipwhite(line);
6699
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006700 // Some things can be recognized by the first character.
6701 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006702 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006703 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006704 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006705 if (ea.cmd[1] != '{')
6706 {
6707 line = (char_u *)"";
6708 continue;
6709 }
6710 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006711
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006712 case '}':
6713 {
6714 // "}" ends a block scope
6715 scopetype_T stype = cctx.ctx_scope == NULL
6716 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006717
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006718 if (stype == BLOCK_SCOPE)
6719 {
6720 compile_endblock(&cctx);
6721 line = ea.cmd;
6722 }
6723 else
6724 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006725 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006726 goto erret;
6727 }
6728 if (line != NULL)
6729 line = skipwhite(ea.cmd + 1);
6730 continue;
6731 }
6732
6733 case '{':
6734 // "{" starts a block scope
6735 // "{'a': 1}->func() is something else
6736 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6737 {
6738 line = compile_block(ea.cmd, &cctx);
6739 continue;
6740 }
6741 break;
6742
6743 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006744 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006745 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006746 }
6747
6748 /*
6749 * COMMAND MODIFIERS
6750 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006751 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006752 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6753 {
6754 if (errormsg != NULL)
6755 goto erret;
6756 // empty line or comment
6757 line = (char_u *)"";
6758 continue;
6759 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006760 // TODO: use modifiers in the command
6761 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006762 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006763
6764 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006765 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006766 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006767 {
6768 if (*ea.cmd == '(')
6769 // not for "call()"
6770 ea.cmd = p;
6771 else
6772 ea.cmd = skipwhite(ea.cmd);
6773 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006774
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006775 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006776 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006777 char_u *pskip;
6778
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006779 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006780 // find what follows.
6781 // Skip over "var.member", "var[idx]" and the like.
6782 // Also "&opt = val", "$ENV = val" and "@r = val".
6783 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006784 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006785 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006786 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006787 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006788 char_u *var_end;
6789 int oplen;
6790 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006791
Bram Moolenaar65821722020-08-02 18:58:54 +02006792 if (ea.cmd[0] == '@')
6793 var_end = ea.cmd + 2;
6794 else
6795 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006796 FNE_CHECK_START | FNE_INCL_BR);
6797 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006798 if (oplen > 0)
6799 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006800 size_t len = p - ea.cmd;
6801
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006802 // Recognize an assignment if we recognize the variable
6803 // name:
6804 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006805 // "local = expr" where "local" is a local var.
6806 // "script = expr" where "script" is a script-local var.
6807 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006808 // "&opt = expr"
6809 // "$ENV = expr"
6810 // "@r = expr"
6811 if (*ea.cmd == '&'
6812 || *ea.cmd == '$'
6813 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006814 || ((len) > 2 && ea.cmd[1] == ':')
6815 || lookup_local(ea.cmd, len, &cctx) != NULL
6816 || lookup_arg(ea.cmd, len, NULL, NULL,
6817 NULL, &cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02006818 || lookup_script(ea.cmd, len, FALSE) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006819 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006820 {
6821 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006822 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006823 goto erret;
6824 continue;
6825 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006826 }
6827 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006828
6829 if (*ea.cmd == '[')
6830 {
6831 // [var, var] = expr
6832 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6833 if (line == NULL)
6834 goto erret;
6835 if (line != ea.cmd)
6836 continue;
6837 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006838 }
6839
6840 /*
6841 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006842 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006843 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006844 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006845 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006846 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02006847 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006848 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006849 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006850 if (!starts_with_colon)
6851 {
6852 emsg(_(e_colon_required_before_a_range));
6853 goto erret;
6854 }
6855 if (ends_excmd2(line, ea.cmd))
6856 {
6857 // A range without a command: jump to the line.
6858 // TODO: compile to a more efficient command, possibly
6859 // calling parse_cmd_address().
6860 ea.cmdidx = CMD_SIZE;
6861 line = compile_exec(line, &ea, &cctx);
6862 goto nextline;
6863 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006864 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006865 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006866 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006867 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006868 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006869
6870 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6871 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006872 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006873 {
6874 line += STRLEN(line);
6875 continue;
6876 }
6877
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006878 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006879 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006880 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006881 // CMD_let cannot happen, compile_assignment() above is used
6882 iemsg("Command from find_ex_command() not handled");
6883 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006884 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006885 }
6886
Bram Moolenaar3988f642020-08-27 22:43:03 +02006887 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006888 && ea.cmdidx != CMD_elseif
6889 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02006890 && ea.cmdidx != CMD_endif
6891 && ea.cmdidx != CMD_endfor
6892 && ea.cmdidx != CMD_endwhile
6893 && ea.cmdidx != CMD_catch
6894 && ea.cmdidx != CMD_finally
6895 && ea.cmdidx != CMD_endtry)
6896 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02006897 emsg(_(e_unreachable_code_after_return));
6898 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006899 }
6900
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006901 p = skipwhite(p);
6902 if (ea.cmdidx != CMD_SIZE
6903 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
6904 {
6905 ea.argt = excmd_get_argt(ea.cmdidx);
6906 if ((ea.argt & EX_BANG) && *p == '!')
6907 {
6908 ea.forceit = TRUE;
6909 p = skipwhite(p + 1);
6910 }
6911 }
6912
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006913 switch (ea.cmdidx)
6914 {
6915 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006916 ea.arg = p;
6917 line = compile_nested_function(&ea, &cctx);
6918 break;
6919
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006920 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006921 // TODO: should we allow this, e.g. to declare a global
6922 // function?
6923 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006924 goto erret;
6925
6926 case CMD_return:
6927 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006928 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006929 break;
6930
6931 case CMD_let:
6932 case CMD_const:
6933 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006934 if (line == p)
6935 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006936 break;
6937
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006938 case CMD_unlet:
6939 case CMD_unlockvar:
6940 case CMD_lockvar:
6941 line = compile_unletlock(p, &ea, &cctx);
6942 break;
6943
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006944 case CMD_import:
6945 line = compile_import(p, &cctx);
6946 break;
6947
6948 case CMD_if:
6949 line = compile_if(p, &cctx);
6950 break;
6951 case CMD_elseif:
6952 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006953 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006954 break;
6955 case CMD_else:
6956 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006957 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006958 break;
6959 case CMD_endif:
6960 line = compile_endif(p, &cctx);
6961 break;
6962
6963 case CMD_while:
6964 line = compile_while(p, &cctx);
6965 break;
6966 case CMD_endwhile:
6967 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006968 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006969 break;
6970
6971 case CMD_for:
6972 line = compile_for(p, &cctx);
6973 break;
6974 case CMD_endfor:
6975 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006976 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006977 break;
6978 case CMD_continue:
6979 line = compile_continue(p, &cctx);
6980 break;
6981 case CMD_break:
6982 line = compile_break(p, &cctx);
6983 break;
6984
6985 case CMD_try:
6986 line = compile_try(p, &cctx);
6987 break;
6988 case CMD_catch:
6989 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006990 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006991 break;
6992 case CMD_finally:
6993 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006994 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006995 break;
6996 case CMD_endtry:
6997 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006998 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006999 break;
7000 case CMD_throw:
7001 line = compile_throw(p, &cctx);
7002 break;
7003
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007004 case CMD_eval:
7005 if (compile_expr0(&p, &cctx) == FAIL)
7006 goto erret;
7007
Bram Moolenaar3988f642020-08-27 22:43:03 +02007008 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007009 generate_instr_drop(&cctx, ISN_DROP, 1);
7010
7011 line = skipwhite(p);
7012 break;
7013
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007014 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007015 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007016 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007017 case CMD_echomsg:
7018 case CMD_echoerr:
7019 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007020 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007021
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007022 case CMD_put:
7023 ea.cmd = cmd;
7024 line = compile_put(p, &ea, &cctx);
7025 break;
7026
Bram Moolenaar3988f642020-08-27 22:43:03 +02007027 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007028
Bram Moolenaarae616492020-07-28 20:07:27 +02007029 case CMD_append:
7030 case CMD_change:
7031 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007032 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007033 case CMD_xit:
7034 not_in_vim9(&ea);
7035 goto erret;
7036
Bram Moolenaar002262f2020-07-08 17:47:57 +02007037 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007038 if (cctx.ctx_skip != SKIP_YES)
7039 {
7040 semsg(_(e_invalid_command_str), ea.cmd);
7041 goto erret;
7042 }
7043 // We don't check for a next command here.
7044 line = (char_u *)"";
7045 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007046
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007047 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007048 if (cctx.ctx_skip == SKIP_YES)
7049 {
7050 // We don't check for a next command here.
7051 line = (char_u *)"";
7052 }
7053 else
7054 {
7055 // Not recognized, execute with do_cmdline_cmd().
7056 ea.arg = p;
7057 line = compile_exec(line, &ea, &cctx);
7058 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007059 break;
7060 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007061nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007062 if (line == NULL)
7063 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007064 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007065
7066 if (cctx.ctx_type_stack.ga_len < 0)
7067 {
7068 iemsg("Type stack underflow");
7069 goto erret;
7070 }
7071 }
7072
7073 if (cctx.ctx_scope != NULL)
7074 {
7075 if (cctx.ctx_scope->se_type == IF_SCOPE)
7076 emsg(_(e_endif));
7077 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7078 emsg(_(e_endwhile));
7079 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7080 emsg(_(e_endfor));
7081 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007082 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007083 goto erret;
7084 }
7085
Bram Moolenaarefd88552020-06-18 20:50:10 +02007086 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007087 {
7088 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7089 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007090 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007091 goto erret;
7092 }
7093
7094 // Return zero if there is no return at the end.
7095 generate_PUSHNR(&cctx, 0);
7096 generate_instr(&cctx, ISN_RETURN);
7097 }
7098
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007099 {
7100 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7101 + ufunc->uf_dfunc_idx;
7102 dfunc->df_deleted = FALSE;
7103 dfunc->df_instr = instr->ga_data;
7104 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007105 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007106 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007107 if (cctx.ctx_outer_used)
7108 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007109 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007110 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007111
7112 ret = OK;
7113
7114erret:
7115 if (ret == FAIL)
7116 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007117 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007118 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7119 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007120
7121 for (idx = 0; idx < instr->ga_len; ++idx)
7122 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007123 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007124
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007125 // If using the last entry in the table and it was added above, we
7126 // might as well remove it.
7127 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007128 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007129 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007130 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007131 ufunc->uf_dfunc_idx = 0;
7132 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007133 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007134
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007135 while (cctx.ctx_scope != NULL)
7136 drop_scope(&cctx);
7137
Bram Moolenaar20431c92020-03-20 18:39:46 +01007138 // Don't execute this function body.
7139 ga_clear_strings(&ufunc->uf_lines);
7140
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007141 if (errormsg != NULL)
7142 emsg(errormsg);
7143 else if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007144 emsg(_(e_compile_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007145 }
7146
7147 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007148 if (do_estack_push)
7149 estack_pop();
7150
Bram Moolenaar20431c92020-03-20 18:39:46 +01007151 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007152 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007153 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007154 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007155}
7156
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007157 void
7158set_function_type(ufunc_T *ufunc)
7159{
7160 int varargs = ufunc->uf_va_name != NULL;
7161 int argcount = ufunc->uf_args.ga_len;
7162
7163 // Create a type for the function, with the return type and any
7164 // argument types.
7165 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7166 // The type is included in "tt_args".
7167 if (argcount > 0 || varargs)
7168 {
7169 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7170 argcount, &ufunc->uf_type_list);
7171 // Add argument types to the function type.
7172 if (func_type_add_arg_types(ufunc->uf_func_type,
7173 argcount + varargs,
7174 &ufunc->uf_type_list) == FAIL)
7175 return;
7176 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7177 ufunc->uf_func_type->tt_min_argcount =
7178 argcount - ufunc->uf_def_args.ga_len;
7179 if (ufunc->uf_arg_types == NULL)
7180 {
7181 int i;
7182
7183 // lambda does not have argument types.
7184 for (i = 0; i < argcount; ++i)
7185 ufunc->uf_func_type->tt_args[i] = &t_any;
7186 }
7187 else
7188 mch_memmove(ufunc->uf_func_type->tt_args,
7189 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7190 if (varargs)
7191 {
7192 ufunc->uf_func_type->tt_args[argcount] =
7193 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7194 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7195 }
7196 }
7197 else
7198 // No arguments, can use a predefined type.
7199 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7200 argcount, &ufunc->uf_type_list);
7201}
7202
7203
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007204/*
7205 * Delete an instruction, free what it contains.
7206 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007207 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007208delete_instr(isn_T *isn)
7209{
7210 switch (isn->isn_type)
7211 {
7212 case ISN_EXEC:
7213 case ISN_LOADENV:
7214 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007215 case ISN_LOADB:
7216 case ISN_LOADW:
7217 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007218 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007219 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007220 case ISN_PUSHEXC:
7221 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007222 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007223 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007224 case ISN_STOREB:
7225 case ISN_STOREW:
7226 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007227 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007228 vim_free(isn->isn_arg.string);
7229 break;
7230
7231 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007232 case ISN_STORES:
7233 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007234 break;
7235
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007236 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007237 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007238 vim_free(isn->isn_arg.unlet.ul_name);
7239 break;
7240
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007241 case ISN_STOREOPT:
7242 vim_free(isn->isn_arg.storeopt.so_name);
7243 break;
7244
7245 case ISN_PUSHBLOB: // push blob isn_arg.blob
7246 blob_unref(isn->isn_arg.blob);
7247 break;
7248
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007249 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007250#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007251 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007252#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007253 break;
7254
7255 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007256#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007257 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007258#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007259 break;
7260
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007261 case ISN_UCALL:
7262 vim_free(isn->isn_arg.ufunc.cuf_name);
7263 break;
7264
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007265 case ISN_FUNCREF:
7266 {
7267 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7268 + isn->isn_arg.funcref.fr_func;
7269 func_ptr_unref(dfunc->df_ufunc);
7270 }
7271 break;
7272
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007273 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007274 {
7275 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7276 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7277
7278 if (ufunc != NULL)
7279 {
7280 // Clear uf_dfunc_idx so that the function is deleted.
7281 clear_def_function(ufunc);
7282 ufunc->uf_dfunc_idx = 0;
7283 func_ptr_unref(ufunc);
7284 }
7285
7286 vim_free(lambda);
7287 vim_free(isn->isn_arg.newfunc.nf_global);
7288 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007289 break;
7290
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007291 case ISN_2BOOL:
7292 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007293 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007294 case ISN_ADDBLOB:
7295 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007296 case ISN_ANYINDEX:
7297 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007298 case ISN_BCALL:
7299 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007300 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007301 case ISN_CHECKNR:
7302 case ISN_CHECKTYPE:
7303 case ISN_COMPAREANY:
7304 case ISN_COMPAREBLOB:
7305 case ISN_COMPAREBOOL:
7306 case ISN_COMPAREDICT:
7307 case ISN_COMPAREFLOAT:
7308 case ISN_COMPAREFUNC:
7309 case ISN_COMPARELIST:
7310 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007311 case ISN_COMPARESPECIAL:
7312 case ISN_COMPARESTRING:
7313 case ISN_CONCAT:
7314 case ISN_DCALL:
7315 case ISN_DROP:
7316 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007317 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007318 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007319 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007320 case ISN_EXECCONCAT:
7321 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007322 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007323 case ISN_GETITEM:
7324 case ISN_JUMP:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007325 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007326 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007327 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007328 case ISN_LOADBDICT:
7329 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007330 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007331 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007332 case ISN_LOADSCRIPT:
7333 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007334 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007335 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007336 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007337 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007338 case ISN_NEGATENR:
7339 case ISN_NEWDICT:
7340 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007341 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007342 case ISN_OPFLOAT:
7343 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007344 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007345 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007346 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007347 case ISN_PUSHF:
7348 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007349 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007350 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007351 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007352 case ISN_SHUFFLE:
7353 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007354 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007355 case ISN_STOREDICT:
7356 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007357 case ISN_STORENR:
7358 case ISN_STOREOUTER:
7359 case ISN_STOREREG:
7360 case ISN_STORESCRIPT:
7361 case ISN_STOREV:
7362 case ISN_STRINDEX:
7363 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007364 case ISN_THROW:
7365 case ISN_TRY:
7366 // nothing allocated
7367 break;
7368 }
7369}
7370
7371/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007372 * Free all instructions for "dfunc".
7373 */
7374 static void
7375delete_def_function_contents(dfunc_T *dfunc)
7376{
7377 int idx;
7378
7379 ga_clear(&dfunc->df_def_args_isn);
7380
7381 if (dfunc->df_instr != NULL)
7382 {
7383 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7384 delete_instr(dfunc->df_instr + idx);
7385 VIM_CLEAR(dfunc->df_instr);
7386 }
7387
7388 dfunc->df_deleted = TRUE;
7389}
7390
7391/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007392 * When a user function is deleted, clear the contents of any associated def
7393 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007394 */
7395 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007396clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007397{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007398 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007399 {
7400 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7401 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007402
Bram Moolenaar20431c92020-03-20 18:39:46 +01007403 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007404 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007405 }
7406}
7407
7408#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007409/*
7410 * Free all functions defined with ":def".
7411 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007412 void
7413free_def_functions(void)
7414{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007415 int idx;
7416
7417 for (idx = 0; idx < def_functions.ga_len; ++idx)
7418 {
7419 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7420
7421 delete_def_function_contents(dfunc);
7422 }
7423
7424 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007425}
7426#endif
7427
7428
7429#endif // FEAT_EVAL