blob: bec6988e546c0b94852f35454dd27228701b60fe [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".
295 * Return FAIL and give an error if it defined.
296 */
297 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200298check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100299{
Bram Moolenaarad486a02020-08-01 23:22:18 +0200300 int c = p[len];
301
302 p[len] = NUL;
Bram Moolenaar53900992020-08-22 19:02:02 +0200303 if (lookup_script(p, len, FALSE) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100304 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200305 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaareef21022020-08-01 22:16:43 +0200306 || lookup_arg(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200307 || find_imported(p, len, cctx) != NULL
308 || find_func_even_dead(p, FALSE, cctx) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100309 {
Bram Moolenaarad486a02020-08-01 23:22:18 +0200310 p[len] = c;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200311 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100312 return FAIL;
313 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200314 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100315 return OK;
316}
317
Bram Moolenaar65b95452020-07-19 14:03:09 +0200318
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100319/////////////////////////////////////////////////////////////////////
320// Following generate_ functions expect the caller to call ga_grow().
321
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200322#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
323#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100324
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100325/*
326 * Generate an instruction without arguments.
327 * Returns a pointer to the new instruction, NULL if failed.
328 */
329 static isn_T *
330generate_instr(cctx_T *cctx, isntype_T isn_type)
331{
332 garray_T *instr = &cctx->ctx_instr;
333 isn_T *isn;
334
Bram Moolenaar080457c2020-03-03 21:53:32 +0100335 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100336 if (ga_grow(instr, 1) == FAIL)
337 return NULL;
338 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
339 isn->isn_type = isn_type;
340 isn->isn_lnum = cctx->ctx_lnum + 1;
341 ++instr->ga_len;
342
343 return isn;
344}
345
346/*
347 * Generate an instruction without arguments.
348 * "drop" will be removed from the stack.
349 * Returns a pointer to the new instruction, NULL if failed.
350 */
351 static isn_T *
352generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
353{
354 garray_T *stack = &cctx->ctx_type_stack;
355
Bram Moolenaar080457c2020-03-03 21:53:32 +0100356 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100357 stack->ga_len -= drop;
358 return generate_instr(cctx, isn_type);
359}
360
361/*
362 * Generate instruction "isn_type" and put "type" on the type stack.
363 */
364 static isn_T *
365generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
366{
367 isn_T *isn;
368 garray_T *stack = &cctx->ctx_type_stack;
369
370 if ((isn = generate_instr(cctx, isn_type)) == NULL)
371 return NULL;
372
373 if (ga_grow(stack, 1) == FAIL)
374 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200375 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100376 ++stack->ga_len;
377
378 return isn;
379}
380
381/*
382 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200383 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384 */
385 static int
386may_generate_2STRING(int offset, cctx_T *cctx)
387{
388 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200389 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100390 garray_T *stack = &cctx->ctx_type_stack;
391 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
392
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200393 switch ((*type)->tt_type)
394 {
395 // nothing to be done
396 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100397
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200398 // conversion possible
399 case VAR_SPECIAL:
400 case VAR_BOOL:
401 case VAR_NUMBER:
402 case VAR_FLOAT:
403 break;
404
405 // conversion possible (with runtime check)
406 case VAR_ANY:
407 case VAR_UNKNOWN:
408 isntype = ISN_2STRING_ANY;
409 break;
410
411 // conversion not possible
412 case VAR_VOID:
413 case VAR_BLOB:
414 case VAR_FUNC:
415 case VAR_PARTIAL:
416 case VAR_LIST:
417 case VAR_DICT:
418 case VAR_JOB:
419 case VAR_CHANNEL:
420 to_string_error((*type)->tt_type);
421 return FAIL;
422 }
423
424 *type = &t_string;
425 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100426 return FAIL;
427 isn->isn_arg.number = offset;
428
429 return OK;
430}
431
432 static int
433check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
434{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200435 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100436 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200437 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100438 {
439 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200440 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100441 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200442 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100443 return FAIL;
444 }
445 return OK;
446}
447
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200448 static int
449generate_add_instr(
450 cctx_T *cctx,
451 vartype_T vartype,
452 type_T *type1,
453 type_T *type2)
454{
455 isn_T *isn = generate_instr_drop(cctx,
456 vartype == VAR_NUMBER ? ISN_OPNR
457 : vartype == VAR_LIST ? ISN_ADDLIST
458 : vartype == VAR_BLOB ? ISN_ADDBLOB
459#ifdef FEAT_FLOAT
460 : vartype == VAR_FLOAT ? ISN_OPFLOAT
461#endif
462 : ISN_OPANY, 1);
463
464 if (vartype != VAR_LIST && vartype != VAR_BLOB
465 && type1->tt_type != VAR_ANY
466 && type2->tt_type != VAR_ANY
467 && check_number_or_float(
468 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
469 return FAIL;
470
471 if (isn != NULL)
472 isn->isn_arg.op.op_type = EXPR_ADD;
473 return isn == NULL ? FAIL : OK;
474}
475
476/*
477 * Get the type to use for an instruction for an operation on "type1" and
478 * "type2". If they are matching use a type-specific instruction. Otherwise
479 * fall back to runtime type checking.
480 */
481 static vartype_T
482operator_type(type_T *type1, type_T *type2)
483{
484 if (type1->tt_type == type2->tt_type
485 && (type1->tt_type == VAR_NUMBER
486 || type1->tt_type == VAR_LIST
487#ifdef FEAT_FLOAT
488 || type1->tt_type == VAR_FLOAT
489#endif
490 || type1->tt_type == VAR_BLOB))
491 return type1->tt_type;
492 return VAR_ANY;
493}
494
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100495/*
496 * Generate an instruction with two arguments. The instruction depends on the
497 * type of the arguments.
498 */
499 static int
500generate_two_op(cctx_T *cctx, char_u *op)
501{
502 garray_T *stack = &cctx->ctx_type_stack;
503 type_T *type1;
504 type_T *type2;
505 vartype_T vartype;
506 isn_T *isn;
507
Bram Moolenaar080457c2020-03-03 21:53:32 +0100508 RETURN_OK_IF_SKIP(cctx);
509
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200510 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100511 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
512 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200513 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100514
515 switch (*op)
516 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200517 case '+':
518 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100519 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100520 break;
521
522 case '-':
523 case '*':
524 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
525 op) == FAIL)
526 return FAIL;
527 if (vartype == VAR_NUMBER)
528 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
529#ifdef FEAT_FLOAT
530 else if (vartype == VAR_FLOAT)
531 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
532#endif
533 else
534 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
535 if (isn != NULL)
536 isn->isn_arg.op.op_type = *op == '*'
537 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
538 break;
539
Bram Moolenaar4c683752020-04-05 21:38:23 +0200540 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100541 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200542 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100543 && type2->tt_type != VAR_NUMBER))
544 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200545 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100546 return FAIL;
547 }
548 isn = generate_instr_drop(cctx,
549 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
550 if (isn != NULL)
551 isn->isn_arg.op.op_type = EXPR_REM;
552 break;
553 }
554
555 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200556 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100557 {
558 type_T *type = &t_any;
559
560#ifdef FEAT_FLOAT
561 // float+number and number+float results in float
562 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
563 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
564 type = &t_float;
565#endif
566 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
567 }
568
569 return OK;
570}
571
572/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200573 * Get the instruction to use for comparing "type1" with "type2"
574 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200576 static isntype_T
577get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100578{
579 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580
Bram Moolenaar4c683752020-04-05 21:38:23 +0200581 if (type1 == VAR_UNKNOWN)
582 type1 = VAR_ANY;
583 if (type2 == VAR_UNKNOWN)
584 type2 = VAR_ANY;
585
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586 if (type1 == type2)
587 {
588 switch (type1)
589 {
590 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
591 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
592 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
593 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
594 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
595 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
596 case VAR_LIST: isntype = ISN_COMPARELIST; break;
597 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
598 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100599 default: isntype = ISN_COMPAREANY; break;
600 }
601 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200602 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100603 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
604 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
605 isntype = ISN_COMPAREANY;
606
607 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
608 && (isntype == ISN_COMPAREBOOL
609 || isntype == ISN_COMPARESPECIAL
610 || isntype == ISN_COMPARENR
611 || isntype == ISN_COMPAREFLOAT))
612 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200613 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200615 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100616 }
617 if (isntype == ISN_DROP
618 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
619 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
620 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
621 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
622 && exptype != EXPR_IS && exptype != EXPR_ISNOT
623 && (type1 == VAR_BLOB || type2 == VAR_BLOB
624 || type1 == VAR_LIST || type2 == VAR_LIST))))
625 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200626 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100627 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200628 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100629 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200630 return isntype;
631}
632
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200633 int
634check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
635{
636 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
637 return FAIL;
638 return OK;
639}
640
Bram Moolenaara5565e42020-05-09 15:44:01 +0200641/*
642 * Generate an ISN_COMPARE* instruction with a boolean result.
643 */
644 static int
645generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
646{
647 isntype_T isntype;
648 isn_T *isn;
649 garray_T *stack = &cctx->ctx_type_stack;
650 vartype_T type1;
651 vartype_T type2;
652
653 RETURN_OK_IF_SKIP(cctx);
654
655 // Get the known type of the two items on the stack. If they are matching
656 // use a type-specific instruction. Otherwise fall back to runtime type
657 // checking.
658 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
659 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
660 isntype = get_compare_isn(exptype, type1, type2);
661 if (isntype == ISN_DROP)
662 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100663
664 if ((isn = generate_instr(cctx, isntype)) == NULL)
665 return FAIL;
666 isn->isn_arg.op.op_type = exptype;
667 isn->isn_arg.op.op_ic = ic;
668
669 // takes two arguments, puts one bool back
670 if (stack->ga_len >= 2)
671 {
672 --stack->ga_len;
673 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
674 }
675
676 return OK;
677}
678
679/*
680 * Generate an ISN_2BOOL instruction.
681 */
682 static int
683generate_2BOOL(cctx_T *cctx, int invert)
684{
685 isn_T *isn;
686 garray_T *stack = &cctx->ctx_type_stack;
687
Bram Moolenaar080457c2020-03-03 21:53:32 +0100688 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100689 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
690 return FAIL;
691 isn->isn_arg.number = invert;
692
693 // type becomes bool
694 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
695
696 return OK;
697}
698
699 static int
700generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
701{
702 isn_T *isn;
703 garray_T *stack = &cctx->ctx_type_stack;
704
Bram Moolenaar080457c2020-03-03 21:53:32 +0100705 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
707 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200708 // TODO: whole type, e.g. for a function also arg and return types
709 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100710 isn->isn_arg.type.ct_off = offset;
711
712 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200713 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100714
715 return OK;
716}
717
718/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200719 * Check that
720 * - "actual" is "expected" type or
721 * - "actual" is a type that can be "expected" type: add a runtime check; or
722 * - return FAIL.
723 */
724 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200725need_type(
726 type_T *actual,
727 type_T *expected,
728 int offset,
729 cctx_T *cctx,
730 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200731{
732 if (check_type(expected, actual, FALSE) == OK)
733 return OK;
734 if (actual->tt_type != VAR_ANY
735 && actual->tt_type != VAR_UNKNOWN
736 && !(actual->tt_type == VAR_FUNC
737 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
738 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200739 if (!silent)
740 type_mismatch(expected, actual);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200741 return FAIL;
742 }
743 generate_TYPECHECK(cctx, expected, offset);
744 return OK;
745}
746
747/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100748 * Generate an ISN_PUSHNR instruction.
749 */
750 static int
751generate_PUSHNR(cctx_T *cctx, varnumber_T number)
752{
753 isn_T *isn;
754
Bram Moolenaar080457c2020-03-03 21:53:32 +0100755 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
757 return FAIL;
758 isn->isn_arg.number = number;
759
760 return OK;
761}
762
763/*
764 * Generate an ISN_PUSHBOOL instruction.
765 */
766 static int
767generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
768{
769 isn_T *isn;
770
Bram Moolenaar080457c2020-03-03 21:53:32 +0100771 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100772 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
773 return FAIL;
774 isn->isn_arg.number = number;
775
776 return OK;
777}
778
779/*
780 * Generate an ISN_PUSHSPEC instruction.
781 */
782 static int
783generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
784{
785 isn_T *isn;
786
Bram Moolenaar080457c2020-03-03 21:53:32 +0100787 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100788 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
789 return FAIL;
790 isn->isn_arg.number = number;
791
792 return OK;
793}
794
795#ifdef FEAT_FLOAT
796/*
797 * Generate an ISN_PUSHF instruction.
798 */
799 static int
800generate_PUSHF(cctx_T *cctx, float_T fnumber)
801{
802 isn_T *isn;
803
Bram Moolenaar080457c2020-03-03 21:53:32 +0100804 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100805 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
806 return FAIL;
807 isn->isn_arg.fnumber = fnumber;
808
809 return OK;
810}
811#endif
812
813/*
814 * Generate an ISN_PUSHS instruction.
815 * Consumes "str".
816 */
817 static int
818generate_PUSHS(cctx_T *cctx, char_u *str)
819{
820 isn_T *isn;
821
Bram Moolenaar080457c2020-03-03 21:53:32 +0100822 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
824 return FAIL;
825 isn->isn_arg.string = str;
826
827 return OK;
828}
829
830/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100831 * Generate an ISN_PUSHCHANNEL instruction.
832 * Consumes "channel".
833 */
834 static int
835generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
836{
837 isn_T *isn;
838
Bram Moolenaar080457c2020-03-03 21:53:32 +0100839 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100840 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
841 return FAIL;
842 isn->isn_arg.channel = channel;
843
844 return OK;
845}
846
847/*
848 * Generate an ISN_PUSHJOB instruction.
849 * Consumes "job".
850 */
851 static int
852generate_PUSHJOB(cctx_T *cctx, job_T *job)
853{
854 isn_T *isn;
855
Bram Moolenaar080457c2020-03-03 21:53:32 +0100856 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100857 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100858 return FAIL;
859 isn->isn_arg.job = job;
860
861 return OK;
862}
863
864/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100865 * Generate an ISN_PUSHBLOB instruction.
866 * Consumes "blob".
867 */
868 static int
869generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
870{
871 isn_T *isn;
872
Bram Moolenaar080457c2020-03-03 21:53:32 +0100873 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
875 return FAIL;
876 isn->isn_arg.blob = blob;
877
878 return OK;
879}
880
881/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100882 * Generate an ISN_PUSHFUNC instruction with name "name".
883 * Consumes "name".
884 */
885 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200886generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100887{
888 isn_T *isn;
889
Bram Moolenaar080457c2020-03-03 21:53:32 +0100890 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200891 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100892 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200893 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100894
895 return OK;
896}
897
898/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200899 * Generate an ISN_GETITEM instruction with "index".
900 */
901 static int
902generate_GETITEM(cctx_T *cctx, int index)
903{
904 isn_T *isn;
905 garray_T *stack = &cctx->ctx_type_stack;
906 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
907 type_T *item_type = &t_any;
908
909 RETURN_OK_IF_SKIP(cctx);
910
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200911 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200912 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200913 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200914 emsg(_(e_listreq));
915 return FAIL;
916 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200917 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200918 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
919 return FAIL;
920 isn->isn_arg.number = index;
921
922 // add the item type to the type stack
923 if (ga_grow(stack, 1) == FAIL)
924 return FAIL;
925 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
926 ++stack->ga_len;
927 return OK;
928}
929
930/*
Bram Moolenaar9af78762020-06-16 11:34:42 +0200931 * Generate an ISN_SLICE instruction with "count".
932 */
933 static int
934generate_SLICE(cctx_T *cctx, int count)
935{
936 isn_T *isn;
937
938 RETURN_OK_IF_SKIP(cctx);
939 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
940 return FAIL;
941 isn->isn_arg.number = count;
942 return OK;
943}
944
945/*
946 * Generate an ISN_CHECKLEN instruction with "min_len".
947 */
948 static int
949generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
950{
951 isn_T *isn;
952
953 RETURN_OK_IF_SKIP(cctx);
954
955 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
956 return FAIL;
957 isn->isn_arg.checklen.cl_min_len = min_len;
958 isn->isn_arg.checklen.cl_more_OK = more_OK;
959
960 return OK;
961}
962
963/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100964 * Generate an ISN_STORE instruction.
965 */
966 static int
967generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
968{
969 isn_T *isn;
970
Bram Moolenaar080457c2020-03-03 21:53:32 +0100971 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100972 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
973 return FAIL;
974 if (name != NULL)
975 isn->isn_arg.string = vim_strsave(name);
976 else
977 isn->isn_arg.number = idx;
978
979 return OK;
980}
981
982/*
983 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
984 */
985 static int
986generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
987{
988 isn_T *isn;
989
Bram Moolenaar080457c2020-03-03 21:53:32 +0100990 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100991 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
992 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +0100993 isn->isn_arg.storenr.stnr_idx = idx;
994 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100995
996 return OK;
997}
998
999/*
1000 * Generate an ISN_STOREOPT instruction
1001 */
1002 static int
1003generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1004{
1005 isn_T *isn;
1006
Bram Moolenaar080457c2020-03-03 21:53:32 +01001007 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001008 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001009 return FAIL;
1010 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1011 isn->isn_arg.storeopt.so_flags = opt_flags;
1012
1013 return OK;
1014}
1015
1016/*
1017 * Generate an ISN_LOAD or similar instruction.
1018 */
1019 static int
1020generate_LOAD(
1021 cctx_T *cctx,
1022 isntype_T isn_type,
1023 int idx,
1024 char_u *name,
1025 type_T *type)
1026{
1027 isn_T *isn;
1028
Bram Moolenaar080457c2020-03-03 21:53:32 +01001029 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001030 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1031 return FAIL;
1032 if (name != NULL)
1033 isn->isn_arg.string = vim_strsave(name);
1034 else
1035 isn->isn_arg.number = idx;
1036
1037 return OK;
1038}
1039
1040/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001041 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001042 */
1043 static int
1044generate_LOADV(
1045 cctx_T *cctx,
1046 char_u *name,
1047 int error)
1048{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001049 int di_flags;
1050 int vidx = find_vim_var(name, &di_flags);
1051 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001052
Bram Moolenaar080457c2020-03-03 21:53:32 +01001053 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001054 if (vidx < 0)
1055 {
1056 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001057 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001058 return FAIL;
1059 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001060 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001061
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001062 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001063}
1064
1065/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001066 * Generate an ISN_UNLET instruction.
1067 */
1068 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001069generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001070{
1071 isn_T *isn;
1072
1073 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001074 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001075 return FAIL;
1076 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1077 isn->isn_arg.unlet.ul_forceit = forceit;
1078
1079 return OK;
1080}
1081
1082/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001083 * Generate an ISN_LOADS instruction.
1084 */
1085 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001086generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001087 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001088 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001089 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001090 int sid,
1091 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001092{
1093 isn_T *isn;
1094
Bram Moolenaar080457c2020-03-03 21:53:32 +01001095 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001096 if (isn_type == ISN_LOADS)
1097 isn = generate_instr_type(cctx, isn_type, type);
1098 else
1099 isn = generate_instr_drop(cctx, isn_type, 1);
1100 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001101 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001102 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1103 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001104
1105 return OK;
1106}
1107
1108/*
1109 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1110 */
1111 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001112generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001113 cctx_T *cctx,
1114 isntype_T isn_type,
1115 int sid,
1116 int idx,
1117 type_T *type)
1118{
1119 isn_T *isn;
1120
Bram Moolenaar080457c2020-03-03 21:53:32 +01001121 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001122 if (isn_type == ISN_LOADSCRIPT)
1123 isn = generate_instr_type(cctx, isn_type, type);
1124 else
1125 isn = generate_instr_drop(cctx, isn_type, 1);
1126 if (isn == NULL)
1127 return FAIL;
1128 isn->isn_arg.script.script_sid = sid;
1129 isn->isn_arg.script.script_idx = idx;
1130 return OK;
1131}
1132
1133/*
1134 * Generate an ISN_NEWLIST instruction.
1135 */
1136 static int
1137generate_NEWLIST(cctx_T *cctx, int count)
1138{
1139 isn_T *isn;
1140 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001141 type_T *type;
1142 type_T *member;
1143
Bram Moolenaar080457c2020-03-03 21:53:32 +01001144 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001145 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1146 return FAIL;
1147 isn->isn_arg.number = count;
1148
Bram Moolenaar127542b2020-08-09 17:22:04 +02001149 // get the member type from all the items on the stack.
1150 member = get_member_type_from_stack(
1151 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1152 cctx->ctx_type_list);
1153 type = get_list_type(member, cctx->ctx_type_list);
1154
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155 // drop the value types
1156 stack->ga_len -= count;
1157
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001158 // add the list type to the type stack
1159 if (ga_grow(stack, 1) == FAIL)
1160 return FAIL;
1161 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1162 ++stack->ga_len;
1163
1164 return OK;
1165}
1166
1167/*
1168 * Generate an ISN_NEWDICT instruction.
1169 */
1170 static int
1171generate_NEWDICT(cctx_T *cctx, int count)
1172{
1173 isn_T *isn;
1174 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175 type_T *type;
1176 type_T *member;
1177
Bram Moolenaar080457c2020-03-03 21:53:32 +01001178 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1180 return FAIL;
1181 isn->isn_arg.number = count;
1182
Bram Moolenaar127542b2020-08-09 17:22:04 +02001183 member = get_member_type_from_stack(
1184 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1185 cctx->ctx_type_list);
1186 type = get_dict_type(member, cctx->ctx_type_list);
1187
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001188 // drop the key and value types
1189 stack->ga_len -= 2 * count;
1190
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001191 // add the dict type to the type stack
1192 if (ga_grow(stack, 1) == FAIL)
1193 return FAIL;
1194 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1195 ++stack->ga_len;
1196
1197 return OK;
1198}
1199
1200/*
1201 * Generate an ISN_FUNCREF instruction.
1202 */
1203 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001204generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001205{
1206 isn_T *isn;
1207 garray_T *stack = &cctx->ctx_type_stack;
1208
Bram Moolenaar080457c2020-03-03 21:53:32 +01001209 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001210 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1211 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001212 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001213 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001214
1215 if (ga_grow(stack, 1) == FAIL)
1216 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001217 ((type_T **)stack->ga_data)[stack->ga_len] =
1218 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219 ++stack->ga_len;
1220
1221 return OK;
1222}
1223
1224/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001225 * Generate an ISN_NEWFUNC instruction.
1226 */
1227 static int
1228generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1229{
1230 isn_T *isn;
1231 char_u *name;
1232
1233 RETURN_OK_IF_SKIP(cctx);
1234 name = vim_strsave(lambda_name);
1235 if (name == NULL)
1236 return FAIL;
1237 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1238 return FAIL;
1239 isn->isn_arg.newfunc.nf_lambda = name;
1240 isn->isn_arg.newfunc.nf_global = func_name;
1241
1242 return OK;
1243}
1244
1245/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001246 * Generate an ISN_JUMP instruction.
1247 */
1248 static int
1249generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1250{
1251 isn_T *isn;
1252 garray_T *stack = &cctx->ctx_type_stack;
1253
Bram Moolenaar080457c2020-03-03 21:53:32 +01001254 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001255 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1256 return FAIL;
1257 isn->isn_arg.jump.jump_when = when;
1258 isn->isn_arg.jump.jump_where = where;
1259
1260 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1261 --stack->ga_len;
1262
1263 return OK;
1264}
1265
1266 static int
1267generate_FOR(cctx_T *cctx, int loop_idx)
1268{
1269 isn_T *isn;
1270 garray_T *stack = &cctx->ctx_type_stack;
1271
Bram Moolenaar080457c2020-03-03 21:53:32 +01001272 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1274 return FAIL;
1275 isn->isn_arg.forloop.for_idx = loop_idx;
1276
1277 if (ga_grow(stack, 1) == FAIL)
1278 return FAIL;
1279 // type doesn't matter, will be stored next
1280 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1281 ++stack->ga_len;
1282
1283 return OK;
1284}
1285
1286/*
1287 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001288 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001289 * Return FAIL if the number of arguments is wrong.
1290 */
1291 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001292generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001293{
1294 isn_T *isn;
1295 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001296 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001297 type_T *argtypes[MAX_FUNC_ARGS];
1298 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001299
Bram Moolenaar080457c2020-03-03 21:53:32 +01001300 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001301 argoff = check_internal_func(func_idx, argcount);
1302 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303 return FAIL;
1304
Bram Moolenaar389df252020-07-09 21:20:47 +02001305 if (method_call && argoff > 1)
1306 {
1307 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1308 return FAIL;
1309 isn->isn_arg.shuffle.shfl_item = argcount;
1310 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1311 }
1312
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001313 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1314 return FAIL;
1315 isn->isn_arg.bfunc.cbf_idx = func_idx;
1316 isn->isn_arg.bfunc.cbf_argcount = argcount;
1317
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001318 for (i = 0; i < argcount; ++i)
1319 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1320
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 stack->ga_len -= argcount; // drop the arguments
1322 if (ga_grow(stack, 1) == FAIL)
1323 return FAIL;
1324 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001325 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001326 ++stack->ga_len; // add return value
1327
1328 return OK;
1329}
1330
1331/*
1332 * Generate an ISN_DCALL or ISN_UCALL instruction.
1333 * Return FAIL if the number of arguments is wrong.
1334 */
1335 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001336generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337{
1338 isn_T *isn;
1339 garray_T *stack = &cctx->ctx_type_stack;
1340 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001341 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342
Bram Moolenaar080457c2020-03-03 21:53:32 +01001343 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 if (argcount > regular_args && !has_varargs(ufunc))
1345 {
1346 semsg(_(e_toomanyarg), ufunc->uf_name);
1347 return FAIL;
1348 }
1349 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1350 {
1351 semsg(_(e_toofewarg), ufunc->uf_name);
1352 return FAIL;
1353 }
1354
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001355 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001356 {
1357 int i;
1358
1359 for (i = 0; i < argcount; ++i)
1360 {
1361 type_T *expected;
1362 type_T *actual;
1363
1364 if (i < regular_args)
1365 {
1366 if (ufunc->uf_arg_types == NULL)
1367 continue;
1368 expected = ufunc->uf_arg_types[i];
1369 }
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001370 else if (ufunc->uf_va_type == NULL)
1371 // possibly a lambda
1372 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001373 else
1374 expected = ufunc->uf_va_type->tt_member;
1375 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001376 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001377 {
1378 arg_type_mismatch(expected, actual, i + 1);
1379 return FAIL;
1380 }
1381 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001382 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001383 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1384 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001385 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001386 }
1387
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001388 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001389 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001390 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001391 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001392 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393 {
1394 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1395 isn->isn_arg.dfunc.cdf_argcount = argcount;
1396 }
1397 else
1398 {
1399 // A user function may be deleted and redefined later, can't use the
1400 // ufunc pointer, need to look it up again at runtime.
1401 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1402 isn->isn_arg.ufunc.cuf_argcount = argcount;
1403 }
1404
1405 stack->ga_len -= argcount; // drop the arguments
1406 if (ga_grow(stack, 1) == FAIL)
1407 return FAIL;
1408 // add return value
1409 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1410 ++stack->ga_len;
1411
1412 return OK;
1413}
1414
1415/*
1416 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1417 */
1418 static int
1419generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1420{
1421 isn_T *isn;
1422 garray_T *stack = &cctx->ctx_type_stack;
1423
Bram Moolenaar080457c2020-03-03 21:53:32 +01001424 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1426 return FAIL;
1427 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1428 isn->isn_arg.ufunc.cuf_argcount = argcount;
1429
1430 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001431 if (ga_grow(stack, 1) == FAIL)
1432 return FAIL;
1433 // add return value
1434 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1435 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001436
1437 return OK;
1438}
1439
1440/*
1441 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001442 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001443 */
1444 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001445generate_PCALL(
1446 cctx_T *cctx,
1447 int argcount,
1448 char_u *name,
1449 type_T *type,
1450 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001451{
1452 isn_T *isn;
1453 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001454 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455
Bram Moolenaar080457c2020-03-03 21:53:32 +01001456 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001457
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001458 if (type->tt_type == VAR_ANY)
1459 ret_type = &t_any;
1460 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001461 {
1462 if (type->tt_argcount != -1)
1463 {
1464 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1465
1466 if (argcount < type->tt_min_argcount - varargs)
1467 {
1468 semsg(_(e_toofewarg), "[reference]");
1469 return FAIL;
1470 }
1471 if (!varargs && argcount > type->tt_argcount)
1472 {
1473 semsg(_(e_toomanyarg), "[reference]");
1474 return FAIL;
1475 }
1476 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001477 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001478 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001479 else
1480 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001481 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001482 return FAIL;
1483 }
1484
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1486 return FAIL;
1487 isn->isn_arg.pfunc.cpf_top = at_top;
1488 isn->isn_arg.pfunc.cpf_argcount = argcount;
1489
1490 stack->ga_len -= argcount; // drop the arguments
1491
1492 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001493 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001494
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001495 // If partial is above the arguments it must be cleared and replaced with
1496 // the return value.
1497 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1498 return FAIL;
1499
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001500 return OK;
1501}
1502
1503/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001504 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001505 */
1506 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001507generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508{
1509 isn_T *isn;
1510 garray_T *stack = &cctx->ctx_type_stack;
1511 type_T *type;
1512
Bram Moolenaar080457c2020-03-03 21:53:32 +01001513 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001514 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001515 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001516 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001517
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001518 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001519 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001520 if (type->tt_type != VAR_DICT && type != &t_any)
1521 {
1522 emsg(_(e_dictreq));
1523 return FAIL;
1524 }
1525 // change dict type to dict member type
1526 if (type->tt_type == VAR_DICT)
1527 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528
1529 return OK;
1530}
1531
1532/*
1533 * Generate an ISN_ECHO instruction.
1534 */
1535 static int
1536generate_ECHO(cctx_T *cctx, int with_white, int count)
1537{
1538 isn_T *isn;
1539
Bram Moolenaar080457c2020-03-03 21:53:32 +01001540 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1542 return FAIL;
1543 isn->isn_arg.echo.echo_with_white = with_white;
1544 isn->isn_arg.echo.echo_count = count;
1545
1546 return OK;
1547}
1548
Bram Moolenaarad39c092020-02-26 18:23:43 +01001549/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001550 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001551 */
1552 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001553generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001554{
1555 isn_T *isn;
1556
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001557 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001558 return FAIL;
1559 isn->isn_arg.number = count;
1560
1561 return OK;
1562}
1563
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001564 static int
1565generate_EXEC(cctx_T *cctx, char_u *line)
1566{
1567 isn_T *isn;
1568
Bram Moolenaar080457c2020-03-03 21:53:32 +01001569 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001570 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1571 return FAIL;
1572 isn->isn_arg.string = vim_strsave(line);
1573 return OK;
1574}
1575
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001576 static int
1577generate_EXECCONCAT(cctx_T *cctx, int count)
1578{
1579 isn_T *isn;
1580
1581 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1582 return FAIL;
1583 isn->isn_arg.number = count;
1584 return OK;
1585}
1586
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001587/*
1588 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001589 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001591 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1593{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 lvar_T *lvar;
1595
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001596 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001597 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001598 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001599 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 }
1601
1602 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001603 return NULL;
1604 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001606 // Every local variable uses the next entry on the stack. We could re-use
1607 // the last ones when leaving a scope, but then variables used in a closure
1608 // might get overwritten. To keep things simple do not re-use stack
1609 // entries. This is less efficient, but memory is cheap these days.
1610 lvar->lv_idx = cctx->ctx_locals_count++;
1611
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001612 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 lvar->lv_const = isConst;
1614 lvar->lv_type = type;
1615
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001616 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001617}
1618
1619/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001620 * Remove local variables above "new_top".
1621 */
1622 static void
1623unwind_locals(cctx_T *cctx, int new_top)
1624{
1625 if (cctx->ctx_locals.ga_len > new_top)
1626 {
1627 int idx;
1628 lvar_T *lvar;
1629
1630 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1631 {
1632 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1633 vim_free(lvar->lv_name);
1634 }
1635 }
1636 cctx->ctx_locals.ga_len = new_top;
1637}
1638
1639/*
1640 * Free all local variables.
1641 */
1642 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001643free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001644{
1645 unwind_locals(cctx, 0);
1646 ga_clear(&cctx->ctx_locals);
1647}
1648
1649/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001650 * Find "name" in script-local items of script "sid".
1651 * Returns the index in "sn_var_vals" if found.
1652 * If found but not in "sn_var_vals" returns -1.
1653 * If not found returns -2.
1654 */
1655 int
1656get_script_item_idx(int sid, char_u *name, int check_writable)
1657{
1658 hashtab_T *ht;
1659 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001660 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001661 int idx;
1662
1663 // First look the name up in the hashtable.
Bram Moolenaare3d46852020-08-29 13:39:17 +02001664 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665 return -1;
1666 ht = &SCRIPT_VARS(sid);
1667 di = find_var_in_ht(ht, 0, name, TRUE);
1668 if (di == NULL)
1669 return -2;
1670
1671 // Now find the svar_T index in sn_var_vals.
1672 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1673 {
1674 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1675
1676 if (sv->sv_tv == &di->di_tv)
1677 {
1678 if (check_writable && sv->sv_const)
1679 semsg(_(e_readonlyvar), name);
1680 return idx;
1681 }
1682 }
1683 return -1;
1684}
1685
1686/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001687 * Find "name" in imported items of the current script or in "cctx" if not
1688 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689 */
1690 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001691find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001692{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001693 int idx;
1694
Bram Moolenaare3d46852020-08-29 13:39:17 +02001695 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001696 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697 if (cctx != NULL)
1698 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1699 {
1700 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1701 + idx;
1702
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001703 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1704 : STRLEN(import->imp_name) == len
1705 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001706 return import;
1707 }
1708
Bram Moolenaarefa94442020-08-08 22:16:00 +02001709 return find_imported_in_script(name, len, current_sctx.sc_sid);
1710}
1711
1712 imported_T *
1713find_imported_in_script(char_u *name, size_t len, int sid)
1714{
Bram Moolenaare3d46852020-08-29 13:39:17 +02001715 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001716 int idx;
1717
Bram Moolenaare3d46852020-08-29 13:39:17 +02001718 if (!SCRIPT_ID_VALID(sid))
1719 return NULL;
1720 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001721 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1722 {
1723 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1724
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001725 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1726 : STRLEN(import->imp_name) == len
1727 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728 return import;
1729 }
1730 return NULL;
1731}
1732
1733/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001734 * Free all imported variables.
1735 */
1736 static void
1737free_imported(cctx_T *cctx)
1738{
1739 int idx;
1740
1741 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1742 {
1743 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1744
1745 vim_free(import->imp_name);
1746 }
1747 ga_clear(&cctx->ctx_imports);
1748}
1749
1750/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02001751 * Return TRUE if "p" points at a "#" but not at "#{".
1752 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001753 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001754vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001755{
1756 return p[0] == '#' && p[1] != '{';
1757}
1758
1759/*
1760 * Return a pointer to the next line that isn't empty or only contains a
1761 * comment. Skips over white space.
1762 * Returns NULL if there is none.
1763 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001764 char_u *
1765peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001766{
1767 int lnum = cctx->ctx_lnum;
1768
1769 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
1770 {
1771 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001772 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001773
Bram Moolenaarba60cc42020-08-12 19:15:33 +02001774 // ignore NULLs inserted for continuation lines
1775 if (line != NULL)
1776 {
1777 p = skipwhite(line);
1778 if (*p != NUL && !vim9_comment_start(p))
1779 return p;
1780 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02001781 }
1782 return NULL;
1783}
1784
1785/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001786 * Called when checking for a following operator at "arg". When the rest of
1787 * the line is empty or only a comment, peek the next line. If there is a next
1788 * line return a pointer to it and set "nextp".
1789 * Otherwise skip over white space.
1790 */
1791 static char_u *
1792may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
1793{
1794 char_u *p = skipwhite(arg);
1795
1796 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001797 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001798 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001799 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001800 if (*nextp != NULL)
1801 return *nextp;
1802 }
1803 return p;
1804}
1805
1806/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02001807 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02001808 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02001809 * Returns NULL when at the end.
1810 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001811 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02001812next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02001813{
Bram Moolenaar7a092242020-04-16 22:10:49 +02001814 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001815
1816 do
1817 {
1818 ++cctx->ctx_lnum;
1819 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02001820 {
1821 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001822 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02001823 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02001824 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02001825 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001826 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001827 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02001828 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02001829 return line;
1830}
1831
1832/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001833 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02001834 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001835 * Return FAIL if beyond the last line, "*arg" is unmodified then.
1836 */
1837 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02001838may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001839{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001840 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001841 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02001842 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001843
1844 if (next == NULL)
1845 return FAIL;
1846 *arg = skipwhite(next);
1847 }
1848 return OK;
1849}
1850
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001851/*
1852 * Idem, and give an error when failed.
1853 */
1854 static int
1855may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
1856{
1857 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1858 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001859 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001860 return FAIL;
1861 }
1862 return OK;
1863}
1864
1865
Bram Moolenaara5565e42020-05-09 15:44:01 +02001866// Structure passed between the compile_expr* functions to keep track of
1867// constants that have been parsed but for which no code was produced yet. If
1868// possible expressions on these constants are applied at compile time. If
1869// that is not possible, the code to push the constants needs to be generated
1870// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02001871// Using 50 should be more than enough of 5 levels of ().
1872#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02001873typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02001874 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02001875 int pp_used; // active entries in pp_tv[]
1876} ppconst_T;
1877
Bram Moolenaar1c747212020-05-09 18:28:34 +02001878static int compile_expr0(char_u **arg, cctx_T *cctx);
1879static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1880
Bram Moolenaara5565e42020-05-09 15:44:01 +02001881/*
1882 * Generate a PUSH instruction for "tv".
1883 * "tv" will be consumed or cleared.
1884 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
1885 */
1886 static int
1887generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
1888{
1889 if (tv != NULL)
1890 {
1891 switch (tv->v_type)
1892 {
1893 case VAR_UNKNOWN:
1894 break;
1895 case VAR_BOOL:
1896 generate_PUSHBOOL(cctx, tv->vval.v_number);
1897 break;
1898 case VAR_SPECIAL:
1899 generate_PUSHSPEC(cctx, tv->vval.v_number);
1900 break;
1901 case VAR_NUMBER:
1902 generate_PUSHNR(cctx, tv->vval.v_number);
1903 break;
1904#ifdef FEAT_FLOAT
1905 case VAR_FLOAT:
1906 generate_PUSHF(cctx, tv->vval.v_float);
1907 break;
1908#endif
1909 case VAR_BLOB:
1910 generate_PUSHBLOB(cctx, tv->vval.v_blob);
1911 tv->vval.v_blob = NULL;
1912 break;
1913 case VAR_STRING:
1914 generate_PUSHS(cctx, tv->vval.v_string);
1915 tv->vval.v_string = NULL;
1916 break;
1917 default:
1918 iemsg("constant type not supported");
1919 clear_tv(tv);
1920 return FAIL;
1921 }
1922 tv->v_type = VAR_UNKNOWN;
1923 }
1924 return OK;
1925}
1926
1927/*
1928 * Generate code for any ppconst entries.
1929 */
1930 static int
1931generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
1932{
1933 int i;
1934 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001935 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001936
Bram Moolenaar9b68c822020-06-18 19:31:08 +02001937 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001938 for (i = 0; i < ppconst->pp_used; ++i)
1939 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
1940 ret = FAIL;
1941 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001942 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001943 return ret;
1944}
1945
1946/*
1947 * Clear ppconst constants. Used when failing.
1948 */
1949 static void
1950clear_ppconst(ppconst_T *ppconst)
1951{
1952 int i;
1953
1954 for (i = 0; i < ppconst->pp_used; ++i)
1955 clear_tv(&ppconst->pp_tv[i]);
1956 ppconst->pp_used = 0;
1957}
1958
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001959/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001960 * Generate an instruction to load script-local variable "name", without the
1961 * leading "s:".
1962 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001963 */
1964 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001965compile_load_scriptvar(
1966 cctx_T *cctx,
1967 char_u *name, // variable NUL terminated
1968 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001969 char_u **end, // end of variable
1970 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001971{
Bram Moolenaare3d46852020-08-29 13:39:17 +02001972 scriptitem_T *si;
1973 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001974 imported_T *import;
1975
Bram Moolenaare3d46852020-08-29 13:39:17 +02001976 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
1977 return FAIL;
1978 si = SCRIPT_ITEM(current_sctx.sc_sid);
1979 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001980 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001981 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001982 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001983 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
1984 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985 }
1986 if (idx >= 0)
1987 {
1988 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1989
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001990 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001991 current_sctx.sc_sid, idx, sv->sv_type);
1992 return OK;
1993 }
1994
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001995 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001996 if (import != NULL)
1997 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001998 if (import->imp_all)
1999 {
2000 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002001 char_u *exp_name;
2002 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002003 ufunc_T *ufunc;
2004 type_T *type;
2005
2006 // Used "import * as Name", need to lookup the member.
2007 if (*p != '.')
2008 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002009 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002010 return FAIL;
2011 }
2012 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002013 if (VIM_ISWHITE(*p))
2014 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002015 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002016 return FAIL;
2017 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002018
Bram Moolenaar1c991142020-07-04 13:15:31 +02002019 // isolate one name
2020 exp_name = p;
2021 while (eval_isnamec(*p))
2022 ++p;
2023 cc = *p;
2024 *p = NUL;
2025
2026 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2027 *p = cc;
2028 p = skipwhite(p);
2029
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002030 // TODO: what if it is a function?
2031 if (idx < 0)
2032 return FAIL;
2033 *end = p;
2034
2035 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2036 import->imp_sid,
2037 idx,
2038 type);
2039 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002040 else if (import->imp_funcname != NULL)
2041 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002042 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002043 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2044 import->imp_sid,
2045 import->imp_var_vals_idx,
2046 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002047 return OK;
2048 }
2049
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002050 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002051 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002052 return FAIL;
2053}
2054
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002055 static int
2056generate_funcref(cctx_T *cctx, char_u *name)
2057{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002058 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002059
2060 if (ufunc == NULL)
2061 return FAIL;
2062
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002063 // Need to compile any default values to get the argument types.
2064 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2065 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2066 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002067 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002068}
2069
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070/*
2071 * Compile a variable name into a load instruction.
2072 * "end" points to just after the name.
2073 * When "error" is FALSE do not give an error when not found.
2074 */
2075 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002076compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002077{
2078 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002079 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002080 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002081 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002082 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002083
2084 if (*(*arg + 1) == ':')
2085 {
2086 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002087 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002088 {
2089 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002090
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002091 switch (**arg)
2092 {
2093 case 'g': isn_type = ISN_LOADGDICT; break;
2094 case 'w': isn_type = ISN_LOADWDICT; break;
2095 case 't': isn_type = ISN_LOADTDICT; break;
2096 case 'b': isn_type = ISN_LOADBDICT; break;
2097 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002098 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002099 goto theend;
2100 }
2101 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2102 goto theend;
2103 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002104 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002105 else
2106 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002107 isntype_T isn_type = ISN_DROP;
2108
2109 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2110 if (name == NULL)
2111 return FAIL;
2112
2113 switch (**arg)
2114 {
2115 case 'v': res = generate_LOADV(cctx, name, error);
2116 break;
2117 case 's': res = compile_load_scriptvar(cctx, name,
2118 NULL, NULL, error);
2119 break;
2120 case 'g': isn_type = ISN_LOADG; break;
2121 case 'w': isn_type = ISN_LOADW; break;
2122 case 't': isn_type = ISN_LOADT; break;
2123 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002124 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002125 goto theend;
2126 }
2127 if (isn_type != ISN_DROP)
2128 {
2129 // Global, Buffer-local, Window-local and Tabpage-local
2130 // variables can be defined later, thus we don't check if it
2131 // exists, give error at runtime.
2132 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2133 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002134 }
2135 }
2136 else
2137 {
2138 size_t len = end - *arg;
2139 int idx;
2140 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002141 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002142
2143 name = vim_strnsave(*arg, end - *arg);
2144 if (name == NULL)
2145 return FAIL;
2146
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002147 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002148 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002149 if (!gen_load_outer)
2150 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002151 }
2152 else
2153 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002154 lvar_T *lvar = lookup_local(*arg, len, cctx);
2155
2156 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002157 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002158 type = lvar->lv_type;
2159 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002160 if (lvar->lv_from_outer)
2161 gen_load_outer = TRUE;
2162 else
2163 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164 }
2165 else
2166 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002167 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002168 // already exists in a Vim9 script or when it's imported.
2169 if (lookup_script(*arg, len, TRUE) == OK
2170 || find_imported(name, 0, cctx) != NULL)
2171 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002172
Bram Moolenaara5565e42020-05-09 15:44:01 +02002173 // When the name starts with an uppercase letter or "x:" it
2174 // can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002175 // TODO: this is just guessing
Bram Moolenaara5565e42020-05-09 15:44:01 +02002176 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2177 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002178 }
2179 }
2180 if (gen_load)
2181 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002182 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002183 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002184 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002185 cctx->ctx_outer_used = TRUE;
2186 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002187 }
2188
2189 *arg = end;
2190
2191theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002192 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002193 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002194 vim_free(name);
2195 return res;
2196}
2197
2198/*
2199 * Compile the argument expressions.
2200 * "arg" points to just after the "(" and is advanced to after the ")"
2201 */
2202 static int
2203compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2204{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002205 char_u *p = *arg;
2206 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002207
Bram Moolenaare6085c52020-04-12 20:19:16 +02002208 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002209 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002210 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2211 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002212 if (*p == ')')
2213 {
2214 *arg = p + 1;
2215 return OK;
2216 }
2217
Bram Moolenaara5565e42020-05-09 15:44:01 +02002218 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002219 return FAIL;
2220 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002221
2222 if (*p != ',' && *skipwhite(p) == ',')
2223 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002224 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002225 p = skipwhite(p);
2226 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002227 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002228 {
2229 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002230 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002231 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002232 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002233 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002234 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002235 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002236failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002237 emsg(_(e_missing_close));
2238 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002239}
2240
2241/*
2242 * Compile a function call: name(arg1, arg2)
2243 * "arg" points to "name", "arg + varlen" to the "(".
2244 * "argcount_init" is 1 for "value->method()"
2245 * Instructions:
2246 * EVAL arg1
2247 * EVAL arg2
2248 * BCALL / DCALL / UCALL
2249 */
2250 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002251compile_call(
2252 char_u **arg,
2253 size_t varlen,
2254 cctx_T *cctx,
2255 ppconst_T *ppconst,
2256 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002257{
2258 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002259 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002260 int argcount = argcount_init;
2261 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002262 char_u fname_buf[FLEN_FIXED + 1];
2263 char_u *tofree = NULL;
2264 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002265 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002266 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002267 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002268
Bram Moolenaara5565e42020-05-09 15:44:01 +02002269 // we can evaluate "has('name')" at compile time
2270 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2271 {
2272 char_u *s = skipwhite(*arg + varlen + 1);
2273 typval_T argvars[2];
2274
2275 argvars[0].v_type = VAR_UNKNOWN;
2276 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002277 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002278 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002279 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002280 s = skipwhite(s);
2281 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2282 {
2283 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2284
2285 *arg = s + 1;
2286 argvars[1].v_type = VAR_UNKNOWN;
2287 tv->v_type = VAR_NUMBER;
2288 tv->vval.v_number = 0;
2289 f_has(argvars, tv);
2290 clear_tv(&argvars[0]);
2291 ++ppconst->pp_used;
2292 return OK;
2293 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002294 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002295 }
2296
2297 if (generate_ppconst(cctx, ppconst) == FAIL)
2298 return FAIL;
2299
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002300 if (varlen >= sizeof(namebuf))
2301 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002302 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002303 return FAIL;
2304 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002305 vim_strncpy(namebuf, *arg, varlen);
2306 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002307
2308 *arg = skipwhite(*arg + varlen + 1);
2309 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002310 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311
Bram Moolenaara1773442020-08-12 15:21:22 +02002312 is_autoload = vim_strchr(name, '#') != NULL;
2313 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002314 {
2315 int idx;
2316
2317 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002318 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002319 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002320 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002321 else
2322 semsg(_(e_unknownfunc), namebuf);
2323 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002324 }
2325
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002326 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002327 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002328 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002329 {
2330 res = generate_CALL(cctx, ufunc, argcount);
2331 goto theend;
2332 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333
2334 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002335 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002336 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002337 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002338 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaara26b9702020-04-18 19:53:28 +02002339 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002340 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002341 garray_T *stack = &cctx->ctx_type_stack;
2342 type_T *type;
2343
2344 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2345 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002346 goto theend;
2347 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002348
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002349 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002350 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002351 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002352 res = generate_UCALL(cctx, name, argcount);
2353 else
2354 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002355
2356theend:
2357 vim_free(tofree);
2358 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002359}
2360
2361// like NAMESPACE_CHAR but with 'a' and 'l'.
2362#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2363
2364/*
2365 * Find the end of a variable or function name. Unlike find_name_end() this
2366 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002367 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368 * Return a pointer to just after the name. Equal to "arg" if there is no
2369 * valid name.
2370 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002371 static char_u *
2372to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002373{
2374 char_u *p;
2375
2376 // Quick check for valid starting character.
2377 if (!eval_isnamec1(*arg))
2378 return arg;
2379
2380 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2381 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2382 // and can be used in slice "[n:]".
2383 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002384 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002385 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2386 break;
2387 return p;
2388}
2389
2390/*
2391 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002392 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393 */
2394 char_u *
2395to_name_const_end(char_u *arg)
2396{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002397 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002398 typval_T rettv;
2399
2400 if (p == arg && *arg == '[')
2401 {
2402
2403 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002404 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405 p = arg;
2406 }
2407 else if (p == arg && *arg == '#' && arg[1] == '{')
2408 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002409 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002410 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002411 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002412 p = arg;
2413 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002414
2415 return p;
2416}
2417
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002418/*
2419 * parse a list: [expr, expr]
2420 * "*arg" points to the '['.
2421 */
2422 static int
2423compile_list(char_u **arg, cctx_T *cctx)
2424{
2425 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002426 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002427 int count = 0;
2428
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002429 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002430 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002431 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002432 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002433 semsg(_(e_list_end), *arg);
2434 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002435 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002436 if (*p == ',')
2437 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002438 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002439 return FAIL;
2440 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002441 if (*p == ']')
2442 {
2443 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002444 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002445 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002446 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447 break;
2448 ++count;
2449 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002450 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002451 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002452 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2453 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002454 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002455 return FAIL;
2456 }
2457 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002458 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002459 p = skipwhite(p);
2460 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002461 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002462
2463 generate_NEWLIST(cctx, count);
2464 return OK;
2465}
2466
2467/*
2468 * parse a lambda: {arg, arg -> expr}
2469 * "*arg" points to the '{'.
2470 */
2471 static int
2472compile_lambda(char_u **arg, cctx_T *cctx)
2473{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002474 typval_T rettv;
2475 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002476 evalarg_T evalarg;
2477
2478 CLEAR_FIELD(evalarg);
2479 evalarg.eval_flags = EVAL_EVALUATE;
2480 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481
2482 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002483 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002487 ++ufunc->uf_refcount;
2488 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002489 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490
2491 // The function will have one line: "return {expr}".
2492 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002493 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002494
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002495 clear_evalarg(&evalarg, NULL);
2496
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002497 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002498 {
2499 // The return type will now be known.
2500 set_function_type(ufunc);
2501
2502 return generate_FUNCREF(cctx, ufunc);
2503 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002504
2505 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506 return FAIL;
2507}
2508
2509/*
2510 * Compile a lamda call: expr->{lambda}(args)
2511 * "arg" points to the "{".
2512 */
2513 static int
2514compile_lambda_call(char_u **arg, cctx_T *cctx)
2515{
2516 ufunc_T *ufunc;
2517 typval_T rettv;
2518 int argcount = 1;
2519 int ret = FAIL;
2520
2521 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002522 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 return FAIL;
2524
2525 if (**arg != '(')
2526 {
2527 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002528 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 else
2530 semsg(_(e_missing_paren), "lambda");
2531 clear_tv(&rettv);
2532 return FAIL;
2533 }
2534
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002535 ufunc = rettv.vval.v_partial->pt_func;
2536 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002537 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002538 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002539
2540 // The function will have one line: "return {expr}".
2541 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002542 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543
2544 // compile the arguments
2545 *arg = skipwhite(*arg + 1);
2546 if (compile_arguments(arg, cctx, &argcount) == OK)
2547 // call the compiled function
2548 ret = generate_CALL(cctx, ufunc, argcount);
2549
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002550 if (ret == FAIL)
2551 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002552 return ret;
2553}
2554
2555/*
2556 * parse a dict: {'key': val} or #{key: val}
2557 * "*arg" points to the '{'.
2558 */
2559 static int
2560compile_dict(char_u **arg, cctx_T *cctx, int literal)
2561{
2562 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002563 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 int count = 0;
2565 dict_T *d = dict_alloc();
2566 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002567 char_u *whitep = *arg;
2568 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002569
2570 if (d == NULL)
2571 return FAIL;
2572 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002573 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574 {
2575 char_u *key = NULL;
2576
Bram Moolenaar23c55272020-06-21 16:58:13 +02002577 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002578 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002579 *arg = NULL;
2580 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002581 }
2582
2583 if (**arg == '}')
2584 break;
2585
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586 if (literal)
2587 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002588 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589
Bram Moolenaar2c330432020-04-13 14:41:35 +02002590 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002592 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 return FAIL;
2594 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002595 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596 if (generate_PUSHS(cctx, key) == FAIL)
2597 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002598 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 }
2600 else
2601 {
2602 isn_T *isn;
2603
Bram Moolenaara5565e42020-05-09 15:44:01 +02002604 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002606 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2607 if (isn->isn_type == ISN_PUSHS)
2608 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002609 else
2610 {
2611 type_T *keytype = ((type_T **)stack->ga_data)
2612 [stack->ga_len - 1];
2613 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2614 return FAIL;
2615 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002616 }
2617
2618 // Check for duplicate keys, if using string keys.
2619 if (key != NULL)
2620 {
2621 item = dict_find(d, key, -1);
2622 if (item != NULL)
2623 {
2624 semsg(_(e_duplicate_key), key);
2625 goto failret;
2626 }
2627 item = dictitem_alloc(key);
2628 if (item != NULL)
2629 {
2630 item->di_tv.v_type = VAR_UNKNOWN;
2631 item->di_tv.v_lock = 0;
2632 if (dict_add(d, item) == FAIL)
2633 dictitem_free(item);
2634 }
2635 }
2636
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637 if (**arg != ':')
2638 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002639 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002640 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002641 else
2642 semsg(_(e_missing_dict_colon), *arg);
2643 return FAIL;
2644 }
2645 whitep = *arg + 1;
2646 if (!IS_WHITE_OR_NUL(*whitep))
2647 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002648 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649 return FAIL;
2650 }
2651
2652 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002653 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002654 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002655 *arg = NULL;
2656 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002657 }
2658
Bram Moolenaara5565e42020-05-09 15:44:01 +02002659 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660 return FAIL;
2661 ++count;
2662
Bram Moolenaar2c330432020-04-13 14:41:35 +02002663 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002664 *arg = skipwhite(*arg);
2665 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002666 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002667 *arg = NULL;
2668 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002669 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002670 if (**arg == '}')
2671 break;
2672 if (**arg != ',')
2673 {
2674 semsg(_(e_missing_dict_comma), *arg);
2675 goto failret;
2676 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002677 if (IS_WHITE_OR_NUL(*whitep))
2678 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002679 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002680 return FAIL;
2681 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002682 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002683 *arg = skipwhite(*arg + 1);
2684 }
2685
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002686 *arg = *arg + 1;
2687
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002688 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002689 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002690 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002691 *arg += STRLEN(*arg);
2692
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002693 dict_unref(d);
2694 return generate_NEWDICT(cctx, count);
2695
2696failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002697 if (*arg == NULL)
2698 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699 dict_unref(d);
2700 return FAIL;
2701}
2702
2703/*
2704 * Compile "&option".
2705 */
2706 static int
2707compile_get_option(char_u **arg, cctx_T *cctx)
2708{
2709 typval_T rettv;
2710 char_u *start = *arg;
2711 int ret;
2712
2713 // parse the option and get the current value to get the type.
2714 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002715 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716 if (ret == OK)
2717 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002718 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719 char_u *name = vim_strnsave(start, *arg - start);
2720 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2721
2722 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2723 vim_free(name);
2724 }
2725 clear_tv(&rettv);
2726
2727 return ret;
2728}
2729
2730/*
2731 * Compile "$VAR".
2732 */
2733 static int
2734compile_get_env(char_u **arg, cctx_T *cctx)
2735{
2736 char_u *start = *arg;
2737 int len;
2738 int ret;
2739 char_u *name;
2740
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002741 ++*arg;
2742 len = get_env_len(arg);
2743 if (len == 0)
2744 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002745 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002746 return FAIL;
2747 }
2748
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002749 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002750 name = vim_strnsave(start, len + 1);
2751 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2752 vim_free(name);
2753 return ret;
2754}
2755
2756/*
2757 * Compile "@r".
2758 */
2759 static int
2760compile_get_register(char_u **arg, cctx_T *cctx)
2761{
2762 int ret;
2763
2764 ++*arg;
2765 if (**arg == NUL)
2766 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002767 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 return FAIL;
2769 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002770 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771 {
2772 emsg_invreg(**arg);
2773 return FAIL;
2774 }
2775 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2776 ++*arg;
2777 return ret;
2778}
2779
2780/*
2781 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002782 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783 */
2784 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002785apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002786{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002787 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788
2789 // this works from end to start
2790 while (p > start)
2791 {
2792 --p;
2793 if (*p == '-' || *p == '+')
2794 {
2795 // only '-' has an effect, for '+' we only check the type
2796#ifdef FEAT_FLOAT
2797 if (rettv->v_type == VAR_FLOAT)
2798 {
2799 if (*p == '-')
2800 rettv->vval.v_float = -rettv->vval.v_float;
2801 }
2802 else
2803#endif
2804 {
2805 varnumber_T val;
2806 int error = FALSE;
2807
2808 // tv_get_number_chk() accepts a string, but we don't want that
2809 // here
2810 if (check_not_string(rettv) == FAIL)
2811 return FAIL;
2812 val = tv_get_number_chk(rettv, &error);
2813 clear_tv(rettv);
2814 if (error)
2815 return FAIL;
2816 if (*p == '-')
2817 val = -val;
2818 rettv->v_type = VAR_NUMBER;
2819 rettv->vval.v_number = val;
2820 }
2821 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002822 else if (numeric_only)
2823 {
2824 ++p;
2825 break;
2826 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002827 else
2828 {
2829 int v = tv2bool(rettv);
2830
2831 // '!' is permissive in the type.
2832 clear_tv(rettv);
2833 rettv->v_type = VAR_BOOL;
2834 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2835 }
2836 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002837 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 return OK;
2839}
2840
2841/*
2842 * Recognize v: variables that are constants and set "rettv".
2843 */
2844 static void
2845get_vim_constant(char_u **arg, typval_T *rettv)
2846{
2847 if (STRNCMP(*arg, "v:true", 6) == 0)
2848 {
2849 rettv->v_type = VAR_BOOL;
2850 rettv->vval.v_number = VVAL_TRUE;
2851 *arg += 6;
2852 }
2853 else if (STRNCMP(*arg, "v:false", 7) == 0)
2854 {
2855 rettv->v_type = VAR_BOOL;
2856 rettv->vval.v_number = VVAL_FALSE;
2857 *arg += 7;
2858 }
2859 else if (STRNCMP(*arg, "v:null", 6) == 0)
2860 {
2861 rettv->v_type = VAR_SPECIAL;
2862 rettv->vval.v_number = VVAL_NULL;
2863 *arg += 6;
2864 }
2865 else if (STRNCMP(*arg, "v:none", 6) == 0)
2866 {
2867 rettv->v_type = VAR_SPECIAL;
2868 rettv->vval.v_number = VVAL_NONE;
2869 *arg += 6;
2870 }
2871}
2872
Bram Moolenaar696ba232020-07-29 21:20:41 +02002873 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02002874get_compare_type(char_u *p, int *len, int *type_is)
2875{
2876 exptype_T type = EXPR_UNKNOWN;
2877 int i;
2878
2879 switch (p[0])
2880 {
2881 case '=': if (p[1] == '=')
2882 type = EXPR_EQUAL;
2883 else if (p[1] == '~')
2884 type = EXPR_MATCH;
2885 break;
2886 case '!': if (p[1] == '=')
2887 type = EXPR_NEQUAL;
2888 else if (p[1] == '~')
2889 type = EXPR_NOMATCH;
2890 break;
2891 case '>': if (p[1] != '=')
2892 {
2893 type = EXPR_GREATER;
2894 *len = 1;
2895 }
2896 else
2897 type = EXPR_GEQUAL;
2898 break;
2899 case '<': if (p[1] != '=')
2900 {
2901 type = EXPR_SMALLER;
2902 *len = 1;
2903 }
2904 else
2905 type = EXPR_SEQUAL;
2906 break;
2907 case 'i': if (p[1] == 's')
2908 {
2909 // "is" and "isnot"; but not a prefix of a name
2910 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2911 *len = 5;
2912 i = p[*len];
2913 if (!isalnum(i) && i != '_')
2914 {
2915 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2916 *type_is = TRUE;
2917 }
2918 }
2919 break;
2920 }
2921 return type;
2922}
2923
2924/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002926 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927 */
2928 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002929compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002931 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932
2933 // this works from end to start
2934 while (p > start)
2935 {
2936 --p;
2937 if (*p == '-' || *p == '+')
2938 {
2939 int negate = *p == '-';
2940 isn_T *isn;
2941
2942 // TODO: check type
2943 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2944 {
2945 --p;
2946 if (*p == '-')
2947 negate = !negate;
2948 }
2949 // only '-' has an effect, for '+' we only check the type
2950 if (negate)
2951 isn = generate_instr(cctx, ISN_NEGATENR);
2952 else
2953 isn = generate_instr(cctx, ISN_CHECKNR);
2954 if (isn == NULL)
2955 return FAIL;
2956 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002957 else if (numeric_only)
2958 {
2959 ++p;
2960 break;
2961 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 else
2963 {
2964 int invert = TRUE;
2965
2966 while (p > start && p[-1] == '!')
2967 {
2968 --p;
2969 invert = !invert;
2970 }
2971 if (generate_2BOOL(cctx, invert) == FAIL)
2972 return FAIL;
2973 }
2974 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002975 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976 return OK;
2977}
2978
2979/*
2980 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002981 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982 */
2983 static int
2984compile_subscript(
2985 char_u **arg,
2986 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002987 char_u *start_leader,
2988 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02002989 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002991 char_u *name_start = *end_leader;
2992
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 for (;;)
2994 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002995 char_u *p = skipwhite(*arg);
2996
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002997 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002998 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002999 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003000
3001 // If a following line starts with "->{" or "->X" advance to that
3002 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003003 // Also if a following line starts with ".x".
3004 if (next != NULL &&
3005 ((next[0] == '-' && next[1] == '>'
3006 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003007 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003008 {
3009 next = next_line_from_context(cctx, TRUE);
3010 if (next == NULL)
3011 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003012 *arg = next;
3013 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003014 }
3015 }
3016
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003017 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3018 // not a function call.
3019 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003021 garray_T *stack = &cctx->ctx_type_stack;
3022 type_T *type;
3023 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003025 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003026 return FAIL;
3027
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003029 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3030
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003031 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3033 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003034 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035 return FAIL;
3036 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003037 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003039 char_u *pstart = p;
3040
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003041 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003042 return FAIL;
3043
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 // something->method()
3045 // Apply the '!', '-' and '+' first:
3046 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003047 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003050 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003051 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003052 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 if (**arg == '{')
3054 {
3055 // lambda call: list->{lambda}
3056 if (compile_lambda_call(arg, cctx) == FAIL)
3057 return FAIL;
3058 }
3059 else
3060 {
3061 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003062 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003063 if (!eval_isnamec1(*p))
3064 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003065 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003066 return FAIL;
3067 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003068 if (ASCII_ISALPHA(*p) && p[1] == ':')
3069 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003070 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071 ;
3072 if (*p != '(')
3073 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003074 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 return FAIL;
3076 }
3077 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003078 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 return FAIL;
3080 }
3081 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003082 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003084 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003085 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003086 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003087 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003088 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003089
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003090 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003091 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003092 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003093 // TODO: blob index
3094 // TODO: more arguments
3095 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003096 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003097 return FAIL;
3098
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003099 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003100 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003101 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003102 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003103 if (**arg == ':')
3104 // missing first index is equal to zero
3105 generate_PUSHNR(cctx, 0);
3106 else
3107 {
3108 if (compile_expr0(arg, cctx) == FAIL)
3109 return FAIL;
3110 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3111 return FAIL;
3112 *arg = skipwhite(*arg);
3113 }
3114 if (**arg == ':')
3115 {
3116 *arg = skipwhite(*arg + 1);
3117 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3118 return FAIL;
3119 if (**arg == ']')
3120 // missing second index is equal to end of string
3121 generate_PUSHNR(cctx, -1);
3122 else
3123 {
3124 if (compile_expr0(arg, cctx) == FAIL)
3125 return FAIL;
3126 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3127 return FAIL;
3128 *arg = skipwhite(*arg);
3129 }
3130 is_slice = TRUE;
3131 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132
3133 if (**arg != ']')
3134 {
3135 emsg(_(e_missbrac));
3136 return FAIL;
3137 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003138 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003140 // We can index a list and a dict. If we don't know the type
3141 // we can use the index value type.
3142 // TODO: If we don't know use an instruction to figure it out at
3143 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003144 typep = ((type_T **)stack->ga_data) + stack->ga_len
3145 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003146 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003147 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3148 // If the index is a string, the variable must be a Dict.
3149 if (*typep == &t_any && valtype == &t_string)
3150 vtype = VAR_DICT;
3151 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003152 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003153 if (need_type(valtype, &t_number, -1, cctx, FALSE) == FAIL)
3154 return FAIL;
3155 if (is_slice)
3156 {
3157 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
3158 if (need_type(valtype, &t_number, -2, cctx, FALSE) == FAIL)
3159 return FAIL;
3160 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003161 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003162
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003163 if (vtype == VAR_DICT)
3164 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003165 if (is_slice)
3166 {
3167 emsg(_(e_cannot_slice_dictionary));
3168 return FAIL;
3169 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003170 if ((*typep)->tt_type == VAR_DICT)
3171 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003172 else
3173 {
3174 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3175 return FAIL;
3176 *typep = &t_any;
3177 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003178 if (may_generate_2STRING(-1, cctx) == FAIL)
3179 return FAIL;
3180 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3181 return FAIL;
3182 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003183 else if (vtype == VAR_STRING)
3184 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003185 *typep = &t_string;
3186 if ((is_slice
3187 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3188 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003189 return FAIL;
3190 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003191 else if (vtype == VAR_BLOB)
3192 {
3193 emsg("Sorry, blob index and slice not implemented yet");
3194 return FAIL;
3195 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003196 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003197 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003198 if (is_slice)
3199 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003200 if (generate_instr_drop(cctx,
3201 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3202 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003203 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003204 }
Bram Moolenaared591872020-08-15 22:14:53 +02003205 else
3206 {
3207 if ((*typep)->tt_type == VAR_LIST)
3208 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003209 if (generate_instr_drop(cctx,
3210 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3211 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003212 return FAIL;
3213 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003214 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003215 else
3216 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003217 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003218 return FAIL;
3219 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003221 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003223 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003224 return FAIL;
3225
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003226 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003227 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3228 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003230 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003231 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 while (eval_isnamec(*p))
3233 MB_PTR_ADV(p);
3234 if (p == *arg)
3235 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003236 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 return FAIL;
3238 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003239 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240 return FAIL;
3241 *arg = p;
3242 }
3243 else
3244 break;
3245 }
3246
3247 // TODO - see handle_subscript():
3248 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3249 // Don't do this when "Func" is already a partial that was bound
3250 // explicitly (pt_auto is FALSE).
3251
3252 return OK;
3253}
3254
3255/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003256 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3257 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003259 * If the value is a constant "ppconst->pp_ret" will be set.
3260 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003261 *
3262 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263 */
3264
3265/*
3266 * number number constant
3267 * 0zFFFFFFFF Blob constant
3268 * "string" string constant
3269 * 'string' literal string constant
3270 * &option-name option value
3271 * @r register contents
3272 * identifier variable value
3273 * function() function call
3274 * $VAR environment variable
3275 * (expression) nested expression
3276 * [expr, expr] List
3277 * {key: val, key: val} Dictionary
3278 * #{key: val, key: val} Dictionary with literal keys
3279 *
3280 * Also handle:
3281 * ! in front logical NOT
3282 * - in front unary minus
3283 * + in front unary plus (ignored)
3284 * trailing (arg) funcref/partial call
3285 * trailing [] subscript in String or List
3286 * trailing .name entry in Dictionary
3287 * trailing ->name() method call
3288 */
3289 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003290compile_expr7(
3291 char_u **arg,
3292 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003293 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295 char_u *start_leader, *end_leader;
3296 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003297 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003298 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299
3300 /*
3301 * Skip '!', '-' and '+' characters. They are handled later.
3302 */
3303 start_leader = *arg;
3304 while (**arg == '!' || **arg == '-' || **arg == '+')
3305 *arg = skipwhite(*arg + 1);
3306 end_leader = *arg;
3307
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003308 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 switch (**arg)
3310 {
3311 /*
3312 * Number constant.
3313 */
3314 case '0': // also for blob starting with 0z
3315 case '1':
3316 case '2':
3317 case '3':
3318 case '4':
3319 case '5':
3320 case '6':
3321 case '7':
3322 case '8':
3323 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003324 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003325 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003326 // Apply "-" and "+" just before the number now, right to
3327 // left. Matters especially when "->" follows. Stops at
3328 // '!'.
3329 if (apply_leader(rettv, TRUE,
3330 start_leader, &end_leader) == FAIL)
3331 {
3332 clear_tv(rettv);
3333 return FAIL;
3334 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003335 break;
3336
3337 /*
3338 * String constant: "string".
3339 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003340 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 return FAIL;
3342 break;
3343
3344 /*
3345 * Literal string constant: 'str''ing'.
3346 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003347 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348 return FAIL;
3349 break;
3350
3351 /*
3352 * Constant Vim variable.
3353 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003354 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 ret = NOTDONE;
3356 break;
3357
3358 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003359 * "true" constant
3360 */
3361 case 't': if (STRNCMP(*arg, "true", 4) == 0
3362 && !eval_isnamec((*arg)[4]))
3363 {
3364 *arg += 4;
3365 rettv->v_type = VAR_BOOL;
3366 rettv->vval.v_number = VVAL_TRUE;
3367 }
3368 else
3369 ret = NOTDONE;
3370 break;
3371
3372 /*
3373 * "false" constant
3374 */
3375 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3376 && !eval_isnamec((*arg)[5]))
3377 {
3378 *arg += 5;
3379 rettv->v_type = VAR_BOOL;
3380 rettv->vval.v_number = VVAL_FALSE;
3381 }
3382 else
3383 ret = NOTDONE;
3384 break;
3385
3386 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003387 * List: [expr, expr]
3388 */
3389 case '[': ret = compile_list(arg, cctx);
3390 break;
3391
3392 /*
3393 * Dictionary: #{key: val, key: val}
3394 */
3395 case '#': if ((*arg)[1] == '{')
3396 {
3397 ++*arg;
3398 ret = compile_dict(arg, cctx, TRUE);
3399 }
3400 else
3401 ret = NOTDONE;
3402 break;
3403
3404 /*
3405 * Lambda: {arg, arg -> expr}
3406 * Dictionary: {'key': val, 'key': val}
3407 */
3408 case '{': {
3409 char_u *start = skipwhite(*arg + 1);
3410
3411 // Find out what comes after the arguments.
3412 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003413 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414 if (ret != FAIL && *start == '>')
3415 ret = compile_lambda(arg, cctx);
3416 else
3417 ret = compile_dict(arg, cctx, FALSE);
3418 }
3419 break;
3420
3421 /*
3422 * Option value: &name
3423 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003424 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3425 return FAIL;
3426 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427 break;
3428
3429 /*
3430 * Environment variable: $VAR.
3431 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003432 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3433 return FAIL;
3434 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 break;
3436
3437 /*
3438 * Register contents: @r.
3439 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003440 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3441 return FAIL;
3442 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003443 break;
3444 /*
3445 * nested expression: (expression).
3446 */
3447 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003448
3449 // recursive!
3450 if (ppconst->pp_used <= PPSIZE - 10)
3451 {
3452 ret = compile_expr1(arg, cctx, ppconst);
3453 }
3454 else
3455 {
3456 // Not enough space in ppconst, flush constants.
3457 if (generate_ppconst(cctx, ppconst) == FAIL)
3458 return FAIL;
3459 ret = compile_expr0(arg, cctx);
3460 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 *arg = skipwhite(*arg);
3462 if (**arg == ')')
3463 ++*arg;
3464 else if (ret == OK)
3465 {
3466 emsg(_(e_missing_close));
3467 ret = FAIL;
3468 }
3469 break;
3470
3471 default: ret = NOTDONE;
3472 break;
3473 }
3474 if (ret == FAIL)
3475 return FAIL;
3476
Bram Moolenaar1c747212020-05-09 18:28:34 +02003477 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003479 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003480 clear_tv(rettv);
3481 else
3482 // A constant expression can possibly be handled compile time,
3483 // return the value instead of generating code.
3484 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 }
3486 else if (ret == NOTDONE)
3487 {
3488 char_u *p;
3489 int r;
3490
3491 if (!eval_isnamec1(**arg))
3492 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003493 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494 return FAIL;
3495 }
3496
3497 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003498 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003499 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003500 {
3501 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3502 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003504 {
3505 if (generate_ppconst(cctx, ppconst) == FAIL)
3506 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003508 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509 if (r == FAIL)
3510 return FAIL;
3511 }
3512
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003513 // Handle following "[]", ".member", etc.
3514 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003515 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003516 ppconst) == FAIL)
3517 return FAIL;
3518 if (ppconst->pp_used > 0)
3519 {
3520 // apply the '!', '-' and '+' before the constant
3521 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003522 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003523 return FAIL;
3524 return OK;
3525 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003526 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003527 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003528 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003529}
3530
3531/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003532 * Give the "white on both sides" error, taking the operator from "p[len]".
3533 */
3534 void
3535error_white_both(char_u *op, int len)
3536{
3537 char_u buf[10];
3538
3539 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003540 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003541}
3542
3543/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003544 * <type>expr7: runtime type check / conversion
3545 */
3546 static int
3547compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3548{
3549 type_T *want_type = NULL;
3550
3551 // Recognize <type>
3552 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3553 {
3554 int called_emsg_before = called_emsg;
3555
3556 ++*arg;
3557 want_type = parse_type(arg, cctx->ctx_type_list);
3558 if (called_emsg != called_emsg_before)
3559 return FAIL;
3560
3561 if (**arg != '>')
3562 {
3563 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003564 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003565 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003566 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003567 return FAIL;
3568 }
3569 ++*arg;
3570 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3571 return FAIL;
3572 }
3573
3574 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3575 return FAIL;
3576
3577 if (want_type != NULL)
3578 {
3579 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003580 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003581
Bram Moolenaard1103582020-08-14 22:44:25 +02003582 generate_ppconst(cctx, ppconst);
3583 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003584 if (check_type(want_type, actual, FALSE) == FAIL)
3585 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003586 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3587 return FAIL;
3588 }
3589 }
3590
3591 return OK;
3592}
3593
3594/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595 * * number multiplication
3596 * / number division
3597 * % number modulo
3598 */
3599 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003600compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601{
3602 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003603 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003604 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003606 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003607 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 return FAIL;
3609
3610 /*
3611 * Repeat computing, until no "*", "/" or "%" is following.
3612 */
3613 for (;;)
3614 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003615 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003616 if (*op != '*' && *op != '/' && *op != '%')
3617 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003618 if (next != NULL)
3619 {
3620 *arg = next_line_from_context(cctx, TRUE);
3621 op = skipwhite(*arg);
3622 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003623
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003624 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003626 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003627 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 }
3629 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003630 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003631 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003633 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003634 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003636
3637 if (ppconst->pp_used == ppconst_used + 2
3638 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3639 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003640 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003641 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3642 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003643 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003645 // both are numbers: compute the result
3646 switch (*op)
3647 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003648 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003649 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003650 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003651 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003652 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003653 break;
3654 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003655 tv1->vval.v_number = res;
3656 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003657 }
3658 else
3659 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003660 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003661 generate_two_op(cctx, op);
3662 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 }
3664
3665 return OK;
3666}
3667
3668/*
3669 * + number addition
3670 * - number subtraction
3671 * .. string concatenation
3672 */
3673 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003674compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675{
3676 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003677 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003679 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680
3681 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003682 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003683 return FAIL;
3684
3685 /*
3686 * Repeat computing, until no "+", "-" or ".." is following.
3687 */
3688 for (;;)
3689 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003690 op = may_peek_next_line(cctx, *arg, &next);
3691 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692 break;
3693 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003694 if (next != NULL)
3695 {
3696 *arg = next_line_from_context(cctx, TRUE);
3697 op = skipwhite(*arg);
3698 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003700 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003702 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003703 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 }
3705
3706 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003707 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003708 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003709
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003710 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003711 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 return FAIL;
3713
Bram Moolenaara5565e42020-05-09 15:44:01 +02003714 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003715 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003716 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3717 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3718 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3719 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003721 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3722 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003723
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003724 // concat/subtract/add constant numbers
3725 if (*op == '+')
3726 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3727 else if (*op == '-')
3728 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3729 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003730 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003731 // concatenate constant strings
3732 char_u *s1 = tv1->vval.v_string;
3733 char_u *s2 = tv2->vval.v_string;
3734 size_t len1 = STRLEN(s1);
3735
3736 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3737 if (tv1->vval.v_string == NULL)
3738 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003739 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003740 return FAIL;
3741 }
3742 mch_memmove(tv1->vval.v_string, s1, len1);
3743 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003744 vim_free(s1);
3745 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003746 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003747 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748 }
3749 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003750 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003751 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003752 if (*op == '.')
3753 {
3754 if (may_generate_2STRING(-2, cctx) == FAIL
3755 || may_generate_2STRING(-1, cctx) == FAIL)
3756 return FAIL;
3757 generate_instr_drop(cctx, ISN_CONCAT, 1);
3758 }
3759 else
3760 generate_two_op(cctx, op);
3761 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762 }
3763
3764 return OK;
3765}
3766
3767/*
3768 * expr5a == expr5b
3769 * expr5a =~ expr5b
3770 * expr5a != expr5b
3771 * expr5a !~ expr5b
3772 * expr5a > expr5b
3773 * expr5a >= expr5b
3774 * expr5a < expr5b
3775 * expr5a <= expr5b
3776 * expr5a is expr5b
3777 * expr5a isnot expr5b
3778 *
3779 * Produces instructions:
3780 * EVAL expr5a Push result of "expr5a"
3781 * EVAL expr5b Push result of "expr5b"
3782 * COMPARE one of the compare instructions
3783 */
3784 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003785compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786{
3787 exptype_T type = EXPR_UNKNOWN;
3788 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003789 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003792 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003793
3794 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003795 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003796 return FAIL;
3797
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003798 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003799 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800
3801 /*
3802 * If there is a comparative operator, use it.
3803 */
3804 if (type != EXPR_UNKNOWN)
3805 {
3806 int ic = FALSE; // Default: do not ignore case
3807
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003808 if (next != NULL)
3809 {
3810 *arg = next_line_from_context(cctx, TRUE);
3811 p = skipwhite(*arg);
3812 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813 if (type_is && (p[len] == '?' || p[len] == '#'))
3814 {
3815 semsg(_(e_invexpr2), *arg);
3816 return FAIL;
3817 }
3818 // extra question mark appended: ignore case
3819 if (p[len] == '?')
3820 {
3821 ic = TRUE;
3822 ++len;
3823 }
3824 // extra '#' appended: match case (ignored)
3825 else if (p[len] == '#')
3826 ++len;
3827 // nothing appended: match case
3828
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003829 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003831 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003832 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833 }
3834
3835 // get the second variable
3836 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003837 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003838 return FAIL;
3839
Bram Moolenaara5565e42020-05-09 15:44:01 +02003840 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 return FAIL;
3842
Bram Moolenaara5565e42020-05-09 15:44:01 +02003843 if (ppconst->pp_used == ppconst_used + 2)
3844 {
3845 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3846 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3847 int ret;
3848
3849 // Both sides are a constant, compute the result now.
3850 // First check for a valid combination of types, this is more
3851 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003852 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003853 ret = FAIL;
3854 else
3855 {
3856 ret = typval_compare(tv1, tv2, type, ic);
3857 tv1->v_type = VAR_BOOL;
3858 tv1->vval.v_number = tv1->vval.v_number
3859 ? VVAL_TRUE : VVAL_FALSE;
3860 clear_tv(tv2);
3861 --ppconst->pp_used;
3862 }
3863 return ret;
3864 }
3865
3866 generate_ppconst(cctx, ppconst);
3867 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 }
3869
3870 return OK;
3871}
3872
Bram Moolenaar7f141552020-05-09 17:35:53 +02003873static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3874
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875/*
3876 * Compile || or &&.
3877 */
3878 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003879compile_and_or(
3880 char_u **arg,
3881 cctx_T *cctx,
3882 char *op,
3883 ppconst_T *ppconst,
3884 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003886 char_u *next;
3887 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 int opchar = *op;
3889
3890 if (p[0] == opchar && p[1] == opchar)
3891 {
3892 garray_T *instr = &cctx->ctx_instr;
3893 garray_T end_ga;
3894
3895 /*
3896 * Repeat until there is no following "||" or "&&"
3897 */
3898 ga_init2(&end_ga, sizeof(int), 10);
3899 while (p[0] == opchar && p[1] == opchar)
3900 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003901 if (next != NULL)
3902 {
3903 *arg = next_line_from_context(cctx, TRUE);
3904 p = skipwhite(*arg);
3905 }
3906
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003907 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3908 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003909 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003910 return FAIL;
3911 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912
Bram Moolenaara5565e42020-05-09 15:44:01 +02003913 // TODO: use ppconst if the value is a constant
3914 generate_ppconst(cctx, ppconst);
3915
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 if (ga_grow(&end_ga, 1) == FAIL)
3917 {
3918 ga_clear(&end_ga);
3919 return FAIL;
3920 }
3921 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3922 ++end_ga.ga_len;
3923 generate_JUMP(cctx, opchar == '|'
3924 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3925
3926 // eval the next expression
3927 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003928 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003929 return FAIL;
3930
Bram Moolenaara5565e42020-05-09 15:44:01 +02003931 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3932 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933 {
3934 ga_clear(&end_ga);
3935 return FAIL;
3936 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003937
3938 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003940 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941
3942 // Fill in the end label in all jumps.
3943 while (end_ga.ga_len > 0)
3944 {
3945 isn_T *isn;
3946
3947 --end_ga.ga_len;
3948 isn = ((isn_T *)instr->ga_data)
3949 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3950 isn->isn_arg.jump.jump_where = instr->ga_len;
3951 }
3952 ga_clear(&end_ga);
3953 }
3954
3955 return OK;
3956}
3957
3958/*
3959 * expr4a && expr4a && expr4a logical AND
3960 *
3961 * Produces instructions:
3962 * EVAL expr4a Push result of "expr4a"
3963 * JUMP_AND_KEEP_IF_FALSE end
3964 * EVAL expr4b Push result of "expr4b"
3965 * JUMP_AND_KEEP_IF_FALSE end
3966 * EVAL expr4c Push result of "expr4c"
3967 * end:
3968 */
3969 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003970compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003971{
Bram Moolenaara5565e42020-05-09 15:44:01 +02003972 int ppconst_used = ppconst->pp_used;
3973
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003975 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976 return FAIL;
3977
3978 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02003979 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980}
3981
3982/*
3983 * expr3a || expr3b || expr3c logical OR
3984 *
3985 * Produces instructions:
3986 * EVAL expr3a Push result of "expr3a"
3987 * JUMP_AND_KEEP_IF_TRUE end
3988 * EVAL expr3b Push result of "expr3b"
3989 * JUMP_AND_KEEP_IF_TRUE end
3990 * EVAL expr3c Push result of "expr3c"
3991 * end:
3992 */
3993 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003994compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003995{
Bram Moolenaara5565e42020-05-09 15:44:01 +02003996 int ppconst_used = ppconst->pp_used;
3997
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003998 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003999 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 return FAIL;
4001
4002 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004003 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004}
4005
4006/*
4007 * Toplevel expression: expr2 ? expr1a : expr1b
4008 *
4009 * Produces instructions:
4010 * EVAL expr2 Push result of "expr"
4011 * JUMP_IF_FALSE alt jump if false
4012 * EVAL expr1a
4013 * JUMP_ALWAYS end
4014 * alt: EVAL expr1b
4015 * end:
4016 */
4017 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004018compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019{
4020 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004021 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004022 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023
Bram Moolenaar3988f642020-08-27 22:43:03 +02004024 // Ignore all kinds of errors when not producing code.
4025 if (cctx->ctx_skip == SKIP_YES)
4026 {
4027 skip_expr(arg);
4028 return OK;
4029 }
4030
Bram Moolenaar61a89812020-05-07 16:58:17 +02004031 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004032 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033 return FAIL;
4034
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004035 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004036 if (*p == '?')
4037 {
4038 garray_T *instr = &cctx->ctx_instr;
4039 garray_T *stack = &cctx->ctx_type_stack;
4040 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004041 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004043 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004044 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004045 int has_const_expr = FALSE;
4046 int const_value = FALSE;
4047 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004049 if (next != NULL)
4050 {
4051 *arg = next_line_from_context(cctx, TRUE);
4052 p = skipwhite(*arg);
4053 }
4054
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004055 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4056 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004057 semsg(_(e_white_space_required_before_and_after_str), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004058 return FAIL;
4059 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060
Bram Moolenaara5565e42020-05-09 15:44:01 +02004061 if (ppconst->pp_used == ppconst_used + 1)
4062 {
4063 // the condition is a constant, we know whether the ? or the :
4064 // expression is to be evaluated.
4065 has_const_expr = TRUE;
4066 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4067 clear_tv(&ppconst->pp_tv[ppconst_used]);
4068 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004069 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4070 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004071 }
4072 else
4073 {
4074 generate_ppconst(cctx, ppconst);
4075 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4076 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077
4078 // evaluate the second expression; any type is accepted
4079 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004080 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004081 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004082 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004083 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084
Bram Moolenaara5565e42020-05-09 15:44:01 +02004085 if (!has_const_expr)
4086 {
4087 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004088
Bram Moolenaara5565e42020-05-09 15:44:01 +02004089 // remember the type and drop it
4090 --stack->ga_len;
4091 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004092
Bram Moolenaara5565e42020-05-09 15:44:01 +02004093 end_idx = instr->ga_len;
4094 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4095
4096 // jump here from JUMP_IF_FALSE
4097 isn = ((isn_T *)instr->ga_data) + alt_idx;
4098 isn->isn_arg.jump.jump_where = instr->ga_len;
4099 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004100
4101 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004102 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004103 if (*p != ':')
4104 {
4105 emsg(_(e_missing_colon));
4106 return FAIL;
4107 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004108 if (next != NULL)
4109 {
4110 *arg = next_line_from_context(cctx, TRUE);
4111 p = skipwhite(*arg);
4112 }
4113
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004114 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4115 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004116 semsg(_(e_white_space_required_before_and_after_str), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004117 return FAIL;
4118 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119
4120 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004121 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004122 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4123 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004125 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004126 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004127 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004128 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129
Bram Moolenaara5565e42020-05-09 15:44:01 +02004130 if (!has_const_expr)
4131 {
4132 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133
Bram Moolenaara5565e42020-05-09 15:44:01 +02004134 // If the types differ, the result has a more generic type.
4135 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4136 common_type(type1, type2, &type2, cctx->ctx_type_list);
4137
4138 // jump here from JUMP_ALWAYS
4139 isn = ((isn_T *)instr->ga_data) + end_idx;
4140 isn->isn_arg.jump.jump_where = instr->ga_len;
4141 }
4142
4143 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144 }
4145 return OK;
4146}
4147
4148/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004149 * Toplevel expression.
4150 */
4151 static int
4152compile_expr0(char_u **arg, cctx_T *cctx)
4153{
4154 ppconst_T ppconst;
4155
4156 CLEAR_FIELD(ppconst);
4157 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4158 {
4159 clear_ppconst(&ppconst);
4160 return FAIL;
4161 }
4162 if (generate_ppconst(cctx, &ppconst) == FAIL)
4163 return FAIL;
4164 return OK;
4165}
4166
4167/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168 * compile "return [expr]"
4169 */
4170 static char_u *
4171compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4172{
4173 char_u *p = arg;
4174 garray_T *stack = &cctx->ctx_type_stack;
4175 type_T *stack_type;
4176
4177 if (*p != NUL && *p != '|' && *p != '\n')
4178 {
4179 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004180 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181 return NULL;
4182
4183 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4184 if (set_return_type)
4185 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004186 else
4187 {
4188 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4189 && stack_type->tt_type != VAR_VOID
4190 && stack_type->tt_type != VAR_UNKNOWN)
4191 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004192 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004193 return NULL;
4194 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004195 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4196 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004197 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004198 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004199 }
4200 else
4201 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004202 // "set_return_type" cannot be TRUE, only used for a lambda which
4203 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004204 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4205 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004207 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004208 return NULL;
4209 }
4210
4211 // No argument, return zero.
4212 generate_PUSHNR(cctx, 0);
4213 }
4214
4215 if (generate_instr(cctx, ISN_RETURN) == NULL)
4216 return NULL;
4217
4218 // "return val | endif" is possible
4219 return skipwhite(p);
4220}
4221
4222/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004223 * Get a line from the compilation context, compatible with exarg_T getline().
4224 * Return a pointer to the line in allocated memory.
4225 * Return NULL for end-of-file or some error.
4226 */
4227 static char_u *
4228exarg_getline(
4229 int c UNUSED,
4230 void *cookie,
4231 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004232 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004233{
4234 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004235 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004236
Bram Moolenaar66250c92020-08-20 15:02:42 +02004237 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004238 {
Bram Moolenaar66250c92020-08-20 15:02:42 +02004239 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4240 return NULL;
4241 ++cctx->ctx_lnum;
4242 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4243 // Comment lines result in NULL pointers, skip them.
4244 if (p != NULL)
4245 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004246 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004247}
4248
4249/*
4250 * Compile a nested :def command.
4251 */
4252 static char_u *
4253compile_nested_function(exarg_T *eap, cctx_T *cctx)
4254{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004255 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004256 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004257 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004258 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004259 lvar_T *lvar;
4260 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004261 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004262
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004263 // Only g:Func() can use a namespace.
4264 if (name_start[1] == ':' && !is_global)
4265 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004266 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004267 return NULL;
4268 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004269 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4270 return NULL;
4271
Bram Moolenaar04b12692020-05-04 23:24:44 +02004272 eap->arg = name_end;
4273 eap->getline = exarg_getline;
4274 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004275 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004276 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004277 lambda_name = get_lambda_name();
4278 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004279
Bram Moolenaar822ba242020-05-24 23:00:18 +02004280 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004281 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004282 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004283 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004284 return NULL;
4285
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004286 if (is_global)
4287 {
4288 char_u *func_name = vim_strnsave(name_start + 2,
4289 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004290
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004291 if (func_name == NULL)
4292 r = FAIL;
4293 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004294 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004295 }
4296 else
4297 {
4298 // Define a local variable for the function reference.
4299 lvar = reserve_local(cctx, name_start, name_end - name_start,
4300 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004301 if (lvar == NULL)
4302 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004303 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004304 return NULL;
4305 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4306 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004307
Bram Moolenaar61a89812020-05-07 16:58:17 +02004308 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004309 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004310}
4311
4312/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 * Return the length of an assignment operator, or zero if there isn't one.
4314 */
4315 int
4316assignment_len(char_u *p, int *heredoc)
4317{
4318 if (*p == '=')
4319 {
4320 if (p[1] == '<' && p[2] == '<')
4321 {
4322 *heredoc = TRUE;
4323 return 3;
4324 }
4325 return 1;
4326 }
4327 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4328 return 2;
4329 if (STRNCMP(p, "..=", 3) == 0)
4330 return 3;
4331 return 0;
4332}
4333
4334// words that cannot be used as a variable
4335static char *reserved[] = {
4336 "true",
4337 "false",
4338 NULL
4339};
4340
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004341typedef enum {
4342 dest_local,
4343 dest_option,
4344 dest_env,
4345 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004346 dest_buffer,
4347 dest_window,
4348 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004349 dest_vimvar,
4350 dest_script,
4351 dest_reg,
4352} assign_dest_T;
4353
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004354/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004355 * Generate the load instruction for "name".
4356 */
4357 static void
4358generate_loadvar(
4359 cctx_T *cctx,
4360 assign_dest_T dest,
4361 char_u *name,
4362 lvar_T *lvar,
4363 type_T *type)
4364{
4365 switch (dest)
4366 {
4367 case dest_option:
4368 // TODO: check the option exists
4369 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4370 break;
4371 case dest_global:
4372 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4373 break;
4374 case dest_buffer:
4375 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4376 break;
4377 case dest_window:
4378 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4379 break;
4380 case dest_tab:
4381 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4382 break;
4383 case dest_script:
4384 compile_load_scriptvar(cctx,
4385 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4386 break;
4387 case dest_env:
4388 // Include $ in the name here
4389 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4390 break;
4391 case dest_reg:
4392 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4393 break;
4394 case dest_vimvar:
4395 generate_LOADV(cctx, name + 2, TRUE);
4396 break;
4397 case dest_local:
4398 if (lvar->lv_from_outer)
4399 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4400 NULL, type);
4401 else
4402 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4403 break;
4404 }
4405}
4406
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004407 void
4408vim9_declare_error(char_u *name)
4409{
4410 char *scope = "";
4411
4412 switch (*name)
4413 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004414 case 'g': scope = _("global"); break;
4415 case 'b': scope = _("buffer"); break;
4416 case 'w': scope = _("window"); break;
4417 case 't': scope = _("tab"); break;
4418 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004419 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004420 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004421 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004422 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004423 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004424 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004425 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004426 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004427 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004428}
4429
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004430/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004431 * Compile declaration and assignment:
4432 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004434 * Return NULL for an error.
4435 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004436 */
4437 static char_u *
4438compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4439{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004440 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004442 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443 char_u *ret = NULL;
4444 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004445 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004446 int scriptvar_sid = 0;
4447 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004449 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004450 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004452 int oplen = 0;
4453 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004454 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004455 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004456 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004457 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004458 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004460 // Skip over the "var" or "[var, var]" to get to any "=".
4461 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4462 if (p == NULL)
4463 return *arg == '[' ? arg : NULL;
4464
4465 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004467 // TODO: should we allow this, and figure out type inference from list
4468 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004469 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470 return NULL;
4471 }
4472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004473 sp = p;
4474 p = skipwhite(p);
4475 op = p;
4476 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004477
4478 if (var_count > 0 && oplen == 0)
4479 // can be something like "[1, 2]->func()"
4480 return arg;
4481
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004482 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4483 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004484 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004485 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004486 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004487
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488 if (heredoc)
4489 {
4490 list_T *l;
4491 listitem_T *li;
4492
4493 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004494 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004495 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004496 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004497
4498 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004499 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004500 {
4501 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4502 li->li_tv.vval.v_string = NULL;
4503 }
4504 generate_NEWLIST(cctx, l->lv_len);
4505 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004506 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004507 list_free(l);
4508 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004509 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004510 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004511 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004513 // for "[var, var] = expr" evaluate the expression here, loop over the
4514 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004515
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004516 p = skipwhite(op + oplen);
4517 if (compile_expr0(&p, cctx) == FAIL)
4518 return NULL;
4519 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004520
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004521 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004522 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004523 type_T *stacktype;
4524
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004525 stacktype = stack->ga_len == 0 ? &t_void
4526 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004527 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004528 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004529 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004530 goto theend;
4531 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004532 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004533 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004534 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004535 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4536 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004537 }
4538 }
4539
4540 /*
4541 * Loop over variables in "[var, var] = expr".
4542 * For "var = expr" and "let var: type" this is done only once.
4543 */
4544 if (var_count > 0)
4545 var_start = skipwhite(arg + 1); // skip over the "["
4546 else
4547 var_start = arg;
4548 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4549 {
4550 char_u *var_end = skip_var_one(var_start, FALSE);
4551 size_t varlen;
4552 int new_local = FALSE;
4553 int opt_type;
4554 int opt_flags = 0;
4555 assign_dest_T dest = dest_local;
4556 int vimvaridx = -1;
4557 lvar_T *lvar = NULL;
4558 lvar_T arg_lvar;
4559 int has_type = FALSE;
4560 int has_index = FALSE;
4561 int instr_count = -1;
4562
Bram Moolenaar65821722020-08-02 18:58:54 +02004563 if (*var_start == '@')
4564 p = var_start + 2;
4565 else
4566 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02004567 // skip over the leading "&", "&l:", "&g:" and "$"
4568 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02004569 p = to_name_end(p, TRUE);
4570 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004571
4572 // "a: type" is declaring variable "a" with a type, not "a:".
4573 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4574 --var_end;
4575 if (is_decl && p == var_start + 2 && p[-1] == ':')
4576 --p;
4577
4578 varlen = p - var_start;
4579 vim_free(name);
4580 name = vim_strnsave(var_start, varlen);
4581 if (name == NULL)
4582 return NULL;
4583 if (!heredoc)
4584 type = &t_any;
4585
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004586 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004587 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004588 int declare_error = FALSE;
4589
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004590 if (*var_start == '&')
4591 {
4592 int cc;
4593 long numval;
4594
4595 dest = dest_option;
4596 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004598 emsg(_(e_const_option));
4599 goto theend;
4600 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004601 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004602 p = var_start;
4603 p = find_option_end(&p, &opt_flags);
4604 if (p == NULL)
4605 {
4606 // cannot happen?
4607 emsg(_(e_letunexp));
4608 goto theend;
4609 }
4610 cc = *p;
4611 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02004612 opt_type = get_option_value(skip_option_env_lead(var_start),
4613 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004614 *p = cc;
4615 if (opt_type == -3)
4616 {
4617 semsg(_(e_unknown_option), var_start);
4618 goto theend;
4619 }
4620 if (opt_type == -2 || opt_type == 0)
4621 type = &t_string;
4622 else
4623 type = &t_number; // both number and boolean option
4624 }
4625 else if (*var_start == '$')
4626 {
4627 dest = dest_env;
4628 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004629 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004630 }
4631 else if (*var_start == '@')
4632 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004633 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004634 {
4635 emsg_invreg(var_start[1]);
4636 goto theend;
4637 }
4638 dest = dest_reg;
4639 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004640 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004641 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004642 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004643 {
4644 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004645 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004646 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004647 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004648 {
4649 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004650 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004651 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004652 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004653 {
4654 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004655 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004656 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004657 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004658 {
4659 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004660 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004661 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004662 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004663 {
4664 typval_T *vtv;
4665 int di_flags;
4666
4667 vimvaridx = find_vim_var(name + 2, &di_flags);
4668 if (vimvaridx < 0)
4669 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004670 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004671 goto theend;
4672 }
4673 // We use the current value of "sandbox" here, is that OK?
4674 if (var_check_ro(di_flags, name, FALSE))
4675 goto theend;
4676 dest = dest_vimvar;
4677 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004678 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004679 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004680 }
4681 else
4682 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004683 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004684
4685 for (idx = 0; reserved[idx] != NULL; ++idx)
4686 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004687 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004688 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004689 goto theend;
4690 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004691
4692 lvar = lookup_local(var_start, varlen, cctx);
4693 if (lvar == NULL)
4694 {
4695 CLEAR_FIELD(arg_lvar);
4696 if (lookup_arg(var_start, varlen,
4697 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4698 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004699 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004700 if (is_decl)
4701 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004702 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004703 goto theend;
4704 }
4705 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004706 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004707 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004708 if (lvar != NULL)
4709 {
4710 if (is_decl)
4711 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004712 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004713 goto theend;
4714 }
4715 else if (lvar->lv_const)
4716 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004717 semsg(_(e_cannot_assign_to_constant), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004718 goto theend;
4719 }
4720 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004721 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004722 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004723 int script_namespace = varlen > 1
4724 && STRNCMP(var_start, "s:", 2) == 0;
4725 int script_var = (script_namespace
Bram Moolenaar53900992020-08-22 19:02:02 +02004726 ? lookup_script(var_start + 2, varlen - 2, FALSE)
4727 : lookup_script(var_start, varlen, TRUE)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004728 imported_T *import =
4729 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004730
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004731 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004732 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004733 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4734
4735 if (is_decl)
4736 {
4737 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004738 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004739 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004740 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004741 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004742 name);
4743 goto theend;
4744 }
4745 else if (cctx->ctx_ufunc->uf_script_ctx_version
4746 == SCRIPT_VERSION_VIM9
4747 && script_namespace
4748 && !script_var && import == NULL)
4749 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004750 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004751 goto theend;
4752 }
4753
4754 dest = dest_script;
4755
4756 // existing script-local variables should have a type
4757 scriptvar_sid = current_sctx.sc_sid;
4758 if (import != NULL)
4759 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004760 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004761 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02004762 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4763 rawname, TRUE);
4764 if (scriptvar_idx > 0)
4765 {
4766 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4767 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004768 ((svar_T *)si->sn_var_vals.ga_data)
4769 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004770 type = sv->sv_type;
4771 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004772 }
4773 }
4774 else if (name[1] == ':' && name[2] != NUL)
4775 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004776 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004777 goto theend;
4778 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004779 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004780 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004781 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004782 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004783 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004784 else if (check_defined(var_start, varlen, cctx) == FAIL)
4785 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004786 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004787 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004788
4789 if (declare_error)
4790 {
4791 vim9_declare_error(name);
4792 goto theend;
4793 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004794 }
4795
4796 // handle "a:name" as a name, not index "name" on "a"
4797 if (varlen > 1 || var_start[varlen] != ':')
4798 p = var_end;
4799
4800 if (dest != dest_option)
4801 {
4802 if (is_decl && *p == ':')
4803 {
4804 // parse optional type: "let var: type = expr"
4805 if (!VIM_ISWHITE(p[1]))
4806 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004807 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004808 goto theend;
4809 }
4810 p = skipwhite(p + 1);
4811 type = parse_type(&p, cctx->ctx_type_list);
4812 has_type = TRUE;
4813 }
4814 else if (lvar != NULL)
4815 type = lvar->lv_type;
4816 }
4817
4818 if (oplen == 3 && !heredoc && dest != dest_global
4819 && type->tt_type != VAR_STRING
4820 && type->tt_type != VAR_ANY)
4821 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004822 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004823 goto theend;
4824 }
4825
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004826 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004827 {
4828 if (oplen > 1 && !heredoc)
4829 {
4830 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004831 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004832 goto theend;
4833 }
4834
4835 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004836 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
4837 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004838 goto theend;
4839 lvar = reserve_local(cctx, var_start, varlen,
4840 cmdidx == CMD_const, type);
4841 if (lvar == NULL)
4842 goto theend;
4843 new_local = TRUE;
4844 }
4845
4846 member_type = type;
4847 if (var_end > var_start + varlen)
4848 {
4849 // Something follows after the variable: "var[idx]".
4850 if (is_decl)
4851 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004852 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004853 goto theend;
4854 }
4855
4856 if (var_start[varlen] == '[')
4857 {
4858 has_index = TRUE;
4859 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004860 member_type = &t_any;
4861 else
4862 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004863 }
4864 else
4865 {
4866 semsg("Not supported yet: %s", var_start);
4867 goto theend;
4868 }
4869 }
4870 else if (lvar == &arg_lvar)
4871 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004872 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004873 goto theend;
4874 }
4875
4876 if (!heredoc)
4877 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004878 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004879 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004880 if (oplen > 0 && var_count == 0)
4881 {
4882 // skip over the "=" and the expression
4883 p = skipwhite(op + oplen);
4884 compile_expr0(&p, cctx);
4885 }
4886 }
4887 else if (oplen > 0)
4888 {
4889 type_T *stacktype;
4890
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004891 // For "var = expr" evaluate the expression.
4892 if (var_count == 0)
4893 {
4894 int r;
4895
4896 // for "+=", "*=", "..=" etc. first load the current value
4897 if (*op != '=')
4898 {
4899 generate_loadvar(cctx, dest, name, lvar, type);
4900
4901 if (has_index)
4902 {
4903 // TODO: get member from list or dict
4904 emsg("Index with operation not supported yet");
4905 goto theend;
4906 }
4907 }
4908
4909 // Compile the expression. Temporarily hide the new local
4910 // variable here, it is not available to this expression.
4911 if (new_local)
4912 --cctx->ctx_locals.ga_len;
4913 instr_count = instr->ga_len;
4914 p = skipwhite(op + oplen);
4915 r = compile_expr0(&p, cctx);
4916 if (new_local)
4917 ++cctx->ctx_locals.ga_len;
4918 if (r == FAIL)
4919 goto theend;
4920 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004921 else if (semicolon && var_idx == var_count - 1)
4922 {
4923 // For "[var; var] = expr" get the rest of the list
4924 if (generate_SLICE(cctx, var_count - 1) == FAIL)
4925 goto theend;
4926 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004927 else
4928 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004929 // For "[var, var] = expr" get the "var_idx" item from the
4930 // list.
4931 if (generate_GETITEM(cctx, var_idx) == FAIL)
4932 return FAIL;
4933 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004934
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004935 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004936 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004937 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004938 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004939 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004940 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004941 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004942 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004943 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004944 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004945 }
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004946 else if ((stacktype->tt_type == VAR_FUNC
4947 || stacktype->tt_type == VAR_PARTIAL)
4948 && var_wrong_func_name(name, TRUE))
4949 {
4950 goto theend;
4951 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004952 else
4953 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004954 // An empty list or dict has a &t_void member,
4955 // for a variable that implies &t_any.
4956 if (stacktype == &t_list_empty)
4957 lvar->lv_type = &t_list_any;
4958 else if (stacktype == &t_dict_empty)
4959 lvar->lv_type = &t_dict_any;
4960 else
4961 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004962 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004963 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02004964 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004965 {
4966 type_T *use_type = lvar->lv_type;
4967
Bram Moolenaar93ad1472020-08-19 22:02:41 +02004968 // without operator type is here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004969 if (has_index)
4970 {
4971 use_type = use_type->tt_member;
4972 if (use_type == NULL)
4973 use_type = &t_void;
4974 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004975 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar93ad1472020-08-19 22:02:41 +02004976 == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004977 goto theend;
4978 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004979 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004980 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004981 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004982 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004983 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004984 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004985 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004986 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004987 goto theend;
4988 }
4989 else if (!has_type || dest == dest_option)
4990 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004991 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004992 goto theend;
4993 }
4994 else
4995 {
4996 // variables are always initialized
4997 if (ga_grow(instr, 1) == FAIL)
4998 goto theend;
4999 switch (member_type->tt_type)
5000 {
5001 case VAR_BOOL:
5002 generate_PUSHBOOL(cctx, VVAL_FALSE);
5003 break;
5004 case VAR_FLOAT:
5005#ifdef FEAT_FLOAT
5006 generate_PUSHF(cctx, 0.0);
5007#endif
5008 break;
5009 case VAR_STRING:
5010 generate_PUSHS(cctx, NULL);
5011 break;
5012 case VAR_BLOB:
5013 generate_PUSHBLOB(cctx, NULL);
5014 break;
5015 case VAR_FUNC:
5016 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5017 break;
5018 case VAR_LIST:
5019 generate_NEWLIST(cctx, 0);
5020 break;
5021 case VAR_DICT:
5022 generate_NEWDICT(cctx, 0);
5023 break;
5024 case VAR_JOB:
5025 generate_PUSHJOB(cctx, NULL);
5026 break;
5027 case VAR_CHANNEL:
5028 generate_PUSHCHANNEL(cctx, NULL);
5029 break;
5030 case VAR_NUMBER:
5031 case VAR_UNKNOWN:
5032 case VAR_ANY:
5033 case VAR_PARTIAL:
5034 case VAR_VOID:
5035 case VAR_SPECIAL: // cannot happen
5036 generate_PUSHNR(cctx, 0);
5037 break;
5038 }
5039 }
5040 if (var_count == 0)
5041 end = p;
5042 }
5043
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005044 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005045 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005046 break;
5047
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005048 if (oplen > 0 && *op != '=')
5049 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005050 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005051 type_T *stacktype;
5052
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005053 if (*op == '.')
5054 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005055 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005056 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005057 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005058 if (
5059#ifdef FEAT_FLOAT
5060 // If variable is float operation with number is OK.
5061 !(expected == &t_float && stacktype == &t_number) &&
5062#endif
5063 need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005064 goto theend;
5065
5066 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005067 {
5068 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5069 goto theend;
5070 }
5071 else if (*op == '+')
5072 {
5073 if (generate_add_instr(cctx,
5074 operator_type(member_type, stacktype),
5075 member_type, stacktype) == FAIL)
5076 goto theend;
5077 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005078 else if (generate_two_op(cctx, op) == FAIL)
5079 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005080 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005081
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005082 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005083 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005084 int r;
5085
5086 // Compile the "idx" in "var[idx]".
5087 if (new_local)
5088 --cctx->ctx_locals.ga_len;
5089 p = skipwhite(var_start + varlen + 1);
5090 r = compile_expr0(&p, cctx);
5091 if (new_local)
5092 ++cctx->ctx_locals.ga_len;
5093 if (r == FAIL)
5094 goto theend;
5095 if (*skipwhite(p) != ']')
5096 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005097 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005098 emsg(_(e_missbrac));
5099 goto theend;
5100 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005101 if (type == &t_any)
5102 {
5103 type_T *idx_type = ((type_T **)stack->ga_data)[
5104 stack->ga_len - 1];
5105 // Index on variable of unknown type: guess the type from the
5106 // index type: number is dict, otherwise dict.
5107 // TODO: should do the assignment at runtime
5108 if (idx_type->tt_type == VAR_NUMBER)
5109 type = &t_list_any;
5110 else
5111 type = &t_dict_any;
5112 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005113 if (type->tt_type == VAR_DICT
5114 && may_generate_2STRING(-1, cctx) == FAIL)
5115 goto theend;
5116 if (type->tt_type == VAR_LIST
5117 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005118 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005119 {
5120 emsg(_(e_number_exp));
5121 goto theend;
5122 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005123
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005124 // Load the dict or list. On the stack we then have:
5125 // - value
5126 // - index
5127 // - variable
5128 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005129
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005130 if (type->tt_type == VAR_LIST)
5131 {
5132 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5133 return FAIL;
5134 }
5135 else if (type->tt_type == VAR_DICT)
5136 {
5137 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5138 return FAIL;
5139 }
5140 else
5141 {
5142 emsg(_(e_listreq));
5143 goto theend;
5144 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005145 }
5146 else
5147 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005148 switch (dest)
5149 {
5150 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005151 generate_STOREOPT(cctx, skip_option_env_lead(name),
5152 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005153 break;
5154 case dest_global:
5155 // include g: with the name, easier to execute that way
5156 generate_STORE(cctx, ISN_STOREG, 0, name);
5157 break;
5158 case dest_buffer:
5159 // include b: with the name, easier to execute that way
5160 generate_STORE(cctx, ISN_STOREB, 0, name);
5161 break;
5162 case dest_window:
5163 // include w: with the name, easier to execute that way
5164 generate_STORE(cctx, ISN_STOREW, 0, name);
5165 break;
5166 case dest_tab:
5167 // include t: with the name, easier to execute that way
5168 generate_STORE(cctx, ISN_STORET, 0, name);
5169 break;
5170 case dest_env:
5171 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5172 break;
5173 case dest_reg:
5174 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5175 break;
5176 case dest_vimvar:
5177 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5178 break;
5179 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005180 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005181 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005182 {
5183 char_u *name_s = name;
5184
5185 // Include s: in the name for store_var()
5186 if (name[1] != ':')
5187 {
5188 int len = (int)STRLEN(name) + 3;
5189
5190 name_s = alloc(len);
5191 if (name_s == NULL)
5192 name_s = name;
5193 else
5194 vim_snprintf((char *)name_s, len,
5195 "s:%s", name);
5196 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005197 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5198 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005199 if (name_s != name)
5200 vim_free(name_s);
5201 }
5202 else
5203 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005204 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005205 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005206 break;
5207 case dest_local:
5208 if (lvar != NULL)
5209 {
5210 isn_T *isn = ((isn_T *)instr->ga_data)
5211 + instr->ga_len - 1;
5212
5213 // optimization: turn "var = 123" from ISN_PUSHNR +
5214 // ISN_STORE into ISN_STORENR
5215 if (!lvar->lv_from_outer
5216 && instr->ga_len == instr_count + 1
5217 && isn->isn_type == ISN_PUSHNR)
5218 {
5219 varnumber_T val = isn->isn_arg.number;
5220
5221 isn->isn_type = ISN_STORENR;
5222 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5223 isn->isn_arg.storenr.stnr_val = val;
5224 if (stack->ga_len > 0)
5225 --stack->ga_len;
5226 }
5227 else if (lvar->lv_from_outer)
5228 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005229 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005230 else
5231 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5232 }
5233 break;
5234 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005235 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005236
5237 if (var_idx + 1 < var_count)
5238 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005239 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005240
5241 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005242 if (var_count > 0 && !semicolon)
5243 {
5244 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5245 goto theend;
5246 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005247
Bram Moolenaarb2097502020-07-19 17:17:02 +02005248 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005249
5250theend:
5251 vim_free(name);
5252 return ret;
5253}
5254
5255/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005256 * Check if "name" can be "unlet".
5257 */
5258 int
5259check_vim9_unlet(char_u *name)
5260{
5261 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5262 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005263 // "unlet s:var" is allowed in legacy script.
5264 if (*name == 's' && !script_is_vim9())
5265 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005266 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005267 return FAIL;
5268 }
5269 return OK;
5270}
5271
5272/*
5273 * Callback passed to ex_unletlock().
5274 */
5275 static int
5276compile_unlet(
5277 lval_T *lvp,
5278 char_u *name_end,
5279 exarg_T *eap,
5280 int deep UNUSED,
5281 void *coookie)
5282{
5283 cctx_T *cctx = coookie;
5284
5285 if (lvp->ll_tv == NULL)
5286 {
5287 char_u *p = lvp->ll_name;
5288 int cc = *name_end;
5289 int ret = OK;
5290
5291 // Normal name. Only supports g:, w:, t: and b: namespaces.
5292 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005293 if (*p == '$')
5294 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5295 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005296 ret = FAIL;
5297 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005298 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005299
5300 *name_end = cc;
5301 return ret;
5302 }
5303
5304 // TODO: unlet {list}[idx]
5305 // TODO: unlet {dict}[key]
5306 emsg("Sorry, :unlet not fully implemented yet");
5307 return FAIL;
5308}
5309
5310/*
5311 * compile "unlet var", "lock var" and "unlock var"
5312 * "arg" points to "var".
5313 */
5314 static char_u *
5315compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5316{
5317 char_u *p = arg;
5318
5319 if (eap->cmdidx != CMD_unlet)
5320 {
5321 emsg("Sorry, :lock and unlock not implemented yet");
5322 return NULL;
5323 }
5324
5325 if (*p == '!')
5326 {
5327 p = skipwhite(p + 1);
5328 eap->forceit = TRUE;
5329 }
5330
5331 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5332 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5333}
5334
5335/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005336 * Compile an :import command.
5337 */
5338 static char_u *
5339compile_import(char_u *arg, cctx_T *cctx)
5340{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005341 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005342}
5343
5344/*
5345 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5346 */
5347 static int
5348compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5349{
5350 garray_T *instr = &cctx->ctx_instr;
5351 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5352
5353 if (endlabel == NULL)
5354 return FAIL;
5355 endlabel->el_next = *el;
5356 *el = endlabel;
5357 endlabel->el_end_label = instr->ga_len;
5358
5359 generate_JUMP(cctx, when, 0);
5360 return OK;
5361}
5362
5363 static void
5364compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5365{
5366 garray_T *instr = &cctx->ctx_instr;
5367
5368 while (*el != NULL)
5369 {
5370 endlabel_T *cur = (*el);
5371 isn_T *isn;
5372
5373 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5374 isn->isn_arg.jump.jump_where = instr->ga_len;
5375 *el = cur->el_next;
5376 vim_free(cur);
5377 }
5378}
5379
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005380 static void
5381compile_free_jump_to_end(endlabel_T **el)
5382{
5383 while (*el != NULL)
5384 {
5385 endlabel_T *cur = (*el);
5386
5387 *el = cur->el_next;
5388 vim_free(cur);
5389 }
5390}
5391
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005392/*
5393 * Create a new scope and set up the generic items.
5394 */
5395 static scope_T *
5396new_scope(cctx_T *cctx, scopetype_T type)
5397{
5398 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5399
5400 if (scope == NULL)
5401 return NULL;
5402 scope->se_outer = cctx->ctx_scope;
5403 cctx->ctx_scope = scope;
5404 scope->se_type = type;
5405 scope->se_local_count = cctx->ctx_locals.ga_len;
5406 return scope;
5407}
5408
5409/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005410 * Free the current scope and go back to the outer scope.
5411 */
5412 static void
5413drop_scope(cctx_T *cctx)
5414{
5415 scope_T *scope = cctx->ctx_scope;
5416
5417 if (scope == NULL)
5418 {
5419 iemsg("calling drop_scope() without a scope");
5420 return;
5421 }
5422 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005423 switch (scope->se_type)
5424 {
5425 case IF_SCOPE:
5426 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5427 case FOR_SCOPE:
5428 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5429 case WHILE_SCOPE:
5430 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5431 case TRY_SCOPE:
5432 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5433 case NO_SCOPE:
5434 case BLOCK_SCOPE:
5435 break;
5436 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005437 vim_free(scope);
5438}
5439
5440/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005441 * compile "if expr"
5442 *
5443 * "if expr" Produces instructions:
5444 * EVAL expr Push result of "expr"
5445 * JUMP_IF_FALSE end
5446 * ... body ...
5447 * end:
5448 *
5449 * "if expr | else" Produces instructions:
5450 * EVAL expr Push result of "expr"
5451 * JUMP_IF_FALSE else
5452 * ... body ...
5453 * JUMP_ALWAYS end
5454 * else:
5455 * ... body ...
5456 * end:
5457 *
5458 * "if expr1 | elseif expr2 | else" Produces instructions:
5459 * EVAL expr Push result of "expr"
5460 * JUMP_IF_FALSE elseif
5461 * ... body ...
5462 * JUMP_ALWAYS end
5463 * elseif:
5464 * EVAL expr Push result of "expr"
5465 * JUMP_IF_FALSE else
5466 * ... body ...
5467 * JUMP_ALWAYS end
5468 * else:
5469 * ... body ...
5470 * end:
5471 */
5472 static char_u *
5473compile_if(char_u *arg, cctx_T *cctx)
5474{
5475 char_u *p = arg;
5476 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005477 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005478 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005479 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005480 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005481
Bram Moolenaara5565e42020-05-09 15:44:01 +02005482 CLEAR_FIELD(ppconst);
5483 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005484 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005485 clear_ppconst(&ppconst);
5486 return NULL;
5487 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005488 if (cctx->ctx_skip == SKIP_YES)
5489 clear_ppconst(&ppconst);
5490 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005491 {
5492 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005493 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005494 clear_ppconst(&ppconst);
5495 }
5496 else
5497 {
5498 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005499 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005500 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005501 return NULL;
5502 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005503
5504 scope = new_scope(cctx, IF_SCOPE);
5505 if (scope == NULL)
5506 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005507 scope->se_skip_save = skip_save;
5508 // "is_had_return" will be reset if any block does not end in :return
5509 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005510
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005511 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005512 {
5513 // "where" is set when ":elseif", "else" or ":endif" is found
5514 scope->se_u.se_if.is_if_label = instr->ga_len;
5515 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5516 }
5517 else
5518 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005519
5520 return p;
5521}
5522
5523 static char_u *
5524compile_elseif(char_u *arg, cctx_T *cctx)
5525{
5526 char_u *p = arg;
5527 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005528 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005529 isn_T *isn;
5530 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005531 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02005532 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005533
5534 if (scope == NULL || scope->se_type != IF_SCOPE)
5535 {
5536 emsg(_(e_elseif_without_if));
5537 return NULL;
5538 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005539 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005540 if (!cctx->ctx_had_return)
5541 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005542
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005543 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005544 {
5545 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005546 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005547 return NULL;
5548 // previous "if" or "elseif" jumps here
5549 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5550 isn->isn_arg.jump.jump_where = instr->ga_len;
5551 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005552
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005553 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005554 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02005555 if (cctx->ctx_skip == SKIP_YES)
5556 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005557 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005558 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005559 clear_ppconst(&ppconst);
5560 return NULL;
5561 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02005562 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005563 if (scope->se_skip_save == SKIP_YES)
5564 clear_ppconst(&ppconst);
5565 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005566 {
5567 // The expression results in a constant.
5568 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005569 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005570 clear_ppconst(&ppconst);
5571 scope->se_u.se_if.is_if_label = -1;
5572 }
5573 else
5574 {
5575 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005576 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005577 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005578 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005579
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005580 // "where" is set when ":elseif", "else" or ":endif" is found
5581 scope->se_u.se_if.is_if_label = instr->ga_len;
5582 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5583 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005584
5585 return p;
5586}
5587
5588 static char_u *
5589compile_else(char_u *arg, cctx_T *cctx)
5590{
5591 char_u *p = arg;
5592 garray_T *instr = &cctx->ctx_instr;
5593 isn_T *isn;
5594 scope_T *scope = cctx->ctx_scope;
5595
5596 if (scope == NULL || scope->se_type != IF_SCOPE)
5597 {
5598 emsg(_(e_else_without_if));
5599 return NULL;
5600 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005601 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005602 if (!cctx->ctx_had_return)
5603 scope->se_u.se_if.is_had_return = FALSE;
5604 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005605
Bram Moolenaarefd88552020-06-18 20:50:10 +02005606 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005607 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005608 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005609 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005610 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005611 if (!cctx->ctx_had_return
5612 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5613 JUMP_ALWAYS, cctx) == FAIL)
5614 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005615 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005616
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005617 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005618 {
5619 if (scope->se_u.se_if.is_if_label >= 0)
5620 {
5621 // previous "if" or "elseif" jumps here
5622 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5623 isn->isn_arg.jump.jump_where = instr->ga_len;
5624 scope->se_u.se_if.is_if_label = -1;
5625 }
5626 }
5627
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005628 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005629 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5630 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005631
5632 return p;
5633}
5634
5635 static char_u *
5636compile_endif(char_u *arg, cctx_T *cctx)
5637{
5638 scope_T *scope = cctx->ctx_scope;
5639 ifscope_T *ifscope;
5640 garray_T *instr = &cctx->ctx_instr;
5641 isn_T *isn;
5642
5643 if (scope == NULL || scope->se_type != IF_SCOPE)
5644 {
5645 emsg(_(e_endif_without_if));
5646 return NULL;
5647 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005648 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005649 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005650 if (!cctx->ctx_had_return)
5651 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005652
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005653 if (scope->se_u.se_if.is_if_label >= 0)
5654 {
5655 // previous "if" or "elseif" jumps here
5656 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5657 isn->isn_arg.jump.jump_where = instr->ga_len;
5658 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005659 // Fill in the "end" label in jumps at the end of the blocks.
5660 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005661 cctx->ctx_skip = scope->se_skip_save;
5662
5663 // If all the blocks end in :return and there is an :else then the
5664 // had_return flag is set.
5665 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005666
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005667 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005668 return arg;
5669}
5670
5671/*
5672 * compile "for var in expr"
5673 *
5674 * Produces instructions:
5675 * PUSHNR -1
5676 * STORE loop-idx Set index to -1
5677 * EVAL expr Push result of "expr"
5678 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5679 * - if beyond end, jump to "end"
5680 * - otherwise get item from list and push it
5681 * STORE var Store item in "var"
5682 * ... body ...
5683 * JUMP top Jump back to repeat
5684 * end: DROP Drop the result of "expr"
5685 *
5686 */
5687 static char_u *
5688compile_for(char_u *arg, cctx_T *cctx)
5689{
5690 char_u *p;
5691 size_t varlen;
5692 garray_T *instr = &cctx->ctx_instr;
5693 garray_T *stack = &cctx->ctx_type_stack;
5694 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005695 lvar_T *loop_lvar; // loop iteration variable
5696 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005697 type_T *vartype;
5698
5699 // TODO: list of variables: "for [key, value] in dict"
5700 // parse "var"
5701 for (p = arg; eval_isnamec1(*p); ++p)
5702 ;
5703 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005704 var_lvar = lookup_local(arg, varlen, cctx);
5705 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005706 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005707 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005708 return NULL;
5709 }
5710
5711 // consume "in"
5712 p = skipwhite(p);
5713 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5714 {
5715 emsg(_(e_missing_in));
5716 return NULL;
5717 }
5718 p = skipwhite(p + 2);
5719
5720
5721 scope = new_scope(cctx, FOR_SCOPE);
5722 if (scope == NULL)
5723 return NULL;
5724
5725 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005726 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5727 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005728 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005729 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005730 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005731 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005732 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005733
5734 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005735 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5736 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005737 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005738 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005739 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005740 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005741 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005742
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005743 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005744
5745 // compile "expr", it remains on the stack until "endfor"
5746 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005747 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005748 {
5749 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005750 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005751 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005752
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005753 // Now that we know the type of "var", check that it is a list, now or at
5754 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005755 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005756 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005757 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005758 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005759 return NULL;
5760 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005761 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005762 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005763
5764 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005765 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005766
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005767 generate_FOR(cctx, loop_lvar->lv_idx);
5768 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005769
5770 return arg;
5771}
5772
5773/*
5774 * compile "endfor"
5775 */
5776 static char_u *
5777compile_endfor(char_u *arg, cctx_T *cctx)
5778{
5779 garray_T *instr = &cctx->ctx_instr;
5780 scope_T *scope = cctx->ctx_scope;
5781 forscope_T *forscope;
5782 isn_T *isn;
5783
5784 if (scope == NULL || scope->se_type != FOR_SCOPE)
5785 {
5786 emsg(_(e_for));
5787 return NULL;
5788 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005789 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005790 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005791 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005792
5793 // At end of ":for" scope jump back to the FOR instruction.
5794 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5795
5796 // Fill in the "end" label in the FOR statement so it can jump here
5797 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5798 isn->isn_arg.forloop.for_end = instr->ga_len;
5799
5800 // Fill in the "end" label any BREAK statements
5801 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5802
5803 // Below the ":for" scope drop the "expr" list from the stack.
5804 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5805 return NULL;
5806
5807 vim_free(scope);
5808
5809 return arg;
5810}
5811
5812/*
5813 * compile "while expr"
5814 *
5815 * Produces instructions:
5816 * top: EVAL expr Push result of "expr"
5817 * JUMP_IF_FALSE end jump if false
5818 * ... body ...
5819 * JUMP top Jump back to repeat
5820 * end:
5821 *
5822 */
5823 static char_u *
5824compile_while(char_u *arg, cctx_T *cctx)
5825{
5826 char_u *p = arg;
5827 garray_T *instr = &cctx->ctx_instr;
5828 scope_T *scope;
5829
5830 scope = new_scope(cctx, WHILE_SCOPE);
5831 if (scope == NULL)
5832 return NULL;
5833
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005834 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005835
5836 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005837 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005838 return NULL;
5839
5840 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005841 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005842 JUMP_IF_FALSE, cctx) == FAIL)
5843 return FAIL;
5844
5845 return p;
5846}
5847
5848/*
5849 * compile "endwhile"
5850 */
5851 static char_u *
5852compile_endwhile(char_u *arg, cctx_T *cctx)
5853{
5854 scope_T *scope = cctx->ctx_scope;
5855
5856 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5857 {
5858 emsg(_(e_while));
5859 return NULL;
5860 }
5861 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005862 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005863
5864 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005865 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005866
5867 // Fill in the "end" label in the WHILE statement so it can jump here.
5868 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005869 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005870
5871 vim_free(scope);
5872
5873 return arg;
5874}
5875
5876/*
5877 * compile "continue"
5878 */
5879 static char_u *
5880compile_continue(char_u *arg, cctx_T *cctx)
5881{
5882 scope_T *scope = cctx->ctx_scope;
5883
5884 for (;;)
5885 {
5886 if (scope == NULL)
5887 {
5888 emsg(_(e_continue));
5889 return NULL;
5890 }
5891 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5892 break;
5893 scope = scope->se_outer;
5894 }
5895
5896 // Jump back to the FOR or WHILE instruction.
5897 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005898 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5899 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005900 return arg;
5901}
5902
5903/*
5904 * compile "break"
5905 */
5906 static char_u *
5907compile_break(char_u *arg, cctx_T *cctx)
5908{
5909 scope_T *scope = cctx->ctx_scope;
5910 endlabel_T **el;
5911
5912 for (;;)
5913 {
5914 if (scope == NULL)
5915 {
5916 emsg(_(e_break));
5917 return NULL;
5918 }
5919 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5920 break;
5921 scope = scope->se_outer;
5922 }
5923
5924 // Jump to the end of the FOR or WHILE loop.
5925 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005926 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005927 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005928 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005929 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5930 return FAIL;
5931
5932 return arg;
5933}
5934
5935/*
5936 * compile "{" start of block
5937 */
5938 static char_u *
5939compile_block(char_u *arg, cctx_T *cctx)
5940{
5941 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5942 return NULL;
5943 return skipwhite(arg + 1);
5944}
5945
5946/*
5947 * compile end of block: drop one scope
5948 */
5949 static void
5950compile_endblock(cctx_T *cctx)
5951{
5952 scope_T *scope = cctx->ctx_scope;
5953
5954 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005955 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005956 vim_free(scope);
5957}
5958
5959/*
5960 * compile "try"
5961 * Creates a new scope for the try-endtry, pointing to the first catch and
5962 * finally.
5963 * Creates another scope for the "try" block itself.
5964 * TRY instruction sets up exception handling at runtime.
5965 *
5966 * "try"
5967 * TRY -> catch1, -> finally push trystack entry
5968 * ... try block
5969 * "throw {exception}"
5970 * EVAL {exception}
5971 * THROW create exception
5972 * ... try block
5973 * " catch {expr}"
5974 * JUMP -> finally
5975 * catch1: PUSH exeception
5976 * EVAL {expr}
5977 * MATCH
5978 * JUMP nomatch -> catch2
5979 * CATCH remove exception
5980 * ... catch block
5981 * " catch"
5982 * JUMP -> finally
5983 * catch2: CATCH remove exception
5984 * ... catch block
5985 * " finally"
5986 * finally:
5987 * ... finally block
5988 * " endtry"
5989 * ENDTRY pop trystack entry, may rethrow
5990 */
5991 static char_u *
5992compile_try(char_u *arg, cctx_T *cctx)
5993{
5994 garray_T *instr = &cctx->ctx_instr;
5995 scope_T *try_scope;
5996 scope_T *scope;
5997
5998 // scope that holds the jumps that go to catch/finally/endtry
5999 try_scope = new_scope(cctx, TRY_SCOPE);
6000 if (try_scope == NULL)
6001 return NULL;
6002
6003 // "catch" is set when the first ":catch" is found.
6004 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006005 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006006 if (generate_instr(cctx, ISN_TRY) == NULL)
6007 return NULL;
6008
6009 // scope for the try block itself
6010 scope = new_scope(cctx, BLOCK_SCOPE);
6011 if (scope == NULL)
6012 return NULL;
6013
6014 return arg;
6015}
6016
6017/*
6018 * compile "catch {expr}"
6019 */
6020 static char_u *
6021compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6022{
6023 scope_T *scope = cctx->ctx_scope;
6024 garray_T *instr = &cctx->ctx_instr;
6025 char_u *p;
6026 isn_T *isn;
6027
6028 // end block scope from :try or :catch
6029 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6030 compile_endblock(cctx);
6031 scope = cctx->ctx_scope;
6032
6033 // Error if not in a :try scope
6034 if (scope == NULL || scope->se_type != TRY_SCOPE)
6035 {
6036 emsg(_(e_catch));
6037 return NULL;
6038 }
6039
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006040 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006041 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006042 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006043 return NULL;
6044 }
6045
6046 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006047 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006048 JUMP_ALWAYS, cctx) == FAIL)
6049 return NULL;
6050
6051 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006052 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006053 if (isn->isn_arg.try.try_catch == 0)
6054 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006055 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006056 {
6057 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006058 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006059 isn->isn_arg.jump.jump_where = instr->ga_len;
6060 }
6061
6062 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006063 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006064 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006065 scope->se_u.se_try.ts_caught_all = TRUE;
6066 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006067 }
6068 else
6069 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006070 char_u *end;
6071 char_u *pat;
6072 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006073 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006074 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006075
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006076 // Push v:exception, push {expr} and MATCH
6077 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6078
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006079 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006080 if (*end != *p)
6081 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006082 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006083 vim_free(tofree);
6084 return FAIL;
6085 }
6086 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006087 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006088 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006089 len = (int)(end - tofree);
6090 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006091 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006092 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006093 if (pat == NULL)
6094 return FAIL;
6095 if (generate_PUSHS(cctx, pat) == FAIL)
6096 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006097
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006098 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6099 return NULL;
6100
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006101 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006102 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6103 return NULL;
6104 }
6105
6106 if (generate_instr(cctx, ISN_CATCH) == NULL)
6107 return NULL;
6108
6109 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6110 return NULL;
6111 return p;
6112}
6113
6114 static char_u *
6115compile_finally(char_u *arg, cctx_T *cctx)
6116{
6117 scope_T *scope = cctx->ctx_scope;
6118 garray_T *instr = &cctx->ctx_instr;
6119 isn_T *isn;
6120
6121 // end block scope from :try or :catch
6122 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6123 compile_endblock(cctx);
6124 scope = cctx->ctx_scope;
6125
6126 // Error if not in a :try scope
6127 if (scope == NULL || scope->se_type != TRY_SCOPE)
6128 {
6129 emsg(_(e_finally));
6130 return NULL;
6131 }
6132
6133 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006134 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006135 if (isn->isn_arg.try.try_finally != 0)
6136 {
6137 emsg(_(e_finally_dup));
6138 return NULL;
6139 }
6140
6141 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006142 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006143
Bram Moolenaar585fea72020-04-02 22:33:21 +02006144 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006145 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006146 {
6147 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006148 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006149 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006150 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006151 }
6152
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006153 // TODO: set index in ts_finally_label jumps
6154
6155 return arg;
6156}
6157
6158 static char_u *
6159compile_endtry(char_u *arg, cctx_T *cctx)
6160{
6161 scope_T *scope = cctx->ctx_scope;
6162 garray_T *instr = &cctx->ctx_instr;
6163 isn_T *isn;
6164
6165 // end block scope from :catch or :finally
6166 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6167 compile_endblock(cctx);
6168 scope = cctx->ctx_scope;
6169
6170 // Error if not in a :try scope
6171 if (scope == NULL || scope->se_type != TRY_SCOPE)
6172 {
6173 if (scope == NULL)
6174 emsg(_(e_no_endtry));
6175 else if (scope->se_type == WHILE_SCOPE)
6176 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006177 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006178 emsg(_(e_endfor));
6179 else
6180 emsg(_(e_endif));
6181 return NULL;
6182 }
6183
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006184 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006185 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6186 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006187 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006188 return NULL;
6189 }
6190
6191 // Fill in the "end" label in jumps at the end of the blocks, if not done
6192 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006193 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006194
6195 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006196 if (isn->isn_arg.try.try_catch == 0)
6197 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006198 if (isn->isn_arg.try.try_finally == 0)
6199 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006200
6201 if (scope->se_u.se_try.ts_catch_label != 0)
6202 {
6203 // Last catch without match jumps here
6204 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6205 isn->isn_arg.jump.jump_where = instr->ga_len;
6206 }
6207
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006208 compile_endblock(cctx);
6209
6210 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6211 return NULL;
6212 return arg;
6213}
6214
6215/*
6216 * compile "throw {expr}"
6217 */
6218 static char_u *
6219compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6220{
6221 char_u *p = skipwhite(arg);
6222
Bram Moolenaara5565e42020-05-09 15:44:01 +02006223 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006224 return NULL;
6225 if (may_generate_2STRING(-1, cctx) == FAIL)
6226 return NULL;
6227 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6228 return NULL;
6229
6230 return p;
6231}
6232
6233/*
6234 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006235 * compile "echomsg expr"
6236 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006237 * compile "execute expr"
6238 */
6239 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006240compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006241{
6242 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006243 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006244 int count = 0;
6245
6246 for (;;)
6247 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006248 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006249 return NULL;
6250 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006251 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006252 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006253 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006254 break;
6255 }
6256
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006257 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6258 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6259 else if (cmdidx == CMD_execute)
6260 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6261 else if (cmdidx == CMD_echomsg)
6262 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6263 else
6264 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006265 return p;
6266}
6267
6268/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006269 * A command that is not compiled, execute with legacy code.
6270 */
6271 static char_u *
6272compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6273{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006274 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006275 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006276 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006277
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006278 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006279 goto theend;
6280
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006281 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006282 {
6283 long argt = excmd_get_argt(eap->cmdidx);
6284 int usefilter = FALSE;
6285
6286 has_expr = argt & (EX_XFILE | EX_EXPAND);
6287
6288 // If the command can be followed by a bar, find the bar and truncate
6289 // it, so that the following command can be compiled.
6290 // The '|' is overwritten with a NUL, it is put back below.
6291 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6292 && *eap->arg == '!')
6293 // :w !filter or :r !filter or :r! filter
6294 usefilter = TRUE;
6295 if ((argt & EX_TRLBAR) && !usefilter)
6296 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006297 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006298 separate_nextcmd(eap);
6299 if (eap->nextcmd != NULL)
6300 nextcmd = eap->nextcmd;
6301 }
6302 }
6303
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006304 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6305 {
6306 // expand filename in "syntax include [@group] filename"
6307 has_expr = TRUE;
6308 eap->arg = skipwhite(eap->arg + 7);
6309 if (*eap->arg == '@')
6310 eap->arg = skiptowhite(eap->arg);
6311 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006312
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006313 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006314 {
6315 int count = 0;
6316 char_u *start = skipwhite(line);
6317
6318 // :cmd xxx`=expr1`yyy`=expr2`zzz
6319 // PUSHS ":cmd xxx"
6320 // eval expr1
6321 // PUSHS "yyy"
6322 // eval expr2
6323 // PUSHS "zzz"
6324 // EXECCONCAT 5
6325 for (;;)
6326 {
6327 if (p > start)
6328 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006329 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006330 ++count;
6331 }
6332 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006333 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006334 return NULL;
6335 may_generate_2STRING(-1, cctx);
6336 ++count;
6337 p = skipwhite(p);
6338 if (*p != '`')
6339 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006340 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006341 return NULL;
6342 }
6343 start = p + 1;
6344
6345 p = (char_u *)strstr((char *)start, "`=");
6346 if (p == NULL)
6347 {
6348 if (*skipwhite(start) != NUL)
6349 {
6350 generate_PUSHS(cctx, vim_strsave(start));
6351 ++count;
6352 }
6353 break;
6354 }
6355 }
6356 generate_EXECCONCAT(cctx, count);
6357 }
6358 else
6359 generate_EXEC(cctx, line);
6360
6361theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006362 if (*nextcmd != NUL)
6363 {
6364 // the parser expects a pointer to the bar, put it back
6365 --nextcmd;
6366 *nextcmd = '|';
6367 }
6368
6369 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006370}
6371
6372/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006373 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006374 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006375 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006376 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006377add_def_function(ufunc_T *ufunc)
6378{
6379 dfunc_T *dfunc;
6380
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006381 if (def_functions.ga_len == 0)
6382 {
6383 // The first position is not used, so that a zero uf_dfunc_idx means it
6384 // wasn't set.
6385 if (ga_grow(&def_functions, 1) == FAIL)
6386 return FAIL;
6387 ++def_functions.ga_len;
6388 }
6389
Bram Moolenaar09689a02020-05-09 22:50:08 +02006390 // Add the function to "def_functions".
6391 if (ga_grow(&def_functions, 1) == FAIL)
6392 return FAIL;
6393 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6394 CLEAR_POINTER(dfunc);
6395 dfunc->df_idx = def_functions.ga_len;
6396 ufunc->uf_dfunc_idx = dfunc->df_idx;
6397 dfunc->df_ufunc = ufunc;
6398 ++def_functions.ga_len;
6399 return OK;
6400}
6401
6402/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006403 * After ex_function() has collected all the function lines: parse and compile
6404 * the lines into instructions.
6405 * Adds the function to "def_functions".
6406 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6407 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006408 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006409 * This can be used recursively through compile_lambda(), which may reallocate
6410 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006411 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006412 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006413 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006414compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006415{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006416 char_u *line = NULL;
6417 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006418 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006419 cctx_T cctx;
6420 garray_T *instr;
6421 int called_emsg_before = called_emsg;
6422 int ret = FAIL;
6423 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006424 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006425 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006426 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006427
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006428 // When using a function that was compiled before: Free old instructions.
6429 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006430 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006431 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006432 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6433 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006434 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006435 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006436 else
6437 {
6438 if (add_def_function(ufunc) == FAIL)
6439 return FAIL;
6440 new_def_function = TRUE;
6441 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006442
Bram Moolenaar985116a2020-07-12 17:31:09 +02006443 ufunc->uf_def_status = UF_COMPILING;
6444
Bram Moolenaara80faa82020-04-12 19:37:17 +02006445 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006446 cctx.ctx_ufunc = ufunc;
6447 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006448 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006449 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6450 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6451 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6452 cctx.ctx_type_list = &ufunc->uf_type_list;
6453 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6454 instr = &cctx.ctx_instr;
6455
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006456 // Set the context to the function, it may be compiled when called from
6457 // another script. Set the script version to the most modern one.
6458 // The line number will be set in next_line_from_context().
6459 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006460 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6461
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006462 // Make sure error messages are OK.
6463 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6464 if (do_estack_push)
6465 estack_push_ufunc(ufunc, 1);
6466
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006467 if (ufunc->uf_def_args.ga_len > 0)
6468 {
6469 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006470 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006471 int i;
6472 char_u *arg;
6473 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006474 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006475
6476 // Produce instructions for the default values of optional arguments.
6477 // Store the instruction index in uf_def_arg_idx[] so that we know
6478 // where to start when the function is called, depending on the number
6479 // of arguments.
6480 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6481 if (ufunc->uf_def_arg_idx == NULL)
6482 goto erret;
6483 for (i = 0; i < count; ++i)
6484 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006485 garray_T *stack = &cctx.ctx_type_stack;
6486 type_T *val_type;
6487 int arg_idx = first_def_arg + i;
6488
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006489 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6490 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006491 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006492 goto erret;
6493
6494 // If no type specified use the type of the default value.
6495 // Otherwise check that the default value type matches the
6496 // specified type.
6497 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6498 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006499 {
6500 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006501 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006502 }
Bram Moolenaare30f64b2020-07-15 19:48:20 +02006503 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006504 == FAIL)
6505 {
6506 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6507 arg_idx + 1);
6508 goto erret;
6509 }
6510
6511 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006512 goto erret;
6513 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006514 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006515
6516 if (did_set_arg_type)
6517 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006518 }
6519
6520 /*
6521 * Loop over all the lines of the function and generate instructions.
6522 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006523 for (;;)
6524 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006525 exarg_T ea;
6526 cmdmod_T save_cmdmod;
6527 int starts_with_colon = FALSE;
6528 char_u *cmd;
6529 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006530
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006531 // Bail out on the first error to avoid a flood of errors and report
6532 // the right line number when inside try/catch.
6533 if (emsg_before != called_emsg)
6534 goto erret;
6535
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006536 if (line != NULL && *line == '|')
6537 // the line continues after a '|'
6538 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006539 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006540 && !(*line == '#' && (line == cctx.ctx_line_start
6541 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006542 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006543 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006544 goto erret;
6545 }
6546 else
6547 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006548 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006549 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006550 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006551 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006552 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006553 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006554
Bram Moolenaara80faa82020-04-12 19:37:17 +02006555 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006556 ea.cmdlinep = &line;
6557 ea.cmd = skipwhite(line);
6558
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006559 // Some things can be recognized by the first character.
6560 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006561 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006562 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006563 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006564 if (ea.cmd[1] != '{')
6565 {
6566 line = (char_u *)"";
6567 continue;
6568 }
6569 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006570
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006571 case '}':
6572 {
6573 // "}" ends a block scope
6574 scopetype_T stype = cctx.ctx_scope == NULL
6575 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006576
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006577 if (stype == BLOCK_SCOPE)
6578 {
6579 compile_endblock(&cctx);
6580 line = ea.cmd;
6581 }
6582 else
6583 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006584 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006585 goto erret;
6586 }
6587 if (line != NULL)
6588 line = skipwhite(ea.cmd + 1);
6589 continue;
6590 }
6591
6592 case '{':
6593 // "{" starts a block scope
6594 // "{'a': 1}->func() is something else
6595 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6596 {
6597 line = compile_block(ea.cmd, &cctx);
6598 continue;
6599 }
6600 break;
6601
6602 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006603 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006604 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006605 }
6606
6607 /*
6608 * COMMAND MODIFIERS
6609 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006610 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006611 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6612 {
6613 if (errormsg != NULL)
6614 goto erret;
6615 // empty line or comment
6616 line = (char_u *)"";
6617 continue;
6618 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006619 // TODO: use modifiers in the command
6620 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006621 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006622
6623 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006624 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006625 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006626 {
6627 if (*ea.cmd == '(')
6628 // not for "call()"
6629 ea.cmd = p;
6630 else
6631 ea.cmd = skipwhite(ea.cmd);
6632 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006633
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006634 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006635 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006636 char_u *pskip;
6637
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006638 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006639 // find what follows.
6640 // Skip over "var.member", "var[idx]" and the like.
6641 // Also "&opt = val", "$ENV = val" and "@r = val".
6642 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006643 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006644 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006645 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006646 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006647 char_u *var_end;
6648 int oplen;
6649 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006650
Bram Moolenaar65821722020-08-02 18:58:54 +02006651 if (ea.cmd[0] == '@')
6652 var_end = ea.cmd + 2;
6653 else
6654 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006655 FNE_CHECK_START | FNE_INCL_BR);
6656 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006657 if (oplen > 0)
6658 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006659 size_t len = p - ea.cmd;
6660
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006661 // Recognize an assignment if we recognize the variable
6662 // name:
6663 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006664 // "local = expr" where "local" is a local var.
6665 // "script = expr" where "script" is a script-local var.
6666 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006667 // "&opt = expr"
6668 // "$ENV = expr"
6669 // "@r = expr"
6670 if (*ea.cmd == '&'
6671 || *ea.cmd == '$'
6672 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006673 || ((len) > 2 && ea.cmd[1] == ':')
6674 || lookup_local(ea.cmd, len, &cctx) != NULL
6675 || lookup_arg(ea.cmd, len, NULL, NULL,
6676 NULL, &cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02006677 || lookup_script(ea.cmd, len, FALSE) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006678 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006679 {
6680 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006681 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006682 goto erret;
6683 continue;
6684 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006685 }
6686 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006687
6688 if (*ea.cmd == '[')
6689 {
6690 // [var, var] = expr
6691 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6692 if (line == NULL)
6693 goto erret;
6694 if (line != ea.cmd)
6695 continue;
6696 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006697 }
6698
6699 /*
6700 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006701 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006702 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006703 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006704 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006705 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006706 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006707 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006708 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006709 if (!starts_with_colon)
6710 {
6711 emsg(_(e_colon_required_before_a_range));
6712 goto erret;
6713 }
6714 if (ends_excmd2(line, ea.cmd))
6715 {
6716 // A range without a command: jump to the line.
6717 // TODO: compile to a more efficient command, possibly
6718 // calling parse_cmd_address().
6719 ea.cmdidx = CMD_SIZE;
6720 line = compile_exec(line, &ea, &cctx);
6721 goto nextline;
6722 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006723 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006724 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006725 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006726 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006727 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006728
6729 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6730 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006731 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006732 {
6733 line += STRLEN(line);
6734 continue;
6735 }
6736
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006737 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006738 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006739 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006740 // CMD_let cannot happen, compile_assignment() above is used
6741 iemsg("Command from find_ex_command() not handled");
6742 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006743 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006744 }
6745
6746 p = skipwhite(p);
6747
Bram Moolenaar3988f642020-08-27 22:43:03 +02006748 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006749 && ea.cmdidx != CMD_elseif
6750 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02006751 && ea.cmdidx != CMD_endif
6752 && ea.cmdidx != CMD_endfor
6753 && ea.cmdidx != CMD_endwhile
6754 && ea.cmdidx != CMD_catch
6755 && ea.cmdidx != CMD_finally
6756 && ea.cmdidx != CMD_endtry)
6757 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02006758 emsg(_(e_unreachable_code_after_return));
6759 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006760 }
6761
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006762 switch (ea.cmdidx)
6763 {
6764 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006765 ea.arg = p;
6766 line = compile_nested_function(&ea, &cctx);
6767 break;
6768
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006769 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006770 // TODO: should we allow this, e.g. to declare a global
6771 // function?
6772 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006773 goto erret;
6774
6775 case CMD_return:
6776 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006777 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006778 break;
6779
6780 case CMD_let:
6781 case CMD_const:
6782 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006783 if (line == p)
6784 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006785 break;
6786
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006787 case CMD_unlet:
6788 case CMD_unlockvar:
6789 case CMD_lockvar:
6790 line = compile_unletlock(p, &ea, &cctx);
6791 break;
6792
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006793 case CMD_import:
6794 line = compile_import(p, &cctx);
6795 break;
6796
6797 case CMD_if:
6798 line = compile_if(p, &cctx);
6799 break;
6800 case CMD_elseif:
6801 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006802 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006803 break;
6804 case CMD_else:
6805 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006806 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006807 break;
6808 case CMD_endif:
6809 line = compile_endif(p, &cctx);
6810 break;
6811
6812 case CMD_while:
6813 line = compile_while(p, &cctx);
6814 break;
6815 case CMD_endwhile:
6816 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006817 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006818 break;
6819
6820 case CMD_for:
6821 line = compile_for(p, &cctx);
6822 break;
6823 case CMD_endfor:
6824 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006825 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006826 break;
6827 case CMD_continue:
6828 line = compile_continue(p, &cctx);
6829 break;
6830 case CMD_break:
6831 line = compile_break(p, &cctx);
6832 break;
6833
6834 case CMD_try:
6835 line = compile_try(p, &cctx);
6836 break;
6837 case CMD_catch:
6838 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006839 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006840 break;
6841 case CMD_finally:
6842 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006843 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006844 break;
6845 case CMD_endtry:
6846 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006847 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006848 break;
6849 case CMD_throw:
6850 line = compile_throw(p, &cctx);
6851 break;
6852
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006853 case CMD_eval:
6854 if (compile_expr0(&p, &cctx) == FAIL)
6855 goto erret;
6856
Bram Moolenaar3988f642020-08-27 22:43:03 +02006857 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006858 generate_instr_drop(&cctx, ISN_DROP, 1);
6859
6860 line = skipwhite(p);
6861 break;
6862
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006863 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006864 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006865 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006866 case CMD_echomsg:
6867 case CMD_echoerr:
6868 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006869 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006870
Bram Moolenaar3988f642020-08-27 22:43:03 +02006871 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006872
Bram Moolenaarae616492020-07-28 20:07:27 +02006873 case CMD_append:
6874 case CMD_change:
6875 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02006876 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02006877 case CMD_xit:
6878 not_in_vim9(&ea);
6879 goto erret;
6880
Bram Moolenaar002262f2020-07-08 17:47:57 +02006881 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02006882 if (cctx.ctx_skip != SKIP_YES)
6883 {
6884 semsg(_(e_invalid_command_str), ea.cmd);
6885 goto erret;
6886 }
6887 // We don't check for a next command here.
6888 line = (char_u *)"";
6889 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02006890
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006891 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02006892 if (cctx.ctx_skip == SKIP_YES)
6893 {
6894 // We don't check for a next command here.
6895 line = (char_u *)"";
6896 }
6897 else
6898 {
6899 // Not recognized, execute with do_cmdline_cmd().
6900 ea.arg = p;
6901 line = compile_exec(line, &ea, &cctx);
6902 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006903 break;
6904 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006905nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006906 if (line == NULL)
6907 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006908 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006909
6910 if (cctx.ctx_type_stack.ga_len < 0)
6911 {
6912 iemsg("Type stack underflow");
6913 goto erret;
6914 }
6915 }
6916
6917 if (cctx.ctx_scope != NULL)
6918 {
6919 if (cctx.ctx_scope->se_type == IF_SCOPE)
6920 emsg(_(e_endif));
6921 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6922 emsg(_(e_endwhile));
6923 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6924 emsg(_(e_endfor));
6925 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006926 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006927 goto erret;
6928 }
6929
Bram Moolenaarefd88552020-06-18 20:50:10 +02006930 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006931 {
6932 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6933 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006934 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006935 goto erret;
6936 }
6937
6938 // Return zero if there is no return at the end.
6939 generate_PUSHNR(&cctx, 0);
6940 generate_instr(&cctx, ISN_RETURN);
6941 }
6942
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006943 {
6944 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6945 + ufunc->uf_dfunc_idx;
6946 dfunc->df_deleted = FALSE;
6947 dfunc->df_instr = instr->ga_data;
6948 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006949 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006950 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006951 if (cctx.ctx_outer_used)
6952 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006953 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006954 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006955
6956 ret = OK;
6957
6958erret:
6959 if (ret == FAIL)
6960 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006961 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006962 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6963 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006964
6965 for (idx = 0; idx < instr->ga_len; ++idx)
6966 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006967 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006968
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006969 // If using the last entry in the table and it was added above, we
6970 // might as well remove it.
6971 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02006972 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006973 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006974 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006975 ufunc->uf_dfunc_idx = 0;
6976 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006977 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006978
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006979 while (cctx.ctx_scope != NULL)
6980 drop_scope(&cctx);
6981
Bram Moolenaar20431c92020-03-20 18:39:46 +01006982 // Don't execute this function body.
6983 ga_clear_strings(&ufunc->uf_lines);
6984
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006985 if (errormsg != NULL)
6986 emsg(errormsg);
6987 else if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006988 emsg(_(e_compile_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006989 }
6990
6991 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006992 if (do_estack_push)
6993 estack_pop();
6994
Bram Moolenaar20431c92020-03-20 18:39:46 +01006995 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006996 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006997 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02006998 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006999}
7000
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007001 void
7002set_function_type(ufunc_T *ufunc)
7003{
7004 int varargs = ufunc->uf_va_name != NULL;
7005 int argcount = ufunc->uf_args.ga_len;
7006
7007 // Create a type for the function, with the return type and any
7008 // argument types.
7009 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7010 // The type is included in "tt_args".
7011 if (argcount > 0 || varargs)
7012 {
7013 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7014 argcount, &ufunc->uf_type_list);
7015 // Add argument types to the function type.
7016 if (func_type_add_arg_types(ufunc->uf_func_type,
7017 argcount + varargs,
7018 &ufunc->uf_type_list) == FAIL)
7019 return;
7020 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7021 ufunc->uf_func_type->tt_min_argcount =
7022 argcount - ufunc->uf_def_args.ga_len;
7023 if (ufunc->uf_arg_types == NULL)
7024 {
7025 int i;
7026
7027 // lambda does not have argument types.
7028 for (i = 0; i < argcount; ++i)
7029 ufunc->uf_func_type->tt_args[i] = &t_any;
7030 }
7031 else
7032 mch_memmove(ufunc->uf_func_type->tt_args,
7033 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7034 if (varargs)
7035 {
7036 ufunc->uf_func_type->tt_args[argcount] =
7037 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7038 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7039 }
7040 }
7041 else
7042 // No arguments, can use a predefined type.
7043 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7044 argcount, &ufunc->uf_type_list);
7045}
7046
7047
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007048/*
7049 * Delete an instruction, free what it contains.
7050 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007051 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007052delete_instr(isn_T *isn)
7053{
7054 switch (isn->isn_type)
7055 {
7056 case ISN_EXEC:
7057 case ISN_LOADENV:
7058 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007059 case ISN_LOADB:
7060 case ISN_LOADW:
7061 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007062 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007063 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007064 case ISN_PUSHEXC:
7065 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007066 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007067 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007068 case ISN_STOREB:
7069 case ISN_STOREW:
7070 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007071 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007072 vim_free(isn->isn_arg.string);
7073 break;
7074
7075 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007076 case ISN_STORES:
7077 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007078 break;
7079
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007080 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007081 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007082 vim_free(isn->isn_arg.unlet.ul_name);
7083 break;
7084
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007085 case ISN_STOREOPT:
7086 vim_free(isn->isn_arg.storeopt.so_name);
7087 break;
7088
7089 case ISN_PUSHBLOB: // push blob isn_arg.blob
7090 blob_unref(isn->isn_arg.blob);
7091 break;
7092
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007093 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007094#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007095 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007096#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007097 break;
7098
7099 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007100#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007101 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007102#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007103 break;
7104
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007105 case ISN_UCALL:
7106 vim_free(isn->isn_arg.ufunc.cuf_name);
7107 break;
7108
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007109 case ISN_FUNCREF:
7110 {
7111 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7112 + isn->isn_arg.funcref.fr_func;
7113 func_ptr_unref(dfunc->df_ufunc);
7114 }
7115 break;
7116
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007117 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007118 {
7119 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7120 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7121
7122 if (ufunc != NULL)
7123 {
7124 // Clear uf_dfunc_idx so that the function is deleted.
7125 clear_def_function(ufunc);
7126 ufunc->uf_dfunc_idx = 0;
7127 func_ptr_unref(ufunc);
7128 }
7129
7130 vim_free(lambda);
7131 vim_free(isn->isn_arg.newfunc.nf_global);
7132 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007133 break;
7134
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007135 case ISN_2BOOL:
7136 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007137 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007138 case ISN_ADDBLOB:
7139 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007140 case ISN_ANYINDEX:
7141 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007142 case ISN_BCALL:
7143 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007144 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007145 case ISN_CHECKNR:
7146 case ISN_CHECKTYPE:
7147 case ISN_COMPAREANY:
7148 case ISN_COMPAREBLOB:
7149 case ISN_COMPAREBOOL:
7150 case ISN_COMPAREDICT:
7151 case ISN_COMPAREFLOAT:
7152 case ISN_COMPAREFUNC:
7153 case ISN_COMPARELIST:
7154 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007155 case ISN_COMPARESPECIAL:
7156 case ISN_COMPARESTRING:
7157 case ISN_CONCAT:
7158 case ISN_DCALL:
7159 case ISN_DROP:
7160 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007161 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007162 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007163 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007164 case ISN_EXECCONCAT:
7165 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007166 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007167 case ISN_GETITEM:
7168 case ISN_JUMP:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007169 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007170 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007171 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007172 case ISN_LOADBDICT:
7173 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007174 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007175 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007176 case ISN_LOADSCRIPT:
7177 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007178 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007179 case ISN_LOADWDICT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007180 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007181 case ISN_NEGATENR:
7182 case ISN_NEWDICT:
7183 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007184 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007185 case ISN_OPFLOAT:
7186 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007187 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007188 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007189 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007190 case ISN_PUSHF:
7191 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007192 case ISN_PUSHSPEC:
7193 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007194 case ISN_SHUFFLE:
7195 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007196 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007197 case ISN_STOREDICT:
7198 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007199 case ISN_STORENR:
7200 case ISN_STOREOUTER:
7201 case ISN_STOREREG:
7202 case ISN_STORESCRIPT:
7203 case ISN_STOREV:
7204 case ISN_STRINDEX:
7205 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007206 case ISN_THROW:
7207 case ISN_TRY:
7208 // nothing allocated
7209 break;
7210 }
7211}
7212
7213/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007214 * Free all instructions for "dfunc".
7215 */
7216 static void
7217delete_def_function_contents(dfunc_T *dfunc)
7218{
7219 int idx;
7220
7221 ga_clear(&dfunc->df_def_args_isn);
7222
7223 if (dfunc->df_instr != NULL)
7224 {
7225 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7226 delete_instr(dfunc->df_instr + idx);
7227 VIM_CLEAR(dfunc->df_instr);
7228 }
7229
7230 dfunc->df_deleted = TRUE;
7231}
7232
7233/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007234 * When a user function is deleted, clear the contents of any associated def
7235 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007236 */
7237 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007238clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007239{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007240 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007241 {
7242 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7243 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007244
Bram Moolenaar20431c92020-03-20 18:39:46 +01007245 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007246 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007247 }
7248}
7249
7250#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007251/*
7252 * Free all functions defined with ":def".
7253 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007254 void
7255free_def_functions(void)
7256{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007257 int idx;
7258
7259 for (idx = 0; idx < def_functions.ga_len; ++idx)
7260 {
7261 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7262
7263 delete_def_function_contents(dfunc);
7264 }
7265
7266 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007267}
7268#endif
7269
7270
7271#endif // FEAT_EVAL