blob: f88f3a3df2885cc9ad84afcb9c1ccb33b9d53479 [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{
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200732 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200733 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;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200754 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100755
Bram Moolenaar080457c2020-03-03 21:53:32 +0100756 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100757 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
758 return FAIL;
759 isn->isn_arg.number = number;
760
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200761 if (number == 0 || number == 1)
762 {
763 type_T *type = alloc_type(cctx->ctx_type_list);
764
765 // A 0 or 1 number can also be used as a bool.
766 if (type != NULL)
767 {
768 type->tt_type = VAR_NUMBER;
769 type->tt_flags = TTFLAG_BOOL_OK;
770 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
771 }
772 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100773 return OK;
774}
775
776/*
777 * Generate an ISN_PUSHBOOL instruction.
778 */
779 static int
780generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
781{
782 isn_T *isn;
783
Bram Moolenaar080457c2020-03-03 21:53:32 +0100784 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100785 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
786 return FAIL;
787 isn->isn_arg.number = number;
788
789 return OK;
790}
791
792/*
793 * Generate an ISN_PUSHSPEC instruction.
794 */
795 static int
796generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
797{
798 isn_T *isn;
799
Bram Moolenaar080457c2020-03-03 21:53:32 +0100800 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100801 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
802 return FAIL;
803 isn->isn_arg.number = number;
804
805 return OK;
806}
807
808#ifdef FEAT_FLOAT
809/*
810 * Generate an ISN_PUSHF instruction.
811 */
812 static int
813generate_PUSHF(cctx_T *cctx, float_T fnumber)
814{
815 isn_T *isn;
816
Bram Moolenaar080457c2020-03-03 21:53:32 +0100817 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100818 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
819 return FAIL;
820 isn->isn_arg.fnumber = fnumber;
821
822 return OK;
823}
824#endif
825
826/*
827 * Generate an ISN_PUSHS instruction.
828 * Consumes "str".
829 */
830 static int
831generate_PUSHS(cctx_T *cctx, char_u *str)
832{
833 isn_T *isn;
834
Bram Moolenaar080457c2020-03-03 21:53:32 +0100835 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100836 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
837 return FAIL;
838 isn->isn_arg.string = str;
839
840 return OK;
841}
842
843/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100844 * Generate an ISN_PUSHCHANNEL instruction.
845 * Consumes "channel".
846 */
847 static int
848generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
849{
850 isn_T *isn;
851
Bram Moolenaar080457c2020-03-03 21:53:32 +0100852 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100853 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
854 return FAIL;
855 isn->isn_arg.channel = channel;
856
857 return OK;
858}
859
860/*
861 * Generate an ISN_PUSHJOB instruction.
862 * Consumes "job".
863 */
864 static int
865generate_PUSHJOB(cctx_T *cctx, job_T *job)
866{
867 isn_T *isn;
868
Bram Moolenaar080457c2020-03-03 21:53:32 +0100869 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100870 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100871 return FAIL;
872 isn->isn_arg.job = job;
873
874 return OK;
875}
876
877/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100878 * Generate an ISN_PUSHBLOB instruction.
879 * Consumes "blob".
880 */
881 static int
882generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
883{
884 isn_T *isn;
885
Bram Moolenaar080457c2020-03-03 21:53:32 +0100886 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
888 return FAIL;
889 isn->isn_arg.blob = blob;
890
891 return OK;
892}
893
894/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100895 * Generate an ISN_PUSHFUNC instruction with name "name".
896 * Consumes "name".
897 */
898 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200899generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100900{
901 isn_T *isn;
902
Bram Moolenaar080457c2020-03-03 21:53:32 +0100903 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200904 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100905 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200906 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100907
908 return OK;
909}
910
911/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200912 * Generate an ISN_GETITEM instruction with "index".
913 */
914 static int
915generate_GETITEM(cctx_T *cctx, int index)
916{
917 isn_T *isn;
918 garray_T *stack = &cctx->ctx_type_stack;
919 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
920 type_T *item_type = &t_any;
921
922 RETURN_OK_IF_SKIP(cctx);
923
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200924 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200925 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200926 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200927 emsg(_(e_listreq));
928 return FAIL;
929 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200930 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200931 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
932 return FAIL;
933 isn->isn_arg.number = index;
934
935 // add the item type to the type stack
936 if (ga_grow(stack, 1) == FAIL)
937 return FAIL;
938 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
939 ++stack->ga_len;
940 return OK;
941}
942
943/*
Bram Moolenaar9af78762020-06-16 11:34:42 +0200944 * Generate an ISN_SLICE instruction with "count".
945 */
946 static int
947generate_SLICE(cctx_T *cctx, int count)
948{
949 isn_T *isn;
950
951 RETURN_OK_IF_SKIP(cctx);
952 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
953 return FAIL;
954 isn->isn_arg.number = count;
955 return OK;
956}
957
958/*
959 * Generate an ISN_CHECKLEN instruction with "min_len".
960 */
961 static int
962generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
963{
964 isn_T *isn;
965
966 RETURN_OK_IF_SKIP(cctx);
967
968 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
969 return FAIL;
970 isn->isn_arg.checklen.cl_min_len = min_len;
971 isn->isn_arg.checklen.cl_more_OK = more_OK;
972
973 return OK;
974}
975
976/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100977 * Generate an ISN_STORE instruction.
978 */
979 static int
980generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
981{
982 isn_T *isn;
983
Bram Moolenaar080457c2020-03-03 21:53:32 +0100984 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
986 return FAIL;
987 if (name != NULL)
988 isn->isn_arg.string = vim_strsave(name);
989 else
990 isn->isn_arg.number = idx;
991
992 return OK;
993}
994
995/*
996 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
997 */
998 static int
999generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1000{
1001 isn_T *isn;
1002
Bram Moolenaar080457c2020-03-03 21:53:32 +01001003 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001004 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1005 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001006 isn->isn_arg.storenr.stnr_idx = idx;
1007 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001008
1009 return OK;
1010}
1011
1012/*
1013 * Generate an ISN_STOREOPT instruction
1014 */
1015 static int
1016generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1017{
1018 isn_T *isn;
1019
Bram Moolenaar080457c2020-03-03 21:53:32 +01001020 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001021 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001022 return FAIL;
1023 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1024 isn->isn_arg.storeopt.so_flags = opt_flags;
1025
1026 return OK;
1027}
1028
1029/*
1030 * Generate an ISN_LOAD or similar instruction.
1031 */
1032 static int
1033generate_LOAD(
1034 cctx_T *cctx,
1035 isntype_T isn_type,
1036 int idx,
1037 char_u *name,
1038 type_T *type)
1039{
1040 isn_T *isn;
1041
Bram Moolenaar080457c2020-03-03 21:53:32 +01001042 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001043 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1044 return FAIL;
1045 if (name != NULL)
1046 isn->isn_arg.string = vim_strsave(name);
1047 else
1048 isn->isn_arg.number = idx;
1049
1050 return OK;
1051}
1052
1053/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001054 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001055 */
1056 static int
1057generate_LOADV(
1058 cctx_T *cctx,
1059 char_u *name,
1060 int error)
1061{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001062 int di_flags;
1063 int vidx = find_vim_var(name, &di_flags);
1064 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001065
Bram Moolenaar080457c2020-03-03 21:53:32 +01001066 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001067 if (vidx < 0)
1068 {
1069 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001070 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001071 return FAIL;
1072 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001073 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001074
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001075 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001076}
1077
1078/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001079 * Generate an ISN_UNLET instruction.
1080 */
1081 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001082generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001083{
1084 isn_T *isn;
1085
1086 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001087 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001088 return FAIL;
1089 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1090 isn->isn_arg.unlet.ul_forceit = forceit;
1091
1092 return OK;
1093}
1094
1095/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096 * Generate an ISN_LOADS instruction.
1097 */
1098 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001099generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001100 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001101 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001102 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001103 int sid,
1104 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001105{
1106 isn_T *isn;
1107
Bram Moolenaar080457c2020-03-03 21:53:32 +01001108 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001109 if (isn_type == ISN_LOADS)
1110 isn = generate_instr_type(cctx, isn_type, type);
1111 else
1112 isn = generate_instr_drop(cctx, isn_type, 1);
1113 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001114 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001115 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1116 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001117
1118 return OK;
1119}
1120
1121/*
1122 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1123 */
1124 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001125generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001126 cctx_T *cctx,
1127 isntype_T isn_type,
1128 int sid,
1129 int idx,
1130 type_T *type)
1131{
1132 isn_T *isn;
1133
Bram Moolenaar080457c2020-03-03 21:53:32 +01001134 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135 if (isn_type == ISN_LOADSCRIPT)
1136 isn = generate_instr_type(cctx, isn_type, type);
1137 else
1138 isn = generate_instr_drop(cctx, isn_type, 1);
1139 if (isn == NULL)
1140 return FAIL;
1141 isn->isn_arg.script.script_sid = sid;
1142 isn->isn_arg.script.script_idx = idx;
1143 return OK;
1144}
1145
1146/*
1147 * Generate an ISN_NEWLIST instruction.
1148 */
1149 static int
1150generate_NEWLIST(cctx_T *cctx, int count)
1151{
1152 isn_T *isn;
1153 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001154 type_T *type;
1155 type_T *member;
1156
Bram Moolenaar080457c2020-03-03 21:53:32 +01001157 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001158 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1159 return FAIL;
1160 isn->isn_arg.number = count;
1161
Bram Moolenaar127542b2020-08-09 17:22:04 +02001162 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001163 if (count == 0)
1164 member = &t_void;
1165 else
1166 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001167 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1168 cctx->ctx_type_list);
1169 type = get_list_type(member, cctx->ctx_type_list);
1170
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171 // drop the value types
1172 stack->ga_len -= count;
1173
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001174 // add the list type to the type stack
1175 if (ga_grow(stack, 1) == FAIL)
1176 return FAIL;
1177 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1178 ++stack->ga_len;
1179
1180 return OK;
1181}
1182
1183/*
1184 * Generate an ISN_NEWDICT instruction.
1185 */
1186 static int
1187generate_NEWDICT(cctx_T *cctx, int count)
1188{
1189 isn_T *isn;
1190 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001191 type_T *type;
1192 type_T *member;
1193
Bram Moolenaar080457c2020-03-03 21:53:32 +01001194 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001195 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1196 return FAIL;
1197 isn->isn_arg.number = count;
1198
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001199 if (count == 0)
1200 member = &t_void;
1201 else
1202 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001203 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1204 cctx->ctx_type_list);
1205 type = get_dict_type(member, cctx->ctx_type_list);
1206
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001207 // drop the key and value types
1208 stack->ga_len -= 2 * count;
1209
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001210 // add the dict type to the type stack
1211 if (ga_grow(stack, 1) == FAIL)
1212 return FAIL;
1213 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1214 ++stack->ga_len;
1215
1216 return OK;
1217}
1218
1219/*
1220 * Generate an ISN_FUNCREF instruction.
1221 */
1222 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001223generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001224{
1225 isn_T *isn;
1226 garray_T *stack = &cctx->ctx_type_stack;
1227
Bram Moolenaar080457c2020-03-03 21:53:32 +01001228 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001229 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1230 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001231 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001232 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001233
1234 if (ga_grow(stack, 1) == FAIL)
1235 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001236 ((type_T **)stack->ga_data)[stack->ga_len] =
1237 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001238 ++stack->ga_len;
1239
1240 return OK;
1241}
1242
1243/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001244 * Generate an ISN_NEWFUNC instruction.
1245 */
1246 static int
1247generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1248{
1249 isn_T *isn;
1250 char_u *name;
1251
1252 RETURN_OK_IF_SKIP(cctx);
1253 name = vim_strsave(lambda_name);
1254 if (name == NULL)
1255 return FAIL;
1256 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1257 return FAIL;
1258 isn->isn_arg.newfunc.nf_lambda = name;
1259 isn->isn_arg.newfunc.nf_global = func_name;
1260
1261 return OK;
1262}
1263
1264/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001265 * Generate an ISN_JUMP instruction.
1266 */
1267 static int
1268generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1269{
1270 isn_T *isn;
1271 garray_T *stack = &cctx->ctx_type_stack;
1272
Bram Moolenaar080457c2020-03-03 21:53:32 +01001273 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001274 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1275 return FAIL;
1276 isn->isn_arg.jump.jump_when = when;
1277 isn->isn_arg.jump.jump_where = where;
1278
1279 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1280 --stack->ga_len;
1281
1282 return OK;
1283}
1284
1285 static int
1286generate_FOR(cctx_T *cctx, int loop_idx)
1287{
1288 isn_T *isn;
1289 garray_T *stack = &cctx->ctx_type_stack;
1290
Bram Moolenaar080457c2020-03-03 21:53:32 +01001291 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001292 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1293 return FAIL;
1294 isn->isn_arg.forloop.for_idx = loop_idx;
1295
1296 if (ga_grow(stack, 1) == FAIL)
1297 return FAIL;
1298 // type doesn't matter, will be stored next
1299 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1300 ++stack->ga_len;
1301
1302 return OK;
1303}
1304
1305/*
1306 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001307 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001308 * Return FAIL if the number of arguments is wrong.
1309 */
1310 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001311generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312{
1313 isn_T *isn;
1314 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001315 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001316 type_T *argtypes[MAX_FUNC_ARGS];
1317 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318
Bram Moolenaar080457c2020-03-03 21:53:32 +01001319 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001320 argoff = check_internal_func(func_idx, argcount);
1321 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001322 return FAIL;
1323
Bram Moolenaar389df252020-07-09 21:20:47 +02001324 if (method_call && argoff > 1)
1325 {
1326 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1327 return FAIL;
1328 isn->isn_arg.shuffle.shfl_item = argcount;
1329 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1330 }
1331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001332 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1333 return FAIL;
1334 isn->isn_arg.bfunc.cbf_idx = func_idx;
1335 isn->isn_arg.bfunc.cbf_argcount = argcount;
1336
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001337 for (i = 0; i < argcount; ++i)
1338 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1339
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 stack->ga_len -= argcount; // drop the arguments
1341 if (ga_grow(stack, 1) == FAIL)
1342 return FAIL;
1343 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001344 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 ++stack->ga_len; // add return value
1346
1347 return OK;
1348}
1349
1350/*
1351 * Generate an ISN_DCALL or ISN_UCALL instruction.
1352 * Return FAIL if the number of arguments is wrong.
1353 */
1354 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001355generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356{
1357 isn_T *isn;
1358 garray_T *stack = &cctx->ctx_type_stack;
1359 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001360 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361
Bram Moolenaar080457c2020-03-03 21:53:32 +01001362 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363 if (argcount > regular_args && !has_varargs(ufunc))
1364 {
1365 semsg(_(e_toomanyarg), ufunc->uf_name);
1366 return FAIL;
1367 }
1368 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1369 {
1370 semsg(_(e_toofewarg), ufunc->uf_name);
1371 return FAIL;
1372 }
1373
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001374 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001375 {
1376 int i;
1377
1378 for (i = 0; i < argcount; ++i)
1379 {
1380 type_T *expected;
1381 type_T *actual;
1382
1383 if (i < regular_args)
1384 {
1385 if (ufunc->uf_arg_types == NULL)
1386 continue;
1387 expected = ufunc->uf_arg_types[i];
1388 }
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001389 else if (ufunc->uf_va_type == NULL)
1390 // possibly a lambda
1391 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001392 else
1393 expected = ufunc->uf_va_type->tt_member;
1394 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001395 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001396 {
1397 arg_type_mismatch(expected, actual, i + 1);
1398 return FAIL;
1399 }
1400 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001401 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001402 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1403 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001404 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001405 }
1406
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001408 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001409 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001410 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001411 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412 {
1413 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1414 isn->isn_arg.dfunc.cdf_argcount = argcount;
1415 }
1416 else
1417 {
1418 // A user function may be deleted and redefined later, can't use the
1419 // ufunc pointer, need to look it up again at runtime.
1420 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1421 isn->isn_arg.ufunc.cuf_argcount = argcount;
1422 }
1423
1424 stack->ga_len -= argcount; // drop the arguments
1425 if (ga_grow(stack, 1) == FAIL)
1426 return FAIL;
1427 // add return value
1428 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1429 ++stack->ga_len;
1430
1431 return OK;
1432}
1433
1434/*
1435 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1436 */
1437 static int
1438generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1439{
1440 isn_T *isn;
1441 garray_T *stack = &cctx->ctx_type_stack;
1442
Bram Moolenaar080457c2020-03-03 21:53:32 +01001443 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1445 return FAIL;
1446 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1447 isn->isn_arg.ufunc.cuf_argcount = argcount;
1448
1449 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001450 if (ga_grow(stack, 1) == FAIL)
1451 return FAIL;
1452 // add return value
1453 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1454 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455
1456 return OK;
1457}
1458
1459/*
1460 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001461 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001462 */
1463 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001464generate_PCALL(
1465 cctx_T *cctx,
1466 int argcount,
1467 char_u *name,
1468 type_T *type,
1469 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470{
1471 isn_T *isn;
1472 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001473 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001474
Bram Moolenaar080457c2020-03-03 21:53:32 +01001475 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001476
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001477 if (type->tt_type == VAR_ANY)
1478 ret_type = &t_any;
1479 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001480 {
1481 if (type->tt_argcount != -1)
1482 {
1483 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1484
1485 if (argcount < type->tt_min_argcount - varargs)
1486 {
1487 semsg(_(e_toofewarg), "[reference]");
1488 return FAIL;
1489 }
1490 if (!varargs && argcount > type->tt_argcount)
1491 {
1492 semsg(_(e_toomanyarg), "[reference]");
1493 return FAIL;
1494 }
1495 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001496 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001497 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001498 else
1499 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001500 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001501 return FAIL;
1502 }
1503
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1505 return FAIL;
1506 isn->isn_arg.pfunc.cpf_top = at_top;
1507 isn->isn_arg.pfunc.cpf_argcount = argcount;
1508
1509 stack->ga_len -= argcount; // drop the arguments
1510
1511 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001512 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001513
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001514 // If partial is above the arguments it must be cleared and replaced with
1515 // the return value.
1516 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1517 return FAIL;
1518
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001519 return OK;
1520}
1521
1522/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001523 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001524 */
1525 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001526generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001527{
1528 isn_T *isn;
1529 garray_T *stack = &cctx->ctx_type_stack;
1530 type_T *type;
1531
Bram Moolenaar080457c2020-03-03 21:53:32 +01001532 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001533 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001534 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001535 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001537 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001538 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001539 if (type->tt_type != VAR_DICT && type != &t_any)
1540 {
1541 emsg(_(e_dictreq));
1542 return FAIL;
1543 }
1544 // change dict type to dict member type
1545 if (type->tt_type == VAR_DICT)
1546 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001547
1548 return OK;
1549}
1550
1551/*
1552 * Generate an ISN_ECHO instruction.
1553 */
1554 static int
1555generate_ECHO(cctx_T *cctx, int with_white, int count)
1556{
1557 isn_T *isn;
1558
Bram Moolenaar080457c2020-03-03 21:53:32 +01001559 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1561 return FAIL;
1562 isn->isn_arg.echo.echo_with_white = with_white;
1563 isn->isn_arg.echo.echo_count = count;
1564
1565 return OK;
1566}
1567
Bram Moolenaarad39c092020-02-26 18:23:43 +01001568/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001569 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001570 */
1571 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001572generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001573{
1574 isn_T *isn;
1575
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001576 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001577 return FAIL;
1578 isn->isn_arg.number = count;
1579
1580 return OK;
1581}
1582
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001583/*
1584 * Generate an ISN_PUT instruction.
1585 */
1586 static int
1587generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1588{
1589 isn_T *isn;
1590
1591 RETURN_OK_IF_SKIP(cctx);
1592 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1593 return FAIL;
1594 isn->isn_arg.put.put_regname = regname;
1595 isn->isn_arg.put.put_lnum = lnum;
1596 return OK;
1597}
1598
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599 static int
1600generate_EXEC(cctx_T *cctx, char_u *line)
1601{
1602 isn_T *isn;
1603
Bram Moolenaar080457c2020-03-03 21:53:32 +01001604 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1606 return FAIL;
1607 isn->isn_arg.string = vim_strsave(line);
1608 return OK;
1609}
1610
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001611 static int
1612generate_EXECCONCAT(cctx_T *cctx, int count)
1613{
1614 isn_T *isn;
1615
1616 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1617 return FAIL;
1618 isn->isn_arg.number = count;
1619 return OK;
1620}
1621
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622/*
1623 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001624 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001625 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001626 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001627reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1628{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629 lvar_T *lvar;
1630
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001631 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001632 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001633 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001634 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001635 }
1636
1637 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001638 return NULL;
1639 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001641 // Every local variable uses the next entry on the stack. We could re-use
1642 // the last ones when leaving a scope, but then variables used in a closure
1643 // might get overwritten. To keep things simple do not re-use stack
1644 // entries. This is less efficient, but memory is cheap these days.
1645 lvar->lv_idx = cctx->ctx_locals_count++;
1646
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001647 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001648 lvar->lv_const = isConst;
1649 lvar->lv_type = type;
1650
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001651 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652}
1653
1654/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001655 * Remove local variables above "new_top".
1656 */
1657 static void
1658unwind_locals(cctx_T *cctx, int new_top)
1659{
1660 if (cctx->ctx_locals.ga_len > new_top)
1661 {
1662 int idx;
1663 lvar_T *lvar;
1664
1665 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1666 {
1667 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1668 vim_free(lvar->lv_name);
1669 }
1670 }
1671 cctx->ctx_locals.ga_len = new_top;
1672}
1673
1674/*
1675 * Free all local variables.
1676 */
1677 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001678free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001679{
1680 unwind_locals(cctx, 0);
1681 ga_clear(&cctx->ctx_locals);
1682}
1683
1684/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001685 * Find "name" in script-local items of script "sid".
1686 * Returns the index in "sn_var_vals" if found.
1687 * If found but not in "sn_var_vals" returns -1.
1688 * If not found returns -2.
1689 */
1690 int
1691get_script_item_idx(int sid, char_u *name, int check_writable)
1692{
1693 hashtab_T *ht;
1694 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001695 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001696 int idx;
1697
1698 // First look the name up in the hashtable.
Bram Moolenaare3d46852020-08-29 13:39:17 +02001699 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700 return -1;
1701 ht = &SCRIPT_VARS(sid);
1702 di = find_var_in_ht(ht, 0, name, TRUE);
1703 if (di == NULL)
1704 return -2;
1705
1706 // Now find the svar_T index in sn_var_vals.
1707 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1708 {
1709 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1710
1711 if (sv->sv_tv == &di->di_tv)
1712 {
1713 if (check_writable && sv->sv_const)
1714 semsg(_(e_readonlyvar), name);
1715 return idx;
1716 }
1717 }
1718 return -1;
1719}
1720
1721/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001722 * Find "name" in imported items of the current script or in "cctx" if not
1723 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001724 */
1725 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001726find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728 int idx;
1729
Bram Moolenaare3d46852020-08-29 13:39:17 +02001730 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001731 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001732 if (cctx != NULL)
1733 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1734 {
1735 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1736 + idx;
1737
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001738 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1739 : STRLEN(import->imp_name) == len
1740 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741 return import;
1742 }
1743
Bram Moolenaarefa94442020-08-08 22:16:00 +02001744 return find_imported_in_script(name, len, current_sctx.sc_sid);
1745}
1746
1747 imported_T *
1748find_imported_in_script(char_u *name, size_t len, int sid)
1749{
Bram Moolenaare3d46852020-08-29 13:39:17 +02001750 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001751 int idx;
1752
Bram Moolenaare3d46852020-08-29 13:39:17 +02001753 if (!SCRIPT_ID_VALID(sid))
1754 return NULL;
1755 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1757 {
1758 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1759
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001760 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1761 : STRLEN(import->imp_name) == len
1762 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001763 return import;
1764 }
1765 return NULL;
1766}
1767
1768/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001769 * Free all imported variables.
1770 */
1771 static void
1772free_imported(cctx_T *cctx)
1773{
1774 int idx;
1775
1776 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1777 {
1778 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1779
1780 vim_free(import->imp_name);
1781 }
1782 ga_clear(&cctx->ctx_imports);
1783}
1784
1785/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02001786 * Return TRUE if "p" points at a "#" but not at "#{".
1787 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001788 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001789vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001790{
1791 return p[0] == '#' && p[1] != '{';
1792}
1793
1794/*
1795 * Return a pointer to the next line that isn't empty or only contains a
1796 * comment. Skips over white space.
1797 * Returns NULL if there is none.
1798 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001799 char_u *
1800peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001801{
1802 int lnum = cctx->ctx_lnum;
1803
1804 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
1805 {
1806 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001807 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001808
Bram Moolenaarba60cc42020-08-12 19:15:33 +02001809 // ignore NULLs inserted for continuation lines
1810 if (line != NULL)
1811 {
1812 p = skipwhite(line);
1813 if (*p != NUL && !vim9_comment_start(p))
1814 return p;
1815 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02001816 }
1817 return NULL;
1818}
1819
1820/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001821 * Called when checking for a following operator at "arg". When the rest of
1822 * the line is empty or only a comment, peek the next line. If there is a next
1823 * line return a pointer to it and set "nextp".
1824 * Otherwise skip over white space.
1825 */
1826 static char_u *
1827may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
1828{
1829 char_u *p = skipwhite(arg);
1830
1831 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001832 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001833 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001834 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001835 if (*nextp != NULL)
1836 return *nextp;
1837 }
1838 return p;
1839}
1840
1841/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02001842 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02001843 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02001844 * Returns NULL when at the end.
1845 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001846 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02001847next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02001848{
Bram Moolenaar7a092242020-04-16 22:10:49 +02001849 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001850
1851 do
1852 {
1853 ++cctx->ctx_lnum;
1854 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02001855 {
1856 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001857 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02001858 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02001859 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02001860 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001861 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001862 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02001863 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02001864 return line;
1865}
1866
1867/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001868 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02001869 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001870 * Return FAIL if beyond the last line, "*arg" is unmodified then.
1871 */
1872 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02001873may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001874{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001875 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001876 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02001877 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001878
1879 if (next == NULL)
1880 return FAIL;
1881 *arg = skipwhite(next);
1882 }
1883 return OK;
1884}
1885
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001886/*
1887 * Idem, and give an error when failed.
1888 */
1889 static int
1890may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
1891{
1892 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1893 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001894 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001895 return FAIL;
1896 }
1897 return OK;
1898}
1899
1900
Bram Moolenaara5565e42020-05-09 15:44:01 +02001901// Structure passed between the compile_expr* functions to keep track of
1902// constants that have been parsed but for which no code was produced yet. If
1903// possible expressions on these constants are applied at compile time. If
1904// that is not possible, the code to push the constants needs to be generated
1905// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02001906// Using 50 should be more than enough of 5 levels of ().
1907#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02001908typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02001909 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02001910 int pp_used; // active entries in pp_tv[]
1911} ppconst_T;
1912
Bram Moolenaar1c747212020-05-09 18:28:34 +02001913static int compile_expr0(char_u **arg, cctx_T *cctx);
1914static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1915
Bram Moolenaara5565e42020-05-09 15:44:01 +02001916/*
1917 * Generate a PUSH instruction for "tv".
1918 * "tv" will be consumed or cleared.
1919 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
1920 */
1921 static int
1922generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
1923{
1924 if (tv != NULL)
1925 {
1926 switch (tv->v_type)
1927 {
1928 case VAR_UNKNOWN:
1929 break;
1930 case VAR_BOOL:
1931 generate_PUSHBOOL(cctx, tv->vval.v_number);
1932 break;
1933 case VAR_SPECIAL:
1934 generate_PUSHSPEC(cctx, tv->vval.v_number);
1935 break;
1936 case VAR_NUMBER:
1937 generate_PUSHNR(cctx, tv->vval.v_number);
1938 break;
1939#ifdef FEAT_FLOAT
1940 case VAR_FLOAT:
1941 generate_PUSHF(cctx, tv->vval.v_float);
1942 break;
1943#endif
1944 case VAR_BLOB:
1945 generate_PUSHBLOB(cctx, tv->vval.v_blob);
1946 tv->vval.v_blob = NULL;
1947 break;
1948 case VAR_STRING:
1949 generate_PUSHS(cctx, tv->vval.v_string);
1950 tv->vval.v_string = NULL;
1951 break;
1952 default:
1953 iemsg("constant type not supported");
1954 clear_tv(tv);
1955 return FAIL;
1956 }
1957 tv->v_type = VAR_UNKNOWN;
1958 }
1959 return OK;
1960}
1961
1962/*
1963 * Generate code for any ppconst entries.
1964 */
1965 static int
1966generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
1967{
1968 int i;
1969 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001970 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001971
Bram Moolenaar9b68c822020-06-18 19:31:08 +02001972 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001973 for (i = 0; i < ppconst->pp_used; ++i)
1974 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
1975 ret = FAIL;
1976 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001977 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001978 return ret;
1979}
1980
1981/*
1982 * Clear ppconst constants. Used when failing.
1983 */
1984 static void
1985clear_ppconst(ppconst_T *ppconst)
1986{
1987 int i;
1988
1989 for (i = 0; i < ppconst->pp_used; ++i)
1990 clear_tv(&ppconst->pp_tv[i]);
1991 ppconst->pp_used = 0;
1992}
1993
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001994/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001995 * Generate an instruction to load script-local variable "name", without the
1996 * leading "s:".
1997 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001998 */
1999 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002000compile_load_scriptvar(
2001 cctx_T *cctx,
2002 char_u *name, // variable NUL terminated
2003 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002004 char_u **end, // end of variable
2005 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002007 scriptitem_T *si;
2008 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002009 imported_T *import;
2010
Bram Moolenaare3d46852020-08-29 13:39:17 +02002011 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2012 return FAIL;
2013 si = SCRIPT_ITEM(current_sctx.sc_sid);
2014 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002015 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002017 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002018 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2019 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002020 }
2021 if (idx >= 0)
2022 {
2023 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2024
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002025 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002026 current_sctx.sc_sid, idx, sv->sv_type);
2027 return OK;
2028 }
2029
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002030 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 if (import != NULL)
2032 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002033 if (import->imp_all)
2034 {
2035 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002036 char_u *exp_name;
2037 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002038 ufunc_T *ufunc;
2039 type_T *type;
2040
2041 // Used "import * as Name", need to lookup the member.
2042 if (*p != '.')
2043 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002044 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002045 return FAIL;
2046 }
2047 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002048 if (VIM_ISWHITE(*p))
2049 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002050 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002051 return FAIL;
2052 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002053
Bram Moolenaar1c991142020-07-04 13:15:31 +02002054 // isolate one name
2055 exp_name = p;
2056 while (eval_isnamec(*p))
2057 ++p;
2058 cc = *p;
2059 *p = NUL;
2060
2061 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2062 *p = cc;
2063 p = skipwhite(p);
2064
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002065 // TODO: what if it is a function?
2066 if (idx < 0)
2067 return FAIL;
2068 *end = p;
2069
2070 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2071 import->imp_sid,
2072 idx,
2073 type);
2074 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002075 else if (import->imp_funcname != NULL)
2076 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002077 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002078 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2079 import->imp_sid,
2080 import->imp_var_vals_idx,
2081 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002082 return OK;
2083 }
2084
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002085 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002086 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002087 return FAIL;
2088}
2089
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002090 static int
2091generate_funcref(cctx_T *cctx, char_u *name)
2092{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002093 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002094
2095 if (ufunc == NULL)
2096 return FAIL;
2097
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002098 // Need to compile any default values to get the argument types.
2099 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2100 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2101 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002102 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002103}
2104
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002105/*
2106 * Compile a variable name into a load instruction.
2107 * "end" points to just after the name.
2108 * When "error" is FALSE do not give an error when not found.
2109 */
2110 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002111compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112{
2113 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002114 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002115 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002117 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002118
2119 if (*(*arg + 1) == ':')
2120 {
2121 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002122 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002123 {
2124 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002125
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002126 switch (**arg)
2127 {
2128 case 'g': isn_type = ISN_LOADGDICT; break;
2129 case 'w': isn_type = ISN_LOADWDICT; break;
2130 case 't': isn_type = ISN_LOADTDICT; break;
2131 case 'b': isn_type = ISN_LOADBDICT; break;
2132 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002133 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002134 goto theend;
2135 }
2136 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2137 goto theend;
2138 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002139 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 else
2141 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002142 isntype_T isn_type = ISN_DROP;
2143
2144 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2145 if (name == NULL)
2146 return FAIL;
2147
2148 switch (**arg)
2149 {
2150 case 'v': res = generate_LOADV(cctx, name, error);
2151 break;
2152 case 's': res = compile_load_scriptvar(cctx, name,
2153 NULL, NULL, error);
2154 break;
2155 case 'g': isn_type = ISN_LOADG; break;
2156 case 'w': isn_type = ISN_LOADW; break;
2157 case 't': isn_type = ISN_LOADT; break;
2158 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002159 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002160 goto theend;
2161 }
2162 if (isn_type != ISN_DROP)
2163 {
2164 // Global, Buffer-local, Window-local and Tabpage-local
2165 // variables can be defined later, thus we don't check if it
2166 // exists, give error at runtime.
2167 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2168 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169 }
2170 }
2171 else
2172 {
2173 size_t len = end - *arg;
2174 int idx;
2175 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002176 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002177
2178 name = vim_strnsave(*arg, end - *arg);
2179 if (name == NULL)
2180 return FAIL;
2181
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002182 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002183 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002184 if (!gen_load_outer)
2185 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002186 }
2187 else
2188 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002189 lvar_T *lvar = lookup_local(*arg, len, cctx);
2190
2191 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002192 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002193 type = lvar->lv_type;
2194 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002195 if (lvar->lv_from_outer)
2196 gen_load_outer = TRUE;
2197 else
2198 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002199 }
2200 else
2201 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002202 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002203 // already exists in a Vim9 script or when it's imported.
2204 if (lookup_script(*arg, len, TRUE) == OK
2205 || find_imported(name, 0, cctx) != NULL)
2206 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002207
Bram Moolenaara5565e42020-05-09 15:44:01 +02002208 // When the name starts with an uppercase letter or "x:" it
2209 // can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002210 // TODO: this is just guessing
Bram Moolenaara5565e42020-05-09 15:44:01 +02002211 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2212 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002213 }
2214 }
2215 if (gen_load)
2216 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002217 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002218 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002219 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002220 cctx->ctx_outer_used = TRUE;
2221 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002222 }
2223
2224 *arg = end;
2225
2226theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002227 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002228 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002229 vim_free(name);
2230 return res;
2231}
2232
2233/*
2234 * Compile the argument expressions.
2235 * "arg" points to just after the "(" and is advanced to after the ")"
2236 */
2237 static int
2238compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2239{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002240 char_u *p = *arg;
2241 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002242
Bram Moolenaare6085c52020-04-12 20:19:16 +02002243 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002244 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002245 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2246 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002247 if (*p == ')')
2248 {
2249 *arg = p + 1;
2250 return OK;
2251 }
2252
Bram Moolenaara5565e42020-05-09 15:44:01 +02002253 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002254 return FAIL;
2255 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002256
2257 if (*p != ',' && *skipwhite(p) == ',')
2258 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002259 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002260 p = skipwhite(p);
2261 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002262 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002263 {
2264 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002265 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002266 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002267 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002268 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002269 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002270 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002271failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002272 emsg(_(e_missing_close));
2273 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002274}
2275
2276/*
2277 * Compile a function call: name(arg1, arg2)
2278 * "arg" points to "name", "arg + varlen" to the "(".
2279 * "argcount_init" is 1 for "value->method()"
2280 * Instructions:
2281 * EVAL arg1
2282 * EVAL arg2
2283 * BCALL / DCALL / UCALL
2284 */
2285 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002286compile_call(
2287 char_u **arg,
2288 size_t varlen,
2289 cctx_T *cctx,
2290 ppconst_T *ppconst,
2291 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002292{
2293 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002294 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002295 int argcount = argcount_init;
2296 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002297 char_u fname_buf[FLEN_FIXED + 1];
2298 char_u *tofree = NULL;
2299 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002300 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002301 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002302 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002303
Bram Moolenaara5565e42020-05-09 15:44:01 +02002304 // we can evaluate "has('name')" at compile time
2305 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2306 {
2307 char_u *s = skipwhite(*arg + varlen + 1);
2308 typval_T argvars[2];
2309
2310 argvars[0].v_type = VAR_UNKNOWN;
2311 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002312 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002313 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002314 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002315 s = skipwhite(s);
2316 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2317 {
2318 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2319
2320 *arg = s + 1;
2321 argvars[1].v_type = VAR_UNKNOWN;
2322 tv->v_type = VAR_NUMBER;
2323 tv->vval.v_number = 0;
2324 f_has(argvars, tv);
2325 clear_tv(&argvars[0]);
2326 ++ppconst->pp_used;
2327 return OK;
2328 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002329 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002330 }
2331
2332 if (generate_ppconst(cctx, ppconst) == FAIL)
2333 return FAIL;
2334
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002335 if (varlen >= sizeof(namebuf))
2336 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002337 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002338 return FAIL;
2339 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002340 vim_strncpy(namebuf, *arg, varlen);
2341 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002342
2343 *arg = skipwhite(*arg + varlen + 1);
2344 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002345 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002346
Bram Moolenaara1773442020-08-12 15:21:22 +02002347 is_autoload = vim_strchr(name, '#') != NULL;
2348 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002349 {
2350 int idx;
2351
2352 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002353 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002354 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002355 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002356 else
2357 semsg(_(e_unknownfunc), namebuf);
2358 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002359 }
2360
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002361 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002362 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002363 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002364 {
2365 res = generate_CALL(cctx, ufunc, argcount);
2366 goto theend;
2367 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368
2369 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002370 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002371 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002373 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaara26b9702020-04-18 19:53:28 +02002374 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002375 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002376 garray_T *stack = &cctx->ctx_type_stack;
2377 type_T *type;
2378
2379 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2380 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002381 goto theend;
2382 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002383
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002384 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002385 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002386 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002387 res = generate_UCALL(cctx, name, argcount);
2388 else
2389 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002390
2391theend:
2392 vim_free(tofree);
2393 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394}
2395
2396// like NAMESPACE_CHAR but with 'a' and 'l'.
2397#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2398
2399/*
2400 * Find the end of a variable or function name. Unlike find_name_end() this
2401 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002402 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403 * Return a pointer to just after the name. Equal to "arg" if there is no
2404 * valid name.
2405 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002406 static char_u *
2407to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002408{
2409 char_u *p;
2410
2411 // Quick check for valid starting character.
2412 if (!eval_isnamec1(*arg))
2413 return arg;
2414
2415 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2416 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2417 // and can be used in slice "[n:]".
2418 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002419 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002420 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2421 break;
2422 return p;
2423}
2424
2425/*
2426 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002427 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428 */
2429 char_u *
2430to_name_const_end(char_u *arg)
2431{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002432 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002433 typval_T rettv;
2434
2435 if (p == arg && *arg == '[')
2436 {
2437
2438 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002439 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002440 p = arg;
2441 }
2442 else if (p == arg && *arg == '#' && arg[1] == '{')
2443 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002444 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002445 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002446 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447 p = arg;
2448 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002449
2450 return p;
2451}
2452
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002453/*
2454 * parse a list: [expr, expr]
2455 * "*arg" points to the '['.
2456 */
2457 static int
2458compile_list(char_u **arg, cctx_T *cctx)
2459{
2460 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002461 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002462 int count = 0;
2463
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002464 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002465 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002466 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002467 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002468 semsg(_(e_list_end), *arg);
2469 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002470 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002471 if (*p == ',')
2472 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002473 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002474 return FAIL;
2475 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002476 if (*p == ']')
2477 {
2478 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002479 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002480 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002481 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002482 break;
2483 ++count;
2484 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002485 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002487 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2488 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002489 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002490 return FAIL;
2491 }
2492 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002493 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002494 p = skipwhite(p);
2495 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002496 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497
2498 generate_NEWLIST(cctx, count);
2499 return OK;
2500}
2501
2502/*
2503 * parse a lambda: {arg, arg -> expr}
2504 * "*arg" points to the '{'.
2505 */
2506 static int
2507compile_lambda(char_u **arg, cctx_T *cctx)
2508{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 typval_T rettv;
2510 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002511 evalarg_T evalarg;
2512
2513 CLEAR_FIELD(evalarg);
2514 evalarg.eval_flags = EVAL_EVALUATE;
2515 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516
2517 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002518 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002520
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002522 ++ufunc->uf_refcount;
2523 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002524 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525
2526 // The function will have one line: "return {expr}".
2527 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002528 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002530 clear_evalarg(&evalarg, NULL);
2531
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002532 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002533 {
2534 // The return type will now be known.
2535 set_function_type(ufunc);
2536
2537 return generate_FUNCREF(cctx, ufunc);
2538 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002539
2540 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002541 return FAIL;
2542}
2543
2544/*
2545 * Compile a lamda call: expr->{lambda}(args)
2546 * "arg" points to the "{".
2547 */
2548 static int
2549compile_lambda_call(char_u **arg, cctx_T *cctx)
2550{
2551 ufunc_T *ufunc;
2552 typval_T rettv;
2553 int argcount = 1;
2554 int ret = FAIL;
2555
2556 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002557 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558 return FAIL;
2559
2560 if (**arg != '(')
2561 {
2562 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002563 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 else
2565 semsg(_(e_missing_paren), "lambda");
2566 clear_tv(&rettv);
2567 return FAIL;
2568 }
2569
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002570 ufunc = rettv.vval.v_partial->pt_func;
2571 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002572 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002573 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002574
2575 // The function will have one line: "return {expr}".
2576 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002577 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578
2579 // compile the arguments
2580 *arg = skipwhite(*arg + 1);
2581 if (compile_arguments(arg, cctx, &argcount) == OK)
2582 // call the compiled function
2583 ret = generate_CALL(cctx, ufunc, argcount);
2584
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002585 if (ret == FAIL)
2586 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002587 return ret;
2588}
2589
2590/*
2591 * parse a dict: {'key': val} or #{key: val}
2592 * "*arg" points to the '{'.
2593 */
2594 static int
2595compile_dict(char_u **arg, cctx_T *cctx, int literal)
2596{
2597 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002598 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 int count = 0;
2600 dict_T *d = dict_alloc();
2601 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002602 char_u *whitep = *arg;
2603 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604
2605 if (d == NULL)
2606 return FAIL;
2607 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002608 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609 {
2610 char_u *key = NULL;
2611
Bram Moolenaar23c55272020-06-21 16:58:13 +02002612 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002613 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002614 *arg = NULL;
2615 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002616 }
2617
2618 if (**arg == '}')
2619 break;
2620
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621 if (literal)
2622 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002623 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624
Bram Moolenaar2c330432020-04-13 14:41:35 +02002625 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002626 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002627 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628 return FAIL;
2629 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002630 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002631 if (generate_PUSHS(cctx, key) == FAIL)
2632 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002633 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 }
2635 else
2636 {
2637 isn_T *isn;
2638
Bram Moolenaara5565e42020-05-09 15:44:01 +02002639 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002641 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2642 if (isn->isn_type == ISN_PUSHS)
2643 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002644 else
2645 {
2646 type_T *keytype = ((type_T **)stack->ga_data)
2647 [stack->ga_len - 1];
2648 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2649 return FAIL;
2650 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651 }
2652
2653 // Check for duplicate keys, if using string keys.
2654 if (key != NULL)
2655 {
2656 item = dict_find(d, key, -1);
2657 if (item != NULL)
2658 {
2659 semsg(_(e_duplicate_key), key);
2660 goto failret;
2661 }
2662 item = dictitem_alloc(key);
2663 if (item != NULL)
2664 {
2665 item->di_tv.v_type = VAR_UNKNOWN;
2666 item->di_tv.v_lock = 0;
2667 if (dict_add(d, item) == FAIL)
2668 dictitem_free(item);
2669 }
2670 }
2671
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672 if (**arg != ':')
2673 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002674 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002675 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002676 else
2677 semsg(_(e_missing_dict_colon), *arg);
2678 return FAIL;
2679 }
2680 whitep = *arg + 1;
2681 if (!IS_WHITE_OR_NUL(*whitep))
2682 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002683 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 return FAIL;
2685 }
2686
2687 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002688 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002689 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002690 *arg = NULL;
2691 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002692 }
2693
Bram Moolenaara5565e42020-05-09 15:44:01 +02002694 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002695 return FAIL;
2696 ++count;
2697
Bram Moolenaar2c330432020-04-13 14:41:35 +02002698 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002699 *arg = skipwhite(*arg);
2700 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002701 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002702 *arg = NULL;
2703 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002704 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705 if (**arg == '}')
2706 break;
2707 if (**arg != ',')
2708 {
2709 semsg(_(e_missing_dict_comma), *arg);
2710 goto failret;
2711 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002712 if (IS_WHITE_OR_NUL(*whitep))
2713 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002714 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002715 return FAIL;
2716 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002717 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002718 *arg = skipwhite(*arg + 1);
2719 }
2720
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002721 *arg = *arg + 1;
2722
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002723 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002724 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002725 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002726 *arg += STRLEN(*arg);
2727
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 dict_unref(d);
2729 return generate_NEWDICT(cctx, count);
2730
2731failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002732 if (*arg == NULL)
2733 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002734 dict_unref(d);
2735 return FAIL;
2736}
2737
2738/*
2739 * Compile "&option".
2740 */
2741 static int
2742compile_get_option(char_u **arg, cctx_T *cctx)
2743{
2744 typval_T rettv;
2745 char_u *start = *arg;
2746 int ret;
2747
2748 // parse the option and get the current value to get the type.
2749 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002750 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 if (ret == OK)
2752 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002753 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002754 char_u *name = vim_strnsave(start, *arg - start);
2755 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2756
2757 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2758 vim_free(name);
2759 }
2760 clear_tv(&rettv);
2761
2762 return ret;
2763}
2764
2765/*
2766 * Compile "$VAR".
2767 */
2768 static int
2769compile_get_env(char_u **arg, cctx_T *cctx)
2770{
2771 char_u *start = *arg;
2772 int len;
2773 int ret;
2774 char_u *name;
2775
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776 ++*arg;
2777 len = get_env_len(arg);
2778 if (len == 0)
2779 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002780 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781 return FAIL;
2782 }
2783
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002784 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785 name = vim_strnsave(start, len + 1);
2786 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2787 vim_free(name);
2788 return ret;
2789}
2790
2791/*
2792 * Compile "@r".
2793 */
2794 static int
2795compile_get_register(char_u **arg, cctx_T *cctx)
2796{
2797 int ret;
2798
2799 ++*arg;
2800 if (**arg == NUL)
2801 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002802 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803 return FAIL;
2804 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002805 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806 {
2807 emsg_invreg(**arg);
2808 return FAIL;
2809 }
2810 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2811 ++*arg;
2812 return ret;
2813}
2814
2815/*
2816 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002817 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818 */
2819 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002820apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002822 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002823
2824 // this works from end to start
2825 while (p > start)
2826 {
2827 --p;
2828 if (*p == '-' || *p == '+')
2829 {
2830 // only '-' has an effect, for '+' we only check the type
2831#ifdef FEAT_FLOAT
2832 if (rettv->v_type == VAR_FLOAT)
2833 {
2834 if (*p == '-')
2835 rettv->vval.v_float = -rettv->vval.v_float;
2836 }
2837 else
2838#endif
2839 {
2840 varnumber_T val;
2841 int error = FALSE;
2842
2843 // tv_get_number_chk() accepts a string, but we don't want that
2844 // here
2845 if (check_not_string(rettv) == FAIL)
2846 return FAIL;
2847 val = tv_get_number_chk(rettv, &error);
2848 clear_tv(rettv);
2849 if (error)
2850 return FAIL;
2851 if (*p == '-')
2852 val = -val;
2853 rettv->v_type = VAR_NUMBER;
2854 rettv->vval.v_number = val;
2855 }
2856 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002857 else if (numeric_only)
2858 {
2859 ++p;
2860 break;
2861 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 else
2863 {
2864 int v = tv2bool(rettv);
2865
2866 // '!' is permissive in the type.
2867 clear_tv(rettv);
2868 rettv->v_type = VAR_BOOL;
2869 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2870 }
2871 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002872 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873 return OK;
2874}
2875
2876/*
2877 * Recognize v: variables that are constants and set "rettv".
2878 */
2879 static void
2880get_vim_constant(char_u **arg, typval_T *rettv)
2881{
2882 if (STRNCMP(*arg, "v:true", 6) == 0)
2883 {
2884 rettv->v_type = VAR_BOOL;
2885 rettv->vval.v_number = VVAL_TRUE;
2886 *arg += 6;
2887 }
2888 else if (STRNCMP(*arg, "v:false", 7) == 0)
2889 {
2890 rettv->v_type = VAR_BOOL;
2891 rettv->vval.v_number = VVAL_FALSE;
2892 *arg += 7;
2893 }
2894 else if (STRNCMP(*arg, "v:null", 6) == 0)
2895 {
2896 rettv->v_type = VAR_SPECIAL;
2897 rettv->vval.v_number = VVAL_NULL;
2898 *arg += 6;
2899 }
2900 else if (STRNCMP(*arg, "v:none", 6) == 0)
2901 {
2902 rettv->v_type = VAR_SPECIAL;
2903 rettv->vval.v_number = VVAL_NONE;
2904 *arg += 6;
2905 }
2906}
2907
Bram Moolenaar696ba232020-07-29 21:20:41 +02002908 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02002909get_compare_type(char_u *p, int *len, int *type_is)
2910{
2911 exptype_T type = EXPR_UNKNOWN;
2912 int i;
2913
2914 switch (p[0])
2915 {
2916 case '=': if (p[1] == '=')
2917 type = EXPR_EQUAL;
2918 else if (p[1] == '~')
2919 type = EXPR_MATCH;
2920 break;
2921 case '!': if (p[1] == '=')
2922 type = EXPR_NEQUAL;
2923 else if (p[1] == '~')
2924 type = EXPR_NOMATCH;
2925 break;
2926 case '>': if (p[1] != '=')
2927 {
2928 type = EXPR_GREATER;
2929 *len = 1;
2930 }
2931 else
2932 type = EXPR_GEQUAL;
2933 break;
2934 case '<': if (p[1] != '=')
2935 {
2936 type = EXPR_SMALLER;
2937 *len = 1;
2938 }
2939 else
2940 type = EXPR_SEQUAL;
2941 break;
2942 case 'i': if (p[1] == 's')
2943 {
2944 // "is" and "isnot"; but not a prefix of a name
2945 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2946 *len = 5;
2947 i = p[*len];
2948 if (!isalnum(i) && i != '_')
2949 {
2950 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2951 *type_is = TRUE;
2952 }
2953 }
2954 break;
2955 }
2956 return type;
2957}
2958
2959/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002961 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 */
2963 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002964compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002966 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967
2968 // this works from end to start
2969 while (p > start)
2970 {
2971 --p;
2972 if (*p == '-' || *p == '+')
2973 {
2974 int negate = *p == '-';
2975 isn_T *isn;
2976
2977 // TODO: check type
2978 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2979 {
2980 --p;
2981 if (*p == '-')
2982 negate = !negate;
2983 }
2984 // only '-' has an effect, for '+' we only check the type
2985 if (negate)
2986 isn = generate_instr(cctx, ISN_NEGATENR);
2987 else
2988 isn = generate_instr(cctx, ISN_CHECKNR);
2989 if (isn == NULL)
2990 return FAIL;
2991 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002992 else if (numeric_only)
2993 {
2994 ++p;
2995 break;
2996 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 else
2998 {
2999 int invert = TRUE;
3000
3001 while (p > start && p[-1] == '!')
3002 {
3003 --p;
3004 invert = !invert;
3005 }
3006 if (generate_2BOOL(cctx, invert) == FAIL)
3007 return FAIL;
3008 }
3009 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003010 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011 return OK;
3012}
3013
3014/*
3015 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003016 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017 */
3018 static int
3019compile_subscript(
3020 char_u **arg,
3021 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003022 char_u *start_leader,
3023 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003024 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003026 char_u *name_start = *end_leader;
3027
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 for (;;)
3029 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003030 char_u *p = skipwhite(*arg);
3031
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003032 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003033 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003034 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003035
3036 // If a following line starts with "->{" or "->X" advance to that
3037 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003038 // Also if a following line starts with ".x".
3039 if (next != NULL &&
3040 ((next[0] == '-' && next[1] == '>'
3041 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003042 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003043 {
3044 next = next_line_from_context(cctx, TRUE);
3045 if (next == NULL)
3046 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003047 *arg = next;
3048 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003049 }
3050 }
3051
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003052 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3053 // not a function call.
3054 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003056 garray_T *stack = &cctx->ctx_type_stack;
3057 type_T *type;
3058 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003060 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003061 return FAIL;
3062
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003063 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003064 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3065
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003066 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3068 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003069 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 return FAIL;
3071 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003072 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003073 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003074 char_u *pstart = p;
3075
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003076 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003077 return FAIL;
3078
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 // something->method()
3080 // Apply the '!', '-' and '+' first:
3081 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003082 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003085 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003086 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003087 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 if (**arg == '{')
3089 {
3090 // lambda call: list->{lambda}
3091 if (compile_lambda_call(arg, cctx) == FAIL)
3092 return FAIL;
3093 }
3094 else
3095 {
3096 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003097 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003098 if (!eval_isnamec1(*p))
3099 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003100 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003101 return FAIL;
3102 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003103 if (ASCII_ISALPHA(*p) && p[1] == ':')
3104 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003105 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106 ;
3107 if (*p != '(')
3108 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003109 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 return FAIL;
3111 }
3112 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003113 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114 return FAIL;
3115 }
3116 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003117 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003119 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003120 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003121 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003122 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003123 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003124
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003125 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003126 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003127 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003128 // TODO: blob index
3129 // TODO: more arguments
3130 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003131 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003132 return FAIL;
3133
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003134 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003135 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003136 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003137 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003138 if (**arg == ':')
3139 // missing first index is equal to zero
3140 generate_PUSHNR(cctx, 0);
3141 else
3142 {
3143 if (compile_expr0(arg, cctx) == FAIL)
3144 return FAIL;
3145 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3146 return FAIL;
3147 *arg = skipwhite(*arg);
3148 }
3149 if (**arg == ':')
3150 {
3151 *arg = skipwhite(*arg + 1);
3152 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3153 return FAIL;
3154 if (**arg == ']')
3155 // missing second index is equal to end of string
3156 generate_PUSHNR(cctx, -1);
3157 else
3158 {
3159 if (compile_expr0(arg, cctx) == FAIL)
3160 return FAIL;
3161 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3162 return FAIL;
3163 *arg = skipwhite(*arg);
3164 }
3165 is_slice = TRUE;
3166 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167
3168 if (**arg != ']')
3169 {
3170 emsg(_(e_missbrac));
3171 return FAIL;
3172 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003173 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003175 // We can index a list and a dict. If we don't know the type
3176 // we can use the index value type.
3177 // TODO: If we don't know use an instruction to figure it out at
3178 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003179 typep = ((type_T **)stack->ga_data) + stack->ga_len
3180 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003181 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003182 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3183 // If the index is a string, the variable must be a Dict.
3184 if (*typep == &t_any && valtype == &t_string)
3185 vtype = VAR_DICT;
3186 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003187 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003188 if (need_type(valtype, &t_number, -1, cctx, FALSE) == FAIL)
3189 return FAIL;
3190 if (is_slice)
3191 {
3192 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
3193 if (need_type(valtype, &t_number, -2, cctx, FALSE) == FAIL)
3194 return FAIL;
3195 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003196 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003197
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003198 if (vtype == VAR_DICT)
3199 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003200 if (is_slice)
3201 {
3202 emsg(_(e_cannot_slice_dictionary));
3203 return FAIL;
3204 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003205 if ((*typep)->tt_type == VAR_DICT)
3206 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003207 else
3208 {
3209 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3210 return FAIL;
3211 *typep = &t_any;
3212 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003213 if (may_generate_2STRING(-1, cctx) == FAIL)
3214 return FAIL;
3215 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3216 return FAIL;
3217 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003218 else if (vtype == VAR_STRING)
3219 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003220 *typep = &t_string;
3221 if ((is_slice
3222 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3223 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003224 return FAIL;
3225 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003226 else if (vtype == VAR_BLOB)
3227 {
3228 emsg("Sorry, blob index and slice not implemented yet");
3229 return FAIL;
3230 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003231 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003232 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003233 if (is_slice)
3234 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003235 if (generate_instr_drop(cctx,
3236 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3237 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003238 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003239 }
Bram Moolenaared591872020-08-15 22:14:53 +02003240 else
3241 {
3242 if ((*typep)->tt_type == VAR_LIST)
3243 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003244 if (generate_instr_drop(cctx,
3245 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3246 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003247 return FAIL;
3248 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003249 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003250 else
3251 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003252 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003253 return FAIL;
3254 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003256 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003258 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003259 return FAIL;
3260
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003261 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003262 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3263 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003265 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003266 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 while (eval_isnamec(*p))
3268 MB_PTR_ADV(p);
3269 if (p == *arg)
3270 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003271 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003272 return FAIL;
3273 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003274 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 return FAIL;
3276 *arg = p;
3277 }
3278 else
3279 break;
3280 }
3281
3282 // TODO - see handle_subscript():
3283 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3284 // Don't do this when "Func" is already a partial that was bound
3285 // explicitly (pt_auto is FALSE).
3286
3287 return OK;
3288}
3289
3290/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003291 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3292 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003294 * If the value is a constant "ppconst->pp_ret" will be set.
3295 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003296 *
3297 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 */
3299
3300/*
3301 * number number constant
3302 * 0zFFFFFFFF Blob constant
3303 * "string" string constant
3304 * 'string' literal string constant
3305 * &option-name option value
3306 * @r register contents
3307 * identifier variable value
3308 * function() function call
3309 * $VAR environment variable
3310 * (expression) nested expression
3311 * [expr, expr] List
3312 * {key: val, key: val} Dictionary
3313 * #{key: val, key: val} Dictionary with literal keys
3314 *
3315 * Also handle:
3316 * ! in front logical NOT
3317 * - in front unary minus
3318 * + in front unary plus (ignored)
3319 * trailing (arg) funcref/partial call
3320 * trailing [] subscript in String or List
3321 * trailing .name entry in Dictionary
3322 * trailing ->name() method call
3323 */
3324 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003325compile_expr7(
3326 char_u **arg,
3327 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003328 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003330 char_u *start_leader, *end_leader;
3331 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003332 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003333 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003334
3335 /*
3336 * Skip '!', '-' and '+' characters. They are handled later.
3337 */
3338 start_leader = *arg;
3339 while (**arg == '!' || **arg == '-' || **arg == '+')
3340 *arg = skipwhite(*arg + 1);
3341 end_leader = *arg;
3342
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003343 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003344 switch (**arg)
3345 {
3346 /*
3347 * Number constant.
3348 */
3349 case '0': // also for blob starting with 0z
3350 case '1':
3351 case '2':
3352 case '3':
3353 case '4':
3354 case '5':
3355 case '6':
3356 case '7':
3357 case '8':
3358 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003359 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003361 // Apply "-" and "+" just before the number now, right to
3362 // left. Matters especially when "->" follows. Stops at
3363 // '!'.
3364 if (apply_leader(rettv, TRUE,
3365 start_leader, &end_leader) == FAIL)
3366 {
3367 clear_tv(rettv);
3368 return FAIL;
3369 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370 break;
3371
3372 /*
3373 * String constant: "string".
3374 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003375 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376 return FAIL;
3377 break;
3378
3379 /*
3380 * Literal string constant: 'str''ing'.
3381 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003382 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383 return FAIL;
3384 break;
3385
3386 /*
3387 * Constant Vim variable.
3388 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003389 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390 ret = NOTDONE;
3391 break;
3392
3393 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003394 * "true" constant
3395 */
3396 case 't': if (STRNCMP(*arg, "true", 4) == 0
3397 && !eval_isnamec((*arg)[4]))
3398 {
3399 *arg += 4;
3400 rettv->v_type = VAR_BOOL;
3401 rettv->vval.v_number = VVAL_TRUE;
3402 }
3403 else
3404 ret = NOTDONE;
3405 break;
3406
3407 /*
3408 * "false" constant
3409 */
3410 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3411 && !eval_isnamec((*arg)[5]))
3412 {
3413 *arg += 5;
3414 rettv->v_type = VAR_BOOL;
3415 rettv->vval.v_number = VVAL_FALSE;
3416 }
3417 else
3418 ret = NOTDONE;
3419 break;
3420
3421 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 * List: [expr, expr]
3423 */
3424 case '[': ret = compile_list(arg, cctx);
3425 break;
3426
3427 /*
3428 * Dictionary: #{key: val, key: val}
3429 */
3430 case '#': if ((*arg)[1] == '{')
3431 {
3432 ++*arg;
3433 ret = compile_dict(arg, cctx, TRUE);
3434 }
3435 else
3436 ret = NOTDONE;
3437 break;
3438
3439 /*
3440 * Lambda: {arg, arg -> expr}
3441 * Dictionary: {'key': val, 'key': val}
3442 */
3443 case '{': {
3444 char_u *start = skipwhite(*arg + 1);
3445
3446 // Find out what comes after the arguments.
3447 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003448 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 if (ret != FAIL && *start == '>')
3450 ret = compile_lambda(arg, cctx);
3451 else
3452 ret = compile_dict(arg, cctx, FALSE);
3453 }
3454 break;
3455
3456 /*
3457 * Option value: &name
3458 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003459 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3460 return FAIL;
3461 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003462 break;
3463
3464 /*
3465 * Environment variable: $VAR.
3466 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003467 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3468 return FAIL;
3469 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003470 break;
3471
3472 /*
3473 * Register contents: @r.
3474 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003475 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3476 return FAIL;
3477 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 break;
3479 /*
3480 * nested expression: (expression).
3481 */
3482 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003483
3484 // recursive!
3485 if (ppconst->pp_used <= PPSIZE - 10)
3486 {
3487 ret = compile_expr1(arg, cctx, ppconst);
3488 }
3489 else
3490 {
3491 // Not enough space in ppconst, flush constants.
3492 if (generate_ppconst(cctx, ppconst) == FAIL)
3493 return FAIL;
3494 ret = compile_expr0(arg, cctx);
3495 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496 *arg = skipwhite(*arg);
3497 if (**arg == ')')
3498 ++*arg;
3499 else if (ret == OK)
3500 {
3501 emsg(_(e_missing_close));
3502 ret = FAIL;
3503 }
3504 break;
3505
3506 default: ret = NOTDONE;
3507 break;
3508 }
3509 if (ret == FAIL)
3510 return FAIL;
3511
Bram Moolenaar1c747212020-05-09 18:28:34 +02003512 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003514 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003515 clear_tv(rettv);
3516 else
3517 // A constant expression can possibly be handled compile time,
3518 // return the value instead of generating code.
3519 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 }
3521 else if (ret == NOTDONE)
3522 {
3523 char_u *p;
3524 int r;
3525
3526 if (!eval_isnamec1(**arg))
3527 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003528 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003529 return FAIL;
3530 }
3531
3532 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003533 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003535 {
3536 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3537 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003539 {
3540 if (generate_ppconst(cctx, ppconst) == FAIL)
3541 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003543 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003544 if (r == FAIL)
3545 return FAIL;
3546 }
3547
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003548 // Handle following "[]", ".member", etc.
3549 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003550 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003551 ppconst) == FAIL)
3552 return FAIL;
3553 if (ppconst->pp_used > 0)
3554 {
3555 // apply the '!', '-' and '+' before the constant
3556 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003557 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003558 return FAIL;
3559 return OK;
3560 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003561 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003563 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564}
3565
3566/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003567 * Give the "white on both sides" error, taking the operator from "p[len]".
3568 */
3569 void
3570error_white_both(char_u *op, int len)
3571{
3572 char_u buf[10];
3573
3574 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003575 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003576}
3577
3578/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003579 * <type>expr7: runtime type check / conversion
3580 */
3581 static int
3582compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3583{
3584 type_T *want_type = NULL;
3585
3586 // Recognize <type>
3587 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3588 {
3589 int called_emsg_before = called_emsg;
3590
3591 ++*arg;
3592 want_type = parse_type(arg, cctx->ctx_type_list);
3593 if (called_emsg != called_emsg_before)
3594 return FAIL;
3595
3596 if (**arg != '>')
3597 {
3598 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003599 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003600 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003601 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003602 return FAIL;
3603 }
3604 ++*arg;
3605 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3606 return FAIL;
3607 }
3608
3609 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3610 return FAIL;
3611
3612 if (want_type != NULL)
3613 {
3614 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003615 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003616
Bram Moolenaard1103582020-08-14 22:44:25 +02003617 generate_ppconst(cctx, ppconst);
3618 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003619 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003620 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003621 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3622 return FAIL;
3623 }
3624 }
3625
3626 return OK;
3627}
3628
3629/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630 * * number multiplication
3631 * / number division
3632 * % number modulo
3633 */
3634 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003635compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636{
3637 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003638 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003639 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003641 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003642 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 return FAIL;
3644
3645 /*
3646 * Repeat computing, until no "*", "/" or "%" is following.
3647 */
3648 for (;;)
3649 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003650 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651 if (*op != '*' && *op != '/' && *op != '%')
3652 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003653 if (next != NULL)
3654 {
3655 *arg = next_line_from_context(cctx, TRUE);
3656 op = skipwhite(*arg);
3657 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003658
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003659 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003661 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003662 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 }
3664 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003665 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003666 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003668 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003669 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003671
3672 if (ppconst->pp_used == ppconst_used + 2
3673 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3674 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003675 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003676 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3677 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003678 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003680 // both are numbers: compute the result
3681 switch (*op)
3682 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003683 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003684 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003685 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003686 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003687 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003688 break;
3689 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003690 tv1->vval.v_number = res;
3691 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003692 }
3693 else
3694 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003695 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003696 generate_two_op(cctx, op);
3697 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 }
3699
3700 return OK;
3701}
3702
3703/*
3704 * + number addition
3705 * - number subtraction
3706 * .. string concatenation
3707 */
3708 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003709compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710{
3711 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003712 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003714 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715
3716 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003717 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003718 return FAIL;
3719
3720 /*
3721 * Repeat computing, until no "+", "-" or ".." is following.
3722 */
3723 for (;;)
3724 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003725 op = may_peek_next_line(cctx, *arg, &next);
3726 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727 break;
3728 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003729 if (next != NULL)
3730 {
3731 *arg = next_line_from_context(cctx, TRUE);
3732 op = skipwhite(*arg);
3733 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003735 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003737 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003738 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003739 }
3740
3741 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003742 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003743 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003745 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003746 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 return FAIL;
3748
Bram Moolenaara5565e42020-05-09 15:44:01 +02003749 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003750 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003751 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3752 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3753 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3754 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003755 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003756 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3757 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003758
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003759 // concat/subtract/add constant numbers
3760 if (*op == '+')
3761 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3762 else if (*op == '-')
3763 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3764 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003765 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003766 // concatenate constant strings
3767 char_u *s1 = tv1->vval.v_string;
3768 char_u *s2 = tv2->vval.v_string;
3769 size_t len1 = STRLEN(s1);
3770
3771 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3772 if (tv1->vval.v_string == NULL)
3773 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003774 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003775 return FAIL;
3776 }
3777 mch_memmove(tv1->vval.v_string, s1, len1);
3778 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003779 vim_free(s1);
3780 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003781 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003782 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783 }
3784 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003785 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003786 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003787 if (*op == '.')
3788 {
3789 if (may_generate_2STRING(-2, cctx) == FAIL
3790 || may_generate_2STRING(-1, cctx) == FAIL)
3791 return FAIL;
3792 generate_instr_drop(cctx, ISN_CONCAT, 1);
3793 }
3794 else
3795 generate_two_op(cctx, op);
3796 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797 }
3798
3799 return OK;
3800}
3801
3802/*
3803 * expr5a == expr5b
3804 * expr5a =~ expr5b
3805 * expr5a != expr5b
3806 * expr5a !~ expr5b
3807 * expr5a > expr5b
3808 * expr5a >= expr5b
3809 * expr5a < expr5b
3810 * expr5a <= expr5b
3811 * expr5a is expr5b
3812 * expr5a isnot expr5b
3813 *
3814 * Produces instructions:
3815 * EVAL expr5a Push result of "expr5a"
3816 * EVAL expr5b Push result of "expr5b"
3817 * COMPARE one of the compare instructions
3818 */
3819 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003820compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821{
3822 exptype_T type = EXPR_UNKNOWN;
3823 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003824 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003827 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828
3829 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003830 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 return FAIL;
3832
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003833 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003834 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003835
3836 /*
3837 * If there is a comparative operator, use it.
3838 */
3839 if (type != EXPR_UNKNOWN)
3840 {
3841 int ic = FALSE; // Default: do not ignore case
3842
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003843 if (next != NULL)
3844 {
3845 *arg = next_line_from_context(cctx, TRUE);
3846 p = skipwhite(*arg);
3847 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848 if (type_is && (p[len] == '?' || p[len] == '#'))
3849 {
3850 semsg(_(e_invexpr2), *arg);
3851 return FAIL;
3852 }
3853 // extra question mark appended: ignore case
3854 if (p[len] == '?')
3855 {
3856 ic = TRUE;
3857 ++len;
3858 }
3859 // extra '#' appended: match case (ignored)
3860 else if (p[len] == '#')
3861 ++len;
3862 // nothing appended: match case
3863
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003864 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003866 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003867 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 }
3869
3870 // get the second variable
3871 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003872 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003873 return FAIL;
3874
Bram Moolenaara5565e42020-05-09 15:44:01 +02003875 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003876 return FAIL;
3877
Bram Moolenaara5565e42020-05-09 15:44:01 +02003878 if (ppconst->pp_used == ppconst_used + 2)
3879 {
3880 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3881 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3882 int ret;
3883
3884 // Both sides are a constant, compute the result now.
3885 // First check for a valid combination of types, this is more
3886 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003887 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003888 ret = FAIL;
3889 else
3890 {
3891 ret = typval_compare(tv1, tv2, type, ic);
3892 tv1->v_type = VAR_BOOL;
3893 tv1->vval.v_number = tv1->vval.v_number
3894 ? VVAL_TRUE : VVAL_FALSE;
3895 clear_tv(tv2);
3896 --ppconst->pp_used;
3897 }
3898 return ret;
3899 }
3900
3901 generate_ppconst(cctx, ppconst);
3902 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 }
3904
3905 return OK;
3906}
3907
Bram Moolenaar7f141552020-05-09 17:35:53 +02003908static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3909
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003910/*
3911 * Compile || or &&.
3912 */
3913 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003914compile_and_or(
3915 char_u **arg,
3916 cctx_T *cctx,
3917 char *op,
3918 ppconst_T *ppconst,
3919 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003921 char_u *next;
3922 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 int opchar = *op;
3924
3925 if (p[0] == opchar && p[1] == opchar)
3926 {
3927 garray_T *instr = &cctx->ctx_instr;
3928 garray_T end_ga;
3929
3930 /*
3931 * Repeat until there is no following "||" or "&&"
3932 */
3933 ga_init2(&end_ga, sizeof(int), 10);
3934 while (p[0] == opchar && p[1] == opchar)
3935 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003936 if (next != NULL)
3937 {
3938 *arg = next_line_from_context(cctx, TRUE);
3939 p = skipwhite(*arg);
3940 }
3941
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003942 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3943 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003944 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003945 return FAIL;
3946 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947
Bram Moolenaara5565e42020-05-09 15:44:01 +02003948 // TODO: use ppconst if the value is a constant
3949 generate_ppconst(cctx, ppconst);
3950
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 if (ga_grow(&end_ga, 1) == FAIL)
3952 {
3953 ga_clear(&end_ga);
3954 return FAIL;
3955 }
3956 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3957 ++end_ga.ga_len;
3958 generate_JUMP(cctx, opchar == '|'
3959 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3960
3961 // eval the next expression
3962 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003963 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003964 return FAIL;
3965
Bram Moolenaara5565e42020-05-09 15:44:01 +02003966 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3967 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 {
3969 ga_clear(&end_ga);
3970 return FAIL;
3971 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003972
3973 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003975 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976
3977 // Fill in the end label in all jumps.
3978 while (end_ga.ga_len > 0)
3979 {
3980 isn_T *isn;
3981
3982 --end_ga.ga_len;
3983 isn = ((isn_T *)instr->ga_data)
3984 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3985 isn->isn_arg.jump.jump_where = instr->ga_len;
3986 }
3987 ga_clear(&end_ga);
3988 }
3989
3990 return OK;
3991}
3992
3993/*
3994 * expr4a && expr4a && expr4a logical AND
3995 *
3996 * Produces instructions:
3997 * EVAL expr4a Push result of "expr4a"
3998 * JUMP_AND_KEEP_IF_FALSE end
3999 * EVAL expr4b Push result of "expr4b"
4000 * JUMP_AND_KEEP_IF_FALSE end
4001 * EVAL expr4c Push result of "expr4c"
4002 * end:
4003 */
4004 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004005compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004006{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004007 int ppconst_used = ppconst->pp_used;
4008
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004010 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 return FAIL;
4012
4013 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004014 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004015}
4016
4017/*
4018 * expr3a || expr3b || expr3c logical OR
4019 *
4020 * Produces instructions:
4021 * EVAL expr3a Push result of "expr3a"
4022 * JUMP_AND_KEEP_IF_TRUE end
4023 * EVAL expr3b Push result of "expr3b"
4024 * JUMP_AND_KEEP_IF_TRUE end
4025 * EVAL expr3c Push result of "expr3c"
4026 * end:
4027 */
4028 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004029compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004031 int ppconst_used = ppconst->pp_used;
4032
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004034 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004035 return FAIL;
4036
4037 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004038 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039}
4040
4041/*
4042 * Toplevel expression: expr2 ? expr1a : expr1b
4043 *
4044 * Produces instructions:
4045 * EVAL expr2 Push result of "expr"
4046 * JUMP_IF_FALSE alt jump if false
4047 * EVAL expr1a
4048 * JUMP_ALWAYS end
4049 * alt: EVAL expr1b
4050 * end:
4051 */
4052 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004053compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054{
4055 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004056 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004057 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058
Bram Moolenaar3988f642020-08-27 22:43:03 +02004059 // Ignore all kinds of errors when not producing code.
4060 if (cctx->ctx_skip == SKIP_YES)
4061 {
4062 skip_expr(arg);
4063 return OK;
4064 }
4065
Bram Moolenaar61a89812020-05-07 16:58:17 +02004066 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004067 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068 return FAIL;
4069
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004070 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071 if (*p == '?')
4072 {
4073 garray_T *instr = &cctx->ctx_instr;
4074 garray_T *stack = &cctx->ctx_type_stack;
4075 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004076 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004078 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004079 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004080 int has_const_expr = FALSE;
4081 int const_value = FALSE;
4082 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004083
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004084 if (next != NULL)
4085 {
4086 *arg = next_line_from_context(cctx, TRUE);
4087 p = skipwhite(*arg);
4088 }
4089
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004090 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4091 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004092 semsg(_(e_white_space_required_before_and_after_str), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004093 return FAIL;
4094 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095
Bram Moolenaara5565e42020-05-09 15:44:01 +02004096 if (ppconst->pp_used == ppconst_used + 1)
4097 {
4098 // the condition is a constant, we know whether the ? or the :
4099 // expression is to be evaluated.
4100 has_const_expr = TRUE;
4101 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4102 clear_tv(&ppconst->pp_tv[ppconst_used]);
4103 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004104 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4105 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004106 }
4107 else
4108 {
4109 generate_ppconst(cctx, ppconst);
4110 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4111 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112
4113 // evaluate the second expression; any type is accepted
4114 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004115 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004116 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004117 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004118 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119
Bram Moolenaara5565e42020-05-09 15:44:01 +02004120 if (!has_const_expr)
4121 {
4122 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123
Bram Moolenaara5565e42020-05-09 15:44:01 +02004124 // remember the type and drop it
4125 --stack->ga_len;
4126 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127
Bram Moolenaara5565e42020-05-09 15:44:01 +02004128 end_idx = instr->ga_len;
4129 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4130
4131 // jump here from JUMP_IF_FALSE
4132 isn = ((isn_T *)instr->ga_data) + alt_idx;
4133 isn->isn_arg.jump.jump_where = instr->ga_len;
4134 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004135
4136 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004137 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138 if (*p != ':')
4139 {
4140 emsg(_(e_missing_colon));
4141 return FAIL;
4142 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004143 if (next != NULL)
4144 {
4145 *arg = next_line_from_context(cctx, TRUE);
4146 p = skipwhite(*arg);
4147 }
4148
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004149 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4150 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004151 semsg(_(e_white_space_required_before_and_after_str), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004152 return FAIL;
4153 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154
4155 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004156 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004157 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4158 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004160 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004161 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004162 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004163 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004164
Bram Moolenaara5565e42020-05-09 15:44:01 +02004165 if (!has_const_expr)
4166 {
4167 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168
Bram Moolenaara5565e42020-05-09 15:44:01 +02004169 // If the types differ, the result has a more generic type.
4170 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4171 common_type(type1, type2, &type2, cctx->ctx_type_list);
4172
4173 // jump here from JUMP_ALWAYS
4174 isn = ((isn_T *)instr->ga_data) + end_idx;
4175 isn->isn_arg.jump.jump_where = instr->ga_len;
4176 }
4177
4178 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 }
4180 return OK;
4181}
4182
4183/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004184 * Toplevel expression.
4185 */
4186 static int
4187compile_expr0(char_u **arg, cctx_T *cctx)
4188{
4189 ppconst_T ppconst;
4190
4191 CLEAR_FIELD(ppconst);
4192 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4193 {
4194 clear_ppconst(&ppconst);
4195 return FAIL;
4196 }
4197 if (generate_ppconst(cctx, &ppconst) == FAIL)
4198 return FAIL;
4199 return OK;
4200}
4201
4202/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004203 * compile "return [expr]"
4204 */
4205 static char_u *
4206compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4207{
4208 char_u *p = arg;
4209 garray_T *stack = &cctx->ctx_type_stack;
4210 type_T *stack_type;
4211
4212 if (*p != NUL && *p != '|' && *p != '\n')
4213 {
4214 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004215 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216 return NULL;
4217
4218 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4219 if (set_return_type)
4220 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004221 else
4222 {
4223 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4224 && stack_type->tt_type != VAR_VOID
4225 && stack_type->tt_type != VAR_UNKNOWN)
4226 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004227 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004228 return NULL;
4229 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004230 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4231 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004233 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234 }
4235 else
4236 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004237 // "set_return_type" cannot be TRUE, only used for a lambda which
4238 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004239 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4240 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004242 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004243 return NULL;
4244 }
4245
4246 // No argument, return zero.
4247 generate_PUSHNR(cctx, 0);
4248 }
4249
4250 if (generate_instr(cctx, ISN_RETURN) == NULL)
4251 return NULL;
4252
4253 // "return val | endif" is possible
4254 return skipwhite(p);
4255}
4256
4257/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004258 * Get a line from the compilation context, compatible with exarg_T getline().
4259 * Return a pointer to the line in allocated memory.
4260 * Return NULL for end-of-file or some error.
4261 */
4262 static char_u *
4263exarg_getline(
4264 int c UNUSED,
4265 void *cookie,
4266 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004267 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004268{
4269 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004270 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004271
Bram Moolenaar66250c92020-08-20 15:02:42 +02004272 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004273 {
Bram Moolenaar66250c92020-08-20 15:02:42 +02004274 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4275 return NULL;
4276 ++cctx->ctx_lnum;
4277 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4278 // Comment lines result in NULL pointers, skip them.
4279 if (p != NULL)
4280 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004281 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004282}
4283
4284/*
4285 * Compile a nested :def command.
4286 */
4287 static char_u *
4288compile_nested_function(exarg_T *eap, cctx_T *cctx)
4289{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004290 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004291 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004292 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004293 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004294 lvar_T *lvar;
4295 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004296 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004297
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004298 // Only g:Func() can use a namespace.
4299 if (name_start[1] == ':' && !is_global)
4300 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004301 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004302 return NULL;
4303 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004304 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4305 return NULL;
4306
Bram Moolenaar04b12692020-05-04 23:24:44 +02004307 eap->arg = name_end;
4308 eap->getline = exarg_getline;
4309 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004310 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004311 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004312 lambda_name = get_lambda_name();
4313 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004314
Bram Moolenaar822ba242020-05-24 23:00:18 +02004315 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004316 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004317 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004318 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004319 return NULL;
4320
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004321 if (is_global)
4322 {
4323 char_u *func_name = vim_strnsave(name_start + 2,
4324 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004325
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004326 if (func_name == NULL)
4327 r = FAIL;
4328 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004329 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004330 }
4331 else
4332 {
4333 // Define a local variable for the function reference.
4334 lvar = reserve_local(cctx, name_start, name_end - name_start,
4335 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004336 if (lvar == NULL)
4337 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004338 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004339 return NULL;
4340 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4341 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004342
Bram Moolenaar61a89812020-05-07 16:58:17 +02004343 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004344 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004345}
4346
4347/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348 * Return the length of an assignment operator, or zero if there isn't one.
4349 */
4350 int
4351assignment_len(char_u *p, int *heredoc)
4352{
4353 if (*p == '=')
4354 {
4355 if (p[1] == '<' && p[2] == '<')
4356 {
4357 *heredoc = TRUE;
4358 return 3;
4359 }
4360 return 1;
4361 }
4362 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4363 return 2;
4364 if (STRNCMP(p, "..=", 3) == 0)
4365 return 3;
4366 return 0;
4367}
4368
4369// words that cannot be used as a variable
4370static char *reserved[] = {
4371 "true",
4372 "false",
4373 NULL
4374};
4375
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004376typedef enum {
4377 dest_local,
4378 dest_option,
4379 dest_env,
4380 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004381 dest_buffer,
4382 dest_window,
4383 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004384 dest_vimvar,
4385 dest_script,
4386 dest_reg,
4387} assign_dest_T;
4388
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004390 * Generate the load instruction for "name".
4391 */
4392 static void
4393generate_loadvar(
4394 cctx_T *cctx,
4395 assign_dest_T dest,
4396 char_u *name,
4397 lvar_T *lvar,
4398 type_T *type)
4399{
4400 switch (dest)
4401 {
4402 case dest_option:
4403 // TODO: check the option exists
4404 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4405 break;
4406 case dest_global:
4407 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4408 break;
4409 case dest_buffer:
4410 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4411 break;
4412 case dest_window:
4413 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4414 break;
4415 case dest_tab:
4416 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4417 break;
4418 case dest_script:
4419 compile_load_scriptvar(cctx,
4420 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4421 break;
4422 case dest_env:
4423 // Include $ in the name here
4424 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4425 break;
4426 case dest_reg:
4427 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4428 break;
4429 case dest_vimvar:
4430 generate_LOADV(cctx, name + 2, TRUE);
4431 break;
4432 case dest_local:
4433 if (lvar->lv_from_outer)
4434 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4435 NULL, type);
4436 else
4437 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4438 break;
4439 }
4440}
4441
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004442 void
4443vim9_declare_error(char_u *name)
4444{
4445 char *scope = "";
4446
4447 switch (*name)
4448 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004449 case 'g': scope = _("global"); break;
4450 case 'b': scope = _("buffer"); break;
4451 case 'w': scope = _("window"); break;
4452 case 't': scope = _("tab"); break;
4453 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004454 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004455 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004456 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004457 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004458 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004459 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004460 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004461 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004462 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004463}
4464
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004465/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004466 * Compile declaration and assignment:
4467 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004469 * Return NULL for an error.
4470 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004471 */
4472 static char_u *
4473compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4474{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004475 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004477 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478 char_u *ret = NULL;
4479 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004480 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004481 int scriptvar_sid = 0;
4482 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004483 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004485 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004486 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487 int oplen = 0;
4488 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004489 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004490 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004491 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004492 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004493 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004494
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004495 // Skip over the "var" or "[var, var]" to get to any "=".
4496 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4497 if (p == NULL)
4498 return *arg == '[' ? arg : NULL;
4499
4500 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004501 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004502 // TODO: should we allow this, and figure out type inference from list
4503 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004504 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004505 return NULL;
4506 }
4507
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508 sp = p;
4509 p = skipwhite(p);
4510 op = p;
4511 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004512
4513 if (var_count > 0 && oplen == 0)
4514 // can be something like "[1, 2]->func()"
4515 return arg;
4516
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004517 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4518 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004519 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004520 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004521 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004522
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523 if (heredoc)
4524 {
4525 list_T *l;
4526 listitem_T *li;
4527
4528 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004529 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004530 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004531 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004532
4533 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004534 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004535 {
4536 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4537 li->li_tv.vval.v_string = NULL;
4538 }
4539 generate_NEWLIST(cctx, l->lv_len);
4540 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004541 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542 list_free(l);
4543 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004544 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004546 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004547 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004548 // for "[var, var] = expr" evaluate the expression here, loop over the
4549 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004550
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004551 p = skipwhite(op + oplen);
4552 if (compile_expr0(&p, cctx) == FAIL)
4553 return NULL;
4554 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004556 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004557 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004558 type_T *stacktype;
4559
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004560 stacktype = stack->ga_len == 0 ? &t_void
4561 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004562 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004563 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004564 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004565 goto theend;
4566 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004567 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004568 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004569 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004570 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4571 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004572 }
4573 }
4574
4575 /*
4576 * Loop over variables in "[var, var] = expr".
4577 * For "var = expr" and "let var: type" this is done only once.
4578 */
4579 if (var_count > 0)
4580 var_start = skipwhite(arg + 1); // skip over the "["
4581 else
4582 var_start = arg;
4583 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4584 {
4585 char_u *var_end = skip_var_one(var_start, FALSE);
4586 size_t varlen;
4587 int new_local = FALSE;
4588 int opt_type;
4589 int opt_flags = 0;
4590 assign_dest_T dest = dest_local;
4591 int vimvaridx = -1;
4592 lvar_T *lvar = NULL;
4593 lvar_T arg_lvar;
4594 int has_type = FALSE;
4595 int has_index = FALSE;
4596 int instr_count = -1;
4597
Bram Moolenaar65821722020-08-02 18:58:54 +02004598 if (*var_start == '@')
4599 p = var_start + 2;
4600 else
4601 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02004602 // skip over the leading "&", "&l:", "&g:" and "$"
4603 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02004604 p = to_name_end(p, TRUE);
4605 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004606
4607 // "a: type" is declaring variable "a" with a type, not "a:".
4608 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4609 --var_end;
4610 if (is_decl && p == var_start + 2 && p[-1] == ':')
4611 --p;
4612
4613 varlen = p - var_start;
4614 vim_free(name);
4615 name = vim_strnsave(var_start, varlen);
4616 if (name == NULL)
4617 return NULL;
4618 if (!heredoc)
4619 type = &t_any;
4620
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004621 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004622 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004623 int declare_error = FALSE;
4624
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004625 if (*var_start == '&')
4626 {
4627 int cc;
4628 long numval;
4629
4630 dest = dest_option;
4631 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004632 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004633 emsg(_(e_const_option));
4634 goto theend;
4635 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004636 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004637 p = var_start;
4638 p = find_option_end(&p, &opt_flags);
4639 if (p == NULL)
4640 {
4641 // cannot happen?
4642 emsg(_(e_letunexp));
4643 goto theend;
4644 }
4645 cc = *p;
4646 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02004647 opt_type = get_option_value(skip_option_env_lead(var_start),
4648 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004649 *p = cc;
4650 if (opt_type == -3)
4651 {
4652 semsg(_(e_unknown_option), var_start);
4653 goto theend;
4654 }
4655 if (opt_type == -2 || opt_type == 0)
4656 type = &t_string;
4657 else
4658 type = &t_number; // both number and boolean option
4659 }
4660 else if (*var_start == '$')
4661 {
4662 dest = dest_env;
4663 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004664 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004665 }
4666 else if (*var_start == '@')
4667 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004668 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004669 {
4670 emsg_invreg(var_start[1]);
4671 goto theend;
4672 }
4673 dest = dest_reg;
4674 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004675 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004676 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004677 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004678 {
4679 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004680 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004681 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004682 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004683 {
4684 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004685 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004686 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004687 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004688 {
4689 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004690 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004691 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004692 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004693 {
4694 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004695 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004696 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004697 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004698 {
4699 typval_T *vtv;
4700 int di_flags;
4701
4702 vimvaridx = find_vim_var(name + 2, &di_flags);
4703 if (vimvaridx < 0)
4704 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004705 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004706 goto theend;
4707 }
4708 // We use the current value of "sandbox" here, is that OK?
4709 if (var_check_ro(di_flags, name, FALSE))
4710 goto theend;
4711 dest = dest_vimvar;
4712 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004713 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004714 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004715 }
4716 else
4717 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004718 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004719
4720 for (idx = 0; reserved[idx] != NULL; ++idx)
4721 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004722 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004723 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004724 goto theend;
4725 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004726
4727 lvar = lookup_local(var_start, varlen, cctx);
4728 if (lvar == NULL)
4729 {
4730 CLEAR_FIELD(arg_lvar);
4731 if (lookup_arg(var_start, varlen,
4732 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4733 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004734 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004735 if (is_decl)
4736 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004737 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004738 goto theend;
4739 }
4740 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004741 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004742 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004743 if (lvar != NULL)
4744 {
4745 if (is_decl)
4746 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004747 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004748 goto theend;
4749 }
4750 else if (lvar->lv_const)
4751 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004752 semsg(_(e_cannot_assign_to_constant), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004753 goto theend;
4754 }
4755 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004756 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004757 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004758 int script_namespace = varlen > 1
4759 && STRNCMP(var_start, "s:", 2) == 0;
4760 int script_var = (script_namespace
Bram Moolenaar53900992020-08-22 19:02:02 +02004761 ? lookup_script(var_start + 2, varlen - 2, FALSE)
4762 : lookup_script(var_start, varlen, TRUE)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004763 imported_T *import =
4764 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004765
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004766 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004767 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004768 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4769
4770 if (is_decl)
4771 {
4772 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004773 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004774 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004775 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004776 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004777 name);
4778 goto theend;
4779 }
4780 else if (cctx->ctx_ufunc->uf_script_ctx_version
4781 == SCRIPT_VERSION_VIM9
4782 && script_namespace
4783 && !script_var && import == NULL)
4784 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004785 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004786 goto theend;
4787 }
4788
4789 dest = dest_script;
4790
4791 // existing script-local variables should have a type
4792 scriptvar_sid = current_sctx.sc_sid;
4793 if (import != NULL)
4794 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004795 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004796 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02004797 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4798 rawname, TRUE);
4799 if (scriptvar_idx > 0)
4800 {
4801 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4802 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004803 ((svar_T *)si->sn_var_vals.ga_data)
4804 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004805 type = sv->sv_type;
4806 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004807 }
4808 }
4809 else if (name[1] == ':' && name[2] != NUL)
4810 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004811 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004812 goto theend;
4813 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004814 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004815 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004816 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004817 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004818 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004819 else if (check_defined(var_start, varlen, cctx) == FAIL)
4820 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004821 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004822 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004823
4824 if (declare_error)
4825 {
4826 vim9_declare_error(name);
4827 goto theend;
4828 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004829 }
4830
4831 // handle "a:name" as a name, not index "name" on "a"
4832 if (varlen > 1 || var_start[varlen] != ':')
4833 p = var_end;
4834
4835 if (dest != dest_option)
4836 {
4837 if (is_decl && *p == ':')
4838 {
4839 // parse optional type: "let var: type = expr"
4840 if (!VIM_ISWHITE(p[1]))
4841 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004842 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004843 goto theend;
4844 }
4845 p = skipwhite(p + 1);
4846 type = parse_type(&p, cctx->ctx_type_list);
4847 has_type = TRUE;
4848 }
4849 else if (lvar != NULL)
4850 type = lvar->lv_type;
4851 }
4852
4853 if (oplen == 3 && !heredoc && dest != dest_global
4854 && type->tt_type != VAR_STRING
4855 && type->tt_type != VAR_ANY)
4856 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004857 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004858 goto theend;
4859 }
4860
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004861 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004862 {
4863 if (oplen > 1 && !heredoc)
4864 {
4865 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004866 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004867 goto theend;
4868 }
4869
4870 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004871 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
4872 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004873 goto theend;
4874 lvar = reserve_local(cctx, var_start, varlen,
4875 cmdidx == CMD_const, type);
4876 if (lvar == NULL)
4877 goto theend;
4878 new_local = TRUE;
4879 }
4880
4881 member_type = type;
4882 if (var_end > var_start + varlen)
4883 {
4884 // Something follows after the variable: "var[idx]".
4885 if (is_decl)
4886 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004887 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004888 goto theend;
4889 }
4890
4891 if (var_start[varlen] == '[')
4892 {
4893 has_index = TRUE;
4894 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004895 member_type = &t_any;
4896 else
4897 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004898 }
4899 else
4900 {
4901 semsg("Not supported yet: %s", var_start);
4902 goto theend;
4903 }
4904 }
4905 else if (lvar == &arg_lvar)
4906 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004907 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004908 goto theend;
4909 }
4910
4911 if (!heredoc)
4912 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004913 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004914 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004915 if (oplen > 0 && var_count == 0)
4916 {
4917 // skip over the "=" and the expression
4918 p = skipwhite(op + oplen);
4919 compile_expr0(&p, cctx);
4920 }
4921 }
4922 else if (oplen > 0)
4923 {
4924 type_T *stacktype;
4925
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004926 // For "var = expr" evaluate the expression.
4927 if (var_count == 0)
4928 {
4929 int r;
4930
4931 // for "+=", "*=", "..=" etc. first load the current value
4932 if (*op != '=')
4933 {
4934 generate_loadvar(cctx, dest, name, lvar, type);
4935
4936 if (has_index)
4937 {
4938 // TODO: get member from list or dict
4939 emsg("Index with operation not supported yet");
4940 goto theend;
4941 }
4942 }
4943
4944 // Compile the expression. Temporarily hide the new local
4945 // variable here, it is not available to this expression.
4946 if (new_local)
4947 --cctx->ctx_locals.ga_len;
4948 instr_count = instr->ga_len;
4949 p = skipwhite(op + oplen);
4950 r = compile_expr0(&p, cctx);
4951 if (new_local)
4952 ++cctx->ctx_locals.ga_len;
4953 if (r == FAIL)
4954 goto theend;
4955 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004956 else if (semicolon && var_idx == var_count - 1)
4957 {
4958 // For "[var; var] = expr" get the rest of the list
4959 if (generate_SLICE(cctx, var_count - 1) == FAIL)
4960 goto theend;
4961 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004962 else
4963 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004964 // For "[var, var] = expr" get the "var_idx" item from the
4965 // list.
4966 if (generate_GETITEM(cctx, var_idx) == FAIL)
4967 return FAIL;
4968 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004969
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004970 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004971 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004972 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004973 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004974 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004975 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004976 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004977 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004978 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004979 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004980 }
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004981 else if ((stacktype->tt_type == VAR_FUNC
4982 || stacktype->tt_type == VAR_PARTIAL)
4983 && var_wrong_func_name(name, TRUE))
4984 {
4985 goto theend;
4986 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004987 else
4988 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004989 // An empty list or dict has a &t_void member,
4990 // for a variable that implies &t_any.
4991 if (stacktype == &t_list_empty)
4992 lvar->lv_type = &t_list_any;
4993 else if (stacktype == &t_dict_empty)
4994 lvar->lv_type = &t_dict_any;
4995 else
4996 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004997 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004998 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02004999 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005000 {
5001 type_T *use_type = lvar->lv_type;
5002
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005003 // without operator type is here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005004 if (has_index)
5005 {
5006 use_type = use_type->tt_member;
5007 if (use_type == NULL)
5008 use_type = &t_void;
5009 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005010 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005011 == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005012 goto theend;
5013 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005014 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005015 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005016 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005017 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005018 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005019 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005020 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005021 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005022 goto theend;
5023 }
5024 else if (!has_type || dest == dest_option)
5025 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005026 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005027 goto theend;
5028 }
5029 else
5030 {
5031 // variables are always initialized
5032 if (ga_grow(instr, 1) == FAIL)
5033 goto theend;
5034 switch (member_type->tt_type)
5035 {
5036 case VAR_BOOL:
5037 generate_PUSHBOOL(cctx, VVAL_FALSE);
5038 break;
5039 case VAR_FLOAT:
5040#ifdef FEAT_FLOAT
5041 generate_PUSHF(cctx, 0.0);
5042#endif
5043 break;
5044 case VAR_STRING:
5045 generate_PUSHS(cctx, NULL);
5046 break;
5047 case VAR_BLOB:
5048 generate_PUSHBLOB(cctx, NULL);
5049 break;
5050 case VAR_FUNC:
5051 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5052 break;
5053 case VAR_LIST:
5054 generate_NEWLIST(cctx, 0);
5055 break;
5056 case VAR_DICT:
5057 generate_NEWDICT(cctx, 0);
5058 break;
5059 case VAR_JOB:
5060 generate_PUSHJOB(cctx, NULL);
5061 break;
5062 case VAR_CHANNEL:
5063 generate_PUSHCHANNEL(cctx, NULL);
5064 break;
5065 case VAR_NUMBER:
5066 case VAR_UNKNOWN:
5067 case VAR_ANY:
5068 case VAR_PARTIAL:
5069 case VAR_VOID:
5070 case VAR_SPECIAL: // cannot happen
5071 generate_PUSHNR(cctx, 0);
5072 break;
5073 }
5074 }
5075 if (var_count == 0)
5076 end = p;
5077 }
5078
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005079 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005080 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005081 break;
5082
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005083 if (oplen > 0 && *op != '=')
5084 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005085 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005086 type_T *stacktype;
5087
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005088 if (*op == '.')
5089 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005090 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005091 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005092 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005093 if (
5094#ifdef FEAT_FLOAT
5095 // If variable is float operation with number is OK.
5096 !(expected == &t_float && stacktype == &t_number) &&
5097#endif
5098 need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005099 goto theend;
5100
5101 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005102 {
5103 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5104 goto theend;
5105 }
5106 else if (*op == '+')
5107 {
5108 if (generate_add_instr(cctx,
5109 operator_type(member_type, stacktype),
5110 member_type, stacktype) == FAIL)
5111 goto theend;
5112 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005113 else if (generate_two_op(cctx, op) == FAIL)
5114 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005115 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005116
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005117 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005118 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005119 int r;
5120
5121 // Compile the "idx" in "var[idx]".
5122 if (new_local)
5123 --cctx->ctx_locals.ga_len;
5124 p = skipwhite(var_start + varlen + 1);
5125 r = compile_expr0(&p, cctx);
5126 if (new_local)
5127 ++cctx->ctx_locals.ga_len;
5128 if (r == FAIL)
5129 goto theend;
5130 if (*skipwhite(p) != ']')
5131 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005132 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005133 emsg(_(e_missbrac));
5134 goto theend;
5135 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005136 if (type == &t_any)
5137 {
5138 type_T *idx_type = ((type_T **)stack->ga_data)[
5139 stack->ga_len - 1];
5140 // Index on variable of unknown type: guess the type from the
5141 // index type: number is dict, otherwise dict.
5142 // TODO: should do the assignment at runtime
5143 if (idx_type->tt_type == VAR_NUMBER)
5144 type = &t_list_any;
5145 else
5146 type = &t_dict_any;
5147 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005148 if (type->tt_type == VAR_DICT
5149 && may_generate_2STRING(-1, cctx) == FAIL)
5150 goto theend;
5151 if (type->tt_type == VAR_LIST
5152 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005153 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005154 {
5155 emsg(_(e_number_exp));
5156 goto theend;
5157 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005158
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005159 // Load the dict or list. On the stack we then have:
5160 // - value
5161 // - index
5162 // - variable
5163 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005164
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005165 if (type->tt_type == VAR_LIST)
5166 {
5167 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5168 return FAIL;
5169 }
5170 else if (type->tt_type == VAR_DICT)
5171 {
5172 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5173 return FAIL;
5174 }
5175 else
5176 {
5177 emsg(_(e_listreq));
5178 goto theend;
5179 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005180 }
5181 else
5182 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005183 switch (dest)
5184 {
5185 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005186 generate_STOREOPT(cctx, skip_option_env_lead(name),
5187 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005188 break;
5189 case dest_global:
5190 // include g: with the name, easier to execute that way
5191 generate_STORE(cctx, ISN_STOREG, 0, name);
5192 break;
5193 case dest_buffer:
5194 // include b: with the name, easier to execute that way
5195 generate_STORE(cctx, ISN_STOREB, 0, name);
5196 break;
5197 case dest_window:
5198 // include w: with the name, easier to execute that way
5199 generate_STORE(cctx, ISN_STOREW, 0, name);
5200 break;
5201 case dest_tab:
5202 // include t: with the name, easier to execute that way
5203 generate_STORE(cctx, ISN_STORET, 0, name);
5204 break;
5205 case dest_env:
5206 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5207 break;
5208 case dest_reg:
5209 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5210 break;
5211 case dest_vimvar:
5212 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5213 break;
5214 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005215 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005216 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005217 {
5218 char_u *name_s = name;
5219
5220 // Include s: in the name for store_var()
5221 if (name[1] != ':')
5222 {
5223 int len = (int)STRLEN(name) + 3;
5224
5225 name_s = alloc(len);
5226 if (name_s == NULL)
5227 name_s = name;
5228 else
5229 vim_snprintf((char *)name_s, len,
5230 "s:%s", name);
5231 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005232 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5233 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005234 if (name_s != name)
5235 vim_free(name_s);
5236 }
5237 else
5238 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005239 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005240 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005241 break;
5242 case dest_local:
5243 if (lvar != NULL)
5244 {
5245 isn_T *isn = ((isn_T *)instr->ga_data)
5246 + instr->ga_len - 1;
5247
5248 // optimization: turn "var = 123" from ISN_PUSHNR +
5249 // ISN_STORE into ISN_STORENR
5250 if (!lvar->lv_from_outer
5251 && instr->ga_len == instr_count + 1
5252 && isn->isn_type == ISN_PUSHNR)
5253 {
5254 varnumber_T val = isn->isn_arg.number;
5255
5256 isn->isn_type = ISN_STORENR;
5257 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5258 isn->isn_arg.storenr.stnr_val = val;
5259 if (stack->ga_len > 0)
5260 --stack->ga_len;
5261 }
5262 else if (lvar->lv_from_outer)
5263 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005264 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005265 else
5266 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5267 }
5268 break;
5269 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005270 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005271
5272 if (var_idx + 1 < var_count)
5273 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005274 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005275
5276 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005277 if (var_count > 0 && !semicolon)
5278 {
5279 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5280 goto theend;
5281 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005282
Bram Moolenaarb2097502020-07-19 17:17:02 +02005283 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005284
5285theend:
5286 vim_free(name);
5287 return ret;
5288}
5289
5290/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005291 * Check if "name" can be "unlet".
5292 */
5293 int
5294check_vim9_unlet(char_u *name)
5295{
5296 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5297 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005298 // "unlet s:var" is allowed in legacy script.
5299 if (*name == 's' && !script_is_vim9())
5300 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005301 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005302 return FAIL;
5303 }
5304 return OK;
5305}
5306
5307/*
5308 * Callback passed to ex_unletlock().
5309 */
5310 static int
5311compile_unlet(
5312 lval_T *lvp,
5313 char_u *name_end,
5314 exarg_T *eap,
5315 int deep UNUSED,
5316 void *coookie)
5317{
5318 cctx_T *cctx = coookie;
5319
5320 if (lvp->ll_tv == NULL)
5321 {
5322 char_u *p = lvp->ll_name;
5323 int cc = *name_end;
5324 int ret = OK;
5325
5326 // Normal name. Only supports g:, w:, t: and b: namespaces.
5327 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005328 if (*p == '$')
5329 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5330 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005331 ret = FAIL;
5332 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005333 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005334
5335 *name_end = cc;
5336 return ret;
5337 }
5338
5339 // TODO: unlet {list}[idx]
5340 // TODO: unlet {dict}[key]
5341 emsg("Sorry, :unlet not fully implemented yet");
5342 return FAIL;
5343}
5344
5345/*
5346 * compile "unlet var", "lock var" and "unlock var"
5347 * "arg" points to "var".
5348 */
5349 static char_u *
5350compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5351{
5352 char_u *p = arg;
5353
5354 if (eap->cmdidx != CMD_unlet)
5355 {
5356 emsg("Sorry, :lock and unlock not implemented yet");
5357 return NULL;
5358 }
5359
5360 if (*p == '!')
5361 {
5362 p = skipwhite(p + 1);
5363 eap->forceit = TRUE;
5364 }
5365
5366 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5367 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5368}
5369
5370/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005371 * Compile an :import command.
5372 */
5373 static char_u *
5374compile_import(char_u *arg, cctx_T *cctx)
5375{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005376 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005377}
5378
5379/*
5380 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5381 */
5382 static int
5383compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5384{
5385 garray_T *instr = &cctx->ctx_instr;
5386 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5387
5388 if (endlabel == NULL)
5389 return FAIL;
5390 endlabel->el_next = *el;
5391 *el = endlabel;
5392 endlabel->el_end_label = instr->ga_len;
5393
5394 generate_JUMP(cctx, when, 0);
5395 return OK;
5396}
5397
5398 static void
5399compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5400{
5401 garray_T *instr = &cctx->ctx_instr;
5402
5403 while (*el != NULL)
5404 {
5405 endlabel_T *cur = (*el);
5406 isn_T *isn;
5407
5408 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5409 isn->isn_arg.jump.jump_where = instr->ga_len;
5410 *el = cur->el_next;
5411 vim_free(cur);
5412 }
5413}
5414
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005415 static void
5416compile_free_jump_to_end(endlabel_T **el)
5417{
5418 while (*el != NULL)
5419 {
5420 endlabel_T *cur = (*el);
5421
5422 *el = cur->el_next;
5423 vim_free(cur);
5424 }
5425}
5426
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005427/*
5428 * Create a new scope and set up the generic items.
5429 */
5430 static scope_T *
5431new_scope(cctx_T *cctx, scopetype_T type)
5432{
5433 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5434
5435 if (scope == NULL)
5436 return NULL;
5437 scope->se_outer = cctx->ctx_scope;
5438 cctx->ctx_scope = scope;
5439 scope->se_type = type;
5440 scope->se_local_count = cctx->ctx_locals.ga_len;
5441 return scope;
5442}
5443
5444/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005445 * Free the current scope and go back to the outer scope.
5446 */
5447 static void
5448drop_scope(cctx_T *cctx)
5449{
5450 scope_T *scope = cctx->ctx_scope;
5451
5452 if (scope == NULL)
5453 {
5454 iemsg("calling drop_scope() without a scope");
5455 return;
5456 }
5457 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005458 switch (scope->se_type)
5459 {
5460 case IF_SCOPE:
5461 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5462 case FOR_SCOPE:
5463 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5464 case WHILE_SCOPE:
5465 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5466 case TRY_SCOPE:
5467 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5468 case NO_SCOPE:
5469 case BLOCK_SCOPE:
5470 break;
5471 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005472 vim_free(scope);
5473}
5474
5475/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005476 * compile "if expr"
5477 *
5478 * "if expr" Produces instructions:
5479 * EVAL expr Push result of "expr"
5480 * JUMP_IF_FALSE end
5481 * ... body ...
5482 * end:
5483 *
5484 * "if expr | else" Produces instructions:
5485 * EVAL expr Push result of "expr"
5486 * JUMP_IF_FALSE else
5487 * ... body ...
5488 * JUMP_ALWAYS end
5489 * else:
5490 * ... body ...
5491 * end:
5492 *
5493 * "if expr1 | elseif expr2 | else" Produces instructions:
5494 * EVAL expr Push result of "expr"
5495 * JUMP_IF_FALSE elseif
5496 * ... body ...
5497 * JUMP_ALWAYS end
5498 * elseif:
5499 * EVAL expr Push result of "expr"
5500 * JUMP_IF_FALSE else
5501 * ... body ...
5502 * JUMP_ALWAYS end
5503 * else:
5504 * ... body ...
5505 * end:
5506 */
5507 static char_u *
5508compile_if(char_u *arg, cctx_T *cctx)
5509{
5510 char_u *p = arg;
5511 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005512 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005513 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005514 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005515 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005516
Bram Moolenaara5565e42020-05-09 15:44:01 +02005517 CLEAR_FIELD(ppconst);
5518 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005519 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005520 clear_ppconst(&ppconst);
5521 return NULL;
5522 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005523 if (cctx->ctx_skip == SKIP_YES)
5524 clear_ppconst(&ppconst);
5525 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005526 {
5527 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005528 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005529 clear_ppconst(&ppconst);
5530 }
5531 else
5532 {
5533 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005534 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005535 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005536 return NULL;
5537 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005538
5539 scope = new_scope(cctx, IF_SCOPE);
5540 if (scope == NULL)
5541 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005542 scope->se_skip_save = skip_save;
5543 // "is_had_return" will be reset if any block does not end in :return
5544 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005545
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005546 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005547 {
5548 // "where" is set when ":elseif", "else" or ":endif" is found
5549 scope->se_u.se_if.is_if_label = instr->ga_len;
5550 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5551 }
5552 else
5553 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005554
5555 return p;
5556}
5557
5558 static char_u *
5559compile_elseif(char_u *arg, cctx_T *cctx)
5560{
5561 char_u *p = arg;
5562 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005563 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005564 isn_T *isn;
5565 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005566 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02005567 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005568
5569 if (scope == NULL || scope->se_type != IF_SCOPE)
5570 {
5571 emsg(_(e_elseif_without_if));
5572 return NULL;
5573 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005574 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005575 if (!cctx->ctx_had_return)
5576 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005577
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005578 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005579 {
5580 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005581 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005582 return NULL;
5583 // previous "if" or "elseif" jumps here
5584 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5585 isn->isn_arg.jump.jump_where = instr->ga_len;
5586 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005587
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005588 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005589 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02005590 if (cctx->ctx_skip == SKIP_YES)
5591 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005592 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005593 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005594 clear_ppconst(&ppconst);
5595 return NULL;
5596 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02005597 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005598 if (scope->se_skip_save == SKIP_YES)
5599 clear_ppconst(&ppconst);
5600 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005601 {
5602 // The expression results in a constant.
5603 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005604 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005605 clear_ppconst(&ppconst);
5606 scope->se_u.se_if.is_if_label = -1;
5607 }
5608 else
5609 {
5610 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005611 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005612 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005613 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005614
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005615 // "where" is set when ":elseif", "else" or ":endif" is found
5616 scope->se_u.se_if.is_if_label = instr->ga_len;
5617 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5618 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005619
5620 return p;
5621}
5622
5623 static char_u *
5624compile_else(char_u *arg, cctx_T *cctx)
5625{
5626 char_u *p = arg;
5627 garray_T *instr = &cctx->ctx_instr;
5628 isn_T *isn;
5629 scope_T *scope = cctx->ctx_scope;
5630
5631 if (scope == NULL || scope->se_type != IF_SCOPE)
5632 {
5633 emsg(_(e_else_without_if));
5634 return NULL;
5635 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005636 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005637 if (!cctx->ctx_had_return)
5638 scope->se_u.se_if.is_had_return = FALSE;
5639 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005640
Bram Moolenaarefd88552020-06-18 20:50:10 +02005641 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005642 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005643 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005644 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005645 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005646 if (!cctx->ctx_had_return
5647 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5648 JUMP_ALWAYS, cctx) == FAIL)
5649 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005650 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005651
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005652 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005653 {
5654 if (scope->se_u.se_if.is_if_label >= 0)
5655 {
5656 // previous "if" or "elseif" jumps here
5657 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5658 isn->isn_arg.jump.jump_where = instr->ga_len;
5659 scope->se_u.se_if.is_if_label = -1;
5660 }
5661 }
5662
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005663 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005664 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5665 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005666
5667 return p;
5668}
5669
5670 static char_u *
5671compile_endif(char_u *arg, cctx_T *cctx)
5672{
5673 scope_T *scope = cctx->ctx_scope;
5674 ifscope_T *ifscope;
5675 garray_T *instr = &cctx->ctx_instr;
5676 isn_T *isn;
5677
5678 if (scope == NULL || scope->se_type != IF_SCOPE)
5679 {
5680 emsg(_(e_endif_without_if));
5681 return NULL;
5682 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005683 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005684 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005685 if (!cctx->ctx_had_return)
5686 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005687
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005688 if (scope->se_u.se_if.is_if_label >= 0)
5689 {
5690 // previous "if" or "elseif" jumps here
5691 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5692 isn->isn_arg.jump.jump_where = instr->ga_len;
5693 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005694 // Fill in the "end" label in jumps at the end of the blocks.
5695 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005696 cctx->ctx_skip = scope->se_skip_save;
5697
5698 // If all the blocks end in :return and there is an :else then the
5699 // had_return flag is set.
5700 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005701
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005702 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005703 return arg;
5704}
5705
5706/*
5707 * compile "for var in expr"
5708 *
5709 * Produces instructions:
5710 * PUSHNR -1
5711 * STORE loop-idx Set index to -1
5712 * EVAL expr Push result of "expr"
5713 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5714 * - if beyond end, jump to "end"
5715 * - otherwise get item from list and push it
5716 * STORE var Store item in "var"
5717 * ... body ...
5718 * JUMP top Jump back to repeat
5719 * end: DROP Drop the result of "expr"
5720 *
5721 */
5722 static char_u *
5723compile_for(char_u *arg, cctx_T *cctx)
5724{
5725 char_u *p;
5726 size_t varlen;
5727 garray_T *instr = &cctx->ctx_instr;
5728 garray_T *stack = &cctx->ctx_type_stack;
5729 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005730 lvar_T *loop_lvar; // loop iteration variable
5731 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005732 type_T *vartype;
5733
5734 // TODO: list of variables: "for [key, value] in dict"
5735 // parse "var"
5736 for (p = arg; eval_isnamec1(*p); ++p)
5737 ;
5738 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005739 var_lvar = lookup_local(arg, varlen, cctx);
5740 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005741 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005742 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005743 return NULL;
5744 }
5745
5746 // consume "in"
5747 p = skipwhite(p);
5748 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5749 {
5750 emsg(_(e_missing_in));
5751 return NULL;
5752 }
5753 p = skipwhite(p + 2);
5754
5755
5756 scope = new_scope(cctx, FOR_SCOPE);
5757 if (scope == NULL)
5758 return NULL;
5759
5760 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005761 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5762 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005763 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005764 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005765 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005766 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005767 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005768
5769 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005770 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5771 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005772 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005773 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005774 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005775 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005776 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005777
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005778 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005779
5780 // compile "expr", it remains on the stack until "endfor"
5781 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005782 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005783 {
5784 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005785 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005786 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005787
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005788 // Now that we know the type of "var", check that it is a list, now or at
5789 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005790 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005791 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005792 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005793 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005794 return NULL;
5795 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005796 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005797 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005798
5799 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005800 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005801
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005802 generate_FOR(cctx, loop_lvar->lv_idx);
5803 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005804
5805 return arg;
5806}
5807
5808/*
5809 * compile "endfor"
5810 */
5811 static char_u *
5812compile_endfor(char_u *arg, cctx_T *cctx)
5813{
5814 garray_T *instr = &cctx->ctx_instr;
5815 scope_T *scope = cctx->ctx_scope;
5816 forscope_T *forscope;
5817 isn_T *isn;
5818
5819 if (scope == NULL || scope->se_type != FOR_SCOPE)
5820 {
5821 emsg(_(e_for));
5822 return NULL;
5823 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005824 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005825 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005826 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005827
5828 // At end of ":for" scope jump back to the FOR instruction.
5829 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5830
5831 // Fill in the "end" label in the FOR statement so it can jump here
5832 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5833 isn->isn_arg.forloop.for_end = instr->ga_len;
5834
5835 // Fill in the "end" label any BREAK statements
5836 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5837
5838 // Below the ":for" scope drop the "expr" list from the stack.
5839 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5840 return NULL;
5841
5842 vim_free(scope);
5843
5844 return arg;
5845}
5846
5847/*
5848 * compile "while expr"
5849 *
5850 * Produces instructions:
5851 * top: EVAL expr Push result of "expr"
5852 * JUMP_IF_FALSE end jump if false
5853 * ... body ...
5854 * JUMP top Jump back to repeat
5855 * end:
5856 *
5857 */
5858 static char_u *
5859compile_while(char_u *arg, cctx_T *cctx)
5860{
5861 char_u *p = arg;
5862 garray_T *instr = &cctx->ctx_instr;
5863 scope_T *scope;
5864
5865 scope = new_scope(cctx, WHILE_SCOPE);
5866 if (scope == NULL)
5867 return NULL;
5868
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005869 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005870
5871 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005872 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005873 return NULL;
5874
5875 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005876 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005877 JUMP_IF_FALSE, cctx) == FAIL)
5878 return FAIL;
5879
5880 return p;
5881}
5882
5883/*
5884 * compile "endwhile"
5885 */
5886 static char_u *
5887compile_endwhile(char_u *arg, cctx_T *cctx)
5888{
5889 scope_T *scope = cctx->ctx_scope;
5890
5891 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5892 {
5893 emsg(_(e_while));
5894 return NULL;
5895 }
5896 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005897 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005898
5899 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005900 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005901
5902 // Fill in the "end" label in the WHILE statement so it can jump here.
5903 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005904 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005905
5906 vim_free(scope);
5907
5908 return arg;
5909}
5910
5911/*
5912 * compile "continue"
5913 */
5914 static char_u *
5915compile_continue(char_u *arg, cctx_T *cctx)
5916{
5917 scope_T *scope = cctx->ctx_scope;
5918
5919 for (;;)
5920 {
5921 if (scope == NULL)
5922 {
5923 emsg(_(e_continue));
5924 return NULL;
5925 }
5926 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5927 break;
5928 scope = scope->se_outer;
5929 }
5930
5931 // Jump back to the FOR or WHILE instruction.
5932 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005933 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5934 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005935 return arg;
5936}
5937
5938/*
5939 * compile "break"
5940 */
5941 static char_u *
5942compile_break(char_u *arg, cctx_T *cctx)
5943{
5944 scope_T *scope = cctx->ctx_scope;
5945 endlabel_T **el;
5946
5947 for (;;)
5948 {
5949 if (scope == NULL)
5950 {
5951 emsg(_(e_break));
5952 return NULL;
5953 }
5954 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5955 break;
5956 scope = scope->se_outer;
5957 }
5958
5959 // Jump to the end of the FOR or WHILE loop.
5960 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005961 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005962 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005963 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005964 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5965 return FAIL;
5966
5967 return arg;
5968}
5969
5970/*
5971 * compile "{" start of block
5972 */
5973 static char_u *
5974compile_block(char_u *arg, cctx_T *cctx)
5975{
5976 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5977 return NULL;
5978 return skipwhite(arg + 1);
5979}
5980
5981/*
5982 * compile end of block: drop one scope
5983 */
5984 static void
5985compile_endblock(cctx_T *cctx)
5986{
5987 scope_T *scope = cctx->ctx_scope;
5988
5989 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005990 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005991 vim_free(scope);
5992}
5993
5994/*
5995 * compile "try"
5996 * Creates a new scope for the try-endtry, pointing to the first catch and
5997 * finally.
5998 * Creates another scope for the "try" block itself.
5999 * TRY instruction sets up exception handling at runtime.
6000 *
6001 * "try"
6002 * TRY -> catch1, -> finally push trystack entry
6003 * ... try block
6004 * "throw {exception}"
6005 * EVAL {exception}
6006 * THROW create exception
6007 * ... try block
6008 * " catch {expr}"
6009 * JUMP -> finally
6010 * catch1: PUSH exeception
6011 * EVAL {expr}
6012 * MATCH
6013 * JUMP nomatch -> catch2
6014 * CATCH remove exception
6015 * ... catch block
6016 * " catch"
6017 * JUMP -> finally
6018 * catch2: CATCH remove exception
6019 * ... catch block
6020 * " finally"
6021 * finally:
6022 * ... finally block
6023 * " endtry"
6024 * ENDTRY pop trystack entry, may rethrow
6025 */
6026 static char_u *
6027compile_try(char_u *arg, cctx_T *cctx)
6028{
6029 garray_T *instr = &cctx->ctx_instr;
6030 scope_T *try_scope;
6031 scope_T *scope;
6032
6033 // scope that holds the jumps that go to catch/finally/endtry
6034 try_scope = new_scope(cctx, TRY_SCOPE);
6035 if (try_scope == NULL)
6036 return NULL;
6037
6038 // "catch" is set when the first ":catch" is found.
6039 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006040 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006041 if (generate_instr(cctx, ISN_TRY) == NULL)
6042 return NULL;
6043
6044 // scope for the try block itself
6045 scope = new_scope(cctx, BLOCK_SCOPE);
6046 if (scope == NULL)
6047 return NULL;
6048
6049 return arg;
6050}
6051
6052/*
6053 * compile "catch {expr}"
6054 */
6055 static char_u *
6056compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6057{
6058 scope_T *scope = cctx->ctx_scope;
6059 garray_T *instr = &cctx->ctx_instr;
6060 char_u *p;
6061 isn_T *isn;
6062
6063 // end block scope from :try or :catch
6064 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6065 compile_endblock(cctx);
6066 scope = cctx->ctx_scope;
6067
6068 // Error if not in a :try scope
6069 if (scope == NULL || scope->se_type != TRY_SCOPE)
6070 {
6071 emsg(_(e_catch));
6072 return NULL;
6073 }
6074
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006075 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006076 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006077 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006078 return NULL;
6079 }
6080
6081 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006082 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006083 JUMP_ALWAYS, cctx) == FAIL)
6084 return NULL;
6085
6086 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006087 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006088 if (isn->isn_arg.try.try_catch == 0)
6089 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006090 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006091 {
6092 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006093 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006094 isn->isn_arg.jump.jump_where = instr->ga_len;
6095 }
6096
6097 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006098 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006099 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006100 scope->se_u.se_try.ts_caught_all = TRUE;
6101 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006102 }
6103 else
6104 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006105 char_u *end;
6106 char_u *pat;
6107 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006108 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006109 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006110
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006111 // Push v:exception, push {expr} and MATCH
6112 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6113
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006114 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006115 if (*end != *p)
6116 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006117 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006118 vim_free(tofree);
6119 return FAIL;
6120 }
6121 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006122 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006123 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006124 len = (int)(end - tofree);
6125 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006126 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006127 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006128 if (pat == NULL)
6129 return FAIL;
6130 if (generate_PUSHS(cctx, pat) == FAIL)
6131 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006132
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006133 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6134 return NULL;
6135
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006136 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006137 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6138 return NULL;
6139 }
6140
6141 if (generate_instr(cctx, ISN_CATCH) == NULL)
6142 return NULL;
6143
6144 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6145 return NULL;
6146 return p;
6147}
6148
6149 static char_u *
6150compile_finally(char_u *arg, cctx_T *cctx)
6151{
6152 scope_T *scope = cctx->ctx_scope;
6153 garray_T *instr = &cctx->ctx_instr;
6154 isn_T *isn;
6155
6156 // end block scope from :try or :catch
6157 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6158 compile_endblock(cctx);
6159 scope = cctx->ctx_scope;
6160
6161 // Error if not in a :try scope
6162 if (scope == NULL || scope->se_type != TRY_SCOPE)
6163 {
6164 emsg(_(e_finally));
6165 return NULL;
6166 }
6167
6168 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006169 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006170 if (isn->isn_arg.try.try_finally != 0)
6171 {
6172 emsg(_(e_finally_dup));
6173 return NULL;
6174 }
6175
6176 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006177 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006178
Bram Moolenaar585fea72020-04-02 22:33:21 +02006179 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006180 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006181 {
6182 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006183 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006184 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006185 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006186 }
6187
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006188 // TODO: set index in ts_finally_label jumps
6189
6190 return arg;
6191}
6192
6193 static char_u *
6194compile_endtry(char_u *arg, cctx_T *cctx)
6195{
6196 scope_T *scope = cctx->ctx_scope;
6197 garray_T *instr = &cctx->ctx_instr;
6198 isn_T *isn;
6199
6200 // end block scope from :catch or :finally
6201 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6202 compile_endblock(cctx);
6203 scope = cctx->ctx_scope;
6204
6205 // Error if not in a :try scope
6206 if (scope == NULL || scope->se_type != TRY_SCOPE)
6207 {
6208 if (scope == NULL)
6209 emsg(_(e_no_endtry));
6210 else if (scope->se_type == WHILE_SCOPE)
6211 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006212 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006213 emsg(_(e_endfor));
6214 else
6215 emsg(_(e_endif));
6216 return NULL;
6217 }
6218
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006219 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006220 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6221 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006222 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006223 return NULL;
6224 }
6225
6226 // Fill in the "end" label in jumps at the end of the blocks, if not done
6227 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006228 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006229
6230 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006231 if (isn->isn_arg.try.try_catch == 0)
6232 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006233 if (isn->isn_arg.try.try_finally == 0)
6234 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006235
6236 if (scope->se_u.se_try.ts_catch_label != 0)
6237 {
6238 // Last catch without match jumps here
6239 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6240 isn->isn_arg.jump.jump_where = instr->ga_len;
6241 }
6242
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006243 compile_endblock(cctx);
6244
6245 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6246 return NULL;
6247 return arg;
6248}
6249
6250/*
6251 * compile "throw {expr}"
6252 */
6253 static char_u *
6254compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6255{
6256 char_u *p = skipwhite(arg);
6257
Bram Moolenaara5565e42020-05-09 15:44:01 +02006258 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006259 return NULL;
6260 if (may_generate_2STRING(-1, cctx) == FAIL)
6261 return NULL;
6262 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6263 return NULL;
6264
6265 return p;
6266}
6267
6268/*
6269 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006270 * compile "echomsg expr"
6271 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006272 * compile "execute expr"
6273 */
6274 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006275compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006276{
6277 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006278 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006279 int count = 0;
6280
6281 for (;;)
6282 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006283 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006284 return NULL;
6285 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006286 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006287 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006288 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006289 break;
6290 }
6291
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006292 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6293 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6294 else if (cmdidx == CMD_execute)
6295 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6296 else if (cmdidx == CMD_echomsg)
6297 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6298 else
6299 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006300 return p;
6301}
6302
6303/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006304 * :put r
6305 * :put ={expr}
6306 */
6307 static char_u *
6308compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6309{
6310 char_u *line = arg;
6311 linenr_T lnum;
6312 char *errormsg;
6313 int above = FALSE;
6314
6315 if (*arg == '!')
6316 {
6317 above = TRUE;
6318 line = skipwhite(arg + 1);
6319 }
6320 eap->regname = *line;
6321
6322 if (eap->regname == '=')
6323 {
6324 char_u *p = line + 1;
6325
6326 if (compile_expr0(&p, cctx) == FAIL)
6327 return NULL;
6328 line = p;
6329 }
6330 else if (eap->regname != NUL)
6331 ++line;
6332
6333 // TODO: if the range is something like "$" need to evaluate at runtime
6334 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6335 return NULL;
6336 if (eap->addr_count == 0)
6337 lnum = -1;
6338 else
6339 lnum = eap->line2;
6340 if (above)
6341 --lnum;
6342
6343 generate_PUT(cctx, eap->regname, lnum);
6344 return line;
6345}
6346
6347/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006348 * A command that is not compiled, execute with legacy code.
6349 */
6350 static char_u *
6351compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6352{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006353 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006354 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006355 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006356
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006357 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006358 goto theend;
6359
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006360 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006361 {
6362 long argt = excmd_get_argt(eap->cmdidx);
6363 int usefilter = FALSE;
6364
6365 has_expr = argt & (EX_XFILE | EX_EXPAND);
6366
6367 // If the command can be followed by a bar, find the bar and truncate
6368 // it, so that the following command can be compiled.
6369 // The '|' is overwritten with a NUL, it is put back below.
6370 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6371 && *eap->arg == '!')
6372 // :w !filter or :r !filter or :r! filter
6373 usefilter = TRUE;
6374 if ((argt & EX_TRLBAR) && !usefilter)
6375 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006376 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006377 separate_nextcmd(eap);
6378 if (eap->nextcmd != NULL)
6379 nextcmd = eap->nextcmd;
6380 }
6381 }
6382
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006383 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6384 {
6385 // expand filename in "syntax include [@group] filename"
6386 has_expr = TRUE;
6387 eap->arg = skipwhite(eap->arg + 7);
6388 if (*eap->arg == '@')
6389 eap->arg = skiptowhite(eap->arg);
6390 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006391
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006392 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006393 {
6394 int count = 0;
6395 char_u *start = skipwhite(line);
6396
6397 // :cmd xxx`=expr1`yyy`=expr2`zzz
6398 // PUSHS ":cmd xxx"
6399 // eval expr1
6400 // PUSHS "yyy"
6401 // eval expr2
6402 // PUSHS "zzz"
6403 // EXECCONCAT 5
6404 for (;;)
6405 {
6406 if (p > start)
6407 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006408 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006409 ++count;
6410 }
6411 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006412 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006413 return NULL;
6414 may_generate_2STRING(-1, cctx);
6415 ++count;
6416 p = skipwhite(p);
6417 if (*p != '`')
6418 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006419 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006420 return NULL;
6421 }
6422 start = p + 1;
6423
6424 p = (char_u *)strstr((char *)start, "`=");
6425 if (p == NULL)
6426 {
6427 if (*skipwhite(start) != NUL)
6428 {
6429 generate_PUSHS(cctx, vim_strsave(start));
6430 ++count;
6431 }
6432 break;
6433 }
6434 }
6435 generate_EXECCONCAT(cctx, count);
6436 }
6437 else
6438 generate_EXEC(cctx, line);
6439
6440theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006441 if (*nextcmd != NUL)
6442 {
6443 // the parser expects a pointer to the bar, put it back
6444 --nextcmd;
6445 *nextcmd = '|';
6446 }
6447
6448 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006449}
6450
6451/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006452 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006453 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006454 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006455 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006456add_def_function(ufunc_T *ufunc)
6457{
6458 dfunc_T *dfunc;
6459
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006460 if (def_functions.ga_len == 0)
6461 {
6462 // The first position is not used, so that a zero uf_dfunc_idx means it
6463 // wasn't set.
6464 if (ga_grow(&def_functions, 1) == FAIL)
6465 return FAIL;
6466 ++def_functions.ga_len;
6467 }
6468
Bram Moolenaar09689a02020-05-09 22:50:08 +02006469 // Add the function to "def_functions".
6470 if (ga_grow(&def_functions, 1) == FAIL)
6471 return FAIL;
6472 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6473 CLEAR_POINTER(dfunc);
6474 dfunc->df_idx = def_functions.ga_len;
6475 ufunc->uf_dfunc_idx = dfunc->df_idx;
6476 dfunc->df_ufunc = ufunc;
6477 ++def_functions.ga_len;
6478 return OK;
6479}
6480
6481/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006482 * After ex_function() has collected all the function lines: parse and compile
6483 * the lines into instructions.
6484 * Adds the function to "def_functions".
6485 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6486 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006487 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006488 * This can be used recursively through compile_lambda(), which may reallocate
6489 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006490 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006491 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006492 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006493compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006494{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006495 char_u *line = NULL;
6496 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006497 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006498 cctx_T cctx;
6499 garray_T *instr;
6500 int called_emsg_before = called_emsg;
6501 int ret = FAIL;
6502 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006503 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006504 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006505 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006506
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006507 // When using a function that was compiled before: Free old instructions.
6508 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006509 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006510 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006511 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6512 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006513 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006514 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006515 else
6516 {
6517 if (add_def_function(ufunc) == FAIL)
6518 return FAIL;
6519 new_def_function = TRUE;
6520 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006521
Bram Moolenaar985116a2020-07-12 17:31:09 +02006522 ufunc->uf_def_status = UF_COMPILING;
6523
Bram Moolenaara80faa82020-04-12 19:37:17 +02006524 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006525 cctx.ctx_ufunc = ufunc;
6526 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006527 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006528 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6529 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6530 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6531 cctx.ctx_type_list = &ufunc->uf_type_list;
6532 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6533 instr = &cctx.ctx_instr;
6534
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006535 // Set the context to the function, it may be compiled when called from
6536 // another script. Set the script version to the most modern one.
6537 // The line number will be set in next_line_from_context().
6538 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006539 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6540
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006541 // Make sure error messages are OK.
6542 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6543 if (do_estack_push)
6544 estack_push_ufunc(ufunc, 1);
6545
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006546 if (ufunc->uf_def_args.ga_len > 0)
6547 {
6548 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006549 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006550 int i;
6551 char_u *arg;
6552 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006553 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006554
6555 // Produce instructions for the default values of optional arguments.
6556 // Store the instruction index in uf_def_arg_idx[] so that we know
6557 // where to start when the function is called, depending on the number
6558 // of arguments.
6559 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6560 if (ufunc->uf_def_arg_idx == NULL)
6561 goto erret;
6562 for (i = 0; i < count; ++i)
6563 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006564 garray_T *stack = &cctx.ctx_type_stack;
6565 type_T *val_type;
6566 int arg_idx = first_def_arg + i;
6567
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006568 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6569 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006570 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006571 goto erret;
6572
6573 // If no type specified use the type of the default value.
6574 // Otherwise check that the default value type matches the
6575 // specified type.
6576 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6577 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006578 {
6579 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006580 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006581 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02006582 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
6583 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006584 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006585
6586 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006587 goto erret;
6588 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006589 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006590
6591 if (did_set_arg_type)
6592 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006593 }
6594
6595 /*
6596 * Loop over all the lines of the function and generate instructions.
6597 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006598 for (;;)
6599 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006600 exarg_T ea;
6601 cmdmod_T save_cmdmod;
6602 int starts_with_colon = FALSE;
6603 char_u *cmd;
6604 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006605
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006606 // Bail out on the first error to avoid a flood of errors and report
6607 // the right line number when inside try/catch.
6608 if (emsg_before != called_emsg)
6609 goto erret;
6610
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006611 if (line != NULL && *line == '|')
6612 // the line continues after a '|'
6613 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006614 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006615 && !(*line == '#' && (line == cctx.ctx_line_start
6616 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006617 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006618 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006619 goto erret;
6620 }
6621 else
6622 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006623 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006624 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006625 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006626 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006627 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006628 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006629
Bram Moolenaara80faa82020-04-12 19:37:17 +02006630 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006631 ea.cmdlinep = &line;
6632 ea.cmd = skipwhite(line);
6633
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006634 // Some things can be recognized by the first character.
6635 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006636 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006637 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006638 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006639 if (ea.cmd[1] != '{')
6640 {
6641 line = (char_u *)"";
6642 continue;
6643 }
6644 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006645
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006646 case '}':
6647 {
6648 // "}" ends a block scope
6649 scopetype_T stype = cctx.ctx_scope == NULL
6650 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006651
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006652 if (stype == BLOCK_SCOPE)
6653 {
6654 compile_endblock(&cctx);
6655 line = ea.cmd;
6656 }
6657 else
6658 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006659 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006660 goto erret;
6661 }
6662 if (line != NULL)
6663 line = skipwhite(ea.cmd + 1);
6664 continue;
6665 }
6666
6667 case '{':
6668 // "{" starts a block scope
6669 // "{'a': 1}->func() is something else
6670 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6671 {
6672 line = compile_block(ea.cmd, &cctx);
6673 continue;
6674 }
6675 break;
6676
6677 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006678 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006679 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006680 }
6681
6682 /*
6683 * COMMAND MODIFIERS
6684 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006685 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006686 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6687 {
6688 if (errormsg != NULL)
6689 goto erret;
6690 // empty line or comment
6691 line = (char_u *)"";
6692 continue;
6693 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006694 // TODO: use modifiers in the command
6695 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006696 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006697
6698 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006699 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006700 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006701 {
6702 if (*ea.cmd == '(')
6703 // not for "call()"
6704 ea.cmd = p;
6705 else
6706 ea.cmd = skipwhite(ea.cmd);
6707 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006708
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006709 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006710 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006711 char_u *pskip;
6712
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006713 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006714 // find what follows.
6715 // Skip over "var.member", "var[idx]" and the like.
6716 // Also "&opt = val", "$ENV = val" and "@r = val".
6717 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006718 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006719 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006720 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006721 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006722 char_u *var_end;
6723 int oplen;
6724 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006725
Bram Moolenaar65821722020-08-02 18:58:54 +02006726 if (ea.cmd[0] == '@')
6727 var_end = ea.cmd + 2;
6728 else
6729 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006730 FNE_CHECK_START | FNE_INCL_BR);
6731 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006732 if (oplen > 0)
6733 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006734 size_t len = p - ea.cmd;
6735
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006736 // Recognize an assignment if we recognize the variable
6737 // name:
6738 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006739 // "local = expr" where "local" is a local var.
6740 // "script = expr" where "script" is a script-local var.
6741 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006742 // "&opt = expr"
6743 // "$ENV = expr"
6744 // "@r = expr"
6745 if (*ea.cmd == '&'
6746 || *ea.cmd == '$'
6747 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006748 || ((len) > 2 && ea.cmd[1] == ':')
6749 || lookup_local(ea.cmd, len, &cctx) != NULL
6750 || lookup_arg(ea.cmd, len, NULL, NULL,
6751 NULL, &cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02006752 || lookup_script(ea.cmd, len, FALSE) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006753 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006754 {
6755 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006756 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006757 goto erret;
6758 continue;
6759 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006760 }
6761 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006762
6763 if (*ea.cmd == '[')
6764 {
6765 // [var, var] = expr
6766 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6767 if (line == NULL)
6768 goto erret;
6769 if (line != ea.cmd)
6770 continue;
6771 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006772 }
6773
6774 /*
6775 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006776 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006777 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006778 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006779 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006780 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006781 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006782 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006783 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006784 if (!starts_with_colon)
6785 {
6786 emsg(_(e_colon_required_before_a_range));
6787 goto erret;
6788 }
6789 if (ends_excmd2(line, ea.cmd))
6790 {
6791 // A range without a command: jump to the line.
6792 // TODO: compile to a more efficient command, possibly
6793 // calling parse_cmd_address().
6794 ea.cmdidx = CMD_SIZE;
6795 line = compile_exec(line, &ea, &cctx);
6796 goto nextline;
6797 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006798 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006799 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006800 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006801 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006802 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006803
6804 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6805 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006806 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006807 {
6808 line += STRLEN(line);
6809 continue;
6810 }
6811
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006812 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006813 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006814 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006815 // CMD_let cannot happen, compile_assignment() above is used
6816 iemsg("Command from find_ex_command() not handled");
6817 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006818 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006819 }
6820
6821 p = skipwhite(p);
6822
Bram Moolenaar3988f642020-08-27 22:43:03 +02006823 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006824 && ea.cmdidx != CMD_elseif
6825 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02006826 && ea.cmdidx != CMD_endif
6827 && ea.cmdidx != CMD_endfor
6828 && ea.cmdidx != CMD_endwhile
6829 && ea.cmdidx != CMD_catch
6830 && ea.cmdidx != CMD_finally
6831 && ea.cmdidx != CMD_endtry)
6832 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02006833 emsg(_(e_unreachable_code_after_return));
6834 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006835 }
6836
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006837 switch (ea.cmdidx)
6838 {
6839 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006840 ea.arg = p;
6841 line = compile_nested_function(&ea, &cctx);
6842 break;
6843
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006844 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006845 // TODO: should we allow this, e.g. to declare a global
6846 // function?
6847 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006848 goto erret;
6849
6850 case CMD_return:
6851 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006852 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006853 break;
6854
6855 case CMD_let:
6856 case CMD_const:
6857 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006858 if (line == p)
6859 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006860 break;
6861
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006862 case CMD_unlet:
6863 case CMD_unlockvar:
6864 case CMD_lockvar:
6865 line = compile_unletlock(p, &ea, &cctx);
6866 break;
6867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006868 case CMD_import:
6869 line = compile_import(p, &cctx);
6870 break;
6871
6872 case CMD_if:
6873 line = compile_if(p, &cctx);
6874 break;
6875 case CMD_elseif:
6876 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006877 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006878 break;
6879 case CMD_else:
6880 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006881 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006882 break;
6883 case CMD_endif:
6884 line = compile_endif(p, &cctx);
6885 break;
6886
6887 case CMD_while:
6888 line = compile_while(p, &cctx);
6889 break;
6890 case CMD_endwhile:
6891 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006892 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006893 break;
6894
6895 case CMD_for:
6896 line = compile_for(p, &cctx);
6897 break;
6898 case CMD_endfor:
6899 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006900 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006901 break;
6902 case CMD_continue:
6903 line = compile_continue(p, &cctx);
6904 break;
6905 case CMD_break:
6906 line = compile_break(p, &cctx);
6907 break;
6908
6909 case CMD_try:
6910 line = compile_try(p, &cctx);
6911 break;
6912 case CMD_catch:
6913 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006914 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006915 break;
6916 case CMD_finally:
6917 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006918 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006919 break;
6920 case CMD_endtry:
6921 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006922 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006923 break;
6924 case CMD_throw:
6925 line = compile_throw(p, &cctx);
6926 break;
6927
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006928 case CMD_eval:
6929 if (compile_expr0(&p, &cctx) == FAIL)
6930 goto erret;
6931
Bram Moolenaar3988f642020-08-27 22:43:03 +02006932 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006933 generate_instr_drop(&cctx, ISN_DROP, 1);
6934
6935 line = skipwhite(p);
6936 break;
6937
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006938 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006939 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006940 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006941 case CMD_echomsg:
6942 case CMD_echoerr:
6943 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006944 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006945
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006946 case CMD_put:
6947 ea.cmd = cmd;
6948 line = compile_put(p, &ea, &cctx);
6949 break;
6950
Bram Moolenaar3988f642020-08-27 22:43:03 +02006951 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006952
Bram Moolenaarae616492020-07-28 20:07:27 +02006953 case CMD_append:
6954 case CMD_change:
6955 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02006956 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02006957 case CMD_xit:
6958 not_in_vim9(&ea);
6959 goto erret;
6960
Bram Moolenaar002262f2020-07-08 17:47:57 +02006961 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02006962 if (cctx.ctx_skip != SKIP_YES)
6963 {
6964 semsg(_(e_invalid_command_str), ea.cmd);
6965 goto erret;
6966 }
6967 // We don't check for a next command here.
6968 line = (char_u *)"";
6969 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02006970
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006971 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02006972 if (cctx.ctx_skip == SKIP_YES)
6973 {
6974 // We don't check for a next command here.
6975 line = (char_u *)"";
6976 }
6977 else
6978 {
6979 // Not recognized, execute with do_cmdline_cmd().
6980 ea.arg = p;
6981 line = compile_exec(line, &ea, &cctx);
6982 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006983 break;
6984 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006985nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006986 if (line == NULL)
6987 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006988 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006989
6990 if (cctx.ctx_type_stack.ga_len < 0)
6991 {
6992 iemsg("Type stack underflow");
6993 goto erret;
6994 }
6995 }
6996
6997 if (cctx.ctx_scope != NULL)
6998 {
6999 if (cctx.ctx_scope->se_type == IF_SCOPE)
7000 emsg(_(e_endif));
7001 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7002 emsg(_(e_endwhile));
7003 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7004 emsg(_(e_endfor));
7005 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007006 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007007 goto erret;
7008 }
7009
Bram Moolenaarefd88552020-06-18 20:50:10 +02007010 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007011 {
7012 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7013 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007014 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007015 goto erret;
7016 }
7017
7018 // Return zero if there is no return at the end.
7019 generate_PUSHNR(&cctx, 0);
7020 generate_instr(&cctx, ISN_RETURN);
7021 }
7022
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007023 {
7024 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7025 + ufunc->uf_dfunc_idx;
7026 dfunc->df_deleted = FALSE;
7027 dfunc->df_instr = instr->ga_data;
7028 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007029 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007030 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007031 if (cctx.ctx_outer_used)
7032 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007033 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007034 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007035
7036 ret = OK;
7037
7038erret:
7039 if (ret == FAIL)
7040 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007041 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007042 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7043 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007044
7045 for (idx = 0; idx < instr->ga_len; ++idx)
7046 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007047 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007048
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007049 // If using the last entry in the table and it was added above, we
7050 // might as well remove it.
7051 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007052 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007053 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007054 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007055 ufunc->uf_dfunc_idx = 0;
7056 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007057 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007058
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007059 while (cctx.ctx_scope != NULL)
7060 drop_scope(&cctx);
7061
Bram Moolenaar20431c92020-03-20 18:39:46 +01007062 // Don't execute this function body.
7063 ga_clear_strings(&ufunc->uf_lines);
7064
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007065 if (errormsg != NULL)
7066 emsg(errormsg);
7067 else if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007068 emsg(_(e_compile_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007069 }
7070
7071 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007072 if (do_estack_push)
7073 estack_pop();
7074
Bram Moolenaar20431c92020-03-20 18:39:46 +01007075 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007076 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007077 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007078 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007079}
7080
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007081 void
7082set_function_type(ufunc_T *ufunc)
7083{
7084 int varargs = ufunc->uf_va_name != NULL;
7085 int argcount = ufunc->uf_args.ga_len;
7086
7087 // Create a type for the function, with the return type and any
7088 // argument types.
7089 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7090 // The type is included in "tt_args".
7091 if (argcount > 0 || varargs)
7092 {
7093 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7094 argcount, &ufunc->uf_type_list);
7095 // Add argument types to the function type.
7096 if (func_type_add_arg_types(ufunc->uf_func_type,
7097 argcount + varargs,
7098 &ufunc->uf_type_list) == FAIL)
7099 return;
7100 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7101 ufunc->uf_func_type->tt_min_argcount =
7102 argcount - ufunc->uf_def_args.ga_len;
7103 if (ufunc->uf_arg_types == NULL)
7104 {
7105 int i;
7106
7107 // lambda does not have argument types.
7108 for (i = 0; i < argcount; ++i)
7109 ufunc->uf_func_type->tt_args[i] = &t_any;
7110 }
7111 else
7112 mch_memmove(ufunc->uf_func_type->tt_args,
7113 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7114 if (varargs)
7115 {
7116 ufunc->uf_func_type->tt_args[argcount] =
7117 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7118 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7119 }
7120 }
7121 else
7122 // No arguments, can use a predefined type.
7123 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7124 argcount, &ufunc->uf_type_list);
7125}
7126
7127
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007128/*
7129 * Delete an instruction, free what it contains.
7130 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007131 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007132delete_instr(isn_T *isn)
7133{
7134 switch (isn->isn_type)
7135 {
7136 case ISN_EXEC:
7137 case ISN_LOADENV:
7138 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007139 case ISN_LOADB:
7140 case ISN_LOADW:
7141 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007142 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007143 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007144 case ISN_PUSHEXC:
7145 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007146 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007147 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007148 case ISN_STOREB:
7149 case ISN_STOREW:
7150 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007151 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007152 vim_free(isn->isn_arg.string);
7153 break;
7154
7155 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007156 case ISN_STORES:
7157 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007158 break;
7159
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007160 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007161 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007162 vim_free(isn->isn_arg.unlet.ul_name);
7163 break;
7164
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007165 case ISN_STOREOPT:
7166 vim_free(isn->isn_arg.storeopt.so_name);
7167 break;
7168
7169 case ISN_PUSHBLOB: // push blob isn_arg.blob
7170 blob_unref(isn->isn_arg.blob);
7171 break;
7172
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007173 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007174#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007175 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007176#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007177 break;
7178
7179 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007180#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007181 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007182#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007183 break;
7184
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007185 case ISN_UCALL:
7186 vim_free(isn->isn_arg.ufunc.cuf_name);
7187 break;
7188
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007189 case ISN_FUNCREF:
7190 {
7191 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7192 + isn->isn_arg.funcref.fr_func;
7193 func_ptr_unref(dfunc->df_ufunc);
7194 }
7195 break;
7196
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007197 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007198 {
7199 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7200 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7201
7202 if (ufunc != NULL)
7203 {
7204 // Clear uf_dfunc_idx so that the function is deleted.
7205 clear_def_function(ufunc);
7206 ufunc->uf_dfunc_idx = 0;
7207 func_ptr_unref(ufunc);
7208 }
7209
7210 vim_free(lambda);
7211 vim_free(isn->isn_arg.newfunc.nf_global);
7212 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007213 break;
7214
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007215 case ISN_2BOOL:
7216 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007217 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007218 case ISN_ADDBLOB:
7219 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007220 case ISN_ANYINDEX:
7221 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007222 case ISN_BCALL:
7223 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007224 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007225 case ISN_CHECKNR:
7226 case ISN_CHECKTYPE:
7227 case ISN_COMPAREANY:
7228 case ISN_COMPAREBLOB:
7229 case ISN_COMPAREBOOL:
7230 case ISN_COMPAREDICT:
7231 case ISN_COMPAREFLOAT:
7232 case ISN_COMPAREFUNC:
7233 case ISN_COMPARELIST:
7234 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007235 case ISN_COMPARESPECIAL:
7236 case ISN_COMPARESTRING:
7237 case ISN_CONCAT:
7238 case ISN_DCALL:
7239 case ISN_DROP:
7240 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007241 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007242 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007243 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007244 case ISN_EXECCONCAT:
7245 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007246 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007247 case ISN_GETITEM:
7248 case ISN_JUMP:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007249 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007250 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007251 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007252 case ISN_LOADBDICT:
7253 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007254 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007255 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007256 case ISN_LOADSCRIPT:
7257 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007258 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007259 case ISN_LOADWDICT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007260 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007261 case ISN_NEGATENR:
7262 case ISN_NEWDICT:
7263 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007264 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007265 case ISN_OPFLOAT:
7266 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007267 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007268 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007269 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007270 case ISN_PUSHF:
7271 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007272 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007273 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007274 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007275 case ISN_SHUFFLE:
7276 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007277 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007278 case ISN_STOREDICT:
7279 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007280 case ISN_STORENR:
7281 case ISN_STOREOUTER:
7282 case ISN_STOREREG:
7283 case ISN_STORESCRIPT:
7284 case ISN_STOREV:
7285 case ISN_STRINDEX:
7286 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007287 case ISN_THROW:
7288 case ISN_TRY:
7289 // nothing allocated
7290 break;
7291 }
7292}
7293
7294/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007295 * Free all instructions for "dfunc".
7296 */
7297 static void
7298delete_def_function_contents(dfunc_T *dfunc)
7299{
7300 int idx;
7301
7302 ga_clear(&dfunc->df_def_args_isn);
7303
7304 if (dfunc->df_instr != NULL)
7305 {
7306 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7307 delete_instr(dfunc->df_instr + idx);
7308 VIM_CLEAR(dfunc->df_instr);
7309 }
7310
7311 dfunc->df_deleted = TRUE;
7312}
7313
7314/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007315 * When a user function is deleted, clear the contents of any associated def
7316 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007317 */
7318 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007319clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007320{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007321 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007322 {
7323 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7324 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007325
Bram Moolenaar20431c92020-03-20 18:39:46 +01007326 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007327 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007328 }
7329}
7330
7331#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007332/*
7333 * Free all functions defined with ":def".
7334 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007335 void
7336free_def_functions(void)
7337{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007338 int idx;
7339
7340 for (idx = 0; idx < def_functions.ga_len; ++idx)
7341 {
7342 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7343
7344 delete_def_function_contents(dfunc);
7345 }
7346
7347 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007348}
7349#endif
7350
7351
7352#endif // FEAT_EVAL