blob: 4ef3fdccb2f4cf0606f82725546240190d102fb5 [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 Moolenaar8a7d6542020-01-26 15:56:19 +01001112 * Generate an ISN_LOADS instruction.
1113 */
1114 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001115generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001117 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001118 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001119 int sid,
1120 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001121{
1122 isn_T *isn;
1123
Bram Moolenaar080457c2020-03-03 21:53:32 +01001124 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001125 if (isn_type == ISN_LOADS)
1126 isn = generate_instr_type(cctx, isn_type, type);
1127 else
1128 isn = generate_instr_drop(cctx, isn_type, 1);
1129 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001130 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001131 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1132 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001133
1134 return OK;
1135}
1136
1137/*
1138 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1139 */
1140 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001141generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142 cctx_T *cctx,
1143 isntype_T isn_type,
1144 int sid,
1145 int idx,
1146 type_T *type)
1147{
1148 isn_T *isn;
1149
Bram Moolenaar080457c2020-03-03 21:53:32 +01001150 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001151 if (isn_type == ISN_LOADSCRIPT)
1152 isn = generate_instr_type(cctx, isn_type, type);
1153 else
1154 isn = generate_instr_drop(cctx, isn_type, 1);
1155 if (isn == NULL)
1156 return FAIL;
1157 isn->isn_arg.script.script_sid = sid;
1158 isn->isn_arg.script.script_idx = idx;
1159 return OK;
1160}
1161
1162/*
1163 * Generate an ISN_NEWLIST instruction.
1164 */
1165 static int
1166generate_NEWLIST(cctx_T *cctx, int count)
1167{
1168 isn_T *isn;
1169 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001170 type_T *type;
1171 type_T *member;
1172
Bram Moolenaar080457c2020-03-03 21:53:32 +01001173 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001174 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1175 return FAIL;
1176 isn->isn_arg.number = count;
1177
Bram Moolenaar127542b2020-08-09 17:22:04 +02001178 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001179 if (count == 0)
1180 member = &t_void;
1181 else
1182 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001183 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1184 cctx->ctx_type_list);
1185 type = get_list_type(member, cctx->ctx_type_list);
1186
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001187 // drop the value types
1188 stack->ga_len -= count;
1189
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190 // add the list type to the type stack
1191 if (ga_grow(stack, 1) == FAIL)
1192 return FAIL;
1193 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1194 ++stack->ga_len;
1195
1196 return OK;
1197}
1198
1199/*
1200 * Generate an ISN_NEWDICT instruction.
1201 */
1202 static int
1203generate_NEWDICT(cctx_T *cctx, int count)
1204{
1205 isn_T *isn;
1206 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001207 type_T *type;
1208 type_T *member;
1209
Bram Moolenaar080457c2020-03-03 21:53:32 +01001210 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001211 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1212 return FAIL;
1213 isn->isn_arg.number = count;
1214
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001215 if (count == 0)
1216 member = &t_void;
1217 else
1218 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001219 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1220 cctx->ctx_type_list);
1221 type = get_dict_type(member, cctx->ctx_type_list);
1222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001223 // drop the key and value types
1224 stack->ga_len -= 2 * count;
1225
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001226 // add the dict type to the type stack
1227 if (ga_grow(stack, 1) == FAIL)
1228 return FAIL;
1229 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1230 ++stack->ga_len;
1231
1232 return OK;
1233}
1234
1235/*
1236 * Generate an ISN_FUNCREF instruction.
1237 */
1238 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001239generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240{
1241 isn_T *isn;
1242 garray_T *stack = &cctx->ctx_type_stack;
1243
Bram Moolenaar080457c2020-03-03 21:53:32 +01001244 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001245 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1246 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001247 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001248 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001249
1250 if (ga_grow(stack, 1) == FAIL)
1251 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001252 ((type_T **)stack->ga_data)[stack->ga_len] =
1253 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001254 ++stack->ga_len;
1255
1256 return OK;
1257}
1258
1259/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001260 * Generate an ISN_NEWFUNC instruction.
1261 */
1262 static int
1263generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1264{
1265 isn_T *isn;
1266 char_u *name;
1267
1268 RETURN_OK_IF_SKIP(cctx);
1269 name = vim_strsave(lambda_name);
1270 if (name == NULL)
1271 return FAIL;
1272 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1273 return FAIL;
1274 isn->isn_arg.newfunc.nf_lambda = name;
1275 isn->isn_arg.newfunc.nf_global = func_name;
1276
1277 return OK;
1278}
1279
1280/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001281 * Generate an ISN_JUMP instruction.
1282 */
1283 static int
1284generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1285{
1286 isn_T *isn;
1287 garray_T *stack = &cctx->ctx_type_stack;
1288
Bram Moolenaar080457c2020-03-03 21:53:32 +01001289 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001290 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1291 return FAIL;
1292 isn->isn_arg.jump.jump_when = when;
1293 isn->isn_arg.jump.jump_where = where;
1294
1295 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1296 --stack->ga_len;
1297
1298 return OK;
1299}
1300
1301 static int
1302generate_FOR(cctx_T *cctx, int loop_idx)
1303{
1304 isn_T *isn;
1305 garray_T *stack = &cctx->ctx_type_stack;
1306
Bram Moolenaar080457c2020-03-03 21:53:32 +01001307 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001308 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1309 return FAIL;
1310 isn->isn_arg.forloop.for_idx = loop_idx;
1311
1312 if (ga_grow(stack, 1) == FAIL)
1313 return FAIL;
1314 // type doesn't matter, will be stored next
1315 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1316 ++stack->ga_len;
1317
1318 return OK;
1319}
1320
1321/*
1322 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001323 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001324 * Return FAIL if the number of arguments is wrong.
1325 */
1326 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001327generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328{
1329 isn_T *isn;
1330 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001331 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001332 type_T *argtypes[MAX_FUNC_ARGS];
1333 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001334
Bram Moolenaar080457c2020-03-03 21:53:32 +01001335 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001336 argoff = check_internal_func(func_idx, argcount);
1337 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338 return FAIL;
1339
Bram Moolenaar389df252020-07-09 21:20:47 +02001340 if (method_call && argoff > 1)
1341 {
1342 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1343 return FAIL;
1344 isn->isn_arg.shuffle.shfl_item = argcount;
1345 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1346 }
1347
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001348 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1349 return FAIL;
1350 isn->isn_arg.bfunc.cbf_idx = func_idx;
1351 isn->isn_arg.bfunc.cbf_argcount = argcount;
1352
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001353 for (i = 0; i < argcount; ++i)
1354 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1355
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356 stack->ga_len -= argcount; // drop the arguments
1357 if (ga_grow(stack, 1) == FAIL)
1358 return FAIL;
1359 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001360 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 ++stack->ga_len; // add return value
1362
1363 return OK;
1364}
1365
1366/*
1367 * Generate an ISN_DCALL or ISN_UCALL instruction.
1368 * Return FAIL if the number of arguments is wrong.
1369 */
1370 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001371generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001372{
1373 isn_T *isn;
1374 garray_T *stack = &cctx->ctx_type_stack;
1375 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001376 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377
Bram Moolenaar080457c2020-03-03 21:53:32 +01001378 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001379 if (argcount > regular_args && !has_varargs(ufunc))
1380 {
1381 semsg(_(e_toomanyarg), ufunc->uf_name);
1382 return FAIL;
1383 }
1384 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1385 {
1386 semsg(_(e_toofewarg), ufunc->uf_name);
1387 return FAIL;
1388 }
1389
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001390 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001391 {
1392 int i;
1393
1394 for (i = 0; i < argcount; ++i)
1395 {
1396 type_T *expected;
1397 type_T *actual;
1398
1399 if (i < regular_args)
1400 {
1401 if (ufunc->uf_arg_types == NULL)
1402 continue;
1403 expected = ufunc->uf_arg_types[i];
1404 }
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001405 else if (ufunc->uf_va_type == NULL)
1406 // possibly a lambda
1407 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001408 else
1409 expected = ufunc->uf_va_type->tt_member;
1410 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001411 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001412 {
1413 arg_type_mismatch(expected, actual, i + 1);
1414 return FAIL;
1415 }
1416 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001417 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001418 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1419 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001420 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001421 }
1422
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001423 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001424 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001425 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001426 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001427 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001428 {
1429 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1430 isn->isn_arg.dfunc.cdf_argcount = argcount;
1431 }
1432 else
1433 {
1434 // A user function may be deleted and redefined later, can't use the
1435 // ufunc pointer, need to look it up again at runtime.
1436 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1437 isn->isn_arg.ufunc.cuf_argcount = argcount;
1438 }
1439
1440 stack->ga_len -= argcount; // drop the arguments
1441 if (ga_grow(stack, 1) == FAIL)
1442 return FAIL;
1443 // add return value
1444 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1445 ++stack->ga_len;
1446
1447 return OK;
1448}
1449
1450/*
1451 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1452 */
1453 static int
1454generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1455{
1456 isn_T *isn;
1457 garray_T *stack = &cctx->ctx_type_stack;
1458
Bram Moolenaar080457c2020-03-03 21:53:32 +01001459 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001460 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1461 return FAIL;
1462 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1463 isn->isn_arg.ufunc.cuf_argcount = argcount;
1464
1465 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001466 if (ga_grow(stack, 1) == FAIL)
1467 return FAIL;
1468 // add return value
1469 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1470 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471
1472 return OK;
1473}
1474
1475/*
1476 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001477 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001478 */
1479 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001480generate_PCALL(
1481 cctx_T *cctx,
1482 int argcount,
1483 char_u *name,
1484 type_T *type,
1485 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001486{
1487 isn_T *isn;
1488 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001489 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001490
Bram Moolenaar080457c2020-03-03 21:53:32 +01001491 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001492
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001493 if (type->tt_type == VAR_ANY)
1494 ret_type = &t_any;
1495 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001496 {
1497 if (type->tt_argcount != -1)
1498 {
1499 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1500
1501 if (argcount < type->tt_min_argcount - varargs)
1502 {
1503 semsg(_(e_toofewarg), "[reference]");
1504 return FAIL;
1505 }
1506 if (!varargs && argcount > type->tt_argcount)
1507 {
1508 semsg(_(e_toomanyarg), "[reference]");
1509 return FAIL;
1510 }
1511 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001512 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001513 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001514 else
1515 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001516 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001517 return FAIL;
1518 }
1519
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001520 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1521 return FAIL;
1522 isn->isn_arg.pfunc.cpf_top = at_top;
1523 isn->isn_arg.pfunc.cpf_argcount = argcount;
1524
1525 stack->ga_len -= argcount; // drop the arguments
1526
1527 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001528 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001529
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001530 // If partial is above the arguments it must be cleared and replaced with
1531 // the return value.
1532 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1533 return FAIL;
1534
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001535 return OK;
1536}
1537
1538/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001539 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001540 */
1541 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001542generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001543{
1544 isn_T *isn;
1545 garray_T *stack = &cctx->ctx_type_stack;
1546 type_T *type;
1547
Bram Moolenaar080457c2020-03-03 21:53:32 +01001548 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001549 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001550 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001551 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001553 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001554 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001555 if (type->tt_type != VAR_DICT && type != &t_any)
1556 {
1557 emsg(_(e_dictreq));
1558 return FAIL;
1559 }
1560 // change dict type to dict member type
1561 if (type->tt_type == VAR_DICT)
1562 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563
1564 return OK;
1565}
1566
1567/*
1568 * Generate an ISN_ECHO instruction.
1569 */
1570 static int
1571generate_ECHO(cctx_T *cctx, int with_white, int count)
1572{
1573 isn_T *isn;
1574
Bram Moolenaar080457c2020-03-03 21:53:32 +01001575 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001576 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1577 return FAIL;
1578 isn->isn_arg.echo.echo_with_white = with_white;
1579 isn->isn_arg.echo.echo_count = count;
1580
1581 return OK;
1582}
1583
Bram Moolenaarad39c092020-02-26 18:23:43 +01001584/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001585 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001586 */
1587 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001588generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001589{
1590 isn_T *isn;
1591
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001592 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001593 return FAIL;
1594 isn->isn_arg.number = count;
1595
1596 return OK;
1597}
1598
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001599/*
1600 * Generate an ISN_PUT instruction.
1601 */
1602 static int
1603generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1604{
1605 isn_T *isn;
1606
1607 RETURN_OK_IF_SKIP(cctx);
1608 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1609 return FAIL;
1610 isn->isn_arg.put.put_regname = regname;
1611 isn->isn_arg.put.put_lnum = lnum;
1612 return OK;
1613}
1614
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615 static int
1616generate_EXEC(cctx_T *cctx, char_u *line)
1617{
1618 isn_T *isn;
1619
Bram Moolenaar080457c2020-03-03 21:53:32 +01001620 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1622 return FAIL;
1623 isn->isn_arg.string = vim_strsave(line);
1624 return OK;
1625}
1626
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001627 static int
1628generate_EXECCONCAT(cctx_T *cctx, int count)
1629{
1630 isn_T *isn;
1631
1632 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1633 return FAIL;
1634 isn->isn_arg.number = count;
1635 return OK;
1636}
1637
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001638/*
1639 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001640 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001642 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001643reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1644{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645 lvar_T *lvar;
1646
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001647 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001648 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001649 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001650 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001651 }
1652
1653 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001654 return NULL;
1655 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001656
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001657 // Every local variable uses the next entry on the stack. We could re-use
1658 // the last ones when leaving a scope, but then variables used in a closure
1659 // might get overwritten. To keep things simple do not re-use stack
1660 // entries. This is less efficient, but memory is cheap these days.
1661 lvar->lv_idx = cctx->ctx_locals_count++;
1662
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001663 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001664 lvar->lv_const = isConst;
1665 lvar->lv_type = type;
1666
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001667 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001668}
1669
1670/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001671 * Remove local variables above "new_top".
1672 */
1673 static void
1674unwind_locals(cctx_T *cctx, int new_top)
1675{
1676 if (cctx->ctx_locals.ga_len > new_top)
1677 {
1678 int idx;
1679 lvar_T *lvar;
1680
1681 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1682 {
1683 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1684 vim_free(lvar->lv_name);
1685 }
1686 }
1687 cctx->ctx_locals.ga_len = new_top;
1688}
1689
1690/*
1691 * Free all local variables.
1692 */
1693 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001694free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001695{
1696 unwind_locals(cctx, 0);
1697 ga_clear(&cctx->ctx_locals);
1698}
1699
1700/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001701 * Find "name" in script-local items of script "sid".
1702 * Returns the index in "sn_var_vals" if found.
1703 * If found but not in "sn_var_vals" returns -1.
1704 * If not found returns -2.
1705 */
1706 int
1707get_script_item_idx(int sid, char_u *name, int check_writable)
1708{
1709 hashtab_T *ht;
1710 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001711 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001712 int idx;
1713
1714 // First look the name up in the hashtable.
Bram Moolenaare3d46852020-08-29 13:39:17 +02001715 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001716 return -1;
1717 ht = &SCRIPT_VARS(sid);
1718 di = find_var_in_ht(ht, 0, name, TRUE);
1719 if (di == NULL)
1720 return -2;
1721
1722 // Now find the svar_T index in sn_var_vals.
1723 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1724 {
1725 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1726
1727 if (sv->sv_tv == &di->di_tv)
1728 {
1729 if (check_writable && sv->sv_const)
1730 semsg(_(e_readonlyvar), name);
1731 return idx;
1732 }
1733 }
1734 return -1;
1735}
1736
1737/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001738 * Find "name" in imported items of the current script or in "cctx" if not
1739 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001740 */
1741 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001742find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001744 int idx;
1745
Bram Moolenaare3d46852020-08-29 13:39:17 +02001746 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001747 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001748 if (cctx != NULL)
1749 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1750 {
1751 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1752 + idx;
1753
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001754 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1755 : STRLEN(import->imp_name) == len
1756 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001757 return import;
1758 }
1759
Bram Moolenaarefa94442020-08-08 22:16:00 +02001760 return find_imported_in_script(name, len, current_sctx.sc_sid);
1761}
1762
1763 imported_T *
1764find_imported_in_script(char_u *name, size_t len, int sid)
1765{
Bram Moolenaare3d46852020-08-29 13:39:17 +02001766 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001767 int idx;
1768
Bram Moolenaare3d46852020-08-29 13:39:17 +02001769 if (!SCRIPT_ID_VALID(sid))
1770 return NULL;
1771 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001772 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1773 {
1774 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1775
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001776 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1777 : STRLEN(import->imp_name) == len
1778 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001779 return import;
1780 }
1781 return NULL;
1782}
1783
1784/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001785 * Free all imported variables.
1786 */
1787 static void
1788free_imported(cctx_T *cctx)
1789{
1790 int idx;
1791
1792 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1793 {
1794 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1795
1796 vim_free(import->imp_name);
1797 }
1798 ga_clear(&cctx->ctx_imports);
1799}
1800
1801/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02001802 * Return TRUE if "p" points at a "#" but not at "#{".
1803 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001804 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001805vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001806{
1807 return p[0] == '#' && p[1] != '{';
1808}
1809
1810/*
1811 * Return a pointer to the next line that isn't empty or only contains a
1812 * comment. Skips over white space.
1813 * Returns NULL if there is none.
1814 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001815 char_u *
1816peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001817{
1818 int lnum = cctx->ctx_lnum;
1819
1820 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
1821 {
1822 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001823 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001824
Bram Moolenaarba60cc42020-08-12 19:15:33 +02001825 // ignore NULLs inserted for continuation lines
1826 if (line != NULL)
1827 {
1828 p = skipwhite(line);
1829 if (*p != NUL && !vim9_comment_start(p))
1830 return p;
1831 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02001832 }
1833 return NULL;
1834}
1835
1836/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001837 * Called when checking for a following operator at "arg". When the rest of
1838 * the line is empty or only a comment, peek the next line. If there is a next
1839 * line return a pointer to it and set "nextp".
1840 * Otherwise skip over white space.
1841 */
1842 static char_u *
1843may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
1844{
1845 char_u *p = skipwhite(arg);
1846
1847 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001848 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001849 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001850 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001851 if (*nextp != NULL)
1852 return *nextp;
1853 }
1854 return p;
1855}
1856
1857/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02001858 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02001859 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02001860 * Returns NULL when at the end.
1861 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001862 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02001863next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02001864{
Bram Moolenaar7a092242020-04-16 22:10:49 +02001865 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001866
1867 do
1868 {
1869 ++cctx->ctx_lnum;
1870 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02001871 {
1872 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001873 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02001874 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02001875 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02001876 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001877 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001878 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02001879 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02001880 return line;
1881}
1882
1883/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001884 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02001885 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001886 * Return FAIL if beyond the last line, "*arg" is unmodified then.
1887 */
1888 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02001889may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001890{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001891 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001892 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02001893 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001894
1895 if (next == NULL)
1896 return FAIL;
1897 *arg = skipwhite(next);
1898 }
1899 return OK;
1900}
1901
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001902/*
1903 * Idem, and give an error when failed.
1904 */
1905 static int
1906may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
1907{
1908 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1909 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001910 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001911 return FAIL;
1912 }
1913 return OK;
1914}
1915
1916
Bram Moolenaara5565e42020-05-09 15:44:01 +02001917// Structure passed between the compile_expr* functions to keep track of
1918// constants that have been parsed but for which no code was produced yet. If
1919// possible expressions on these constants are applied at compile time. If
1920// that is not possible, the code to push the constants needs to be generated
1921// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02001922// Using 50 should be more than enough of 5 levels of ().
1923#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02001924typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02001925 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02001926 int pp_used; // active entries in pp_tv[]
1927} ppconst_T;
1928
Bram Moolenaar1c747212020-05-09 18:28:34 +02001929static int compile_expr0(char_u **arg, cctx_T *cctx);
1930static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1931
Bram Moolenaara5565e42020-05-09 15:44:01 +02001932/*
1933 * Generate a PUSH instruction for "tv".
1934 * "tv" will be consumed or cleared.
1935 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
1936 */
1937 static int
1938generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
1939{
1940 if (tv != NULL)
1941 {
1942 switch (tv->v_type)
1943 {
1944 case VAR_UNKNOWN:
1945 break;
1946 case VAR_BOOL:
1947 generate_PUSHBOOL(cctx, tv->vval.v_number);
1948 break;
1949 case VAR_SPECIAL:
1950 generate_PUSHSPEC(cctx, tv->vval.v_number);
1951 break;
1952 case VAR_NUMBER:
1953 generate_PUSHNR(cctx, tv->vval.v_number);
1954 break;
1955#ifdef FEAT_FLOAT
1956 case VAR_FLOAT:
1957 generate_PUSHF(cctx, tv->vval.v_float);
1958 break;
1959#endif
1960 case VAR_BLOB:
1961 generate_PUSHBLOB(cctx, tv->vval.v_blob);
1962 tv->vval.v_blob = NULL;
1963 break;
1964 case VAR_STRING:
1965 generate_PUSHS(cctx, tv->vval.v_string);
1966 tv->vval.v_string = NULL;
1967 break;
1968 default:
1969 iemsg("constant type not supported");
1970 clear_tv(tv);
1971 return FAIL;
1972 }
1973 tv->v_type = VAR_UNKNOWN;
1974 }
1975 return OK;
1976}
1977
1978/*
1979 * Generate code for any ppconst entries.
1980 */
1981 static int
1982generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
1983{
1984 int i;
1985 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001986 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001987
Bram Moolenaar9b68c822020-06-18 19:31:08 +02001988 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001989 for (i = 0; i < ppconst->pp_used; ++i)
1990 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
1991 ret = FAIL;
1992 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001993 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001994 return ret;
1995}
1996
1997/*
1998 * Clear ppconst constants. Used when failing.
1999 */
2000 static void
2001clear_ppconst(ppconst_T *ppconst)
2002{
2003 int i;
2004
2005 for (i = 0; i < ppconst->pp_used; ++i)
2006 clear_tv(&ppconst->pp_tv[i]);
2007 ppconst->pp_used = 0;
2008}
2009
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002010/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002011 * Generate an instruction to load script-local variable "name", without the
2012 * leading "s:".
2013 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002014 */
2015 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002016compile_load_scriptvar(
2017 cctx_T *cctx,
2018 char_u *name, // variable NUL terminated
2019 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002020 char_u **end, // end of variable
2021 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002023 scriptitem_T *si;
2024 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 imported_T *import;
2026
Bram Moolenaare3d46852020-08-29 13:39:17 +02002027 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2028 return FAIL;
2029 si = SCRIPT_ITEM(current_sctx.sc_sid);
2030 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002031 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002032 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002033 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002034 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2035 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002036 }
2037 if (idx >= 0)
2038 {
2039 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2040
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002041 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042 current_sctx.sc_sid, idx, sv->sv_type);
2043 return OK;
2044 }
2045
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002046 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002047 if (import != NULL)
2048 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002049 if (import->imp_all)
2050 {
2051 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002052 char_u *exp_name;
2053 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002054 ufunc_T *ufunc;
2055 type_T *type;
2056
2057 // Used "import * as Name", need to lookup the member.
2058 if (*p != '.')
2059 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002060 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002061 return FAIL;
2062 }
2063 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002064 if (VIM_ISWHITE(*p))
2065 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002066 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002067 return FAIL;
2068 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002069
Bram Moolenaar1c991142020-07-04 13:15:31 +02002070 // isolate one name
2071 exp_name = p;
2072 while (eval_isnamec(*p))
2073 ++p;
2074 cc = *p;
2075 *p = NUL;
2076
2077 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2078 *p = cc;
2079 p = skipwhite(p);
2080
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002081 // TODO: what if it is a function?
2082 if (idx < 0)
2083 return FAIL;
2084 *end = p;
2085
2086 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2087 import->imp_sid,
2088 idx,
2089 type);
2090 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002091 else if (import->imp_funcname != NULL)
2092 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002093 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002094 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2095 import->imp_sid,
2096 import->imp_var_vals_idx,
2097 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002098 return OK;
2099 }
2100
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002101 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002102 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002103 return FAIL;
2104}
2105
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002106 static int
2107generate_funcref(cctx_T *cctx, char_u *name)
2108{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002109 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002110
2111 if (ufunc == NULL)
2112 return FAIL;
2113
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002114 // Need to compile any default values to get the argument types.
2115 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2116 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2117 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002118 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002119}
2120
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121/*
2122 * Compile a variable name into a load instruction.
2123 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002124 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002125 * When "error" is FALSE do not give an error when not found.
2126 */
2127 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002128compile_load(
2129 char_u **arg,
2130 char_u *end_arg,
2131 cctx_T *cctx,
2132 int is_expr,
2133 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002134{
2135 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002136 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002137 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002138 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002139 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140
2141 if (*(*arg + 1) == ':')
2142 {
2143 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002144 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002145 {
2146 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002147
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002148 switch (**arg)
2149 {
2150 case 'g': isn_type = ISN_LOADGDICT; break;
2151 case 'w': isn_type = ISN_LOADWDICT; break;
2152 case 't': isn_type = ISN_LOADTDICT; break;
2153 case 'b': isn_type = ISN_LOADBDICT; break;
2154 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002155 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002156 goto theend;
2157 }
2158 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2159 goto theend;
2160 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002161 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162 else
2163 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002164 isntype_T isn_type = ISN_DROP;
2165
2166 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2167 if (name == NULL)
2168 return FAIL;
2169
2170 switch (**arg)
2171 {
2172 case 'v': res = generate_LOADV(cctx, name, error);
2173 break;
2174 case 's': res = compile_load_scriptvar(cctx, name,
2175 NULL, NULL, error);
2176 break;
2177 case 'g': isn_type = ISN_LOADG; break;
2178 case 'w': isn_type = ISN_LOADW; break;
2179 case 't': isn_type = ISN_LOADT; break;
2180 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002181 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002182 goto theend;
2183 }
2184 if (isn_type != ISN_DROP)
2185 {
2186 // Global, Buffer-local, Window-local and Tabpage-local
2187 // variables can be defined later, thus we don't check if it
2188 // exists, give error at runtime.
2189 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2190 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002191 }
2192 }
2193 else
2194 {
2195 size_t len = end - *arg;
2196 int idx;
2197 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002198 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002199
2200 name = vim_strnsave(*arg, end - *arg);
2201 if (name == NULL)
2202 return FAIL;
2203
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002204 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002205 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002206 if (!gen_load_outer)
2207 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002208 }
2209 else
2210 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002211 lvar_T *lvar = lookup_local(*arg, len, cctx);
2212
2213 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002214 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002215 type = lvar->lv_type;
2216 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002217 if (lvar->lv_from_outer)
2218 gen_load_outer = TRUE;
2219 else
2220 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002221 }
2222 else
2223 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002224 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002225 // already exists in a Vim9 script or when it's imported.
2226 if (lookup_script(*arg, len, TRUE) == OK
2227 || find_imported(name, 0, cctx) != NULL)
2228 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002229
Bram Moolenaar0f769812020-09-12 18:32:34 +02002230 // When evaluating an expression and the name starts with an
2231 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002232 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002233 if (res == FAIL && is_expr
2234 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002235 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002236 }
2237 }
2238 if (gen_load)
2239 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002240 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002241 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002242 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002243 cctx->ctx_outer_used = TRUE;
2244 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245 }
2246
2247 *arg = end;
2248
2249theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002250 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002251 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002252 vim_free(name);
2253 return res;
2254}
2255
2256/*
2257 * Compile the argument expressions.
2258 * "arg" points to just after the "(" and is advanced to after the ")"
2259 */
2260 static int
2261compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2262{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002263 char_u *p = *arg;
2264 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002265
Bram Moolenaare6085c52020-04-12 20:19:16 +02002266 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002267 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002268 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2269 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002270 if (*p == ')')
2271 {
2272 *arg = p + 1;
2273 return OK;
2274 }
2275
Bram Moolenaara5565e42020-05-09 15:44:01 +02002276 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002277 return FAIL;
2278 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002279
2280 if (*p != ',' && *skipwhite(p) == ',')
2281 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002282 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002283 p = skipwhite(p);
2284 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002286 {
2287 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002288 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002289 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002290 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002291 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002292 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002293 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002294failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002295 emsg(_(e_missing_close));
2296 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002297}
2298
2299/*
2300 * Compile a function call: name(arg1, arg2)
2301 * "arg" points to "name", "arg + varlen" to the "(".
2302 * "argcount_init" is 1 for "value->method()"
2303 * Instructions:
2304 * EVAL arg1
2305 * EVAL arg2
2306 * BCALL / DCALL / UCALL
2307 */
2308 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002309compile_call(
2310 char_u **arg,
2311 size_t varlen,
2312 cctx_T *cctx,
2313 ppconst_T *ppconst,
2314 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002315{
2316 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002317 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002318 int argcount = argcount_init;
2319 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002320 char_u fname_buf[FLEN_FIXED + 1];
2321 char_u *tofree = NULL;
2322 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002323 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002324 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002325 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002326
Bram Moolenaara5565e42020-05-09 15:44:01 +02002327 // we can evaluate "has('name')" at compile time
2328 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2329 {
2330 char_u *s = skipwhite(*arg + varlen + 1);
2331 typval_T argvars[2];
2332
2333 argvars[0].v_type = VAR_UNKNOWN;
2334 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002335 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002336 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002337 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002338 s = skipwhite(s);
2339 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2340 {
2341 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2342
2343 *arg = s + 1;
2344 argvars[1].v_type = VAR_UNKNOWN;
2345 tv->v_type = VAR_NUMBER;
2346 tv->vval.v_number = 0;
2347 f_has(argvars, tv);
2348 clear_tv(&argvars[0]);
2349 ++ppconst->pp_used;
2350 return OK;
2351 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002352 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002353 }
2354
2355 if (generate_ppconst(cctx, ppconst) == FAIL)
2356 return FAIL;
2357
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002358 if (varlen >= sizeof(namebuf))
2359 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002360 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002361 return FAIL;
2362 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002363 vim_strncpy(namebuf, *arg, varlen);
2364 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365
2366 *arg = skipwhite(*arg + varlen + 1);
2367 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002368 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002369
Bram Moolenaara1773442020-08-12 15:21:22 +02002370 is_autoload = vim_strchr(name, '#') != NULL;
2371 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372 {
2373 int idx;
2374
2375 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002376 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002377 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002378 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002379 else
2380 semsg(_(e_unknownfunc), namebuf);
2381 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002382 }
2383
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384 // If we can find the function by name generate the right call.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002385 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002386 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar0f769812020-09-12 18:32:34 +02002387 if (ufunc != NULL && !func_is_global(ufunc))
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002388 {
2389 res = generate_CALL(cctx, ufunc, argcount);
2390 goto theend;
2391 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002392
2393 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002394 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002395 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002396 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002397 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002398 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002399 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002400 garray_T *stack = &cctx->ctx_type_stack;
2401 type_T *type;
2402
2403 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2404 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002405 goto theend;
2406 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002407
Bram Moolenaar0f769812020-09-12 18:32:34 +02002408 // If we can find a global function by name generate the right call.
2409 if (ufunc != NULL)
2410 {
2411 res = generate_CALL(cctx, ufunc, argcount);
2412 goto theend;
2413 }
2414
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002415 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002416 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002417 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002418 res = generate_UCALL(cctx, name, argcount);
2419 else
2420 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002421
2422theend:
2423 vim_free(tofree);
2424 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002425}
2426
2427// like NAMESPACE_CHAR but with 'a' and 'l'.
2428#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2429
2430/*
2431 * Find the end of a variable or function name. Unlike find_name_end() this
2432 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002433 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002434 * Return a pointer to just after the name. Equal to "arg" if there is no
2435 * valid name.
2436 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002437 static char_u *
2438to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002439{
2440 char_u *p;
2441
2442 // Quick check for valid starting character.
2443 if (!eval_isnamec1(*arg))
2444 return arg;
2445
2446 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2447 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2448 // and can be used in slice "[n:]".
2449 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002450 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002451 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2452 break;
2453 return p;
2454}
2455
2456/*
2457 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002458 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002459 */
2460 char_u *
2461to_name_const_end(char_u *arg)
2462{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002463 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002464 typval_T rettv;
2465
2466 if (p == arg && *arg == '[')
2467 {
2468
2469 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002470 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471 p = arg;
2472 }
2473 else if (p == arg && *arg == '#' && arg[1] == '{')
2474 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002475 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002476 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002477 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002478 p = arg;
2479 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002480
2481 return p;
2482}
2483
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484/*
2485 * parse a list: [expr, expr]
2486 * "*arg" points to the '['.
2487 */
2488 static int
2489compile_list(char_u **arg, cctx_T *cctx)
2490{
2491 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002492 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493 int count = 0;
2494
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002495 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002497 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002498 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002499 semsg(_(e_list_end), *arg);
2500 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002501 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002502 if (*p == ',')
2503 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002504 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002505 return FAIL;
2506 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002507 if (*p == ']')
2508 {
2509 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002510 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002511 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002512 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002513 break;
2514 ++count;
2515 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002516 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002518 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2519 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002520 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002521 return FAIL;
2522 }
2523 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002524 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525 p = skipwhite(p);
2526 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002527 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528
2529 generate_NEWLIST(cctx, count);
2530 return OK;
2531}
2532
2533/*
2534 * parse a lambda: {arg, arg -> expr}
2535 * "*arg" points to the '{'.
2536 */
2537 static int
2538compile_lambda(char_u **arg, cctx_T *cctx)
2539{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 typval_T rettv;
2541 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002542 evalarg_T evalarg;
2543
2544 CLEAR_FIELD(evalarg);
2545 evalarg.eval_flags = EVAL_EVALUATE;
2546 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547
2548 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002549 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002551
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002552 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002553 ++ufunc->uf_refcount;
2554 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002555 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002556
2557 // The function will have one line: "return {expr}".
2558 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002559 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002561 clear_evalarg(&evalarg, NULL);
2562
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002563 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002564 {
2565 // The return type will now be known.
2566 set_function_type(ufunc);
2567
2568 return generate_FUNCREF(cctx, ufunc);
2569 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002570
2571 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 return FAIL;
2573}
2574
2575/*
2576 * Compile a lamda call: expr->{lambda}(args)
2577 * "arg" points to the "{".
2578 */
2579 static int
2580compile_lambda_call(char_u **arg, cctx_T *cctx)
2581{
2582 ufunc_T *ufunc;
2583 typval_T rettv;
2584 int argcount = 1;
2585 int ret = FAIL;
2586
2587 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002588 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 return FAIL;
2590
2591 if (**arg != '(')
2592 {
2593 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002594 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595 else
2596 semsg(_(e_missing_paren), "lambda");
2597 clear_tv(&rettv);
2598 return FAIL;
2599 }
2600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601 ufunc = rettv.vval.v_partial->pt_func;
2602 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002603 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002604 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002605
2606 // The function will have one line: "return {expr}".
2607 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002608 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609
2610 // compile the arguments
2611 *arg = skipwhite(*arg + 1);
2612 if (compile_arguments(arg, cctx, &argcount) == OK)
2613 // call the compiled function
2614 ret = generate_CALL(cctx, ufunc, argcount);
2615
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002616 if (ret == FAIL)
2617 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002618 return ret;
2619}
2620
2621/*
2622 * parse a dict: {'key': val} or #{key: val}
2623 * "*arg" points to the '{'.
2624 */
2625 static int
2626compile_dict(char_u **arg, cctx_T *cctx, int literal)
2627{
2628 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002629 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 int count = 0;
2631 dict_T *d = dict_alloc();
2632 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002633 char_u *whitep = *arg;
2634 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635
2636 if (d == NULL)
2637 return FAIL;
2638 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002639 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640 {
2641 char_u *key = NULL;
2642
Bram Moolenaar23c55272020-06-21 16:58:13 +02002643 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002644 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002645 *arg = NULL;
2646 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002647 }
2648
2649 if (**arg == '}')
2650 break;
2651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002652 if (literal)
2653 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002654 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655
Bram Moolenaar2c330432020-04-13 14:41:35 +02002656 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002658 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002659 return FAIL;
2660 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002661 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002662 if (generate_PUSHS(cctx, key) == FAIL)
2663 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002664 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 }
2666 else
2667 {
2668 isn_T *isn;
2669
Bram Moolenaara5565e42020-05-09 15:44:01 +02002670 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002671 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2673 if (isn->isn_type == ISN_PUSHS)
2674 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002675 else
2676 {
2677 type_T *keytype = ((type_T **)stack->ga_data)
2678 [stack->ga_len - 1];
2679 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2680 return FAIL;
2681 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002682 }
2683
2684 // Check for duplicate keys, if using string keys.
2685 if (key != NULL)
2686 {
2687 item = dict_find(d, key, -1);
2688 if (item != NULL)
2689 {
2690 semsg(_(e_duplicate_key), key);
2691 goto failret;
2692 }
2693 item = dictitem_alloc(key);
2694 if (item != NULL)
2695 {
2696 item->di_tv.v_type = VAR_UNKNOWN;
2697 item->di_tv.v_lock = 0;
2698 if (dict_add(d, item) == FAIL)
2699 dictitem_free(item);
2700 }
2701 }
2702
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002703 if (**arg != ':')
2704 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002705 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002706 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002707 else
2708 semsg(_(e_missing_dict_colon), *arg);
2709 return FAIL;
2710 }
2711 whitep = *arg + 1;
2712 if (!IS_WHITE_OR_NUL(*whitep))
2713 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002714 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002715 return FAIL;
2716 }
2717
2718 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002719 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002720 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002721 *arg = NULL;
2722 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002723 }
2724
Bram Moolenaara5565e42020-05-09 15:44:01 +02002725 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002726 return FAIL;
2727 ++count;
2728
Bram Moolenaar2c330432020-04-13 14:41:35 +02002729 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002730 *arg = skipwhite(*arg);
2731 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002732 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002733 *arg = NULL;
2734 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002735 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002736 if (**arg == '}')
2737 break;
2738 if (**arg != ',')
2739 {
2740 semsg(_(e_missing_dict_comma), *arg);
2741 goto failret;
2742 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002743 if (IS_WHITE_OR_NUL(*whitep))
2744 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002745 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002746 return FAIL;
2747 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002748 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002749 *arg = skipwhite(*arg + 1);
2750 }
2751
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752 *arg = *arg + 1;
2753
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002754 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002755 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002756 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002757 *arg += STRLEN(*arg);
2758
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002759 dict_unref(d);
2760 return generate_NEWDICT(cctx, count);
2761
2762failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002763 if (*arg == NULL)
2764 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765 dict_unref(d);
2766 return FAIL;
2767}
2768
2769/*
2770 * Compile "&option".
2771 */
2772 static int
2773compile_get_option(char_u **arg, cctx_T *cctx)
2774{
2775 typval_T rettv;
2776 char_u *start = *arg;
2777 int ret;
2778
2779 // parse the option and get the current value to get the type.
2780 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002781 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782 if (ret == OK)
2783 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002784 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785 char_u *name = vim_strnsave(start, *arg - start);
2786 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2787
2788 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2789 vim_free(name);
2790 }
2791 clear_tv(&rettv);
2792
2793 return ret;
2794}
2795
2796/*
2797 * Compile "$VAR".
2798 */
2799 static int
2800compile_get_env(char_u **arg, cctx_T *cctx)
2801{
2802 char_u *start = *arg;
2803 int len;
2804 int ret;
2805 char_u *name;
2806
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 ++*arg;
2808 len = get_env_len(arg);
2809 if (len == 0)
2810 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002811 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002812 return FAIL;
2813 }
2814
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002815 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 name = vim_strnsave(start, len + 1);
2817 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2818 vim_free(name);
2819 return ret;
2820}
2821
2822/*
2823 * Compile "@r".
2824 */
2825 static int
2826compile_get_register(char_u **arg, cctx_T *cctx)
2827{
2828 int ret;
2829
2830 ++*arg;
2831 if (**arg == NUL)
2832 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002833 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002834 return FAIL;
2835 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002836 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837 {
2838 emsg_invreg(**arg);
2839 return FAIL;
2840 }
2841 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2842 ++*arg;
2843 return ret;
2844}
2845
2846/*
2847 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002848 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 */
2850 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002851apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002853 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854
2855 // this works from end to start
2856 while (p > start)
2857 {
2858 --p;
2859 if (*p == '-' || *p == '+')
2860 {
2861 // only '-' has an effect, for '+' we only check the type
2862#ifdef FEAT_FLOAT
2863 if (rettv->v_type == VAR_FLOAT)
2864 {
2865 if (*p == '-')
2866 rettv->vval.v_float = -rettv->vval.v_float;
2867 }
2868 else
2869#endif
2870 {
2871 varnumber_T val;
2872 int error = FALSE;
2873
2874 // tv_get_number_chk() accepts a string, but we don't want that
2875 // here
2876 if (check_not_string(rettv) == FAIL)
2877 return FAIL;
2878 val = tv_get_number_chk(rettv, &error);
2879 clear_tv(rettv);
2880 if (error)
2881 return FAIL;
2882 if (*p == '-')
2883 val = -val;
2884 rettv->v_type = VAR_NUMBER;
2885 rettv->vval.v_number = val;
2886 }
2887 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002888 else if (numeric_only)
2889 {
2890 ++p;
2891 break;
2892 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893 else
2894 {
2895 int v = tv2bool(rettv);
2896
2897 // '!' is permissive in the type.
2898 clear_tv(rettv);
2899 rettv->v_type = VAR_BOOL;
2900 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2901 }
2902 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002903 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904 return OK;
2905}
2906
2907/*
2908 * Recognize v: variables that are constants and set "rettv".
2909 */
2910 static void
2911get_vim_constant(char_u **arg, typval_T *rettv)
2912{
2913 if (STRNCMP(*arg, "v:true", 6) == 0)
2914 {
2915 rettv->v_type = VAR_BOOL;
2916 rettv->vval.v_number = VVAL_TRUE;
2917 *arg += 6;
2918 }
2919 else if (STRNCMP(*arg, "v:false", 7) == 0)
2920 {
2921 rettv->v_type = VAR_BOOL;
2922 rettv->vval.v_number = VVAL_FALSE;
2923 *arg += 7;
2924 }
2925 else if (STRNCMP(*arg, "v:null", 6) == 0)
2926 {
2927 rettv->v_type = VAR_SPECIAL;
2928 rettv->vval.v_number = VVAL_NULL;
2929 *arg += 6;
2930 }
2931 else if (STRNCMP(*arg, "v:none", 6) == 0)
2932 {
2933 rettv->v_type = VAR_SPECIAL;
2934 rettv->vval.v_number = VVAL_NONE;
2935 *arg += 6;
2936 }
2937}
2938
Bram Moolenaar696ba232020-07-29 21:20:41 +02002939 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02002940get_compare_type(char_u *p, int *len, int *type_is)
2941{
2942 exptype_T type = EXPR_UNKNOWN;
2943 int i;
2944
2945 switch (p[0])
2946 {
2947 case '=': if (p[1] == '=')
2948 type = EXPR_EQUAL;
2949 else if (p[1] == '~')
2950 type = EXPR_MATCH;
2951 break;
2952 case '!': if (p[1] == '=')
2953 type = EXPR_NEQUAL;
2954 else if (p[1] == '~')
2955 type = EXPR_NOMATCH;
2956 break;
2957 case '>': if (p[1] != '=')
2958 {
2959 type = EXPR_GREATER;
2960 *len = 1;
2961 }
2962 else
2963 type = EXPR_GEQUAL;
2964 break;
2965 case '<': if (p[1] != '=')
2966 {
2967 type = EXPR_SMALLER;
2968 *len = 1;
2969 }
2970 else
2971 type = EXPR_SEQUAL;
2972 break;
2973 case 'i': if (p[1] == 's')
2974 {
2975 // "is" and "isnot"; but not a prefix of a name
2976 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2977 *len = 5;
2978 i = p[*len];
2979 if (!isalnum(i) && i != '_')
2980 {
2981 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2982 *type_is = TRUE;
2983 }
2984 }
2985 break;
2986 }
2987 return type;
2988}
2989
2990/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002992 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 */
2994 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002995compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002997 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002998
2999 // this works from end to start
3000 while (p > start)
3001 {
3002 --p;
3003 if (*p == '-' || *p == '+')
3004 {
3005 int negate = *p == '-';
3006 isn_T *isn;
3007
3008 // TODO: check type
3009 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3010 {
3011 --p;
3012 if (*p == '-')
3013 negate = !negate;
3014 }
3015 // only '-' has an effect, for '+' we only check the type
3016 if (negate)
3017 isn = generate_instr(cctx, ISN_NEGATENR);
3018 else
3019 isn = generate_instr(cctx, ISN_CHECKNR);
3020 if (isn == NULL)
3021 return FAIL;
3022 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003023 else if (numeric_only)
3024 {
3025 ++p;
3026 break;
3027 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 else
3029 {
3030 int invert = TRUE;
3031
3032 while (p > start && p[-1] == '!')
3033 {
3034 --p;
3035 invert = !invert;
3036 }
3037 if (generate_2BOOL(cctx, invert) == FAIL)
3038 return FAIL;
3039 }
3040 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003041 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042 return OK;
3043}
3044
3045/*
3046 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003047 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 */
3049 static int
3050compile_subscript(
3051 char_u **arg,
3052 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003053 char_u *start_leader,
3054 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003055 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003057 char_u *name_start = *end_leader;
3058
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 for (;;)
3060 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003061 char_u *p = skipwhite(*arg);
3062
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003063 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003064 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003065 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003066
3067 // If a following line starts with "->{" or "->X" advance to that
3068 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003069 // Also if a following line starts with ".x".
3070 if (next != NULL &&
3071 ((next[0] == '-' && next[1] == '>'
3072 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003073 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003074 {
3075 next = next_line_from_context(cctx, TRUE);
3076 if (next == NULL)
3077 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003078 *arg = next;
3079 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003080 }
3081 }
3082
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003083 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3084 // not a function call.
3085 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003087 garray_T *stack = &cctx->ctx_type_stack;
3088 type_T *type;
3089 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003091 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003092 return FAIL;
3093
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003095 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3096
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003097 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3099 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003100 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 return FAIL;
3102 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003103 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003105 char_u *pstart = p;
3106
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003107 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003108 return FAIL;
3109
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 // something->method()
3111 // Apply the '!', '-' and '+' first:
3112 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003113 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003116 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003117 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003118 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003119 if (**arg == '{')
3120 {
3121 // lambda call: list->{lambda}
3122 if (compile_lambda_call(arg, cctx) == FAIL)
3123 return FAIL;
3124 }
3125 else
3126 {
3127 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003128 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003129 if (!eval_isnamec1(*p))
3130 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003131 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003132 return FAIL;
3133 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003134 if (ASCII_ISALPHA(*p) && p[1] == ':')
3135 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003136 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 ;
3138 if (*p != '(')
3139 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003140 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 return FAIL;
3142 }
3143 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003144 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 return FAIL;
3146 }
3147 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003148 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003150 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003151 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003152 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003153 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003154 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003155
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003156 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003157 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003158 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003159 // TODO: blob index
3160 // TODO: more arguments
3161 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003162 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003163 return FAIL;
3164
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003165 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003166 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003167 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003168 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003169 if (**arg == ':')
3170 // missing first index is equal to zero
3171 generate_PUSHNR(cctx, 0);
3172 else
3173 {
3174 if (compile_expr0(arg, cctx) == FAIL)
3175 return FAIL;
3176 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3177 return FAIL;
3178 *arg = skipwhite(*arg);
3179 }
3180 if (**arg == ':')
3181 {
3182 *arg = skipwhite(*arg + 1);
3183 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3184 return FAIL;
3185 if (**arg == ']')
3186 // missing second index is equal to end of string
3187 generate_PUSHNR(cctx, -1);
3188 else
3189 {
3190 if (compile_expr0(arg, cctx) == FAIL)
3191 return FAIL;
3192 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3193 return FAIL;
3194 *arg = skipwhite(*arg);
3195 }
3196 is_slice = TRUE;
3197 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198
3199 if (**arg != ']')
3200 {
3201 emsg(_(e_missbrac));
3202 return FAIL;
3203 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003204 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003206 // We can index a list and a dict. If we don't know the type
3207 // we can use the index value type.
3208 // TODO: If we don't know use an instruction to figure it out at
3209 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003210 typep = ((type_T **)stack->ga_data) + stack->ga_len
3211 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003212 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003213 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3214 // If the index is a string, the variable must be a Dict.
3215 if (*typep == &t_any && valtype == &t_string)
3216 vtype = VAR_DICT;
3217 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003218 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003219 if (need_type(valtype, &t_number, -1, cctx, FALSE) == FAIL)
3220 return FAIL;
3221 if (is_slice)
3222 {
3223 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
3224 if (need_type(valtype, &t_number, -2, cctx, FALSE) == FAIL)
3225 return FAIL;
3226 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003227 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003228
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003229 if (vtype == VAR_DICT)
3230 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003231 if (is_slice)
3232 {
3233 emsg(_(e_cannot_slice_dictionary));
3234 return FAIL;
3235 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003236 if ((*typep)->tt_type == VAR_DICT)
3237 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003238 else
3239 {
3240 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3241 return FAIL;
3242 *typep = &t_any;
3243 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003244 if (may_generate_2STRING(-1, cctx) == FAIL)
3245 return FAIL;
3246 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3247 return FAIL;
3248 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003249 else if (vtype == VAR_STRING)
3250 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003251 *typep = &t_string;
3252 if ((is_slice
3253 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3254 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003255 return FAIL;
3256 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003257 else if (vtype == VAR_BLOB)
3258 {
3259 emsg("Sorry, blob index and slice not implemented yet");
3260 return FAIL;
3261 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003262 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003263 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003264 if (is_slice)
3265 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003266 if (generate_instr_drop(cctx,
3267 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3268 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003269 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003270 }
Bram Moolenaared591872020-08-15 22:14:53 +02003271 else
3272 {
3273 if ((*typep)->tt_type == VAR_LIST)
3274 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003275 if (generate_instr_drop(cctx,
3276 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3277 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003278 return FAIL;
3279 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003280 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003281 else
3282 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003283 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003284 return FAIL;
3285 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003287 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003288 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003289 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003290 return FAIL;
3291
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003292 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003293 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3294 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003296 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003297 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 while (eval_isnamec(*p))
3299 MB_PTR_ADV(p);
3300 if (p == *arg)
3301 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003302 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 return FAIL;
3304 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003305 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306 return FAIL;
3307 *arg = p;
3308 }
3309 else
3310 break;
3311 }
3312
3313 // TODO - see handle_subscript():
3314 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3315 // Don't do this when "Func" is already a partial that was bound
3316 // explicitly (pt_auto is FALSE).
3317
3318 return OK;
3319}
3320
3321/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003322 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3323 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003325 * If the value is a constant "ppconst->pp_ret" will be set.
3326 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003327 *
3328 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329 */
3330
3331/*
3332 * number number constant
3333 * 0zFFFFFFFF Blob constant
3334 * "string" string constant
3335 * 'string' literal string constant
3336 * &option-name option value
3337 * @r register contents
3338 * identifier variable value
3339 * function() function call
3340 * $VAR environment variable
3341 * (expression) nested expression
3342 * [expr, expr] List
3343 * {key: val, key: val} Dictionary
3344 * #{key: val, key: val} Dictionary with literal keys
3345 *
3346 * Also handle:
3347 * ! in front logical NOT
3348 * - in front unary minus
3349 * + in front unary plus (ignored)
3350 * trailing (arg) funcref/partial call
3351 * trailing [] subscript in String or List
3352 * trailing .name entry in Dictionary
3353 * trailing ->name() method call
3354 */
3355 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003356compile_expr7(
3357 char_u **arg,
3358 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003359 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003361 char_u *start_leader, *end_leader;
3362 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003363 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003364 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003365
3366 /*
3367 * Skip '!', '-' and '+' characters. They are handled later.
3368 */
3369 start_leader = *arg;
3370 while (**arg == '!' || **arg == '-' || **arg == '+')
3371 *arg = skipwhite(*arg + 1);
3372 end_leader = *arg;
3373
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003374 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 switch (**arg)
3376 {
3377 /*
3378 * Number constant.
3379 */
3380 case '0': // also for blob starting with 0z
3381 case '1':
3382 case '2':
3383 case '3':
3384 case '4':
3385 case '5':
3386 case '6':
3387 case '7':
3388 case '8':
3389 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003390 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003392 // Apply "-" and "+" just before the number now, right to
3393 // left. Matters especially when "->" follows. Stops at
3394 // '!'.
3395 if (apply_leader(rettv, TRUE,
3396 start_leader, &end_leader) == FAIL)
3397 {
3398 clear_tv(rettv);
3399 return FAIL;
3400 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401 break;
3402
3403 /*
3404 * String constant: "string".
3405 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003406 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407 return FAIL;
3408 break;
3409
3410 /*
3411 * Literal string constant: 'str''ing'.
3412 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003413 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414 return FAIL;
3415 break;
3416
3417 /*
3418 * Constant Vim variable.
3419 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003420 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421 ret = NOTDONE;
3422 break;
3423
3424 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003425 * "true" constant
3426 */
3427 case 't': if (STRNCMP(*arg, "true", 4) == 0
3428 && !eval_isnamec((*arg)[4]))
3429 {
3430 *arg += 4;
3431 rettv->v_type = VAR_BOOL;
3432 rettv->vval.v_number = VVAL_TRUE;
3433 }
3434 else
3435 ret = NOTDONE;
3436 break;
3437
3438 /*
3439 * "false" constant
3440 */
3441 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3442 && !eval_isnamec((*arg)[5]))
3443 {
3444 *arg += 5;
3445 rettv->v_type = VAR_BOOL;
3446 rettv->vval.v_number = VVAL_FALSE;
3447 }
3448 else
3449 ret = NOTDONE;
3450 break;
3451
3452 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 * List: [expr, expr]
3454 */
3455 case '[': ret = compile_list(arg, cctx);
3456 break;
3457
3458 /*
3459 * Dictionary: #{key: val, key: val}
3460 */
3461 case '#': if ((*arg)[1] == '{')
3462 {
3463 ++*arg;
3464 ret = compile_dict(arg, cctx, TRUE);
3465 }
3466 else
3467 ret = NOTDONE;
3468 break;
3469
3470 /*
3471 * Lambda: {arg, arg -> expr}
3472 * Dictionary: {'key': val, 'key': val}
3473 */
3474 case '{': {
3475 char_u *start = skipwhite(*arg + 1);
3476
3477 // Find out what comes after the arguments.
3478 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003479 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003480 if (ret != FAIL && *start == '>')
3481 ret = compile_lambda(arg, cctx);
3482 else
3483 ret = compile_dict(arg, cctx, FALSE);
3484 }
3485 break;
3486
3487 /*
3488 * Option value: &name
3489 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003490 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3491 return FAIL;
3492 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 break;
3494
3495 /*
3496 * Environment variable: $VAR.
3497 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003498 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3499 return FAIL;
3500 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 break;
3502
3503 /*
3504 * Register contents: @r.
3505 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003506 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3507 return FAIL;
3508 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509 break;
3510 /*
3511 * nested expression: (expression).
3512 */
3513 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003514
3515 // recursive!
3516 if (ppconst->pp_used <= PPSIZE - 10)
3517 {
3518 ret = compile_expr1(arg, cctx, ppconst);
3519 }
3520 else
3521 {
3522 // Not enough space in ppconst, flush constants.
3523 if (generate_ppconst(cctx, ppconst) == FAIL)
3524 return FAIL;
3525 ret = compile_expr0(arg, cctx);
3526 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003527 *arg = skipwhite(*arg);
3528 if (**arg == ')')
3529 ++*arg;
3530 else if (ret == OK)
3531 {
3532 emsg(_(e_missing_close));
3533 ret = FAIL;
3534 }
3535 break;
3536
3537 default: ret = NOTDONE;
3538 break;
3539 }
3540 if (ret == FAIL)
3541 return FAIL;
3542
Bram Moolenaar1c747212020-05-09 18:28:34 +02003543 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003544 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003545 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003546 clear_tv(rettv);
3547 else
3548 // A constant expression can possibly be handled compile time,
3549 // return the value instead of generating code.
3550 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551 }
3552 else if (ret == NOTDONE)
3553 {
3554 char_u *p;
3555 int r;
3556
3557 if (!eval_isnamec1(**arg))
3558 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003559 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560 return FAIL;
3561 }
3562
3563 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003564 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003566 {
3567 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3568 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003570 {
3571 if (generate_ppconst(cctx, ppconst) == FAIL)
3572 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003573 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003574 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 if (r == FAIL)
3576 return FAIL;
3577 }
3578
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003579 // Handle following "[]", ".member", etc.
3580 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003581 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003582 ppconst) == FAIL)
3583 return FAIL;
3584 if (ppconst->pp_used > 0)
3585 {
3586 // apply the '!', '-' and '+' before the constant
3587 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003588 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003589 return FAIL;
3590 return OK;
3591 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003592 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003594 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595}
3596
3597/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003598 * Give the "white on both sides" error, taking the operator from "p[len]".
3599 */
3600 void
3601error_white_both(char_u *op, int len)
3602{
3603 char_u buf[10];
3604
3605 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003606 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003607}
3608
3609/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003610 * <type>expr7: runtime type check / conversion
3611 */
3612 static int
3613compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3614{
3615 type_T *want_type = NULL;
3616
3617 // Recognize <type>
3618 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3619 {
3620 int called_emsg_before = called_emsg;
3621
3622 ++*arg;
3623 want_type = parse_type(arg, cctx->ctx_type_list);
3624 if (called_emsg != called_emsg_before)
3625 return FAIL;
3626
3627 if (**arg != '>')
3628 {
3629 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003630 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003631 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003632 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003633 return FAIL;
3634 }
3635 ++*arg;
3636 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3637 return FAIL;
3638 }
3639
3640 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3641 return FAIL;
3642
3643 if (want_type != NULL)
3644 {
3645 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003646 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003647
Bram Moolenaard1103582020-08-14 22:44:25 +02003648 generate_ppconst(cctx, ppconst);
3649 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003650 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003651 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003652 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3653 return FAIL;
3654 }
3655 }
3656
3657 return OK;
3658}
3659
3660/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 * * number multiplication
3662 * / number division
3663 * % number modulo
3664 */
3665 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003666compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667{
3668 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003669 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003670 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003672 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003673 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003674 return FAIL;
3675
3676 /*
3677 * Repeat computing, until no "*", "/" or "%" is following.
3678 */
3679 for (;;)
3680 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003681 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 if (*op != '*' && *op != '/' && *op != '%')
3683 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003684 if (next != NULL)
3685 {
3686 *arg = next_line_from_context(cctx, TRUE);
3687 op = skipwhite(*arg);
3688 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003689
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003690 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003692 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003693 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 }
3695 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003696 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003697 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003699 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003700 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003702
3703 if (ppconst->pp_used == ppconst_used + 2
3704 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3705 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003706 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003707 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3708 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003709 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003711 // both are numbers: compute the result
3712 switch (*op)
3713 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003714 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003715 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003716 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003717 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003718 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003719 break;
3720 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003721 tv1->vval.v_number = res;
3722 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003723 }
3724 else
3725 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003726 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003727 generate_two_op(cctx, op);
3728 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729 }
3730
3731 return OK;
3732}
3733
3734/*
3735 * + number addition
3736 * - number subtraction
3737 * .. string concatenation
3738 */
3739 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003740compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741{
3742 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003743 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003745 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746
3747 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003748 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749 return FAIL;
3750
3751 /*
3752 * Repeat computing, until no "+", "-" or ".." is following.
3753 */
3754 for (;;)
3755 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003756 op = may_peek_next_line(cctx, *arg, &next);
3757 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003758 break;
3759 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003760 if (next != NULL)
3761 {
3762 *arg = next_line_from_context(cctx, TRUE);
3763 op = skipwhite(*arg);
3764 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003766 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003768 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003769 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 }
3771
3772 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003773 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003774 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003775
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003776 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003777 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778 return FAIL;
3779
Bram Moolenaara5565e42020-05-09 15:44:01 +02003780 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003781 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003782 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3783 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3784 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3785 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003787 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3788 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003789
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003790 // concat/subtract/add constant numbers
3791 if (*op == '+')
3792 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3793 else if (*op == '-')
3794 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3795 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003796 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003797 // concatenate constant strings
3798 char_u *s1 = tv1->vval.v_string;
3799 char_u *s2 = tv2->vval.v_string;
3800 size_t len1 = STRLEN(s1);
3801
3802 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3803 if (tv1->vval.v_string == NULL)
3804 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003805 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003806 return FAIL;
3807 }
3808 mch_memmove(tv1->vval.v_string, s1, len1);
3809 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003810 vim_free(s1);
3811 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003812 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003813 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 }
3815 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003816 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003817 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003818 if (*op == '.')
3819 {
3820 if (may_generate_2STRING(-2, cctx) == FAIL
3821 || may_generate_2STRING(-1, cctx) == FAIL)
3822 return FAIL;
3823 generate_instr_drop(cctx, ISN_CONCAT, 1);
3824 }
3825 else
3826 generate_two_op(cctx, op);
3827 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 }
3829
3830 return OK;
3831}
3832
3833/*
3834 * expr5a == expr5b
3835 * expr5a =~ expr5b
3836 * expr5a != expr5b
3837 * expr5a !~ expr5b
3838 * expr5a > expr5b
3839 * expr5a >= expr5b
3840 * expr5a < expr5b
3841 * expr5a <= expr5b
3842 * expr5a is expr5b
3843 * expr5a isnot expr5b
3844 *
3845 * Produces instructions:
3846 * EVAL expr5a Push result of "expr5a"
3847 * EVAL expr5b Push result of "expr5b"
3848 * COMPARE one of the compare instructions
3849 */
3850 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003851compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003852{
3853 exptype_T type = EXPR_UNKNOWN;
3854 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003855 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003858 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859
3860 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003861 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862 return FAIL;
3863
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003864 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003865 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003866
3867 /*
3868 * If there is a comparative operator, use it.
3869 */
3870 if (type != EXPR_UNKNOWN)
3871 {
3872 int ic = FALSE; // Default: do not ignore case
3873
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003874 if (next != NULL)
3875 {
3876 *arg = next_line_from_context(cctx, TRUE);
3877 p = skipwhite(*arg);
3878 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879 if (type_is && (p[len] == '?' || p[len] == '#'))
3880 {
3881 semsg(_(e_invexpr2), *arg);
3882 return FAIL;
3883 }
3884 // extra question mark appended: ignore case
3885 if (p[len] == '?')
3886 {
3887 ic = TRUE;
3888 ++len;
3889 }
3890 // extra '#' appended: match case (ignored)
3891 else if (p[len] == '#')
3892 ++len;
3893 // nothing appended: match case
3894
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003895 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003897 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003898 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899 }
3900
3901 // get the second variable
3902 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003903 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003904 return FAIL;
3905
Bram Moolenaara5565e42020-05-09 15:44:01 +02003906 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907 return FAIL;
3908
Bram Moolenaara5565e42020-05-09 15:44:01 +02003909 if (ppconst->pp_used == ppconst_used + 2)
3910 {
3911 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3912 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3913 int ret;
3914
3915 // Both sides are a constant, compute the result now.
3916 // First check for a valid combination of types, this is more
3917 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003918 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003919 ret = FAIL;
3920 else
3921 {
3922 ret = typval_compare(tv1, tv2, type, ic);
3923 tv1->v_type = VAR_BOOL;
3924 tv1->vval.v_number = tv1->vval.v_number
3925 ? VVAL_TRUE : VVAL_FALSE;
3926 clear_tv(tv2);
3927 --ppconst->pp_used;
3928 }
3929 return ret;
3930 }
3931
3932 generate_ppconst(cctx, ppconst);
3933 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934 }
3935
3936 return OK;
3937}
3938
Bram Moolenaar7f141552020-05-09 17:35:53 +02003939static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3940
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941/*
3942 * Compile || or &&.
3943 */
3944 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003945compile_and_or(
3946 char_u **arg,
3947 cctx_T *cctx,
3948 char *op,
3949 ppconst_T *ppconst,
3950 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003952 char_u *next;
3953 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 int opchar = *op;
3955
3956 if (p[0] == opchar && p[1] == opchar)
3957 {
3958 garray_T *instr = &cctx->ctx_instr;
3959 garray_T end_ga;
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02003960 garray_T *stack = &cctx->ctx_type_stack;
3961 type_T **typep;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962
3963 /*
3964 * Repeat until there is no following "||" or "&&"
3965 */
3966 ga_init2(&end_ga, sizeof(int), 10);
3967 while (p[0] == opchar && p[1] == opchar)
3968 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003969 if (next != NULL)
3970 {
3971 *arg = next_line_from_context(cctx, TRUE);
3972 p = skipwhite(*arg);
3973 }
3974
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003975 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3976 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003977 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003978 return FAIL;
3979 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980
Bram Moolenaara5565e42020-05-09 15:44:01 +02003981 // TODO: use ppconst if the value is a constant
3982 generate_ppconst(cctx, ppconst);
3983
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984 if (ga_grow(&end_ga, 1) == FAIL)
3985 {
3986 ga_clear(&end_ga);
3987 return FAIL;
3988 }
3989 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3990 ++end_ga.ga_len;
3991 generate_JUMP(cctx, opchar == '|'
3992 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3993
3994 // eval the next expression
3995 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003996 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003997 return FAIL;
3998
Bram Moolenaara5565e42020-05-09 15:44:01 +02003999 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4000 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001 {
4002 ga_clear(&end_ga);
4003 return FAIL;
4004 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004005
4006 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004008 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009
4010 // Fill in the end label in all jumps.
4011 while (end_ga.ga_len > 0)
4012 {
4013 isn_T *isn;
4014
4015 --end_ga.ga_len;
4016 isn = ((isn_T *)instr->ga_data)
4017 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4018 isn->isn_arg.jump.jump_where = instr->ga_len;
4019 }
4020 ga_clear(&end_ga);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004021
4022 // The resulting type can be used as a bool.
4023 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4024 if (*typep != &t_bool)
4025 {
4026 type_T *type = alloc_type(cctx->ctx_type_list);
4027
4028 if (type != NULL)
4029 {
4030 *type = **typep;
4031 type->tt_flags |= TTFLAG_BOOL_OK;
4032 *typep = type;
4033 }
4034 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004035 }
4036
4037 return OK;
4038}
4039
4040/*
4041 * expr4a && expr4a && expr4a logical AND
4042 *
4043 * Produces instructions:
4044 * EVAL expr4a Push result of "expr4a"
4045 * JUMP_AND_KEEP_IF_FALSE end
4046 * EVAL expr4b Push result of "expr4b"
4047 * JUMP_AND_KEEP_IF_FALSE end
4048 * EVAL expr4c Push result of "expr4c"
4049 * end:
4050 */
4051 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004052compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004054 int ppconst_used = ppconst->pp_used;
4055
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004056 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004057 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058 return FAIL;
4059
4060 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004061 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062}
4063
4064/*
4065 * expr3a || expr3b || expr3c logical OR
4066 *
4067 * Produces instructions:
4068 * EVAL expr3a Push result of "expr3a"
4069 * JUMP_AND_KEEP_IF_TRUE end
4070 * EVAL expr3b Push result of "expr3b"
4071 * JUMP_AND_KEEP_IF_TRUE end
4072 * EVAL expr3c Push result of "expr3c"
4073 * end:
4074 */
4075 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004076compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004078 int ppconst_used = ppconst->pp_used;
4079
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004080 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004081 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004082 return FAIL;
4083
4084 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004085 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004086}
4087
4088/*
4089 * Toplevel expression: expr2 ? expr1a : expr1b
4090 *
4091 * Produces instructions:
4092 * EVAL expr2 Push result of "expr"
4093 * JUMP_IF_FALSE alt jump if false
4094 * EVAL expr1a
4095 * JUMP_ALWAYS end
4096 * alt: EVAL expr1b
4097 * end:
4098 */
4099 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004100compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101{
4102 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004103 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004104 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004105
Bram Moolenaar3988f642020-08-27 22:43:03 +02004106 // Ignore all kinds of errors when not producing code.
4107 if (cctx->ctx_skip == SKIP_YES)
4108 {
4109 skip_expr(arg);
4110 return OK;
4111 }
4112
Bram Moolenaar61a89812020-05-07 16:58:17 +02004113 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004114 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115 return FAIL;
4116
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004117 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 if (*p == '?')
4119 {
4120 garray_T *instr = &cctx->ctx_instr;
4121 garray_T *stack = &cctx->ctx_type_stack;
4122 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004123 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004125 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004127 int has_const_expr = FALSE;
4128 int const_value = FALSE;
4129 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004131 if (next != NULL)
4132 {
4133 *arg = next_line_from_context(cctx, TRUE);
4134 p = skipwhite(*arg);
4135 }
4136
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004137 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4138 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004139 semsg(_(e_white_space_required_before_and_after_str), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004140 return FAIL;
4141 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142
Bram Moolenaara5565e42020-05-09 15:44:01 +02004143 if (ppconst->pp_used == ppconst_used + 1)
4144 {
4145 // the condition is a constant, we know whether the ? or the :
4146 // expression is to be evaluated.
4147 has_const_expr = TRUE;
4148 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4149 clear_tv(&ppconst->pp_tv[ppconst_used]);
4150 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004151 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4152 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004153 }
4154 else
4155 {
4156 generate_ppconst(cctx, ppconst);
4157 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4158 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159
4160 // evaluate the second expression; any type is accepted
4161 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004162 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004163 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004164 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004165 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004166
Bram Moolenaara5565e42020-05-09 15:44:01 +02004167 if (!has_const_expr)
4168 {
4169 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170
Bram Moolenaara5565e42020-05-09 15:44:01 +02004171 // remember the type and drop it
4172 --stack->ga_len;
4173 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004174
Bram Moolenaara5565e42020-05-09 15:44:01 +02004175 end_idx = instr->ga_len;
4176 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4177
4178 // jump here from JUMP_IF_FALSE
4179 isn = ((isn_T *)instr->ga_data) + alt_idx;
4180 isn->isn_arg.jump.jump_where = instr->ga_len;
4181 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004182
4183 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004184 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 if (*p != ':')
4186 {
4187 emsg(_(e_missing_colon));
4188 return FAIL;
4189 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004190 if (next != NULL)
4191 {
4192 *arg = next_line_from_context(cctx, TRUE);
4193 p = skipwhite(*arg);
4194 }
4195
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004196 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4197 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004198 semsg(_(e_white_space_required_before_and_after_str), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004199 return FAIL;
4200 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201
4202 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004203 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004204 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4205 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004207 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004208 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004209 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004210 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211
Bram Moolenaara5565e42020-05-09 15:44:01 +02004212 if (!has_const_expr)
4213 {
4214 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215
Bram Moolenaara5565e42020-05-09 15:44:01 +02004216 // If the types differ, the result has a more generic type.
4217 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4218 common_type(type1, type2, &type2, cctx->ctx_type_list);
4219
4220 // jump here from JUMP_ALWAYS
4221 isn = ((isn_T *)instr->ga_data) + end_idx;
4222 isn->isn_arg.jump.jump_where = instr->ga_len;
4223 }
4224
4225 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226 }
4227 return OK;
4228}
4229
4230/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004231 * Toplevel expression.
4232 */
4233 static int
4234compile_expr0(char_u **arg, cctx_T *cctx)
4235{
4236 ppconst_T ppconst;
4237
4238 CLEAR_FIELD(ppconst);
4239 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4240 {
4241 clear_ppconst(&ppconst);
4242 return FAIL;
4243 }
4244 if (generate_ppconst(cctx, &ppconst) == FAIL)
4245 return FAIL;
4246 return OK;
4247}
4248
4249/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250 * compile "return [expr]"
4251 */
4252 static char_u *
4253compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4254{
4255 char_u *p = arg;
4256 garray_T *stack = &cctx->ctx_type_stack;
4257 type_T *stack_type;
4258
4259 if (*p != NUL && *p != '|' && *p != '\n')
4260 {
4261 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004262 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004263 return NULL;
4264
4265 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4266 if (set_return_type)
4267 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004268 else
4269 {
4270 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4271 && stack_type->tt_type != VAR_VOID
4272 && stack_type->tt_type != VAR_UNKNOWN)
4273 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004274 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004275 return NULL;
4276 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004277 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4278 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004280 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004281 }
4282 else
4283 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004284 // "set_return_type" cannot be TRUE, only used for a lambda which
4285 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004286 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4287 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004289 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 return NULL;
4291 }
4292
4293 // No argument, return zero.
4294 generate_PUSHNR(cctx, 0);
4295 }
4296
4297 if (generate_instr(cctx, ISN_RETURN) == NULL)
4298 return NULL;
4299
4300 // "return val | endif" is possible
4301 return skipwhite(p);
4302}
4303
4304/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004305 * Get a line from the compilation context, compatible with exarg_T getline().
4306 * Return a pointer to the line in allocated memory.
4307 * Return NULL for end-of-file or some error.
4308 */
4309 static char_u *
4310exarg_getline(
4311 int c UNUSED,
4312 void *cookie,
4313 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004314 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004315{
4316 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004317 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004318
Bram Moolenaar66250c92020-08-20 15:02:42 +02004319 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004320 {
Bram Moolenaar66250c92020-08-20 15:02:42 +02004321 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4322 return NULL;
4323 ++cctx->ctx_lnum;
4324 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4325 // Comment lines result in NULL pointers, skip them.
4326 if (p != NULL)
4327 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004328 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004329}
4330
4331/*
4332 * Compile a nested :def command.
4333 */
4334 static char_u *
4335compile_nested_function(exarg_T *eap, cctx_T *cctx)
4336{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004337 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004338 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004339 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004340 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004341 lvar_T *lvar;
4342 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004343 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004344
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004345 if (*name_start == '!')
4346 {
4347 emsg(_(e_cannot_use_bang_with_nested_def));
4348 return NULL;
4349 }
4350
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004351 // Only g:Func() can use a namespace.
4352 if (name_start[1] == ':' && !is_global)
4353 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004354 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004355 return NULL;
4356 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004357 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4358 return NULL;
4359
Bram Moolenaar04b12692020-05-04 23:24:44 +02004360 eap->arg = name_end;
4361 eap->getline = exarg_getline;
4362 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004363 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004364 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004365 lambda_name = get_lambda_name();
4366 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004367
Bram Moolenaar822ba242020-05-24 23:00:18 +02004368 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004369 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004370 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004371 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004372 return NULL;
4373
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004374 if (is_global)
4375 {
4376 char_u *func_name = vim_strnsave(name_start + 2,
4377 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004378
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004379 if (func_name == NULL)
4380 r = FAIL;
4381 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004382 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004383 }
4384 else
4385 {
4386 // Define a local variable for the function reference.
4387 lvar = reserve_local(cctx, name_start, name_end - name_start,
4388 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004389 if (lvar == NULL)
4390 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004391 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004392 return NULL;
4393 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4394 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004395
Bram Moolenaar61a89812020-05-07 16:58:17 +02004396 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004397 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004398}
4399
4400/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 * Return the length of an assignment operator, or zero if there isn't one.
4402 */
4403 int
4404assignment_len(char_u *p, int *heredoc)
4405{
4406 if (*p == '=')
4407 {
4408 if (p[1] == '<' && p[2] == '<')
4409 {
4410 *heredoc = TRUE;
4411 return 3;
4412 }
4413 return 1;
4414 }
4415 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4416 return 2;
4417 if (STRNCMP(p, "..=", 3) == 0)
4418 return 3;
4419 return 0;
4420}
4421
4422// words that cannot be used as a variable
4423static char *reserved[] = {
4424 "true",
4425 "false",
4426 NULL
4427};
4428
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004429typedef enum {
4430 dest_local,
4431 dest_option,
4432 dest_env,
4433 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004434 dest_buffer,
4435 dest_window,
4436 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004437 dest_vimvar,
4438 dest_script,
4439 dest_reg,
4440} assign_dest_T;
4441
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004443 * Generate the load instruction for "name".
4444 */
4445 static void
4446generate_loadvar(
4447 cctx_T *cctx,
4448 assign_dest_T dest,
4449 char_u *name,
4450 lvar_T *lvar,
4451 type_T *type)
4452{
4453 switch (dest)
4454 {
4455 case dest_option:
4456 // TODO: check the option exists
4457 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4458 break;
4459 case dest_global:
4460 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4461 break;
4462 case dest_buffer:
4463 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4464 break;
4465 case dest_window:
4466 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4467 break;
4468 case dest_tab:
4469 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4470 break;
4471 case dest_script:
4472 compile_load_scriptvar(cctx,
4473 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4474 break;
4475 case dest_env:
4476 // Include $ in the name here
4477 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4478 break;
4479 case dest_reg:
4480 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4481 break;
4482 case dest_vimvar:
4483 generate_LOADV(cctx, name + 2, TRUE);
4484 break;
4485 case dest_local:
4486 if (lvar->lv_from_outer)
4487 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4488 NULL, type);
4489 else
4490 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4491 break;
4492 }
4493}
4494
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004495 void
4496vim9_declare_error(char_u *name)
4497{
4498 char *scope = "";
4499
4500 switch (*name)
4501 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004502 case 'g': scope = _("global"); break;
4503 case 'b': scope = _("buffer"); break;
4504 case 'w': scope = _("window"); break;
4505 case 't': scope = _("tab"); break;
4506 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004507 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004508 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004509 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004510 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004511 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004512 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004513 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004514 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004515 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004516}
4517
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004518/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004519 * Compile declaration and assignment:
4520 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004521 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004522 * Return NULL for an error.
4523 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004524 */
4525 static char_u *
4526compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4527{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004528 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004529 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004530 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004531 char_u *ret = NULL;
4532 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004533 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004534 int scriptvar_sid = 0;
4535 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004537 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004538 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004539 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004540 int oplen = 0;
4541 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004542 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004543 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004544 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004546 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004547
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004548 // Skip over the "var" or "[var, var]" to get to any "=".
4549 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4550 if (p == NULL)
4551 return *arg == '[' ? arg : NULL;
4552
4553 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004554 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004555 // TODO: should we allow this, and figure out type inference from list
4556 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004557 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004558 return NULL;
4559 }
4560
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004561 sp = p;
4562 p = skipwhite(p);
4563 op = p;
4564 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004565
4566 if (var_count > 0 && oplen == 0)
4567 // can be something like "[1, 2]->func()"
4568 return arg;
4569
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4571 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004572 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004573 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004574 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004575
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 if (heredoc)
4577 {
4578 list_T *l;
4579 listitem_T *li;
4580
4581 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004582 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004583 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004584 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585
4586 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004587 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004588 {
4589 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4590 li->li_tv.vval.v_string = NULL;
4591 }
4592 generate_NEWLIST(cctx, l->lv_len);
4593 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004594 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595 list_free(l);
4596 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004597 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004599 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004601 // for "[var, var] = expr" evaluate the expression here, loop over the
4602 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004603
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004604 p = skipwhite(op + oplen);
4605 if (compile_expr0(&p, cctx) == FAIL)
4606 return NULL;
4607 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004609 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004611 type_T *stacktype;
4612
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004613 stacktype = stack->ga_len == 0 ? &t_void
4614 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004615 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004617 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004618 goto theend;
4619 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004620 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004621 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004622 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004623 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4624 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004625 }
4626 }
4627
4628 /*
4629 * Loop over variables in "[var, var] = expr".
4630 * For "var = expr" and "let var: type" this is done only once.
4631 */
4632 if (var_count > 0)
4633 var_start = skipwhite(arg + 1); // skip over the "["
4634 else
4635 var_start = arg;
4636 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4637 {
4638 char_u *var_end = skip_var_one(var_start, FALSE);
4639 size_t varlen;
4640 int new_local = FALSE;
4641 int opt_type;
4642 int opt_flags = 0;
4643 assign_dest_T dest = dest_local;
4644 int vimvaridx = -1;
4645 lvar_T *lvar = NULL;
4646 lvar_T arg_lvar;
4647 int has_type = FALSE;
4648 int has_index = FALSE;
4649 int instr_count = -1;
4650
Bram Moolenaar65821722020-08-02 18:58:54 +02004651 if (*var_start == '@')
4652 p = var_start + 2;
4653 else
4654 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02004655 // skip over the leading "&", "&l:", "&g:" and "$"
4656 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02004657 p = to_name_end(p, TRUE);
4658 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004659
4660 // "a: type" is declaring variable "a" with a type, not "a:".
4661 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4662 --var_end;
4663 if (is_decl && p == var_start + 2 && p[-1] == ':')
4664 --p;
4665
4666 varlen = p - var_start;
4667 vim_free(name);
4668 name = vim_strnsave(var_start, varlen);
4669 if (name == NULL)
4670 return NULL;
4671 if (!heredoc)
4672 type = &t_any;
4673
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004674 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004675 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004676 int declare_error = FALSE;
4677
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004678 if (*var_start == '&')
4679 {
4680 int cc;
4681 long numval;
4682
4683 dest = dest_option;
4684 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004685 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004686 emsg(_(e_const_option));
4687 goto theend;
4688 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004689 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004690 p = var_start;
4691 p = find_option_end(&p, &opt_flags);
4692 if (p == NULL)
4693 {
4694 // cannot happen?
4695 emsg(_(e_letunexp));
4696 goto theend;
4697 }
4698 cc = *p;
4699 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02004700 opt_type = get_option_value(skip_option_env_lead(var_start),
4701 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004702 *p = cc;
4703 if (opt_type == -3)
4704 {
4705 semsg(_(e_unknown_option), var_start);
4706 goto theend;
4707 }
4708 if (opt_type == -2 || opt_type == 0)
4709 type = &t_string;
4710 else
4711 type = &t_number; // both number and boolean option
4712 }
4713 else if (*var_start == '$')
4714 {
4715 dest = dest_env;
4716 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004717 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004718 }
4719 else if (*var_start == '@')
4720 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004721 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004722 {
4723 emsg_invreg(var_start[1]);
4724 goto theend;
4725 }
4726 dest = dest_reg;
4727 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004728 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004729 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004730 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004731 {
4732 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004733 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004734 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004735 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004736 {
4737 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004738 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004739 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004740 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004741 {
4742 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004743 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004744 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004745 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004746 {
4747 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004748 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004749 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004750 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004751 {
4752 typval_T *vtv;
4753 int di_flags;
4754
4755 vimvaridx = find_vim_var(name + 2, &di_flags);
4756 if (vimvaridx < 0)
4757 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004758 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004759 goto theend;
4760 }
4761 // We use the current value of "sandbox" here, is that OK?
4762 if (var_check_ro(di_flags, name, FALSE))
4763 goto theend;
4764 dest = dest_vimvar;
4765 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004766 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004767 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004768 }
4769 else
4770 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004771 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004772
4773 for (idx = 0; reserved[idx] != NULL; ++idx)
4774 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004775 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004776 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004777 goto theend;
4778 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004779
4780 lvar = lookup_local(var_start, varlen, cctx);
4781 if (lvar == NULL)
4782 {
4783 CLEAR_FIELD(arg_lvar);
4784 if (lookup_arg(var_start, varlen,
4785 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4786 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004787 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004788 if (is_decl)
4789 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004790 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004791 goto theend;
4792 }
4793 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004794 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004795 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004796 if (lvar != NULL)
4797 {
4798 if (is_decl)
4799 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004800 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004801 goto theend;
4802 }
4803 else if (lvar->lv_const)
4804 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004805 semsg(_(e_cannot_assign_to_constant), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004806 goto theend;
4807 }
4808 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004809 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004810 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004811 int script_namespace = varlen > 1
4812 && STRNCMP(var_start, "s:", 2) == 0;
4813 int script_var = (script_namespace
Bram Moolenaar53900992020-08-22 19:02:02 +02004814 ? lookup_script(var_start + 2, varlen - 2, FALSE)
4815 : lookup_script(var_start, varlen, TRUE)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004816 imported_T *import =
4817 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004818
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004819 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004820 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004821 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4822
4823 if (is_decl)
4824 {
4825 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004826 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004827 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004828 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004829 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004830 name);
4831 goto theend;
4832 }
4833 else if (cctx->ctx_ufunc->uf_script_ctx_version
4834 == SCRIPT_VERSION_VIM9
4835 && script_namespace
4836 && !script_var && import == NULL)
4837 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004838 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004839 goto theend;
4840 }
4841
4842 dest = dest_script;
4843
4844 // existing script-local variables should have a type
4845 scriptvar_sid = current_sctx.sc_sid;
4846 if (import != NULL)
4847 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004848 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004849 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02004850 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4851 rawname, TRUE);
4852 if (scriptvar_idx > 0)
4853 {
4854 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4855 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004856 ((svar_T *)si->sn_var_vals.ga_data)
4857 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004858 type = sv->sv_type;
4859 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004860 }
4861 }
4862 else if (name[1] == ':' && name[2] != NUL)
4863 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004864 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004865 goto theend;
4866 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004867 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004868 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004869 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004870 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004871 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004872 else if (check_defined(var_start, varlen, cctx) == FAIL)
4873 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004874 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004875 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004876
4877 if (declare_error)
4878 {
4879 vim9_declare_error(name);
4880 goto theend;
4881 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004882 }
4883
4884 // handle "a:name" as a name, not index "name" on "a"
4885 if (varlen > 1 || var_start[varlen] != ':')
4886 p = var_end;
4887
4888 if (dest != dest_option)
4889 {
4890 if (is_decl && *p == ':')
4891 {
4892 // parse optional type: "let var: type = expr"
4893 if (!VIM_ISWHITE(p[1]))
4894 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004895 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004896 goto theend;
4897 }
4898 p = skipwhite(p + 1);
4899 type = parse_type(&p, cctx->ctx_type_list);
4900 has_type = TRUE;
4901 }
4902 else if (lvar != NULL)
4903 type = lvar->lv_type;
4904 }
4905
4906 if (oplen == 3 && !heredoc && dest != dest_global
4907 && type->tt_type != VAR_STRING
4908 && type->tt_type != VAR_ANY)
4909 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004910 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004911 goto theend;
4912 }
4913
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004914 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004915 {
4916 if (oplen > 1 && !heredoc)
4917 {
4918 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004919 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004920 goto theend;
4921 }
4922
4923 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004924 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
4925 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004926 goto theend;
4927 lvar = reserve_local(cctx, var_start, varlen,
4928 cmdidx == CMD_const, type);
4929 if (lvar == NULL)
4930 goto theend;
4931 new_local = TRUE;
4932 }
4933
4934 member_type = type;
4935 if (var_end > var_start + varlen)
4936 {
4937 // Something follows after the variable: "var[idx]".
4938 if (is_decl)
4939 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004940 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004941 goto theend;
4942 }
4943
4944 if (var_start[varlen] == '[')
4945 {
4946 has_index = TRUE;
4947 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004948 member_type = &t_any;
4949 else
4950 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004951 }
4952 else
4953 {
4954 semsg("Not supported yet: %s", var_start);
4955 goto theend;
4956 }
4957 }
4958 else if (lvar == &arg_lvar)
4959 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004960 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004961 goto theend;
4962 }
4963
4964 if (!heredoc)
4965 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004966 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004967 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004968 if (oplen > 0 && var_count == 0)
4969 {
4970 // skip over the "=" and the expression
4971 p = skipwhite(op + oplen);
4972 compile_expr0(&p, cctx);
4973 }
4974 }
4975 else if (oplen > 0)
4976 {
4977 type_T *stacktype;
4978
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004979 // For "var = expr" evaluate the expression.
4980 if (var_count == 0)
4981 {
4982 int r;
4983
4984 // for "+=", "*=", "..=" etc. first load the current value
4985 if (*op != '=')
4986 {
4987 generate_loadvar(cctx, dest, name, lvar, type);
4988
4989 if (has_index)
4990 {
4991 // TODO: get member from list or dict
4992 emsg("Index with operation not supported yet");
4993 goto theend;
4994 }
4995 }
4996
4997 // Compile the expression. Temporarily hide the new local
4998 // variable here, it is not available to this expression.
4999 if (new_local)
5000 --cctx->ctx_locals.ga_len;
5001 instr_count = instr->ga_len;
5002 p = skipwhite(op + oplen);
5003 r = compile_expr0(&p, cctx);
5004 if (new_local)
5005 ++cctx->ctx_locals.ga_len;
5006 if (r == FAIL)
5007 goto theend;
5008 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005009 else if (semicolon && var_idx == var_count - 1)
5010 {
5011 // For "[var; var] = expr" get the rest of the list
5012 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5013 goto theend;
5014 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005015 else
5016 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005017 // For "[var, var] = expr" get the "var_idx" item from the
5018 // list.
5019 if (generate_GETITEM(cctx, var_idx) == FAIL)
5020 return FAIL;
5021 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005022
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005023 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005024 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005025 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005026 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005027 if ((stacktype->tt_type == VAR_FUNC
5028 || stacktype->tt_type == VAR_PARTIAL)
5029 && var_wrong_func_name(name, TRUE))
5030 goto theend;
5031
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005032 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005033 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005034 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005035 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005036 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005037 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005038 }
5039 else
5040 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005041 // An empty list or dict has a &t_void member,
5042 // for a variable that implies &t_any.
5043 if (stacktype == &t_list_empty)
5044 lvar->lv_type = &t_list_any;
5045 else if (stacktype == &t_dict_empty)
5046 lvar->lv_type = &t_dict_any;
5047 else
5048 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005049 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005050 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005051 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005052 {
5053 type_T *use_type = lvar->lv_type;
5054
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005055 // without operator type is here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005056 if (has_index)
5057 {
5058 use_type = use_type->tt_member;
5059 if (use_type == NULL)
5060 use_type = &t_void;
5061 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005062 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005063 == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005064 goto theend;
5065 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005066 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005067 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005068 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005069 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005070 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005071 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005072 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005073 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005074 goto theend;
5075 }
5076 else if (!has_type || dest == dest_option)
5077 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005078 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005079 goto theend;
5080 }
5081 else
5082 {
5083 // variables are always initialized
5084 if (ga_grow(instr, 1) == FAIL)
5085 goto theend;
5086 switch (member_type->tt_type)
5087 {
5088 case VAR_BOOL:
5089 generate_PUSHBOOL(cctx, VVAL_FALSE);
5090 break;
5091 case VAR_FLOAT:
5092#ifdef FEAT_FLOAT
5093 generate_PUSHF(cctx, 0.0);
5094#endif
5095 break;
5096 case VAR_STRING:
5097 generate_PUSHS(cctx, NULL);
5098 break;
5099 case VAR_BLOB:
5100 generate_PUSHBLOB(cctx, NULL);
5101 break;
5102 case VAR_FUNC:
5103 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5104 break;
5105 case VAR_LIST:
5106 generate_NEWLIST(cctx, 0);
5107 break;
5108 case VAR_DICT:
5109 generate_NEWDICT(cctx, 0);
5110 break;
5111 case VAR_JOB:
5112 generate_PUSHJOB(cctx, NULL);
5113 break;
5114 case VAR_CHANNEL:
5115 generate_PUSHCHANNEL(cctx, NULL);
5116 break;
5117 case VAR_NUMBER:
5118 case VAR_UNKNOWN:
5119 case VAR_ANY:
5120 case VAR_PARTIAL:
5121 case VAR_VOID:
5122 case VAR_SPECIAL: // cannot happen
5123 generate_PUSHNR(cctx, 0);
5124 break;
5125 }
5126 }
5127 if (var_count == 0)
5128 end = p;
5129 }
5130
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005131 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005132 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005133 break;
5134
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005135 if (oplen > 0 && *op != '=')
5136 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005137 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005138 type_T *stacktype;
5139
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005140 if (*op == '.')
5141 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005142 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005143 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005144 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005145 if (
5146#ifdef FEAT_FLOAT
5147 // If variable is float operation with number is OK.
5148 !(expected == &t_float && stacktype == &t_number) &&
5149#endif
5150 need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005151 goto theend;
5152
5153 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005154 {
5155 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5156 goto theend;
5157 }
5158 else if (*op == '+')
5159 {
5160 if (generate_add_instr(cctx,
5161 operator_type(member_type, stacktype),
5162 member_type, stacktype) == FAIL)
5163 goto theend;
5164 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005165 else if (generate_two_op(cctx, op) == FAIL)
5166 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005167 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005168
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005169 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005170 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005171 int r;
5172
5173 // Compile the "idx" in "var[idx]".
5174 if (new_local)
5175 --cctx->ctx_locals.ga_len;
5176 p = skipwhite(var_start + varlen + 1);
5177 r = compile_expr0(&p, cctx);
5178 if (new_local)
5179 ++cctx->ctx_locals.ga_len;
5180 if (r == FAIL)
5181 goto theend;
5182 if (*skipwhite(p) != ']')
5183 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005184 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005185 emsg(_(e_missbrac));
5186 goto theend;
5187 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005188 if (type == &t_any)
5189 {
5190 type_T *idx_type = ((type_T **)stack->ga_data)[
5191 stack->ga_len - 1];
5192 // Index on variable of unknown type: guess the type from the
5193 // index type: number is dict, otherwise dict.
5194 // TODO: should do the assignment at runtime
5195 if (idx_type->tt_type == VAR_NUMBER)
5196 type = &t_list_any;
5197 else
5198 type = &t_dict_any;
5199 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005200 if (type->tt_type == VAR_DICT
5201 && may_generate_2STRING(-1, cctx) == FAIL)
5202 goto theend;
5203 if (type->tt_type == VAR_LIST
5204 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005205 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005206 {
5207 emsg(_(e_number_exp));
5208 goto theend;
5209 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005210
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005211 // Load the dict or list. On the stack we then have:
5212 // - value
5213 // - index
5214 // - variable
5215 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005216
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005217 if (type->tt_type == VAR_LIST)
5218 {
5219 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5220 return FAIL;
5221 }
5222 else if (type->tt_type == VAR_DICT)
5223 {
5224 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5225 return FAIL;
5226 }
5227 else
5228 {
5229 emsg(_(e_listreq));
5230 goto theend;
5231 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005232 }
5233 else
5234 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005235 switch (dest)
5236 {
5237 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005238 generate_STOREOPT(cctx, skip_option_env_lead(name),
5239 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005240 break;
5241 case dest_global:
5242 // include g: with the name, easier to execute that way
5243 generate_STORE(cctx, ISN_STOREG, 0, name);
5244 break;
5245 case dest_buffer:
5246 // include b: with the name, easier to execute that way
5247 generate_STORE(cctx, ISN_STOREB, 0, name);
5248 break;
5249 case dest_window:
5250 // include w: with the name, easier to execute that way
5251 generate_STORE(cctx, ISN_STOREW, 0, name);
5252 break;
5253 case dest_tab:
5254 // include t: with the name, easier to execute that way
5255 generate_STORE(cctx, ISN_STORET, 0, name);
5256 break;
5257 case dest_env:
5258 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5259 break;
5260 case dest_reg:
5261 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5262 break;
5263 case dest_vimvar:
5264 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5265 break;
5266 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005267 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005268 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005269 {
5270 char_u *name_s = name;
5271
5272 // Include s: in the name for store_var()
5273 if (name[1] != ':')
5274 {
5275 int len = (int)STRLEN(name) + 3;
5276
5277 name_s = alloc(len);
5278 if (name_s == NULL)
5279 name_s = name;
5280 else
5281 vim_snprintf((char *)name_s, len,
5282 "s:%s", name);
5283 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005284 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5285 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005286 if (name_s != name)
5287 vim_free(name_s);
5288 }
5289 else
5290 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005291 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005292 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005293 break;
5294 case dest_local:
5295 if (lvar != NULL)
5296 {
5297 isn_T *isn = ((isn_T *)instr->ga_data)
5298 + instr->ga_len - 1;
5299
5300 // optimization: turn "var = 123" from ISN_PUSHNR +
5301 // ISN_STORE into ISN_STORENR
5302 if (!lvar->lv_from_outer
5303 && instr->ga_len == instr_count + 1
5304 && isn->isn_type == ISN_PUSHNR)
5305 {
5306 varnumber_T val = isn->isn_arg.number;
5307
5308 isn->isn_type = ISN_STORENR;
5309 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5310 isn->isn_arg.storenr.stnr_val = val;
5311 if (stack->ga_len > 0)
5312 --stack->ga_len;
5313 }
5314 else if (lvar->lv_from_outer)
5315 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005316 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005317 else
5318 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5319 }
5320 break;
5321 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005322 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005323
5324 if (var_idx + 1 < var_count)
5325 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005326 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005327
5328 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005329 if (var_count > 0 && !semicolon)
5330 {
5331 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5332 goto theend;
5333 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005334
Bram Moolenaarb2097502020-07-19 17:17:02 +02005335 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005336
5337theend:
5338 vim_free(name);
5339 return ret;
5340}
5341
5342/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005343 * Check if "name" can be "unlet".
5344 */
5345 int
5346check_vim9_unlet(char_u *name)
5347{
5348 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5349 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005350 // "unlet s:var" is allowed in legacy script.
5351 if (*name == 's' && !script_is_vim9())
5352 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005353 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005354 return FAIL;
5355 }
5356 return OK;
5357}
5358
5359/*
5360 * Callback passed to ex_unletlock().
5361 */
5362 static int
5363compile_unlet(
5364 lval_T *lvp,
5365 char_u *name_end,
5366 exarg_T *eap,
5367 int deep UNUSED,
5368 void *coookie)
5369{
5370 cctx_T *cctx = coookie;
5371
5372 if (lvp->ll_tv == NULL)
5373 {
5374 char_u *p = lvp->ll_name;
5375 int cc = *name_end;
5376 int ret = OK;
5377
5378 // Normal name. Only supports g:, w:, t: and b: namespaces.
5379 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005380 if (*p == '$')
5381 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5382 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005383 ret = FAIL;
5384 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005385 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005386
5387 *name_end = cc;
5388 return ret;
5389 }
5390
5391 // TODO: unlet {list}[idx]
5392 // TODO: unlet {dict}[key]
5393 emsg("Sorry, :unlet not fully implemented yet");
5394 return FAIL;
5395}
5396
5397/*
5398 * compile "unlet var", "lock var" and "unlock var"
5399 * "arg" points to "var".
5400 */
5401 static char_u *
5402compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5403{
5404 char_u *p = arg;
5405
5406 if (eap->cmdidx != CMD_unlet)
5407 {
5408 emsg("Sorry, :lock and unlock not implemented yet");
5409 return NULL;
5410 }
5411
5412 if (*p == '!')
5413 {
5414 p = skipwhite(p + 1);
5415 eap->forceit = TRUE;
5416 }
5417
5418 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5419 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5420}
5421
5422/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005423 * Compile an :import command.
5424 */
5425 static char_u *
5426compile_import(char_u *arg, cctx_T *cctx)
5427{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005428 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005429}
5430
5431/*
5432 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5433 */
5434 static int
5435compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5436{
5437 garray_T *instr = &cctx->ctx_instr;
5438 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5439
5440 if (endlabel == NULL)
5441 return FAIL;
5442 endlabel->el_next = *el;
5443 *el = endlabel;
5444 endlabel->el_end_label = instr->ga_len;
5445
5446 generate_JUMP(cctx, when, 0);
5447 return OK;
5448}
5449
5450 static void
5451compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5452{
5453 garray_T *instr = &cctx->ctx_instr;
5454
5455 while (*el != NULL)
5456 {
5457 endlabel_T *cur = (*el);
5458 isn_T *isn;
5459
5460 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5461 isn->isn_arg.jump.jump_where = instr->ga_len;
5462 *el = cur->el_next;
5463 vim_free(cur);
5464 }
5465}
5466
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005467 static void
5468compile_free_jump_to_end(endlabel_T **el)
5469{
5470 while (*el != NULL)
5471 {
5472 endlabel_T *cur = (*el);
5473
5474 *el = cur->el_next;
5475 vim_free(cur);
5476 }
5477}
5478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005479/*
5480 * Create a new scope and set up the generic items.
5481 */
5482 static scope_T *
5483new_scope(cctx_T *cctx, scopetype_T type)
5484{
5485 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5486
5487 if (scope == NULL)
5488 return NULL;
5489 scope->se_outer = cctx->ctx_scope;
5490 cctx->ctx_scope = scope;
5491 scope->se_type = type;
5492 scope->se_local_count = cctx->ctx_locals.ga_len;
5493 return scope;
5494}
5495
5496/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005497 * Free the current scope and go back to the outer scope.
5498 */
5499 static void
5500drop_scope(cctx_T *cctx)
5501{
5502 scope_T *scope = cctx->ctx_scope;
5503
5504 if (scope == NULL)
5505 {
5506 iemsg("calling drop_scope() without a scope");
5507 return;
5508 }
5509 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005510 switch (scope->se_type)
5511 {
5512 case IF_SCOPE:
5513 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5514 case FOR_SCOPE:
5515 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5516 case WHILE_SCOPE:
5517 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5518 case TRY_SCOPE:
5519 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5520 case NO_SCOPE:
5521 case BLOCK_SCOPE:
5522 break;
5523 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005524 vim_free(scope);
5525}
5526
5527/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005528 * compile "if expr"
5529 *
5530 * "if expr" Produces instructions:
5531 * EVAL expr Push result of "expr"
5532 * JUMP_IF_FALSE end
5533 * ... body ...
5534 * end:
5535 *
5536 * "if expr | else" Produces instructions:
5537 * EVAL expr Push result of "expr"
5538 * JUMP_IF_FALSE else
5539 * ... body ...
5540 * JUMP_ALWAYS end
5541 * else:
5542 * ... body ...
5543 * end:
5544 *
5545 * "if expr1 | elseif expr2 | else" Produces instructions:
5546 * EVAL expr Push result of "expr"
5547 * JUMP_IF_FALSE elseif
5548 * ... body ...
5549 * JUMP_ALWAYS end
5550 * elseif:
5551 * EVAL expr Push result of "expr"
5552 * JUMP_IF_FALSE else
5553 * ... body ...
5554 * JUMP_ALWAYS end
5555 * else:
5556 * ... body ...
5557 * end:
5558 */
5559 static char_u *
5560compile_if(char_u *arg, cctx_T *cctx)
5561{
5562 char_u *p = arg;
5563 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005564 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005565 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005566 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005567 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005568
Bram Moolenaara5565e42020-05-09 15:44:01 +02005569 CLEAR_FIELD(ppconst);
5570 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005571 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005572 clear_ppconst(&ppconst);
5573 return NULL;
5574 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005575 if (cctx->ctx_skip == SKIP_YES)
5576 clear_ppconst(&ppconst);
5577 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005578 {
5579 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005580 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005581 clear_ppconst(&ppconst);
5582 }
5583 else
5584 {
5585 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005586 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005587 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005588 return NULL;
5589 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005590
5591 scope = new_scope(cctx, IF_SCOPE);
5592 if (scope == NULL)
5593 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005594 scope->se_skip_save = skip_save;
5595 // "is_had_return" will be reset if any block does not end in :return
5596 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005597
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005598 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005599 {
5600 // "where" is set when ":elseif", "else" or ":endif" is found
5601 scope->se_u.se_if.is_if_label = instr->ga_len;
5602 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5603 }
5604 else
5605 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005606
5607 return p;
5608}
5609
5610 static char_u *
5611compile_elseif(char_u *arg, cctx_T *cctx)
5612{
5613 char_u *p = arg;
5614 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005615 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005616 isn_T *isn;
5617 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005618 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02005619 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005620
5621 if (scope == NULL || scope->se_type != IF_SCOPE)
5622 {
5623 emsg(_(e_elseif_without_if));
5624 return NULL;
5625 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005626 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005627 if (!cctx->ctx_had_return)
5628 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005629
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005630 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005631 {
5632 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005633 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005634 return NULL;
5635 // previous "if" or "elseif" jumps here
5636 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5637 isn->isn_arg.jump.jump_where = instr->ga_len;
5638 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005639
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005640 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005641 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02005642 if (cctx->ctx_skip == SKIP_YES)
5643 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005644 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005645 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005646 clear_ppconst(&ppconst);
5647 return NULL;
5648 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02005649 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005650 if (scope->se_skip_save == SKIP_YES)
5651 clear_ppconst(&ppconst);
5652 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005653 {
5654 // The expression results in a constant.
5655 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005656 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005657 clear_ppconst(&ppconst);
5658 scope->se_u.se_if.is_if_label = -1;
5659 }
5660 else
5661 {
5662 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005663 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005664 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005665 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005666
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005667 // "where" is set when ":elseif", "else" or ":endif" is found
5668 scope->se_u.se_if.is_if_label = instr->ga_len;
5669 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5670 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005671
5672 return p;
5673}
5674
5675 static char_u *
5676compile_else(char_u *arg, cctx_T *cctx)
5677{
5678 char_u *p = arg;
5679 garray_T *instr = &cctx->ctx_instr;
5680 isn_T *isn;
5681 scope_T *scope = cctx->ctx_scope;
5682
5683 if (scope == NULL || scope->se_type != IF_SCOPE)
5684 {
5685 emsg(_(e_else_without_if));
5686 return NULL;
5687 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005688 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005689 if (!cctx->ctx_had_return)
5690 scope->se_u.se_if.is_had_return = FALSE;
5691 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005692
Bram Moolenaarefd88552020-06-18 20:50:10 +02005693 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005694 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005695 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005696 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005697 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005698 if (!cctx->ctx_had_return
5699 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5700 JUMP_ALWAYS, cctx) == FAIL)
5701 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005702 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005703
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005704 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005705 {
5706 if (scope->se_u.se_if.is_if_label >= 0)
5707 {
5708 // previous "if" or "elseif" jumps here
5709 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5710 isn->isn_arg.jump.jump_where = instr->ga_len;
5711 scope->se_u.se_if.is_if_label = -1;
5712 }
5713 }
5714
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005715 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005716 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5717 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005718
5719 return p;
5720}
5721
5722 static char_u *
5723compile_endif(char_u *arg, cctx_T *cctx)
5724{
5725 scope_T *scope = cctx->ctx_scope;
5726 ifscope_T *ifscope;
5727 garray_T *instr = &cctx->ctx_instr;
5728 isn_T *isn;
5729
5730 if (scope == NULL || scope->se_type != IF_SCOPE)
5731 {
5732 emsg(_(e_endif_without_if));
5733 return NULL;
5734 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005735 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005736 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005737 if (!cctx->ctx_had_return)
5738 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005739
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005740 if (scope->se_u.se_if.is_if_label >= 0)
5741 {
5742 // previous "if" or "elseif" jumps here
5743 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5744 isn->isn_arg.jump.jump_where = instr->ga_len;
5745 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005746 // Fill in the "end" label in jumps at the end of the blocks.
5747 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005748 cctx->ctx_skip = scope->se_skip_save;
5749
5750 // If all the blocks end in :return and there is an :else then the
5751 // had_return flag is set.
5752 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005753
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005754 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005755 return arg;
5756}
5757
5758/*
5759 * compile "for var in expr"
5760 *
5761 * Produces instructions:
5762 * PUSHNR -1
5763 * STORE loop-idx Set index to -1
5764 * EVAL expr Push result of "expr"
5765 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5766 * - if beyond end, jump to "end"
5767 * - otherwise get item from list and push it
5768 * STORE var Store item in "var"
5769 * ... body ...
5770 * JUMP top Jump back to repeat
5771 * end: DROP Drop the result of "expr"
5772 *
5773 */
5774 static char_u *
5775compile_for(char_u *arg, cctx_T *cctx)
5776{
5777 char_u *p;
5778 size_t varlen;
5779 garray_T *instr = &cctx->ctx_instr;
5780 garray_T *stack = &cctx->ctx_type_stack;
5781 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005782 lvar_T *loop_lvar; // loop iteration variable
5783 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005784 type_T *vartype;
5785
5786 // TODO: list of variables: "for [key, value] in dict"
5787 // parse "var"
5788 for (p = arg; eval_isnamec1(*p); ++p)
5789 ;
5790 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005791 var_lvar = lookup_local(arg, varlen, cctx);
5792 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005793 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005794 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005795 return NULL;
5796 }
5797
5798 // consume "in"
5799 p = skipwhite(p);
5800 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5801 {
5802 emsg(_(e_missing_in));
5803 return NULL;
5804 }
5805 p = skipwhite(p + 2);
5806
5807
5808 scope = new_scope(cctx, FOR_SCOPE);
5809 if (scope == NULL)
5810 return NULL;
5811
5812 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005813 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5814 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005815 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005816 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005817 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005818 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005819 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005820
5821 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005822 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5823 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005824 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005825 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005826 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005827 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005828 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005829
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005830 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005831
5832 // compile "expr", it remains on the stack until "endfor"
5833 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005834 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005835 {
5836 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
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005840 // Now that we know the type of "var", check that it is a list, now or at
5841 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005842 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005843 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005844 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005845 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005846 return NULL;
5847 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005848 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005849 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005850
5851 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005852 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005853
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005854 generate_FOR(cctx, loop_lvar->lv_idx);
5855 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005856
5857 return arg;
5858}
5859
5860/*
5861 * compile "endfor"
5862 */
5863 static char_u *
5864compile_endfor(char_u *arg, cctx_T *cctx)
5865{
5866 garray_T *instr = &cctx->ctx_instr;
5867 scope_T *scope = cctx->ctx_scope;
5868 forscope_T *forscope;
5869 isn_T *isn;
5870
5871 if (scope == NULL || scope->se_type != FOR_SCOPE)
5872 {
5873 emsg(_(e_for));
5874 return NULL;
5875 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005876 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005877 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005878 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005879
5880 // At end of ":for" scope jump back to the FOR instruction.
5881 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5882
5883 // Fill in the "end" label in the FOR statement so it can jump here
5884 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5885 isn->isn_arg.forloop.for_end = instr->ga_len;
5886
5887 // Fill in the "end" label any BREAK statements
5888 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5889
5890 // Below the ":for" scope drop the "expr" list from the stack.
5891 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5892 return NULL;
5893
5894 vim_free(scope);
5895
5896 return arg;
5897}
5898
5899/*
5900 * compile "while expr"
5901 *
5902 * Produces instructions:
5903 * top: EVAL expr Push result of "expr"
5904 * JUMP_IF_FALSE end jump if false
5905 * ... body ...
5906 * JUMP top Jump back to repeat
5907 * end:
5908 *
5909 */
5910 static char_u *
5911compile_while(char_u *arg, cctx_T *cctx)
5912{
5913 char_u *p = arg;
5914 garray_T *instr = &cctx->ctx_instr;
5915 scope_T *scope;
5916
5917 scope = new_scope(cctx, WHILE_SCOPE);
5918 if (scope == NULL)
5919 return NULL;
5920
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005921 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005922
5923 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005924 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005925 return NULL;
5926
5927 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005928 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005929 JUMP_IF_FALSE, cctx) == FAIL)
5930 return FAIL;
5931
5932 return p;
5933}
5934
5935/*
5936 * compile "endwhile"
5937 */
5938 static char_u *
5939compile_endwhile(char_u *arg, cctx_T *cctx)
5940{
5941 scope_T *scope = cctx->ctx_scope;
5942
5943 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5944 {
5945 emsg(_(e_while));
5946 return NULL;
5947 }
5948 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005949 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005950
5951 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005952 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005953
5954 // Fill in the "end" label in the WHILE statement so it can jump here.
5955 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005956 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005957
5958 vim_free(scope);
5959
5960 return arg;
5961}
5962
5963/*
5964 * compile "continue"
5965 */
5966 static char_u *
5967compile_continue(char_u *arg, cctx_T *cctx)
5968{
5969 scope_T *scope = cctx->ctx_scope;
5970
5971 for (;;)
5972 {
5973 if (scope == NULL)
5974 {
5975 emsg(_(e_continue));
5976 return NULL;
5977 }
5978 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5979 break;
5980 scope = scope->se_outer;
5981 }
5982
5983 // Jump back to the FOR or WHILE instruction.
5984 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005985 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5986 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005987 return arg;
5988}
5989
5990/*
5991 * compile "break"
5992 */
5993 static char_u *
5994compile_break(char_u *arg, cctx_T *cctx)
5995{
5996 scope_T *scope = cctx->ctx_scope;
5997 endlabel_T **el;
5998
5999 for (;;)
6000 {
6001 if (scope == NULL)
6002 {
6003 emsg(_(e_break));
6004 return NULL;
6005 }
6006 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6007 break;
6008 scope = scope->se_outer;
6009 }
6010
6011 // Jump to the end of the FOR or WHILE loop.
6012 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006013 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006014 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006015 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006016 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6017 return FAIL;
6018
6019 return arg;
6020}
6021
6022/*
6023 * compile "{" start of block
6024 */
6025 static char_u *
6026compile_block(char_u *arg, cctx_T *cctx)
6027{
6028 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6029 return NULL;
6030 return skipwhite(arg + 1);
6031}
6032
6033/*
6034 * compile end of block: drop one scope
6035 */
6036 static void
6037compile_endblock(cctx_T *cctx)
6038{
6039 scope_T *scope = cctx->ctx_scope;
6040
6041 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006042 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006043 vim_free(scope);
6044}
6045
6046/*
6047 * compile "try"
6048 * Creates a new scope for the try-endtry, pointing to the first catch and
6049 * finally.
6050 * Creates another scope for the "try" block itself.
6051 * TRY instruction sets up exception handling at runtime.
6052 *
6053 * "try"
6054 * TRY -> catch1, -> finally push trystack entry
6055 * ... try block
6056 * "throw {exception}"
6057 * EVAL {exception}
6058 * THROW create exception
6059 * ... try block
6060 * " catch {expr}"
6061 * JUMP -> finally
6062 * catch1: PUSH exeception
6063 * EVAL {expr}
6064 * MATCH
6065 * JUMP nomatch -> catch2
6066 * CATCH remove exception
6067 * ... catch block
6068 * " catch"
6069 * JUMP -> finally
6070 * catch2: CATCH remove exception
6071 * ... catch block
6072 * " finally"
6073 * finally:
6074 * ... finally block
6075 * " endtry"
6076 * ENDTRY pop trystack entry, may rethrow
6077 */
6078 static char_u *
6079compile_try(char_u *arg, cctx_T *cctx)
6080{
6081 garray_T *instr = &cctx->ctx_instr;
6082 scope_T *try_scope;
6083 scope_T *scope;
6084
6085 // scope that holds the jumps that go to catch/finally/endtry
6086 try_scope = new_scope(cctx, TRY_SCOPE);
6087 if (try_scope == NULL)
6088 return NULL;
6089
6090 // "catch" is set when the first ":catch" is found.
6091 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006092 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006093 if (generate_instr(cctx, ISN_TRY) == NULL)
6094 return NULL;
6095
6096 // scope for the try block itself
6097 scope = new_scope(cctx, BLOCK_SCOPE);
6098 if (scope == NULL)
6099 return NULL;
6100
6101 return arg;
6102}
6103
6104/*
6105 * compile "catch {expr}"
6106 */
6107 static char_u *
6108compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6109{
6110 scope_T *scope = cctx->ctx_scope;
6111 garray_T *instr = &cctx->ctx_instr;
6112 char_u *p;
6113 isn_T *isn;
6114
6115 // end block scope from :try or :catch
6116 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6117 compile_endblock(cctx);
6118 scope = cctx->ctx_scope;
6119
6120 // Error if not in a :try scope
6121 if (scope == NULL || scope->se_type != TRY_SCOPE)
6122 {
6123 emsg(_(e_catch));
6124 return NULL;
6125 }
6126
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006127 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006128 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006129 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006130 return NULL;
6131 }
6132
6133 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006134 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006135 JUMP_ALWAYS, cctx) == FAIL)
6136 return NULL;
6137
6138 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006139 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006140 if (isn->isn_arg.try.try_catch == 0)
6141 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006142 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006143 {
6144 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006145 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006146 isn->isn_arg.jump.jump_where = instr->ga_len;
6147 }
6148
6149 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006150 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006151 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006152 scope->se_u.se_try.ts_caught_all = TRUE;
6153 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006154 }
6155 else
6156 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006157 char_u *end;
6158 char_u *pat;
6159 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006160 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006161 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006162
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006163 // Push v:exception, push {expr} and MATCH
6164 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6165
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006166 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006167 if (*end != *p)
6168 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006169 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006170 vim_free(tofree);
6171 return FAIL;
6172 }
6173 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006174 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006175 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006176 len = (int)(end - tofree);
6177 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006178 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006179 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006180 if (pat == NULL)
6181 return FAIL;
6182 if (generate_PUSHS(cctx, pat) == FAIL)
6183 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006184
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006185 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6186 return NULL;
6187
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006188 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006189 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6190 return NULL;
6191 }
6192
6193 if (generate_instr(cctx, ISN_CATCH) == NULL)
6194 return NULL;
6195
6196 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6197 return NULL;
6198 return p;
6199}
6200
6201 static char_u *
6202compile_finally(char_u *arg, cctx_T *cctx)
6203{
6204 scope_T *scope = cctx->ctx_scope;
6205 garray_T *instr = &cctx->ctx_instr;
6206 isn_T *isn;
6207
6208 // end block scope from :try or :catch
6209 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6210 compile_endblock(cctx);
6211 scope = cctx->ctx_scope;
6212
6213 // Error if not in a :try scope
6214 if (scope == NULL || scope->se_type != TRY_SCOPE)
6215 {
6216 emsg(_(e_finally));
6217 return NULL;
6218 }
6219
6220 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006221 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222 if (isn->isn_arg.try.try_finally != 0)
6223 {
6224 emsg(_(e_finally_dup));
6225 return NULL;
6226 }
6227
6228 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006229 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006230
Bram Moolenaar585fea72020-04-02 22:33:21 +02006231 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006232 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006233 {
6234 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006235 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006236 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006237 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006238 }
6239
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006240 // TODO: set index in ts_finally_label jumps
6241
6242 return arg;
6243}
6244
6245 static char_u *
6246compile_endtry(char_u *arg, cctx_T *cctx)
6247{
6248 scope_T *scope = cctx->ctx_scope;
6249 garray_T *instr = &cctx->ctx_instr;
6250 isn_T *isn;
6251
6252 // end block scope from :catch or :finally
6253 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6254 compile_endblock(cctx);
6255 scope = cctx->ctx_scope;
6256
6257 // Error if not in a :try scope
6258 if (scope == NULL || scope->se_type != TRY_SCOPE)
6259 {
6260 if (scope == NULL)
6261 emsg(_(e_no_endtry));
6262 else if (scope->se_type == WHILE_SCOPE)
6263 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006264 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006265 emsg(_(e_endfor));
6266 else
6267 emsg(_(e_endif));
6268 return NULL;
6269 }
6270
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006271 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006272 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6273 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006274 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006275 return NULL;
6276 }
6277
6278 // Fill in the "end" label in jumps at the end of the blocks, if not done
6279 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006280 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006281
6282 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006283 if (isn->isn_arg.try.try_catch == 0)
6284 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006285 if (isn->isn_arg.try.try_finally == 0)
6286 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006287
6288 if (scope->se_u.se_try.ts_catch_label != 0)
6289 {
6290 // Last catch without match jumps here
6291 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6292 isn->isn_arg.jump.jump_where = instr->ga_len;
6293 }
6294
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006295 compile_endblock(cctx);
6296
6297 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6298 return NULL;
6299 return arg;
6300}
6301
6302/*
6303 * compile "throw {expr}"
6304 */
6305 static char_u *
6306compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6307{
6308 char_u *p = skipwhite(arg);
6309
Bram Moolenaara5565e42020-05-09 15:44:01 +02006310 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006311 return NULL;
6312 if (may_generate_2STRING(-1, cctx) == FAIL)
6313 return NULL;
6314 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6315 return NULL;
6316
6317 return p;
6318}
6319
6320/*
6321 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006322 * compile "echomsg expr"
6323 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006324 * compile "execute expr"
6325 */
6326 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006327compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006328{
6329 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006330 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006331 int count = 0;
6332
6333 for (;;)
6334 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006335 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006336 return NULL;
6337 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006338 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006339 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006340 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006341 break;
6342 }
6343
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006344 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6345 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6346 else if (cmdidx == CMD_execute)
6347 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6348 else if (cmdidx == CMD_echomsg)
6349 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6350 else
6351 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006352 return p;
6353}
6354
6355/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006356 * :put r
6357 * :put ={expr}
6358 */
6359 static char_u *
6360compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6361{
6362 char_u *line = arg;
6363 linenr_T lnum;
6364 char *errormsg;
6365 int above = FALSE;
6366
6367 if (*arg == '!')
6368 {
6369 above = TRUE;
6370 line = skipwhite(arg + 1);
6371 }
6372 eap->regname = *line;
6373
6374 if (eap->regname == '=')
6375 {
6376 char_u *p = line + 1;
6377
6378 if (compile_expr0(&p, cctx) == FAIL)
6379 return NULL;
6380 line = p;
6381 }
6382 else if (eap->regname != NUL)
6383 ++line;
6384
6385 // TODO: if the range is something like "$" need to evaluate at runtime
6386 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6387 return NULL;
6388 if (eap->addr_count == 0)
6389 lnum = -1;
6390 else
6391 lnum = eap->line2;
6392 if (above)
6393 --lnum;
6394
6395 generate_PUT(cctx, eap->regname, lnum);
6396 return line;
6397}
6398
6399/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006400 * A command that is not compiled, execute with legacy code.
6401 */
6402 static char_u *
6403compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6404{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006405 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006406 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006407 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006408
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006409 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006410 goto theend;
6411
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006412 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006413 {
6414 long argt = excmd_get_argt(eap->cmdidx);
6415 int usefilter = FALSE;
6416
6417 has_expr = argt & (EX_XFILE | EX_EXPAND);
6418
6419 // If the command can be followed by a bar, find the bar and truncate
6420 // it, so that the following command can be compiled.
6421 // The '|' is overwritten with a NUL, it is put back below.
6422 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6423 && *eap->arg == '!')
6424 // :w !filter or :r !filter or :r! filter
6425 usefilter = TRUE;
6426 if ((argt & EX_TRLBAR) && !usefilter)
6427 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006428 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006429 separate_nextcmd(eap);
6430 if (eap->nextcmd != NULL)
6431 nextcmd = eap->nextcmd;
6432 }
6433 }
6434
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006435 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6436 {
6437 // expand filename in "syntax include [@group] filename"
6438 has_expr = TRUE;
6439 eap->arg = skipwhite(eap->arg + 7);
6440 if (*eap->arg == '@')
6441 eap->arg = skiptowhite(eap->arg);
6442 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006443
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006444 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006445 {
6446 int count = 0;
6447 char_u *start = skipwhite(line);
6448
6449 // :cmd xxx`=expr1`yyy`=expr2`zzz
6450 // PUSHS ":cmd xxx"
6451 // eval expr1
6452 // PUSHS "yyy"
6453 // eval expr2
6454 // PUSHS "zzz"
6455 // EXECCONCAT 5
6456 for (;;)
6457 {
6458 if (p > start)
6459 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006460 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006461 ++count;
6462 }
6463 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006464 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006465 return NULL;
6466 may_generate_2STRING(-1, cctx);
6467 ++count;
6468 p = skipwhite(p);
6469 if (*p != '`')
6470 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006471 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006472 return NULL;
6473 }
6474 start = p + 1;
6475
6476 p = (char_u *)strstr((char *)start, "`=");
6477 if (p == NULL)
6478 {
6479 if (*skipwhite(start) != NUL)
6480 {
6481 generate_PUSHS(cctx, vim_strsave(start));
6482 ++count;
6483 }
6484 break;
6485 }
6486 }
6487 generate_EXECCONCAT(cctx, count);
6488 }
6489 else
6490 generate_EXEC(cctx, line);
6491
6492theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006493 if (*nextcmd != NUL)
6494 {
6495 // the parser expects a pointer to the bar, put it back
6496 --nextcmd;
6497 *nextcmd = '|';
6498 }
6499
6500 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006501}
6502
6503/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006504 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006505 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006506 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006507 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006508add_def_function(ufunc_T *ufunc)
6509{
6510 dfunc_T *dfunc;
6511
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006512 if (def_functions.ga_len == 0)
6513 {
6514 // The first position is not used, so that a zero uf_dfunc_idx means it
6515 // wasn't set.
6516 if (ga_grow(&def_functions, 1) == FAIL)
6517 return FAIL;
6518 ++def_functions.ga_len;
6519 }
6520
Bram Moolenaar09689a02020-05-09 22:50:08 +02006521 // Add the function to "def_functions".
6522 if (ga_grow(&def_functions, 1) == FAIL)
6523 return FAIL;
6524 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6525 CLEAR_POINTER(dfunc);
6526 dfunc->df_idx = def_functions.ga_len;
6527 ufunc->uf_dfunc_idx = dfunc->df_idx;
6528 dfunc->df_ufunc = ufunc;
6529 ++def_functions.ga_len;
6530 return OK;
6531}
6532
6533/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006534 * After ex_function() has collected all the function lines: parse and compile
6535 * the lines into instructions.
6536 * Adds the function to "def_functions".
6537 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6538 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006539 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006540 * This can be used recursively through compile_lambda(), which may reallocate
6541 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006542 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006543 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006544 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006545compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006546{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006547 char_u *line = NULL;
6548 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006549 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006550 cctx_T cctx;
6551 garray_T *instr;
6552 int called_emsg_before = called_emsg;
6553 int ret = FAIL;
6554 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006555 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006556 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006557 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006558
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006559 // When using a function that was compiled before: Free old instructions.
6560 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006561 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006562 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006563 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6564 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006565 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006566 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006567 else
6568 {
6569 if (add_def_function(ufunc) == FAIL)
6570 return FAIL;
6571 new_def_function = TRUE;
6572 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006573
Bram Moolenaar985116a2020-07-12 17:31:09 +02006574 ufunc->uf_def_status = UF_COMPILING;
6575
Bram Moolenaara80faa82020-04-12 19:37:17 +02006576 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006577 cctx.ctx_ufunc = ufunc;
6578 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006579 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006580 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6581 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6582 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6583 cctx.ctx_type_list = &ufunc->uf_type_list;
6584 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6585 instr = &cctx.ctx_instr;
6586
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006587 // Set the context to the function, it may be compiled when called from
6588 // another script. Set the script version to the most modern one.
6589 // The line number will be set in next_line_from_context().
6590 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006591 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6592
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006593 // Make sure error messages are OK.
6594 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6595 if (do_estack_push)
6596 estack_push_ufunc(ufunc, 1);
6597
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006598 if (ufunc->uf_def_args.ga_len > 0)
6599 {
6600 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006601 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006602 int i;
6603 char_u *arg;
6604 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006605 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006606
6607 // Produce instructions for the default values of optional arguments.
6608 // Store the instruction index in uf_def_arg_idx[] so that we know
6609 // where to start when the function is called, depending on the number
6610 // of arguments.
6611 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6612 if (ufunc->uf_def_arg_idx == NULL)
6613 goto erret;
6614 for (i = 0; i < count; ++i)
6615 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006616 garray_T *stack = &cctx.ctx_type_stack;
6617 type_T *val_type;
6618 int arg_idx = first_def_arg + i;
6619
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006620 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6621 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006622 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006623 goto erret;
6624
6625 // If no type specified use the type of the default value.
6626 // Otherwise check that the default value type matches the
6627 // specified type.
6628 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6629 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006630 {
6631 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006632 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006633 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02006634 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
6635 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006636 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006637
6638 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006639 goto erret;
6640 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006641 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006642
6643 if (did_set_arg_type)
6644 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006645 }
6646
6647 /*
6648 * Loop over all the lines of the function and generate instructions.
6649 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006650 for (;;)
6651 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006652 exarg_T ea;
6653 cmdmod_T save_cmdmod;
6654 int starts_with_colon = FALSE;
6655 char_u *cmd;
6656 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006657
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006658 // Bail out on the first error to avoid a flood of errors and report
6659 // the right line number when inside try/catch.
6660 if (emsg_before != called_emsg)
6661 goto erret;
6662
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006663 if (line != NULL && *line == '|')
6664 // the line continues after a '|'
6665 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006666 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006667 && !(*line == '#' && (line == cctx.ctx_line_start
6668 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006669 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006670 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006671 goto erret;
6672 }
6673 else
6674 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006675 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006676 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006677 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006678 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006679 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006680 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006681
Bram Moolenaara80faa82020-04-12 19:37:17 +02006682 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006683 ea.cmdlinep = &line;
6684 ea.cmd = skipwhite(line);
6685
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006686 // Some things can be recognized by the first character.
6687 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006688 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006689 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006690 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006691 if (ea.cmd[1] != '{')
6692 {
6693 line = (char_u *)"";
6694 continue;
6695 }
6696 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006697
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006698 case '}':
6699 {
6700 // "}" ends a block scope
6701 scopetype_T stype = cctx.ctx_scope == NULL
6702 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006703
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006704 if (stype == BLOCK_SCOPE)
6705 {
6706 compile_endblock(&cctx);
6707 line = ea.cmd;
6708 }
6709 else
6710 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006711 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006712 goto erret;
6713 }
6714 if (line != NULL)
6715 line = skipwhite(ea.cmd + 1);
6716 continue;
6717 }
6718
6719 case '{':
6720 // "{" starts a block scope
6721 // "{'a': 1}->func() is something else
6722 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6723 {
6724 line = compile_block(ea.cmd, &cctx);
6725 continue;
6726 }
6727 break;
6728
6729 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006730 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006731 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006732 }
6733
6734 /*
6735 * COMMAND MODIFIERS
6736 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006737 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006738 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6739 {
6740 if (errormsg != NULL)
6741 goto erret;
6742 // empty line or comment
6743 line = (char_u *)"";
6744 continue;
6745 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006746 // TODO: use modifiers in the command
6747 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006748 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006749
6750 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006751 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006752 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006753 {
6754 if (*ea.cmd == '(')
6755 // not for "call()"
6756 ea.cmd = p;
6757 else
6758 ea.cmd = skipwhite(ea.cmd);
6759 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006760
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006761 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006762 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006763 char_u *pskip;
6764
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006765 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006766 // find what follows.
6767 // Skip over "var.member", "var[idx]" and the like.
6768 // Also "&opt = val", "$ENV = val" and "@r = val".
6769 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006770 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006771 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006772 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006773 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006774 char_u *var_end;
6775 int oplen;
6776 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006777
Bram Moolenaar65821722020-08-02 18:58:54 +02006778 if (ea.cmd[0] == '@')
6779 var_end = ea.cmd + 2;
6780 else
6781 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006782 FNE_CHECK_START | FNE_INCL_BR);
6783 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006784 if (oplen > 0)
6785 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006786 size_t len = p - ea.cmd;
6787
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006788 // Recognize an assignment if we recognize the variable
6789 // name:
6790 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006791 // "local = expr" where "local" is a local var.
6792 // "script = expr" where "script" is a script-local var.
6793 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006794 // "&opt = expr"
6795 // "$ENV = expr"
6796 // "@r = expr"
6797 if (*ea.cmd == '&'
6798 || *ea.cmd == '$'
6799 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006800 || ((len) > 2 && ea.cmd[1] == ':')
6801 || lookup_local(ea.cmd, len, &cctx) != NULL
6802 || lookup_arg(ea.cmd, len, NULL, NULL,
6803 NULL, &cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02006804 || lookup_script(ea.cmd, len, FALSE) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006805 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006806 {
6807 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006808 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006809 goto erret;
6810 continue;
6811 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006812 }
6813 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006814
6815 if (*ea.cmd == '[')
6816 {
6817 // [var, var] = expr
6818 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6819 if (line == NULL)
6820 goto erret;
6821 if (line != ea.cmd)
6822 continue;
6823 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006824 }
6825
6826 /*
6827 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006828 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006829 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006830 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006831 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006832 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006833 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006834 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006835 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006836 if (!starts_with_colon)
6837 {
6838 emsg(_(e_colon_required_before_a_range));
6839 goto erret;
6840 }
6841 if (ends_excmd2(line, ea.cmd))
6842 {
6843 // A range without a command: jump to the line.
6844 // TODO: compile to a more efficient command, possibly
6845 // calling parse_cmd_address().
6846 ea.cmdidx = CMD_SIZE;
6847 line = compile_exec(line, &ea, &cctx);
6848 goto nextline;
6849 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006850 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006851 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006852 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006853 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006854 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006855
6856 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6857 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006858 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006859 {
6860 line += STRLEN(line);
6861 continue;
6862 }
6863
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006864 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006865 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006866 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006867 // CMD_let cannot happen, compile_assignment() above is used
6868 iemsg("Command from find_ex_command() not handled");
6869 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006870 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006871 }
6872
6873 p = skipwhite(p);
6874
Bram Moolenaar3988f642020-08-27 22:43:03 +02006875 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006876 && ea.cmdidx != CMD_elseif
6877 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02006878 && ea.cmdidx != CMD_endif
6879 && ea.cmdidx != CMD_endfor
6880 && ea.cmdidx != CMD_endwhile
6881 && ea.cmdidx != CMD_catch
6882 && ea.cmdidx != CMD_finally
6883 && ea.cmdidx != CMD_endtry)
6884 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02006885 emsg(_(e_unreachable_code_after_return));
6886 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006887 }
6888
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006889 switch (ea.cmdidx)
6890 {
6891 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006892 ea.arg = p;
6893 line = compile_nested_function(&ea, &cctx);
6894 break;
6895
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006896 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006897 // TODO: should we allow this, e.g. to declare a global
6898 // function?
6899 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006900 goto erret;
6901
6902 case CMD_return:
6903 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006904 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006905 break;
6906
6907 case CMD_let:
6908 case CMD_const:
6909 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006910 if (line == p)
6911 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006912 break;
6913
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006914 case CMD_unlet:
6915 case CMD_unlockvar:
6916 case CMD_lockvar:
6917 line = compile_unletlock(p, &ea, &cctx);
6918 break;
6919
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006920 case CMD_import:
6921 line = compile_import(p, &cctx);
6922 break;
6923
6924 case CMD_if:
6925 line = compile_if(p, &cctx);
6926 break;
6927 case CMD_elseif:
6928 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006929 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006930 break;
6931 case CMD_else:
6932 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006933 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006934 break;
6935 case CMD_endif:
6936 line = compile_endif(p, &cctx);
6937 break;
6938
6939 case CMD_while:
6940 line = compile_while(p, &cctx);
6941 break;
6942 case CMD_endwhile:
6943 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006944 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006945 break;
6946
6947 case CMD_for:
6948 line = compile_for(p, &cctx);
6949 break;
6950 case CMD_endfor:
6951 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006952 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006953 break;
6954 case CMD_continue:
6955 line = compile_continue(p, &cctx);
6956 break;
6957 case CMD_break:
6958 line = compile_break(p, &cctx);
6959 break;
6960
6961 case CMD_try:
6962 line = compile_try(p, &cctx);
6963 break;
6964 case CMD_catch:
6965 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006966 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006967 break;
6968 case CMD_finally:
6969 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006970 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006971 break;
6972 case CMD_endtry:
6973 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006974 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006975 break;
6976 case CMD_throw:
6977 line = compile_throw(p, &cctx);
6978 break;
6979
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006980 case CMD_eval:
6981 if (compile_expr0(&p, &cctx) == FAIL)
6982 goto erret;
6983
Bram Moolenaar3988f642020-08-27 22:43:03 +02006984 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006985 generate_instr_drop(&cctx, ISN_DROP, 1);
6986
6987 line = skipwhite(p);
6988 break;
6989
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006990 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006991 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006992 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006993 case CMD_echomsg:
6994 case CMD_echoerr:
6995 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006996 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006997
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006998 case CMD_put:
6999 ea.cmd = cmd;
7000 line = compile_put(p, &ea, &cctx);
7001 break;
7002
Bram Moolenaar3988f642020-08-27 22:43:03 +02007003 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007004
Bram Moolenaarae616492020-07-28 20:07:27 +02007005 case CMD_append:
7006 case CMD_change:
7007 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007008 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007009 case CMD_xit:
7010 not_in_vim9(&ea);
7011 goto erret;
7012
Bram Moolenaar002262f2020-07-08 17:47:57 +02007013 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007014 if (cctx.ctx_skip != SKIP_YES)
7015 {
7016 semsg(_(e_invalid_command_str), ea.cmd);
7017 goto erret;
7018 }
7019 // We don't check for a next command here.
7020 line = (char_u *)"";
7021 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007022
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007023 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007024 if (cctx.ctx_skip == SKIP_YES)
7025 {
7026 // We don't check for a next command here.
7027 line = (char_u *)"";
7028 }
7029 else
7030 {
7031 // Not recognized, execute with do_cmdline_cmd().
7032 ea.arg = p;
7033 line = compile_exec(line, &ea, &cctx);
7034 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007035 break;
7036 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007037nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007038 if (line == NULL)
7039 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007040 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007041
7042 if (cctx.ctx_type_stack.ga_len < 0)
7043 {
7044 iemsg("Type stack underflow");
7045 goto erret;
7046 }
7047 }
7048
7049 if (cctx.ctx_scope != NULL)
7050 {
7051 if (cctx.ctx_scope->se_type == IF_SCOPE)
7052 emsg(_(e_endif));
7053 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7054 emsg(_(e_endwhile));
7055 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7056 emsg(_(e_endfor));
7057 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007058 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007059 goto erret;
7060 }
7061
Bram Moolenaarefd88552020-06-18 20:50:10 +02007062 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007063 {
7064 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7065 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007066 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007067 goto erret;
7068 }
7069
7070 // Return zero if there is no return at the end.
7071 generate_PUSHNR(&cctx, 0);
7072 generate_instr(&cctx, ISN_RETURN);
7073 }
7074
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007075 {
7076 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7077 + ufunc->uf_dfunc_idx;
7078 dfunc->df_deleted = FALSE;
7079 dfunc->df_instr = instr->ga_data;
7080 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007081 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007082 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007083 if (cctx.ctx_outer_used)
7084 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007085 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007086 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007087
7088 ret = OK;
7089
7090erret:
7091 if (ret == FAIL)
7092 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007093 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007094 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7095 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007096
7097 for (idx = 0; idx < instr->ga_len; ++idx)
7098 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007099 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007100
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007101 // If using the last entry in the table and it was added above, we
7102 // might as well remove it.
7103 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007104 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007105 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007106 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007107 ufunc->uf_dfunc_idx = 0;
7108 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007109 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007110
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007111 while (cctx.ctx_scope != NULL)
7112 drop_scope(&cctx);
7113
Bram Moolenaar20431c92020-03-20 18:39:46 +01007114 // Don't execute this function body.
7115 ga_clear_strings(&ufunc->uf_lines);
7116
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007117 if (errormsg != NULL)
7118 emsg(errormsg);
7119 else if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007120 emsg(_(e_compile_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007121 }
7122
7123 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007124 if (do_estack_push)
7125 estack_pop();
7126
Bram Moolenaar20431c92020-03-20 18:39:46 +01007127 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007128 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007129 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007130 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007131}
7132
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007133 void
7134set_function_type(ufunc_T *ufunc)
7135{
7136 int varargs = ufunc->uf_va_name != NULL;
7137 int argcount = ufunc->uf_args.ga_len;
7138
7139 // Create a type for the function, with the return type and any
7140 // argument types.
7141 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7142 // The type is included in "tt_args".
7143 if (argcount > 0 || varargs)
7144 {
7145 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7146 argcount, &ufunc->uf_type_list);
7147 // Add argument types to the function type.
7148 if (func_type_add_arg_types(ufunc->uf_func_type,
7149 argcount + varargs,
7150 &ufunc->uf_type_list) == FAIL)
7151 return;
7152 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7153 ufunc->uf_func_type->tt_min_argcount =
7154 argcount - ufunc->uf_def_args.ga_len;
7155 if (ufunc->uf_arg_types == NULL)
7156 {
7157 int i;
7158
7159 // lambda does not have argument types.
7160 for (i = 0; i < argcount; ++i)
7161 ufunc->uf_func_type->tt_args[i] = &t_any;
7162 }
7163 else
7164 mch_memmove(ufunc->uf_func_type->tt_args,
7165 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7166 if (varargs)
7167 {
7168 ufunc->uf_func_type->tt_args[argcount] =
7169 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7170 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7171 }
7172 }
7173 else
7174 // No arguments, can use a predefined type.
7175 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7176 argcount, &ufunc->uf_type_list);
7177}
7178
7179
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007180/*
7181 * Delete an instruction, free what it contains.
7182 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007183 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007184delete_instr(isn_T *isn)
7185{
7186 switch (isn->isn_type)
7187 {
7188 case ISN_EXEC:
7189 case ISN_LOADENV:
7190 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007191 case ISN_LOADB:
7192 case ISN_LOADW:
7193 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007194 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007195 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007196 case ISN_PUSHEXC:
7197 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007198 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007199 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007200 case ISN_STOREB:
7201 case ISN_STOREW:
7202 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007203 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007204 vim_free(isn->isn_arg.string);
7205 break;
7206
7207 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007208 case ISN_STORES:
7209 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007210 break;
7211
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007212 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007213 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007214 vim_free(isn->isn_arg.unlet.ul_name);
7215 break;
7216
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007217 case ISN_STOREOPT:
7218 vim_free(isn->isn_arg.storeopt.so_name);
7219 break;
7220
7221 case ISN_PUSHBLOB: // push blob isn_arg.blob
7222 blob_unref(isn->isn_arg.blob);
7223 break;
7224
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007225 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007226#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007227 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007228#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007229 break;
7230
7231 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007232#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007233 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007234#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007235 break;
7236
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007237 case ISN_UCALL:
7238 vim_free(isn->isn_arg.ufunc.cuf_name);
7239 break;
7240
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007241 case ISN_FUNCREF:
7242 {
7243 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7244 + isn->isn_arg.funcref.fr_func;
7245 func_ptr_unref(dfunc->df_ufunc);
7246 }
7247 break;
7248
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007249 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007250 {
7251 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7252 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7253
7254 if (ufunc != NULL)
7255 {
7256 // Clear uf_dfunc_idx so that the function is deleted.
7257 clear_def_function(ufunc);
7258 ufunc->uf_dfunc_idx = 0;
7259 func_ptr_unref(ufunc);
7260 }
7261
7262 vim_free(lambda);
7263 vim_free(isn->isn_arg.newfunc.nf_global);
7264 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007265 break;
7266
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007267 case ISN_2BOOL:
7268 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007269 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007270 case ISN_ADDBLOB:
7271 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007272 case ISN_ANYINDEX:
7273 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007274 case ISN_BCALL:
7275 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007276 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007277 case ISN_CHECKNR:
7278 case ISN_CHECKTYPE:
7279 case ISN_COMPAREANY:
7280 case ISN_COMPAREBLOB:
7281 case ISN_COMPAREBOOL:
7282 case ISN_COMPAREDICT:
7283 case ISN_COMPAREFLOAT:
7284 case ISN_COMPAREFUNC:
7285 case ISN_COMPARELIST:
7286 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007287 case ISN_COMPARESPECIAL:
7288 case ISN_COMPARESTRING:
7289 case ISN_CONCAT:
7290 case ISN_DCALL:
7291 case ISN_DROP:
7292 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007293 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007294 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007295 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007296 case ISN_EXECCONCAT:
7297 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007298 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007299 case ISN_GETITEM:
7300 case ISN_JUMP:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007301 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007302 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007303 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007304 case ISN_LOADBDICT:
7305 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007306 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007307 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007308 case ISN_LOADSCRIPT:
7309 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007310 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007311 case ISN_LOADWDICT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007312 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007313 case ISN_NEGATENR:
7314 case ISN_NEWDICT:
7315 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007316 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007317 case ISN_OPFLOAT:
7318 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007319 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007320 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007321 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007322 case ISN_PUSHF:
7323 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007324 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007325 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007326 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007327 case ISN_SHUFFLE:
7328 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007329 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007330 case ISN_STOREDICT:
7331 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007332 case ISN_STORENR:
7333 case ISN_STOREOUTER:
7334 case ISN_STOREREG:
7335 case ISN_STORESCRIPT:
7336 case ISN_STOREV:
7337 case ISN_STRINDEX:
7338 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007339 case ISN_THROW:
7340 case ISN_TRY:
7341 // nothing allocated
7342 break;
7343 }
7344}
7345
7346/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007347 * Free all instructions for "dfunc".
7348 */
7349 static void
7350delete_def_function_contents(dfunc_T *dfunc)
7351{
7352 int idx;
7353
7354 ga_clear(&dfunc->df_def_args_isn);
7355
7356 if (dfunc->df_instr != NULL)
7357 {
7358 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7359 delete_instr(dfunc->df_instr + idx);
7360 VIM_CLEAR(dfunc->df_instr);
7361 }
7362
7363 dfunc->df_deleted = TRUE;
7364}
7365
7366/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007367 * When a user function is deleted, clear the contents of any associated def
7368 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007369 */
7370 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007371clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007372{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007373 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007374 {
7375 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7376 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007377
Bram Moolenaar20431c92020-03-20 18:39:46 +01007378 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007379 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007380 }
7381}
7382
7383#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007384/*
7385 * Free all functions defined with ":def".
7386 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007387 void
7388free_def_functions(void)
7389{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007390 int idx;
7391
7392 for (idx = 0; idx < def_functions.ga_len; ++idx)
7393 {
7394 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7395
7396 delete_def_function_contents(dfunc);
7397 }
7398
7399 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007400}
7401#endif
7402
7403
7404#endif // FEAT_EVAL