blob: 43d994e1a078a51ada5dba529a21c5a7cabdded7 [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 Moolenaar4ed124c2020-09-09 20:03:46 +0200732 if (expected == &t_bool && actual != &t_bool
733 && (actual->tt_flags & TTFLAG_BOOL_OK))
734 {
735 // Using "0", "1" or the result of an expression with "&&" or "||" as a
736 // boolean is OK but requires a conversion.
737 generate_2BOOL(cctx, FALSE);
738 return OK;
739 }
740
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200741 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200742 return OK;
743 if (actual->tt_type != VAR_ANY
744 && actual->tt_type != VAR_UNKNOWN
745 && !(actual->tt_type == VAR_FUNC
746 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
747 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200748 if (!silent)
749 type_mismatch(expected, actual);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200750 return FAIL;
751 }
752 generate_TYPECHECK(cctx, expected, offset);
753 return OK;
754}
755
756/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100757 * Generate an ISN_PUSHNR instruction.
758 */
759 static int
760generate_PUSHNR(cctx_T *cctx, varnumber_T number)
761{
762 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200763 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764
Bram Moolenaar080457c2020-03-03 21:53:32 +0100765 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100766 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
767 return FAIL;
768 isn->isn_arg.number = number;
769
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200770 if (number == 0 || number == 1)
771 {
772 type_T *type = alloc_type(cctx->ctx_type_list);
773
774 // A 0 or 1 number can also be used as a bool.
775 if (type != NULL)
776 {
777 type->tt_type = VAR_NUMBER;
778 type->tt_flags = TTFLAG_BOOL_OK;
779 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
780 }
781 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100782 return OK;
783}
784
785/*
786 * Generate an ISN_PUSHBOOL instruction.
787 */
788 static int
789generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
790{
791 isn_T *isn;
792
Bram Moolenaar080457c2020-03-03 21:53:32 +0100793 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100794 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
795 return FAIL;
796 isn->isn_arg.number = number;
797
798 return OK;
799}
800
801/*
802 * Generate an ISN_PUSHSPEC instruction.
803 */
804 static int
805generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
806{
807 isn_T *isn;
808
Bram Moolenaar080457c2020-03-03 21:53:32 +0100809 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
811 return FAIL;
812 isn->isn_arg.number = number;
813
814 return OK;
815}
816
817#ifdef FEAT_FLOAT
818/*
819 * Generate an ISN_PUSHF instruction.
820 */
821 static int
822generate_PUSHF(cctx_T *cctx, float_T fnumber)
823{
824 isn_T *isn;
825
Bram Moolenaar080457c2020-03-03 21:53:32 +0100826 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
828 return FAIL;
829 isn->isn_arg.fnumber = fnumber;
830
831 return OK;
832}
833#endif
834
835/*
836 * Generate an ISN_PUSHS instruction.
837 * Consumes "str".
838 */
839 static int
840generate_PUSHS(cctx_T *cctx, char_u *str)
841{
842 isn_T *isn;
843
Bram Moolenaar080457c2020-03-03 21:53:32 +0100844 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100845 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
846 return FAIL;
847 isn->isn_arg.string = str;
848
849 return OK;
850}
851
852/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100853 * Generate an ISN_PUSHCHANNEL instruction.
854 * Consumes "channel".
855 */
856 static int
857generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
858{
859 isn_T *isn;
860
Bram Moolenaar080457c2020-03-03 21:53:32 +0100861 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100862 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
863 return FAIL;
864 isn->isn_arg.channel = channel;
865
866 return OK;
867}
868
869/*
870 * Generate an ISN_PUSHJOB instruction.
871 * Consumes "job".
872 */
873 static int
874generate_PUSHJOB(cctx_T *cctx, job_T *job)
875{
876 isn_T *isn;
877
Bram Moolenaar080457c2020-03-03 21:53:32 +0100878 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100879 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100880 return FAIL;
881 isn->isn_arg.job = job;
882
883 return OK;
884}
885
886/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887 * Generate an ISN_PUSHBLOB instruction.
888 * Consumes "blob".
889 */
890 static int
891generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
892{
893 isn_T *isn;
894
Bram Moolenaar080457c2020-03-03 21:53:32 +0100895 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
897 return FAIL;
898 isn->isn_arg.blob = blob;
899
900 return OK;
901}
902
903/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100904 * Generate an ISN_PUSHFUNC instruction with name "name".
905 * Consumes "name".
906 */
907 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200908generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100909{
910 isn_T *isn;
911
Bram Moolenaar080457c2020-03-03 21:53:32 +0100912 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200913 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100914 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200915 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100916
917 return OK;
918}
919
920/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200921 * Generate an ISN_GETITEM instruction with "index".
922 */
923 static int
924generate_GETITEM(cctx_T *cctx, int index)
925{
926 isn_T *isn;
927 garray_T *stack = &cctx->ctx_type_stack;
928 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
929 type_T *item_type = &t_any;
930
931 RETURN_OK_IF_SKIP(cctx);
932
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200933 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200934 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200935 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200936 emsg(_(e_listreq));
937 return FAIL;
938 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200939 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200940 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
941 return FAIL;
942 isn->isn_arg.number = index;
943
944 // add the item type to the type stack
945 if (ga_grow(stack, 1) == FAIL)
946 return FAIL;
947 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
948 ++stack->ga_len;
949 return OK;
950}
951
952/*
Bram Moolenaar9af78762020-06-16 11:34:42 +0200953 * Generate an ISN_SLICE instruction with "count".
954 */
955 static int
956generate_SLICE(cctx_T *cctx, int count)
957{
958 isn_T *isn;
959
960 RETURN_OK_IF_SKIP(cctx);
961 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
962 return FAIL;
963 isn->isn_arg.number = count;
964 return OK;
965}
966
967/*
968 * Generate an ISN_CHECKLEN instruction with "min_len".
969 */
970 static int
971generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
972{
973 isn_T *isn;
974
975 RETURN_OK_IF_SKIP(cctx);
976
977 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
978 return FAIL;
979 isn->isn_arg.checklen.cl_min_len = min_len;
980 isn->isn_arg.checklen.cl_more_OK = more_OK;
981
982 return OK;
983}
984
985/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100986 * Generate an ISN_STORE instruction.
987 */
988 static int
989generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
990{
991 isn_T *isn;
992
Bram Moolenaar080457c2020-03-03 21:53:32 +0100993 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100994 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
995 return FAIL;
996 if (name != NULL)
997 isn->isn_arg.string = vim_strsave(name);
998 else
999 isn->isn_arg.number = idx;
1000
1001 return OK;
1002}
1003
1004/*
1005 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1006 */
1007 static int
1008generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1009{
1010 isn_T *isn;
1011
Bram Moolenaar080457c2020-03-03 21:53:32 +01001012 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001013 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1014 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001015 isn->isn_arg.storenr.stnr_idx = idx;
1016 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001017
1018 return OK;
1019}
1020
1021/*
1022 * Generate an ISN_STOREOPT instruction
1023 */
1024 static int
1025generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1026{
1027 isn_T *isn;
1028
Bram Moolenaar080457c2020-03-03 21:53:32 +01001029 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001030 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001031 return FAIL;
1032 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1033 isn->isn_arg.storeopt.so_flags = opt_flags;
1034
1035 return OK;
1036}
1037
1038/*
1039 * Generate an ISN_LOAD or similar instruction.
1040 */
1041 static int
1042generate_LOAD(
1043 cctx_T *cctx,
1044 isntype_T isn_type,
1045 int idx,
1046 char_u *name,
1047 type_T *type)
1048{
1049 isn_T *isn;
1050
Bram Moolenaar080457c2020-03-03 21:53:32 +01001051 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001052 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1053 return FAIL;
1054 if (name != NULL)
1055 isn->isn_arg.string = vim_strsave(name);
1056 else
1057 isn->isn_arg.number = idx;
1058
1059 return OK;
1060}
1061
1062/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001063 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001064 */
1065 static int
1066generate_LOADV(
1067 cctx_T *cctx,
1068 char_u *name,
1069 int error)
1070{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001071 int di_flags;
1072 int vidx = find_vim_var(name, &di_flags);
1073 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001074
Bram Moolenaar080457c2020-03-03 21:53:32 +01001075 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001076 if (vidx < 0)
1077 {
1078 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001079 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001080 return FAIL;
1081 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001082 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001083
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001084 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001085}
1086
1087/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001088 * Generate an ISN_UNLET instruction.
1089 */
1090 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001091generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001092{
1093 isn_T *isn;
1094
1095 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001096 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001097 return FAIL;
1098 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1099 isn->isn_arg.unlet.ul_forceit = forceit;
1100
1101 return OK;
1102}
1103
1104/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001105 * Generate an ISN_LOADS instruction.
1106 */
1107 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001108generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001109 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001110 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001111 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001112 int sid,
1113 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001114{
1115 isn_T *isn;
1116
Bram Moolenaar080457c2020-03-03 21:53:32 +01001117 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001118 if (isn_type == ISN_LOADS)
1119 isn = generate_instr_type(cctx, isn_type, type);
1120 else
1121 isn = generate_instr_drop(cctx, isn_type, 1);
1122 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001123 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001124 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1125 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001126
1127 return OK;
1128}
1129
1130/*
1131 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1132 */
1133 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001134generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135 cctx_T *cctx,
1136 isntype_T isn_type,
1137 int sid,
1138 int idx,
1139 type_T *type)
1140{
1141 isn_T *isn;
1142
Bram Moolenaar080457c2020-03-03 21:53:32 +01001143 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001144 if (isn_type == ISN_LOADSCRIPT)
1145 isn = generate_instr_type(cctx, isn_type, type);
1146 else
1147 isn = generate_instr_drop(cctx, isn_type, 1);
1148 if (isn == NULL)
1149 return FAIL;
1150 isn->isn_arg.script.script_sid = sid;
1151 isn->isn_arg.script.script_idx = idx;
1152 return OK;
1153}
1154
1155/*
1156 * Generate an ISN_NEWLIST instruction.
1157 */
1158 static int
1159generate_NEWLIST(cctx_T *cctx, int count)
1160{
1161 isn_T *isn;
1162 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001163 type_T *type;
1164 type_T *member;
1165
Bram Moolenaar080457c2020-03-03 21:53:32 +01001166 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001167 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1168 return FAIL;
1169 isn->isn_arg.number = count;
1170
Bram Moolenaar127542b2020-08-09 17:22:04 +02001171 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001172 if (count == 0)
1173 member = &t_void;
1174 else
1175 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001176 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1177 cctx->ctx_type_list);
1178 type = get_list_type(member, cctx->ctx_type_list);
1179
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001180 // drop the value types
1181 stack->ga_len -= count;
1182
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001183 // add the list type to the type stack
1184 if (ga_grow(stack, 1) == FAIL)
1185 return FAIL;
1186 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1187 ++stack->ga_len;
1188
1189 return OK;
1190}
1191
1192/*
1193 * Generate an ISN_NEWDICT instruction.
1194 */
1195 static int
1196generate_NEWDICT(cctx_T *cctx, int count)
1197{
1198 isn_T *isn;
1199 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001200 type_T *type;
1201 type_T *member;
1202
Bram Moolenaar080457c2020-03-03 21:53:32 +01001203 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1205 return FAIL;
1206 isn->isn_arg.number = count;
1207
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001208 if (count == 0)
1209 member = &t_void;
1210 else
1211 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001212 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1213 cctx->ctx_type_list);
1214 type = get_dict_type(member, cctx->ctx_type_list);
1215
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001216 // drop the key and value types
1217 stack->ga_len -= 2 * count;
1218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219 // add the dict type to the type stack
1220 if (ga_grow(stack, 1) == FAIL)
1221 return FAIL;
1222 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1223 ++stack->ga_len;
1224
1225 return OK;
1226}
1227
1228/*
1229 * Generate an ISN_FUNCREF instruction.
1230 */
1231 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001232generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001233{
1234 isn_T *isn;
1235 garray_T *stack = &cctx->ctx_type_stack;
1236
Bram Moolenaar080457c2020-03-03 21:53:32 +01001237 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001238 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1239 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001240 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001241 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001242
1243 if (ga_grow(stack, 1) == FAIL)
1244 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001245 ((type_T **)stack->ga_data)[stack->ga_len] =
1246 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001247 ++stack->ga_len;
1248
1249 return OK;
1250}
1251
1252/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001253 * Generate an ISN_NEWFUNC instruction.
1254 */
1255 static int
1256generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1257{
1258 isn_T *isn;
1259 char_u *name;
1260
1261 RETURN_OK_IF_SKIP(cctx);
1262 name = vim_strsave(lambda_name);
1263 if (name == NULL)
1264 return FAIL;
1265 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1266 return FAIL;
1267 isn->isn_arg.newfunc.nf_lambda = name;
1268 isn->isn_arg.newfunc.nf_global = func_name;
1269
1270 return OK;
1271}
1272
1273/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001274 * Generate an ISN_JUMP instruction.
1275 */
1276 static int
1277generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1278{
1279 isn_T *isn;
1280 garray_T *stack = &cctx->ctx_type_stack;
1281
Bram Moolenaar080457c2020-03-03 21:53:32 +01001282 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001283 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1284 return FAIL;
1285 isn->isn_arg.jump.jump_when = when;
1286 isn->isn_arg.jump.jump_where = where;
1287
1288 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1289 --stack->ga_len;
1290
1291 return OK;
1292}
1293
1294 static int
1295generate_FOR(cctx_T *cctx, int loop_idx)
1296{
1297 isn_T *isn;
1298 garray_T *stack = &cctx->ctx_type_stack;
1299
Bram Moolenaar080457c2020-03-03 21:53:32 +01001300 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001301 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1302 return FAIL;
1303 isn->isn_arg.forloop.for_idx = loop_idx;
1304
1305 if (ga_grow(stack, 1) == FAIL)
1306 return FAIL;
1307 // type doesn't matter, will be stored next
1308 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1309 ++stack->ga_len;
1310
1311 return OK;
1312}
1313
1314/*
1315 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001316 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001317 * Return FAIL if the number of arguments is wrong.
1318 */
1319 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001320generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321{
1322 isn_T *isn;
1323 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001324 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001325 type_T *argtypes[MAX_FUNC_ARGS];
1326 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327
Bram Moolenaar080457c2020-03-03 21:53:32 +01001328 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001329 argoff = check_internal_func(func_idx, argcount);
1330 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331 return FAIL;
1332
Bram Moolenaar389df252020-07-09 21:20:47 +02001333 if (method_call && argoff > 1)
1334 {
1335 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1336 return FAIL;
1337 isn->isn_arg.shuffle.shfl_item = argcount;
1338 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1339 }
1340
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001341 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1342 return FAIL;
1343 isn->isn_arg.bfunc.cbf_idx = func_idx;
1344 isn->isn_arg.bfunc.cbf_argcount = argcount;
1345
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001346 for (i = 0; i < argcount; ++i)
1347 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1348
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349 stack->ga_len -= argcount; // drop the arguments
1350 if (ga_grow(stack, 1) == FAIL)
1351 return FAIL;
1352 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001353 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354 ++stack->ga_len; // add return value
1355
1356 return OK;
1357}
1358
1359/*
1360 * Generate an ISN_DCALL or ISN_UCALL instruction.
1361 * Return FAIL if the number of arguments is wrong.
1362 */
1363 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001364generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001365{
1366 isn_T *isn;
1367 garray_T *stack = &cctx->ctx_type_stack;
1368 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001369 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001370
Bram Moolenaar080457c2020-03-03 21:53:32 +01001371 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001372 if (argcount > regular_args && !has_varargs(ufunc))
1373 {
1374 semsg(_(e_toomanyarg), ufunc->uf_name);
1375 return FAIL;
1376 }
1377 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1378 {
1379 semsg(_(e_toofewarg), ufunc->uf_name);
1380 return FAIL;
1381 }
1382
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001383 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001384 {
1385 int i;
1386
1387 for (i = 0; i < argcount; ++i)
1388 {
1389 type_T *expected;
1390 type_T *actual;
1391
1392 if (i < regular_args)
1393 {
1394 if (ufunc->uf_arg_types == NULL)
1395 continue;
1396 expected = ufunc->uf_arg_types[i];
1397 }
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001398 else if (ufunc->uf_va_type == NULL)
1399 // possibly a lambda
1400 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001401 else
1402 expected = ufunc->uf_va_type->tt_member;
1403 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001404 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001405 {
1406 arg_type_mismatch(expected, actual, i + 1);
1407 return FAIL;
1408 }
1409 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001410 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001411 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1412 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001413 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001414 }
1415
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001417 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001418 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001419 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001420 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421 {
1422 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1423 isn->isn_arg.dfunc.cdf_argcount = argcount;
1424 }
1425 else
1426 {
1427 // A user function may be deleted and redefined later, can't use the
1428 // ufunc pointer, need to look it up again at runtime.
1429 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1430 isn->isn_arg.ufunc.cuf_argcount = argcount;
1431 }
1432
1433 stack->ga_len -= argcount; // drop the arguments
1434 if (ga_grow(stack, 1) == FAIL)
1435 return FAIL;
1436 // add return value
1437 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1438 ++stack->ga_len;
1439
1440 return OK;
1441}
1442
1443/*
1444 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1445 */
1446 static int
1447generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1448{
1449 isn_T *isn;
1450 garray_T *stack = &cctx->ctx_type_stack;
1451
Bram Moolenaar080457c2020-03-03 21:53:32 +01001452 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001453 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1454 return FAIL;
1455 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1456 isn->isn_arg.ufunc.cuf_argcount = argcount;
1457
1458 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001459 if (ga_grow(stack, 1) == FAIL)
1460 return FAIL;
1461 // add return value
1462 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1463 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001464
1465 return OK;
1466}
1467
1468/*
1469 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001470 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471 */
1472 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001473generate_PCALL(
1474 cctx_T *cctx,
1475 int argcount,
1476 char_u *name,
1477 type_T *type,
1478 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479{
1480 isn_T *isn;
1481 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001482 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483
Bram Moolenaar080457c2020-03-03 21:53:32 +01001484 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001485
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001486 if (type->tt_type == VAR_ANY)
1487 ret_type = &t_any;
1488 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001489 {
1490 if (type->tt_argcount != -1)
1491 {
1492 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1493
1494 if (argcount < type->tt_min_argcount - varargs)
1495 {
1496 semsg(_(e_toofewarg), "[reference]");
1497 return FAIL;
1498 }
1499 if (!varargs && argcount > type->tt_argcount)
1500 {
1501 semsg(_(e_toomanyarg), "[reference]");
1502 return FAIL;
1503 }
1504 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001505 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001506 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001507 else
1508 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001509 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001510 return FAIL;
1511 }
1512
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001513 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1514 return FAIL;
1515 isn->isn_arg.pfunc.cpf_top = at_top;
1516 isn->isn_arg.pfunc.cpf_argcount = argcount;
1517
1518 stack->ga_len -= argcount; // drop the arguments
1519
1520 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001521 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001523 // If partial is above the arguments it must be cleared and replaced with
1524 // the return value.
1525 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1526 return FAIL;
1527
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528 return OK;
1529}
1530
1531/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001532 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001533 */
1534 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001535generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536{
1537 isn_T *isn;
1538 garray_T *stack = &cctx->ctx_type_stack;
1539 type_T *type;
1540
Bram Moolenaar080457c2020-03-03 21:53:32 +01001541 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001542 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001543 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001544 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001545
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001546 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001547 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001548 if (type->tt_type != VAR_DICT && type != &t_any)
1549 {
1550 emsg(_(e_dictreq));
1551 return FAIL;
1552 }
1553 // change dict type to dict member type
1554 if (type->tt_type == VAR_DICT)
1555 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556
1557 return OK;
1558}
1559
1560/*
1561 * Generate an ISN_ECHO instruction.
1562 */
1563 static int
1564generate_ECHO(cctx_T *cctx, int with_white, int count)
1565{
1566 isn_T *isn;
1567
Bram Moolenaar080457c2020-03-03 21:53:32 +01001568 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1570 return FAIL;
1571 isn->isn_arg.echo.echo_with_white = with_white;
1572 isn->isn_arg.echo.echo_count = count;
1573
1574 return OK;
1575}
1576
Bram Moolenaarad39c092020-02-26 18:23:43 +01001577/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001578 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001579 */
1580 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001581generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001582{
1583 isn_T *isn;
1584
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001585 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001586 return FAIL;
1587 isn->isn_arg.number = count;
1588
1589 return OK;
1590}
1591
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001592/*
1593 * Generate an ISN_PUT instruction.
1594 */
1595 static int
1596generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1597{
1598 isn_T *isn;
1599
1600 RETURN_OK_IF_SKIP(cctx);
1601 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1602 return FAIL;
1603 isn->isn_arg.put.put_regname = regname;
1604 isn->isn_arg.put.put_lnum = lnum;
1605 return OK;
1606}
1607
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 static int
1609generate_EXEC(cctx_T *cctx, char_u *line)
1610{
1611 isn_T *isn;
1612
Bram Moolenaar080457c2020-03-03 21:53:32 +01001613 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001614 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1615 return FAIL;
1616 isn->isn_arg.string = vim_strsave(line);
1617 return OK;
1618}
1619
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001620 static int
1621generate_EXECCONCAT(cctx_T *cctx, int count)
1622{
1623 isn_T *isn;
1624
1625 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1626 return FAIL;
1627 isn->isn_arg.number = count;
1628 return OK;
1629}
1630
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001631/*
1632 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001633 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001634 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001635 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001636reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1637{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001638 lvar_T *lvar;
1639
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001640 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001642 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001643 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001644 }
1645
1646 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001647 return NULL;
1648 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001649
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001650 // Every local variable uses the next entry on the stack. We could re-use
1651 // the last ones when leaving a scope, but then variables used in a closure
1652 // might get overwritten. To keep things simple do not re-use stack
1653 // entries. This is less efficient, but memory is cheap these days.
1654 lvar->lv_idx = cctx->ctx_locals_count++;
1655
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001656 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001657 lvar->lv_const = isConst;
1658 lvar->lv_type = type;
1659
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001660 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001661}
1662
1663/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001664 * Remove local variables above "new_top".
1665 */
1666 static void
1667unwind_locals(cctx_T *cctx, int new_top)
1668{
1669 if (cctx->ctx_locals.ga_len > new_top)
1670 {
1671 int idx;
1672 lvar_T *lvar;
1673
1674 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1675 {
1676 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1677 vim_free(lvar->lv_name);
1678 }
1679 }
1680 cctx->ctx_locals.ga_len = new_top;
1681}
1682
1683/*
1684 * Free all local variables.
1685 */
1686 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001687free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001688{
1689 unwind_locals(cctx, 0);
1690 ga_clear(&cctx->ctx_locals);
1691}
1692
1693/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001694 * Find "name" in script-local items of script "sid".
1695 * Returns the index in "sn_var_vals" if found.
1696 * If found but not in "sn_var_vals" returns -1.
1697 * If not found returns -2.
1698 */
1699 int
1700get_script_item_idx(int sid, char_u *name, int check_writable)
1701{
1702 hashtab_T *ht;
1703 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001704 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001705 int idx;
1706
1707 // First look the name up in the hashtable.
Bram Moolenaare3d46852020-08-29 13:39:17 +02001708 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001709 return -1;
1710 ht = &SCRIPT_VARS(sid);
1711 di = find_var_in_ht(ht, 0, name, TRUE);
1712 if (di == NULL)
1713 return -2;
1714
1715 // Now find the svar_T index in sn_var_vals.
1716 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1717 {
1718 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1719
1720 if (sv->sv_tv == &di->di_tv)
1721 {
1722 if (check_writable && sv->sv_const)
1723 semsg(_(e_readonlyvar), name);
1724 return idx;
1725 }
1726 }
1727 return -1;
1728}
1729
1730/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001731 * Find "name" in imported items of the current script or in "cctx" if not
1732 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001733 */
1734 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001735find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001736{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737 int idx;
1738
Bram Moolenaare3d46852020-08-29 13:39:17 +02001739 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001740 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741 if (cctx != NULL)
1742 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1743 {
1744 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1745 + idx;
1746
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001747 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1748 : STRLEN(import->imp_name) == len
1749 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750 return import;
1751 }
1752
Bram Moolenaarefa94442020-08-08 22:16:00 +02001753 return find_imported_in_script(name, len, current_sctx.sc_sid);
1754}
1755
1756 imported_T *
1757find_imported_in_script(char_u *name, size_t len, int sid)
1758{
Bram Moolenaare3d46852020-08-29 13:39:17 +02001759 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001760 int idx;
1761
Bram Moolenaare3d46852020-08-29 13:39:17 +02001762 if (!SCRIPT_ID_VALID(sid))
1763 return NULL;
1764 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001765 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1766 {
1767 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1768
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001769 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1770 : STRLEN(import->imp_name) == len
1771 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001772 return import;
1773 }
1774 return NULL;
1775}
1776
1777/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001778 * Free all imported variables.
1779 */
1780 static void
1781free_imported(cctx_T *cctx)
1782{
1783 int idx;
1784
1785 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1786 {
1787 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1788
1789 vim_free(import->imp_name);
1790 }
1791 ga_clear(&cctx->ctx_imports);
1792}
1793
1794/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02001795 * Return TRUE if "p" points at a "#" but not at "#{".
1796 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001797 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001798vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001799{
1800 return p[0] == '#' && p[1] != '{';
1801}
1802
1803/*
1804 * Return a pointer to the next line that isn't empty or only contains a
1805 * comment. Skips over white space.
1806 * Returns NULL if there is none.
1807 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001808 char_u *
1809peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001810{
1811 int lnum = cctx->ctx_lnum;
1812
1813 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
1814 {
1815 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001816 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001817
Bram Moolenaarba60cc42020-08-12 19:15:33 +02001818 // ignore NULLs inserted for continuation lines
1819 if (line != NULL)
1820 {
1821 p = skipwhite(line);
1822 if (*p != NUL && !vim9_comment_start(p))
1823 return p;
1824 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02001825 }
1826 return NULL;
1827}
1828
1829/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001830 * Called when checking for a following operator at "arg". When the rest of
1831 * the line is empty or only a comment, peek the next line. If there is a next
1832 * line return a pointer to it and set "nextp".
1833 * Otherwise skip over white space.
1834 */
1835 static char_u *
1836may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
1837{
1838 char_u *p = skipwhite(arg);
1839
1840 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001841 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001842 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001843 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001844 if (*nextp != NULL)
1845 return *nextp;
1846 }
1847 return p;
1848}
1849
1850/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02001851 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02001852 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02001853 * Returns NULL when at the end.
1854 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001855 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02001856next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02001857{
Bram Moolenaar7a092242020-04-16 22:10:49 +02001858 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001859
1860 do
1861 {
1862 ++cctx->ctx_lnum;
1863 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02001864 {
1865 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001866 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02001867 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02001868 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02001869 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001870 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001871 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02001872 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02001873 return line;
1874}
1875
1876/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001877 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02001878 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001879 * Return FAIL if beyond the last line, "*arg" is unmodified then.
1880 */
1881 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02001882may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001883{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001884 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001885 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02001886 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001887
1888 if (next == NULL)
1889 return FAIL;
1890 *arg = skipwhite(next);
1891 }
1892 return OK;
1893}
1894
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001895/*
1896 * Idem, and give an error when failed.
1897 */
1898 static int
1899may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
1900{
1901 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1902 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001903 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001904 return FAIL;
1905 }
1906 return OK;
1907}
1908
1909
Bram Moolenaara5565e42020-05-09 15:44:01 +02001910// Structure passed between the compile_expr* functions to keep track of
1911// constants that have been parsed but for which no code was produced yet. If
1912// possible expressions on these constants are applied at compile time. If
1913// that is not possible, the code to push the constants needs to be generated
1914// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02001915// Using 50 should be more than enough of 5 levels of ().
1916#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02001917typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02001918 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02001919 int pp_used; // active entries in pp_tv[]
1920} ppconst_T;
1921
Bram Moolenaar1c747212020-05-09 18:28:34 +02001922static int compile_expr0(char_u **arg, cctx_T *cctx);
1923static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1924
Bram Moolenaara5565e42020-05-09 15:44:01 +02001925/*
1926 * Generate a PUSH instruction for "tv".
1927 * "tv" will be consumed or cleared.
1928 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
1929 */
1930 static int
1931generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
1932{
1933 if (tv != NULL)
1934 {
1935 switch (tv->v_type)
1936 {
1937 case VAR_UNKNOWN:
1938 break;
1939 case VAR_BOOL:
1940 generate_PUSHBOOL(cctx, tv->vval.v_number);
1941 break;
1942 case VAR_SPECIAL:
1943 generate_PUSHSPEC(cctx, tv->vval.v_number);
1944 break;
1945 case VAR_NUMBER:
1946 generate_PUSHNR(cctx, tv->vval.v_number);
1947 break;
1948#ifdef FEAT_FLOAT
1949 case VAR_FLOAT:
1950 generate_PUSHF(cctx, tv->vval.v_float);
1951 break;
1952#endif
1953 case VAR_BLOB:
1954 generate_PUSHBLOB(cctx, tv->vval.v_blob);
1955 tv->vval.v_blob = NULL;
1956 break;
1957 case VAR_STRING:
1958 generate_PUSHS(cctx, tv->vval.v_string);
1959 tv->vval.v_string = NULL;
1960 break;
1961 default:
1962 iemsg("constant type not supported");
1963 clear_tv(tv);
1964 return FAIL;
1965 }
1966 tv->v_type = VAR_UNKNOWN;
1967 }
1968 return OK;
1969}
1970
1971/*
1972 * Generate code for any ppconst entries.
1973 */
1974 static int
1975generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
1976{
1977 int i;
1978 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001979 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001980
Bram Moolenaar9b68c822020-06-18 19:31:08 +02001981 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001982 for (i = 0; i < ppconst->pp_used; ++i)
1983 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
1984 ret = FAIL;
1985 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001986 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001987 return ret;
1988}
1989
1990/*
1991 * Clear ppconst constants. Used when failing.
1992 */
1993 static void
1994clear_ppconst(ppconst_T *ppconst)
1995{
1996 int i;
1997
1998 for (i = 0; i < ppconst->pp_used; ++i)
1999 clear_tv(&ppconst->pp_tv[i]);
2000 ppconst->pp_used = 0;
2001}
2002
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002003/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002004 * Generate an instruction to load script-local variable "name", without the
2005 * leading "s:".
2006 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007 */
2008 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002009compile_load_scriptvar(
2010 cctx_T *cctx,
2011 char_u *name, // variable NUL terminated
2012 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002013 char_u **end, // end of variable
2014 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002015{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002016 scriptitem_T *si;
2017 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002018 imported_T *import;
2019
Bram Moolenaare3d46852020-08-29 13:39:17 +02002020 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2021 return FAIL;
2022 si = SCRIPT_ITEM(current_sctx.sc_sid);
2023 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002024 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002026 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002027 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2028 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029 }
2030 if (idx >= 0)
2031 {
2032 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2033
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002034 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 current_sctx.sc_sid, idx, sv->sv_type);
2036 return OK;
2037 }
2038
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002039 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040 if (import != NULL)
2041 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002042 if (import->imp_all)
2043 {
2044 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002045 char_u *exp_name;
2046 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002047 ufunc_T *ufunc;
2048 type_T *type;
2049
2050 // Used "import * as Name", need to lookup the member.
2051 if (*p != '.')
2052 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002053 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002054 return FAIL;
2055 }
2056 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002057 if (VIM_ISWHITE(*p))
2058 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002059 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002060 return FAIL;
2061 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002062
Bram Moolenaar1c991142020-07-04 13:15:31 +02002063 // isolate one name
2064 exp_name = p;
2065 while (eval_isnamec(*p))
2066 ++p;
2067 cc = *p;
2068 *p = NUL;
2069
2070 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2071 *p = cc;
2072 p = skipwhite(p);
2073
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002074 // TODO: what if it is a function?
2075 if (idx < 0)
2076 return FAIL;
2077 *end = p;
2078
2079 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2080 import->imp_sid,
2081 idx,
2082 type);
2083 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002084 else if (import->imp_funcname != NULL)
2085 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002086 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002087 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2088 import->imp_sid,
2089 import->imp_var_vals_idx,
2090 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002091 return OK;
2092 }
2093
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002094 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002095 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002096 return FAIL;
2097}
2098
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002099 static int
2100generate_funcref(cctx_T *cctx, char_u *name)
2101{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002102 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002103
2104 if (ufunc == NULL)
2105 return FAIL;
2106
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002107 // Need to compile any default values to get the argument types.
2108 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2109 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2110 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002111 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002112}
2113
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002114/*
2115 * Compile a variable name into a load instruction.
2116 * "end" points to just after the name.
2117 * When "error" is FALSE do not give an error when not found.
2118 */
2119 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002120compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121{
2122 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002123 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002124 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002125 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002126 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002127
2128 if (*(*arg + 1) == ':')
2129 {
2130 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002131 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002132 {
2133 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002134
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002135 switch (**arg)
2136 {
2137 case 'g': isn_type = ISN_LOADGDICT; break;
2138 case 'w': isn_type = ISN_LOADWDICT; break;
2139 case 't': isn_type = ISN_LOADTDICT; break;
2140 case 'b': isn_type = ISN_LOADBDICT; break;
2141 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002142 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002143 goto theend;
2144 }
2145 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2146 goto theend;
2147 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002148 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002149 else
2150 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002151 isntype_T isn_type = ISN_DROP;
2152
2153 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2154 if (name == NULL)
2155 return FAIL;
2156
2157 switch (**arg)
2158 {
2159 case 'v': res = generate_LOADV(cctx, name, error);
2160 break;
2161 case 's': res = compile_load_scriptvar(cctx, name,
2162 NULL, NULL, error);
2163 break;
2164 case 'g': isn_type = ISN_LOADG; break;
2165 case 'w': isn_type = ISN_LOADW; break;
2166 case 't': isn_type = ISN_LOADT; break;
2167 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002168 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002169 goto theend;
2170 }
2171 if (isn_type != ISN_DROP)
2172 {
2173 // Global, Buffer-local, Window-local and Tabpage-local
2174 // variables can be defined later, thus we don't check if it
2175 // exists, give error at runtime.
2176 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2177 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002178 }
2179 }
2180 else
2181 {
2182 size_t len = end - *arg;
2183 int idx;
2184 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002185 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002186
2187 name = vim_strnsave(*arg, end - *arg);
2188 if (name == NULL)
2189 return FAIL;
2190
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002191 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002192 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002193 if (!gen_load_outer)
2194 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002195 }
2196 else
2197 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002198 lvar_T *lvar = lookup_local(*arg, len, cctx);
2199
2200 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002201 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002202 type = lvar->lv_type;
2203 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002204 if (lvar->lv_from_outer)
2205 gen_load_outer = TRUE;
2206 else
2207 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002208 }
2209 else
2210 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002211 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002212 // already exists in a Vim9 script or when it's imported.
2213 if (lookup_script(*arg, len, TRUE) == OK
2214 || find_imported(name, 0, cctx) != NULL)
2215 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002216
Bram Moolenaara5565e42020-05-09 15:44:01 +02002217 // When the name starts with an uppercase letter or "x:" it
2218 // can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002219 // TODO: this is just guessing
Bram Moolenaara5565e42020-05-09 15:44:01 +02002220 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2221 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002222 }
2223 }
2224 if (gen_load)
2225 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002226 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002227 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002228 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002229 cctx->ctx_outer_used = TRUE;
2230 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002231 }
2232
2233 *arg = end;
2234
2235theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002236 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002237 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002238 vim_free(name);
2239 return res;
2240}
2241
2242/*
2243 * Compile the argument expressions.
2244 * "arg" points to just after the "(" and is advanced to after the ")"
2245 */
2246 static int
2247compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2248{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002249 char_u *p = *arg;
2250 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002251
Bram Moolenaare6085c52020-04-12 20:19:16 +02002252 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002254 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2255 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002256 if (*p == ')')
2257 {
2258 *arg = p + 1;
2259 return OK;
2260 }
2261
Bram Moolenaara5565e42020-05-09 15:44:01 +02002262 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002263 return FAIL;
2264 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002265
2266 if (*p != ',' && *skipwhite(p) == ',')
2267 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002268 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002269 p = skipwhite(p);
2270 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002272 {
2273 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002274 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002275 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002276 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002277 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002278 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002279 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002280failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002281 emsg(_(e_missing_close));
2282 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002283}
2284
2285/*
2286 * Compile a function call: name(arg1, arg2)
2287 * "arg" points to "name", "arg + varlen" to the "(".
2288 * "argcount_init" is 1 for "value->method()"
2289 * Instructions:
2290 * EVAL arg1
2291 * EVAL arg2
2292 * BCALL / DCALL / UCALL
2293 */
2294 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002295compile_call(
2296 char_u **arg,
2297 size_t varlen,
2298 cctx_T *cctx,
2299 ppconst_T *ppconst,
2300 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002301{
2302 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002303 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002304 int argcount = argcount_init;
2305 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002306 char_u fname_buf[FLEN_FIXED + 1];
2307 char_u *tofree = NULL;
2308 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002309 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002310 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002311 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002312
Bram Moolenaara5565e42020-05-09 15:44:01 +02002313 // we can evaluate "has('name')" at compile time
2314 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2315 {
2316 char_u *s = skipwhite(*arg + varlen + 1);
2317 typval_T argvars[2];
2318
2319 argvars[0].v_type = VAR_UNKNOWN;
2320 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002321 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002322 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002323 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002324 s = skipwhite(s);
2325 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2326 {
2327 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2328
2329 *arg = s + 1;
2330 argvars[1].v_type = VAR_UNKNOWN;
2331 tv->v_type = VAR_NUMBER;
2332 tv->vval.v_number = 0;
2333 f_has(argvars, tv);
2334 clear_tv(&argvars[0]);
2335 ++ppconst->pp_used;
2336 return OK;
2337 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002338 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002339 }
2340
2341 if (generate_ppconst(cctx, ppconst) == FAIL)
2342 return FAIL;
2343
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002344 if (varlen >= sizeof(namebuf))
2345 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002346 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347 return FAIL;
2348 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002349 vim_strncpy(namebuf, *arg, varlen);
2350 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002351
2352 *arg = skipwhite(*arg + varlen + 1);
2353 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002354 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002355
Bram Moolenaara1773442020-08-12 15:21:22 +02002356 is_autoload = vim_strchr(name, '#') != NULL;
2357 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002358 {
2359 int idx;
2360
2361 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002362 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002363 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002364 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002365 else
2366 semsg(_(e_unknownfunc), namebuf);
2367 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368 }
2369
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002370 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002371 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002373 {
2374 res = generate_CALL(cctx, ufunc, argcount);
2375 goto theend;
2376 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002377
2378 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002379 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002380 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002381 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002382 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaara26b9702020-04-18 19:53:28 +02002383 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002384 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002385 garray_T *stack = &cctx->ctx_type_stack;
2386 type_T *type;
2387
2388 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2389 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002390 goto theend;
2391 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002392
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002393 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002394 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002395 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002396 res = generate_UCALL(cctx, name, argcount);
2397 else
2398 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002399
2400theend:
2401 vim_free(tofree);
2402 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403}
2404
2405// like NAMESPACE_CHAR but with 'a' and 'l'.
2406#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2407
2408/*
2409 * Find the end of a variable or function name. Unlike find_name_end() this
2410 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002411 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002412 * Return a pointer to just after the name. Equal to "arg" if there is no
2413 * valid name.
2414 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002415 static char_u *
2416to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417{
2418 char_u *p;
2419
2420 // Quick check for valid starting character.
2421 if (!eval_isnamec1(*arg))
2422 return arg;
2423
2424 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2425 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2426 // and can be used in slice "[n:]".
2427 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002428 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002429 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2430 break;
2431 return p;
2432}
2433
2434/*
2435 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002436 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002437 */
2438 char_u *
2439to_name_const_end(char_u *arg)
2440{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002441 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442 typval_T rettv;
2443
2444 if (p == arg && *arg == '[')
2445 {
2446
2447 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002448 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002449 p = arg;
2450 }
2451 else if (p == arg && *arg == '#' && arg[1] == '{')
2452 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002453 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002454 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002455 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002456 p = arg;
2457 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002458
2459 return p;
2460}
2461
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002462/*
2463 * parse a list: [expr, expr]
2464 * "*arg" points to the '['.
2465 */
2466 static int
2467compile_list(char_u **arg, cctx_T *cctx)
2468{
2469 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002470 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471 int count = 0;
2472
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002473 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002474 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002475 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002476 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002477 semsg(_(e_list_end), *arg);
2478 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002479 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002480 if (*p == ',')
2481 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002482 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002483 return FAIL;
2484 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002485 if (*p == ']')
2486 {
2487 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002488 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002489 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002490 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002491 break;
2492 ++count;
2493 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002494 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002496 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2497 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002498 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002499 return FAIL;
2500 }
2501 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002502 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 p = skipwhite(p);
2504 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002505 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506
2507 generate_NEWLIST(cctx, count);
2508 return OK;
2509}
2510
2511/*
2512 * parse a lambda: {arg, arg -> expr}
2513 * "*arg" points to the '{'.
2514 */
2515 static int
2516compile_lambda(char_u **arg, cctx_T *cctx)
2517{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 typval_T rettv;
2519 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002520 evalarg_T evalarg;
2521
2522 CLEAR_FIELD(evalarg);
2523 evalarg.eval_flags = EVAL_EVALUATE;
2524 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525
2526 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002527 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002529
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002531 ++ufunc->uf_refcount;
2532 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002533 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534
2535 // The function will have one line: "return {expr}".
2536 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002537 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002539 clear_evalarg(&evalarg, NULL);
2540
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002541 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002542 {
2543 // The return type will now be known.
2544 set_function_type(ufunc);
2545
2546 return generate_FUNCREF(cctx, ufunc);
2547 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002548
2549 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 return FAIL;
2551}
2552
2553/*
2554 * Compile a lamda call: expr->{lambda}(args)
2555 * "arg" points to the "{".
2556 */
2557 static int
2558compile_lambda_call(char_u **arg, cctx_T *cctx)
2559{
2560 ufunc_T *ufunc;
2561 typval_T rettv;
2562 int argcount = 1;
2563 int ret = FAIL;
2564
2565 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002566 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 return FAIL;
2568
2569 if (**arg != '(')
2570 {
2571 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002572 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002573 else
2574 semsg(_(e_missing_paren), "lambda");
2575 clear_tv(&rettv);
2576 return FAIL;
2577 }
2578
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579 ufunc = rettv.vval.v_partial->pt_func;
2580 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002581 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002582 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002583
2584 // The function will have one line: "return {expr}".
2585 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002586 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002587
2588 // compile the arguments
2589 *arg = skipwhite(*arg + 1);
2590 if (compile_arguments(arg, cctx, &argcount) == OK)
2591 // call the compiled function
2592 ret = generate_CALL(cctx, ufunc, argcount);
2593
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002594 if (ret == FAIL)
2595 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596 return ret;
2597}
2598
2599/*
2600 * parse a dict: {'key': val} or #{key: val}
2601 * "*arg" points to the '{'.
2602 */
2603 static int
2604compile_dict(char_u **arg, cctx_T *cctx, int literal)
2605{
2606 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002607 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002608 int count = 0;
2609 dict_T *d = dict_alloc();
2610 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002611 char_u *whitep = *arg;
2612 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002613
2614 if (d == NULL)
2615 return FAIL;
2616 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002617 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002618 {
2619 char_u *key = NULL;
2620
Bram Moolenaar23c55272020-06-21 16:58:13 +02002621 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002622 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002623 *arg = NULL;
2624 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002625 }
2626
2627 if (**arg == '}')
2628 break;
2629
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 if (literal)
2631 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002632 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633
Bram Moolenaar2c330432020-04-13 14:41:35 +02002634 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002636 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637 return FAIL;
2638 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002639 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640 if (generate_PUSHS(cctx, key) == FAIL)
2641 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002642 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643 }
2644 else
2645 {
2646 isn_T *isn;
2647
Bram Moolenaara5565e42020-05-09 15:44:01 +02002648 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002650 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2651 if (isn->isn_type == ISN_PUSHS)
2652 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002653 else
2654 {
2655 type_T *keytype = ((type_T **)stack->ga_data)
2656 [stack->ga_len - 1];
2657 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2658 return FAIL;
2659 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660 }
2661
2662 // Check for duplicate keys, if using string keys.
2663 if (key != NULL)
2664 {
2665 item = dict_find(d, key, -1);
2666 if (item != NULL)
2667 {
2668 semsg(_(e_duplicate_key), key);
2669 goto failret;
2670 }
2671 item = dictitem_alloc(key);
2672 if (item != NULL)
2673 {
2674 item->di_tv.v_type = VAR_UNKNOWN;
2675 item->di_tv.v_lock = 0;
2676 if (dict_add(d, item) == FAIL)
2677 dictitem_free(item);
2678 }
2679 }
2680
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002681 if (**arg != ':')
2682 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002683 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002684 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002685 else
2686 semsg(_(e_missing_dict_colon), *arg);
2687 return FAIL;
2688 }
2689 whitep = *arg + 1;
2690 if (!IS_WHITE_OR_NUL(*whitep))
2691 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002692 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002693 return FAIL;
2694 }
2695
2696 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002697 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002698 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002699 *arg = NULL;
2700 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002701 }
2702
Bram Moolenaara5565e42020-05-09 15:44:01 +02002703 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704 return FAIL;
2705 ++count;
2706
Bram Moolenaar2c330432020-04-13 14:41:35 +02002707 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002708 *arg = skipwhite(*arg);
2709 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002710 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002711 *arg = NULL;
2712 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002713 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002714 if (**arg == '}')
2715 break;
2716 if (**arg != ',')
2717 {
2718 semsg(_(e_missing_dict_comma), *arg);
2719 goto failret;
2720 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002721 if (IS_WHITE_OR_NUL(*whitep))
2722 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002723 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002724 return FAIL;
2725 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002726 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 *arg = skipwhite(*arg + 1);
2728 }
2729
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730 *arg = *arg + 1;
2731
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002732 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002733 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002734 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002735 *arg += STRLEN(*arg);
2736
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002737 dict_unref(d);
2738 return generate_NEWDICT(cctx, count);
2739
2740failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002741 if (*arg == NULL)
2742 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002743 dict_unref(d);
2744 return FAIL;
2745}
2746
2747/*
2748 * Compile "&option".
2749 */
2750 static int
2751compile_get_option(char_u **arg, cctx_T *cctx)
2752{
2753 typval_T rettv;
2754 char_u *start = *arg;
2755 int ret;
2756
2757 // parse the option and get the current value to get the type.
2758 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002759 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760 if (ret == OK)
2761 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002762 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002763 char_u *name = vim_strnsave(start, *arg - start);
2764 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2765
2766 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2767 vim_free(name);
2768 }
2769 clear_tv(&rettv);
2770
2771 return ret;
2772}
2773
2774/*
2775 * Compile "$VAR".
2776 */
2777 static int
2778compile_get_env(char_u **arg, cctx_T *cctx)
2779{
2780 char_u *start = *arg;
2781 int len;
2782 int ret;
2783 char_u *name;
2784
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785 ++*arg;
2786 len = get_env_len(arg);
2787 if (len == 0)
2788 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002789 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002790 return FAIL;
2791 }
2792
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002793 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794 name = vim_strnsave(start, len + 1);
2795 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2796 vim_free(name);
2797 return ret;
2798}
2799
2800/*
2801 * Compile "@r".
2802 */
2803 static int
2804compile_get_register(char_u **arg, cctx_T *cctx)
2805{
2806 int ret;
2807
2808 ++*arg;
2809 if (**arg == NUL)
2810 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002811 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002812 return FAIL;
2813 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002814 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 {
2816 emsg_invreg(**arg);
2817 return FAIL;
2818 }
2819 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2820 ++*arg;
2821 return ret;
2822}
2823
2824/*
2825 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002826 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002827 */
2828 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002829apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002831 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002832
2833 // this works from end to start
2834 while (p > start)
2835 {
2836 --p;
2837 if (*p == '-' || *p == '+')
2838 {
2839 // only '-' has an effect, for '+' we only check the type
2840#ifdef FEAT_FLOAT
2841 if (rettv->v_type == VAR_FLOAT)
2842 {
2843 if (*p == '-')
2844 rettv->vval.v_float = -rettv->vval.v_float;
2845 }
2846 else
2847#endif
2848 {
2849 varnumber_T val;
2850 int error = FALSE;
2851
2852 // tv_get_number_chk() accepts a string, but we don't want that
2853 // here
2854 if (check_not_string(rettv) == FAIL)
2855 return FAIL;
2856 val = tv_get_number_chk(rettv, &error);
2857 clear_tv(rettv);
2858 if (error)
2859 return FAIL;
2860 if (*p == '-')
2861 val = -val;
2862 rettv->v_type = VAR_NUMBER;
2863 rettv->vval.v_number = val;
2864 }
2865 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002866 else if (numeric_only)
2867 {
2868 ++p;
2869 break;
2870 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871 else
2872 {
2873 int v = tv2bool(rettv);
2874
2875 // '!' is permissive in the type.
2876 clear_tv(rettv);
2877 rettv->v_type = VAR_BOOL;
2878 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2879 }
2880 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002881 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 return OK;
2883}
2884
2885/*
2886 * Recognize v: variables that are constants and set "rettv".
2887 */
2888 static void
2889get_vim_constant(char_u **arg, typval_T *rettv)
2890{
2891 if (STRNCMP(*arg, "v:true", 6) == 0)
2892 {
2893 rettv->v_type = VAR_BOOL;
2894 rettv->vval.v_number = VVAL_TRUE;
2895 *arg += 6;
2896 }
2897 else if (STRNCMP(*arg, "v:false", 7) == 0)
2898 {
2899 rettv->v_type = VAR_BOOL;
2900 rettv->vval.v_number = VVAL_FALSE;
2901 *arg += 7;
2902 }
2903 else if (STRNCMP(*arg, "v:null", 6) == 0)
2904 {
2905 rettv->v_type = VAR_SPECIAL;
2906 rettv->vval.v_number = VVAL_NULL;
2907 *arg += 6;
2908 }
2909 else if (STRNCMP(*arg, "v:none", 6) == 0)
2910 {
2911 rettv->v_type = VAR_SPECIAL;
2912 rettv->vval.v_number = VVAL_NONE;
2913 *arg += 6;
2914 }
2915}
2916
Bram Moolenaar696ba232020-07-29 21:20:41 +02002917 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02002918get_compare_type(char_u *p, int *len, int *type_is)
2919{
2920 exptype_T type = EXPR_UNKNOWN;
2921 int i;
2922
2923 switch (p[0])
2924 {
2925 case '=': if (p[1] == '=')
2926 type = EXPR_EQUAL;
2927 else if (p[1] == '~')
2928 type = EXPR_MATCH;
2929 break;
2930 case '!': if (p[1] == '=')
2931 type = EXPR_NEQUAL;
2932 else if (p[1] == '~')
2933 type = EXPR_NOMATCH;
2934 break;
2935 case '>': if (p[1] != '=')
2936 {
2937 type = EXPR_GREATER;
2938 *len = 1;
2939 }
2940 else
2941 type = EXPR_GEQUAL;
2942 break;
2943 case '<': if (p[1] != '=')
2944 {
2945 type = EXPR_SMALLER;
2946 *len = 1;
2947 }
2948 else
2949 type = EXPR_SEQUAL;
2950 break;
2951 case 'i': if (p[1] == 's')
2952 {
2953 // "is" and "isnot"; but not a prefix of a name
2954 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2955 *len = 5;
2956 i = p[*len];
2957 if (!isalnum(i) && i != '_')
2958 {
2959 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2960 *type_is = TRUE;
2961 }
2962 }
2963 break;
2964 }
2965 return type;
2966}
2967
2968/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002970 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971 */
2972 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002973compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002975 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976
2977 // this works from end to start
2978 while (p > start)
2979 {
2980 --p;
2981 if (*p == '-' || *p == '+')
2982 {
2983 int negate = *p == '-';
2984 isn_T *isn;
2985
2986 // TODO: check type
2987 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2988 {
2989 --p;
2990 if (*p == '-')
2991 negate = !negate;
2992 }
2993 // only '-' has an effect, for '+' we only check the type
2994 if (negate)
2995 isn = generate_instr(cctx, ISN_NEGATENR);
2996 else
2997 isn = generate_instr(cctx, ISN_CHECKNR);
2998 if (isn == NULL)
2999 return FAIL;
3000 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003001 else if (numeric_only)
3002 {
3003 ++p;
3004 break;
3005 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006 else
3007 {
3008 int invert = TRUE;
3009
3010 while (p > start && p[-1] == '!')
3011 {
3012 --p;
3013 invert = !invert;
3014 }
3015 if (generate_2BOOL(cctx, invert) == FAIL)
3016 return FAIL;
3017 }
3018 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003019 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 return OK;
3021}
3022
3023/*
3024 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003025 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 */
3027 static int
3028compile_subscript(
3029 char_u **arg,
3030 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003031 char_u *start_leader,
3032 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003033 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003035 char_u *name_start = *end_leader;
3036
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 for (;;)
3038 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003039 char_u *p = skipwhite(*arg);
3040
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003041 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003042 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003043 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003044
3045 // If a following line starts with "->{" or "->X" advance to that
3046 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003047 // Also if a following line starts with ".x".
3048 if (next != NULL &&
3049 ((next[0] == '-' && next[1] == '>'
3050 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003051 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003052 {
3053 next = next_line_from_context(cctx, TRUE);
3054 if (next == NULL)
3055 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003056 *arg = next;
3057 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003058 }
3059 }
3060
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003061 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3062 // not a function call.
3063 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003065 garray_T *stack = &cctx->ctx_type_stack;
3066 type_T *type;
3067 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003069 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003070 return FAIL;
3071
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003073 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3074
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003075 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3077 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003078 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 return FAIL;
3080 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003081 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003083 char_u *pstart = p;
3084
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003085 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003086 return FAIL;
3087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 // something->method()
3089 // Apply the '!', '-' and '+' first:
3090 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003091 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003094 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003095 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003096 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 if (**arg == '{')
3098 {
3099 // lambda call: list->{lambda}
3100 if (compile_lambda_call(arg, cctx) == FAIL)
3101 return FAIL;
3102 }
3103 else
3104 {
3105 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003106 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003107 if (!eval_isnamec1(*p))
3108 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003109 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003110 return FAIL;
3111 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003112 if (ASCII_ISALPHA(*p) && p[1] == ':')
3113 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003114 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 ;
3116 if (*p != '(')
3117 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003118 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003119 return FAIL;
3120 }
3121 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003122 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123 return FAIL;
3124 }
3125 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003126 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003128 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003129 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003130 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003131 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003132 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003133
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003134 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003135 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003136 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003137 // TODO: blob index
3138 // TODO: more arguments
3139 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003140 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003141 return FAIL;
3142
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003143 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003144 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003145 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003146 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003147 if (**arg == ':')
3148 // missing first index is equal to zero
3149 generate_PUSHNR(cctx, 0);
3150 else
3151 {
3152 if (compile_expr0(arg, cctx) == FAIL)
3153 return FAIL;
3154 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3155 return FAIL;
3156 *arg = skipwhite(*arg);
3157 }
3158 if (**arg == ':')
3159 {
3160 *arg = skipwhite(*arg + 1);
3161 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3162 return FAIL;
3163 if (**arg == ']')
3164 // missing second index is equal to end of string
3165 generate_PUSHNR(cctx, -1);
3166 else
3167 {
3168 if (compile_expr0(arg, cctx) == FAIL)
3169 return FAIL;
3170 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3171 return FAIL;
3172 *arg = skipwhite(*arg);
3173 }
3174 is_slice = TRUE;
3175 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176
3177 if (**arg != ']')
3178 {
3179 emsg(_(e_missbrac));
3180 return FAIL;
3181 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003182 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003184 // We can index a list and a dict. If we don't know the type
3185 // we can use the index value type.
3186 // TODO: If we don't know use an instruction to figure it out at
3187 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003188 typep = ((type_T **)stack->ga_data) + stack->ga_len
3189 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003190 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003191 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3192 // If the index is a string, the variable must be a Dict.
3193 if (*typep == &t_any && valtype == &t_string)
3194 vtype = VAR_DICT;
3195 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003196 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003197 if (need_type(valtype, &t_number, -1, cctx, FALSE) == FAIL)
3198 return FAIL;
3199 if (is_slice)
3200 {
3201 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
3202 if (need_type(valtype, &t_number, -2, cctx, FALSE) == FAIL)
3203 return FAIL;
3204 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003205 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003206
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003207 if (vtype == VAR_DICT)
3208 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003209 if (is_slice)
3210 {
3211 emsg(_(e_cannot_slice_dictionary));
3212 return FAIL;
3213 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003214 if ((*typep)->tt_type == VAR_DICT)
3215 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003216 else
3217 {
3218 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3219 return FAIL;
3220 *typep = &t_any;
3221 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003222 if (may_generate_2STRING(-1, cctx) == FAIL)
3223 return FAIL;
3224 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3225 return FAIL;
3226 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003227 else if (vtype == VAR_STRING)
3228 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003229 *typep = &t_string;
3230 if ((is_slice
3231 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3232 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003233 return FAIL;
3234 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003235 else if (vtype == VAR_BLOB)
3236 {
3237 emsg("Sorry, blob index and slice not implemented yet");
3238 return FAIL;
3239 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003240 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003241 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003242 if (is_slice)
3243 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003244 if (generate_instr_drop(cctx,
3245 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3246 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003247 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003248 }
Bram Moolenaared591872020-08-15 22:14:53 +02003249 else
3250 {
3251 if ((*typep)->tt_type == VAR_LIST)
3252 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003253 if (generate_instr_drop(cctx,
3254 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3255 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003256 return FAIL;
3257 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003258 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003259 else
3260 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003261 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003262 return FAIL;
3263 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003265 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003267 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003268 return FAIL;
3269
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003270 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003271 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3272 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003274 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003275 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 while (eval_isnamec(*p))
3277 MB_PTR_ADV(p);
3278 if (p == *arg)
3279 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003280 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281 return FAIL;
3282 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003283 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 return FAIL;
3285 *arg = p;
3286 }
3287 else
3288 break;
3289 }
3290
3291 // TODO - see handle_subscript():
3292 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3293 // Don't do this when "Func" is already a partial that was bound
3294 // explicitly (pt_auto is FALSE).
3295
3296 return OK;
3297}
3298
3299/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003300 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3301 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003303 * If the value is a constant "ppconst->pp_ret" will be set.
3304 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003305 *
3306 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307 */
3308
3309/*
3310 * number number constant
3311 * 0zFFFFFFFF Blob constant
3312 * "string" string constant
3313 * 'string' literal string constant
3314 * &option-name option value
3315 * @r register contents
3316 * identifier variable value
3317 * function() function call
3318 * $VAR environment variable
3319 * (expression) nested expression
3320 * [expr, expr] List
3321 * {key: val, key: val} Dictionary
3322 * #{key: val, key: val} Dictionary with literal keys
3323 *
3324 * Also handle:
3325 * ! in front logical NOT
3326 * - in front unary minus
3327 * + in front unary plus (ignored)
3328 * trailing (arg) funcref/partial call
3329 * trailing [] subscript in String or List
3330 * trailing .name entry in Dictionary
3331 * trailing ->name() method call
3332 */
3333 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003334compile_expr7(
3335 char_u **arg,
3336 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003337 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003338{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339 char_u *start_leader, *end_leader;
3340 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003341 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003342 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343
3344 /*
3345 * Skip '!', '-' and '+' characters. They are handled later.
3346 */
3347 start_leader = *arg;
3348 while (**arg == '!' || **arg == '-' || **arg == '+')
3349 *arg = skipwhite(*arg + 1);
3350 end_leader = *arg;
3351
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003352 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353 switch (**arg)
3354 {
3355 /*
3356 * Number constant.
3357 */
3358 case '0': // also for blob starting with 0z
3359 case '1':
3360 case '2':
3361 case '3':
3362 case '4':
3363 case '5':
3364 case '6':
3365 case '7':
3366 case '8':
3367 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003368 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003369 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003370 // Apply "-" and "+" just before the number now, right to
3371 // left. Matters especially when "->" follows. Stops at
3372 // '!'.
3373 if (apply_leader(rettv, TRUE,
3374 start_leader, &end_leader) == FAIL)
3375 {
3376 clear_tv(rettv);
3377 return FAIL;
3378 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379 break;
3380
3381 /*
3382 * String constant: "string".
3383 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003384 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385 return FAIL;
3386 break;
3387
3388 /*
3389 * Literal string constant: 'str''ing'.
3390 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003391 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 return FAIL;
3393 break;
3394
3395 /*
3396 * Constant Vim variable.
3397 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003398 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 ret = NOTDONE;
3400 break;
3401
3402 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003403 * "true" constant
3404 */
3405 case 't': if (STRNCMP(*arg, "true", 4) == 0
3406 && !eval_isnamec((*arg)[4]))
3407 {
3408 *arg += 4;
3409 rettv->v_type = VAR_BOOL;
3410 rettv->vval.v_number = VVAL_TRUE;
3411 }
3412 else
3413 ret = NOTDONE;
3414 break;
3415
3416 /*
3417 * "false" constant
3418 */
3419 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3420 && !eval_isnamec((*arg)[5]))
3421 {
3422 *arg += 5;
3423 rettv->v_type = VAR_BOOL;
3424 rettv->vval.v_number = VVAL_FALSE;
3425 }
3426 else
3427 ret = NOTDONE;
3428 break;
3429
3430 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 * List: [expr, expr]
3432 */
3433 case '[': ret = compile_list(arg, cctx);
3434 break;
3435
3436 /*
3437 * Dictionary: #{key: val, key: val}
3438 */
3439 case '#': if ((*arg)[1] == '{')
3440 {
3441 ++*arg;
3442 ret = compile_dict(arg, cctx, TRUE);
3443 }
3444 else
3445 ret = NOTDONE;
3446 break;
3447
3448 /*
3449 * Lambda: {arg, arg -> expr}
3450 * Dictionary: {'key': val, 'key': val}
3451 */
3452 case '{': {
3453 char_u *start = skipwhite(*arg + 1);
3454
3455 // Find out what comes after the arguments.
3456 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003457 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 if (ret != FAIL && *start == '>')
3459 ret = compile_lambda(arg, cctx);
3460 else
3461 ret = compile_dict(arg, cctx, FALSE);
3462 }
3463 break;
3464
3465 /*
3466 * Option value: &name
3467 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003468 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3469 return FAIL;
3470 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003471 break;
3472
3473 /*
3474 * Environment variable: $VAR.
3475 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003476 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3477 return FAIL;
3478 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479 break;
3480
3481 /*
3482 * Register contents: @r.
3483 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003484 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3485 return FAIL;
3486 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 break;
3488 /*
3489 * nested expression: (expression).
3490 */
3491 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003492
3493 // recursive!
3494 if (ppconst->pp_used <= PPSIZE - 10)
3495 {
3496 ret = compile_expr1(arg, cctx, ppconst);
3497 }
3498 else
3499 {
3500 // Not enough space in ppconst, flush constants.
3501 if (generate_ppconst(cctx, ppconst) == FAIL)
3502 return FAIL;
3503 ret = compile_expr0(arg, cctx);
3504 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003505 *arg = skipwhite(*arg);
3506 if (**arg == ')')
3507 ++*arg;
3508 else if (ret == OK)
3509 {
3510 emsg(_(e_missing_close));
3511 ret = FAIL;
3512 }
3513 break;
3514
3515 default: ret = NOTDONE;
3516 break;
3517 }
3518 if (ret == FAIL)
3519 return FAIL;
3520
Bram Moolenaar1c747212020-05-09 18:28:34 +02003521 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003522 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003523 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003524 clear_tv(rettv);
3525 else
3526 // A constant expression can possibly be handled compile time,
3527 // return the value instead of generating code.
3528 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003529 }
3530 else if (ret == NOTDONE)
3531 {
3532 char_u *p;
3533 int r;
3534
3535 if (!eval_isnamec1(**arg))
3536 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003537 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538 return FAIL;
3539 }
3540
3541 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003542 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003544 {
3545 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3546 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003548 {
3549 if (generate_ppconst(cctx, ppconst) == FAIL)
3550 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003552 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553 if (r == FAIL)
3554 return FAIL;
3555 }
3556
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003557 // Handle following "[]", ".member", etc.
3558 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003559 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003560 ppconst) == FAIL)
3561 return FAIL;
3562 if (ppconst->pp_used > 0)
3563 {
3564 // apply the '!', '-' and '+' before the constant
3565 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003566 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003567 return FAIL;
3568 return OK;
3569 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003570 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003572 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573}
3574
3575/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003576 * Give the "white on both sides" error, taking the operator from "p[len]".
3577 */
3578 void
3579error_white_both(char_u *op, int len)
3580{
3581 char_u buf[10];
3582
3583 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003584 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003585}
3586
3587/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003588 * <type>expr7: runtime type check / conversion
3589 */
3590 static int
3591compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3592{
3593 type_T *want_type = NULL;
3594
3595 // Recognize <type>
3596 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3597 {
3598 int called_emsg_before = called_emsg;
3599
3600 ++*arg;
3601 want_type = parse_type(arg, cctx->ctx_type_list);
3602 if (called_emsg != called_emsg_before)
3603 return FAIL;
3604
3605 if (**arg != '>')
3606 {
3607 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003608 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003609 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003610 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003611 return FAIL;
3612 }
3613 ++*arg;
3614 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3615 return FAIL;
3616 }
3617
3618 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3619 return FAIL;
3620
3621 if (want_type != NULL)
3622 {
3623 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003624 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003625
Bram Moolenaard1103582020-08-14 22:44:25 +02003626 generate_ppconst(cctx, ppconst);
3627 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003628 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003629 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003630 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3631 return FAIL;
3632 }
3633 }
3634
3635 return OK;
3636}
3637
3638/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639 * * number multiplication
3640 * / number division
3641 * % number modulo
3642 */
3643 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003644compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645{
3646 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003647 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003648 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003650 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003651 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 return FAIL;
3653
3654 /*
3655 * Repeat computing, until no "*", "/" or "%" is following.
3656 */
3657 for (;;)
3658 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003659 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 if (*op != '*' && *op != '/' && *op != '%')
3661 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003662 if (next != NULL)
3663 {
3664 *arg = next_line_from_context(cctx, TRUE);
3665 op = skipwhite(*arg);
3666 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003667
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003668 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003670 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003671 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003672 }
3673 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003674 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003675 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003677 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003678 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003680
3681 if (ppconst->pp_used == ppconst_used + 2
3682 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3683 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003684 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003685 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3686 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003687 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003689 // both are numbers: compute the result
3690 switch (*op)
3691 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003692 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003693 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003694 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003695 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003696 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003697 break;
3698 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003699 tv1->vval.v_number = res;
3700 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003701 }
3702 else
3703 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003704 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003705 generate_two_op(cctx, op);
3706 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707 }
3708
3709 return OK;
3710}
3711
3712/*
3713 * + number addition
3714 * - number subtraction
3715 * .. string concatenation
3716 */
3717 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003718compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719{
3720 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003721 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003722 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003723 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724
3725 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003726 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727 return FAIL;
3728
3729 /*
3730 * Repeat computing, until no "+", "-" or ".." is following.
3731 */
3732 for (;;)
3733 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003734 op = may_peek_next_line(cctx, *arg, &next);
3735 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 break;
3737 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003738 if (next != NULL)
3739 {
3740 *arg = next_line_from_context(cctx, TRUE);
3741 op = skipwhite(*arg);
3742 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003743
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003744 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003745 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003746 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003747 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748 }
3749
3750 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003751 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003752 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003754 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003755 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756 return FAIL;
3757
Bram Moolenaara5565e42020-05-09 15:44:01 +02003758 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003759 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003760 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3761 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3762 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3763 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003764 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003765 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3766 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003767
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003768 // concat/subtract/add constant numbers
3769 if (*op == '+')
3770 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3771 else if (*op == '-')
3772 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3773 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003774 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003775 // concatenate constant strings
3776 char_u *s1 = tv1->vval.v_string;
3777 char_u *s2 = tv2->vval.v_string;
3778 size_t len1 = STRLEN(s1);
3779
3780 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3781 if (tv1->vval.v_string == NULL)
3782 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003783 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003784 return FAIL;
3785 }
3786 mch_memmove(tv1->vval.v_string, s1, len1);
3787 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003788 vim_free(s1);
3789 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003790 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003791 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792 }
3793 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003794 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003795 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003796 if (*op == '.')
3797 {
3798 if (may_generate_2STRING(-2, cctx) == FAIL
3799 || may_generate_2STRING(-1, cctx) == FAIL)
3800 return FAIL;
3801 generate_instr_drop(cctx, ISN_CONCAT, 1);
3802 }
3803 else
3804 generate_two_op(cctx, op);
3805 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 }
3807
3808 return OK;
3809}
3810
3811/*
3812 * expr5a == expr5b
3813 * expr5a =~ expr5b
3814 * expr5a != expr5b
3815 * expr5a !~ expr5b
3816 * expr5a > expr5b
3817 * expr5a >= expr5b
3818 * expr5a < expr5b
3819 * expr5a <= expr5b
3820 * expr5a is expr5b
3821 * expr5a isnot expr5b
3822 *
3823 * Produces instructions:
3824 * EVAL expr5a Push result of "expr5a"
3825 * EVAL expr5b Push result of "expr5b"
3826 * COMPARE one of the compare instructions
3827 */
3828 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003829compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830{
3831 exptype_T type = EXPR_UNKNOWN;
3832 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003833 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003835 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003836 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837
3838 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003839 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 return FAIL;
3841
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003842 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003843 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844
3845 /*
3846 * If there is a comparative operator, use it.
3847 */
3848 if (type != EXPR_UNKNOWN)
3849 {
3850 int ic = FALSE; // Default: do not ignore case
3851
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003852 if (next != NULL)
3853 {
3854 *arg = next_line_from_context(cctx, TRUE);
3855 p = skipwhite(*arg);
3856 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 if (type_is && (p[len] == '?' || p[len] == '#'))
3858 {
3859 semsg(_(e_invexpr2), *arg);
3860 return FAIL;
3861 }
3862 // extra question mark appended: ignore case
3863 if (p[len] == '?')
3864 {
3865 ic = TRUE;
3866 ++len;
3867 }
3868 // extra '#' appended: match case (ignored)
3869 else if (p[len] == '#')
3870 ++len;
3871 // nothing appended: match case
3872
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003873 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003874 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003875 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003876 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003877 }
3878
3879 // get the second variable
3880 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003881 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003882 return FAIL;
3883
Bram Moolenaara5565e42020-05-09 15:44:01 +02003884 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 return FAIL;
3886
Bram Moolenaara5565e42020-05-09 15:44:01 +02003887 if (ppconst->pp_used == ppconst_used + 2)
3888 {
3889 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3890 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3891 int ret;
3892
3893 // Both sides are a constant, compute the result now.
3894 // First check for a valid combination of types, this is more
3895 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003896 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003897 ret = FAIL;
3898 else
3899 {
3900 ret = typval_compare(tv1, tv2, type, ic);
3901 tv1->v_type = VAR_BOOL;
3902 tv1->vval.v_number = tv1->vval.v_number
3903 ? VVAL_TRUE : VVAL_FALSE;
3904 clear_tv(tv2);
3905 --ppconst->pp_used;
3906 }
3907 return ret;
3908 }
3909
3910 generate_ppconst(cctx, ppconst);
3911 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 }
3913
3914 return OK;
3915}
3916
Bram Moolenaar7f141552020-05-09 17:35:53 +02003917static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3918
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919/*
3920 * Compile || or &&.
3921 */
3922 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003923compile_and_or(
3924 char_u **arg,
3925 cctx_T *cctx,
3926 char *op,
3927 ppconst_T *ppconst,
3928 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003929{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003930 char_u *next;
3931 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932 int opchar = *op;
3933
3934 if (p[0] == opchar && p[1] == opchar)
3935 {
3936 garray_T *instr = &cctx->ctx_instr;
3937 garray_T end_ga;
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02003938 garray_T *stack = &cctx->ctx_type_stack;
3939 type_T **typep;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940
3941 /*
3942 * Repeat until there is no following "||" or "&&"
3943 */
3944 ga_init2(&end_ga, sizeof(int), 10);
3945 while (p[0] == opchar && p[1] == opchar)
3946 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003947 if (next != NULL)
3948 {
3949 *arg = next_line_from_context(cctx, TRUE);
3950 p = skipwhite(*arg);
3951 }
3952
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003953 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3954 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003955 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003956 return FAIL;
3957 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003958
Bram Moolenaara5565e42020-05-09 15:44:01 +02003959 // TODO: use ppconst if the value is a constant
3960 generate_ppconst(cctx, ppconst);
3961
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962 if (ga_grow(&end_ga, 1) == FAIL)
3963 {
3964 ga_clear(&end_ga);
3965 return FAIL;
3966 }
3967 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3968 ++end_ga.ga_len;
3969 generate_JUMP(cctx, opchar == '|'
3970 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3971
3972 // eval the next expression
3973 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003974 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003975 return FAIL;
3976
Bram Moolenaara5565e42020-05-09 15:44:01 +02003977 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3978 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979 {
3980 ga_clear(&end_ga);
3981 return FAIL;
3982 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003983
3984 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003985 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003986 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987
3988 // Fill in the end label in all jumps.
3989 while (end_ga.ga_len > 0)
3990 {
3991 isn_T *isn;
3992
3993 --end_ga.ga_len;
3994 isn = ((isn_T *)instr->ga_data)
3995 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3996 isn->isn_arg.jump.jump_where = instr->ga_len;
3997 }
3998 ga_clear(&end_ga);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02003999
4000 // The resulting type can be used as a bool.
4001 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4002 if (*typep != &t_bool)
4003 {
4004 type_T *type = alloc_type(cctx->ctx_type_list);
4005
4006 if (type != NULL)
4007 {
4008 *type = **typep;
4009 type->tt_flags |= TTFLAG_BOOL_OK;
4010 *typep = type;
4011 }
4012 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004013 }
4014
4015 return OK;
4016}
4017
4018/*
4019 * expr4a && expr4a && expr4a logical AND
4020 *
4021 * Produces instructions:
4022 * EVAL expr4a Push result of "expr4a"
4023 * JUMP_AND_KEEP_IF_FALSE end
4024 * EVAL expr4b Push result of "expr4b"
4025 * JUMP_AND_KEEP_IF_FALSE end
4026 * EVAL expr4c Push result of "expr4c"
4027 * end:
4028 */
4029 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004030compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004031{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004032 int ppconst_used = ppconst->pp_used;
4033
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004035 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004036 return FAIL;
4037
4038 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004039 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040}
4041
4042/*
4043 * expr3a || expr3b || expr3c logical OR
4044 *
4045 * Produces instructions:
4046 * EVAL expr3a Push result of "expr3a"
4047 * JUMP_AND_KEEP_IF_TRUE end
4048 * EVAL expr3b Push result of "expr3b"
4049 * JUMP_AND_KEEP_IF_TRUE end
4050 * EVAL expr3c Push result of "expr3c"
4051 * end:
4052 */
4053 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004054compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004056 int ppconst_used = ppconst->pp_used;
4057
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004059 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060 return FAIL;
4061
4062 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004063 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064}
4065
4066/*
4067 * Toplevel expression: expr2 ? expr1a : expr1b
4068 *
4069 * Produces instructions:
4070 * EVAL expr2 Push result of "expr"
4071 * JUMP_IF_FALSE alt jump if false
4072 * EVAL expr1a
4073 * JUMP_ALWAYS end
4074 * alt: EVAL expr1b
4075 * end:
4076 */
4077 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004078compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004079{
4080 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004081 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004082 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004083
Bram Moolenaar3988f642020-08-27 22:43:03 +02004084 // Ignore all kinds of errors when not producing code.
4085 if (cctx->ctx_skip == SKIP_YES)
4086 {
4087 skip_expr(arg);
4088 return OK;
4089 }
4090
Bram Moolenaar61a89812020-05-07 16:58:17 +02004091 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004092 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093 return FAIL;
4094
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004095 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 if (*p == '?')
4097 {
4098 garray_T *instr = &cctx->ctx_instr;
4099 garray_T *stack = &cctx->ctx_type_stack;
4100 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004101 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004102 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004103 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004105 int has_const_expr = FALSE;
4106 int const_value = FALSE;
4107 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004109 if (next != NULL)
4110 {
4111 *arg = next_line_from_context(cctx, TRUE);
4112 p = skipwhite(*arg);
4113 }
4114
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004115 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4116 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004117 semsg(_(e_white_space_required_before_and_after_str), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004118 return FAIL;
4119 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120
Bram Moolenaara5565e42020-05-09 15:44:01 +02004121 if (ppconst->pp_used == ppconst_used + 1)
4122 {
4123 // the condition is a constant, we know whether the ? or the :
4124 // expression is to be evaluated.
4125 has_const_expr = TRUE;
4126 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4127 clear_tv(&ppconst->pp_tv[ppconst_used]);
4128 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004129 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4130 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004131 }
4132 else
4133 {
4134 generate_ppconst(cctx, ppconst);
4135 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4136 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137
4138 // evaluate the second expression; any type is accepted
4139 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004140 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004141 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004142 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004143 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144
Bram Moolenaara5565e42020-05-09 15:44:01 +02004145 if (!has_const_expr)
4146 {
4147 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004148
Bram Moolenaara5565e42020-05-09 15:44:01 +02004149 // remember the type and drop it
4150 --stack->ga_len;
4151 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152
Bram Moolenaara5565e42020-05-09 15:44:01 +02004153 end_idx = instr->ga_len;
4154 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4155
4156 // jump here from JUMP_IF_FALSE
4157 isn = ((isn_T *)instr->ga_data) + alt_idx;
4158 isn->isn_arg.jump.jump_where = instr->ga_len;
4159 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160
4161 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004162 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 if (*p != ':')
4164 {
4165 emsg(_(e_missing_colon));
4166 return FAIL;
4167 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004168 if (next != NULL)
4169 {
4170 *arg = next_line_from_context(cctx, TRUE);
4171 p = skipwhite(*arg);
4172 }
4173
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004174 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4175 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004176 semsg(_(e_white_space_required_before_and_after_str), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004177 return FAIL;
4178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179
4180 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004181 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004182 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4183 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004185 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004186 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004187 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004188 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004189
Bram Moolenaara5565e42020-05-09 15:44:01 +02004190 if (!has_const_expr)
4191 {
4192 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004193
Bram Moolenaara5565e42020-05-09 15:44:01 +02004194 // If the types differ, the result has a more generic type.
4195 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4196 common_type(type1, type2, &type2, cctx->ctx_type_list);
4197
4198 // jump here from JUMP_ALWAYS
4199 isn = ((isn_T *)instr->ga_data) + end_idx;
4200 isn->isn_arg.jump.jump_where = instr->ga_len;
4201 }
4202
4203 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204 }
4205 return OK;
4206}
4207
4208/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004209 * Toplevel expression.
4210 */
4211 static int
4212compile_expr0(char_u **arg, cctx_T *cctx)
4213{
4214 ppconst_T ppconst;
4215
4216 CLEAR_FIELD(ppconst);
4217 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4218 {
4219 clear_ppconst(&ppconst);
4220 return FAIL;
4221 }
4222 if (generate_ppconst(cctx, &ppconst) == FAIL)
4223 return FAIL;
4224 return OK;
4225}
4226
4227/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 * compile "return [expr]"
4229 */
4230 static char_u *
4231compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4232{
4233 char_u *p = arg;
4234 garray_T *stack = &cctx->ctx_type_stack;
4235 type_T *stack_type;
4236
4237 if (*p != NUL && *p != '|' && *p != '\n')
4238 {
4239 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004240 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241 return NULL;
4242
4243 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4244 if (set_return_type)
4245 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004246 else
4247 {
4248 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4249 && stack_type->tt_type != VAR_VOID
4250 && stack_type->tt_type != VAR_UNKNOWN)
4251 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004252 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004253 return NULL;
4254 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004255 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4256 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004257 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004258 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259 }
4260 else
4261 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004262 // "set_return_type" cannot be TRUE, only used for a lambda which
4263 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004264 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4265 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004267 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004268 return NULL;
4269 }
4270
4271 // No argument, return zero.
4272 generate_PUSHNR(cctx, 0);
4273 }
4274
4275 if (generate_instr(cctx, ISN_RETURN) == NULL)
4276 return NULL;
4277
4278 // "return val | endif" is possible
4279 return skipwhite(p);
4280}
4281
4282/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004283 * Get a line from the compilation context, compatible with exarg_T getline().
4284 * Return a pointer to the line in allocated memory.
4285 * Return NULL for end-of-file or some error.
4286 */
4287 static char_u *
4288exarg_getline(
4289 int c UNUSED,
4290 void *cookie,
4291 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004292 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004293{
4294 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004295 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004296
Bram Moolenaar66250c92020-08-20 15:02:42 +02004297 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004298 {
Bram Moolenaar66250c92020-08-20 15:02:42 +02004299 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4300 return NULL;
4301 ++cctx->ctx_lnum;
4302 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4303 // Comment lines result in NULL pointers, skip them.
4304 if (p != NULL)
4305 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004306 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004307}
4308
4309/*
4310 * Compile a nested :def command.
4311 */
4312 static char_u *
4313compile_nested_function(exarg_T *eap, cctx_T *cctx)
4314{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004315 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004316 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004317 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004318 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004319 lvar_T *lvar;
4320 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004321 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004322
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004323 if (*name_start == '!')
4324 {
4325 emsg(_(e_cannot_use_bang_with_nested_def));
4326 return NULL;
4327 }
4328
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004329 // Only g:Func() can use a namespace.
4330 if (name_start[1] == ':' && !is_global)
4331 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004332 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004333 return NULL;
4334 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004335 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4336 return NULL;
4337
Bram Moolenaar04b12692020-05-04 23:24:44 +02004338 eap->arg = name_end;
4339 eap->getline = exarg_getline;
4340 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004341 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004342 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004343 lambda_name = get_lambda_name();
4344 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004345
Bram Moolenaar822ba242020-05-24 23:00:18 +02004346 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004347 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004348 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004349 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004350 return NULL;
4351
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004352 if (is_global)
4353 {
4354 char_u *func_name = vim_strnsave(name_start + 2,
4355 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004356
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004357 if (func_name == NULL)
4358 r = FAIL;
4359 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004360 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004361 }
4362 else
4363 {
4364 // Define a local variable for the function reference.
4365 lvar = reserve_local(cctx, name_start, name_end - name_start,
4366 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004367 if (lvar == NULL)
4368 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004369 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004370 return NULL;
4371 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4372 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004373
Bram Moolenaar61a89812020-05-07 16:58:17 +02004374 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004375 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004376}
4377
4378/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004379 * Return the length of an assignment operator, or zero if there isn't one.
4380 */
4381 int
4382assignment_len(char_u *p, int *heredoc)
4383{
4384 if (*p == '=')
4385 {
4386 if (p[1] == '<' && p[2] == '<')
4387 {
4388 *heredoc = TRUE;
4389 return 3;
4390 }
4391 return 1;
4392 }
4393 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4394 return 2;
4395 if (STRNCMP(p, "..=", 3) == 0)
4396 return 3;
4397 return 0;
4398}
4399
4400// words that cannot be used as a variable
4401static char *reserved[] = {
4402 "true",
4403 "false",
4404 NULL
4405};
4406
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004407typedef enum {
4408 dest_local,
4409 dest_option,
4410 dest_env,
4411 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004412 dest_buffer,
4413 dest_window,
4414 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004415 dest_vimvar,
4416 dest_script,
4417 dest_reg,
4418} assign_dest_T;
4419
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004421 * Generate the load instruction for "name".
4422 */
4423 static void
4424generate_loadvar(
4425 cctx_T *cctx,
4426 assign_dest_T dest,
4427 char_u *name,
4428 lvar_T *lvar,
4429 type_T *type)
4430{
4431 switch (dest)
4432 {
4433 case dest_option:
4434 // TODO: check the option exists
4435 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4436 break;
4437 case dest_global:
4438 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4439 break;
4440 case dest_buffer:
4441 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4442 break;
4443 case dest_window:
4444 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4445 break;
4446 case dest_tab:
4447 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4448 break;
4449 case dest_script:
4450 compile_load_scriptvar(cctx,
4451 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4452 break;
4453 case dest_env:
4454 // Include $ in the name here
4455 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4456 break;
4457 case dest_reg:
4458 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4459 break;
4460 case dest_vimvar:
4461 generate_LOADV(cctx, name + 2, TRUE);
4462 break;
4463 case dest_local:
4464 if (lvar->lv_from_outer)
4465 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4466 NULL, type);
4467 else
4468 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4469 break;
4470 }
4471}
4472
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004473 void
4474vim9_declare_error(char_u *name)
4475{
4476 char *scope = "";
4477
4478 switch (*name)
4479 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004480 case 'g': scope = _("global"); break;
4481 case 'b': scope = _("buffer"); break;
4482 case 'w': scope = _("window"); break;
4483 case 't': scope = _("tab"); break;
4484 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004485 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004486 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004487 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004488 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004489 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004490 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004491 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004492 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004493 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004494}
4495
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004496/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004497 * Compile declaration and assignment:
4498 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004499 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004500 * Return NULL for an error.
4501 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004502 */
4503 static char_u *
4504compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4505{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004506 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004507 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004508 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004509 char_u *ret = NULL;
4510 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004511 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004512 int scriptvar_sid = 0;
4513 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004514 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004515 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004516 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004517 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004518 int oplen = 0;
4519 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004520 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004521 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004522 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004524 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004526 // Skip over the "var" or "[var, var]" to get to any "=".
4527 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4528 if (p == NULL)
4529 return *arg == '[' ? arg : NULL;
4530
4531 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004532 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004533 // TODO: should we allow this, and figure out type inference from list
4534 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004535 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536 return NULL;
4537 }
4538
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004539 sp = p;
4540 p = skipwhite(p);
4541 op = p;
4542 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004543
4544 if (var_count > 0 && oplen == 0)
4545 // can be something like "[1, 2]->func()"
4546 return arg;
4547
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004548 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4549 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004550 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004551 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004552 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004553
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004554 if (heredoc)
4555 {
4556 list_T *l;
4557 listitem_T *li;
4558
4559 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004560 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004561 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004562 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004563
4564 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004565 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004566 {
4567 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4568 li->li_tv.vval.v_string = NULL;
4569 }
4570 generate_NEWLIST(cctx, l->lv_len);
4571 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004572 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573 list_free(l);
4574 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004575 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004577 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004578 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004579 // for "[var, var] = expr" evaluate the expression here, loop over the
4580 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004581
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004582 p = skipwhite(op + oplen);
4583 if (compile_expr0(&p, cctx) == FAIL)
4584 return NULL;
4585 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004586
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004587 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004588 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004589 type_T *stacktype;
4590
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004591 stacktype = stack->ga_len == 0 ? &t_void
4592 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004593 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004595 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004596 goto theend;
4597 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004598 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004599 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004600 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004601 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4602 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004603 }
4604 }
4605
4606 /*
4607 * Loop over variables in "[var, var] = expr".
4608 * For "var = expr" and "let var: type" this is done only once.
4609 */
4610 if (var_count > 0)
4611 var_start = skipwhite(arg + 1); // skip over the "["
4612 else
4613 var_start = arg;
4614 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4615 {
4616 char_u *var_end = skip_var_one(var_start, FALSE);
4617 size_t varlen;
4618 int new_local = FALSE;
4619 int opt_type;
4620 int opt_flags = 0;
4621 assign_dest_T dest = dest_local;
4622 int vimvaridx = -1;
4623 lvar_T *lvar = NULL;
4624 lvar_T arg_lvar;
4625 int has_type = FALSE;
4626 int has_index = FALSE;
4627 int instr_count = -1;
4628
Bram Moolenaar65821722020-08-02 18:58:54 +02004629 if (*var_start == '@')
4630 p = var_start + 2;
4631 else
4632 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02004633 // skip over the leading "&", "&l:", "&g:" and "$"
4634 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02004635 p = to_name_end(p, TRUE);
4636 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004637
4638 // "a: type" is declaring variable "a" with a type, not "a:".
4639 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4640 --var_end;
4641 if (is_decl && p == var_start + 2 && p[-1] == ':')
4642 --p;
4643
4644 varlen = p - var_start;
4645 vim_free(name);
4646 name = vim_strnsave(var_start, varlen);
4647 if (name == NULL)
4648 return NULL;
4649 if (!heredoc)
4650 type = &t_any;
4651
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004652 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004653 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004654 int declare_error = FALSE;
4655
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004656 if (*var_start == '&')
4657 {
4658 int cc;
4659 long numval;
4660
4661 dest = dest_option;
4662 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004663 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004664 emsg(_(e_const_option));
4665 goto theend;
4666 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004667 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004668 p = var_start;
4669 p = find_option_end(&p, &opt_flags);
4670 if (p == NULL)
4671 {
4672 // cannot happen?
4673 emsg(_(e_letunexp));
4674 goto theend;
4675 }
4676 cc = *p;
4677 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02004678 opt_type = get_option_value(skip_option_env_lead(var_start),
4679 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004680 *p = cc;
4681 if (opt_type == -3)
4682 {
4683 semsg(_(e_unknown_option), var_start);
4684 goto theend;
4685 }
4686 if (opt_type == -2 || opt_type == 0)
4687 type = &t_string;
4688 else
4689 type = &t_number; // both number and boolean option
4690 }
4691 else if (*var_start == '$')
4692 {
4693 dest = dest_env;
4694 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004695 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004696 }
4697 else if (*var_start == '@')
4698 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004699 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004700 {
4701 emsg_invreg(var_start[1]);
4702 goto theend;
4703 }
4704 dest = dest_reg;
4705 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004706 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004707 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004708 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004709 {
4710 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004711 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004712 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004713 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004714 {
4715 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004716 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004717 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004718 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004719 {
4720 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004721 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004722 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004723 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004724 {
4725 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004726 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004727 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004728 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004729 {
4730 typval_T *vtv;
4731 int di_flags;
4732
4733 vimvaridx = find_vim_var(name + 2, &di_flags);
4734 if (vimvaridx < 0)
4735 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004736 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004737 goto theend;
4738 }
4739 // We use the current value of "sandbox" here, is that OK?
4740 if (var_check_ro(di_flags, name, FALSE))
4741 goto theend;
4742 dest = dest_vimvar;
4743 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004744 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004745 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004746 }
4747 else
4748 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004749 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004750
4751 for (idx = 0; reserved[idx] != NULL; ++idx)
4752 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004753 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004754 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004755 goto theend;
4756 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004757
4758 lvar = lookup_local(var_start, varlen, cctx);
4759 if (lvar == NULL)
4760 {
4761 CLEAR_FIELD(arg_lvar);
4762 if (lookup_arg(var_start, varlen,
4763 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4764 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004765 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004766 if (is_decl)
4767 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004768 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004769 goto theend;
4770 }
4771 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004772 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004773 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004774 if (lvar != NULL)
4775 {
4776 if (is_decl)
4777 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004778 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004779 goto theend;
4780 }
4781 else if (lvar->lv_const)
4782 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004783 semsg(_(e_cannot_assign_to_constant), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004784 goto theend;
4785 }
4786 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004787 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004788 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004789 int script_namespace = varlen > 1
4790 && STRNCMP(var_start, "s:", 2) == 0;
4791 int script_var = (script_namespace
Bram Moolenaar53900992020-08-22 19:02:02 +02004792 ? lookup_script(var_start + 2, varlen - 2, FALSE)
4793 : lookup_script(var_start, varlen, TRUE)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004794 imported_T *import =
4795 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004796
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004797 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004798 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004799 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4800
4801 if (is_decl)
4802 {
4803 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004804 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004805 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004806 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004807 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004808 name);
4809 goto theend;
4810 }
4811 else if (cctx->ctx_ufunc->uf_script_ctx_version
4812 == SCRIPT_VERSION_VIM9
4813 && script_namespace
4814 && !script_var && import == NULL)
4815 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004816 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004817 goto theend;
4818 }
4819
4820 dest = dest_script;
4821
4822 // existing script-local variables should have a type
4823 scriptvar_sid = current_sctx.sc_sid;
4824 if (import != NULL)
4825 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004826 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004827 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02004828 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4829 rawname, TRUE);
4830 if (scriptvar_idx > 0)
4831 {
4832 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4833 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004834 ((svar_T *)si->sn_var_vals.ga_data)
4835 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004836 type = sv->sv_type;
4837 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004838 }
4839 }
4840 else if (name[1] == ':' && name[2] != NUL)
4841 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004842 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004843 goto theend;
4844 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004845 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004846 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004847 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004848 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004849 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004850 else if (check_defined(var_start, varlen, cctx) == FAIL)
4851 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004852 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004853 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004854
4855 if (declare_error)
4856 {
4857 vim9_declare_error(name);
4858 goto theend;
4859 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004860 }
4861
4862 // handle "a:name" as a name, not index "name" on "a"
4863 if (varlen > 1 || var_start[varlen] != ':')
4864 p = var_end;
4865
4866 if (dest != dest_option)
4867 {
4868 if (is_decl && *p == ':')
4869 {
4870 // parse optional type: "let var: type = expr"
4871 if (!VIM_ISWHITE(p[1]))
4872 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004873 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004874 goto theend;
4875 }
4876 p = skipwhite(p + 1);
4877 type = parse_type(&p, cctx->ctx_type_list);
4878 has_type = TRUE;
4879 }
4880 else if (lvar != NULL)
4881 type = lvar->lv_type;
4882 }
4883
4884 if (oplen == 3 && !heredoc && dest != dest_global
4885 && type->tt_type != VAR_STRING
4886 && type->tt_type != VAR_ANY)
4887 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004888 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004889 goto theend;
4890 }
4891
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004892 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004893 {
4894 if (oplen > 1 && !heredoc)
4895 {
4896 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004897 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004898 goto theend;
4899 }
4900
4901 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004902 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
4903 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004904 goto theend;
4905 lvar = reserve_local(cctx, var_start, varlen,
4906 cmdidx == CMD_const, type);
4907 if (lvar == NULL)
4908 goto theend;
4909 new_local = TRUE;
4910 }
4911
4912 member_type = type;
4913 if (var_end > var_start + varlen)
4914 {
4915 // Something follows after the variable: "var[idx]".
4916 if (is_decl)
4917 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004918 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004919 goto theend;
4920 }
4921
4922 if (var_start[varlen] == '[')
4923 {
4924 has_index = TRUE;
4925 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004926 member_type = &t_any;
4927 else
4928 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004929 }
4930 else
4931 {
4932 semsg("Not supported yet: %s", var_start);
4933 goto theend;
4934 }
4935 }
4936 else if (lvar == &arg_lvar)
4937 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004938 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004939 goto theend;
4940 }
4941
4942 if (!heredoc)
4943 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004944 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004945 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004946 if (oplen > 0 && var_count == 0)
4947 {
4948 // skip over the "=" and the expression
4949 p = skipwhite(op + oplen);
4950 compile_expr0(&p, cctx);
4951 }
4952 }
4953 else if (oplen > 0)
4954 {
4955 type_T *stacktype;
4956
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004957 // For "var = expr" evaluate the expression.
4958 if (var_count == 0)
4959 {
4960 int r;
4961
4962 // for "+=", "*=", "..=" etc. first load the current value
4963 if (*op != '=')
4964 {
4965 generate_loadvar(cctx, dest, name, lvar, type);
4966
4967 if (has_index)
4968 {
4969 // TODO: get member from list or dict
4970 emsg("Index with operation not supported yet");
4971 goto theend;
4972 }
4973 }
4974
4975 // Compile the expression. Temporarily hide the new local
4976 // variable here, it is not available to this expression.
4977 if (new_local)
4978 --cctx->ctx_locals.ga_len;
4979 instr_count = instr->ga_len;
4980 p = skipwhite(op + oplen);
4981 r = compile_expr0(&p, cctx);
4982 if (new_local)
4983 ++cctx->ctx_locals.ga_len;
4984 if (r == FAIL)
4985 goto theend;
4986 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004987 else if (semicolon && var_idx == var_count - 1)
4988 {
4989 // For "[var; var] = expr" get the rest of the list
4990 if (generate_SLICE(cctx, var_count - 1) == FAIL)
4991 goto theend;
4992 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004993 else
4994 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004995 // For "[var, var] = expr" get the "var_idx" item from the
4996 // list.
4997 if (generate_GETITEM(cctx, var_idx) == FAIL)
4998 return FAIL;
4999 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005000
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005001 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005002 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005003 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005004 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005005 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005006 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005007 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005008 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005009 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005010 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005011 }
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005012 else if ((stacktype->tt_type == VAR_FUNC
5013 || stacktype->tt_type == VAR_PARTIAL)
5014 && var_wrong_func_name(name, TRUE))
5015 {
5016 goto theend;
5017 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005018 else
5019 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005020 // An empty list or dict has a &t_void member,
5021 // for a variable that implies &t_any.
5022 if (stacktype == &t_list_empty)
5023 lvar->lv_type = &t_list_any;
5024 else if (stacktype == &t_dict_empty)
5025 lvar->lv_type = &t_dict_any;
5026 else
5027 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005028 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005029 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005030 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005031 {
5032 type_T *use_type = lvar->lv_type;
5033
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005034 // without operator type is here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005035 if (has_index)
5036 {
5037 use_type = use_type->tt_member;
5038 if (use_type == NULL)
5039 use_type = &t_void;
5040 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005041 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005042 == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005043 goto theend;
5044 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005045 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005046 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005047 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005048 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005049 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005050 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005051 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005052 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005053 goto theend;
5054 }
5055 else if (!has_type || dest == dest_option)
5056 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005057 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005058 goto theend;
5059 }
5060 else
5061 {
5062 // variables are always initialized
5063 if (ga_grow(instr, 1) == FAIL)
5064 goto theend;
5065 switch (member_type->tt_type)
5066 {
5067 case VAR_BOOL:
5068 generate_PUSHBOOL(cctx, VVAL_FALSE);
5069 break;
5070 case VAR_FLOAT:
5071#ifdef FEAT_FLOAT
5072 generate_PUSHF(cctx, 0.0);
5073#endif
5074 break;
5075 case VAR_STRING:
5076 generate_PUSHS(cctx, NULL);
5077 break;
5078 case VAR_BLOB:
5079 generate_PUSHBLOB(cctx, NULL);
5080 break;
5081 case VAR_FUNC:
5082 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5083 break;
5084 case VAR_LIST:
5085 generate_NEWLIST(cctx, 0);
5086 break;
5087 case VAR_DICT:
5088 generate_NEWDICT(cctx, 0);
5089 break;
5090 case VAR_JOB:
5091 generate_PUSHJOB(cctx, NULL);
5092 break;
5093 case VAR_CHANNEL:
5094 generate_PUSHCHANNEL(cctx, NULL);
5095 break;
5096 case VAR_NUMBER:
5097 case VAR_UNKNOWN:
5098 case VAR_ANY:
5099 case VAR_PARTIAL:
5100 case VAR_VOID:
5101 case VAR_SPECIAL: // cannot happen
5102 generate_PUSHNR(cctx, 0);
5103 break;
5104 }
5105 }
5106 if (var_count == 0)
5107 end = p;
5108 }
5109
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005110 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005111 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005112 break;
5113
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005114 if (oplen > 0 && *op != '=')
5115 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005116 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005117 type_T *stacktype;
5118
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005119 if (*op == '.')
5120 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005121 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005122 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005123 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005124 if (
5125#ifdef FEAT_FLOAT
5126 // If variable is float operation with number is OK.
5127 !(expected == &t_float && stacktype == &t_number) &&
5128#endif
5129 need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005130 goto theend;
5131
5132 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005133 {
5134 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5135 goto theend;
5136 }
5137 else if (*op == '+')
5138 {
5139 if (generate_add_instr(cctx,
5140 operator_type(member_type, stacktype),
5141 member_type, stacktype) == FAIL)
5142 goto theend;
5143 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005144 else if (generate_two_op(cctx, op) == FAIL)
5145 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005146 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005147
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005148 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005149 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005150 int r;
5151
5152 // Compile the "idx" in "var[idx]".
5153 if (new_local)
5154 --cctx->ctx_locals.ga_len;
5155 p = skipwhite(var_start + varlen + 1);
5156 r = compile_expr0(&p, cctx);
5157 if (new_local)
5158 ++cctx->ctx_locals.ga_len;
5159 if (r == FAIL)
5160 goto theend;
5161 if (*skipwhite(p) != ']')
5162 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005163 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005164 emsg(_(e_missbrac));
5165 goto theend;
5166 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005167 if (type == &t_any)
5168 {
5169 type_T *idx_type = ((type_T **)stack->ga_data)[
5170 stack->ga_len - 1];
5171 // Index on variable of unknown type: guess the type from the
5172 // index type: number is dict, otherwise dict.
5173 // TODO: should do the assignment at runtime
5174 if (idx_type->tt_type == VAR_NUMBER)
5175 type = &t_list_any;
5176 else
5177 type = &t_dict_any;
5178 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005179 if (type->tt_type == VAR_DICT
5180 && may_generate_2STRING(-1, cctx) == FAIL)
5181 goto theend;
5182 if (type->tt_type == VAR_LIST
5183 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005184 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005185 {
5186 emsg(_(e_number_exp));
5187 goto theend;
5188 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005189
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005190 // Load the dict or list. On the stack we then have:
5191 // - value
5192 // - index
5193 // - variable
5194 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005195
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005196 if (type->tt_type == VAR_LIST)
5197 {
5198 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5199 return FAIL;
5200 }
5201 else if (type->tt_type == VAR_DICT)
5202 {
5203 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5204 return FAIL;
5205 }
5206 else
5207 {
5208 emsg(_(e_listreq));
5209 goto theend;
5210 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005211 }
5212 else
5213 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005214 switch (dest)
5215 {
5216 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005217 generate_STOREOPT(cctx, skip_option_env_lead(name),
5218 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005219 break;
5220 case dest_global:
5221 // include g: with the name, easier to execute that way
5222 generate_STORE(cctx, ISN_STOREG, 0, name);
5223 break;
5224 case dest_buffer:
5225 // include b: with the name, easier to execute that way
5226 generate_STORE(cctx, ISN_STOREB, 0, name);
5227 break;
5228 case dest_window:
5229 // include w: with the name, easier to execute that way
5230 generate_STORE(cctx, ISN_STOREW, 0, name);
5231 break;
5232 case dest_tab:
5233 // include t: with the name, easier to execute that way
5234 generate_STORE(cctx, ISN_STORET, 0, name);
5235 break;
5236 case dest_env:
5237 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5238 break;
5239 case dest_reg:
5240 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5241 break;
5242 case dest_vimvar:
5243 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5244 break;
5245 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005246 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005247 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005248 {
5249 char_u *name_s = name;
5250
5251 // Include s: in the name for store_var()
5252 if (name[1] != ':')
5253 {
5254 int len = (int)STRLEN(name) + 3;
5255
5256 name_s = alloc(len);
5257 if (name_s == NULL)
5258 name_s = name;
5259 else
5260 vim_snprintf((char *)name_s, len,
5261 "s:%s", name);
5262 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005263 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5264 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005265 if (name_s != name)
5266 vim_free(name_s);
5267 }
5268 else
5269 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005270 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005271 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005272 break;
5273 case dest_local:
5274 if (lvar != NULL)
5275 {
5276 isn_T *isn = ((isn_T *)instr->ga_data)
5277 + instr->ga_len - 1;
5278
5279 // optimization: turn "var = 123" from ISN_PUSHNR +
5280 // ISN_STORE into ISN_STORENR
5281 if (!lvar->lv_from_outer
5282 && instr->ga_len == instr_count + 1
5283 && isn->isn_type == ISN_PUSHNR)
5284 {
5285 varnumber_T val = isn->isn_arg.number;
5286
5287 isn->isn_type = ISN_STORENR;
5288 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5289 isn->isn_arg.storenr.stnr_val = val;
5290 if (stack->ga_len > 0)
5291 --stack->ga_len;
5292 }
5293 else if (lvar->lv_from_outer)
5294 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005295 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005296 else
5297 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5298 }
5299 break;
5300 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005301 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005302
5303 if (var_idx + 1 < var_count)
5304 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005305 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005306
5307 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005308 if (var_count > 0 && !semicolon)
5309 {
5310 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5311 goto theend;
5312 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005313
Bram Moolenaarb2097502020-07-19 17:17:02 +02005314 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005315
5316theend:
5317 vim_free(name);
5318 return ret;
5319}
5320
5321/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005322 * Check if "name" can be "unlet".
5323 */
5324 int
5325check_vim9_unlet(char_u *name)
5326{
5327 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5328 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005329 // "unlet s:var" is allowed in legacy script.
5330 if (*name == 's' && !script_is_vim9())
5331 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005332 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005333 return FAIL;
5334 }
5335 return OK;
5336}
5337
5338/*
5339 * Callback passed to ex_unletlock().
5340 */
5341 static int
5342compile_unlet(
5343 lval_T *lvp,
5344 char_u *name_end,
5345 exarg_T *eap,
5346 int deep UNUSED,
5347 void *coookie)
5348{
5349 cctx_T *cctx = coookie;
5350
5351 if (lvp->ll_tv == NULL)
5352 {
5353 char_u *p = lvp->ll_name;
5354 int cc = *name_end;
5355 int ret = OK;
5356
5357 // Normal name. Only supports g:, w:, t: and b: namespaces.
5358 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005359 if (*p == '$')
5360 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5361 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005362 ret = FAIL;
5363 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005364 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005365
5366 *name_end = cc;
5367 return ret;
5368 }
5369
5370 // TODO: unlet {list}[idx]
5371 // TODO: unlet {dict}[key]
5372 emsg("Sorry, :unlet not fully implemented yet");
5373 return FAIL;
5374}
5375
5376/*
5377 * compile "unlet var", "lock var" and "unlock var"
5378 * "arg" points to "var".
5379 */
5380 static char_u *
5381compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5382{
5383 char_u *p = arg;
5384
5385 if (eap->cmdidx != CMD_unlet)
5386 {
5387 emsg("Sorry, :lock and unlock not implemented yet");
5388 return NULL;
5389 }
5390
5391 if (*p == '!')
5392 {
5393 p = skipwhite(p + 1);
5394 eap->forceit = TRUE;
5395 }
5396
5397 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5398 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5399}
5400
5401/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005402 * Compile an :import command.
5403 */
5404 static char_u *
5405compile_import(char_u *arg, cctx_T *cctx)
5406{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005407 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005408}
5409
5410/*
5411 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5412 */
5413 static int
5414compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5415{
5416 garray_T *instr = &cctx->ctx_instr;
5417 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5418
5419 if (endlabel == NULL)
5420 return FAIL;
5421 endlabel->el_next = *el;
5422 *el = endlabel;
5423 endlabel->el_end_label = instr->ga_len;
5424
5425 generate_JUMP(cctx, when, 0);
5426 return OK;
5427}
5428
5429 static void
5430compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5431{
5432 garray_T *instr = &cctx->ctx_instr;
5433
5434 while (*el != NULL)
5435 {
5436 endlabel_T *cur = (*el);
5437 isn_T *isn;
5438
5439 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5440 isn->isn_arg.jump.jump_where = instr->ga_len;
5441 *el = cur->el_next;
5442 vim_free(cur);
5443 }
5444}
5445
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005446 static void
5447compile_free_jump_to_end(endlabel_T **el)
5448{
5449 while (*el != NULL)
5450 {
5451 endlabel_T *cur = (*el);
5452
5453 *el = cur->el_next;
5454 vim_free(cur);
5455 }
5456}
5457
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005458/*
5459 * Create a new scope and set up the generic items.
5460 */
5461 static scope_T *
5462new_scope(cctx_T *cctx, scopetype_T type)
5463{
5464 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5465
5466 if (scope == NULL)
5467 return NULL;
5468 scope->se_outer = cctx->ctx_scope;
5469 cctx->ctx_scope = scope;
5470 scope->se_type = type;
5471 scope->se_local_count = cctx->ctx_locals.ga_len;
5472 return scope;
5473}
5474
5475/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005476 * Free the current scope and go back to the outer scope.
5477 */
5478 static void
5479drop_scope(cctx_T *cctx)
5480{
5481 scope_T *scope = cctx->ctx_scope;
5482
5483 if (scope == NULL)
5484 {
5485 iemsg("calling drop_scope() without a scope");
5486 return;
5487 }
5488 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005489 switch (scope->se_type)
5490 {
5491 case IF_SCOPE:
5492 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5493 case FOR_SCOPE:
5494 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5495 case WHILE_SCOPE:
5496 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5497 case TRY_SCOPE:
5498 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5499 case NO_SCOPE:
5500 case BLOCK_SCOPE:
5501 break;
5502 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005503 vim_free(scope);
5504}
5505
5506/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005507 * compile "if expr"
5508 *
5509 * "if expr" Produces instructions:
5510 * EVAL expr Push result of "expr"
5511 * JUMP_IF_FALSE end
5512 * ... body ...
5513 * end:
5514 *
5515 * "if expr | else" Produces instructions:
5516 * EVAL expr Push result of "expr"
5517 * JUMP_IF_FALSE else
5518 * ... body ...
5519 * JUMP_ALWAYS end
5520 * else:
5521 * ... body ...
5522 * end:
5523 *
5524 * "if expr1 | elseif expr2 | else" Produces instructions:
5525 * EVAL expr Push result of "expr"
5526 * JUMP_IF_FALSE elseif
5527 * ... body ...
5528 * JUMP_ALWAYS end
5529 * elseif:
5530 * EVAL expr Push result of "expr"
5531 * JUMP_IF_FALSE else
5532 * ... body ...
5533 * JUMP_ALWAYS end
5534 * else:
5535 * ... body ...
5536 * end:
5537 */
5538 static char_u *
5539compile_if(char_u *arg, cctx_T *cctx)
5540{
5541 char_u *p = arg;
5542 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005543 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005544 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005545 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005546 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005547
Bram Moolenaara5565e42020-05-09 15:44:01 +02005548 CLEAR_FIELD(ppconst);
5549 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005550 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005551 clear_ppconst(&ppconst);
5552 return NULL;
5553 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005554 if (cctx->ctx_skip == SKIP_YES)
5555 clear_ppconst(&ppconst);
5556 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005557 {
5558 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005559 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005560 clear_ppconst(&ppconst);
5561 }
5562 else
5563 {
5564 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005565 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005566 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005567 return NULL;
5568 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005569
5570 scope = new_scope(cctx, IF_SCOPE);
5571 if (scope == NULL)
5572 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005573 scope->se_skip_save = skip_save;
5574 // "is_had_return" will be reset if any block does not end in :return
5575 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005576
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005577 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005578 {
5579 // "where" is set when ":elseif", "else" or ":endif" is found
5580 scope->se_u.se_if.is_if_label = instr->ga_len;
5581 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5582 }
5583 else
5584 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005585
5586 return p;
5587}
5588
5589 static char_u *
5590compile_elseif(char_u *arg, cctx_T *cctx)
5591{
5592 char_u *p = arg;
5593 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005594 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005595 isn_T *isn;
5596 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005597 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02005598 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005599
5600 if (scope == NULL || scope->se_type != IF_SCOPE)
5601 {
5602 emsg(_(e_elseif_without_if));
5603 return NULL;
5604 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005605 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005606 if (!cctx->ctx_had_return)
5607 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005608
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005609 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005610 {
5611 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005612 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005613 return NULL;
5614 // previous "if" or "elseif" jumps here
5615 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5616 isn->isn_arg.jump.jump_where = instr->ga_len;
5617 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005618
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005619 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005620 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02005621 if (cctx->ctx_skip == SKIP_YES)
5622 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005623 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005624 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005625 clear_ppconst(&ppconst);
5626 return NULL;
5627 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02005628 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005629 if (scope->se_skip_save == SKIP_YES)
5630 clear_ppconst(&ppconst);
5631 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005632 {
5633 // The expression results in a constant.
5634 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005635 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005636 clear_ppconst(&ppconst);
5637 scope->se_u.se_if.is_if_label = -1;
5638 }
5639 else
5640 {
5641 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005642 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005643 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005644 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005645
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005646 // "where" is set when ":elseif", "else" or ":endif" is found
5647 scope->se_u.se_if.is_if_label = instr->ga_len;
5648 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5649 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005650
5651 return p;
5652}
5653
5654 static char_u *
5655compile_else(char_u *arg, cctx_T *cctx)
5656{
5657 char_u *p = arg;
5658 garray_T *instr = &cctx->ctx_instr;
5659 isn_T *isn;
5660 scope_T *scope = cctx->ctx_scope;
5661
5662 if (scope == NULL || scope->se_type != IF_SCOPE)
5663 {
5664 emsg(_(e_else_without_if));
5665 return NULL;
5666 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005667 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005668 if (!cctx->ctx_had_return)
5669 scope->se_u.se_if.is_had_return = FALSE;
5670 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005671
Bram Moolenaarefd88552020-06-18 20:50:10 +02005672 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005673 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005674 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005675 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005676 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005677 if (!cctx->ctx_had_return
5678 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5679 JUMP_ALWAYS, cctx) == FAIL)
5680 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005681 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005682
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005683 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005684 {
5685 if (scope->se_u.se_if.is_if_label >= 0)
5686 {
5687 // previous "if" or "elseif" jumps here
5688 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5689 isn->isn_arg.jump.jump_where = instr->ga_len;
5690 scope->se_u.se_if.is_if_label = -1;
5691 }
5692 }
5693
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005694 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005695 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5696 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005697
5698 return p;
5699}
5700
5701 static char_u *
5702compile_endif(char_u *arg, cctx_T *cctx)
5703{
5704 scope_T *scope = cctx->ctx_scope;
5705 ifscope_T *ifscope;
5706 garray_T *instr = &cctx->ctx_instr;
5707 isn_T *isn;
5708
5709 if (scope == NULL || scope->se_type != IF_SCOPE)
5710 {
5711 emsg(_(e_endif_without_if));
5712 return NULL;
5713 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005714 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005715 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005716 if (!cctx->ctx_had_return)
5717 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005718
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005719 if (scope->se_u.se_if.is_if_label >= 0)
5720 {
5721 // previous "if" or "elseif" jumps here
5722 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5723 isn->isn_arg.jump.jump_where = instr->ga_len;
5724 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005725 // Fill in the "end" label in jumps at the end of the blocks.
5726 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005727 cctx->ctx_skip = scope->se_skip_save;
5728
5729 // If all the blocks end in :return and there is an :else then the
5730 // had_return flag is set.
5731 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005732
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005733 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005734 return arg;
5735}
5736
5737/*
5738 * compile "for var in expr"
5739 *
5740 * Produces instructions:
5741 * PUSHNR -1
5742 * STORE loop-idx Set index to -1
5743 * EVAL expr Push result of "expr"
5744 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5745 * - if beyond end, jump to "end"
5746 * - otherwise get item from list and push it
5747 * STORE var Store item in "var"
5748 * ... body ...
5749 * JUMP top Jump back to repeat
5750 * end: DROP Drop the result of "expr"
5751 *
5752 */
5753 static char_u *
5754compile_for(char_u *arg, cctx_T *cctx)
5755{
5756 char_u *p;
5757 size_t varlen;
5758 garray_T *instr = &cctx->ctx_instr;
5759 garray_T *stack = &cctx->ctx_type_stack;
5760 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005761 lvar_T *loop_lvar; // loop iteration variable
5762 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005763 type_T *vartype;
5764
5765 // TODO: list of variables: "for [key, value] in dict"
5766 // parse "var"
5767 for (p = arg; eval_isnamec1(*p); ++p)
5768 ;
5769 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005770 var_lvar = lookup_local(arg, varlen, cctx);
5771 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005772 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005773 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005774 return NULL;
5775 }
5776
5777 // consume "in"
5778 p = skipwhite(p);
5779 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5780 {
5781 emsg(_(e_missing_in));
5782 return NULL;
5783 }
5784 p = skipwhite(p + 2);
5785
5786
5787 scope = new_scope(cctx, FOR_SCOPE);
5788 if (scope == NULL)
5789 return NULL;
5790
5791 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005792 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5793 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005794 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005795 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005796 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005797 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005798 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005799
5800 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005801 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5802 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005803 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005804 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005805 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005806 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005807 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005808
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005809 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005810
5811 // compile "expr", it remains on the stack until "endfor"
5812 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005813 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005814 {
5815 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005816 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005817 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005818
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005819 // Now that we know the type of "var", check that it is a list, now or at
5820 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005821 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005822 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005823 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005824 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005825 return NULL;
5826 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005827 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005828 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005829
5830 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005831 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005832
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005833 generate_FOR(cctx, loop_lvar->lv_idx);
5834 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005835
5836 return arg;
5837}
5838
5839/*
5840 * compile "endfor"
5841 */
5842 static char_u *
5843compile_endfor(char_u *arg, cctx_T *cctx)
5844{
5845 garray_T *instr = &cctx->ctx_instr;
5846 scope_T *scope = cctx->ctx_scope;
5847 forscope_T *forscope;
5848 isn_T *isn;
5849
5850 if (scope == NULL || scope->se_type != FOR_SCOPE)
5851 {
5852 emsg(_(e_for));
5853 return NULL;
5854 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005855 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005856 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005857 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005858
5859 // At end of ":for" scope jump back to the FOR instruction.
5860 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5861
5862 // Fill in the "end" label in the FOR statement so it can jump here
5863 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5864 isn->isn_arg.forloop.for_end = instr->ga_len;
5865
5866 // Fill in the "end" label any BREAK statements
5867 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5868
5869 // Below the ":for" scope drop the "expr" list from the stack.
5870 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5871 return NULL;
5872
5873 vim_free(scope);
5874
5875 return arg;
5876}
5877
5878/*
5879 * compile "while expr"
5880 *
5881 * Produces instructions:
5882 * top: EVAL expr Push result of "expr"
5883 * JUMP_IF_FALSE end jump if false
5884 * ... body ...
5885 * JUMP top Jump back to repeat
5886 * end:
5887 *
5888 */
5889 static char_u *
5890compile_while(char_u *arg, cctx_T *cctx)
5891{
5892 char_u *p = arg;
5893 garray_T *instr = &cctx->ctx_instr;
5894 scope_T *scope;
5895
5896 scope = new_scope(cctx, WHILE_SCOPE);
5897 if (scope == NULL)
5898 return NULL;
5899
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005900 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005901
5902 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005903 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005904 return NULL;
5905
5906 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005907 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005908 JUMP_IF_FALSE, cctx) == FAIL)
5909 return FAIL;
5910
5911 return p;
5912}
5913
5914/*
5915 * compile "endwhile"
5916 */
5917 static char_u *
5918compile_endwhile(char_u *arg, cctx_T *cctx)
5919{
5920 scope_T *scope = cctx->ctx_scope;
5921
5922 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5923 {
5924 emsg(_(e_while));
5925 return NULL;
5926 }
5927 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005928 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005929
5930 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005931 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005932
5933 // Fill in the "end" label in the WHILE statement so it can jump here.
5934 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005935 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005936
5937 vim_free(scope);
5938
5939 return arg;
5940}
5941
5942/*
5943 * compile "continue"
5944 */
5945 static char_u *
5946compile_continue(char_u *arg, cctx_T *cctx)
5947{
5948 scope_T *scope = cctx->ctx_scope;
5949
5950 for (;;)
5951 {
5952 if (scope == NULL)
5953 {
5954 emsg(_(e_continue));
5955 return NULL;
5956 }
5957 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5958 break;
5959 scope = scope->se_outer;
5960 }
5961
5962 // Jump back to the FOR or WHILE instruction.
5963 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005964 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5965 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005966 return arg;
5967}
5968
5969/*
5970 * compile "break"
5971 */
5972 static char_u *
5973compile_break(char_u *arg, cctx_T *cctx)
5974{
5975 scope_T *scope = cctx->ctx_scope;
5976 endlabel_T **el;
5977
5978 for (;;)
5979 {
5980 if (scope == NULL)
5981 {
5982 emsg(_(e_break));
5983 return NULL;
5984 }
5985 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5986 break;
5987 scope = scope->se_outer;
5988 }
5989
5990 // Jump to the end of the FOR or WHILE loop.
5991 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005992 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005993 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005994 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005995 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5996 return FAIL;
5997
5998 return arg;
5999}
6000
6001/*
6002 * compile "{" start of block
6003 */
6004 static char_u *
6005compile_block(char_u *arg, cctx_T *cctx)
6006{
6007 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6008 return NULL;
6009 return skipwhite(arg + 1);
6010}
6011
6012/*
6013 * compile end of block: drop one scope
6014 */
6015 static void
6016compile_endblock(cctx_T *cctx)
6017{
6018 scope_T *scope = cctx->ctx_scope;
6019
6020 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006021 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006022 vim_free(scope);
6023}
6024
6025/*
6026 * compile "try"
6027 * Creates a new scope for the try-endtry, pointing to the first catch and
6028 * finally.
6029 * Creates another scope for the "try" block itself.
6030 * TRY instruction sets up exception handling at runtime.
6031 *
6032 * "try"
6033 * TRY -> catch1, -> finally push trystack entry
6034 * ... try block
6035 * "throw {exception}"
6036 * EVAL {exception}
6037 * THROW create exception
6038 * ... try block
6039 * " catch {expr}"
6040 * JUMP -> finally
6041 * catch1: PUSH exeception
6042 * EVAL {expr}
6043 * MATCH
6044 * JUMP nomatch -> catch2
6045 * CATCH remove exception
6046 * ... catch block
6047 * " catch"
6048 * JUMP -> finally
6049 * catch2: CATCH remove exception
6050 * ... catch block
6051 * " finally"
6052 * finally:
6053 * ... finally block
6054 * " endtry"
6055 * ENDTRY pop trystack entry, may rethrow
6056 */
6057 static char_u *
6058compile_try(char_u *arg, cctx_T *cctx)
6059{
6060 garray_T *instr = &cctx->ctx_instr;
6061 scope_T *try_scope;
6062 scope_T *scope;
6063
6064 // scope that holds the jumps that go to catch/finally/endtry
6065 try_scope = new_scope(cctx, TRY_SCOPE);
6066 if (try_scope == NULL)
6067 return NULL;
6068
6069 // "catch" is set when the first ":catch" is found.
6070 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006071 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006072 if (generate_instr(cctx, ISN_TRY) == NULL)
6073 return NULL;
6074
6075 // scope for the try block itself
6076 scope = new_scope(cctx, BLOCK_SCOPE);
6077 if (scope == NULL)
6078 return NULL;
6079
6080 return arg;
6081}
6082
6083/*
6084 * compile "catch {expr}"
6085 */
6086 static char_u *
6087compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6088{
6089 scope_T *scope = cctx->ctx_scope;
6090 garray_T *instr = &cctx->ctx_instr;
6091 char_u *p;
6092 isn_T *isn;
6093
6094 // end block scope from :try or :catch
6095 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6096 compile_endblock(cctx);
6097 scope = cctx->ctx_scope;
6098
6099 // Error if not in a :try scope
6100 if (scope == NULL || scope->se_type != TRY_SCOPE)
6101 {
6102 emsg(_(e_catch));
6103 return NULL;
6104 }
6105
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006106 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006107 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006108 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006109 return NULL;
6110 }
6111
6112 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006113 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006114 JUMP_ALWAYS, cctx) == FAIL)
6115 return NULL;
6116
6117 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006118 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006119 if (isn->isn_arg.try.try_catch == 0)
6120 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006121 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006122 {
6123 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006124 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006125 isn->isn_arg.jump.jump_where = instr->ga_len;
6126 }
6127
6128 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006129 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006130 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006131 scope->se_u.se_try.ts_caught_all = TRUE;
6132 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006133 }
6134 else
6135 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006136 char_u *end;
6137 char_u *pat;
6138 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006139 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006140 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006142 // Push v:exception, push {expr} and MATCH
6143 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6144
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006145 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006146 if (*end != *p)
6147 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006148 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006149 vim_free(tofree);
6150 return FAIL;
6151 }
6152 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006153 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006154 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006155 len = (int)(end - tofree);
6156 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006157 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006158 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006159 if (pat == NULL)
6160 return FAIL;
6161 if (generate_PUSHS(cctx, pat) == FAIL)
6162 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006163
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006164 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6165 return NULL;
6166
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006167 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006168 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6169 return NULL;
6170 }
6171
6172 if (generate_instr(cctx, ISN_CATCH) == NULL)
6173 return NULL;
6174
6175 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6176 return NULL;
6177 return p;
6178}
6179
6180 static char_u *
6181compile_finally(char_u *arg, cctx_T *cctx)
6182{
6183 scope_T *scope = cctx->ctx_scope;
6184 garray_T *instr = &cctx->ctx_instr;
6185 isn_T *isn;
6186
6187 // end block scope from :try or :catch
6188 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6189 compile_endblock(cctx);
6190 scope = cctx->ctx_scope;
6191
6192 // Error if not in a :try scope
6193 if (scope == NULL || scope->se_type != TRY_SCOPE)
6194 {
6195 emsg(_(e_finally));
6196 return NULL;
6197 }
6198
6199 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006200 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006201 if (isn->isn_arg.try.try_finally != 0)
6202 {
6203 emsg(_(e_finally_dup));
6204 return NULL;
6205 }
6206
6207 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006208 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006209
Bram Moolenaar585fea72020-04-02 22:33:21 +02006210 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006211 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006212 {
6213 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006214 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006215 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006216 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006217 }
6218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006219 // TODO: set index in ts_finally_label jumps
6220
6221 return arg;
6222}
6223
6224 static char_u *
6225compile_endtry(char_u *arg, cctx_T *cctx)
6226{
6227 scope_T *scope = cctx->ctx_scope;
6228 garray_T *instr = &cctx->ctx_instr;
6229 isn_T *isn;
6230
6231 // end block scope from :catch or :finally
6232 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6233 compile_endblock(cctx);
6234 scope = cctx->ctx_scope;
6235
6236 // Error if not in a :try scope
6237 if (scope == NULL || scope->se_type != TRY_SCOPE)
6238 {
6239 if (scope == NULL)
6240 emsg(_(e_no_endtry));
6241 else if (scope->se_type == WHILE_SCOPE)
6242 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006243 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006244 emsg(_(e_endfor));
6245 else
6246 emsg(_(e_endif));
6247 return NULL;
6248 }
6249
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006250 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006251 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6252 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006253 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006254 return NULL;
6255 }
6256
6257 // Fill in the "end" label in jumps at the end of the blocks, if not done
6258 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006259 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006260
6261 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006262 if (isn->isn_arg.try.try_catch == 0)
6263 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006264 if (isn->isn_arg.try.try_finally == 0)
6265 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006266
6267 if (scope->se_u.se_try.ts_catch_label != 0)
6268 {
6269 // Last catch without match jumps here
6270 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6271 isn->isn_arg.jump.jump_where = instr->ga_len;
6272 }
6273
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006274 compile_endblock(cctx);
6275
6276 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6277 return NULL;
6278 return arg;
6279}
6280
6281/*
6282 * compile "throw {expr}"
6283 */
6284 static char_u *
6285compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6286{
6287 char_u *p = skipwhite(arg);
6288
Bram Moolenaara5565e42020-05-09 15:44:01 +02006289 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006290 return NULL;
6291 if (may_generate_2STRING(-1, cctx) == FAIL)
6292 return NULL;
6293 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6294 return NULL;
6295
6296 return p;
6297}
6298
6299/*
6300 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006301 * compile "echomsg expr"
6302 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006303 * compile "execute expr"
6304 */
6305 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006306compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006307{
6308 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006309 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006310 int count = 0;
6311
6312 for (;;)
6313 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006314 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006315 return NULL;
6316 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006317 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006318 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006319 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006320 break;
6321 }
6322
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006323 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6324 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6325 else if (cmdidx == CMD_execute)
6326 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6327 else if (cmdidx == CMD_echomsg)
6328 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6329 else
6330 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006331 return p;
6332}
6333
6334/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006335 * :put r
6336 * :put ={expr}
6337 */
6338 static char_u *
6339compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6340{
6341 char_u *line = arg;
6342 linenr_T lnum;
6343 char *errormsg;
6344 int above = FALSE;
6345
6346 if (*arg == '!')
6347 {
6348 above = TRUE;
6349 line = skipwhite(arg + 1);
6350 }
6351 eap->regname = *line;
6352
6353 if (eap->regname == '=')
6354 {
6355 char_u *p = line + 1;
6356
6357 if (compile_expr0(&p, cctx) == FAIL)
6358 return NULL;
6359 line = p;
6360 }
6361 else if (eap->regname != NUL)
6362 ++line;
6363
6364 // TODO: if the range is something like "$" need to evaluate at runtime
6365 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6366 return NULL;
6367 if (eap->addr_count == 0)
6368 lnum = -1;
6369 else
6370 lnum = eap->line2;
6371 if (above)
6372 --lnum;
6373
6374 generate_PUT(cctx, eap->regname, lnum);
6375 return line;
6376}
6377
6378/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006379 * A command that is not compiled, execute with legacy code.
6380 */
6381 static char_u *
6382compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6383{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006384 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006385 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006386 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006387
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006388 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006389 goto theend;
6390
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006391 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006392 {
6393 long argt = excmd_get_argt(eap->cmdidx);
6394 int usefilter = FALSE;
6395
6396 has_expr = argt & (EX_XFILE | EX_EXPAND);
6397
6398 // If the command can be followed by a bar, find the bar and truncate
6399 // it, so that the following command can be compiled.
6400 // The '|' is overwritten with a NUL, it is put back below.
6401 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6402 && *eap->arg == '!')
6403 // :w !filter or :r !filter or :r! filter
6404 usefilter = TRUE;
6405 if ((argt & EX_TRLBAR) && !usefilter)
6406 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006407 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006408 separate_nextcmd(eap);
6409 if (eap->nextcmd != NULL)
6410 nextcmd = eap->nextcmd;
6411 }
6412 }
6413
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006414 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6415 {
6416 // expand filename in "syntax include [@group] filename"
6417 has_expr = TRUE;
6418 eap->arg = skipwhite(eap->arg + 7);
6419 if (*eap->arg == '@')
6420 eap->arg = skiptowhite(eap->arg);
6421 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006422
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006423 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006424 {
6425 int count = 0;
6426 char_u *start = skipwhite(line);
6427
6428 // :cmd xxx`=expr1`yyy`=expr2`zzz
6429 // PUSHS ":cmd xxx"
6430 // eval expr1
6431 // PUSHS "yyy"
6432 // eval expr2
6433 // PUSHS "zzz"
6434 // EXECCONCAT 5
6435 for (;;)
6436 {
6437 if (p > start)
6438 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006439 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006440 ++count;
6441 }
6442 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006443 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006444 return NULL;
6445 may_generate_2STRING(-1, cctx);
6446 ++count;
6447 p = skipwhite(p);
6448 if (*p != '`')
6449 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006450 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006451 return NULL;
6452 }
6453 start = p + 1;
6454
6455 p = (char_u *)strstr((char *)start, "`=");
6456 if (p == NULL)
6457 {
6458 if (*skipwhite(start) != NUL)
6459 {
6460 generate_PUSHS(cctx, vim_strsave(start));
6461 ++count;
6462 }
6463 break;
6464 }
6465 }
6466 generate_EXECCONCAT(cctx, count);
6467 }
6468 else
6469 generate_EXEC(cctx, line);
6470
6471theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006472 if (*nextcmd != NUL)
6473 {
6474 // the parser expects a pointer to the bar, put it back
6475 --nextcmd;
6476 *nextcmd = '|';
6477 }
6478
6479 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006480}
6481
6482/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006483 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006484 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006485 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006486 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006487add_def_function(ufunc_T *ufunc)
6488{
6489 dfunc_T *dfunc;
6490
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006491 if (def_functions.ga_len == 0)
6492 {
6493 // The first position is not used, so that a zero uf_dfunc_idx means it
6494 // wasn't set.
6495 if (ga_grow(&def_functions, 1) == FAIL)
6496 return FAIL;
6497 ++def_functions.ga_len;
6498 }
6499
Bram Moolenaar09689a02020-05-09 22:50:08 +02006500 // Add the function to "def_functions".
6501 if (ga_grow(&def_functions, 1) == FAIL)
6502 return FAIL;
6503 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6504 CLEAR_POINTER(dfunc);
6505 dfunc->df_idx = def_functions.ga_len;
6506 ufunc->uf_dfunc_idx = dfunc->df_idx;
6507 dfunc->df_ufunc = ufunc;
6508 ++def_functions.ga_len;
6509 return OK;
6510}
6511
6512/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006513 * After ex_function() has collected all the function lines: parse and compile
6514 * the lines into instructions.
6515 * Adds the function to "def_functions".
6516 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6517 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006518 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006519 * This can be used recursively through compile_lambda(), which may reallocate
6520 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006521 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006522 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006523 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006524compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006525{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006526 char_u *line = NULL;
6527 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006528 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006529 cctx_T cctx;
6530 garray_T *instr;
6531 int called_emsg_before = called_emsg;
6532 int ret = FAIL;
6533 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006534 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006535 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006536 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006537
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006538 // When using a function that was compiled before: Free old instructions.
6539 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006540 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006541 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006542 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6543 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006544 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006545 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006546 else
6547 {
6548 if (add_def_function(ufunc) == FAIL)
6549 return FAIL;
6550 new_def_function = TRUE;
6551 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006552
Bram Moolenaar985116a2020-07-12 17:31:09 +02006553 ufunc->uf_def_status = UF_COMPILING;
6554
Bram Moolenaara80faa82020-04-12 19:37:17 +02006555 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006556 cctx.ctx_ufunc = ufunc;
6557 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006558 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006559 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6560 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6561 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6562 cctx.ctx_type_list = &ufunc->uf_type_list;
6563 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6564 instr = &cctx.ctx_instr;
6565
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006566 // Set the context to the function, it may be compiled when called from
6567 // another script. Set the script version to the most modern one.
6568 // The line number will be set in next_line_from_context().
6569 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006570 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6571
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006572 // Make sure error messages are OK.
6573 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6574 if (do_estack_push)
6575 estack_push_ufunc(ufunc, 1);
6576
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006577 if (ufunc->uf_def_args.ga_len > 0)
6578 {
6579 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006580 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006581 int i;
6582 char_u *arg;
6583 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006584 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006585
6586 // Produce instructions for the default values of optional arguments.
6587 // Store the instruction index in uf_def_arg_idx[] so that we know
6588 // where to start when the function is called, depending on the number
6589 // of arguments.
6590 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6591 if (ufunc->uf_def_arg_idx == NULL)
6592 goto erret;
6593 for (i = 0; i < count; ++i)
6594 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006595 garray_T *stack = &cctx.ctx_type_stack;
6596 type_T *val_type;
6597 int arg_idx = first_def_arg + i;
6598
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006599 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6600 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006601 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006602 goto erret;
6603
6604 // If no type specified use the type of the default value.
6605 // Otherwise check that the default value type matches the
6606 // specified type.
6607 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6608 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006609 {
6610 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006611 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006612 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02006613 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
6614 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006615 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006616
6617 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006618 goto erret;
6619 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006620 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006621
6622 if (did_set_arg_type)
6623 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006624 }
6625
6626 /*
6627 * Loop over all the lines of the function and generate instructions.
6628 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006629 for (;;)
6630 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006631 exarg_T ea;
6632 cmdmod_T save_cmdmod;
6633 int starts_with_colon = FALSE;
6634 char_u *cmd;
6635 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006636
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006637 // Bail out on the first error to avoid a flood of errors and report
6638 // the right line number when inside try/catch.
6639 if (emsg_before != called_emsg)
6640 goto erret;
6641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006642 if (line != NULL && *line == '|')
6643 // the line continues after a '|'
6644 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006645 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006646 && !(*line == '#' && (line == cctx.ctx_line_start
6647 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006648 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006649 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006650 goto erret;
6651 }
6652 else
6653 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006654 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006655 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006656 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006657 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006658 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006659 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006660
Bram Moolenaara80faa82020-04-12 19:37:17 +02006661 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006662 ea.cmdlinep = &line;
6663 ea.cmd = skipwhite(line);
6664
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006665 // Some things can be recognized by the first character.
6666 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006667 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006668 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006669 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006670 if (ea.cmd[1] != '{')
6671 {
6672 line = (char_u *)"";
6673 continue;
6674 }
6675 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006676
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006677 case '}':
6678 {
6679 // "}" ends a block scope
6680 scopetype_T stype = cctx.ctx_scope == NULL
6681 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006682
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006683 if (stype == BLOCK_SCOPE)
6684 {
6685 compile_endblock(&cctx);
6686 line = ea.cmd;
6687 }
6688 else
6689 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006690 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006691 goto erret;
6692 }
6693 if (line != NULL)
6694 line = skipwhite(ea.cmd + 1);
6695 continue;
6696 }
6697
6698 case '{':
6699 // "{" starts a block scope
6700 // "{'a': 1}->func() is something else
6701 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6702 {
6703 line = compile_block(ea.cmd, &cctx);
6704 continue;
6705 }
6706 break;
6707
6708 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006709 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006710 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006711 }
6712
6713 /*
6714 * COMMAND MODIFIERS
6715 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006716 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006717 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6718 {
6719 if (errormsg != NULL)
6720 goto erret;
6721 // empty line or comment
6722 line = (char_u *)"";
6723 continue;
6724 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006725 // TODO: use modifiers in the command
6726 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006727 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006728
6729 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006730 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006731 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006732 {
6733 if (*ea.cmd == '(')
6734 // not for "call()"
6735 ea.cmd = p;
6736 else
6737 ea.cmd = skipwhite(ea.cmd);
6738 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006739
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006740 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006741 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006742 char_u *pskip;
6743
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006744 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006745 // find what follows.
6746 // Skip over "var.member", "var[idx]" and the like.
6747 // Also "&opt = val", "$ENV = val" and "@r = val".
6748 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006749 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006750 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006751 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006752 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006753 char_u *var_end;
6754 int oplen;
6755 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006756
Bram Moolenaar65821722020-08-02 18:58:54 +02006757 if (ea.cmd[0] == '@')
6758 var_end = ea.cmd + 2;
6759 else
6760 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006761 FNE_CHECK_START | FNE_INCL_BR);
6762 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006763 if (oplen > 0)
6764 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006765 size_t len = p - ea.cmd;
6766
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006767 // Recognize an assignment if we recognize the variable
6768 // name:
6769 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006770 // "local = expr" where "local" is a local var.
6771 // "script = expr" where "script" is a script-local var.
6772 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006773 // "&opt = expr"
6774 // "$ENV = expr"
6775 // "@r = expr"
6776 if (*ea.cmd == '&'
6777 || *ea.cmd == '$'
6778 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006779 || ((len) > 2 && ea.cmd[1] == ':')
6780 || lookup_local(ea.cmd, len, &cctx) != NULL
6781 || lookup_arg(ea.cmd, len, NULL, NULL,
6782 NULL, &cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02006783 || lookup_script(ea.cmd, len, FALSE) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006784 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006785 {
6786 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006787 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006788 goto erret;
6789 continue;
6790 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006791 }
6792 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006793
6794 if (*ea.cmd == '[')
6795 {
6796 // [var, var] = expr
6797 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6798 if (line == NULL)
6799 goto erret;
6800 if (line != ea.cmd)
6801 continue;
6802 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006803 }
6804
6805 /*
6806 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006807 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006808 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006809 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006810 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006811 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006812 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006813 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006814 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006815 if (!starts_with_colon)
6816 {
6817 emsg(_(e_colon_required_before_a_range));
6818 goto erret;
6819 }
6820 if (ends_excmd2(line, ea.cmd))
6821 {
6822 // A range without a command: jump to the line.
6823 // TODO: compile to a more efficient command, possibly
6824 // calling parse_cmd_address().
6825 ea.cmdidx = CMD_SIZE;
6826 line = compile_exec(line, &ea, &cctx);
6827 goto nextline;
6828 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006829 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006830 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006831 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006832 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006833 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006834
6835 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6836 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006837 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006838 {
6839 line += STRLEN(line);
6840 continue;
6841 }
6842
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006843 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006844 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006845 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006846 // CMD_let cannot happen, compile_assignment() above is used
6847 iemsg("Command from find_ex_command() not handled");
6848 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006849 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006850 }
6851
6852 p = skipwhite(p);
6853
Bram Moolenaar3988f642020-08-27 22:43:03 +02006854 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006855 && ea.cmdidx != CMD_elseif
6856 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02006857 && ea.cmdidx != CMD_endif
6858 && ea.cmdidx != CMD_endfor
6859 && ea.cmdidx != CMD_endwhile
6860 && ea.cmdidx != CMD_catch
6861 && ea.cmdidx != CMD_finally
6862 && ea.cmdidx != CMD_endtry)
6863 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02006864 emsg(_(e_unreachable_code_after_return));
6865 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006866 }
6867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006868 switch (ea.cmdidx)
6869 {
6870 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006871 ea.arg = p;
6872 line = compile_nested_function(&ea, &cctx);
6873 break;
6874
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006875 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006876 // TODO: should we allow this, e.g. to declare a global
6877 // function?
6878 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006879 goto erret;
6880
6881 case CMD_return:
6882 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006883 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006884 break;
6885
6886 case CMD_let:
6887 case CMD_const:
6888 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006889 if (line == p)
6890 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006891 break;
6892
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006893 case CMD_unlet:
6894 case CMD_unlockvar:
6895 case CMD_lockvar:
6896 line = compile_unletlock(p, &ea, &cctx);
6897 break;
6898
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006899 case CMD_import:
6900 line = compile_import(p, &cctx);
6901 break;
6902
6903 case CMD_if:
6904 line = compile_if(p, &cctx);
6905 break;
6906 case CMD_elseif:
6907 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006908 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006909 break;
6910 case CMD_else:
6911 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006912 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006913 break;
6914 case CMD_endif:
6915 line = compile_endif(p, &cctx);
6916 break;
6917
6918 case CMD_while:
6919 line = compile_while(p, &cctx);
6920 break;
6921 case CMD_endwhile:
6922 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006923 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006924 break;
6925
6926 case CMD_for:
6927 line = compile_for(p, &cctx);
6928 break;
6929 case CMD_endfor:
6930 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006931 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006932 break;
6933 case CMD_continue:
6934 line = compile_continue(p, &cctx);
6935 break;
6936 case CMD_break:
6937 line = compile_break(p, &cctx);
6938 break;
6939
6940 case CMD_try:
6941 line = compile_try(p, &cctx);
6942 break;
6943 case CMD_catch:
6944 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006945 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006946 break;
6947 case CMD_finally:
6948 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006949 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006950 break;
6951 case CMD_endtry:
6952 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006953 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006954 break;
6955 case CMD_throw:
6956 line = compile_throw(p, &cctx);
6957 break;
6958
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006959 case CMD_eval:
6960 if (compile_expr0(&p, &cctx) == FAIL)
6961 goto erret;
6962
Bram Moolenaar3988f642020-08-27 22:43:03 +02006963 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006964 generate_instr_drop(&cctx, ISN_DROP, 1);
6965
6966 line = skipwhite(p);
6967 break;
6968
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006969 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006970 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006971 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006972 case CMD_echomsg:
6973 case CMD_echoerr:
6974 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006975 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006976
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006977 case CMD_put:
6978 ea.cmd = cmd;
6979 line = compile_put(p, &ea, &cctx);
6980 break;
6981
Bram Moolenaar3988f642020-08-27 22:43:03 +02006982 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006983
Bram Moolenaarae616492020-07-28 20:07:27 +02006984 case CMD_append:
6985 case CMD_change:
6986 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02006987 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02006988 case CMD_xit:
6989 not_in_vim9(&ea);
6990 goto erret;
6991
Bram Moolenaar002262f2020-07-08 17:47:57 +02006992 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02006993 if (cctx.ctx_skip != SKIP_YES)
6994 {
6995 semsg(_(e_invalid_command_str), ea.cmd);
6996 goto erret;
6997 }
6998 // We don't check for a next command here.
6999 line = (char_u *)"";
7000 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007001
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007002 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007003 if (cctx.ctx_skip == SKIP_YES)
7004 {
7005 // We don't check for a next command here.
7006 line = (char_u *)"";
7007 }
7008 else
7009 {
7010 // Not recognized, execute with do_cmdline_cmd().
7011 ea.arg = p;
7012 line = compile_exec(line, &ea, &cctx);
7013 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007014 break;
7015 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007016nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007017 if (line == NULL)
7018 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007019 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007020
7021 if (cctx.ctx_type_stack.ga_len < 0)
7022 {
7023 iemsg("Type stack underflow");
7024 goto erret;
7025 }
7026 }
7027
7028 if (cctx.ctx_scope != NULL)
7029 {
7030 if (cctx.ctx_scope->se_type == IF_SCOPE)
7031 emsg(_(e_endif));
7032 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7033 emsg(_(e_endwhile));
7034 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7035 emsg(_(e_endfor));
7036 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007037 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007038 goto erret;
7039 }
7040
Bram Moolenaarefd88552020-06-18 20:50:10 +02007041 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007042 {
7043 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7044 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007045 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007046 goto erret;
7047 }
7048
7049 // Return zero if there is no return at the end.
7050 generate_PUSHNR(&cctx, 0);
7051 generate_instr(&cctx, ISN_RETURN);
7052 }
7053
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007054 {
7055 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7056 + ufunc->uf_dfunc_idx;
7057 dfunc->df_deleted = FALSE;
7058 dfunc->df_instr = instr->ga_data;
7059 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007060 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007061 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007062 if (cctx.ctx_outer_used)
7063 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007064 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007065 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007066
7067 ret = OK;
7068
7069erret:
7070 if (ret == FAIL)
7071 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007072 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007073 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7074 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007075
7076 for (idx = 0; idx < instr->ga_len; ++idx)
7077 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007078 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007079
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007080 // If using the last entry in the table and it was added above, we
7081 // might as well remove it.
7082 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007083 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007084 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007085 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007086 ufunc->uf_dfunc_idx = 0;
7087 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007088 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007089
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007090 while (cctx.ctx_scope != NULL)
7091 drop_scope(&cctx);
7092
Bram Moolenaar20431c92020-03-20 18:39:46 +01007093 // Don't execute this function body.
7094 ga_clear_strings(&ufunc->uf_lines);
7095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007096 if (errormsg != NULL)
7097 emsg(errormsg);
7098 else if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007099 emsg(_(e_compile_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007100 }
7101
7102 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007103 if (do_estack_push)
7104 estack_pop();
7105
Bram Moolenaar20431c92020-03-20 18:39:46 +01007106 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007107 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007108 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007109 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007110}
7111
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007112 void
7113set_function_type(ufunc_T *ufunc)
7114{
7115 int varargs = ufunc->uf_va_name != NULL;
7116 int argcount = ufunc->uf_args.ga_len;
7117
7118 // Create a type for the function, with the return type and any
7119 // argument types.
7120 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7121 // The type is included in "tt_args".
7122 if (argcount > 0 || varargs)
7123 {
7124 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7125 argcount, &ufunc->uf_type_list);
7126 // Add argument types to the function type.
7127 if (func_type_add_arg_types(ufunc->uf_func_type,
7128 argcount + varargs,
7129 &ufunc->uf_type_list) == FAIL)
7130 return;
7131 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7132 ufunc->uf_func_type->tt_min_argcount =
7133 argcount - ufunc->uf_def_args.ga_len;
7134 if (ufunc->uf_arg_types == NULL)
7135 {
7136 int i;
7137
7138 // lambda does not have argument types.
7139 for (i = 0; i < argcount; ++i)
7140 ufunc->uf_func_type->tt_args[i] = &t_any;
7141 }
7142 else
7143 mch_memmove(ufunc->uf_func_type->tt_args,
7144 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7145 if (varargs)
7146 {
7147 ufunc->uf_func_type->tt_args[argcount] =
7148 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7149 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7150 }
7151 }
7152 else
7153 // No arguments, can use a predefined type.
7154 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7155 argcount, &ufunc->uf_type_list);
7156}
7157
7158
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007159/*
7160 * Delete an instruction, free what it contains.
7161 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007162 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007163delete_instr(isn_T *isn)
7164{
7165 switch (isn->isn_type)
7166 {
7167 case ISN_EXEC:
7168 case ISN_LOADENV:
7169 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007170 case ISN_LOADB:
7171 case ISN_LOADW:
7172 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007173 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007174 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007175 case ISN_PUSHEXC:
7176 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007177 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007178 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007179 case ISN_STOREB:
7180 case ISN_STOREW:
7181 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007182 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007183 vim_free(isn->isn_arg.string);
7184 break;
7185
7186 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007187 case ISN_STORES:
7188 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007189 break;
7190
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007191 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007192 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007193 vim_free(isn->isn_arg.unlet.ul_name);
7194 break;
7195
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007196 case ISN_STOREOPT:
7197 vim_free(isn->isn_arg.storeopt.so_name);
7198 break;
7199
7200 case ISN_PUSHBLOB: // push blob isn_arg.blob
7201 blob_unref(isn->isn_arg.blob);
7202 break;
7203
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007204 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007205#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007206 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007207#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007208 break;
7209
7210 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007211#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007212 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007213#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007214 break;
7215
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007216 case ISN_UCALL:
7217 vim_free(isn->isn_arg.ufunc.cuf_name);
7218 break;
7219
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007220 case ISN_FUNCREF:
7221 {
7222 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7223 + isn->isn_arg.funcref.fr_func;
7224 func_ptr_unref(dfunc->df_ufunc);
7225 }
7226 break;
7227
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007228 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007229 {
7230 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7231 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7232
7233 if (ufunc != NULL)
7234 {
7235 // Clear uf_dfunc_idx so that the function is deleted.
7236 clear_def_function(ufunc);
7237 ufunc->uf_dfunc_idx = 0;
7238 func_ptr_unref(ufunc);
7239 }
7240
7241 vim_free(lambda);
7242 vim_free(isn->isn_arg.newfunc.nf_global);
7243 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007244 break;
7245
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007246 case ISN_2BOOL:
7247 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007248 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007249 case ISN_ADDBLOB:
7250 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007251 case ISN_ANYINDEX:
7252 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007253 case ISN_BCALL:
7254 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007255 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007256 case ISN_CHECKNR:
7257 case ISN_CHECKTYPE:
7258 case ISN_COMPAREANY:
7259 case ISN_COMPAREBLOB:
7260 case ISN_COMPAREBOOL:
7261 case ISN_COMPAREDICT:
7262 case ISN_COMPAREFLOAT:
7263 case ISN_COMPAREFUNC:
7264 case ISN_COMPARELIST:
7265 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007266 case ISN_COMPARESPECIAL:
7267 case ISN_COMPARESTRING:
7268 case ISN_CONCAT:
7269 case ISN_DCALL:
7270 case ISN_DROP:
7271 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007272 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007273 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007274 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007275 case ISN_EXECCONCAT:
7276 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007277 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007278 case ISN_GETITEM:
7279 case ISN_JUMP:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007280 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007281 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007282 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007283 case ISN_LOADBDICT:
7284 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007285 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007286 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007287 case ISN_LOADSCRIPT:
7288 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007289 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007290 case ISN_LOADWDICT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007291 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007292 case ISN_NEGATENR:
7293 case ISN_NEWDICT:
7294 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007295 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007296 case ISN_OPFLOAT:
7297 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007298 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007299 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007300 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007301 case ISN_PUSHF:
7302 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007303 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007304 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007305 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007306 case ISN_SHUFFLE:
7307 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007308 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007309 case ISN_STOREDICT:
7310 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007311 case ISN_STORENR:
7312 case ISN_STOREOUTER:
7313 case ISN_STOREREG:
7314 case ISN_STORESCRIPT:
7315 case ISN_STOREV:
7316 case ISN_STRINDEX:
7317 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007318 case ISN_THROW:
7319 case ISN_TRY:
7320 // nothing allocated
7321 break;
7322 }
7323}
7324
7325/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007326 * Free all instructions for "dfunc".
7327 */
7328 static void
7329delete_def_function_contents(dfunc_T *dfunc)
7330{
7331 int idx;
7332
7333 ga_clear(&dfunc->df_def_args_isn);
7334
7335 if (dfunc->df_instr != NULL)
7336 {
7337 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7338 delete_instr(dfunc->df_instr + idx);
7339 VIM_CLEAR(dfunc->df_instr);
7340 }
7341
7342 dfunc->df_deleted = TRUE;
7343}
7344
7345/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007346 * When a user function is deleted, clear the contents of any associated def
7347 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007348 */
7349 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007350clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007351{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007352 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007353 {
7354 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7355 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007356
Bram Moolenaar20431c92020-03-20 18:39:46 +01007357 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007358 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007359 }
7360}
7361
7362#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007363/*
7364 * Free all functions defined with ":def".
7365 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007366 void
7367free_def_functions(void)
7368{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007369 int idx;
7370
7371 for (idx = 0; idx < def_functions.ga_len; ++idx)
7372 {
7373 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7374
7375 delete_def_function_contents(dfunc);
7376 }
7377
7378 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007379}
7380#endif
7381
7382
7383#endif // FEAT_EVAL