blob: 3e9b42c8547e3bc0ad1b0bdbd5d24437484f442f [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
111 int lv_from_outer; // when TRUE using ctx_outer scope
112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200129 int ctx_closure_count; // number of closures created in the
130 // function
131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144};
145
Bram Moolenaar20431c92020-03-20 18:39:46 +0100146static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100147
148/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200149 * Lookup variable "name" in the local scope and return it.
150 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200152 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153lookup_local(char_u *name, size_t len, cctx_T *cctx)
154{
155 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200156 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100158 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200159 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200160
161 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
163 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200164 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100165 if (STRNCMP(name, lvar->lv_name, len) == 0
166 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167 {
168 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200169 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200170 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200172
173 // Find local in outer function scope.
174 if (cctx->ctx_outer != NULL)
175 {
176 lvar = lookup_local(name, len, cctx->ctx_outer);
177 if (lvar != NULL)
178 {
179 // TODO: are there situations we should not mark the outer scope as
180 // used?
181 cctx->ctx_outer_used = TRUE;
182 lvar->lv_from_outer = TRUE;
183 return lvar;
184 }
185 }
186
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200187 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100188}
189
190/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200191 * Lookup an argument in the current function and an enclosing function.
192 * Returns the argument index in "idxp"
193 * Returns the argument type in "type"
194 * Sets "gen_load_outer" to TRUE if found in outer scope.
195 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196 */
197 static int
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200198lookup_arg(
199 char_u *name,
200 size_t len,
201 int *idxp,
202 type_T **type,
203 int *gen_load_outer,
204 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100205{
206 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200207 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100209 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200210 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
212 {
213 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
214
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200215 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
216 {
217 if (idxp != NULL)
218 {
219 // Arguments are located above the frame pointer. One further
220 // if there is a vararg argument
221 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
222 + STACK_FRAME_SIZE)
223 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
224
225 if (cctx->ctx_ufunc->uf_arg_types != NULL)
226 *type = cctx->ctx_ufunc->uf_arg_types[idx];
227 else
228 *type = &t_any;
229 }
230 return OK;
231 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100232 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100233
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200234 va_name = cctx->ctx_ufunc->uf_va_name;
235 if (va_name != NULL
236 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
237 {
238 if (idxp != NULL)
239 {
240 // varargs is always the last argument
241 *idxp = -STACK_FRAME_SIZE - 1;
242 *type = cctx->ctx_ufunc->uf_va_type;
243 }
244 return OK;
245 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200247 if (cctx->ctx_outer != NULL)
248 {
249 // Lookup the name for an argument of the outer function.
250 if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
251 == OK)
252 {
253 *gen_load_outer = TRUE;
254 return OK;
255 }
256 }
257
258 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100259}
260
261/*
Bram Moolenaar84367732020-08-23 15:21:55 +0200262 * Returnd TRUE if the script context is Vim9 script.
263 */
264 static int
265script_is_vim9()
266{
267 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
268}
269
270/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 * Lookup a variable in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200272 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
273 * without "s:".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274 * Returns OK or FAIL.
275 */
276 static int
Bram Moolenaar53900992020-08-22 19:02:02 +0200277lookup_script(char_u *name, size_t len, int vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278{
279 int cc;
280 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
281 dictitem_T *di;
282
Bram Moolenaar84367732020-08-23 15:21:55 +0200283 if (vim9script && !script_is_vim9())
Bram Moolenaar53900992020-08-22 19:02:02 +0200284 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100285 cc = name[len];
286 name[len] = NUL;
287 di = find_var_in_ht(ht, 0, name, TRUE);
288 name[len] = cc;
289 return di == NULL ? FAIL: OK;
290}
291
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100292/*
293 * Check if "p[len]" is already defined, either in script "import_sid" or in
294 * compilation context "cctx".
295 * Return FAIL and give an error if it defined.
296 */
297 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200298check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100299{
Bram Moolenaarad486a02020-08-01 23:22:18 +0200300 int c = p[len];
301
302 p[len] = NUL;
Bram Moolenaar53900992020-08-22 19:02:02 +0200303 if (lookup_script(p, len, FALSE) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100304 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200305 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaareef21022020-08-01 22:16:43 +0200306 || lookup_arg(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200307 || find_imported(p, len, cctx) != NULL
308 || find_func_even_dead(p, FALSE, cctx) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100309 {
Bram Moolenaarad486a02020-08-01 23:22:18 +0200310 p[len] = c;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200311 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100312 return FAIL;
313 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200314 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100315 return OK;
316}
317
Bram Moolenaar65b95452020-07-19 14:03:09 +0200318
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100319/////////////////////////////////////////////////////////////////////
320// Following generate_ functions expect the caller to call ga_grow().
321
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200322#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
323#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100324
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100325/*
326 * Generate an instruction without arguments.
327 * Returns a pointer to the new instruction, NULL if failed.
328 */
329 static isn_T *
330generate_instr(cctx_T *cctx, isntype_T isn_type)
331{
332 garray_T *instr = &cctx->ctx_instr;
333 isn_T *isn;
334
Bram Moolenaar080457c2020-03-03 21:53:32 +0100335 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100336 if (ga_grow(instr, 1) == FAIL)
337 return NULL;
338 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
339 isn->isn_type = isn_type;
340 isn->isn_lnum = cctx->ctx_lnum + 1;
341 ++instr->ga_len;
342
343 return isn;
344}
345
346/*
347 * Generate an instruction without arguments.
348 * "drop" will be removed from the stack.
349 * Returns a pointer to the new instruction, NULL if failed.
350 */
351 static isn_T *
352generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
353{
354 garray_T *stack = &cctx->ctx_type_stack;
355
Bram Moolenaar080457c2020-03-03 21:53:32 +0100356 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100357 stack->ga_len -= drop;
358 return generate_instr(cctx, isn_type);
359}
360
361/*
362 * Generate instruction "isn_type" and put "type" on the type stack.
363 */
364 static isn_T *
365generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
366{
367 isn_T *isn;
368 garray_T *stack = &cctx->ctx_type_stack;
369
370 if ((isn = generate_instr(cctx, isn_type)) == NULL)
371 return NULL;
372
373 if (ga_grow(stack, 1) == FAIL)
374 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200375 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100376 ++stack->ga_len;
377
378 return isn;
379}
380
381/*
382 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200383 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384 */
385 static int
386may_generate_2STRING(int offset, cctx_T *cctx)
387{
388 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200389 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100390 garray_T *stack = &cctx->ctx_type_stack;
391 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
392
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200393 switch ((*type)->tt_type)
394 {
395 // nothing to be done
396 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100397
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200398 // conversion possible
399 case VAR_SPECIAL:
400 case VAR_BOOL:
401 case VAR_NUMBER:
402 case VAR_FLOAT:
403 break;
404
405 // conversion possible (with runtime check)
406 case VAR_ANY:
407 case VAR_UNKNOWN:
408 isntype = ISN_2STRING_ANY;
409 break;
410
411 // conversion not possible
412 case VAR_VOID:
413 case VAR_BLOB:
414 case VAR_FUNC:
415 case VAR_PARTIAL:
416 case VAR_LIST:
417 case VAR_DICT:
418 case VAR_JOB:
419 case VAR_CHANNEL:
420 to_string_error((*type)->tt_type);
421 return FAIL;
422 }
423
424 *type = &t_string;
425 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100426 return FAIL;
427 isn->isn_arg.number = offset;
428
429 return OK;
430}
431
432 static int
433check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
434{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200435 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100436 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200437 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100438 {
439 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200440 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100441 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200442 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100443 return FAIL;
444 }
445 return OK;
446}
447
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200448 static int
449generate_add_instr(
450 cctx_T *cctx,
451 vartype_T vartype,
452 type_T *type1,
453 type_T *type2)
454{
455 isn_T *isn = generate_instr_drop(cctx,
456 vartype == VAR_NUMBER ? ISN_OPNR
457 : vartype == VAR_LIST ? ISN_ADDLIST
458 : vartype == VAR_BLOB ? ISN_ADDBLOB
459#ifdef FEAT_FLOAT
460 : vartype == VAR_FLOAT ? ISN_OPFLOAT
461#endif
462 : ISN_OPANY, 1);
463
464 if (vartype != VAR_LIST && vartype != VAR_BLOB
465 && type1->tt_type != VAR_ANY
466 && type2->tt_type != VAR_ANY
467 && check_number_or_float(
468 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
469 return FAIL;
470
471 if (isn != NULL)
472 isn->isn_arg.op.op_type = EXPR_ADD;
473 return isn == NULL ? FAIL : OK;
474}
475
476/*
477 * Get the type to use for an instruction for an operation on "type1" and
478 * "type2". If they are matching use a type-specific instruction. Otherwise
479 * fall back to runtime type checking.
480 */
481 static vartype_T
482operator_type(type_T *type1, type_T *type2)
483{
484 if (type1->tt_type == type2->tt_type
485 && (type1->tt_type == VAR_NUMBER
486 || type1->tt_type == VAR_LIST
487#ifdef FEAT_FLOAT
488 || type1->tt_type == VAR_FLOAT
489#endif
490 || type1->tt_type == VAR_BLOB))
491 return type1->tt_type;
492 return VAR_ANY;
493}
494
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100495/*
496 * Generate an instruction with two arguments. The instruction depends on the
497 * type of the arguments.
498 */
499 static int
500generate_two_op(cctx_T *cctx, char_u *op)
501{
502 garray_T *stack = &cctx->ctx_type_stack;
503 type_T *type1;
504 type_T *type2;
505 vartype_T vartype;
506 isn_T *isn;
507
Bram Moolenaar080457c2020-03-03 21:53:32 +0100508 RETURN_OK_IF_SKIP(cctx);
509
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200510 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100511 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
512 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200513 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100514
515 switch (*op)
516 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200517 case '+':
518 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100519 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100520 break;
521
522 case '-':
523 case '*':
524 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
525 op) == FAIL)
526 return FAIL;
527 if (vartype == VAR_NUMBER)
528 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
529#ifdef FEAT_FLOAT
530 else if (vartype == VAR_FLOAT)
531 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
532#endif
533 else
534 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
535 if (isn != NULL)
536 isn->isn_arg.op.op_type = *op == '*'
537 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
538 break;
539
Bram Moolenaar4c683752020-04-05 21:38:23 +0200540 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100541 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200542 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100543 && type2->tt_type != VAR_NUMBER))
544 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200545 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100546 return FAIL;
547 }
548 isn = generate_instr_drop(cctx,
549 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
550 if (isn != NULL)
551 isn->isn_arg.op.op_type = EXPR_REM;
552 break;
553 }
554
555 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200556 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100557 {
558 type_T *type = &t_any;
559
560#ifdef FEAT_FLOAT
561 // float+number and number+float results in float
562 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
563 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
564 type = &t_float;
565#endif
566 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
567 }
568
569 return OK;
570}
571
572/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200573 * Get the instruction to use for comparing "type1" with "type2"
574 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200576 static isntype_T
577get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100578{
579 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580
Bram Moolenaar4c683752020-04-05 21:38:23 +0200581 if (type1 == VAR_UNKNOWN)
582 type1 = VAR_ANY;
583 if (type2 == VAR_UNKNOWN)
584 type2 = VAR_ANY;
585
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586 if (type1 == type2)
587 {
588 switch (type1)
589 {
590 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
591 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
592 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
593 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
594 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
595 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
596 case VAR_LIST: isntype = ISN_COMPARELIST; break;
597 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
598 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100599 default: isntype = ISN_COMPAREANY; break;
600 }
601 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200602 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100603 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
604 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
605 isntype = ISN_COMPAREANY;
606
607 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
608 && (isntype == ISN_COMPAREBOOL
609 || isntype == ISN_COMPARESPECIAL
610 || isntype == ISN_COMPARENR
611 || isntype == ISN_COMPAREFLOAT))
612 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200613 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200615 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100616 }
617 if (isntype == ISN_DROP
618 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
619 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
620 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
621 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
622 && exptype != EXPR_IS && exptype != EXPR_ISNOT
623 && (type1 == VAR_BLOB || type2 == VAR_BLOB
624 || type1 == VAR_LIST || type2 == VAR_LIST))))
625 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200626 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100627 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200628 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100629 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200630 return isntype;
631}
632
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200633 int
634check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
635{
636 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
637 return FAIL;
638 return OK;
639}
640
Bram Moolenaara5565e42020-05-09 15:44:01 +0200641/*
642 * Generate an ISN_COMPARE* instruction with a boolean result.
643 */
644 static int
645generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
646{
647 isntype_T isntype;
648 isn_T *isn;
649 garray_T *stack = &cctx->ctx_type_stack;
650 vartype_T type1;
651 vartype_T type2;
652
653 RETURN_OK_IF_SKIP(cctx);
654
655 // Get the known type of the two items on the stack. If they are matching
656 // use a type-specific instruction. Otherwise fall back to runtime type
657 // checking.
658 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
659 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
660 isntype = get_compare_isn(exptype, type1, type2);
661 if (isntype == ISN_DROP)
662 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100663
664 if ((isn = generate_instr(cctx, isntype)) == NULL)
665 return FAIL;
666 isn->isn_arg.op.op_type = exptype;
667 isn->isn_arg.op.op_ic = ic;
668
669 // takes two arguments, puts one bool back
670 if (stack->ga_len >= 2)
671 {
672 --stack->ga_len;
673 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
674 }
675
676 return OK;
677}
678
679/*
680 * Generate an ISN_2BOOL instruction.
681 */
682 static int
683generate_2BOOL(cctx_T *cctx, int invert)
684{
685 isn_T *isn;
686 garray_T *stack = &cctx->ctx_type_stack;
687
Bram Moolenaar080457c2020-03-03 21:53:32 +0100688 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100689 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
690 return FAIL;
691 isn->isn_arg.number = invert;
692
693 // type becomes bool
694 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
695
696 return OK;
697}
698
699 static int
700generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
701{
702 isn_T *isn;
703 garray_T *stack = &cctx->ctx_type_stack;
704
Bram Moolenaar080457c2020-03-03 21:53:32 +0100705 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
707 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200708 // TODO: whole type, e.g. for a function also arg and return types
709 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100710 isn->isn_arg.type.ct_off = offset;
711
712 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200713 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100714
715 return OK;
716}
717
718/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200719 * Check that
720 * - "actual" is "expected" type or
721 * - "actual" is a type that can be "expected" type: add a runtime check; or
722 * - return FAIL.
723 */
724 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200725need_type(
726 type_T *actual,
727 type_T *expected,
728 int offset,
729 cctx_T *cctx,
730 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200731{
732 if (check_type(expected, actual, FALSE) == OK)
733 return OK;
734 if (actual->tt_type != VAR_ANY
735 && actual->tt_type != VAR_UNKNOWN
736 && !(actual->tt_type == VAR_FUNC
737 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
738 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200739 if (!silent)
740 type_mismatch(expected, actual);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200741 return FAIL;
742 }
743 generate_TYPECHECK(cctx, expected, offset);
744 return OK;
745}
746
747/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100748 * Generate an ISN_PUSHNR instruction.
749 */
750 static int
751generate_PUSHNR(cctx_T *cctx, varnumber_T number)
752{
753 isn_T *isn;
754
Bram Moolenaar080457c2020-03-03 21:53:32 +0100755 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
757 return FAIL;
758 isn->isn_arg.number = number;
759
760 return OK;
761}
762
763/*
764 * Generate an ISN_PUSHBOOL instruction.
765 */
766 static int
767generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
768{
769 isn_T *isn;
770
Bram Moolenaar080457c2020-03-03 21:53:32 +0100771 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100772 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
773 return FAIL;
774 isn->isn_arg.number = number;
775
776 return OK;
777}
778
779/*
780 * Generate an ISN_PUSHSPEC instruction.
781 */
782 static int
783generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
784{
785 isn_T *isn;
786
Bram Moolenaar080457c2020-03-03 21:53:32 +0100787 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100788 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
789 return FAIL;
790 isn->isn_arg.number = number;
791
792 return OK;
793}
794
795#ifdef FEAT_FLOAT
796/*
797 * Generate an ISN_PUSHF instruction.
798 */
799 static int
800generate_PUSHF(cctx_T *cctx, float_T fnumber)
801{
802 isn_T *isn;
803
Bram Moolenaar080457c2020-03-03 21:53:32 +0100804 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100805 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
806 return FAIL;
807 isn->isn_arg.fnumber = fnumber;
808
809 return OK;
810}
811#endif
812
813/*
814 * Generate an ISN_PUSHS instruction.
815 * Consumes "str".
816 */
817 static int
818generate_PUSHS(cctx_T *cctx, char_u *str)
819{
820 isn_T *isn;
821
Bram Moolenaar080457c2020-03-03 21:53:32 +0100822 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
824 return FAIL;
825 isn->isn_arg.string = str;
826
827 return OK;
828}
829
830/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100831 * Generate an ISN_PUSHCHANNEL instruction.
832 * Consumes "channel".
833 */
834 static int
835generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
836{
837 isn_T *isn;
838
Bram Moolenaar080457c2020-03-03 21:53:32 +0100839 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100840 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
841 return FAIL;
842 isn->isn_arg.channel = channel;
843
844 return OK;
845}
846
847/*
848 * Generate an ISN_PUSHJOB instruction.
849 * Consumes "job".
850 */
851 static int
852generate_PUSHJOB(cctx_T *cctx, job_T *job)
853{
854 isn_T *isn;
855
Bram Moolenaar080457c2020-03-03 21:53:32 +0100856 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100857 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100858 return FAIL;
859 isn->isn_arg.job = job;
860
861 return OK;
862}
863
864/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100865 * Generate an ISN_PUSHBLOB instruction.
866 * Consumes "blob".
867 */
868 static int
869generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
870{
871 isn_T *isn;
872
Bram Moolenaar080457c2020-03-03 21:53:32 +0100873 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
875 return FAIL;
876 isn->isn_arg.blob = blob;
877
878 return OK;
879}
880
881/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100882 * Generate an ISN_PUSHFUNC instruction with name "name".
883 * Consumes "name".
884 */
885 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200886generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100887{
888 isn_T *isn;
889
Bram Moolenaar080457c2020-03-03 21:53:32 +0100890 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200891 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100892 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200893 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100894
895 return OK;
896}
897
898/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200899 * Generate an ISN_GETITEM instruction with "index".
900 */
901 static int
902generate_GETITEM(cctx_T *cctx, int index)
903{
904 isn_T *isn;
905 garray_T *stack = &cctx->ctx_type_stack;
906 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
907 type_T *item_type = &t_any;
908
909 RETURN_OK_IF_SKIP(cctx);
910
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200911 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200912 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200913 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200914 emsg(_(e_listreq));
915 return FAIL;
916 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200917 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200918 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
919 return FAIL;
920 isn->isn_arg.number = index;
921
922 // add the item type to the type stack
923 if (ga_grow(stack, 1) == FAIL)
924 return FAIL;
925 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
926 ++stack->ga_len;
927 return OK;
928}
929
930/*
Bram Moolenaar9af78762020-06-16 11:34:42 +0200931 * Generate an ISN_SLICE instruction with "count".
932 */
933 static int
934generate_SLICE(cctx_T *cctx, int count)
935{
936 isn_T *isn;
937
938 RETURN_OK_IF_SKIP(cctx);
939 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
940 return FAIL;
941 isn->isn_arg.number = count;
942 return OK;
943}
944
945/*
946 * Generate an ISN_CHECKLEN instruction with "min_len".
947 */
948 static int
949generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
950{
951 isn_T *isn;
952
953 RETURN_OK_IF_SKIP(cctx);
954
955 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
956 return FAIL;
957 isn->isn_arg.checklen.cl_min_len = min_len;
958 isn->isn_arg.checklen.cl_more_OK = more_OK;
959
960 return OK;
961}
962
963/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100964 * Generate an ISN_STORE instruction.
965 */
966 static int
967generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
968{
969 isn_T *isn;
970
Bram Moolenaar080457c2020-03-03 21:53:32 +0100971 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100972 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
973 return FAIL;
974 if (name != NULL)
975 isn->isn_arg.string = vim_strsave(name);
976 else
977 isn->isn_arg.number = idx;
978
979 return OK;
980}
981
982/*
983 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
984 */
985 static int
986generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
987{
988 isn_T *isn;
989
Bram Moolenaar080457c2020-03-03 21:53:32 +0100990 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100991 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
992 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +0100993 isn->isn_arg.storenr.stnr_idx = idx;
994 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100995
996 return OK;
997}
998
999/*
1000 * Generate an ISN_STOREOPT instruction
1001 */
1002 static int
1003generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1004{
1005 isn_T *isn;
1006
Bram Moolenaar080457c2020-03-03 21:53:32 +01001007 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001008 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001009 return FAIL;
1010 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1011 isn->isn_arg.storeopt.so_flags = opt_flags;
1012
1013 return OK;
1014}
1015
1016/*
1017 * Generate an ISN_LOAD or similar instruction.
1018 */
1019 static int
1020generate_LOAD(
1021 cctx_T *cctx,
1022 isntype_T isn_type,
1023 int idx,
1024 char_u *name,
1025 type_T *type)
1026{
1027 isn_T *isn;
1028
Bram Moolenaar080457c2020-03-03 21:53:32 +01001029 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001030 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1031 return FAIL;
1032 if (name != NULL)
1033 isn->isn_arg.string = vim_strsave(name);
1034 else
1035 isn->isn_arg.number = idx;
1036
1037 return OK;
1038}
1039
1040/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001041 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001042 */
1043 static int
1044generate_LOADV(
1045 cctx_T *cctx,
1046 char_u *name,
1047 int error)
1048{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001049 int di_flags;
1050 int vidx = find_vim_var(name, &di_flags);
1051 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001052
Bram Moolenaar080457c2020-03-03 21:53:32 +01001053 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001054 if (vidx < 0)
1055 {
1056 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001057 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001058 return FAIL;
1059 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001060 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001061
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001062 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001063}
1064
1065/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001066 * Generate an ISN_UNLET instruction.
1067 */
1068 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001069generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001070{
1071 isn_T *isn;
1072
1073 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001074 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001075 return FAIL;
1076 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1077 isn->isn_arg.unlet.ul_forceit = forceit;
1078
1079 return OK;
1080}
1081
1082/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001083 * Generate an ISN_LOADS instruction.
1084 */
1085 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001086generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001087 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001088 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001089 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001090 int sid,
1091 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001092{
1093 isn_T *isn;
1094
Bram Moolenaar080457c2020-03-03 21:53:32 +01001095 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001096 if (isn_type == ISN_LOADS)
1097 isn = generate_instr_type(cctx, isn_type, type);
1098 else
1099 isn = generate_instr_drop(cctx, isn_type, 1);
1100 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001101 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001102 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1103 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001104
1105 return OK;
1106}
1107
1108/*
1109 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1110 */
1111 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001112generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001113 cctx_T *cctx,
1114 isntype_T isn_type,
1115 int sid,
1116 int idx,
1117 type_T *type)
1118{
1119 isn_T *isn;
1120
Bram Moolenaar080457c2020-03-03 21:53:32 +01001121 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001122 if (isn_type == ISN_LOADSCRIPT)
1123 isn = generate_instr_type(cctx, isn_type, type);
1124 else
1125 isn = generate_instr_drop(cctx, isn_type, 1);
1126 if (isn == NULL)
1127 return FAIL;
1128 isn->isn_arg.script.script_sid = sid;
1129 isn->isn_arg.script.script_idx = idx;
1130 return OK;
1131}
1132
1133/*
1134 * Generate an ISN_NEWLIST instruction.
1135 */
1136 static int
1137generate_NEWLIST(cctx_T *cctx, int count)
1138{
1139 isn_T *isn;
1140 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001141 type_T *type;
1142 type_T *member;
1143
Bram Moolenaar080457c2020-03-03 21:53:32 +01001144 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001145 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1146 return FAIL;
1147 isn->isn_arg.number = count;
1148
Bram Moolenaar127542b2020-08-09 17:22:04 +02001149 // get the member type from all the items on the stack.
1150 member = get_member_type_from_stack(
1151 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1152 cctx->ctx_type_list);
1153 type = get_list_type(member, cctx->ctx_type_list);
1154
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155 // drop the value types
1156 stack->ga_len -= count;
1157
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001158 // add the list type to the type stack
1159 if (ga_grow(stack, 1) == FAIL)
1160 return FAIL;
1161 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1162 ++stack->ga_len;
1163
1164 return OK;
1165}
1166
1167/*
1168 * Generate an ISN_NEWDICT instruction.
1169 */
1170 static int
1171generate_NEWDICT(cctx_T *cctx, int count)
1172{
1173 isn_T *isn;
1174 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175 type_T *type;
1176 type_T *member;
1177
Bram Moolenaar080457c2020-03-03 21:53:32 +01001178 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1180 return FAIL;
1181 isn->isn_arg.number = count;
1182
Bram Moolenaar127542b2020-08-09 17:22:04 +02001183 member = get_member_type_from_stack(
1184 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1185 cctx->ctx_type_list);
1186 type = get_dict_type(member, cctx->ctx_type_list);
1187
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001188 // drop the key and value types
1189 stack->ga_len -= 2 * count;
1190
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001191 // add the dict type to the type stack
1192 if (ga_grow(stack, 1) == FAIL)
1193 return FAIL;
1194 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1195 ++stack->ga_len;
1196
1197 return OK;
1198}
1199
1200/*
1201 * Generate an ISN_FUNCREF instruction.
1202 */
1203 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001204generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001205{
1206 isn_T *isn;
1207 garray_T *stack = &cctx->ctx_type_stack;
1208
Bram Moolenaar080457c2020-03-03 21:53:32 +01001209 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001210 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1211 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001212 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001213 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001214
1215 if (ga_grow(stack, 1) == FAIL)
1216 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001217 ((type_T **)stack->ga_data)[stack->ga_len] =
1218 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219 ++stack->ga_len;
1220
1221 return OK;
1222}
1223
1224/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001225 * Generate an ISN_NEWFUNC instruction.
1226 */
1227 static int
1228generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1229{
1230 isn_T *isn;
1231 char_u *name;
1232
1233 RETURN_OK_IF_SKIP(cctx);
1234 name = vim_strsave(lambda_name);
1235 if (name == NULL)
1236 return FAIL;
1237 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1238 return FAIL;
1239 isn->isn_arg.newfunc.nf_lambda = name;
1240 isn->isn_arg.newfunc.nf_global = func_name;
1241
1242 return OK;
1243}
1244
1245/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001246 * Generate an ISN_JUMP instruction.
1247 */
1248 static int
1249generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1250{
1251 isn_T *isn;
1252 garray_T *stack = &cctx->ctx_type_stack;
1253
Bram Moolenaar080457c2020-03-03 21:53:32 +01001254 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001255 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1256 return FAIL;
1257 isn->isn_arg.jump.jump_when = when;
1258 isn->isn_arg.jump.jump_where = where;
1259
1260 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1261 --stack->ga_len;
1262
1263 return OK;
1264}
1265
1266 static int
1267generate_FOR(cctx_T *cctx, int loop_idx)
1268{
1269 isn_T *isn;
1270 garray_T *stack = &cctx->ctx_type_stack;
1271
Bram Moolenaar080457c2020-03-03 21:53:32 +01001272 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1274 return FAIL;
1275 isn->isn_arg.forloop.for_idx = loop_idx;
1276
1277 if (ga_grow(stack, 1) == FAIL)
1278 return FAIL;
1279 // type doesn't matter, will be stored next
1280 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1281 ++stack->ga_len;
1282
1283 return OK;
1284}
1285
1286/*
1287 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001288 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001289 * Return FAIL if the number of arguments is wrong.
1290 */
1291 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001292generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001293{
1294 isn_T *isn;
1295 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001296 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001297 type_T *argtypes[MAX_FUNC_ARGS];
1298 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001299
Bram Moolenaar080457c2020-03-03 21:53:32 +01001300 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001301 argoff = check_internal_func(func_idx, argcount);
1302 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303 return FAIL;
1304
Bram Moolenaar389df252020-07-09 21:20:47 +02001305 if (method_call && argoff > 1)
1306 {
1307 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1308 return FAIL;
1309 isn->isn_arg.shuffle.shfl_item = argcount;
1310 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1311 }
1312
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001313 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1314 return FAIL;
1315 isn->isn_arg.bfunc.cbf_idx = func_idx;
1316 isn->isn_arg.bfunc.cbf_argcount = argcount;
1317
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001318 for (i = 0; i < argcount; ++i)
1319 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1320
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 stack->ga_len -= argcount; // drop the arguments
1322 if (ga_grow(stack, 1) == FAIL)
1323 return FAIL;
1324 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001325 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001326 ++stack->ga_len; // add return value
1327
1328 return OK;
1329}
1330
1331/*
1332 * Generate an ISN_DCALL or ISN_UCALL instruction.
1333 * Return FAIL if the number of arguments is wrong.
1334 */
1335 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001336generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337{
1338 isn_T *isn;
1339 garray_T *stack = &cctx->ctx_type_stack;
1340 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001341 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342
Bram Moolenaar080457c2020-03-03 21:53:32 +01001343 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 if (argcount > regular_args && !has_varargs(ufunc))
1345 {
1346 semsg(_(e_toomanyarg), ufunc->uf_name);
1347 return FAIL;
1348 }
1349 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1350 {
1351 semsg(_(e_toofewarg), ufunc->uf_name);
1352 return FAIL;
1353 }
1354
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001355 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001356 {
1357 int i;
1358
1359 for (i = 0; i < argcount; ++i)
1360 {
1361 type_T *expected;
1362 type_T *actual;
1363
1364 if (i < regular_args)
1365 {
1366 if (ufunc->uf_arg_types == NULL)
1367 continue;
1368 expected = ufunc->uf_arg_types[i];
1369 }
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001370 else if (ufunc->uf_va_type == NULL)
1371 // possibly a lambda
1372 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001373 else
1374 expected = ufunc->uf_va_type->tt_member;
1375 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001376 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001377 {
1378 arg_type_mismatch(expected, actual, i + 1);
1379 return FAIL;
1380 }
1381 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001382 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001383 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1384 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001385 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001386 }
1387
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001388 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001389 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001390 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001391 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001392 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393 {
1394 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1395 isn->isn_arg.dfunc.cdf_argcount = argcount;
1396 }
1397 else
1398 {
1399 // A user function may be deleted and redefined later, can't use the
1400 // ufunc pointer, need to look it up again at runtime.
1401 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1402 isn->isn_arg.ufunc.cuf_argcount = argcount;
1403 }
1404
1405 stack->ga_len -= argcount; // drop the arguments
1406 if (ga_grow(stack, 1) == FAIL)
1407 return FAIL;
1408 // add return value
1409 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1410 ++stack->ga_len;
1411
1412 return OK;
1413}
1414
1415/*
1416 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1417 */
1418 static int
1419generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1420{
1421 isn_T *isn;
1422 garray_T *stack = &cctx->ctx_type_stack;
1423
Bram Moolenaar080457c2020-03-03 21:53:32 +01001424 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1426 return FAIL;
1427 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1428 isn->isn_arg.ufunc.cuf_argcount = argcount;
1429
1430 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001431 if (ga_grow(stack, 1) == FAIL)
1432 return FAIL;
1433 // add return value
1434 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1435 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001436
1437 return OK;
1438}
1439
1440/*
1441 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001442 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001443 */
1444 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001445generate_PCALL(
1446 cctx_T *cctx,
1447 int argcount,
1448 char_u *name,
1449 type_T *type,
1450 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001451{
1452 isn_T *isn;
1453 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001454 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455
Bram Moolenaar080457c2020-03-03 21:53:32 +01001456 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001457
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001458 if (type->tt_type == VAR_ANY)
1459 ret_type = &t_any;
1460 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001461 {
1462 if (type->tt_argcount != -1)
1463 {
1464 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1465
1466 if (argcount < type->tt_min_argcount - varargs)
1467 {
1468 semsg(_(e_toofewarg), "[reference]");
1469 return FAIL;
1470 }
1471 if (!varargs && argcount > type->tt_argcount)
1472 {
1473 semsg(_(e_toomanyarg), "[reference]");
1474 return FAIL;
1475 }
1476 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001477 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001478 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001479 else
1480 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001481 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001482 return FAIL;
1483 }
1484
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1486 return FAIL;
1487 isn->isn_arg.pfunc.cpf_top = at_top;
1488 isn->isn_arg.pfunc.cpf_argcount = argcount;
1489
1490 stack->ga_len -= argcount; // drop the arguments
1491
1492 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001493 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001494
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001495 // If partial is above the arguments it must be cleared and replaced with
1496 // the return value.
1497 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1498 return FAIL;
1499
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001500 return OK;
1501}
1502
1503/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001504 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001505 */
1506 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001507generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508{
1509 isn_T *isn;
1510 garray_T *stack = &cctx->ctx_type_stack;
1511 type_T *type;
1512
Bram Moolenaar080457c2020-03-03 21:53:32 +01001513 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001514 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001515 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001516 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001517
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001518 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001519 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001520 if (type->tt_type != VAR_DICT && type != &t_any)
1521 {
1522 emsg(_(e_dictreq));
1523 return FAIL;
1524 }
1525 // change dict type to dict member type
1526 if (type->tt_type == VAR_DICT)
1527 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528
1529 return OK;
1530}
1531
1532/*
1533 * Generate an ISN_ECHO instruction.
1534 */
1535 static int
1536generate_ECHO(cctx_T *cctx, int with_white, int count)
1537{
1538 isn_T *isn;
1539
Bram Moolenaar080457c2020-03-03 21:53:32 +01001540 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1542 return FAIL;
1543 isn->isn_arg.echo.echo_with_white = with_white;
1544 isn->isn_arg.echo.echo_count = count;
1545
1546 return OK;
1547}
1548
Bram Moolenaarad39c092020-02-26 18:23:43 +01001549/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001550 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001551 */
1552 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001553generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001554{
1555 isn_T *isn;
1556
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001557 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001558 return FAIL;
1559 isn->isn_arg.number = count;
1560
1561 return OK;
1562}
1563
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001564 static int
1565generate_EXEC(cctx_T *cctx, char_u *line)
1566{
1567 isn_T *isn;
1568
Bram Moolenaar080457c2020-03-03 21:53:32 +01001569 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001570 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1571 return FAIL;
1572 isn->isn_arg.string = vim_strsave(line);
1573 return OK;
1574}
1575
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001576 static int
1577generate_EXECCONCAT(cctx_T *cctx, int count)
1578{
1579 isn_T *isn;
1580
1581 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1582 return FAIL;
1583 isn->isn_arg.number = count;
1584 return OK;
1585}
1586
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001587/*
1588 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001589 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001591 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1593{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 lvar_T *lvar;
1595
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001596 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001597 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001598 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001599 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 }
1601
1602 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001603 return NULL;
1604 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001606 // Every local variable uses the next entry on the stack. We could re-use
1607 // the last ones when leaving a scope, but then variables used in a closure
1608 // might get overwritten. To keep things simple do not re-use stack
1609 // entries. This is less efficient, but memory is cheap these days.
1610 lvar->lv_idx = cctx->ctx_locals_count++;
1611
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001612 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 lvar->lv_const = isConst;
1614 lvar->lv_type = type;
1615
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001616 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001617}
1618
1619/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001620 * Remove local variables above "new_top".
1621 */
1622 static void
1623unwind_locals(cctx_T *cctx, int new_top)
1624{
1625 if (cctx->ctx_locals.ga_len > new_top)
1626 {
1627 int idx;
1628 lvar_T *lvar;
1629
1630 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1631 {
1632 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1633 vim_free(lvar->lv_name);
1634 }
1635 }
1636 cctx->ctx_locals.ga_len = new_top;
1637}
1638
1639/*
1640 * Free all local variables.
1641 */
1642 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001643free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001644{
1645 unwind_locals(cctx, 0);
1646 ga_clear(&cctx->ctx_locals);
1647}
1648
1649/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001650 * Find "name" in script-local items of script "sid".
1651 * Returns the index in "sn_var_vals" if found.
1652 * If found but not in "sn_var_vals" returns -1.
1653 * If not found returns -2.
1654 */
1655 int
1656get_script_item_idx(int sid, char_u *name, int check_writable)
1657{
1658 hashtab_T *ht;
1659 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001660 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001661 int idx;
1662
1663 // First look the name up in the hashtable.
1664 if (sid <= 0 || sid > script_items.ga_len)
1665 return -1;
1666 ht = &SCRIPT_VARS(sid);
1667 di = find_var_in_ht(ht, 0, name, TRUE);
1668 if (di == NULL)
1669 return -2;
1670
1671 // Now find the svar_T index in sn_var_vals.
1672 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1673 {
1674 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1675
1676 if (sv->sv_tv == &di->di_tv)
1677 {
1678 if (check_writable && sv->sv_const)
1679 semsg(_(e_readonlyvar), name);
1680 return idx;
1681 }
1682 }
1683 return -1;
1684}
1685
1686/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001687 * Find "name" in imported items of the current script or in "cctx" if not
1688 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689 */
1690 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001691find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001692{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001693 int idx;
1694
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001695 if (current_sctx.sc_sid <= 0)
1696 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697 if (cctx != NULL)
1698 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1699 {
1700 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1701 + idx;
1702
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001703 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1704 : STRLEN(import->imp_name) == len
1705 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001706 return import;
1707 }
1708
Bram Moolenaarefa94442020-08-08 22:16:00 +02001709 return find_imported_in_script(name, len, current_sctx.sc_sid);
1710}
1711
1712 imported_T *
1713find_imported_in_script(char_u *name, size_t len, int sid)
1714{
1715 scriptitem_T *si = SCRIPT_ITEM(sid);
1716 int idx;
1717
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001718 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1719 {
1720 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1721
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001722 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1723 : STRLEN(import->imp_name) == len
1724 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001725 return import;
1726 }
1727 return NULL;
1728}
1729
1730/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001731 * Free all imported variables.
1732 */
1733 static void
1734free_imported(cctx_T *cctx)
1735{
1736 int idx;
1737
1738 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1739 {
1740 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1741
1742 vim_free(import->imp_name);
1743 }
1744 ga_clear(&cctx->ctx_imports);
1745}
1746
1747/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02001748 * Return TRUE if "p" points at a "#" but not at "#{".
1749 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001750 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001751vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001752{
1753 return p[0] == '#' && p[1] != '{';
1754}
1755
1756/*
1757 * Return a pointer to the next line that isn't empty or only contains a
1758 * comment. Skips over white space.
1759 * Returns NULL if there is none.
1760 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001761 char_u *
1762peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001763{
1764 int lnum = cctx->ctx_lnum;
1765
1766 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
1767 {
1768 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001769 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001770
Bram Moolenaarba60cc42020-08-12 19:15:33 +02001771 // ignore NULLs inserted for continuation lines
1772 if (line != NULL)
1773 {
1774 p = skipwhite(line);
1775 if (*p != NUL && !vim9_comment_start(p))
1776 return p;
1777 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02001778 }
1779 return NULL;
1780}
1781
1782/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001783 * Called when checking for a following operator at "arg". When the rest of
1784 * the line is empty or only a comment, peek the next line. If there is a next
1785 * line return a pointer to it and set "nextp".
1786 * Otherwise skip over white space.
1787 */
1788 static char_u *
1789may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
1790{
1791 char_u *p = skipwhite(arg);
1792
1793 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001794 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001795 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001796 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001797 if (*nextp != NULL)
1798 return *nextp;
1799 }
1800 return p;
1801}
1802
1803/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02001804 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02001805 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02001806 * Returns NULL when at the end.
1807 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001808 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02001809next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02001810{
Bram Moolenaar7a092242020-04-16 22:10:49 +02001811 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001812
1813 do
1814 {
1815 ++cctx->ctx_lnum;
1816 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02001817 {
1818 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001819 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02001820 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02001821 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02001822 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001823 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001824 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02001825 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02001826 return line;
1827}
1828
1829/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001830 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02001831 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001832 * Return FAIL if beyond the last line, "*arg" is unmodified then.
1833 */
1834 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02001835may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001836{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001837 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001838 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02001839 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001840
1841 if (next == NULL)
1842 return FAIL;
1843 *arg = skipwhite(next);
1844 }
1845 return OK;
1846}
1847
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001848/*
1849 * Idem, and give an error when failed.
1850 */
1851 static int
1852may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
1853{
1854 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1855 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001856 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001857 return FAIL;
1858 }
1859 return OK;
1860}
1861
1862
Bram Moolenaara5565e42020-05-09 15:44:01 +02001863// Structure passed between the compile_expr* functions to keep track of
1864// constants that have been parsed but for which no code was produced yet. If
1865// possible expressions on these constants are applied at compile time. If
1866// that is not possible, the code to push the constants needs to be generated
1867// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02001868// Using 50 should be more than enough of 5 levels of ().
1869#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02001870typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02001871 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02001872 int pp_used; // active entries in pp_tv[]
1873} ppconst_T;
1874
Bram Moolenaar1c747212020-05-09 18:28:34 +02001875static int compile_expr0(char_u **arg, cctx_T *cctx);
1876static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1877
Bram Moolenaara5565e42020-05-09 15:44:01 +02001878/*
1879 * Generate a PUSH instruction for "tv".
1880 * "tv" will be consumed or cleared.
1881 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
1882 */
1883 static int
1884generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
1885{
1886 if (tv != NULL)
1887 {
1888 switch (tv->v_type)
1889 {
1890 case VAR_UNKNOWN:
1891 break;
1892 case VAR_BOOL:
1893 generate_PUSHBOOL(cctx, tv->vval.v_number);
1894 break;
1895 case VAR_SPECIAL:
1896 generate_PUSHSPEC(cctx, tv->vval.v_number);
1897 break;
1898 case VAR_NUMBER:
1899 generate_PUSHNR(cctx, tv->vval.v_number);
1900 break;
1901#ifdef FEAT_FLOAT
1902 case VAR_FLOAT:
1903 generate_PUSHF(cctx, tv->vval.v_float);
1904 break;
1905#endif
1906 case VAR_BLOB:
1907 generate_PUSHBLOB(cctx, tv->vval.v_blob);
1908 tv->vval.v_blob = NULL;
1909 break;
1910 case VAR_STRING:
1911 generate_PUSHS(cctx, tv->vval.v_string);
1912 tv->vval.v_string = NULL;
1913 break;
1914 default:
1915 iemsg("constant type not supported");
1916 clear_tv(tv);
1917 return FAIL;
1918 }
1919 tv->v_type = VAR_UNKNOWN;
1920 }
1921 return OK;
1922}
1923
1924/*
1925 * Generate code for any ppconst entries.
1926 */
1927 static int
1928generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
1929{
1930 int i;
1931 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001932 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001933
Bram Moolenaar9b68c822020-06-18 19:31:08 +02001934 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001935 for (i = 0; i < ppconst->pp_used; ++i)
1936 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
1937 ret = FAIL;
1938 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001939 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001940 return ret;
1941}
1942
1943/*
1944 * Clear ppconst constants. Used when failing.
1945 */
1946 static void
1947clear_ppconst(ppconst_T *ppconst)
1948{
1949 int i;
1950
1951 for (i = 0; i < ppconst->pp_used; ++i)
1952 clear_tv(&ppconst->pp_tv[i]);
1953 ppconst->pp_used = 0;
1954}
1955
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001956/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001957 * Generate an instruction to load script-local variable "name", without the
1958 * leading "s:".
1959 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001960 */
1961 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001962compile_load_scriptvar(
1963 cctx_T *cctx,
1964 char_u *name, // variable NUL terminated
1965 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001966 char_u **end, // end of variable
1967 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001969 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001970 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
1971 imported_T *import;
1972
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001973 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001974 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001975 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001976 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
1977 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978 }
1979 if (idx >= 0)
1980 {
1981 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1982
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001983 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001984 current_sctx.sc_sid, idx, sv->sv_type);
1985 return OK;
1986 }
1987
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001988 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001989 if (import != NULL)
1990 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001991 if (import->imp_all)
1992 {
1993 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02001994 char_u *exp_name;
1995 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001996 ufunc_T *ufunc;
1997 type_T *type;
1998
1999 // Used "import * as Name", need to lookup the member.
2000 if (*p != '.')
2001 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002002 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002003 return FAIL;
2004 }
2005 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002006 if (VIM_ISWHITE(*p))
2007 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002008 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002009 return FAIL;
2010 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002011
Bram Moolenaar1c991142020-07-04 13:15:31 +02002012 // isolate one name
2013 exp_name = p;
2014 while (eval_isnamec(*p))
2015 ++p;
2016 cc = *p;
2017 *p = NUL;
2018
2019 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2020 *p = cc;
2021 p = skipwhite(p);
2022
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002023 // TODO: what if it is a function?
2024 if (idx < 0)
2025 return FAIL;
2026 *end = p;
2027
2028 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2029 import->imp_sid,
2030 idx,
2031 type);
2032 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002033 else if (import->imp_funcname != NULL)
2034 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002035 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002036 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2037 import->imp_sid,
2038 import->imp_var_vals_idx,
2039 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040 return OK;
2041 }
2042
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002043 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002044 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002045 return FAIL;
2046}
2047
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002048 static int
2049generate_funcref(cctx_T *cctx, char_u *name)
2050{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002051 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002052
2053 if (ufunc == NULL)
2054 return FAIL;
2055
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002056 // Need to compile any default values to get the argument types.
2057 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2058 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2059 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002060 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002061}
2062
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002063/*
2064 * Compile a variable name into a load instruction.
2065 * "end" points to just after the name.
2066 * When "error" is FALSE do not give an error when not found.
2067 */
2068 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002069compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070{
2071 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002072 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002073 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002074 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002075 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002076
2077 if (*(*arg + 1) == ':')
2078 {
2079 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002080 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002081 {
2082 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002083
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002084 switch (**arg)
2085 {
2086 case 'g': isn_type = ISN_LOADGDICT; break;
2087 case 'w': isn_type = ISN_LOADWDICT; break;
2088 case 't': isn_type = ISN_LOADTDICT; break;
2089 case 'b': isn_type = ISN_LOADBDICT; break;
2090 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002091 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002092 goto theend;
2093 }
2094 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2095 goto theend;
2096 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002097 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002098 else
2099 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002100 isntype_T isn_type = ISN_DROP;
2101
2102 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2103 if (name == NULL)
2104 return FAIL;
2105
2106 switch (**arg)
2107 {
2108 case 'v': res = generate_LOADV(cctx, name, error);
2109 break;
2110 case 's': res = compile_load_scriptvar(cctx, name,
2111 NULL, NULL, error);
2112 break;
2113 case 'g': isn_type = ISN_LOADG; break;
2114 case 'w': isn_type = ISN_LOADW; break;
2115 case 't': isn_type = ISN_LOADT; break;
2116 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002117 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002118 goto theend;
2119 }
2120 if (isn_type != ISN_DROP)
2121 {
2122 // Global, Buffer-local, Window-local and Tabpage-local
2123 // variables can be defined later, thus we don't check if it
2124 // exists, give error at runtime.
2125 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2126 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002127 }
2128 }
2129 else
2130 {
2131 size_t len = end - *arg;
2132 int idx;
2133 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002134 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135
2136 name = vim_strnsave(*arg, end - *arg);
2137 if (name == NULL)
2138 return FAIL;
2139
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002140 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002141 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002142 if (!gen_load_outer)
2143 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002144 }
2145 else
2146 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002147 lvar_T *lvar = lookup_local(*arg, len, cctx);
2148
2149 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002150 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002151 type = lvar->lv_type;
2152 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002153 if (lvar->lv_from_outer)
2154 gen_load_outer = TRUE;
2155 else
2156 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002157 }
2158 else
2159 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002160 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002161 // already exists in a Vim9 script or when it's imported.
2162 if (lookup_script(*arg, len, TRUE) == OK
2163 || find_imported(name, 0, cctx) != NULL)
2164 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002165
Bram Moolenaara5565e42020-05-09 15:44:01 +02002166 // When the name starts with an uppercase letter or "x:" it
2167 // can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002168 // TODO: this is just guessing
Bram Moolenaara5565e42020-05-09 15:44:01 +02002169 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2170 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002171 }
2172 }
2173 if (gen_load)
2174 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002175 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002176 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002177 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002178 cctx->ctx_outer_used = TRUE;
2179 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002180 }
2181
2182 *arg = end;
2183
2184theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002185 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002186 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002187 vim_free(name);
2188 return res;
2189}
2190
2191/*
2192 * Compile the argument expressions.
2193 * "arg" points to just after the "(" and is advanced to after the ")"
2194 */
2195 static int
2196compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2197{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002198 char_u *p = *arg;
2199 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002200
Bram Moolenaare6085c52020-04-12 20:19:16 +02002201 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002202 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002203 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2204 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002205 if (*p == ')')
2206 {
2207 *arg = p + 1;
2208 return OK;
2209 }
2210
Bram Moolenaara5565e42020-05-09 15:44:01 +02002211 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212 return FAIL;
2213 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002214
2215 if (*p != ',' && *skipwhite(p) == ',')
2216 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002217 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002218 p = skipwhite(p);
2219 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002220 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002221 {
2222 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002223 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002224 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002225 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002226 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002227 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002228 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002229failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002230 emsg(_(e_missing_close));
2231 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002232}
2233
2234/*
2235 * Compile a function call: name(arg1, arg2)
2236 * "arg" points to "name", "arg + varlen" to the "(".
2237 * "argcount_init" is 1 for "value->method()"
2238 * Instructions:
2239 * EVAL arg1
2240 * EVAL arg2
2241 * BCALL / DCALL / UCALL
2242 */
2243 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002244compile_call(
2245 char_u **arg,
2246 size_t varlen,
2247 cctx_T *cctx,
2248 ppconst_T *ppconst,
2249 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002250{
2251 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002252 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253 int argcount = argcount_init;
2254 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002255 char_u fname_buf[FLEN_FIXED + 1];
2256 char_u *tofree = NULL;
2257 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002259 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002260 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002261
Bram Moolenaara5565e42020-05-09 15:44:01 +02002262 // we can evaluate "has('name')" at compile time
2263 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2264 {
2265 char_u *s = skipwhite(*arg + varlen + 1);
2266 typval_T argvars[2];
2267
2268 argvars[0].v_type = VAR_UNKNOWN;
2269 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002270 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002271 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002272 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002273 s = skipwhite(s);
2274 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2275 {
2276 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2277
2278 *arg = s + 1;
2279 argvars[1].v_type = VAR_UNKNOWN;
2280 tv->v_type = VAR_NUMBER;
2281 tv->vval.v_number = 0;
2282 f_has(argvars, tv);
2283 clear_tv(&argvars[0]);
2284 ++ppconst->pp_used;
2285 return OK;
2286 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002287 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002288 }
2289
2290 if (generate_ppconst(cctx, ppconst) == FAIL)
2291 return FAIL;
2292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002293 if (varlen >= sizeof(namebuf))
2294 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002295 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002296 return FAIL;
2297 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002298 vim_strncpy(namebuf, *arg, varlen);
2299 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002300
2301 *arg = skipwhite(*arg + varlen + 1);
2302 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002303 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002304
Bram Moolenaara1773442020-08-12 15:21:22 +02002305 is_autoload = vim_strchr(name, '#') != NULL;
2306 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002307 {
2308 int idx;
2309
2310 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002311 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002312 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002313 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002314 else
2315 semsg(_(e_unknownfunc), namebuf);
2316 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002317 }
2318
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002319 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002320 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002321 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002322 {
2323 res = generate_CALL(cctx, ufunc, argcount);
2324 goto theend;
2325 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002326
2327 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002328 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002329 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002330 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002331 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaara26b9702020-04-18 19:53:28 +02002332 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002333 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002334 garray_T *stack = &cctx->ctx_type_stack;
2335 type_T *type;
2336
2337 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2338 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002339 goto theend;
2340 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002341
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002342 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002343 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002344 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002345 res = generate_UCALL(cctx, name, argcount);
2346 else
2347 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002348
2349theend:
2350 vim_free(tofree);
2351 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002352}
2353
2354// like NAMESPACE_CHAR but with 'a' and 'l'.
2355#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2356
2357/*
2358 * Find the end of a variable or function name. Unlike find_name_end() this
2359 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002360 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002361 * Return a pointer to just after the name. Equal to "arg" if there is no
2362 * valid name.
2363 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002364 static char_u *
2365to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002366{
2367 char_u *p;
2368
2369 // Quick check for valid starting character.
2370 if (!eval_isnamec1(*arg))
2371 return arg;
2372
2373 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2374 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2375 // and can be used in slice "[n:]".
2376 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002377 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002378 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2379 break;
2380 return p;
2381}
2382
2383/*
2384 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002385 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002386 */
2387 char_u *
2388to_name_const_end(char_u *arg)
2389{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002390 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 typval_T rettv;
2392
2393 if (p == arg && *arg == '[')
2394 {
2395
2396 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002397 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002398 p = arg;
2399 }
2400 else if (p == arg && *arg == '#' && arg[1] == '{')
2401 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002402 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002404 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405 p = arg;
2406 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002407
2408 return p;
2409}
2410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002411/*
2412 * parse a list: [expr, expr]
2413 * "*arg" points to the '['.
2414 */
2415 static int
2416compile_list(char_u **arg, cctx_T *cctx)
2417{
2418 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002419 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002420 int count = 0;
2421
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002422 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002424 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002425 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002426 semsg(_(e_list_end), *arg);
2427 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002428 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002429 if (*p == ',')
2430 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002431 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002432 return FAIL;
2433 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002434 if (*p == ']')
2435 {
2436 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002437 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002438 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002439 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002440 break;
2441 ++count;
2442 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002443 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002444 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002445 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2446 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002447 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002448 return FAIL;
2449 }
2450 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002451 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002452 p = skipwhite(p);
2453 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002454 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002455
2456 generate_NEWLIST(cctx, count);
2457 return OK;
2458}
2459
2460/*
2461 * parse a lambda: {arg, arg -> expr}
2462 * "*arg" points to the '{'.
2463 */
2464 static int
2465compile_lambda(char_u **arg, cctx_T *cctx)
2466{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467 typval_T rettv;
2468 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002469 evalarg_T evalarg;
2470
2471 CLEAR_FIELD(evalarg);
2472 evalarg.eval_flags = EVAL_EVALUATE;
2473 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002474
2475 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002476 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002477 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002480 ++ufunc->uf_refcount;
2481 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002482 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483
2484 // The function will have one line: "return {expr}".
2485 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002486 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002488 clear_evalarg(&evalarg, NULL);
2489
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002490 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002491 {
2492 // The return type will now be known.
2493 set_function_type(ufunc);
2494
2495 return generate_FUNCREF(cctx, ufunc);
2496 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002497
2498 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002499 return FAIL;
2500}
2501
2502/*
2503 * Compile a lamda call: expr->{lambda}(args)
2504 * "arg" points to the "{".
2505 */
2506 static int
2507compile_lambda_call(char_u **arg, cctx_T *cctx)
2508{
2509 ufunc_T *ufunc;
2510 typval_T rettv;
2511 int argcount = 1;
2512 int ret = FAIL;
2513
2514 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002515 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516 return FAIL;
2517
2518 if (**arg != '(')
2519 {
2520 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002521 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522 else
2523 semsg(_(e_missing_paren), "lambda");
2524 clear_tv(&rettv);
2525 return FAIL;
2526 }
2527
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 ufunc = rettv.vval.v_partial->pt_func;
2529 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002530 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002531 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002532
2533 // The function will have one line: "return {expr}".
2534 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002535 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536
2537 // compile the arguments
2538 *arg = skipwhite(*arg + 1);
2539 if (compile_arguments(arg, cctx, &argcount) == OK)
2540 // call the compiled function
2541 ret = generate_CALL(cctx, ufunc, argcount);
2542
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002543 if (ret == FAIL)
2544 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 return ret;
2546}
2547
2548/*
2549 * parse a dict: {'key': val} or #{key: val}
2550 * "*arg" points to the '{'.
2551 */
2552 static int
2553compile_dict(char_u **arg, cctx_T *cctx, int literal)
2554{
2555 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002556 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002557 int count = 0;
2558 dict_T *d = dict_alloc();
2559 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002560 char_u *whitep = *arg;
2561 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002562
2563 if (d == NULL)
2564 return FAIL;
2565 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002566 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 {
2568 char_u *key = NULL;
2569
Bram Moolenaar23c55272020-06-21 16:58:13 +02002570 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002571 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002572 *arg = NULL;
2573 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002574 }
2575
2576 if (**arg == '}')
2577 break;
2578
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579 if (literal)
2580 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002581 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582
Bram Moolenaar2c330432020-04-13 14:41:35 +02002583 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002585 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586 return FAIL;
2587 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002588 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 if (generate_PUSHS(cctx, key) == FAIL)
2590 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002591 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002592 }
2593 else
2594 {
2595 isn_T *isn;
2596
Bram Moolenaara5565e42020-05-09 15:44:01 +02002597 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002598 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2600 if (isn->isn_type == ISN_PUSHS)
2601 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002602 else
2603 {
2604 type_T *keytype = ((type_T **)stack->ga_data)
2605 [stack->ga_len - 1];
2606 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2607 return FAIL;
2608 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609 }
2610
2611 // Check for duplicate keys, if using string keys.
2612 if (key != NULL)
2613 {
2614 item = dict_find(d, key, -1);
2615 if (item != NULL)
2616 {
2617 semsg(_(e_duplicate_key), key);
2618 goto failret;
2619 }
2620 item = dictitem_alloc(key);
2621 if (item != NULL)
2622 {
2623 item->di_tv.v_type = VAR_UNKNOWN;
2624 item->di_tv.v_lock = 0;
2625 if (dict_add(d, item) == FAIL)
2626 dictitem_free(item);
2627 }
2628 }
2629
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 if (**arg != ':')
2631 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002632 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002633 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002634 else
2635 semsg(_(e_missing_dict_colon), *arg);
2636 return FAIL;
2637 }
2638 whitep = *arg + 1;
2639 if (!IS_WHITE_OR_NUL(*whitep))
2640 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002641 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642 return FAIL;
2643 }
2644
2645 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002646 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002647 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002648 *arg = NULL;
2649 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002650 }
2651
Bram Moolenaara5565e42020-05-09 15:44:01 +02002652 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002653 return FAIL;
2654 ++count;
2655
Bram Moolenaar2c330432020-04-13 14:41:35 +02002656 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002657 *arg = skipwhite(*arg);
2658 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002659 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002660 *arg = NULL;
2661 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002662 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002663 if (**arg == '}')
2664 break;
2665 if (**arg != ',')
2666 {
2667 semsg(_(e_missing_dict_comma), *arg);
2668 goto failret;
2669 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002670 if (IS_WHITE_OR_NUL(*whitep))
2671 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002672 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002673 return FAIL;
2674 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002675 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676 *arg = skipwhite(*arg + 1);
2677 }
2678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002679 *arg = *arg + 1;
2680
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002681 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002682 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002683 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002684 *arg += STRLEN(*arg);
2685
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002686 dict_unref(d);
2687 return generate_NEWDICT(cctx, count);
2688
2689failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002690 if (*arg == NULL)
2691 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002692 dict_unref(d);
2693 return FAIL;
2694}
2695
2696/*
2697 * Compile "&option".
2698 */
2699 static int
2700compile_get_option(char_u **arg, cctx_T *cctx)
2701{
2702 typval_T rettv;
2703 char_u *start = *arg;
2704 int ret;
2705
2706 // parse the option and get the current value to get the type.
2707 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002708 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002709 if (ret == OK)
2710 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002711 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002712 char_u *name = vim_strnsave(start, *arg - start);
2713 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2714
2715 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2716 vim_free(name);
2717 }
2718 clear_tv(&rettv);
2719
2720 return ret;
2721}
2722
2723/*
2724 * Compile "$VAR".
2725 */
2726 static int
2727compile_get_env(char_u **arg, cctx_T *cctx)
2728{
2729 char_u *start = *arg;
2730 int len;
2731 int ret;
2732 char_u *name;
2733
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002734 ++*arg;
2735 len = get_env_len(arg);
2736 if (len == 0)
2737 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002738 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002739 return FAIL;
2740 }
2741
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002742 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002743 name = vim_strnsave(start, len + 1);
2744 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2745 vim_free(name);
2746 return ret;
2747}
2748
2749/*
2750 * Compile "@r".
2751 */
2752 static int
2753compile_get_register(char_u **arg, cctx_T *cctx)
2754{
2755 int ret;
2756
2757 ++*arg;
2758 if (**arg == NUL)
2759 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002760 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002761 return FAIL;
2762 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002763 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764 {
2765 emsg_invreg(**arg);
2766 return FAIL;
2767 }
2768 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2769 ++*arg;
2770 return ret;
2771}
2772
2773/*
2774 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002775 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776 */
2777 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002778apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002779{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002780 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781
2782 // this works from end to start
2783 while (p > start)
2784 {
2785 --p;
2786 if (*p == '-' || *p == '+')
2787 {
2788 // only '-' has an effect, for '+' we only check the type
2789#ifdef FEAT_FLOAT
2790 if (rettv->v_type == VAR_FLOAT)
2791 {
2792 if (*p == '-')
2793 rettv->vval.v_float = -rettv->vval.v_float;
2794 }
2795 else
2796#endif
2797 {
2798 varnumber_T val;
2799 int error = FALSE;
2800
2801 // tv_get_number_chk() accepts a string, but we don't want that
2802 // here
2803 if (check_not_string(rettv) == FAIL)
2804 return FAIL;
2805 val = tv_get_number_chk(rettv, &error);
2806 clear_tv(rettv);
2807 if (error)
2808 return FAIL;
2809 if (*p == '-')
2810 val = -val;
2811 rettv->v_type = VAR_NUMBER;
2812 rettv->vval.v_number = val;
2813 }
2814 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002815 else if (numeric_only)
2816 {
2817 ++p;
2818 break;
2819 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820 else
2821 {
2822 int v = tv2bool(rettv);
2823
2824 // '!' is permissive in the type.
2825 clear_tv(rettv);
2826 rettv->v_type = VAR_BOOL;
2827 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2828 }
2829 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002830 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 return OK;
2832}
2833
2834/*
2835 * Recognize v: variables that are constants and set "rettv".
2836 */
2837 static void
2838get_vim_constant(char_u **arg, typval_T *rettv)
2839{
2840 if (STRNCMP(*arg, "v:true", 6) == 0)
2841 {
2842 rettv->v_type = VAR_BOOL;
2843 rettv->vval.v_number = VVAL_TRUE;
2844 *arg += 6;
2845 }
2846 else if (STRNCMP(*arg, "v:false", 7) == 0)
2847 {
2848 rettv->v_type = VAR_BOOL;
2849 rettv->vval.v_number = VVAL_FALSE;
2850 *arg += 7;
2851 }
2852 else if (STRNCMP(*arg, "v:null", 6) == 0)
2853 {
2854 rettv->v_type = VAR_SPECIAL;
2855 rettv->vval.v_number = VVAL_NULL;
2856 *arg += 6;
2857 }
2858 else if (STRNCMP(*arg, "v:none", 6) == 0)
2859 {
2860 rettv->v_type = VAR_SPECIAL;
2861 rettv->vval.v_number = VVAL_NONE;
2862 *arg += 6;
2863 }
2864}
2865
Bram Moolenaar696ba232020-07-29 21:20:41 +02002866 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02002867get_compare_type(char_u *p, int *len, int *type_is)
2868{
2869 exptype_T type = EXPR_UNKNOWN;
2870 int i;
2871
2872 switch (p[0])
2873 {
2874 case '=': if (p[1] == '=')
2875 type = EXPR_EQUAL;
2876 else if (p[1] == '~')
2877 type = EXPR_MATCH;
2878 break;
2879 case '!': if (p[1] == '=')
2880 type = EXPR_NEQUAL;
2881 else if (p[1] == '~')
2882 type = EXPR_NOMATCH;
2883 break;
2884 case '>': if (p[1] != '=')
2885 {
2886 type = EXPR_GREATER;
2887 *len = 1;
2888 }
2889 else
2890 type = EXPR_GEQUAL;
2891 break;
2892 case '<': if (p[1] != '=')
2893 {
2894 type = EXPR_SMALLER;
2895 *len = 1;
2896 }
2897 else
2898 type = EXPR_SEQUAL;
2899 break;
2900 case 'i': if (p[1] == 's')
2901 {
2902 // "is" and "isnot"; but not a prefix of a name
2903 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2904 *len = 5;
2905 i = p[*len];
2906 if (!isalnum(i) && i != '_')
2907 {
2908 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2909 *type_is = TRUE;
2910 }
2911 }
2912 break;
2913 }
2914 return type;
2915}
2916
2917/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002919 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 */
2921 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002922compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002924 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925
2926 // this works from end to start
2927 while (p > start)
2928 {
2929 --p;
2930 if (*p == '-' || *p == '+')
2931 {
2932 int negate = *p == '-';
2933 isn_T *isn;
2934
2935 // TODO: check type
2936 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2937 {
2938 --p;
2939 if (*p == '-')
2940 negate = !negate;
2941 }
2942 // only '-' has an effect, for '+' we only check the type
2943 if (negate)
2944 isn = generate_instr(cctx, ISN_NEGATENR);
2945 else
2946 isn = generate_instr(cctx, ISN_CHECKNR);
2947 if (isn == NULL)
2948 return FAIL;
2949 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002950 else if (numeric_only)
2951 {
2952 ++p;
2953 break;
2954 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955 else
2956 {
2957 int invert = TRUE;
2958
2959 while (p > start && p[-1] == '!')
2960 {
2961 --p;
2962 invert = !invert;
2963 }
2964 if (generate_2BOOL(cctx, invert) == FAIL)
2965 return FAIL;
2966 }
2967 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002968 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 return OK;
2970}
2971
2972/*
2973 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002974 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 */
2976 static int
2977compile_subscript(
2978 char_u **arg,
2979 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002980 char_u *start_leader,
2981 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02002982 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002984 char_u *name_start = *end_leader;
2985
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 for (;;)
2987 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002988 char_u *p = skipwhite(*arg);
2989
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002990 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002991 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002992 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002993
2994 // If a following line starts with "->{" or "->X" advance to that
2995 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002996 // Also if a following line starts with ".x".
2997 if (next != NULL &&
2998 ((next[0] == '-' && next[1] == '>'
2999 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003000 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003001 {
3002 next = next_line_from_context(cctx, TRUE);
3003 if (next == NULL)
3004 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003005 *arg = next;
3006 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003007 }
3008 }
3009
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003010 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3011 // not a function call.
3012 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003014 garray_T *stack = &cctx->ctx_type_stack;
3015 type_T *type;
3016 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003018 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003019 return FAIL;
3020
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003022 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3023
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003024 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3026 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003027 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 return FAIL;
3029 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003030 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003032 char_u *pstart = p;
3033
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003034 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003035 return FAIL;
3036
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 // something->method()
3038 // Apply the '!', '-' and '+' first:
3039 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003040 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003043 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003044 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003045 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 if (**arg == '{')
3047 {
3048 // lambda call: list->{lambda}
3049 if (compile_lambda_call(arg, cctx) == FAIL)
3050 return FAIL;
3051 }
3052 else
3053 {
3054 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003055 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003056 if (!eval_isnamec1(*p))
3057 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003058 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003059 return FAIL;
3060 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003061 if (ASCII_ISALPHA(*p) && p[1] == ':')
3062 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003063 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 ;
3065 if (*p != '(')
3066 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003067 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 return FAIL;
3069 }
3070 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003071 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 return FAIL;
3073 }
3074 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003075 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003077 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003078 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003079 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003080 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003081 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003082
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003083 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003084 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003085 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003086 // TODO: blob index
3087 // TODO: more arguments
3088 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003089 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003090 return FAIL;
3091
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003092 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003093 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003094 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003095 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003096 if (**arg == ':')
3097 // missing first index is equal to zero
3098 generate_PUSHNR(cctx, 0);
3099 else
3100 {
3101 if (compile_expr0(arg, cctx) == FAIL)
3102 return FAIL;
3103 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3104 return FAIL;
3105 *arg = skipwhite(*arg);
3106 }
3107 if (**arg == ':')
3108 {
3109 *arg = skipwhite(*arg + 1);
3110 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3111 return FAIL;
3112 if (**arg == ']')
3113 // missing second index is equal to end of string
3114 generate_PUSHNR(cctx, -1);
3115 else
3116 {
3117 if (compile_expr0(arg, cctx) == FAIL)
3118 return FAIL;
3119 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3120 return FAIL;
3121 *arg = skipwhite(*arg);
3122 }
3123 is_slice = TRUE;
3124 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125
3126 if (**arg != ']')
3127 {
3128 emsg(_(e_missbrac));
3129 return FAIL;
3130 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003131 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003133 // We can index a list and a dict. If we don't know the type
3134 // we can use the index value type.
3135 // TODO: If we don't know use an instruction to figure it out at
3136 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003137 typep = ((type_T **)stack->ga_data) + stack->ga_len
3138 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003139 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003140 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3141 // If the index is a string, the variable must be a Dict.
3142 if (*typep == &t_any && valtype == &t_string)
3143 vtype = VAR_DICT;
3144 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003145 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003146 if (need_type(valtype, &t_number, -1, cctx, FALSE) == FAIL)
3147 return FAIL;
3148 if (is_slice)
3149 {
3150 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
3151 if (need_type(valtype, &t_number, -2, cctx, FALSE) == FAIL)
3152 return FAIL;
3153 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003154 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003155
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003156 if (vtype == VAR_DICT)
3157 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003158 if (is_slice)
3159 {
3160 emsg(_(e_cannot_slice_dictionary));
3161 return FAIL;
3162 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003163 if ((*typep)->tt_type == VAR_DICT)
3164 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003165 else
3166 {
3167 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3168 return FAIL;
3169 *typep = &t_any;
3170 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003171 if (may_generate_2STRING(-1, cctx) == FAIL)
3172 return FAIL;
3173 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3174 return FAIL;
3175 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003176 else if (vtype == VAR_STRING)
3177 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003178 *typep = &t_string;
3179 if ((is_slice
3180 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3181 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003182 return FAIL;
3183 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003184 else if (vtype == VAR_BLOB)
3185 {
3186 emsg("Sorry, blob index and slice not implemented yet");
3187 return FAIL;
3188 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003189 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003190 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003191 if (is_slice)
3192 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003193 if (generate_instr_drop(cctx,
3194 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3195 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003196 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003197 }
Bram Moolenaared591872020-08-15 22:14:53 +02003198 else
3199 {
3200 if ((*typep)->tt_type == VAR_LIST)
3201 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003202 if (generate_instr_drop(cctx,
3203 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3204 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003205 return FAIL;
3206 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003207 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003208 else
3209 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003210 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003211 return FAIL;
3212 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003214 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003216 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003217 return FAIL;
3218
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003219 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003220 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3221 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003223 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003224 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 while (eval_isnamec(*p))
3226 MB_PTR_ADV(p);
3227 if (p == *arg)
3228 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003229 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230 return FAIL;
3231 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003232 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233 return FAIL;
3234 *arg = p;
3235 }
3236 else
3237 break;
3238 }
3239
3240 // TODO - see handle_subscript():
3241 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3242 // Don't do this when "Func" is already a partial that was bound
3243 // explicitly (pt_auto is FALSE).
3244
3245 return OK;
3246}
3247
3248/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003249 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3250 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003252 * If the value is a constant "ppconst->pp_ret" will be set.
3253 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003254 *
3255 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 */
3257
3258/*
3259 * number number constant
3260 * 0zFFFFFFFF Blob constant
3261 * "string" string constant
3262 * 'string' literal string constant
3263 * &option-name option value
3264 * @r register contents
3265 * identifier variable value
3266 * function() function call
3267 * $VAR environment variable
3268 * (expression) nested expression
3269 * [expr, expr] List
3270 * {key: val, key: val} Dictionary
3271 * #{key: val, key: val} Dictionary with literal keys
3272 *
3273 * Also handle:
3274 * ! in front logical NOT
3275 * - in front unary minus
3276 * + in front unary plus (ignored)
3277 * trailing (arg) funcref/partial call
3278 * trailing [] subscript in String or List
3279 * trailing .name entry in Dictionary
3280 * trailing ->name() method call
3281 */
3282 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003283compile_expr7(
3284 char_u **arg,
3285 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003286 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003287{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003288 char_u *start_leader, *end_leader;
3289 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003290 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003291 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292
3293 /*
3294 * Skip '!', '-' and '+' characters. They are handled later.
3295 */
3296 start_leader = *arg;
3297 while (**arg == '!' || **arg == '-' || **arg == '+')
3298 *arg = skipwhite(*arg + 1);
3299 end_leader = *arg;
3300
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003301 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 switch (**arg)
3303 {
3304 /*
3305 * Number constant.
3306 */
3307 case '0': // also for blob starting with 0z
3308 case '1':
3309 case '2':
3310 case '3':
3311 case '4':
3312 case '5':
3313 case '6':
3314 case '7':
3315 case '8':
3316 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003317 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003319 // Apply "-" and "+" just before the number now, right to
3320 // left. Matters especially when "->" follows. Stops at
3321 // '!'.
3322 if (apply_leader(rettv, TRUE,
3323 start_leader, &end_leader) == FAIL)
3324 {
3325 clear_tv(rettv);
3326 return FAIL;
3327 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003328 break;
3329
3330 /*
3331 * String constant: "string".
3332 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003333 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003334 return FAIL;
3335 break;
3336
3337 /*
3338 * Literal string constant: 'str''ing'.
3339 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003340 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 return FAIL;
3342 break;
3343
3344 /*
3345 * Constant Vim variable.
3346 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003347 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348 ret = NOTDONE;
3349 break;
3350
3351 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003352 * "true" constant
3353 */
3354 case 't': if (STRNCMP(*arg, "true", 4) == 0
3355 && !eval_isnamec((*arg)[4]))
3356 {
3357 *arg += 4;
3358 rettv->v_type = VAR_BOOL;
3359 rettv->vval.v_number = VVAL_TRUE;
3360 }
3361 else
3362 ret = NOTDONE;
3363 break;
3364
3365 /*
3366 * "false" constant
3367 */
3368 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3369 && !eval_isnamec((*arg)[5]))
3370 {
3371 *arg += 5;
3372 rettv->v_type = VAR_BOOL;
3373 rettv->vval.v_number = VVAL_FALSE;
3374 }
3375 else
3376 ret = NOTDONE;
3377 break;
3378
3379 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380 * List: [expr, expr]
3381 */
3382 case '[': ret = compile_list(arg, cctx);
3383 break;
3384
3385 /*
3386 * Dictionary: #{key: val, key: val}
3387 */
3388 case '#': if ((*arg)[1] == '{')
3389 {
3390 ++*arg;
3391 ret = compile_dict(arg, cctx, TRUE);
3392 }
3393 else
3394 ret = NOTDONE;
3395 break;
3396
3397 /*
3398 * Lambda: {arg, arg -> expr}
3399 * Dictionary: {'key': val, 'key': val}
3400 */
3401 case '{': {
3402 char_u *start = skipwhite(*arg + 1);
3403
3404 // Find out what comes after the arguments.
3405 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003406 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407 if (ret != FAIL && *start == '>')
3408 ret = compile_lambda(arg, cctx);
3409 else
3410 ret = compile_dict(arg, cctx, FALSE);
3411 }
3412 break;
3413
3414 /*
3415 * Option value: &name
3416 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003417 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3418 return FAIL;
3419 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 break;
3421
3422 /*
3423 * Environment variable: $VAR.
3424 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003425 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3426 return FAIL;
3427 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428 break;
3429
3430 /*
3431 * Register contents: @r.
3432 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003433 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3434 return FAIL;
3435 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 break;
3437 /*
3438 * nested expression: (expression).
3439 */
3440 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003441
3442 // recursive!
3443 if (ppconst->pp_used <= PPSIZE - 10)
3444 {
3445 ret = compile_expr1(arg, cctx, ppconst);
3446 }
3447 else
3448 {
3449 // Not enough space in ppconst, flush constants.
3450 if (generate_ppconst(cctx, ppconst) == FAIL)
3451 return FAIL;
3452 ret = compile_expr0(arg, cctx);
3453 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454 *arg = skipwhite(*arg);
3455 if (**arg == ')')
3456 ++*arg;
3457 else if (ret == OK)
3458 {
3459 emsg(_(e_missing_close));
3460 ret = FAIL;
3461 }
3462 break;
3463
3464 default: ret = NOTDONE;
3465 break;
3466 }
3467 if (ret == FAIL)
3468 return FAIL;
3469
Bram Moolenaar1c747212020-05-09 18:28:34 +02003470 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003471 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003472 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003473 clear_tv(rettv);
3474 else
3475 // A constant expression can possibly be handled compile time,
3476 // return the value instead of generating code.
3477 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 }
3479 else if (ret == NOTDONE)
3480 {
3481 char_u *p;
3482 int r;
3483
3484 if (!eval_isnamec1(**arg))
3485 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003486 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 return FAIL;
3488 }
3489
3490 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003491 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003492 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003493 {
3494 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3495 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003497 {
3498 if (generate_ppconst(cctx, ppconst) == FAIL)
3499 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003501 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502 if (r == FAIL)
3503 return FAIL;
3504 }
3505
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003506 // Handle following "[]", ".member", etc.
3507 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003508 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003509 ppconst) == FAIL)
3510 return FAIL;
3511 if (ppconst->pp_used > 0)
3512 {
3513 // apply the '!', '-' and '+' before the constant
3514 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003515 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003516 return FAIL;
3517 return OK;
3518 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003519 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003521 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003522}
3523
3524/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003525 * Give the "white on both sides" error, taking the operator from "p[len]".
3526 */
3527 void
3528error_white_both(char_u *op, int len)
3529{
3530 char_u buf[10];
3531
3532 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003533 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003534}
3535
3536/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003537 * <type>expr7: runtime type check / conversion
3538 */
3539 static int
3540compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3541{
3542 type_T *want_type = NULL;
3543
3544 // Recognize <type>
3545 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3546 {
3547 int called_emsg_before = called_emsg;
3548
3549 ++*arg;
3550 want_type = parse_type(arg, cctx->ctx_type_list);
3551 if (called_emsg != called_emsg_before)
3552 return FAIL;
3553
3554 if (**arg != '>')
3555 {
3556 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003557 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003558 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003559 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003560 return FAIL;
3561 }
3562 ++*arg;
3563 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3564 return FAIL;
3565 }
3566
3567 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3568 return FAIL;
3569
3570 if (want_type != NULL)
3571 {
3572 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003573 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003574
Bram Moolenaard1103582020-08-14 22:44:25 +02003575 generate_ppconst(cctx, ppconst);
3576 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003577 if (check_type(want_type, actual, FALSE) == FAIL)
3578 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003579 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3580 return FAIL;
3581 }
3582 }
3583
3584 return OK;
3585}
3586
3587/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003588 * * number multiplication
3589 * / number division
3590 * % number modulo
3591 */
3592 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003593compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594{
3595 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003596 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003597 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003599 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003600 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601 return FAIL;
3602
3603 /*
3604 * Repeat computing, until no "*", "/" or "%" is following.
3605 */
3606 for (;;)
3607 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003608 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609 if (*op != '*' && *op != '/' && *op != '%')
3610 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003611 if (next != NULL)
3612 {
3613 *arg = next_line_from_context(cctx, TRUE);
3614 op = skipwhite(*arg);
3615 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003616
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003617 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003619 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003620 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 }
3622 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003623 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003624 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003626 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003627 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003629
3630 if (ppconst->pp_used == ppconst_used + 2
3631 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3632 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003633 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003634 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3635 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003636 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003638 // both are numbers: compute the result
3639 switch (*op)
3640 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003641 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003642 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003643 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003644 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003645 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003646 break;
3647 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003648 tv1->vval.v_number = res;
3649 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003650 }
3651 else
3652 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003653 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003654 generate_two_op(cctx, op);
3655 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656 }
3657
3658 return OK;
3659}
3660
3661/*
3662 * + number addition
3663 * - number subtraction
3664 * .. string concatenation
3665 */
3666 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003667compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668{
3669 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003670 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003672 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673
3674 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003675 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 return FAIL;
3677
3678 /*
3679 * Repeat computing, until no "+", "-" or ".." is following.
3680 */
3681 for (;;)
3682 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003683 op = may_peek_next_line(cctx, *arg, &next);
3684 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685 break;
3686 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003687 if (next != NULL)
3688 {
3689 *arg = next_line_from_context(cctx, TRUE);
3690 op = skipwhite(*arg);
3691 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003693 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003695 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003696 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 }
3698
3699 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003700 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003701 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003703 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003704 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 return FAIL;
3706
Bram Moolenaara5565e42020-05-09 15:44:01 +02003707 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003708 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003709 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3710 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3711 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3712 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003714 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3715 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003716
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003717 // concat/subtract/add constant numbers
3718 if (*op == '+')
3719 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3720 else if (*op == '-')
3721 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3722 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003723 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003724 // concatenate constant strings
3725 char_u *s1 = tv1->vval.v_string;
3726 char_u *s2 = tv2->vval.v_string;
3727 size_t len1 = STRLEN(s1);
3728
3729 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3730 if (tv1->vval.v_string == NULL)
3731 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003732 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003733 return FAIL;
3734 }
3735 mch_memmove(tv1->vval.v_string, s1, len1);
3736 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003737 vim_free(s1);
3738 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003739 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003740 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741 }
3742 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003743 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003744 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003745 if (*op == '.')
3746 {
3747 if (may_generate_2STRING(-2, cctx) == FAIL
3748 || may_generate_2STRING(-1, cctx) == FAIL)
3749 return FAIL;
3750 generate_instr_drop(cctx, ISN_CONCAT, 1);
3751 }
3752 else
3753 generate_two_op(cctx, op);
3754 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003755 }
3756
3757 return OK;
3758}
3759
3760/*
3761 * expr5a == expr5b
3762 * expr5a =~ expr5b
3763 * expr5a != expr5b
3764 * expr5a !~ expr5b
3765 * expr5a > expr5b
3766 * expr5a >= expr5b
3767 * expr5a < expr5b
3768 * expr5a <= expr5b
3769 * expr5a is expr5b
3770 * expr5a isnot expr5b
3771 *
3772 * Produces instructions:
3773 * EVAL expr5a Push result of "expr5a"
3774 * EVAL expr5b Push result of "expr5b"
3775 * COMPARE one of the compare instructions
3776 */
3777 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003778compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003779{
3780 exptype_T type = EXPR_UNKNOWN;
3781 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003782 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003785 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786
3787 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003788 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789 return FAIL;
3790
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003791 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003792 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003793
3794 /*
3795 * If there is a comparative operator, use it.
3796 */
3797 if (type != EXPR_UNKNOWN)
3798 {
3799 int ic = FALSE; // Default: do not ignore case
3800
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003801 if (next != NULL)
3802 {
3803 *arg = next_line_from_context(cctx, TRUE);
3804 p = skipwhite(*arg);
3805 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 if (type_is && (p[len] == '?' || p[len] == '#'))
3807 {
3808 semsg(_(e_invexpr2), *arg);
3809 return FAIL;
3810 }
3811 // extra question mark appended: ignore case
3812 if (p[len] == '?')
3813 {
3814 ic = TRUE;
3815 ++len;
3816 }
3817 // extra '#' appended: match case (ignored)
3818 else if (p[len] == '#')
3819 ++len;
3820 // nothing appended: match case
3821
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003822 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003824 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003825 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826 }
3827
3828 // get the second variable
3829 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003830 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003831 return FAIL;
3832
Bram Moolenaara5565e42020-05-09 15:44:01 +02003833 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 return FAIL;
3835
Bram Moolenaara5565e42020-05-09 15:44:01 +02003836 if (ppconst->pp_used == ppconst_used + 2)
3837 {
3838 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3839 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3840 int ret;
3841
3842 // Both sides are a constant, compute the result now.
3843 // First check for a valid combination of types, this is more
3844 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003845 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003846 ret = FAIL;
3847 else
3848 {
3849 ret = typval_compare(tv1, tv2, type, ic);
3850 tv1->v_type = VAR_BOOL;
3851 tv1->vval.v_number = tv1->vval.v_number
3852 ? VVAL_TRUE : VVAL_FALSE;
3853 clear_tv(tv2);
3854 --ppconst->pp_used;
3855 }
3856 return ret;
3857 }
3858
3859 generate_ppconst(cctx, ppconst);
3860 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861 }
3862
3863 return OK;
3864}
3865
Bram Moolenaar7f141552020-05-09 17:35:53 +02003866static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868/*
3869 * Compile || or &&.
3870 */
3871 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003872compile_and_or(
3873 char_u **arg,
3874 cctx_T *cctx,
3875 char *op,
3876 ppconst_T *ppconst,
3877 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003879 char_u *next;
3880 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003881 int opchar = *op;
3882
3883 if (p[0] == opchar && p[1] == opchar)
3884 {
3885 garray_T *instr = &cctx->ctx_instr;
3886 garray_T end_ga;
3887
3888 /*
3889 * Repeat until there is no following "||" or "&&"
3890 */
3891 ga_init2(&end_ga, sizeof(int), 10);
3892 while (p[0] == opchar && p[1] == opchar)
3893 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003894 if (next != NULL)
3895 {
3896 *arg = next_line_from_context(cctx, TRUE);
3897 p = skipwhite(*arg);
3898 }
3899
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003900 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3901 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003902 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003903 return FAIL;
3904 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905
Bram Moolenaara5565e42020-05-09 15:44:01 +02003906 // TODO: use ppconst if the value is a constant
3907 generate_ppconst(cctx, ppconst);
3908
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003909 if (ga_grow(&end_ga, 1) == FAIL)
3910 {
3911 ga_clear(&end_ga);
3912 return FAIL;
3913 }
3914 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3915 ++end_ga.ga_len;
3916 generate_JUMP(cctx, opchar == '|'
3917 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3918
3919 // eval the next expression
3920 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003921 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003922 return FAIL;
3923
Bram Moolenaara5565e42020-05-09 15:44:01 +02003924 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3925 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926 {
3927 ga_clear(&end_ga);
3928 return FAIL;
3929 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003930
3931 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003933 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934
3935 // Fill in the end label in all jumps.
3936 while (end_ga.ga_len > 0)
3937 {
3938 isn_T *isn;
3939
3940 --end_ga.ga_len;
3941 isn = ((isn_T *)instr->ga_data)
3942 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3943 isn->isn_arg.jump.jump_where = instr->ga_len;
3944 }
3945 ga_clear(&end_ga);
3946 }
3947
3948 return OK;
3949}
3950
3951/*
3952 * expr4a && expr4a && expr4a logical AND
3953 *
3954 * Produces instructions:
3955 * EVAL expr4a Push result of "expr4a"
3956 * JUMP_AND_KEEP_IF_FALSE end
3957 * EVAL expr4b Push result of "expr4b"
3958 * JUMP_AND_KEEP_IF_FALSE end
3959 * EVAL expr4c Push result of "expr4c"
3960 * end:
3961 */
3962 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003963compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003964{
Bram Moolenaara5565e42020-05-09 15:44:01 +02003965 int ppconst_used = ppconst->pp_used;
3966
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003967 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003968 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 return FAIL;
3970
3971 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02003972 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973}
3974
3975/*
3976 * expr3a || expr3b || expr3c logical OR
3977 *
3978 * Produces instructions:
3979 * EVAL expr3a Push result of "expr3a"
3980 * JUMP_AND_KEEP_IF_TRUE end
3981 * EVAL expr3b Push result of "expr3b"
3982 * JUMP_AND_KEEP_IF_TRUE end
3983 * EVAL expr3c Push result of "expr3c"
3984 * end:
3985 */
3986 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003987compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003988{
Bram Moolenaara5565e42020-05-09 15:44:01 +02003989 int ppconst_used = ppconst->pp_used;
3990
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003991 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003992 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003993 return FAIL;
3994
3995 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02003996 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003997}
3998
3999/*
4000 * Toplevel expression: expr2 ? expr1a : expr1b
4001 *
4002 * Produces instructions:
4003 * EVAL expr2 Push result of "expr"
4004 * JUMP_IF_FALSE alt jump if false
4005 * EVAL expr1a
4006 * JUMP_ALWAYS end
4007 * alt: EVAL expr1b
4008 * end:
4009 */
4010 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004011compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012{
4013 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004014 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004015 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016
Bram Moolenaar3988f642020-08-27 22:43:03 +02004017 // Ignore all kinds of errors when not producing code.
4018 if (cctx->ctx_skip == SKIP_YES)
4019 {
4020 skip_expr(arg);
4021 return OK;
4022 }
4023
Bram Moolenaar61a89812020-05-07 16:58:17 +02004024 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004025 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 return FAIL;
4027
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004028 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029 if (*p == '?')
4030 {
4031 garray_T *instr = &cctx->ctx_instr;
4032 garray_T *stack = &cctx->ctx_type_stack;
4033 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004034 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004035 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004036 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004037 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004038 int has_const_expr = FALSE;
4039 int const_value = FALSE;
4040 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004041
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004042 if (next != NULL)
4043 {
4044 *arg = next_line_from_context(cctx, TRUE);
4045 p = skipwhite(*arg);
4046 }
4047
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004048 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4049 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004050 semsg(_(e_white_space_required_before_and_after_str), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004051 return FAIL;
4052 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053
Bram Moolenaara5565e42020-05-09 15:44:01 +02004054 if (ppconst->pp_used == ppconst_used + 1)
4055 {
4056 // the condition is a constant, we know whether the ? or the :
4057 // expression is to be evaluated.
4058 has_const_expr = TRUE;
4059 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4060 clear_tv(&ppconst->pp_tv[ppconst_used]);
4061 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004062 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4063 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004064 }
4065 else
4066 {
4067 generate_ppconst(cctx, ppconst);
4068 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4069 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070
4071 // evaluate the second expression; any type is accepted
4072 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004073 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004074 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004075 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004076 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077
Bram Moolenaara5565e42020-05-09 15:44:01 +02004078 if (!has_const_expr)
4079 {
4080 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081
Bram Moolenaara5565e42020-05-09 15:44:01 +02004082 // remember the type and drop it
4083 --stack->ga_len;
4084 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004085
Bram Moolenaara5565e42020-05-09 15:44:01 +02004086 end_idx = instr->ga_len;
4087 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4088
4089 // jump here from JUMP_IF_FALSE
4090 isn = ((isn_T *)instr->ga_data) + alt_idx;
4091 isn->isn_arg.jump.jump_where = instr->ga_len;
4092 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093
4094 // Check for the ":".
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 emsg(_(e_missing_colon));
4099 return FAIL;
4100 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004101 if (next != NULL)
4102 {
4103 *arg = next_line_from_context(cctx, TRUE);
4104 p = skipwhite(*arg);
4105 }
4106
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004107 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4108 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004109 semsg(_(e_white_space_required_before_and_after_str), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004110 return FAIL;
4111 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112
4113 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004114 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004115 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4116 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004117 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004118 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004119 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004120 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004121 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122
Bram Moolenaara5565e42020-05-09 15:44:01 +02004123 if (!has_const_expr)
4124 {
4125 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126
Bram Moolenaara5565e42020-05-09 15:44:01 +02004127 // If the types differ, the result has a more generic type.
4128 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4129 common_type(type1, type2, &type2, cctx->ctx_type_list);
4130
4131 // jump here from JUMP_ALWAYS
4132 isn = ((isn_T *)instr->ga_data) + end_idx;
4133 isn->isn_arg.jump.jump_where = instr->ga_len;
4134 }
4135
4136 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137 }
4138 return OK;
4139}
4140
4141/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004142 * Toplevel expression.
4143 */
4144 static int
4145compile_expr0(char_u **arg, cctx_T *cctx)
4146{
4147 ppconst_T ppconst;
4148
4149 CLEAR_FIELD(ppconst);
4150 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4151 {
4152 clear_ppconst(&ppconst);
4153 return FAIL;
4154 }
4155 if (generate_ppconst(cctx, &ppconst) == FAIL)
4156 return FAIL;
4157 return OK;
4158}
4159
4160/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161 * compile "return [expr]"
4162 */
4163 static char_u *
4164compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4165{
4166 char_u *p = arg;
4167 garray_T *stack = &cctx->ctx_type_stack;
4168 type_T *stack_type;
4169
4170 if (*p != NUL && *p != '|' && *p != '\n')
4171 {
4172 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004173 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004174 return NULL;
4175
4176 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4177 if (set_return_type)
4178 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004179 else
4180 {
4181 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4182 && stack_type->tt_type != VAR_VOID
4183 && stack_type->tt_type != VAR_UNKNOWN)
4184 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004185 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004186 return NULL;
4187 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004188 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4189 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004190 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004191 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004192 }
4193 else
4194 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004195 // "set_return_type" cannot be TRUE, only used for a lambda which
4196 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004197 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4198 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004199 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004200 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201 return NULL;
4202 }
4203
4204 // No argument, return zero.
4205 generate_PUSHNR(cctx, 0);
4206 }
4207
4208 if (generate_instr(cctx, ISN_RETURN) == NULL)
4209 return NULL;
4210
4211 // "return val | endif" is possible
4212 return skipwhite(p);
4213}
4214
4215/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004216 * Get a line from the compilation context, compatible with exarg_T getline().
4217 * Return a pointer to the line in allocated memory.
4218 * Return NULL for end-of-file or some error.
4219 */
4220 static char_u *
4221exarg_getline(
4222 int c UNUSED,
4223 void *cookie,
4224 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004225 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004226{
4227 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004228 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004229
Bram Moolenaar66250c92020-08-20 15:02:42 +02004230 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004231 {
Bram Moolenaar66250c92020-08-20 15:02:42 +02004232 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4233 return NULL;
4234 ++cctx->ctx_lnum;
4235 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4236 // Comment lines result in NULL pointers, skip them.
4237 if (p != NULL)
4238 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004239 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004240}
4241
4242/*
4243 * Compile a nested :def command.
4244 */
4245 static char_u *
4246compile_nested_function(exarg_T *eap, cctx_T *cctx)
4247{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004248 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004249 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004250 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004251 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004252 lvar_T *lvar;
4253 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004254 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004255
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004256 // Only g:Func() can use a namespace.
4257 if (name_start[1] == ':' && !is_global)
4258 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004259 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004260 return NULL;
4261 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004262 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4263 return NULL;
4264
Bram Moolenaar04b12692020-05-04 23:24:44 +02004265 eap->arg = name_end;
4266 eap->getline = exarg_getline;
4267 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004268 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004269 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004270 lambda_name = get_lambda_name();
4271 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004272
Bram Moolenaar822ba242020-05-24 23:00:18 +02004273 if (ufunc == NULL)
4274 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004275 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004276 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004277 return NULL;
4278
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004279 if (is_global)
4280 {
4281 char_u *func_name = vim_strnsave(name_start + 2,
4282 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004283
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004284 if (func_name == NULL)
4285 r = FAIL;
4286 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004287 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004288 }
4289 else
4290 {
4291 // Define a local variable for the function reference.
4292 lvar = reserve_local(cctx, name_start, name_end - name_start,
4293 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004294 if (lvar == NULL)
4295 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004296 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004297 return NULL;
4298 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4299 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004300
Bram Moolenaar61a89812020-05-07 16:58:17 +02004301 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004302 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004303}
4304
4305/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004306 * Return the length of an assignment operator, or zero if there isn't one.
4307 */
4308 int
4309assignment_len(char_u *p, int *heredoc)
4310{
4311 if (*p == '=')
4312 {
4313 if (p[1] == '<' && p[2] == '<')
4314 {
4315 *heredoc = TRUE;
4316 return 3;
4317 }
4318 return 1;
4319 }
4320 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4321 return 2;
4322 if (STRNCMP(p, "..=", 3) == 0)
4323 return 3;
4324 return 0;
4325}
4326
4327// words that cannot be used as a variable
4328static char *reserved[] = {
4329 "true",
4330 "false",
4331 NULL
4332};
4333
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004334typedef enum {
4335 dest_local,
4336 dest_option,
4337 dest_env,
4338 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004339 dest_buffer,
4340 dest_window,
4341 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004342 dest_vimvar,
4343 dest_script,
4344 dest_reg,
4345} assign_dest_T;
4346
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004348 * Generate the load instruction for "name".
4349 */
4350 static void
4351generate_loadvar(
4352 cctx_T *cctx,
4353 assign_dest_T dest,
4354 char_u *name,
4355 lvar_T *lvar,
4356 type_T *type)
4357{
4358 switch (dest)
4359 {
4360 case dest_option:
4361 // TODO: check the option exists
4362 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4363 break;
4364 case dest_global:
4365 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4366 break;
4367 case dest_buffer:
4368 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4369 break;
4370 case dest_window:
4371 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4372 break;
4373 case dest_tab:
4374 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4375 break;
4376 case dest_script:
4377 compile_load_scriptvar(cctx,
4378 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4379 break;
4380 case dest_env:
4381 // Include $ in the name here
4382 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4383 break;
4384 case dest_reg:
4385 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4386 break;
4387 case dest_vimvar:
4388 generate_LOADV(cctx, name + 2, TRUE);
4389 break;
4390 case dest_local:
4391 if (lvar->lv_from_outer)
4392 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4393 NULL, type);
4394 else
4395 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4396 break;
4397 }
4398}
4399
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004400 void
4401vim9_declare_error(char_u *name)
4402{
4403 char *scope = "";
4404
4405 switch (*name)
4406 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004407 case 'g': scope = _("global"); break;
4408 case 'b': scope = _("buffer"); break;
4409 case 'w': scope = _("window"); break;
4410 case 't': scope = _("tab"); break;
4411 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004412 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004413 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004414 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004415 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004416 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004417 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004418 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004419 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004420 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004421}
4422
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004423/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004424 * Compile declaration and assignment:
4425 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004426 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004427 * Return NULL for an error.
4428 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429 */
4430 static char_u *
4431compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4432{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004433 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004434 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004435 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004436 char_u *ret = NULL;
4437 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004438 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004439 int scriptvar_sid = 0;
4440 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004443 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004444 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 int oplen = 0;
4446 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004447 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004448 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004449 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004452
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004453 // Skip over the "var" or "[var, var]" to get to any "=".
4454 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4455 if (p == NULL)
4456 return *arg == '[' ? arg : NULL;
4457
4458 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004460 // TODO: should we allow this, and figure out type inference from list
4461 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004462 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004463 return NULL;
4464 }
4465
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466 sp = p;
4467 p = skipwhite(p);
4468 op = p;
4469 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004470
4471 if (var_count > 0 && oplen == 0)
4472 // can be something like "[1, 2]->func()"
4473 return arg;
4474
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004475 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4476 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004477 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004478 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004479 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004480
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481 if (heredoc)
4482 {
4483 list_T *l;
4484 listitem_T *li;
4485
4486 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004487 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004489 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490
4491 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004492 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004493 {
4494 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4495 li->li_tv.vval.v_string = NULL;
4496 }
4497 generate_NEWLIST(cctx, l->lv_len);
4498 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004499 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004500 list_free(l);
4501 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004502 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004504 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004505 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004506 // for "[var, var] = expr" evaluate the expression here, loop over the
4507 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004508
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004509 p = skipwhite(op + oplen);
4510 if (compile_expr0(&p, cctx) == FAIL)
4511 return NULL;
4512 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004513
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004514 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004515 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004516 type_T *stacktype;
4517
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004518 stacktype = stack->ga_len == 0 ? &t_void
4519 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004520 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004521 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004522 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004523 goto theend;
4524 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004525 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004526 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004527 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004528 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4529 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004530 }
4531 }
4532
4533 /*
4534 * Loop over variables in "[var, var] = expr".
4535 * For "var = expr" and "let var: type" this is done only once.
4536 */
4537 if (var_count > 0)
4538 var_start = skipwhite(arg + 1); // skip over the "["
4539 else
4540 var_start = arg;
4541 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4542 {
4543 char_u *var_end = skip_var_one(var_start, FALSE);
4544 size_t varlen;
4545 int new_local = FALSE;
4546 int opt_type;
4547 int opt_flags = 0;
4548 assign_dest_T dest = dest_local;
4549 int vimvaridx = -1;
4550 lvar_T *lvar = NULL;
4551 lvar_T arg_lvar;
4552 int has_type = FALSE;
4553 int has_index = FALSE;
4554 int instr_count = -1;
4555
Bram Moolenaar65821722020-08-02 18:58:54 +02004556 if (*var_start == '@')
4557 p = var_start + 2;
4558 else
4559 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02004560 // skip over the leading "&", "&l:", "&g:" and "$"
4561 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02004562 p = to_name_end(p, TRUE);
4563 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004564
4565 // "a: type" is declaring variable "a" with a type, not "a:".
4566 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4567 --var_end;
4568 if (is_decl && p == var_start + 2 && p[-1] == ':')
4569 --p;
4570
4571 varlen = p - var_start;
4572 vim_free(name);
4573 name = vim_strnsave(var_start, varlen);
4574 if (name == NULL)
4575 return NULL;
4576 if (!heredoc)
4577 type = &t_any;
4578
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004579 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004580 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004581 int declare_error = FALSE;
4582
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004583 if (*var_start == '&')
4584 {
4585 int cc;
4586 long numval;
4587
4588 dest = dest_option;
4589 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004590 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004591 emsg(_(e_const_option));
4592 goto theend;
4593 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004594 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004595 p = var_start;
4596 p = find_option_end(&p, &opt_flags);
4597 if (p == NULL)
4598 {
4599 // cannot happen?
4600 emsg(_(e_letunexp));
4601 goto theend;
4602 }
4603 cc = *p;
4604 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02004605 opt_type = get_option_value(skip_option_env_lead(var_start),
4606 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004607 *p = cc;
4608 if (opt_type == -3)
4609 {
4610 semsg(_(e_unknown_option), var_start);
4611 goto theend;
4612 }
4613 if (opt_type == -2 || opt_type == 0)
4614 type = &t_string;
4615 else
4616 type = &t_number; // both number and boolean option
4617 }
4618 else if (*var_start == '$')
4619 {
4620 dest = dest_env;
4621 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004622 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004623 }
4624 else if (*var_start == '@')
4625 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004626 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004627 {
4628 emsg_invreg(var_start[1]);
4629 goto theend;
4630 }
4631 dest = dest_reg;
4632 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004633 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004634 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004635 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004636 {
4637 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004638 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004639 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004640 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004641 {
4642 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004643 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004644 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004645 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004646 {
4647 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004648 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004649 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004650 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004651 {
4652 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004653 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004654 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004655 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004656 {
4657 typval_T *vtv;
4658 int di_flags;
4659
4660 vimvaridx = find_vim_var(name + 2, &di_flags);
4661 if (vimvaridx < 0)
4662 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004663 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004664 goto theend;
4665 }
4666 // We use the current value of "sandbox" here, is that OK?
4667 if (var_check_ro(di_flags, name, FALSE))
4668 goto theend;
4669 dest = dest_vimvar;
4670 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004671 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004672 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004673 }
4674 else
4675 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004676 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004677
4678 for (idx = 0; reserved[idx] != NULL; ++idx)
4679 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004680 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004681 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004682 goto theend;
4683 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004684
4685 lvar = lookup_local(var_start, varlen, cctx);
4686 if (lvar == NULL)
4687 {
4688 CLEAR_FIELD(arg_lvar);
4689 if (lookup_arg(var_start, varlen,
4690 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4691 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004692 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004693 if (is_decl)
4694 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004695 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004696 goto theend;
4697 }
4698 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004699 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004700 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004701 if (lvar != NULL)
4702 {
4703 if (is_decl)
4704 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004705 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004706 goto theend;
4707 }
4708 else if (lvar->lv_const)
4709 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004710 semsg(_(e_cannot_assign_to_constant), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004711 goto theend;
4712 }
4713 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004714 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004715 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004716 int script_namespace = varlen > 1
4717 && STRNCMP(var_start, "s:", 2) == 0;
4718 int script_var = (script_namespace
Bram Moolenaar53900992020-08-22 19:02:02 +02004719 ? lookup_script(var_start + 2, varlen - 2, FALSE)
4720 : lookup_script(var_start, varlen, TRUE)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004721 imported_T *import =
4722 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004723
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004724 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004725 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004726 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4727
4728 if (is_decl)
4729 {
4730 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004731 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004732 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004733 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004734 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004735 name);
4736 goto theend;
4737 }
4738 else if (cctx->ctx_ufunc->uf_script_ctx_version
4739 == SCRIPT_VERSION_VIM9
4740 && script_namespace
4741 && !script_var && import == NULL)
4742 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004743 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004744 goto theend;
4745 }
4746
4747 dest = dest_script;
4748
4749 // existing script-local variables should have a type
4750 scriptvar_sid = current_sctx.sc_sid;
4751 if (import != NULL)
4752 scriptvar_sid = import->imp_sid;
4753 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4754 rawname, TRUE);
4755 if (scriptvar_idx >= 0)
4756 {
4757 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4758 svar_T *sv =
4759 ((svar_T *)si->sn_var_vals.ga_data)
4760 + scriptvar_idx;
4761 type = sv->sv_type;
4762 }
4763 }
4764 else if (name[1] == ':' && name[2] != NUL)
4765 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004766 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004767 goto theend;
4768 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004769 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004770 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004771 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004772 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004773 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004774 else if (check_defined(var_start, varlen, cctx) == FAIL)
4775 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004776 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004777 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004778
4779 if (declare_error)
4780 {
4781 vim9_declare_error(name);
4782 goto theend;
4783 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004784 }
4785
4786 // handle "a:name" as a name, not index "name" on "a"
4787 if (varlen > 1 || var_start[varlen] != ':')
4788 p = var_end;
4789
4790 if (dest != dest_option)
4791 {
4792 if (is_decl && *p == ':')
4793 {
4794 // parse optional type: "let var: type = expr"
4795 if (!VIM_ISWHITE(p[1]))
4796 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004797 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004798 goto theend;
4799 }
4800 p = skipwhite(p + 1);
4801 type = parse_type(&p, cctx->ctx_type_list);
4802 has_type = TRUE;
4803 }
4804 else if (lvar != NULL)
4805 type = lvar->lv_type;
4806 }
4807
4808 if (oplen == 3 && !heredoc && dest != dest_global
4809 && type->tt_type != VAR_STRING
4810 && type->tt_type != VAR_ANY)
4811 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004812 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004813 goto theend;
4814 }
4815
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004816 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004817 {
4818 if (oplen > 1 && !heredoc)
4819 {
4820 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004821 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004822 goto theend;
4823 }
4824
4825 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004826 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
4827 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004828 goto theend;
4829 lvar = reserve_local(cctx, var_start, varlen,
4830 cmdidx == CMD_const, type);
4831 if (lvar == NULL)
4832 goto theend;
4833 new_local = TRUE;
4834 }
4835
4836 member_type = type;
4837 if (var_end > var_start + varlen)
4838 {
4839 // Something follows after the variable: "var[idx]".
4840 if (is_decl)
4841 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004842 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004843 goto theend;
4844 }
4845
4846 if (var_start[varlen] == '[')
4847 {
4848 has_index = TRUE;
4849 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004850 member_type = &t_any;
4851 else
4852 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004853 }
4854 else
4855 {
4856 semsg("Not supported yet: %s", var_start);
4857 goto theend;
4858 }
4859 }
4860 else if (lvar == &arg_lvar)
4861 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004862 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004863 goto theend;
4864 }
4865
4866 if (!heredoc)
4867 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004868 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004869 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004870 if (oplen > 0 && var_count == 0)
4871 {
4872 // skip over the "=" and the expression
4873 p = skipwhite(op + oplen);
4874 compile_expr0(&p, cctx);
4875 }
4876 }
4877 else if (oplen > 0)
4878 {
4879 type_T *stacktype;
4880
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004881 // For "var = expr" evaluate the expression.
4882 if (var_count == 0)
4883 {
4884 int r;
4885
4886 // for "+=", "*=", "..=" etc. first load the current value
4887 if (*op != '=')
4888 {
4889 generate_loadvar(cctx, dest, name, lvar, type);
4890
4891 if (has_index)
4892 {
4893 // TODO: get member from list or dict
4894 emsg("Index with operation not supported yet");
4895 goto theend;
4896 }
4897 }
4898
4899 // Compile the expression. Temporarily hide the new local
4900 // variable here, it is not available to this expression.
4901 if (new_local)
4902 --cctx->ctx_locals.ga_len;
4903 instr_count = instr->ga_len;
4904 p = skipwhite(op + oplen);
4905 r = compile_expr0(&p, cctx);
4906 if (new_local)
4907 ++cctx->ctx_locals.ga_len;
4908 if (r == FAIL)
4909 goto theend;
4910 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004911 else if (semicolon && var_idx == var_count - 1)
4912 {
4913 // For "[var; var] = expr" get the rest of the list
4914 if (generate_SLICE(cctx, var_count - 1) == FAIL)
4915 goto theend;
4916 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004917 else
4918 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004919 // For "[var, var] = expr" get the "var_idx" item from the
4920 // list.
4921 if (generate_GETITEM(cctx, var_idx) == FAIL)
4922 return FAIL;
4923 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004924
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004925 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004926 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004927 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004928 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004929 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004930 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004931 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004932 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004933 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004934 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004935 }
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004936 else if ((stacktype->tt_type == VAR_FUNC
4937 || stacktype->tt_type == VAR_PARTIAL)
4938 && var_wrong_func_name(name, TRUE))
4939 {
4940 goto theend;
4941 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004942 else
4943 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004944 // An empty list or dict has a &t_void member,
4945 // for a variable that implies &t_any.
4946 if (stacktype == &t_list_empty)
4947 lvar->lv_type = &t_list_any;
4948 else if (stacktype == &t_dict_empty)
4949 lvar->lv_type = &t_dict_any;
4950 else
4951 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004952 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004953 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02004954 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004955 {
4956 type_T *use_type = lvar->lv_type;
4957
Bram Moolenaar93ad1472020-08-19 22:02:41 +02004958 // without operator type is here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004959 if (has_index)
4960 {
4961 use_type = use_type->tt_member;
4962 if (use_type == NULL)
4963 use_type = &t_void;
4964 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004965 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar93ad1472020-08-19 22:02:41 +02004966 == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004967 goto theend;
4968 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004969 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004970 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004971 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004972 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004973 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004974 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004975 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004976 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004977 goto theend;
4978 }
4979 else if (!has_type || dest == dest_option)
4980 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004981 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004982 goto theend;
4983 }
4984 else
4985 {
4986 // variables are always initialized
4987 if (ga_grow(instr, 1) == FAIL)
4988 goto theend;
4989 switch (member_type->tt_type)
4990 {
4991 case VAR_BOOL:
4992 generate_PUSHBOOL(cctx, VVAL_FALSE);
4993 break;
4994 case VAR_FLOAT:
4995#ifdef FEAT_FLOAT
4996 generate_PUSHF(cctx, 0.0);
4997#endif
4998 break;
4999 case VAR_STRING:
5000 generate_PUSHS(cctx, NULL);
5001 break;
5002 case VAR_BLOB:
5003 generate_PUSHBLOB(cctx, NULL);
5004 break;
5005 case VAR_FUNC:
5006 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5007 break;
5008 case VAR_LIST:
5009 generate_NEWLIST(cctx, 0);
5010 break;
5011 case VAR_DICT:
5012 generate_NEWDICT(cctx, 0);
5013 break;
5014 case VAR_JOB:
5015 generate_PUSHJOB(cctx, NULL);
5016 break;
5017 case VAR_CHANNEL:
5018 generate_PUSHCHANNEL(cctx, NULL);
5019 break;
5020 case VAR_NUMBER:
5021 case VAR_UNKNOWN:
5022 case VAR_ANY:
5023 case VAR_PARTIAL:
5024 case VAR_VOID:
5025 case VAR_SPECIAL: // cannot happen
5026 generate_PUSHNR(cctx, 0);
5027 break;
5028 }
5029 }
5030 if (var_count == 0)
5031 end = p;
5032 }
5033
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005034 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005035 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005036 break;
5037
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005038 if (oplen > 0 && *op != '=')
5039 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005040 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005041 type_T *stacktype;
5042
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005043 if (*op == '.')
5044 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005045 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005046 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005047 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005048 if (
5049#ifdef FEAT_FLOAT
5050 // If variable is float operation with number is OK.
5051 !(expected == &t_float && stacktype == &t_number) &&
5052#endif
5053 need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005054 goto theend;
5055
5056 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005057 {
5058 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5059 goto theend;
5060 }
5061 else if (*op == '+')
5062 {
5063 if (generate_add_instr(cctx,
5064 operator_type(member_type, stacktype),
5065 member_type, stacktype) == FAIL)
5066 goto theend;
5067 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005068 else if (generate_two_op(cctx, op) == FAIL)
5069 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005070 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005071
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005072 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005073 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005074 int r;
5075
5076 // Compile the "idx" in "var[idx]".
5077 if (new_local)
5078 --cctx->ctx_locals.ga_len;
5079 p = skipwhite(var_start + varlen + 1);
5080 r = compile_expr0(&p, cctx);
5081 if (new_local)
5082 ++cctx->ctx_locals.ga_len;
5083 if (r == FAIL)
5084 goto theend;
5085 if (*skipwhite(p) != ']')
5086 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005087 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005088 emsg(_(e_missbrac));
5089 goto theend;
5090 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005091 if (type == &t_any)
5092 {
5093 type_T *idx_type = ((type_T **)stack->ga_data)[
5094 stack->ga_len - 1];
5095 // Index on variable of unknown type: guess the type from the
5096 // index type: number is dict, otherwise dict.
5097 // TODO: should do the assignment at runtime
5098 if (idx_type->tt_type == VAR_NUMBER)
5099 type = &t_list_any;
5100 else
5101 type = &t_dict_any;
5102 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005103 if (type->tt_type == VAR_DICT
5104 && may_generate_2STRING(-1, cctx) == FAIL)
5105 goto theend;
5106 if (type->tt_type == VAR_LIST
5107 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005108 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005109 {
5110 emsg(_(e_number_exp));
5111 goto theend;
5112 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005113
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005114 // Load the dict or list. On the stack we then have:
5115 // - value
5116 // - index
5117 // - variable
5118 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005119
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005120 if (type->tt_type == VAR_LIST)
5121 {
5122 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5123 return FAIL;
5124 }
5125 else if (type->tt_type == VAR_DICT)
5126 {
5127 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5128 return FAIL;
5129 }
5130 else
5131 {
5132 emsg(_(e_listreq));
5133 goto theend;
5134 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005135 }
5136 else
5137 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005138 switch (dest)
5139 {
5140 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005141 generate_STOREOPT(cctx, skip_option_env_lead(name),
5142 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005143 break;
5144 case dest_global:
5145 // include g: with the name, easier to execute that way
5146 generate_STORE(cctx, ISN_STOREG, 0, name);
5147 break;
5148 case dest_buffer:
5149 // include b: with the name, easier to execute that way
5150 generate_STORE(cctx, ISN_STOREB, 0, name);
5151 break;
5152 case dest_window:
5153 // include w: with the name, easier to execute that way
5154 generate_STORE(cctx, ISN_STOREW, 0, name);
5155 break;
5156 case dest_tab:
5157 // include t: with the name, easier to execute that way
5158 generate_STORE(cctx, ISN_STORET, 0, name);
5159 break;
5160 case dest_env:
5161 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5162 break;
5163 case dest_reg:
5164 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5165 break;
5166 case dest_vimvar:
5167 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5168 break;
5169 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005170 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005171 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005172 {
5173 char_u *name_s = name;
5174
5175 // Include s: in the name for store_var()
5176 if (name[1] != ':')
5177 {
5178 int len = (int)STRLEN(name) + 3;
5179
5180 name_s = alloc(len);
5181 if (name_s == NULL)
5182 name_s = name;
5183 else
5184 vim_snprintf((char *)name_s, len,
5185 "s:%s", name);
5186 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005187 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5188 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005189 if (name_s != name)
5190 vim_free(name_s);
5191 }
5192 else
5193 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005194 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005195 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005196 break;
5197 case dest_local:
5198 if (lvar != NULL)
5199 {
5200 isn_T *isn = ((isn_T *)instr->ga_data)
5201 + instr->ga_len - 1;
5202
5203 // optimization: turn "var = 123" from ISN_PUSHNR +
5204 // ISN_STORE into ISN_STORENR
5205 if (!lvar->lv_from_outer
5206 && instr->ga_len == instr_count + 1
5207 && isn->isn_type == ISN_PUSHNR)
5208 {
5209 varnumber_T val = isn->isn_arg.number;
5210
5211 isn->isn_type = ISN_STORENR;
5212 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5213 isn->isn_arg.storenr.stnr_val = val;
5214 if (stack->ga_len > 0)
5215 --stack->ga_len;
5216 }
5217 else if (lvar->lv_from_outer)
5218 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005219 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005220 else
5221 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5222 }
5223 break;
5224 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005225 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005226
5227 if (var_idx + 1 < var_count)
5228 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005229 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005230
5231 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005232 if (var_count > 0 && !semicolon)
5233 {
5234 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5235 goto theend;
5236 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005237
Bram Moolenaarb2097502020-07-19 17:17:02 +02005238 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005239
5240theend:
5241 vim_free(name);
5242 return ret;
5243}
5244
5245/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005246 * Check if "name" can be "unlet".
5247 */
5248 int
5249check_vim9_unlet(char_u *name)
5250{
5251 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5252 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005253 // "unlet s:var" is allowed in legacy script.
5254 if (*name == 's' && !script_is_vim9())
5255 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005256 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005257 return FAIL;
5258 }
5259 return OK;
5260}
5261
5262/*
5263 * Callback passed to ex_unletlock().
5264 */
5265 static int
5266compile_unlet(
5267 lval_T *lvp,
5268 char_u *name_end,
5269 exarg_T *eap,
5270 int deep UNUSED,
5271 void *coookie)
5272{
5273 cctx_T *cctx = coookie;
5274
5275 if (lvp->ll_tv == NULL)
5276 {
5277 char_u *p = lvp->ll_name;
5278 int cc = *name_end;
5279 int ret = OK;
5280
5281 // Normal name. Only supports g:, w:, t: and b: namespaces.
5282 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005283 if (*p == '$')
5284 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5285 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005286 ret = FAIL;
5287 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005288 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005289
5290 *name_end = cc;
5291 return ret;
5292 }
5293
5294 // TODO: unlet {list}[idx]
5295 // TODO: unlet {dict}[key]
5296 emsg("Sorry, :unlet not fully implemented yet");
5297 return FAIL;
5298}
5299
5300/*
5301 * compile "unlet var", "lock var" and "unlock var"
5302 * "arg" points to "var".
5303 */
5304 static char_u *
5305compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5306{
5307 char_u *p = arg;
5308
5309 if (eap->cmdidx != CMD_unlet)
5310 {
5311 emsg("Sorry, :lock and unlock not implemented yet");
5312 return NULL;
5313 }
5314
5315 if (*p == '!')
5316 {
5317 p = skipwhite(p + 1);
5318 eap->forceit = TRUE;
5319 }
5320
5321 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5322 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5323}
5324
5325/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005326 * Compile an :import command.
5327 */
5328 static char_u *
5329compile_import(char_u *arg, cctx_T *cctx)
5330{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005331 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005332}
5333
5334/*
5335 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5336 */
5337 static int
5338compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5339{
5340 garray_T *instr = &cctx->ctx_instr;
5341 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5342
5343 if (endlabel == NULL)
5344 return FAIL;
5345 endlabel->el_next = *el;
5346 *el = endlabel;
5347 endlabel->el_end_label = instr->ga_len;
5348
5349 generate_JUMP(cctx, when, 0);
5350 return OK;
5351}
5352
5353 static void
5354compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5355{
5356 garray_T *instr = &cctx->ctx_instr;
5357
5358 while (*el != NULL)
5359 {
5360 endlabel_T *cur = (*el);
5361 isn_T *isn;
5362
5363 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5364 isn->isn_arg.jump.jump_where = instr->ga_len;
5365 *el = cur->el_next;
5366 vim_free(cur);
5367 }
5368}
5369
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005370 static void
5371compile_free_jump_to_end(endlabel_T **el)
5372{
5373 while (*el != NULL)
5374 {
5375 endlabel_T *cur = (*el);
5376
5377 *el = cur->el_next;
5378 vim_free(cur);
5379 }
5380}
5381
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005382/*
5383 * Create a new scope and set up the generic items.
5384 */
5385 static scope_T *
5386new_scope(cctx_T *cctx, scopetype_T type)
5387{
5388 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5389
5390 if (scope == NULL)
5391 return NULL;
5392 scope->se_outer = cctx->ctx_scope;
5393 cctx->ctx_scope = scope;
5394 scope->se_type = type;
5395 scope->se_local_count = cctx->ctx_locals.ga_len;
5396 return scope;
5397}
5398
5399/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005400 * Free the current scope and go back to the outer scope.
5401 */
5402 static void
5403drop_scope(cctx_T *cctx)
5404{
5405 scope_T *scope = cctx->ctx_scope;
5406
5407 if (scope == NULL)
5408 {
5409 iemsg("calling drop_scope() without a scope");
5410 return;
5411 }
5412 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005413 switch (scope->se_type)
5414 {
5415 case IF_SCOPE:
5416 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5417 case FOR_SCOPE:
5418 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5419 case WHILE_SCOPE:
5420 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5421 case TRY_SCOPE:
5422 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5423 case NO_SCOPE:
5424 case BLOCK_SCOPE:
5425 break;
5426 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005427 vim_free(scope);
5428}
5429
5430/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005431 * compile "if expr"
5432 *
5433 * "if expr" Produces instructions:
5434 * EVAL expr Push result of "expr"
5435 * JUMP_IF_FALSE end
5436 * ... body ...
5437 * end:
5438 *
5439 * "if expr | else" Produces instructions:
5440 * EVAL expr Push result of "expr"
5441 * JUMP_IF_FALSE else
5442 * ... body ...
5443 * JUMP_ALWAYS end
5444 * else:
5445 * ... body ...
5446 * end:
5447 *
5448 * "if expr1 | elseif expr2 | else" Produces instructions:
5449 * EVAL expr Push result of "expr"
5450 * JUMP_IF_FALSE elseif
5451 * ... body ...
5452 * JUMP_ALWAYS end
5453 * elseif:
5454 * EVAL expr Push result of "expr"
5455 * JUMP_IF_FALSE else
5456 * ... body ...
5457 * JUMP_ALWAYS end
5458 * else:
5459 * ... body ...
5460 * end:
5461 */
5462 static char_u *
5463compile_if(char_u *arg, cctx_T *cctx)
5464{
5465 char_u *p = arg;
5466 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005467 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005468 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005469 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005470 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005471
Bram Moolenaara5565e42020-05-09 15:44:01 +02005472 CLEAR_FIELD(ppconst);
5473 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005474 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005475 clear_ppconst(&ppconst);
5476 return NULL;
5477 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005478 if (cctx->ctx_skip == SKIP_YES)
5479 clear_ppconst(&ppconst);
5480 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005481 {
5482 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005483 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005484 clear_ppconst(&ppconst);
5485 }
5486 else
5487 {
5488 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005489 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005490 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005491 return NULL;
5492 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005493
5494 scope = new_scope(cctx, IF_SCOPE);
5495 if (scope == NULL)
5496 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005497 scope->se_skip_save = skip_save;
5498 // "is_had_return" will be reset if any block does not end in :return
5499 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005500
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005501 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005502 {
5503 // "where" is set when ":elseif", "else" or ":endif" is found
5504 scope->se_u.se_if.is_if_label = instr->ga_len;
5505 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5506 }
5507 else
5508 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005509
5510 return p;
5511}
5512
5513 static char_u *
5514compile_elseif(char_u *arg, cctx_T *cctx)
5515{
5516 char_u *p = arg;
5517 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005518 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005519 isn_T *isn;
5520 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005521 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005522
5523 if (scope == NULL || scope->se_type != IF_SCOPE)
5524 {
5525 emsg(_(e_elseif_without_if));
5526 return NULL;
5527 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005528 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005529 if (!cctx->ctx_had_return)
5530 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005531
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005532 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005533 {
5534 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005535 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005536 return NULL;
5537 // previous "if" or "elseif" jumps here
5538 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5539 isn->isn_arg.jump.jump_where = instr->ga_len;
5540 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005541
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005542 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005543 CLEAR_FIELD(ppconst);
5544 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005545 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005546 clear_ppconst(&ppconst);
5547 return NULL;
5548 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005549 if (scope->se_skip_save == SKIP_YES)
5550 clear_ppconst(&ppconst);
5551 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005552 {
5553 // The expression results in a constant.
5554 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005555 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005556 clear_ppconst(&ppconst);
5557 scope->se_u.se_if.is_if_label = -1;
5558 }
5559 else
5560 {
5561 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005562 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005563 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005564 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005565
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005566 // "where" is set when ":elseif", "else" or ":endif" is found
5567 scope->se_u.se_if.is_if_label = instr->ga_len;
5568 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5569 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005570
5571 return p;
5572}
5573
5574 static char_u *
5575compile_else(char_u *arg, cctx_T *cctx)
5576{
5577 char_u *p = arg;
5578 garray_T *instr = &cctx->ctx_instr;
5579 isn_T *isn;
5580 scope_T *scope = cctx->ctx_scope;
5581
5582 if (scope == NULL || scope->se_type != IF_SCOPE)
5583 {
5584 emsg(_(e_else_without_if));
5585 return NULL;
5586 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005587 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005588 if (!cctx->ctx_had_return)
5589 scope->se_u.se_if.is_had_return = FALSE;
5590 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005591
Bram Moolenaarefd88552020-06-18 20:50:10 +02005592 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005593 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005594 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005595 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005596 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005597 if (!cctx->ctx_had_return
5598 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5599 JUMP_ALWAYS, cctx) == FAIL)
5600 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005601 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005602
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005603 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005604 {
5605 if (scope->se_u.se_if.is_if_label >= 0)
5606 {
5607 // previous "if" or "elseif" jumps here
5608 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5609 isn->isn_arg.jump.jump_where = instr->ga_len;
5610 scope->se_u.se_if.is_if_label = -1;
5611 }
5612 }
5613
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005614 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005615 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5616 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005617
5618 return p;
5619}
5620
5621 static char_u *
5622compile_endif(char_u *arg, cctx_T *cctx)
5623{
5624 scope_T *scope = cctx->ctx_scope;
5625 ifscope_T *ifscope;
5626 garray_T *instr = &cctx->ctx_instr;
5627 isn_T *isn;
5628
5629 if (scope == NULL || scope->se_type != IF_SCOPE)
5630 {
5631 emsg(_(e_endif_without_if));
5632 return NULL;
5633 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005634 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005635 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005636 if (!cctx->ctx_had_return)
5637 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005638
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005639 if (scope->se_u.se_if.is_if_label >= 0)
5640 {
5641 // previous "if" or "elseif" jumps here
5642 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5643 isn->isn_arg.jump.jump_where = instr->ga_len;
5644 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005645 // Fill in the "end" label in jumps at the end of the blocks.
5646 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005647 cctx->ctx_skip = scope->se_skip_save;
5648
5649 // If all the blocks end in :return and there is an :else then the
5650 // had_return flag is set.
5651 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005652
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005653 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005654 return arg;
5655}
5656
5657/*
5658 * compile "for var in expr"
5659 *
5660 * Produces instructions:
5661 * PUSHNR -1
5662 * STORE loop-idx Set index to -1
5663 * EVAL expr Push result of "expr"
5664 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5665 * - if beyond end, jump to "end"
5666 * - otherwise get item from list and push it
5667 * STORE var Store item in "var"
5668 * ... body ...
5669 * JUMP top Jump back to repeat
5670 * end: DROP Drop the result of "expr"
5671 *
5672 */
5673 static char_u *
5674compile_for(char_u *arg, cctx_T *cctx)
5675{
5676 char_u *p;
5677 size_t varlen;
5678 garray_T *instr = &cctx->ctx_instr;
5679 garray_T *stack = &cctx->ctx_type_stack;
5680 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005681 lvar_T *loop_lvar; // loop iteration variable
5682 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005683 type_T *vartype;
5684
5685 // TODO: list of variables: "for [key, value] in dict"
5686 // parse "var"
5687 for (p = arg; eval_isnamec1(*p); ++p)
5688 ;
5689 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005690 var_lvar = lookup_local(arg, varlen, cctx);
5691 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005692 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005693 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005694 return NULL;
5695 }
5696
5697 // consume "in"
5698 p = skipwhite(p);
5699 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5700 {
5701 emsg(_(e_missing_in));
5702 return NULL;
5703 }
5704 p = skipwhite(p + 2);
5705
5706
5707 scope = new_scope(cctx, FOR_SCOPE);
5708 if (scope == NULL)
5709 return NULL;
5710
5711 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005712 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5713 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005714 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005715 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005716 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005717 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005718 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005719
5720 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005721 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5722 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005723 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005724 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005725 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005726 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005727 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005728
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005729 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005730
5731 // compile "expr", it remains on the stack until "endfor"
5732 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005733 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005734 {
5735 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005736 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005737 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005738
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005739 // Now that we know the type of "var", check that it is a list, now or at
5740 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005741 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005742 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005743 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005744 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005745 return NULL;
5746 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005747 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005748 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005749
5750 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005751 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005752
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005753 generate_FOR(cctx, loop_lvar->lv_idx);
5754 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005755
5756 return arg;
5757}
5758
5759/*
5760 * compile "endfor"
5761 */
5762 static char_u *
5763compile_endfor(char_u *arg, cctx_T *cctx)
5764{
5765 garray_T *instr = &cctx->ctx_instr;
5766 scope_T *scope = cctx->ctx_scope;
5767 forscope_T *forscope;
5768 isn_T *isn;
5769
5770 if (scope == NULL || scope->se_type != FOR_SCOPE)
5771 {
5772 emsg(_(e_for));
5773 return NULL;
5774 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005775 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005776 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005777 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005778
5779 // At end of ":for" scope jump back to the FOR instruction.
5780 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5781
5782 // Fill in the "end" label in the FOR statement so it can jump here
5783 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5784 isn->isn_arg.forloop.for_end = instr->ga_len;
5785
5786 // Fill in the "end" label any BREAK statements
5787 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5788
5789 // Below the ":for" scope drop the "expr" list from the stack.
5790 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5791 return NULL;
5792
5793 vim_free(scope);
5794
5795 return arg;
5796}
5797
5798/*
5799 * compile "while expr"
5800 *
5801 * Produces instructions:
5802 * top: EVAL expr Push result of "expr"
5803 * JUMP_IF_FALSE end jump if false
5804 * ... body ...
5805 * JUMP top Jump back to repeat
5806 * end:
5807 *
5808 */
5809 static char_u *
5810compile_while(char_u *arg, cctx_T *cctx)
5811{
5812 char_u *p = arg;
5813 garray_T *instr = &cctx->ctx_instr;
5814 scope_T *scope;
5815
5816 scope = new_scope(cctx, WHILE_SCOPE);
5817 if (scope == NULL)
5818 return NULL;
5819
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005820 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005821
5822 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005823 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005824 return NULL;
5825
5826 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005827 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005828 JUMP_IF_FALSE, cctx) == FAIL)
5829 return FAIL;
5830
5831 return p;
5832}
5833
5834/*
5835 * compile "endwhile"
5836 */
5837 static char_u *
5838compile_endwhile(char_u *arg, cctx_T *cctx)
5839{
5840 scope_T *scope = cctx->ctx_scope;
5841
5842 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5843 {
5844 emsg(_(e_while));
5845 return NULL;
5846 }
5847 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005848 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005849
5850 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005851 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005852
5853 // Fill in the "end" label in the WHILE statement so it can jump here.
5854 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005855 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005856
5857 vim_free(scope);
5858
5859 return arg;
5860}
5861
5862/*
5863 * compile "continue"
5864 */
5865 static char_u *
5866compile_continue(char_u *arg, cctx_T *cctx)
5867{
5868 scope_T *scope = cctx->ctx_scope;
5869
5870 for (;;)
5871 {
5872 if (scope == NULL)
5873 {
5874 emsg(_(e_continue));
5875 return NULL;
5876 }
5877 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5878 break;
5879 scope = scope->se_outer;
5880 }
5881
5882 // Jump back to the FOR or WHILE instruction.
5883 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005884 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5885 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005886 return arg;
5887}
5888
5889/*
5890 * compile "break"
5891 */
5892 static char_u *
5893compile_break(char_u *arg, cctx_T *cctx)
5894{
5895 scope_T *scope = cctx->ctx_scope;
5896 endlabel_T **el;
5897
5898 for (;;)
5899 {
5900 if (scope == NULL)
5901 {
5902 emsg(_(e_break));
5903 return NULL;
5904 }
5905 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5906 break;
5907 scope = scope->se_outer;
5908 }
5909
5910 // Jump to the end of the FOR or WHILE loop.
5911 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005912 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005913 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005914 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005915 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5916 return FAIL;
5917
5918 return arg;
5919}
5920
5921/*
5922 * compile "{" start of block
5923 */
5924 static char_u *
5925compile_block(char_u *arg, cctx_T *cctx)
5926{
5927 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5928 return NULL;
5929 return skipwhite(arg + 1);
5930}
5931
5932/*
5933 * compile end of block: drop one scope
5934 */
5935 static void
5936compile_endblock(cctx_T *cctx)
5937{
5938 scope_T *scope = cctx->ctx_scope;
5939
5940 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005941 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005942 vim_free(scope);
5943}
5944
5945/*
5946 * compile "try"
5947 * Creates a new scope for the try-endtry, pointing to the first catch and
5948 * finally.
5949 * Creates another scope for the "try" block itself.
5950 * TRY instruction sets up exception handling at runtime.
5951 *
5952 * "try"
5953 * TRY -> catch1, -> finally push trystack entry
5954 * ... try block
5955 * "throw {exception}"
5956 * EVAL {exception}
5957 * THROW create exception
5958 * ... try block
5959 * " catch {expr}"
5960 * JUMP -> finally
5961 * catch1: PUSH exeception
5962 * EVAL {expr}
5963 * MATCH
5964 * JUMP nomatch -> catch2
5965 * CATCH remove exception
5966 * ... catch block
5967 * " catch"
5968 * JUMP -> finally
5969 * catch2: CATCH remove exception
5970 * ... catch block
5971 * " finally"
5972 * finally:
5973 * ... finally block
5974 * " endtry"
5975 * ENDTRY pop trystack entry, may rethrow
5976 */
5977 static char_u *
5978compile_try(char_u *arg, cctx_T *cctx)
5979{
5980 garray_T *instr = &cctx->ctx_instr;
5981 scope_T *try_scope;
5982 scope_T *scope;
5983
5984 // scope that holds the jumps that go to catch/finally/endtry
5985 try_scope = new_scope(cctx, TRY_SCOPE);
5986 if (try_scope == NULL)
5987 return NULL;
5988
5989 // "catch" is set when the first ":catch" is found.
5990 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005991 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005992 if (generate_instr(cctx, ISN_TRY) == NULL)
5993 return NULL;
5994
5995 // scope for the try block itself
5996 scope = new_scope(cctx, BLOCK_SCOPE);
5997 if (scope == NULL)
5998 return NULL;
5999
6000 return arg;
6001}
6002
6003/*
6004 * compile "catch {expr}"
6005 */
6006 static char_u *
6007compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6008{
6009 scope_T *scope = cctx->ctx_scope;
6010 garray_T *instr = &cctx->ctx_instr;
6011 char_u *p;
6012 isn_T *isn;
6013
6014 // end block scope from :try or :catch
6015 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6016 compile_endblock(cctx);
6017 scope = cctx->ctx_scope;
6018
6019 // Error if not in a :try scope
6020 if (scope == NULL || scope->se_type != TRY_SCOPE)
6021 {
6022 emsg(_(e_catch));
6023 return NULL;
6024 }
6025
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006026 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006027 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006028 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006029 return NULL;
6030 }
6031
6032 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006033 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006034 JUMP_ALWAYS, cctx) == FAIL)
6035 return NULL;
6036
6037 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006038 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006039 if (isn->isn_arg.try.try_catch == 0)
6040 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006041 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006042 {
6043 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006044 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006045 isn->isn_arg.jump.jump_where = instr->ga_len;
6046 }
6047
6048 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006049 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006050 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006051 scope->se_u.se_try.ts_caught_all = TRUE;
6052 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006053 }
6054 else
6055 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006056 char_u *end;
6057 char_u *pat;
6058 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006059 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006060 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006061
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006062 // Push v:exception, push {expr} and MATCH
6063 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6064
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006065 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006066 if (*end != *p)
6067 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006068 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006069 vim_free(tofree);
6070 return FAIL;
6071 }
6072 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006073 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006074 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006075 len = (int)(end - tofree);
6076 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006077 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006078 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006079 if (pat == NULL)
6080 return FAIL;
6081 if (generate_PUSHS(cctx, pat) == FAIL)
6082 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006083
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006084 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6085 return NULL;
6086
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006087 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006088 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6089 return NULL;
6090 }
6091
6092 if (generate_instr(cctx, ISN_CATCH) == NULL)
6093 return NULL;
6094
6095 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6096 return NULL;
6097 return p;
6098}
6099
6100 static char_u *
6101compile_finally(char_u *arg, cctx_T *cctx)
6102{
6103 scope_T *scope = cctx->ctx_scope;
6104 garray_T *instr = &cctx->ctx_instr;
6105 isn_T *isn;
6106
6107 // end block scope from :try or :catch
6108 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6109 compile_endblock(cctx);
6110 scope = cctx->ctx_scope;
6111
6112 // Error if not in a :try scope
6113 if (scope == NULL || scope->se_type != TRY_SCOPE)
6114 {
6115 emsg(_(e_finally));
6116 return NULL;
6117 }
6118
6119 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006120 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006121 if (isn->isn_arg.try.try_finally != 0)
6122 {
6123 emsg(_(e_finally_dup));
6124 return NULL;
6125 }
6126
6127 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006128 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006129
Bram Moolenaar585fea72020-04-02 22:33:21 +02006130 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006131 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006132 {
6133 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006134 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006135 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006136 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006137 }
6138
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006139 // TODO: set index in ts_finally_label jumps
6140
6141 return arg;
6142}
6143
6144 static char_u *
6145compile_endtry(char_u *arg, cctx_T *cctx)
6146{
6147 scope_T *scope = cctx->ctx_scope;
6148 garray_T *instr = &cctx->ctx_instr;
6149 isn_T *isn;
6150
6151 // end block scope from :catch or :finally
6152 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6153 compile_endblock(cctx);
6154 scope = cctx->ctx_scope;
6155
6156 // Error if not in a :try scope
6157 if (scope == NULL || scope->se_type != TRY_SCOPE)
6158 {
6159 if (scope == NULL)
6160 emsg(_(e_no_endtry));
6161 else if (scope->se_type == WHILE_SCOPE)
6162 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006163 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006164 emsg(_(e_endfor));
6165 else
6166 emsg(_(e_endif));
6167 return NULL;
6168 }
6169
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006170 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006171 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6172 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006173 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006174 return NULL;
6175 }
6176
6177 // Fill in the "end" label in jumps at the end of the blocks, if not done
6178 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006179 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006180
6181 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006182 if (isn->isn_arg.try.try_catch == 0)
6183 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006184 if (isn->isn_arg.try.try_finally == 0)
6185 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006186
6187 if (scope->se_u.se_try.ts_catch_label != 0)
6188 {
6189 // Last catch without match jumps here
6190 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6191 isn->isn_arg.jump.jump_where = instr->ga_len;
6192 }
6193
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006194 compile_endblock(cctx);
6195
6196 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6197 return NULL;
6198 return arg;
6199}
6200
6201/*
6202 * compile "throw {expr}"
6203 */
6204 static char_u *
6205compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6206{
6207 char_u *p = skipwhite(arg);
6208
Bram Moolenaara5565e42020-05-09 15:44:01 +02006209 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006210 return NULL;
6211 if (may_generate_2STRING(-1, cctx) == FAIL)
6212 return NULL;
6213 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6214 return NULL;
6215
6216 return p;
6217}
6218
6219/*
6220 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006221 * compile "echomsg expr"
6222 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006223 * compile "execute expr"
6224 */
6225 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006226compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006227{
6228 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006229 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006230 int count = 0;
6231
6232 for (;;)
6233 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006234 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006235 return NULL;
6236 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006237 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006238 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006239 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006240 break;
6241 }
6242
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006243 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6244 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6245 else if (cmdidx == CMD_execute)
6246 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6247 else if (cmdidx == CMD_echomsg)
6248 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6249 else
6250 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006251 return p;
6252}
6253
6254/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006255 * A command that is not compiled, execute with legacy code.
6256 */
6257 static char_u *
6258compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6259{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006260 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006261 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006262 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006263
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006264 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006265 goto theend;
6266
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006267 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006268 {
6269 long argt = excmd_get_argt(eap->cmdidx);
6270 int usefilter = FALSE;
6271
6272 has_expr = argt & (EX_XFILE | EX_EXPAND);
6273
6274 // If the command can be followed by a bar, find the bar and truncate
6275 // it, so that the following command can be compiled.
6276 // The '|' is overwritten with a NUL, it is put back below.
6277 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6278 && *eap->arg == '!')
6279 // :w !filter or :r !filter or :r! filter
6280 usefilter = TRUE;
6281 if ((argt & EX_TRLBAR) && !usefilter)
6282 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006283 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006284 separate_nextcmd(eap);
6285 if (eap->nextcmd != NULL)
6286 nextcmd = eap->nextcmd;
6287 }
6288 }
6289
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006290 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6291 {
6292 // expand filename in "syntax include [@group] filename"
6293 has_expr = TRUE;
6294 eap->arg = skipwhite(eap->arg + 7);
6295 if (*eap->arg == '@')
6296 eap->arg = skiptowhite(eap->arg);
6297 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006298
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006299 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006300 {
6301 int count = 0;
6302 char_u *start = skipwhite(line);
6303
6304 // :cmd xxx`=expr1`yyy`=expr2`zzz
6305 // PUSHS ":cmd xxx"
6306 // eval expr1
6307 // PUSHS "yyy"
6308 // eval expr2
6309 // PUSHS "zzz"
6310 // EXECCONCAT 5
6311 for (;;)
6312 {
6313 if (p > start)
6314 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006315 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006316 ++count;
6317 }
6318 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006319 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006320 return NULL;
6321 may_generate_2STRING(-1, cctx);
6322 ++count;
6323 p = skipwhite(p);
6324 if (*p != '`')
6325 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006326 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006327 return NULL;
6328 }
6329 start = p + 1;
6330
6331 p = (char_u *)strstr((char *)start, "`=");
6332 if (p == NULL)
6333 {
6334 if (*skipwhite(start) != NUL)
6335 {
6336 generate_PUSHS(cctx, vim_strsave(start));
6337 ++count;
6338 }
6339 break;
6340 }
6341 }
6342 generate_EXECCONCAT(cctx, count);
6343 }
6344 else
6345 generate_EXEC(cctx, line);
6346
6347theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006348 if (*nextcmd != NUL)
6349 {
6350 // the parser expects a pointer to the bar, put it back
6351 --nextcmd;
6352 *nextcmd = '|';
6353 }
6354
6355 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006356}
6357
6358/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006359 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006360 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006361 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006362 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006363add_def_function(ufunc_T *ufunc)
6364{
6365 dfunc_T *dfunc;
6366
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006367 if (def_functions.ga_len == 0)
6368 {
6369 // The first position is not used, so that a zero uf_dfunc_idx means it
6370 // wasn't set.
6371 if (ga_grow(&def_functions, 1) == FAIL)
6372 return FAIL;
6373 ++def_functions.ga_len;
6374 }
6375
Bram Moolenaar09689a02020-05-09 22:50:08 +02006376 // Add the function to "def_functions".
6377 if (ga_grow(&def_functions, 1) == FAIL)
6378 return FAIL;
6379 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6380 CLEAR_POINTER(dfunc);
6381 dfunc->df_idx = def_functions.ga_len;
6382 ufunc->uf_dfunc_idx = dfunc->df_idx;
6383 dfunc->df_ufunc = ufunc;
6384 ++def_functions.ga_len;
6385 return OK;
6386}
6387
6388/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006389 * After ex_function() has collected all the function lines: parse and compile
6390 * the lines into instructions.
6391 * Adds the function to "def_functions".
6392 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6393 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006394 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006395 * This can be used recursively through compile_lambda(), which may reallocate
6396 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006397 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006398 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006399 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006400compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006401{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006402 char_u *line = NULL;
6403 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006404 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006405 cctx_T cctx;
6406 garray_T *instr;
6407 int called_emsg_before = called_emsg;
6408 int ret = FAIL;
6409 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006410 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006411 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006412 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006413
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006414 // When using a function that was compiled before: Free old instructions.
6415 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006416 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006417 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006418 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6419 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006420 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006421 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006422 else
6423 {
6424 if (add_def_function(ufunc) == FAIL)
6425 return FAIL;
6426 new_def_function = TRUE;
6427 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006428
Bram Moolenaar985116a2020-07-12 17:31:09 +02006429 ufunc->uf_def_status = UF_COMPILING;
6430
Bram Moolenaara80faa82020-04-12 19:37:17 +02006431 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006432 cctx.ctx_ufunc = ufunc;
6433 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006434 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006435 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6436 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6437 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6438 cctx.ctx_type_list = &ufunc->uf_type_list;
6439 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6440 instr = &cctx.ctx_instr;
6441
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006442 // Set the context to the function, it may be compiled when called from
6443 // another script. Set the script version to the most modern one.
6444 // The line number will be set in next_line_from_context().
6445 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006446 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6447
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006448 // Make sure error messages are OK.
6449 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6450 if (do_estack_push)
6451 estack_push_ufunc(ufunc, 1);
6452
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006453 if (ufunc->uf_def_args.ga_len > 0)
6454 {
6455 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006456 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006457 int i;
6458 char_u *arg;
6459 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006460 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006461
6462 // Produce instructions for the default values of optional arguments.
6463 // Store the instruction index in uf_def_arg_idx[] so that we know
6464 // where to start when the function is called, depending on the number
6465 // of arguments.
6466 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6467 if (ufunc->uf_def_arg_idx == NULL)
6468 goto erret;
6469 for (i = 0; i < count; ++i)
6470 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006471 garray_T *stack = &cctx.ctx_type_stack;
6472 type_T *val_type;
6473 int arg_idx = first_def_arg + i;
6474
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006475 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6476 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006477 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006478 goto erret;
6479
6480 // If no type specified use the type of the default value.
6481 // Otherwise check that the default value type matches the
6482 // specified type.
6483 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6484 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006485 {
6486 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006487 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006488 }
Bram Moolenaare30f64b2020-07-15 19:48:20 +02006489 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006490 == FAIL)
6491 {
6492 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6493 arg_idx + 1);
6494 goto erret;
6495 }
6496
6497 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006498 goto erret;
6499 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006500 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006501
6502 if (did_set_arg_type)
6503 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006504 }
6505
6506 /*
6507 * Loop over all the lines of the function and generate instructions.
6508 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006509 for (;;)
6510 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006511 exarg_T ea;
6512 cmdmod_T save_cmdmod;
6513 int starts_with_colon = FALSE;
6514 char_u *cmd;
6515 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006516
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006517 // Bail out on the first error to avoid a flood of errors and report
6518 // the right line number when inside try/catch.
6519 if (emsg_before != called_emsg)
6520 goto erret;
6521
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006522 if (line != NULL && *line == '|')
6523 // the line continues after a '|'
6524 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006525 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006526 && !(*line == '#' && (line == cctx.ctx_line_start
6527 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006528 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006529 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006530 goto erret;
6531 }
6532 else
6533 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006534 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006535 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006536 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006537 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006538 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006539 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006540
Bram Moolenaara80faa82020-04-12 19:37:17 +02006541 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006542 ea.cmdlinep = &line;
6543 ea.cmd = skipwhite(line);
6544
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006545 // Some things can be recognized by the first character.
6546 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006547 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006548 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006549 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006550 if (ea.cmd[1] != '{')
6551 {
6552 line = (char_u *)"";
6553 continue;
6554 }
6555 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006556
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006557 case '}':
6558 {
6559 // "}" ends a block scope
6560 scopetype_T stype = cctx.ctx_scope == NULL
6561 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006562
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006563 if (stype == BLOCK_SCOPE)
6564 {
6565 compile_endblock(&cctx);
6566 line = ea.cmd;
6567 }
6568 else
6569 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006570 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006571 goto erret;
6572 }
6573 if (line != NULL)
6574 line = skipwhite(ea.cmd + 1);
6575 continue;
6576 }
6577
6578 case '{':
6579 // "{" starts a block scope
6580 // "{'a': 1}->func() is something else
6581 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6582 {
6583 line = compile_block(ea.cmd, &cctx);
6584 continue;
6585 }
6586 break;
6587
6588 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006589 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006590 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006591 }
6592
6593 /*
6594 * COMMAND MODIFIERS
6595 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006596 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006597 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6598 {
6599 if (errormsg != NULL)
6600 goto erret;
6601 // empty line or comment
6602 line = (char_u *)"";
6603 continue;
6604 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006605 // TODO: use modifiers in the command
6606 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006607 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006608
6609 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006610 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006611 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006612 {
6613 if (*ea.cmd == '(')
6614 // not for "call()"
6615 ea.cmd = p;
6616 else
6617 ea.cmd = skipwhite(ea.cmd);
6618 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006619
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006620 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006621 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006622 char_u *pskip;
6623
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006624 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006625 // find what follows.
6626 // Skip over "var.member", "var[idx]" and the like.
6627 // Also "&opt = val", "$ENV = val" and "@r = val".
6628 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006629 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006630 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006631 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006632 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006633 char_u *var_end;
6634 int oplen;
6635 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006636
Bram Moolenaar65821722020-08-02 18:58:54 +02006637 if (ea.cmd[0] == '@')
6638 var_end = ea.cmd + 2;
6639 else
6640 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006641 FNE_CHECK_START | FNE_INCL_BR);
6642 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006643 if (oplen > 0)
6644 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006645 size_t len = p - ea.cmd;
6646
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006647 // Recognize an assignment if we recognize the variable
6648 // name:
6649 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006650 // "local = expr" where "local" is a local var.
6651 // "script = expr" where "script" is a script-local var.
6652 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006653 // "&opt = expr"
6654 // "$ENV = expr"
6655 // "@r = expr"
6656 if (*ea.cmd == '&'
6657 || *ea.cmd == '$'
6658 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006659 || ((len) > 2 && ea.cmd[1] == ':')
6660 || lookup_local(ea.cmd, len, &cctx) != NULL
6661 || lookup_arg(ea.cmd, len, NULL, NULL,
6662 NULL, &cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02006663 || lookup_script(ea.cmd, len, FALSE) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006664 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006665 {
6666 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006667 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006668 goto erret;
6669 continue;
6670 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006671 }
6672 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006673
6674 if (*ea.cmd == '[')
6675 {
6676 // [var, var] = expr
6677 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6678 if (line == NULL)
6679 goto erret;
6680 if (line != ea.cmd)
6681 continue;
6682 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006683 }
6684
6685 /*
6686 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006687 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006688 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006689 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006690 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006691 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006692 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006693 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006694 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006695 if (!starts_with_colon)
6696 {
6697 emsg(_(e_colon_required_before_a_range));
6698 goto erret;
6699 }
6700 if (ends_excmd2(line, ea.cmd))
6701 {
6702 // A range without a command: jump to the line.
6703 // TODO: compile to a more efficient command, possibly
6704 // calling parse_cmd_address().
6705 ea.cmdidx = CMD_SIZE;
6706 line = compile_exec(line, &ea, &cctx);
6707 goto nextline;
6708 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006709 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006710 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006711 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006712 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006713 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006714
6715 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6716 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006717 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006718 {
6719 line += STRLEN(line);
6720 continue;
6721 }
6722
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006723 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006724 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006725 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006726 // CMD_let cannot happen, compile_assignment() above is used
6727 iemsg("Command from find_ex_command() not handled");
6728 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006729 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006730 }
6731
6732 p = skipwhite(p);
6733
Bram Moolenaar3988f642020-08-27 22:43:03 +02006734 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006735 && ea.cmdidx != CMD_elseif
6736 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02006737 && ea.cmdidx != CMD_endif
6738 && ea.cmdidx != CMD_endfor
6739 && ea.cmdidx != CMD_endwhile
6740 && ea.cmdidx != CMD_catch
6741 && ea.cmdidx != CMD_finally
6742 && ea.cmdidx != CMD_endtry)
6743 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02006744 emsg(_(e_unreachable_code_after_return));
6745 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006746 }
6747
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006748 switch (ea.cmdidx)
6749 {
6750 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006751 ea.arg = p;
6752 line = compile_nested_function(&ea, &cctx);
6753 break;
6754
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006755 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006756 // TODO: should we allow this, e.g. to declare a global
6757 // function?
6758 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006759 goto erret;
6760
6761 case CMD_return:
6762 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006763 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006764 break;
6765
6766 case CMD_let:
6767 case CMD_const:
6768 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006769 if (line == p)
6770 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006771 break;
6772
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006773 case CMD_unlet:
6774 case CMD_unlockvar:
6775 case CMD_lockvar:
6776 line = compile_unletlock(p, &ea, &cctx);
6777 break;
6778
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006779 case CMD_import:
6780 line = compile_import(p, &cctx);
6781 break;
6782
6783 case CMD_if:
6784 line = compile_if(p, &cctx);
6785 break;
6786 case CMD_elseif:
6787 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006788 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006789 break;
6790 case CMD_else:
6791 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006792 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006793 break;
6794 case CMD_endif:
6795 line = compile_endif(p, &cctx);
6796 break;
6797
6798 case CMD_while:
6799 line = compile_while(p, &cctx);
6800 break;
6801 case CMD_endwhile:
6802 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006803 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006804 break;
6805
6806 case CMD_for:
6807 line = compile_for(p, &cctx);
6808 break;
6809 case CMD_endfor:
6810 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006811 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006812 break;
6813 case CMD_continue:
6814 line = compile_continue(p, &cctx);
6815 break;
6816 case CMD_break:
6817 line = compile_break(p, &cctx);
6818 break;
6819
6820 case CMD_try:
6821 line = compile_try(p, &cctx);
6822 break;
6823 case CMD_catch:
6824 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006825 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006826 break;
6827 case CMD_finally:
6828 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006829 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006830 break;
6831 case CMD_endtry:
6832 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006833 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006834 break;
6835 case CMD_throw:
6836 line = compile_throw(p, &cctx);
6837 break;
6838
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006839 case CMD_eval:
6840 if (compile_expr0(&p, &cctx) == FAIL)
6841 goto erret;
6842
Bram Moolenaar3988f642020-08-27 22:43:03 +02006843 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006844 generate_instr_drop(&cctx, ISN_DROP, 1);
6845
6846 line = skipwhite(p);
6847 break;
6848
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006849 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006850 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006851 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006852 case CMD_echomsg:
6853 case CMD_echoerr:
6854 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006855 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006856
Bram Moolenaar3988f642020-08-27 22:43:03 +02006857 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006858
Bram Moolenaarae616492020-07-28 20:07:27 +02006859 case CMD_append:
6860 case CMD_change:
6861 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02006862 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02006863 case CMD_xit:
6864 not_in_vim9(&ea);
6865 goto erret;
6866
Bram Moolenaar002262f2020-07-08 17:47:57 +02006867 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02006868 if (cctx.ctx_skip != SKIP_YES)
6869 {
6870 semsg(_(e_invalid_command_str), ea.cmd);
6871 goto erret;
6872 }
6873 // We don't check for a next command here.
6874 line = (char_u *)"";
6875 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02006876
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006877 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006878 // Not recognized, execute with do_cmdline_cmd().
6879 ea.arg = p;
6880 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006881 break;
6882 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006883nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006884 if (line == NULL)
6885 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006886 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006887
6888 if (cctx.ctx_type_stack.ga_len < 0)
6889 {
6890 iemsg("Type stack underflow");
6891 goto erret;
6892 }
6893 }
6894
6895 if (cctx.ctx_scope != NULL)
6896 {
6897 if (cctx.ctx_scope->se_type == IF_SCOPE)
6898 emsg(_(e_endif));
6899 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6900 emsg(_(e_endwhile));
6901 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6902 emsg(_(e_endfor));
6903 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006904 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006905 goto erret;
6906 }
6907
Bram Moolenaarefd88552020-06-18 20:50:10 +02006908 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006909 {
6910 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6911 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006912 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006913 goto erret;
6914 }
6915
6916 // Return zero if there is no return at the end.
6917 generate_PUSHNR(&cctx, 0);
6918 generate_instr(&cctx, ISN_RETURN);
6919 }
6920
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006921 {
6922 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6923 + ufunc->uf_dfunc_idx;
6924 dfunc->df_deleted = FALSE;
6925 dfunc->df_instr = instr->ga_data;
6926 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006927 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006928 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006929 if (cctx.ctx_outer_used)
6930 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006931 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006932 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006933
6934 ret = OK;
6935
6936erret:
6937 if (ret == FAIL)
6938 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006939 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006940 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6941 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006942
6943 for (idx = 0; idx < instr->ga_len; ++idx)
6944 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006945 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006946
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006947 // If using the last entry in the table and it was added above, we
6948 // might as well remove it.
6949 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02006950 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006951 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006952 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006953 ufunc->uf_dfunc_idx = 0;
6954 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006955 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006956
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006957 while (cctx.ctx_scope != NULL)
6958 drop_scope(&cctx);
6959
Bram Moolenaar20431c92020-03-20 18:39:46 +01006960 // Don't execute this function body.
6961 ga_clear_strings(&ufunc->uf_lines);
6962
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006963 if (errormsg != NULL)
6964 emsg(errormsg);
6965 else if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006966 emsg(_(e_compile_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006967 }
6968
6969 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006970 if (do_estack_push)
6971 estack_pop();
6972
Bram Moolenaar20431c92020-03-20 18:39:46 +01006973 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006974 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006975 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02006976 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006977}
6978
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02006979 void
6980set_function_type(ufunc_T *ufunc)
6981{
6982 int varargs = ufunc->uf_va_name != NULL;
6983 int argcount = ufunc->uf_args.ga_len;
6984
6985 // Create a type for the function, with the return type and any
6986 // argument types.
6987 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6988 // The type is included in "tt_args".
6989 if (argcount > 0 || varargs)
6990 {
6991 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6992 argcount, &ufunc->uf_type_list);
6993 // Add argument types to the function type.
6994 if (func_type_add_arg_types(ufunc->uf_func_type,
6995 argcount + varargs,
6996 &ufunc->uf_type_list) == FAIL)
6997 return;
6998 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6999 ufunc->uf_func_type->tt_min_argcount =
7000 argcount - ufunc->uf_def_args.ga_len;
7001 if (ufunc->uf_arg_types == NULL)
7002 {
7003 int i;
7004
7005 // lambda does not have argument types.
7006 for (i = 0; i < argcount; ++i)
7007 ufunc->uf_func_type->tt_args[i] = &t_any;
7008 }
7009 else
7010 mch_memmove(ufunc->uf_func_type->tt_args,
7011 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7012 if (varargs)
7013 {
7014 ufunc->uf_func_type->tt_args[argcount] =
7015 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7016 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7017 }
7018 }
7019 else
7020 // No arguments, can use a predefined type.
7021 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7022 argcount, &ufunc->uf_type_list);
7023}
7024
7025
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007026/*
7027 * Delete an instruction, free what it contains.
7028 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007029 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007030delete_instr(isn_T *isn)
7031{
7032 switch (isn->isn_type)
7033 {
7034 case ISN_EXEC:
7035 case ISN_LOADENV:
7036 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007037 case ISN_LOADB:
7038 case ISN_LOADW:
7039 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007040 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007041 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007042 case ISN_PUSHEXC:
7043 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007044 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007045 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007046 case ISN_STOREB:
7047 case ISN_STOREW:
7048 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007049 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007050 vim_free(isn->isn_arg.string);
7051 break;
7052
7053 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007054 case ISN_STORES:
7055 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007056 break;
7057
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007058 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007059 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007060 vim_free(isn->isn_arg.unlet.ul_name);
7061 break;
7062
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007063 case ISN_STOREOPT:
7064 vim_free(isn->isn_arg.storeopt.so_name);
7065 break;
7066
7067 case ISN_PUSHBLOB: // push blob isn_arg.blob
7068 blob_unref(isn->isn_arg.blob);
7069 break;
7070
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007071 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007072#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007073 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007074#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007075 break;
7076
7077 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007078#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007079 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007080#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007081 break;
7082
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007083 case ISN_UCALL:
7084 vim_free(isn->isn_arg.ufunc.cuf_name);
7085 break;
7086
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007087 case ISN_FUNCREF:
7088 {
7089 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7090 + isn->isn_arg.funcref.fr_func;
7091 func_ptr_unref(dfunc->df_ufunc);
7092 }
7093 break;
7094
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007095 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007096 {
7097 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7098 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7099
7100 if (ufunc != NULL)
7101 {
7102 // Clear uf_dfunc_idx so that the function is deleted.
7103 clear_def_function(ufunc);
7104 ufunc->uf_dfunc_idx = 0;
7105 func_ptr_unref(ufunc);
7106 }
7107
7108 vim_free(lambda);
7109 vim_free(isn->isn_arg.newfunc.nf_global);
7110 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007111 break;
7112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007113 case ISN_2BOOL:
7114 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007115 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007116 case ISN_ADDBLOB:
7117 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007118 case ISN_ANYINDEX:
7119 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007120 case ISN_BCALL:
7121 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007122 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007123 case ISN_CHECKNR:
7124 case ISN_CHECKTYPE:
7125 case ISN_COMPAREANY:
7126 case ISN_COMPAREBLOB:
7127 case ISN_COMPAREBOOL:
7128 case ISN_COMPAREDICT:
7129 case ISN_COMPAREFLOAT:
7130 case ISN_COMPAREFUNC:
7131 case ISN_COMPARELIST:
7132 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007133 case ISN_COMPARESPECIAL:
7134 case ISN_COMPARESTRING:
7135 case ISN_CONCAT:
7136 case ISN_DCALL:
7137 case ISN_DROP:
7138 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007139 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007140 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007141 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007142 case ISN_EXECCONCAT:
7143 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007144 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007145 case ISN_GETITEM:
7146 case ISN_JUMP:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007147 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007148 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007149 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007150 case ISN_LOADBDICT:
7151 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007152 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007153 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007154 case ISN_LOADSCRIPT:
7155 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007156 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007157 case ISN_LOADWDICT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007158 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007159 case ISN_NEGATENR:
7160 case ISN_NEWDICT:
7161 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007162 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007163 case ISN_OPFLOAT:
7164 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007165 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007166 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007167 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007168 case ISN_PUSHF:
7169 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007170 case ISN_PUSHSPEC:
7171 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007172 case ISN_SHUFFLE:
7173 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007174 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007175 case ISN_STOREDICT:
7176 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007177 case ISN_STORENR:
7178 case ISN_STOREOUTER:
7179 case ISN_STOREREG:
7180 case ISN_STORESCRIPT:
7181 case ISN_STOREV:
7182 case ISN_STRINDEX:
7183 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007184 case ISN_THROW:
7185 case ISN_TRY:
7186 // nothing allocated
7187 break;
7188 }
7189}
7190
7191/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007192 * Free all instructions for "dfunc".
7193 */
7194 static void
7195delete_def_function_contents(dfunc_T *dfunc)
7196{
7197 int idx;
7198
7199 ga_clear(&dfunc->df_def_args_isn);
7200
7201 if (dfunc->df_instr != NULL)
7202 {
7203 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7204 delete_instr(dfunc->df_instr + idx);
7205 VIM_CLEAR(dfunc->df_instr);
7206 }
7207
7208 dfunc->df_deleted = TRUE;
7209}
7210
7211/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007212 * When a user function is deleted, clear the contents of any associated def
7213 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007214 */
7215 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007216clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007217{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007218 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007219 {
7220 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7221 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007222
Bram Moolenaar20431c92020-03-20 18:39:46 +01007223 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007224 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007225 }
7226}
7227
7228#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007229/*
7230 * Free all functions defined with ":def".
7231 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007232 void
7233free_def_functions(void)
7234{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007235 int idx;
7236
7237 for (idx = 0; idx < def_functions.ga_len; ++idx)
7238 {
7239 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7240
7241 delete_def_function_contents(dfunc);
7242 }
7243
7244 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007245}
7246#endif
7247
7248
7249#endif // FEAT_EVAL