blob: c0cea2992cdd488bf187aa4a2d66be78164f4371 [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 Moolenaar61a89812020-05-07 16:58:17 +02004017 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004018 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019 return FAIL;
4020
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004021 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 if (*p == '?')
4023 {
4024 garray_T *instr = &cctx->ctx_instr;
4025 garray_T *stack = &cctx->ctx_type_stack;
4026 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004027 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004029 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004031 int has_const_expr = FALSE;
4032 int const_value = FALSE;
4033 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004035 if (next != NULL)
4036 {
4037 *arg = next_line_from_context(cctx, TRUE);
4038 p = skipwhite(*arg);
4039 }
4040
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004041 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4042 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004043 semsg(_(e_white_space_required_before_and_after_str), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004044 return FAIL;
4045 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046
Bram Moolenaara5565e42020-05-09 15:44:01 +02004047 if (ppconst->pp_used == ppconst_used + 1)
4048 {
4049 // the condition is a constant, we know whether the ? or the :
4050 // expression is to be evaluated.
4051 has_const_expr = TRUE;
4052 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4053 clear_tv(&ppconst->pp_tv[ppconst_used]);
4054 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004055 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4056 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004057 }
4058 else
4059 {
4060 generate_ppconst(cctx, ppconst);
4061 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4062 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004063
4064 // evaluate the second expression; any type is accepted
4065 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004066 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004067 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004068 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004069 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070
Bram Moolenaara5565e42020-05-09 15:44:01 +02004071 if (!has_const_expr)
4072 {
4073 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004074
Bram Moolenaara5565e42020-05-09 15:44:01 +02004075 // remember the type and drop it
4076 --stack->ga_len;
4077 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004078
Bram Moolenaara5565e42020-05-09 15:44:01 +02004079 end_idx = instr->ga_len;
4080 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4081
4082 // jump here from JUMP_IF_FALSE
4083 isn = ((isn_T *)instr->ga_data) + alt_idx;
4084 isn->isn_arg.jump.jump_where = instr->ga_len;
4085 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004086
4087 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004088 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089 if (*p != ':')
4090 {
4091 emsg(_(e_missing_colon));
4092 return FAIL;
4093 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004094 if (next != NULL)
4095 {
4096 *arg = next_line_from_context(cctx, TRUE);
4097 p = skipwhite(*arg);
4098 }
4099
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004100 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4101 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004102 semsg(_(e_white_space_required_before_and_after_str), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004103 return FAIL;
4104 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004105
4106 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004107 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004108 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4109 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004110 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004111 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004112 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004113 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004114 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115
Bram Moolenaara5565e42020-05-09 15:44:01 +02004116 if (!has_const_expr)
4117 {
4118 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119
Bram Moolenaara5565e42020-05-09 15:44:01 +02004120 // If the types differ, the result has a more generic type.
4121 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4122 common_type(type1, type2, &type2, cctx->ctx_type_list);
4123
4124 // jump here from JUMP_ALWAYS
4125 isn = ((isn_T *)instr->ga_data) + end_idx;
4126 isn->isn_arg.jump.jump_where = instr->ga_len;
4127 }
4128
4129 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130 }
4131 return OK;
4132}
4133
4134/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004135 * Toplevel expression.
4136 */
4137 static int
4138compile_expr0(char_u **arg, cctx_T *cctx)
4139{
4140 ppconst_T ppconst;
4141
4142 CLEAR_FIELD(ppconst);
4143 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4144 {
4145 clear_ppconst(&ppconst);
4146 return FAIL;
4147 }
4148 if (generate_ppconst(cctx, &ppconst) == FAIL)
4149 return FAIL;
4150 return OK;
4151}
4152
4153/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154 * compile "return [expr]"
4155 */
4156 static char_u *
4157compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4158{
4159 char_u *p = arg;
4160 garray_T *stack = &cctx->ctx_type_stack;
4161 type_T *stack_type;
4162
4163 if (*p != NUL && *p != '|' && *p != '\n')
4164 {
4165 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004166 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004167 return NULL;
4168
4169 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4170 if (set_return_type)
4171 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004172 else
4173 {
4174 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4175 && stack_type->tt_type != VAR_VOID
4176 && stack_type->tt_type != VAR_UNKNOWN)
4177 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004178 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004179 return NULL;
4180 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004181 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4182 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004183 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004184 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 }
4186 else
4187 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004188 // "set_return_type" cannot be TRUE, only used for a lambda which
4189 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004190 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4191 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004192 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004193 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194 return NULL;
4195 }
4196
4197 // No argument, return zero.
4198 generate_PUSHNR(cctx, 0);
4199 }
4200
4201 if (generate_instr(cctx, ISN_RETURN) == NULL)
4202 return NULL;
4203
4204 // "return val | endif" is possible
4205 return skipwhite(p);
4206}
4207
4208/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004209 * Get a line from the compilation context, compatible with exarg_T getline().
4210 * Return a pointer to the line in allocated memory.
4211 * Return NULL for end-of-file or some error.
4212 */
4213 static char_u *
4214exarg_getline(
4215 int c UNUSED,
4216 void *cookie,
4217 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004218 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004219{
4220 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004221 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004222
Bram Moolenaar66250c92020-08-20 15:02:42 +02004223 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004224 {
Bram Moolenaar66250c92020-08-20 15:02:42 +02004225 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4226 return NULL;
4227 ++cctx->ctx_lnum;
4228 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4229 // Comment lines result in NULL pointers, skip them.
4230 if (p != NULL)
4231 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004232 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004233}
4234
4235/*
4236 * Compile a nested :def command.
4237 */
4238 static char_u *
4239compile_nested_function(exarg_T *eap, cctx_T *cctx)
4240{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004241 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004242 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004243 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004244 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004245 lvar_T *lvar;
4246 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004247 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004248
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004249 // Only g:Func() can use a namespace.
4250 if (name_start[1] == ':' && !is_global)
4251 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004252 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004253 return NULL;
4254 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004255 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4256 return NULL;
4257
Bram Moolenaar04b12692020-05-04 23:24:44 +02004258 eap->arg = name_end;
4259 eap->getline = exarg_getline;
4260 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004261 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004262 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004263 lambda_name = get_lambda_name();
4264 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004265
Bram Moolenaar822ba242020-05-24 23:00:18 +02004266 if (ufunc == NULL)
4267 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004268 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004269 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004270 return NULL;
4271
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004272 if (is_global)
4273 {
4274 char_u *func_name = vim_strnsave(name_start + 2,
4275 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004276
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004277 if (func_name == NULL)
4278 r = FAIL;
4279 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004280 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004281 }
4282 else
4283 {
4284 // Define a local variable for the function reference.
4285 lvar = reserve_local(cctx, name_start, name_end - name_start,
4286 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004287 if (lvar == NULL)
4288 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004289 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004290 return NULL;
4291 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4292 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004293
Bram Moolenaar61a89812020-05-07 16:58:17 +02004294 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004295 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004296}
4297
4298/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 * Return the length of an assignment operator, or zero if there isn't one.
4300 */
4301 int
4302assignment_len(char_u *p, int *heredoc)
4303{
4304 if (*p == '=')
4305 {
4306 if (p[1] == '<' && p[2] == '<')
4307 {
4308 *heredoc = TRUE;
4309 return 3;
4310 }
4311 return 1;
4312 }
4313 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4314 return 2;
4315 if (STRNCMP(p, "..=", 3) == 0)
4316 return 3;
4317 return 0;
4318}
4319
4320// words that cannot be used as a variable
4321static char *reserved[] = {
4322 "true",
4323 "false",
4324 NULL
4325};
4326
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004327typedef enum {
4328 dest_local,
4329 dest_option,
4330 dest_env,
4331 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004332 dest_buffer,
4333 dest_window,
4334 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004335 dest_vimvar,
4336 dest_script,
4337 dest_reg,
4338} assign_dest_T;
4339
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004341 * Generate the load instruction for "name".
4342 */
4343 static void
4344generate_loadvar(
4345 cctx_T *cctx,
4346 assign_dest_T dest,
4347 char_u *name,
4348 lvar_T *lvar,
4349 type_T *type)
4350{
4351 switch (dest)
4352 {
4353 case dest_option:
4354 // TODO: check the option exists
4355 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4356 break;
4357 case dest_global:
4358 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4359 break;
4360 case dest_buffer:
4361 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4362 break;
4363 case dest_window:
4364 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4365 break;
4366 case dest_tab:
4367 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4368 break;
4369 case dest_script:
4370 compile_load_scriptvar(cctx,
4371 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4372 break;
4373 case dest_env:
4374 // Include $ in the name here
4375 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4376 break;
4377 case dest_reg:
4378 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4379 break;
4380 case dest_vimvar:
4381 generate_LOADV(cctx, name + 2, TRUE);
4382 break;
4383 case dest_local:
4384 if (lvar->lv_from_outer)
4385 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4386 NULL, type);
4387 else
4388 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4389 break;
4390 }
4391}
4392
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004393 void
4394vim9_declare_error(char_u *name)
4395{
4396 char *scope = "";
4397
4398 switch (*name)
4399 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004400 case 'g': scope = _("global"); break;
4401 case 'b': scope = _("buffer"); break;
4402 case 'w': scope = _("window"); break;
4403 case 't': scope = _("tab"); break;
4404 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004405 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004406 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004407 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004408 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004409 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004410 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004411 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004412 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004413 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004414}
4415
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004416/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004417 * Compile declaration and assignment:
4418 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004420 * Return NULL for an error.
4421 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422 */
4423 static char_u *
4424compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4425{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004426 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004427 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004428 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429 char_u *ret = NULL;
4430 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004431 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004432 int scriptvar_sid = 0;
4433 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004434 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004435 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004436 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004437 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004438 int oplen = 0;
4439 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004440 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004441 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004442 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004444 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004446 // Skip over the "var" or "[var, var]" to get to any "=".
4447 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4448 if (p == NULL)
4449 return *arg == '[' ? arg : NULL;
4450
4451 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004452 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004453 // TODO: should we allow this, and figure out type inference from list
4454 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004455 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004456 return NULL;
4457 }
4458
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459 sp = p;
4460 p = skipwhite(p);
4461 op = p;
4462 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004463
4464 if (var_count > 0 && oplen == 0)
4465 // can be something like "[1, 2]->func()"
4466 return arg;
4467
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4469 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004470 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004471 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004472 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004473
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004474 if (heredoc)
4475 {
4476 list_T *l;
4477 listitem_T *li;
4478
4479 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004480 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004482 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004483
4484 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004485 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004486 {
4487 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4488 li->li_tv.vval.v_string = NULL;
4489 }
4490 generate_NEWLIST(cctx, l->lv_len);
4491 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004492 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004493 list_free(l);
4494 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004495 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004496 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004497 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004499 // for "[var, var] = expr" evaluate the expression here, loop over the
4500 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004501
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004502 p = skipwhite(op + oplen);
4503 if (compile_expr0(&p, cctx) == FAIL)
4504 return NULL;
4505 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004506
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004507 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004509 type_T *stacktype;
4510
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004511 stacktype = stack->ga_len == 0 ? &t_void
4512 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004513 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004514 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004515 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004516 goto theend;
4517 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004518 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004519 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004520 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004521 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4522 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004523 }
4524 }
4525
4526 /*
4527 * Loop over variables in "[var, var] = expr".
4528 * For "var = expr" and "let var: type" this is done only once.
4529 */
4530 if (var_count > 0)
4531 var_start = skipwhite(arg + 1); // skip over the "["
4532 else
4533 var_start = arg;
4534 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4535 {
4536 char_u *var_end = skip_var_one(var_start, FALSE);
4537 size_t varlen;
4538 int new_local = FALSE;
4539 int opt_type;
4540 int opt_flags = 0;
4541 assign_dest_T dest = dest_local;
4542 int vimvaridx = -1;
4543 lvar_T *lvar = NULL;
4544 lvar_T arg_lvar;
4545 int has_type = FALSE;
4546 int has_index = FALSE;
4547 int instr_count = -1;
4548
Bram Moolenaar65821722020-08-02 18:58:54 +02004549 if (*var_start == '@')
4550 p = var_start + 2;
4551 else
4552 {
4553 p = (*var_start == '&' || *var_start == '$')
4554 ? var_start + 1 : var_start;
4555 p = to_name_end(p, TRUE);
4556 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004557
4558 // "a: type" is declaring variable "a" with a type, not "a:".
4559 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4560 --var_end;
4561 if (is_decl && p == var_start + 2 && p[-1] == ':')
4562 --p;
4563
4564 varlen = p - var_start;
4565 vim_free(name);
4566 name = vim_strnsave(var_start, varlen);
4567 if (name == NULL)
4568 return NULL;
4569 if (!heredoc)
4570 type = &t_any;
4571
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004572 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004573 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004574 int declare_error = FALSE;
4575
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004576 if (*var_start == '&')
4577 {
4578 int cc;
4579 long numval;
4580
4581 dest = dest_option;
4582 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004583 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004584 emsg(_(e_const_option));
4585 goto theend;
4586 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004587 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004588 p = var_start;
4589 p = find_option_end(&p, &opt_flags);
4590 if (p == NULL)
4591 {
4592 // cannot happen?
4593 emsg(_(e_letunexp));
4594 goto theend;
4595 }
4596 cc = *p;
4597 *p = NUL;
4598 opt_type = get_option_value(var_start + 1, &numval,
4599 NULL, opt_flags);
4600 *p = cc;
4601 if (opt_type == -3)
4602 {
4603 semsg(_(e_unknown_option), var_start);
4604 goto theend;
4605 }
4606 if (opt_type == -2 || opt_type == 0)
4607 type = &t_string;
4608 else
4609 type = &t_number; // both number and boolean option
4610 }
4611 else if (*var_start == '$')
4612 {
4613 dest = dest_env;
4614 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004615 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004616 }
4617 else if (*var_start == '@')
4618 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004619 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004620 {
4621 emsg_invreg(var_start[1]);
4622 goto theend;
4623 }
4624 dest = dest_reg;
4625 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004626 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004627 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004628 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004629 {
4630 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004631 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004632 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004633 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004634 {
4635 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004636 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004637 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004638 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004639 {
4640 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004641 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004642 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004643 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004644 {
4645 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004646 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004647 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004648 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004649 {
4650 typval_T *vtv;
4651 int di_flags;
4652
4653 vimvaridx = find_vim_var(name + 2, &di_flags);
4654 if (vimvaridx < 0)
4655 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004656 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004657 goto theend;
4658 }
4659 // We use the current value of "sandbox" here, is that OK?
4660 if (var_check_ro(di_flags, name, FALSE))
4661 goto theend;
4662 dest = dest_vimvar;
4663 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004664 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004665 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004666 }
4667 else
4668 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004669 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004670
4671 for (idx = 0; reserved[idx] != NULL; ++idx)
4672 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004673 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004674 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004675 goto theend;
4676 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004677
4678 lvar = lookup_local(var_start, varlen, cctx);
4679 if (lvar == NULL)
4680 {
4681 CLEAR_FIELD(arg_lvar);
4682 if (lookup_arg(var_start, varlen,
4683 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4684 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004685 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004686 if (is_decl)
4687 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004688 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004689 goto theend;
4690 }
4691 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004692 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004693 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004694 if (lvar != NULL)
4695 {
4696 if (is_decl)
4697 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004698 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004699 goto theend;
4700 }
4701 else if (lvar->lv_const)
4702 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004703 semsg(_(e_cannot_assign_to_constant), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004704 goto theend;
4705 }
4706 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004707 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004708 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004709 int script_namespace = varlen > 1
4710 && STRNCMP(var_start, "s:", 2) == 0;
4711 int script_var = (script_namespace
Bram Moolenaar53900992020-08-22 19:02:02 +02004712 ? lookup_script(var_start + 2, varlen - 2, FALSE)
4713 : lookup_script(var_start, varlen, TRUE)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004714 imported_T *import =
4715 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004716
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004717 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004718 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004719 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4720
4721 if (is_decl)
4722 {
4723 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004724 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004725 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004726 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004727 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004728 name);
4729 goto theend;
4730 }
4731 else if (cctx->ctx_ufunc->uf_script_ctx_version
4732 == SCRIPT_VERSION_VIM9
4733 && script_namespace
4734 && !script_var && import == NULL)
4735 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004736 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004737 goto theend;
4738 }
4739
4740 dest = dest_script;
4741
4742 // existing script-local variables should have a type
4743 scriptvar_sid = current_sctx.sc_sid;
4744 if (import != NULL)
4745 scriptvar_sid = import->imp_sid;
4746 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4747 rawname, TRUE);
4748 if (scriptvar_idx >= 0)
4749 {
4750 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4751 svar_T *sv =
4752 ((svar_T *)si->sn_var_vals.ga_data)
4753 + scriptvar_idx;
4754 type = sv->sv_type;
4755 }
4756 }
4757 else if (name[1] == ':' && name[2] != NUL)
4758 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004759 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004760 goto theend;
4761 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004762 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004763 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004764 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004765 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004766 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004767 else if (check_defined(var_start, varlen, cctx) == FAIL)
4768 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004769 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004770 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004771
4772 if (declare_error)
4773 {
4774 vim9_declare_error(name);
4775 goto theend;
4776 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004777 }
4778
4779 // handle "a:name" as a name, not index "name" on "a"
4780 if (varlen > 1 || var_start[varlen] != ':')
4781 p = var_end;
4782
4783 if (dest != dest_option)
4784 {
4785 if (is_decl && *p == ':')
4786 {
4787 // parse optional type: "let var: type = expr"
4788 if (!VIM_ISWHITE(p[1]))
4789 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004790 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004791 goto theend;
4792 }
4793 p = skipwhite(p + 1);
4794 type = parse_type(&p, cctx->ctx_type_list);
4795 has_type = TRUE;
4796 }
4797 else if (lvar != NULL)
4798 type = lvar->lv_type;
4799 }
4800
4801 if (oplen == 3 && !heredoc && dest != dest_global
4802 && type->tt_type != VAR_STRING
4803 && type->tt_type != VAR_ANY)
4804 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004805 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004806 goto theend;
4807 }
4808
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004809 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004810 {
4811 if (oplen > 1 && !heredoc)
4812 {
4813 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004814 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004815 goto theend;
4816 }
4817
4818 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004819 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
4820 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004821 goto theend;
4822 lvar = reserve_local(cctx, var_start, varlen,
4823 cmdidx == CMD_const, type);
4824 if (lvar == NULL)
4825 goto theend;
4826 new_local = TRUE;
4827 }
4828
4829 member_type = type;
4830 if (var_end > var_start + varlen)
4831 {
4832 // Something follows after the variable: "var[idx]".
4833 if (is_decl)
4834 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004835 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004836 goto theend;
4837 }
4838
4839 if (var_start[varlen] == '[')
4840 {
4841 has_index = TRUE;
4842 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004843 member_type = &t_any;
4844 else
4845 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004846 }
4847 else
4848 {
4849 semsg("Not supported yet: %s", var_start);
4850 goto theend;
4851 }
4852 }
4853 else if (lvar == &arg_lvar)
4854 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004855 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004856 goto theend;
4857 }
4858
4859 if (!heredoc)
4860 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004861 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004862 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004863 if (oplen > 0 && var_count == 0)
4864 {
4865 // skip over the "=" and the expression
4866 p = skipwhite(op + oplen);
4867 compile_expr0(&p, cctx);
4868 }
4869 }
4870 else if (oplen > 0)
4871 {
4872 type_T *stacktype;
4873
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004874 // For "var = expr" evaluate the expression.
4875 if (var_count == 0)
4876 {
4877 int r;
4878
4879 // for "+=", "*=", "..=" etc. first load the current value
4880 if (*op != '=')
4881 {
4882 generate_loadvar(cctx, dest, name, lvar, type);
4883
4884 if (has_index)
4885 {
4886 // TODO: get member from list or dict
4887 emsg("Index with operation not supported yet");
4888 goto theend;
4889 }
4890 }
4891
4892 // Compile the expression. Temporarily hide the new local
4893 // variable here, it is not available to this expression.
4894 if (new_local)
4895 --cctx->ctx_locals.ga_len;
4896 instr_count = instr->ga_len;
4897 p = skipwhite(op + oplen);
4898 r = compile_expr0(&p, cctx);
4899 if (new_local)
4900 ++cctx->ctx_locals.ga_len;
4901 if (r == FAIL)
4902 goto theend;
4903 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004904 else if (semicolon && var_idx == var_count - 1)
4905 {
4906 // For "[var; var] = expr" get the rest of the list
4907 if (generate_SLICE(cctx, var_count - 1) == FAIL)
4908 goto theend;
4909 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004910 else
4911 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004912 // For "[var, var] = expr" get the "var_idx" item from the
4913 // list.
4914 if (generate_GETITEM(cctx, var_idx) == FAIL)
4915 return FAIL;
4916 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004917
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004918 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004919 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004920 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004921 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004922 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004923 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004924 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004925 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004926 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004927 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004928 }
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004929 else if ((stacktype->tt_type == VAR_FUNC
4930 || stacktype->tt_type == VAR_PARTIAL)
4931 && var_wrong_func_name(name, TRUE))
4932 {
4933 goto theend;
4934 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004935 else
4936 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004937 // An empty list or dict has a &t_void member,
4938 // for a variable that implies &t_any.
4939 if (stacktype == &t_list_empty)
4940 lvar->lv_type = &t_list_any;
4941 else if (stacktype == &t_dict_empty)
4942 lvar->lv_type = &t_dict_any;
4943 else
4944 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004945 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004946 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02004947 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004948 {
4949 type_T *use_type = lvar->lv_type;
4950
Bram Moolenaar93ad1472020-08-19 22:02:41 +02004951 // without operator type is here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004952 if (has_index)
4953 {
4954 use_type = use_type->tt_member;
4955 if (use_type == NULL)
4956 use_type = &t_void;
4957 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004958 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar93ad1472020-08-19 22:02:41 +02004959 == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004960 goto theend;
4961 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004962 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004963 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004964 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004965 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004966 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004967 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004968 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004969 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004970 goto theend;
4971 }
4972 else if (!has_type || dest == dest_option)
4973 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004974 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004975 goto theend;
4976 }
4977 else
4978 {
4979 // variables are always initialized
4980 if (ga_grow(instr, 1) == FAIL)
4981 goto theend;
4982 switch (member_type->tt_type)
4983 {
4984 case VAR_BOOL:
4985 generate_PUSHBOOL(cctx, VVAL_FALSE);
4986 break;
4987 case VAR_FLOAT:
4988#ifdef FEAT_FLOAT
4989 generate_PUSHF(cctx, 0.0);
4990#endif
4991 break;
4992 case VAR_STRING:
4993 generate_PUSHS(cctx, NULL);
4994 break;
4995 case VAR_BLOB:
4996 generate_PUSHBLOB(cctx, NULL);
4997 break;
4998 case VAR_FUNC:
4999 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5000 break;
5001 case VAR_LIST:
5002 generate_NEWLIST(cctx, 0);
5003 break;
5004 case VAR_DICT:
5005 generate_NEWDICT(cctx, 0);
5006 break;
5007 case VAR_JOB:
5008 generate_PUSHJOB(cctx, NULL);
5009 break;
5010 case VAR_CHANNEL:
5011 generate_PUSHCHANNEL(cctx, NULL);
5012 break;
5013 case VAR_NUMBER:
5014 case VAR_UNKNOWN:
5015 case VAR_ANY:
5016 case VAR_PARTIAL:
5017 case VAR_VOID:
5018 case VAR_SPECIAL: // cannot happen
5019 generate_PUSHNR(cctx, 0);
5020 break;
5021 }
5022 }
5023 if (var_count == 0)
5024 end = p;
5025 }
5026
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005027 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005028 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005029 break;
5030
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005031 if (oplen > 0 && *op != '=')
5032 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005033 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005034 type_T *stacktype;
5035
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005036 if (*op == '.')
5037 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005038 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005039 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005040 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005041 if (
5042#ifdef FEAT_FLOAT
5043 // If variable is float operation with number is OK.
5044 !(expected == &t_float && stacktype == &t_number) &&
5045#endif
5046 need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005047 goto theend;
5048
5049 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005050 {
5051 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5052 goto theend;
5053 }
5054 else if (*op == '+')
5055 {
5056 if (generate_add_instr(cctx,
5057 operator_type(member_type, stacktype),
5058 member_type, stacktype) == FAIL)
5059 goto theend;
5060 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005061 else if (generate_two_op(cctx, op) == FAIL)
5062 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005063 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005064
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005065 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005066 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005067 int r;
5068
5069 // Compile the "idx" in "var[idx]".
5070 if (new_local)
5071 --cctx->ctx_locals.ga_len;
5072 p = skipwhite(var_start + varlen + 1);
5073 r = compile_expr0(&p, cctx);
5074 if (new_local)
5075 ++cctx->ctx_locals.ga_len;
5076 if (r == FAIL)
5077 goto theend;
5078 if (*skipwhite(p) != ']')
5079 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005080 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005081 emsg(_(e_missbrac));
5082 goto theend;
5083 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005084 if (type == &t_any)
5085 {
5086 type_T *idx_type = ((type_T **)stack->ga_data)[
5087 stack->ga_len - 1];
5088 // Index on variable of unknown type: guess the type from the
5089 // index type: number is dict, otherwise dict.
5090 // TODO: should do the assignment at runtime
5091 if (idx_type->tt_type == VAR_NUMBER)
5092 type = &t_list_any;
5093 else
5094 type = &t_dict_any;
5095 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005096 if (type->tt_type == VAR_DICT
5097 && may_generate_2STRING(-1, cctx) == FAIL)
5098 goto theend;
5099 if (type->tt_type == VAR_LIST
5100 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005101 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005102 {
5103 emsg(_(e_number_exp));
5104 goto theend;
5105 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005106
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005107 // Load the dict or list. On the stack we then have:
5108 // - value
5109 // - index
5110 // - variable
5111 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005112
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005113 if (type->tt_type == VAR_LIST)
5114 {
5115 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5116 return FAIL;
5117 }
5118 else if (type->tt_type == VAR_DICT)
5119 {
5120 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5121 return FAIL;
5122 }
5123 else
5124 {
5125 emsg(_(e_listreq));
5126 goto theend;
5127 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005128 }
5129 else
5130 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005131 switch (dest)
5132 {
5133 case dest_option:
5134 generate_STOREOPT(cctx, name + 1, opt_flags);
5135 break;
5136 case dest_global:
5137 // include g: with the name, easier to execute that way
5138 generate_STORE(cctx, ISN_STOREG, 0, name);
5139 break;
5140 case dest_buffer:
5141 // include b: with the name, easier to execute that way
5142 generate_STORE(cctx, ISN_STOREB, 0, name);
5143 break;
5144 case dest_window:
5145 // include w: with the name, easier to execute that way
5146 generate_STORE(cctx, ISN_STOREW, 0, name);
5147 break;
5148 case dest_tab:
5149 // include t: with the name, easier to execute that way
5150 generate_STORE(cctx, ISN_STORET, 0, name);
5151 break;
5152 case dest_env:
5153 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5154 break;
5155 case dest_reg:
5156 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5157 break;
5158 case dest_vimvar:
5159 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5160 break;
5161 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005162 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005163 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005164 {
5165 char_u *name_s = name;
5166
5167 // Include s: in the name for store_var()
5168 if (name[1] != ':')
5169 {
5170 int len = (int)STRLEN(name) + 3;
5171
5172 name_s = alloc(len);
5173 if (name_s == NULL)
5174 name_s = name;
5175 else
5176 vim_snprintf((char *)name_s, len,
5177 "s:%s", name);
5178 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005179 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5180 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005181 if (name_s != name)
5182 vim_free(name_s);
5183 }
5184 else
5185 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005186 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005187 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005188 break;
5189 case dest_local:
5190 if (lvar != NULL)
5191 {
5192 isn_T *isn = ((isn_T *)instr->ga_data)
5193 + instr->ga_len - 1;
5194
5195 // optimization: turn "var = 123" from ISN_PUSHNR +
5196 // ISN_STORE into ISN_STORENR
5197 if (!lvar->lv_from_outer
5198 && instr->ga_len == instr_count + 1
5199 && isn->isn_type == ISN_PUSHNR)
5200 {
5201 varnumber_T val = isn->isn_arg.number;
5202
5203 isn->isn_type = ISN_STORENR;
5204 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5205 isn->isn_arg.storenr.stnr_val = val;
5206 if (stack->ga_len > 0)
5207 --stack->ga_len;
5208 }
5209 else if (lvar->lv_from_outer)
5210 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005211 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005212 else
5213 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5214 }
5215 break;
5216 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005217 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005218
5219 if (var_idx + 1 < var_count)
5220 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005221 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005222
5223 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005224 if (var_count > 0 && !semicolon)
5225 {
5226 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5227 goto theend;
5228 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005229
Bram Moolenaarb2097502020-07-19 17:17:02 +02005230 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005231
5232theend:
5233 vim_free(name);
5234 return ret;
5235}
5236
5237/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005238 * Check if "name" can be "unlet".
5239 */
5240 int
5241check_vim9_unlet(char_u *name)
5242{
5243 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5244 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005245 // "unlet s:var" is allowed in legacy script.
5246 if (*name == 's' && !script_is_vim9())
5247 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005248 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005249 return FAIL;
5250 }
5251 return OK;
5252}
5253
5254/*
5255 * Callback passed to ex_unletlock().
5256 */
5257 static int
5258compile_unlet(
5259 lval_T *lvp,
5260 char_u *name_end,
5261 exarg_T *eap,
5262 int deep UNUSED,
5263 void *coookie)
5264{
5265 cctx_T *cctx = coookie;
5266
5267 if (lvp->ll_tv == NULL)
5268 {
5269 char_u *p = lvp->ll_name;
5270 int cc = *name_end;
5271 int ret = OK;
5272
5273 // Normal name. Only supports g:, w:, t: and b: namespaces.
5274 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005275 if (*p == '$')
5276 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5277 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005278 ret = FAIL;
5279 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005280 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005281
5282 *name_end = cc;
5283 return ret;
5284 }
5285
5286 // TODO: unlet {list}[idx]
5287 // TODO: unlet {dict}[key]
5288 emsg("Sorry, :unlet not fully implemented yet");
5289 return FAIL;
5290}
5291
5292/*
5293 * compile "unlet var", "lock var" and "unlock var"
5294 * "arg" points to "var".
5295 */
5296 static char_u *
5297compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5298{
5299 char_u *p = arg;
5300
5301 if (eap->cmdidx != CMD_unlet)
5302 {
5303 emsg("Sorry, :lock and unlock not implemented yet");
5304 return NULL;
5305 }
5306
5307 if (*p == '!')
5308 {
5309 p = skipwhite(p + 1);
5310 eap->forceit = TRUE;
5311 }
5312
5313 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5314 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5315}
5316
5317/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005318 * Compile an :import command.
5319 */
5320 static char_u *
5321compile_import(char_u *arg, cctx_T *cctx)
5322{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005323 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005324}
5325
5326/*
5327 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5328 */
5329 static int
5330compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5331{
5332 garray_T *instr = &cctx->ctx_instr;
5333 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5334
5335 if (endlabel == NULL)
5336 return FAIL;
5337 endlabel->el_next = *el;
5338 *el = endlabel;
5339 endlabel->el_end_label = instr->ga_len;
5340
5341 generate_JUMP(cctx, when, 0);
5342 return OK;
5343}
5344
5345 static void
5346compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5347{
5348 garray_T *instr = &cctx->ctx_instr;
5349
5350 while (*el != NULL)
5351 {
5352 endlabel_T *cur = (*el);
5353 isn_T *isn;
5354
5355 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5356 isn->isn_arg.jump.jump_where = instr->ga_len;
5357 *el = cur->el_next;
5358 vim_free(cur);
5359 }
5360}
5361
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005362 static void
5363compile_free_jump_to_end(endlabel_T **el)
5364{
5365 while (*el != NULL)
5366 {
5367 endlabel_T *cur = (*el);
5368
5369 *el = cur->el_next;
5370 vim_free(cur);
5371 }
5372}
5373
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005374/*
5375 * Create a new scope and set up the generic items.
5376 */
5377 static scope_T *
5378new_scope(cctx_T *cctx, scopetype_T type)
5379{
5380 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5381
5382 if (scope == NULL)
5383 return NULL;
5384 scope->se_outer = cctx->ctx_scope;
5385 cctx->ctx_scope = scope;
5386 scope->se_type = type;
5387 scope->se_local_count = cctx->ctx_locals.ga_len;
5388 return scope;
5389}
5390
5391/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005392 * Free the current scope and go back to the outer scope.
5393 */
5394 static void
5395drop_scope(cctx_T *cctx)
5396{
5397 scope_T *scope = cctx->ctx_scope;
5398
5399 if (scope == NULL)
5400 {
5401 iemsg("calling drop_scope() without a scope");
5402 return;
5403 }
5404 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005405 switch (scope->se_type)
5406 {
5407 case IF_SCOPE:
5408 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5409 case FOR_SCOPE:
5410 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5411 case WHILE_SCOPE:
5412 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5413 case TRY_SCOPE:
5414 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5415 case NO_SCOPE:
5416 case BLOCK_SCOPE:
5417 break;
5418 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005419 vim_free(scope);
5420}
5421
5422/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005423 * compile "if expr"
5424 *
5425 * "if expr" Produces instructions:
5426 * EVAL expr Push result of "expr"
5427 * JUMP_IF_FALSE end
5428 * ... body ...
5429 * end:
5430 *
5431 * "if expr | else" Produces instructions:
5432 * EVAL expr Push result of "expr"
5433 * JUMP_IF_FALSE else
5434 * ... body ...
5435 * JUMP_ALWAYS end
5436 * else:
5437 * ... body ...
5438 * end:
5439 *
5440 * "if expr1 | elseif expr2 | else" Produces instructions:
5441 * EVAL expr Push result of "expr"
5442 * JUMP_IF_FALSE elseif
5443 * ... body ...
5444 * JUMP_ALWAYS end
5445 * elseif:
5446 * EVAL expr Push result of "expr"
5447 * JUMP_IF_FALSE else
5448 * ... body ...
5449 * JUMP_ALWAYS end
5450 * else:
5451 * ... body ...
5452 * end:
5453 */
5454 static char_u *
5455compile_if(char_u *arg, cctx_T *cctx)
5456{
5457 char_u *p = arg;
5458 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005459 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005460 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005461 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005462 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005463
Bram Moolenaara5565e42020-05-09 15:44:01 +02005464 CLEAR_FIELD(ppconst);
5465 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005466 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005467 clear_ppconst(&ppconst);
5468 return NULL;
5469 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005470 if (cctx->ctx_skip == SKIP_YES)
5471 clear_ppconst(&ppconst);
5472 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005473 {
5474 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005475 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005476 clear_ppconst(&ppconst);
5477 }
5478 else
5479 {
5480 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005481 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005482 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005483 return NULL;
5484 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005485
5486 scope = new_scope(cctx, IF_SCOPE);
5487 if (scope == NULL)
5488 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005489 scope->se_skip_save = skip_save;
5490 // "is_had_return" will be reset if any block does not end in :return
5491 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005492
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005493 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005494 {
5495 // "where" is set when ":elseif", "else" or ":endif" is found
5496 scope->se_u.se_if.is_if_label = instr->ga_len;
5497 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5498 }
5499 else
5500 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005501
5502 return p;
5503}
5504
5505 static char_u *
5506compile_elseif(char_u *arg, cctx_T *cctx)
5507{
5508 char_u *p = arg;
5509 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005510 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005511 isn_T *isn;
5512 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005513 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005514
5515 if (scope == NULL || scope->se_type != IF_SCOPE)
5516 {
5517 emsg(_(e_elseif_without_if));
5518 return NULL;
5519 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005520 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005521 if (!cctx->ctx_had_return)
5522 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005523
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005524 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005525 {
5526 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005527 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005528 return NULL;
5529 // previous "if" or "elseif" jumps here
5530 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5531 isn->isn_arg.jump.jump_where = instr->ga_len;
5532 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005533
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005534 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005535 CLEAR_FIELD(ppconst);
5536 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005537 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005538 clear_ppconst(&ppconst);
5539 return NULL;
5540 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005541 if (scope->se_skip_save == SKIP_YES)
5542 clear_ppconst(&ppconst);
5543 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005544 {
5545 // The expression results in a constant.
5546 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005547 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005548 clear_ppconst(&ppconst);
5549 scope->se_u.se_if.is_if_label = -1;
5550 }
5551 else
5552 {
5553 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005554 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005555 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005556 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005557
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005558 // "where" is set when ":elseif", "else" or ":endif" is found
5559 scope->se_u.se_if.is_if_label = instr->ga_len;
5560 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5561 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005562
5563 return p;
5564}
5565
5566 static char_u *
5567compile_else(char_u *arg, cctx_T *cctx)
5568{
5569 char_u *p = arg;
5570 garray_T *instr = &cctx->ctx_instr;
5571 isn_T *isn;
5572 scope_T *scope = cctx->ctx_scope;
5573
5574 if (scope == NULL || scope->se_type != IF_SCOPE)
5575 {
5576 emsg(_(e_else_without_if));
5577 return NULL;
5578 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005579 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005580 if (!cctx->ctx_had_return)
5581 scope->se_u.se_if.is_had_return = FALSE;
5582 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005583
Bram Moolenaarefd88552020-06-18 20:50:10 +02005584 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005585 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005586 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005587 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005588 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005589 if (!cctx->ctx_had_return
5590 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5591 JUMP_ALWAYS, cctx) == FAIL)
5592 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005593 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005594
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005595 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005596 {
5597 if (scope->se_u.se_if.is_if_label >= 0)
5598 {
5599 // previous "if" or "elseif" jumps here
5600 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5601 isn->isn_arg.jump.jump_where = instr->ga_len;
5602 scope->se_u.se_if.is_if_label = -1;
5603 }
5604 }
5605
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005606 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005607 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5608 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005609
5610 return p;
5611}
5612
5613 static char_u *
5614compile_endif(char_u *arg, cctx_T *cctx)
5615{
5616 scope_T *scope = cctx->ctx_scope;
5617 ifscope_T *ifscope;
5618 garray_T *instr = &cctx->ctx_instr;
5619 isn_T *isn;
5620
5621 if (scope == NULL || scope->se_type != IF_SCOPE)
5622 {
5623 emsg(_(e_endif_without_if));
5624 return NULL;
5625 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005626 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005627 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005628 if (!cctx->ctx_had_return)
5629 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005630
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005631 if (scope->se_u.se_if.is_if_label >= 0)
5632 {
5633 // previous "if" or "elseif" jumps here
5634 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5635 isn->isn_arg.jump.jump_where = instr->ga_len;
5636 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005637 // Fill in the "end" label in jumps at the end of the blocks.
5638 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005639 cctx->ctx_skip = scope->se_skip_save;
5640
5641 // If all the blocks end in :return and there is an :else then the
5642 // had_return flag is set.
5643 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005644
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005645 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005646 return arg;
5647}
5648
5649/*
5650 * compile "for var in expr"
5651 *
5652 * Produces instructions:
5653 * PUSHNR -1
5654 * STORE loop-idx Set index to -1
5655 * EVAL expr Push result of "expr"
5656 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5657 * - if beyond end, jump to "end"
5658 * - otherwise get item from list and push it
5659 * STORE var Store item in "var"
5660 * ... body ...
5661 * JUMP top Jump back to repeat
5662 * end: DROP Drop the result of "expr"
5663 *
5664 */
5665 static char_u *
5666compile_for(char_u *arg, cctx_T *cctx)
5667{
5668 char_u *p;
5669 size_t varlen;
5670 garray_T *instr = &cctx->ctx_instr;
5671 garray_T *stack = &cctx->ctx_type_stack;
5672 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005673 lvar_T *loop_lvar; // loop iteration variable
5674 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005675 type_T *vartype;
5676
5677 // TODO: list of variables: "for [key, value] in dict"
5678 // parse "var"
5679 for (p = arg; eval_isnamec1(*p); ++p)
5680 ;
5681 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005682 var_lvar = lookup_local(arg, varlen, cctx);
5683 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005684 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005685 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005686 return NULL;
5687 }
5688
5689 // consume "in"
5690 p = skipwhite(p);
5691 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5692 {
5693 emsg(_(e_missing_in));
5694 return NULL;
5695 }
5696 p = skipwhite(p + 2);
5697
5698
5699 scope = new_scope(cctx, FOR_SCOPE);
5700 if (scope == NULL)
5701 return NULL;
5702
5703 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005704 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5705 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005706 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005707 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005708 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005709 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005710 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005711
5712 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005713 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5714 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005715 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005716 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005717 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005718 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005719 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005720
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005721 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005722
5723 // compile "expr", it remains on the stack until "endfor"
5724 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005725 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005726 {
5727 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005728 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005729 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005730
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005731 // Now that we know the type of "var", check that it is a list, now or at
5732 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005733 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005734 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005735 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005736 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005737 return NULL;
5738 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005739 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005740 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005741
5742 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005743 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005744
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005745 generate_FOR(cctx, loop_lvar->lv_idx);
5746 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005747
5748 return arg;
5749}
5750
5751/*
5752 * compile "endfor"
5753 */
5754 static char_u *
5755compile_endfor(char_u *arg, cctx_T *cctx)
5756{
5757 garray_T *instr = &cctx->ctx_instr;
5758 scope_T *scope = cctx->ctx_scope;
5759 forscope_T *forscope;
5760 isn_T *isn;
5761
5762 if (scope == NULL || scope->se_type != FOR_SCOPE)
5763 {
5764 emsg(_(e_for));
5765 return NULL;
5766 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005767 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005768 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005769 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005770
5771 // At end of ":for" scope jump back to the FOR instruction.
5772 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5773
5774 // Fill in the "end" label in the FOR statement so it can jump here
5775 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5776 isn->isn_arg.forloop.for_end = instr->ga_len;
5777
5778 // Fill in the "end" label any BREAK statements
5779 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5780
5781 // Below the ":for" scope drop the "expr" list from the stack.
5782 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5783 return NULL;
5784
5785 vim_free(scope);
5786
5787 return arg;
5788}
5789
5790/*
5791 * compile "while expr"
5792 *
5793 * Produces instructions:
5794 * top: EVAL expr Push result of "expr"
5795 * JUMP_IF_FALSE end jump if false
5796 * ... body ...
5797 * JUMP top Jump back to repeat
5798 * end:
5799 *
5800 */
5801 static char_u *
5802compile_while(char_u *arg, cctx_T *cctx)
5803{
5804 char_u *p = arg;
5805 garray_T *instr = &cctx->ctx_instr;
5806 scope_T *scope;
5807
5808 scope = new_scope(cctx, WHILE_SCOPE);
5809 if (scope == NULL)
5810 return NULL;
5811
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005812 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005813
5814 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005815 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005816 return NULL;
5817
5818 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005819 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005820 JUMP_IF_FALSE, cctx) == FAIL)
5821 return FAIL;
5822
5823 return p;
5824}
5825
5826/*
5827 * compile "endwhile"
5828 */
5829 static char_u *
5830compile_endwhile(char_u *arg, cctx_T *cctx)
5831{
5832 scope_T *scope = cctx->ctx_scope;
5833
5834 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5835 {
5836 emsg(_(e_while));
5837 return NULL;
5838 }
5839 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005840 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005841
5842 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005843 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005844
5845 // Fill in the "end" label in the WHILE statement so it can jump here.
5846 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005847 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005848
5849 vim_free(scope);
5850
5851 return arg;
5852}
5853
5854/*
5855 * compile "continue"
5856 */
5857 static char_u *
5858compile_continue(char_u *arg, cctx_T *cctx)
5859{
5860 scope_T *scope = cctx->ctx_scope;
5861
5862 for (;;)
5863 {
5864 if (scope == NULL)
5865 {
5866 emsg(_(e_continue));
5867 return NULL;
5868 }
5869 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5870 break;
5871 scope = scope->se_outer;
5872 }
5873
5874 // Jump back to the FOR or WHILE instruction.
5875 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005876 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5877 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005878 return arg;
5879}
5880
5881/*
5882 * compile "break"
5883 */
5884 static char_u *
5885compile_break(char_u *arg, cctx_T *cctx)
5886{
5887 scope_T *scope = cctx->ctx_scope;
5888 endlabel_T **el;
5889
5890 for (;;)
5891 {
5892 if (scope == NULL)
5893 {
5894 emsg(_(e_break));
5895 return NULL;
5896 }
5897 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5898 break;
5899 scope = scope->se_outer;
5900 }
5901
5902 // Jump to the end of the FOR or WHILE loop.
5903 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005904 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005905 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005906 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005907 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5908 return FAIL;
5909
5910 return arg;
5911}
5912
5913/*
5914 * compile "{" start of block
5915 */
5916 static char_u *
5917compile_block(char_u *arg, cctx_T *cctx)
5918{
5919 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5920 return NULL;
5921 return skipwhite(arg + 1);
5922}
5923
5924/*
5925 * compile end of block: drop one scope
5926 */
5927 static void
5928compile_endblock(cctx_T *cctx)
5929{
5930 scope_T *scope = cctx->ctx_scope;
5931
5932 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005933 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005934 vim_free(scope);
5935}
5936
5937/*
5938 * compile "try"
5939 * Creates a new scope for the try-endtry, pointing to the first catch and
5940 * finally.
5941 * Creates another scope for the "try" block itself.
5942 * TRY instruction sets up exception handling at runtime.
5943 *
5944 * "try"
5945 * TRY -> catch1, -> finally push trystack entry
5946 * ... try block
5947 * "throw {exception}"
5948 * EVAL {exception}
5949 * THROW create exception
5950 * ... try block
5951 * " catch {expr}"
5952 * JUMP -> finally
5953 * catch1: PUSH exeception
5954 * EVAL {expr}
5955 * MATCH
5956 * JUMP nomatch -> catch2
5957 * CATCH remove exception
5958 * ... catch block
5959 * " catch"
5960 * JUMP -> finally
5961 * catch2: CATCH remove exception
5962 * ... catch block
5963 * " finally"
5964 * finally:
5965 * ... finally block
5966 * " endtry"
5967 * ENDTRY pop trystack entry, may rethrow
5968 */
5969 static char_u *
5970compile_try(char_u *arg, cctx_T *cctx)
5971{
5972 garray_T *instr = &cctx->ctx_instr;
5973 scope_T *try_scope;
5974 scope_T *scope;
5975
5976 // scope that holds the jumps that go to catch/finally/endtry
5977 try_scope = new_scope(cctx, TRY_SCOPE);
5978 if (try_scope == NULL)
5979 return NULL;
5980
5981 // "catch" is set when the first ":catch" is found.
5982 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005983 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005984 if (generate_instr(cctx, ISN_TRY) == NULL)
5985 return NULL;
5986
5987 // scope for the try block itself
5988 scope = new_scope(cctx, BLOCK_SCOPE);
5989 if (scope == NULL)
5990 return NULL;
5991
5992 return arg;
5993}
5994
5995/*
5996 * compile "catch {expr}"
5997 */
5998 static char_u *
5999compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6000{
6001 scope_T *scope = cctx->ctx_scope;
6002 garray_T *instr = &cctx->ctx_instr;
6003 char_u *p;
6004 isn_T *isn;
6005
6006 // end block scope from :try or :catch
6007 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6008 compile_endblock(cctx);
6009 scope = cctx->ctx_scope;
6010
6011 // Error if not in a :try scope
6012 if (scope == NULL || scope->se_type != TRY_SCOPE)
6013 {
6014 emsg(_(e_catch));
6015 return NULL;
6016 }
6017
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006018 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006019 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006020 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006021 return NULL;
6022 }
6023
6024 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006025 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006026 JUMP_ALWAYS, cctx) == FAIL)
6027 return NULL;
6028
6029 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006030 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006031 if (isn->isn_arg.try.try_catch == 0)
6032 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006033 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006034 {
6035 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006036 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006037 isn->isn_arg.jump.jump_where = instr->ga_len;
6038 }
6039
6040 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006041 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006042 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006043 scope->se_u.se_try.ts_caught_all = TRUE;
6044 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006045 }
6046 else
6047 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006048 char_u *end;
6049 char_u *pat;
6050 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006051 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006052 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006053
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006054 // Push v:exception, push {expr} and MATCH
6055 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6056
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006057 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006058 if (*end != *p)
6059 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006060 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006061 vim_free(tofree);
6062 return FAIL;
6063 }
6064 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006065 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006066 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006067 len = (int)(end - tofree);
6068 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006069 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006070 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006071 if (pat == NULL)
6072 return FAIL;
6073 if (generate_PUSHS(cctx, pat) == FAIL)
6074 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006075
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006076 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6077 return NULL;
6078
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006079 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006080 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6081 return NULL;
6082 }
6083
6084 if (generate_instr(cctx, ISN_CATCH) == NULL)
6085 return NULL;
6086
6087 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6088 return NULL;
6089 return p;
6090}
6091
6092 static char_u *
6093compile_finally(char_u *arg, cctx_T *cctx)
6094{
6095 scope_T *scope = cctx->ctx_scope;
6096 garray_T *instr = &cctx->ctx_instr;
6097 isn_T *isn;
6098
6099 // end block scope from :try or :catch
6100 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6101 compile_endblock(cctx);
6102 scope = cctx->ctx_scope;
6103
6104 // Error if not in a :try scope
6105 if (scope == NULL || scope->se_type != TRY_SCOPE)
6106 {
6107 emsg(_(e_finally));
6108 return NULL;
6109 }
6110
6111 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006112 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006113 if (isn->isn_arg.try.try_finally != 0)
6114 {
6115 emsg(_(e_finally_dup));
6116 return NULL;
6117 }
6118
6119 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006120 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006121
Bram Moolenaar585fea72020-04-02 22:33:21 +02006122 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006123 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006124 {
6125 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006126 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006127 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006128 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006129 }
6130
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006131 // TODO: set index in ts_finally_label jumps
6132
6133 return arg;
6134}
6135
6136 static char_u *
6137compile_endtry(char_u *arg, cctx_T *cctx)
6138{
6139 scope_T *scope = cctx->ctx_scope;
6140 garray_T *instr = &cctx->ctx_instr;
6141 isn_T *isn;
6142
6143 // end block scope from :catch or :finally
6144 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6145 compile_endblock(cctx);
6146 scope = cctx->ctx_scope;
6147
6148 // Error if not in a :try scope
6149 if (scope == NULL || scope->se_type != TRY_SCOPE)
6150 {
6151 if (scope == NULL)
6152 emsg(_(e_no_endtry));
6153 else if (scope->se_type == WHILE_SCOPE)
6154 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006155 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006156 emsg(_(e_endfor));
6157 else
6158 emsg(_(e_endif));
6159 return NULL;
6160 }
6161
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006162 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006163 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6164 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006165 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006166 return NULL;
6167 }
6168
6169 // Fill in the "end" label in jumps at the end of the blocks, if not done
6170 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006171 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006172
6173 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006174 if (isn->isn_arg.try.try_catch == 0)
6175 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006176 if (isn->isn_arg.try.try_finally == 0)
6177 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006178
6179 if (scope->se_u.se_try.ts_catch_label != 0)
6180 {
6181 // Last catch without match jumps here
6182 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6183 isn->isn_arg.jump.jump_where = instr->ga_len;
6184 }
6185
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006186 compile_endblock(cctx);
6187
6188 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6189 return NULL;
6190 return arg;
6191}
6192
6193/*
6194 * compile "throw {expr}"
6195 */
6196 static char_u *
6197compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6198{
6199 char_u *p = skipwhite(arg);
6200
Bram Moolenaara5565e42020-05-09 15:44:01 +02006201 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006202 return NULL;
6203 if (may_generate_2STRING(-1, cctx) == FAIL)
6204 return NULL;
6205 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6206 return NULL;
6207
6208 return p;
6209}
6210
6211/*
6212 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006213 * compile "echomsg expr"
6214 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006215 * compile "execute expr"
6216 */
6217 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006218compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006219{
6220 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006221 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006222 int count = 0;
6223
6224 for (;;)
6225 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006226 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006227 return NULL;
6228 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006229 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006230 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006231 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006232 break;
6233 }
6234
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006235 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6236 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6237 else if (cmdidx == CMD_execute)
6238 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6239 else if (cmdidx == CMD_echomsg)
6240 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6241 else
6242 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006243 return p;
6244}
6245
6246/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006247 * A command that is not compiled, execute with legacy code.
6248 */
6249 static char_u *
6250compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6251{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006252 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006253 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006254 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006255
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006256 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006257 goto theend;
6258
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006259 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006260 {
6261 long argt = excmd_get_argt(eap->cmdidx);
6262 int usefilter = FALSE;
6263
6264 has_expr = argt & (EX_XFILE | EX_EXPAND);
6265
6266 // If the command can be followed by a bar, find the bar and truncate
6267 // it, so that the following command can be compiled.
6268 // The '|' is overwritten with a NUL, it is put back below.
6269 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6270 && *eap->arg == '!')
6271 // :w !filter or :r !filter or :r! filter
6272 usefilter = TRUE;
6273 if ((argt & EX_TRLBAR) && !usefilter)
6274 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006275 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006276 separate_nextcmd(eap);
6277 if (eap->nextcmd != NULL)
6278 nextcmd = eap->nextcmd;
6279 }
6280 }
6281
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006282 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6283 {
6284 // expand filename in "syntax include [@group] filename"
6285 has_expr = TRUE;
6286 eap->arg = skipwhite(eap->arg + 7);
6287 if (*eap->arg == '@')
6288 eap->arg = skiptowhite(eap->arg);
6289 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006290
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006291 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006292 {
6293 int count = 0;
6294 char_u *start = skipwhite(line);
6295
6296 // :cmd xxx`=expr1`yyy`=expr2`zzz
6297 // PUSHS ":cmd xxx"
6298 // eval expr1
6299 // PUSHS "yyy"
6300 // eval expr2
6301 // PUSHS "zzz"
6302 // EXECCONCAT 5
6303 for (;;)
6304 {
6305 if (p > start)
6306 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006307 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006308 ++count;
6309 }
6310 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006311 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006312 return NULL;
6313 may_generate_2STRING(-1, cctx);
6314 ++count;
6315 p = skipwhite(p);
6316 if (*p != '`')
6317 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006318 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006319 return NULL;
6320 }
6321 start = p + 1;
6322
6323 p = (char_u *)strstr((char *)start, "`=");
6324 if (p == NULL)
6325 {
6326 if (*skipwhite(start) != NUL)
6327 {
6328 generate_PUSHS(cctx, vim_strsave(start));
6329 ++count;
6330 }
6331 break;
6332 }
6333 }
6334 generate_EXECCONCAT(cctx, count);
6335 }
6336 else
6337 generate_EXEC(cctx, line);
6338
6339theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006340 if (*nextcmd != NUL)
6341 {
6342 // the parser expects a pointer to the bar, put it back
6343 --nextcmd;
6344 *nextcmd = '|';
6345 }
6346
6347 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006348}
6349
6350/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006351 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006352 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006353 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006354 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006355add_def_function(ufunc_T *ufunc)
6356{
6357 dfunc_T *dfunc;
6358
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006359 if (def_functions.ga_len == 0)
6360 {
6361 // The first position is not used, so that a zero uf_dfunc_idx means it
6362 // wasn't set.
6363 if (ga_grow(&def_functions, 1) == FAIL)
6364 return FAIL;
6365 ++def_functions.ga_len;
6366 }
6367
Bram Moolenaar09689a02020-05-09 22:50:08 +02006368 // Add the function to "def_functions".
6369 if (ga_grow(&def_functions, 1) == FAIL)
6370 return FAIL;
6371 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6372 CLEAR_POINTER(dfunc);
6373 dfunc->df_idx = def_functions.ga_len;
6374 ufunc->uf_dfunc_idx = dfunc->df_idx;
6375 dfunc->df_ufunc = ufunc;
6376 ++def_functions.ga_len;
6377 return OK;
6378}
6379
6380/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006381 * After ex_function() has collected all the function lines: parse and compile
6382 * the lines into instructions.
6383 * Adds the function to "def_functions".
6384 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6385 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006386 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006387 * This can be used recursively through compile_lambda(), which may reallocate
6388 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006389 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006390 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006391 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006392compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006393{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006394 char_u *line = NULL;
6395 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006396 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006397 cctx_T cctx;
6398 garray_T *instr;
6399 int called_emsg_before = called_emsg;
6400 int ret = FAIL;
6401 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006402 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006403 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006404 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006405
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006406 // When using a function that was compiled before: Free old instructions.
6407 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006408 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006409 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006410 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6411 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006412 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006413 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006414 else
6415 {
6416 if (add_def_function(ufunc) == FAIL)
6417 return FAIL;
6418 new_def_function = TRUE;
6419 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006420
Bram Moolenaar985116a2020-07-12 17:31:09 +02006421 ufunc->uf_def_status = UF_COMPILING;
6422
Bram Moolenaara80faa82020-04-12 19:37:17 +02006423 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006424 cctx.ctx_ufunc = ufunc;
6425 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006426 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006427 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6428 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6429 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6430 cctx.ctx_type_list = &ufunc->uf_type_list;
6431 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6432 instr = &cctx.ctx_instr;
6433
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006434 // Set the context to the function, it may be compiled when called from
6435 // another script. Set the script version to the most modern one.
6436 // The line number will be set in next_line_from_context().
6437 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006438 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6439
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006440 // Make sure error messages are OK.
6441 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6442 if (do_estack_push)
6443 estack_push_ufunc(ufunc, 1);
6444
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006445 if (ufunc->uf_def_args.ga_len > 0)
6446 {
6447 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006448 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006449 int i;
6450 char_u *arg;
6451 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006452 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006453
6454 // Produce instructions for the default values of optional arguments.
6455 // Store the instruction index in uf_def_arg_idx[] so that we know
6456 // where to start when the function is called, depending on the number
6457 // of arguments.
6458 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6459 if (ufunc->uf_def_arg_idx == NULL)
6460 goto erret;
6461 for (i = 0; i < count; ++i)
6462 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006463 garray_T *stack = &cctx.ctx_type_stack;
6464 type_T *val_type;
6465 int arg_idx = first_def_arg + i;
6466
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006467 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6468 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006469 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006470 goto erret;
6471
6472 // If no type specified use the type of the default value.
6473 // Otherwise check that the default value type matches the
6474 // specified type.
6475 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6476 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006477 {
6478 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006479 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006480 }
Bram Moolenaare30f64b2020-07-15 19:48:20 +02006481 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006482 == FAIL)
6483 {
6484 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6485 arg_idx + 1);
6486 goto erret;
6487 }
6488
6489 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006490 goto erret;
6491 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006492 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006493
6494 if (did_set_arg_type)
6495 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006496 }
6497
6498 /*
6499 * Loop over all the lines of the function and generate instructions.
6500 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006501 for (;;)
6502 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006503 exarg_T ea;
6504 cmdmod_T save_cmdmod;
6505 int starts_with_colon = FALSE;
6506 char_u *cmd;
6507 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006508
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006509 // Bail out on the first error to avoid a flood of errors and report
6510 // the right line number when inside try/catch.
6511 if (emsg_before != called_emsg)
6512 goto erret;
6513
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006514 if (line != NULL && *line == '|')
6515 // the line continues after a '|'
6516 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006517 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006518 && !(*line == '#' && (line == cctx.ctx_line_start
6519 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006520 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006521 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006522 goto erret;
6523 }
6524 else
6525 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006526 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006527 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006528 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006529 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006530 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006531 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006532
Bram Moolenaara80faa82020-04-12 19:37:17 +02006533 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006534 ea.cmdlinep = &line;
6535 ea.cmd = skipwhite(line);
6536
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006537 // Some things can be recognized by the first character.
6538 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006539 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006540 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006541 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006542 if (ea.cmd[1] != '{')
6543 {
6544 line = (char_u *)"";
6545 continue;
6546 }
6547 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006548
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006549 case '}':
6550 {
6551 // "}" ends a block scope
6552 scopetype_T stype = cctx.ctx_scope == NULL
6553 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006554
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006555 if (stype == BLOCK_SCOPE)
6556 {
6557 compile_endblock(&cctx);
6558 line = ea.cmd;
6559 }
6560 else
6561 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006562 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006563 goto erret;
6564 }
6565 if (line != NULL)
6566 line = skipwhite(ea.cmd + 1);
6567 continue;
6568 }
6569
6570 case '{':
6571 // "{" starts a block scope
6572 // "{'a': 1}->func() is something else
6573 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6574 {
6575 line = compile_block(ea.cmd, &cctx);
6576 continue;
6577 }
6578 break;
6579
6580 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006581 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006582 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006583 }
6584
6585 /*
6586 * COMMAND MODIFIERS
6587 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006588 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006589 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6590 {
6591 if (errormsg != NULL)
6592 goto erret;
6593 // empty line or comment
6594 line = (char_u *)"";
6595 continue;
6596 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006597 // TODO: use modifiers in the command
6598 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006599 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006600
6601 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006602 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006603 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006604 {
6605 if (*ea.cmd == '(')
6606 // not for "call()"
6607 ea.cmd = p;
6608 else
6609 ea.cmd = skipwhite(ea.cmd);
6610 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006611
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006612 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006613 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006614 char_u *pskip;
6615
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006616 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006617 // find what follows.
6618 // Skip over "var.member", "var[idx]" and the like.
6619 // Also "&opt = val", "$ENV = val" and "@r = val".
6620 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006621 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006622 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006623 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006624 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006625 char_u *var_end;
6626 int oplen;
6627 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006628
Bram Moolenaar65821722020-08-02 18:58:54 +02006629 if (ea.cmd[0] == '@')
6630 var_end = ea.cmd + 2;
6631 else
6632 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006633 FNE_CHECK_START | FNE_INCL_BR);
6634 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006635 if (oplen > 0)
6636 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006637 size_t len = p - ea.cmd;
6638
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006639 // Recognize an assignment if we recognize the variable
6640 // name:
6641 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006642 // "local = expr" where "local" is a local var.
6643 // "script = expr" where "script" is a script-local var.
6644 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006645 // "&opt = expr"
6646 // "$ENV = expr"
6647 // "@r = expr"
6648 if (*ea.cmd == '&'
6649 || *ea.cmd == '$'
6650 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006651 || ((len) > 2 && ea.cmd[1] == ':')
6652 || lookup_local(ea.cmd, len, &cctx) != NULL
6653 || lookup_arg(ea.cmd, len, NULL, NULL,
6654 NULL, &cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02006655 || lookup_script(ea.cmd, len, FALSE) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006656 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006657 {
6658 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006659 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006660 goto erret;
6661 continue;
6662 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006663 }
6664 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006665
6666 if (*ea.cmd == '[')
6667 {
6668 // [var, var] = expr
6669 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6670 if (line == NULL)
6671 goto erret;
6672 if (line != ea.cmd)
6673 continue;
6674 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006675 }
6676
6677 /*
6678 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006679 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006680 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006681 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006682 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006683 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006684 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006685 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006686 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006687 if (!starts_with_colon)
6688 {
6689 emsg(_(e_colon_required_before_a_range));
6690 goto erret;
6691 }
6692 if (ends_excmd2(line, ea.cmd))
6693 {
6694 // A range without a command: jump to the line.
6695 // TODO: compile to a more efficient command, possibly
6696 // calling parse_cmd_address().
6697 ea.cmdidx = CMD_SIZE;
6698 line = compile_exec(line, &ea, &cctx);
6699 goto nextline;
6700 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006701 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006702 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006703 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006704 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006705 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006706
6707 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6708 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006709 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006710 {
6711 line += STRLEN(line);
6712 continue;
6713 }
6714
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006715 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006716 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006717 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006718 // CMD_let cannot happen, compile_assignment() above is used
6719 iemsg("Command from find_ex_command() not handled");
6720 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006721 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006722 }
6723
6724 p = skipwhite(p);
6725
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006726 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02006727 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006728 && ea.cmdidx != CMD_elseif
6729 && ea.cmdidx != CMD_else
6730 && ea.cmdidx != CMD_endif)
6731 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006732 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006733 continue;
6734 }
6735
Bram Moolenaarefd88552020-06-18 20:50:10 +02006736 if (ea.cmdidx != CMD_elseif
6737 && ea.cmdidx != CMD_else
6738 && ea.cmdidx != CMD_endif
6739 && ea.cmdidx != CMD_endfor
6740 && ea.cmdidx != CMD_endwhile
6741 && ea.cmdidx != CMD_catch
6742 && ea.cmdidx != CMD_finally
6743 && ea.cmdidx != CMD_endtry)
6744 {
6745 if (cctx.ctx_had_return)
6746 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006747 emsg(_(e_unreachable_code_after_return));
Bram Moolenaarefd88552020-06-18 20:50:10 +02006748 goto erret;
6749 }
6750 }
6751
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006752 switch (ea.cmdidx)
6753 {
6754 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006755 ea.arg = p;
6756 line = compile_nested_function(&ea, &cctx);
6757 break;
6758
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006759 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006760 // TODO: should we allow this, e.g. to declare a global
6761 // function?
6762 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006763 goto erret;
6764
6765 case CMD_return:
6766 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006767 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006768 break;
6769
6770 case CMD_let:
6771 case CMD_const:
6772 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006773 if (line == p)
6774 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006775 break;
6776
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006777 case CMD_unlet:
6778 case CMD_unlockvar:
6779 case CMD_lockvar:
6780 line = compile_unletlock(p, &ea, &cctx);
6781 break;
6782
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006783 case CMD_import:
6784 line = compile_import(p, &cctx);
6785 break;
6786
6787 case CMD_if:
6788 line = compile_if(p, &cctx);
6789 break;
6790 case CMD_elseif:
6791 line = compile_elseif(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_else:
6795 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006796 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006797 break;
6798 case CMD_endif:
6799 line = compile_endif(p, &cctx);
6800 break;
6801
6802 case CMD_while:
6803 line = compile_while(p, &cctx);
6804 break;
6805 case CMD_endwhile:
6806 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006807 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006808 break;
6809
6810 case CMD_for:
6811 line = compile_for(p, &cctx);
6812 break;
6813 case CMD_endfor:
6814 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006815 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006816 break;
6817 case CMD_continue:
6818 line = compile_continue(p, &cctx);
6819 break;
6820 case CMD_break:
6821 line = compile_break(p, &cctx);
6822 break;
6823
6824 case CMD_try:
6825 line = compile_try(p, &cctx);
6826 break;
6827 case CMD_catch:
6828 line = compile_catch(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_finally:
6832 line = compile_finally(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_endtry:
6836 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006837 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006838 break;
6839 case CMD_throw:
6840 line = compile_throw(p, &cctx);
6841 break;
6842
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006843 case CMD_eval:
6844 if (compile_expr0(&p, &cctx) == FAIL)
6845 goto erret;
6846
6847 // drop the return value
6848 generate_instr_drop(&cctx, ISN_DROP, 1);
6849
6850 line = skipwhite(p);
6851 break;
6852
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006853 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006854 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006855 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006856 case CMD_echomsg:
6857 case CMD_echoerr:
6858 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006859 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006860
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006861 // TODO: other commands with an expression argument
6862
Bram Moolenaarae616492020-07-28 20:07:27 +02006863 case CMD_append:
6864 case CMD_change:
6865 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02006866 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02006867 case CMD_xit:
6868 not_in_vim9(&ea);
6869 goto erret;
6870
Bram Moolenaar002262f2020-07-08 17:47:57 +02006871 case CMD_SIZE:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006872 semsg(_(e_invalid_command_str), ea.cmd);
Bram Moolenaar002262f2020-07-08 17:47:57 +02006873 goto erret;
6874
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006875 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006876 // Not recognized, execute with do_cmdline_cmd().
6877 ea.arg = p;
6878 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006879 break;
6880 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006881nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006882 if (line == NULL)
6883 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006884 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006885
6886 if (cctx.ctx_type_stack.ga_len < 0)
6887 {
6888 iemsg("Type stack underflow");
6889 goto erret;
6890 }
6891 }
6892
6893 if (cctx.ctx_scope != NULL)
6894 {
6895 if (cctx.ctx_scope->se_type == IF_SCOPE)
6896 emsg(_(e_endif));
6897 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6898 emsg(_(e_endwhile));
6899 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6900 emsg(_(e_endfor));
6901 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006902 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006903 goto erret;
6904 }
6905
Bram Moolenaarefd88552020-06-18 20:50:10 +02006906 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006907 {
6908 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6909 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006910 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006911 goto erret;
6912 }
6913
6914 // Return zero if there is no return at the end.
6915 generate_PUSHNR(&cctx, 0);
6916 generate_instr(&cctx, ISN_RETURN);
6917 }
6918
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006919 {
6920 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6921 + ufunc->uf_dfunc_idx;
6922 dfunc->df_deleted = FALSE;
6923 dfunc->df_instr = instr->ga_data;
6924 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006925 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006926 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006927 if (cctx.ctx_outer_used)
6928 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006929 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006930 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006931
6932 ret = OK;
6933
6934erret:
6935 if (ret == FAIL)
6936 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006937 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006938 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6939 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006940
6941 for (idx = 0; idx < instr->ga_len; ++idx)
6942 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006943 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006944
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006945 // If using the last entry in the table and it was added above, we
6946 // might as well remove it.
6947 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02006948 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006949 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006950 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006951 ufunc->uf_dfunc_idx = 0;
6952 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006953 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006954
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006955 while (cctx.ctx_scope != NULL)
6956 drop_scope(&cctx);
6957
Bram Moolenaar20431c92020-03-20 18:39:46 +01006958 // Don't execute this function body.
6959 ga_clear_strings(&ufunc->uf_lines);
6960
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006961 if (errormsg != NULL)
6962 emsg(errormsg);
6963 else if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006964 emsg(_(e_compile_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006965 }
6966
6967 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006968 if (do_estack_push)
6969 estack_pop();
6970
Bram Moolenaar20431c92020-03-20 18:39:46 +01006971 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006972 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006973 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02006974 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006975}
6976
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02006977 void
6978set_function_type(ufunc_T *ufunc)
6979{
6980 int varargs = ufunc->uf_va_name != NULL;
6981 int argcount = ufunc->uf_args.ga_len;
6982
6983 // Create a type for the function, with the return type and any
6984 // argument types.
6985 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6986 // The type is included in "tt_args".
6987 if (argcount > 0 || varargs)
6988 {
6989 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6990 argcount, &ufunc->uf_type_list);
6991 // Add argument types to the function type.
6992 if (func_type_add_arg_types(ufunc->uf_func_type,
6993 argcount + varargs,
6994 &ufunc->uf_type_list) == FAIL)
6995 return;
6996 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6997 ufunc->uf_func_type->tt_min_argcount =
6998 argcount - ufunc->uf_def_args.ga_len;
6999 if (ufunc->uf_arg_types == NULL)
7000 {
7001 int i;
7002
7003 // lambda does not have argument types.
7004 for (i = 0; i < argcount; ++i)
7005 ufunc->uf_func_type->tt_args[i] = &t_any;
7006 }
7007 else
7008 mch_memmove(ufunc->uf_func_type->tt_args,
7009 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7010 if (varargs)
7011 {
7012 ufunc->uf_func_type->tt_args[argcount] =
7013 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7014 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7015 }
7016 }
7017 else
7018 // No arguments, can use a predefined type.
7019 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7020 argcount, &ufunc->uf_type_list);
7021}
7022
7023
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007024/*
7025 * Delete an instruction, free what it contains.
7026 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007027 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007028delete_instr(isn_T *isn)
7029{
7030 switch (isn->isn_type)
7031 {
7032 case ISN_EXEC:
7033 case ISN_LOADENV:
7034 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007035 case ISN_LOADB:
7036 case ISN_LOADW:
7037 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007038 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007039 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007040 case ISN_PUSHEXC:
7041 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007042 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007043 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007044 case ISN_STOREB:
7045 case ISN_STOREW:
7046 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007047 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007048 vim_free(isn->isn_arg.string);
7049 break;
7050
7051 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007052 case ISN_STORES:
7053 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007054 break;
7055
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007056 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007057 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007058 vim_free(isn->isn_arg.unlet.ul_name);
7059 break;
7060
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007061 case ISN_STOREOPT:
7062 vim_free(isn->isn_arg.storeopt.so_name);
7063 break;
7064
7065 case ISN_PUSHBLOB: // push blob isn_arg.blob
7066 blob_unref(isn->isn_arg.blob);
7067 break;
7068
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007069 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007070#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007071 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007072#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007073 break;
7074
7075 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007076#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007077 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007078#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007079 break;
7080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007081 case ISN_UCALL:
7082 vim_free(isn->isn_arg.ufunc.cuf_name);
7083 break;
7084
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007085 case ISN_FUNCREF:
7086 {
7087 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7088 + isn->isn_arg.funcref.fr_func;
7089 func_ptr_unref(dfunc->df_ufunc);
7090 }
7091 break;
7092
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007093 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007094 {
7095 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7096 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7097
7098 if (ufunc != NULL)
7099 {
7100 // Clear uf_dfunc_idx so that the function is deleted.
7101 clear_def_function(ufunc);
7102 ufunc->uf_dfunc_idx = 0;
7103 func_ptr_unref(ufunc);
7104 }
7105
7106 vim_free(lambda);
7107 vim_free(isn->isn_arg.newfunc.nf_global);
7108 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007109 break;
7110
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007111 case ISN_2BOOL:
7112 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007113 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007114 case ISN_ADDBLOB:
7115 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007116 case ISN_ANYINDEX:
7117 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007118 case ISN_BCALL:
7119 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007120 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007121 case ISN_CHECKNR:
7122 case ISN_CHECKTYPE:
7123 case ISN_COMPAREANY:
7124 case ISN_COMPAREBLOB:
7125 case ISN_COMPAREBOOL:
7126 case ISN_COMPAREDICT:
7127 case ISN_COMPAREFLOAT:
7128 case ISN_COMPAREFUNC:
7129 case ISN_COMPARELIST:
7130 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007131 case ISN_COMPARESPECIAL:
7132 case ISN_COMPARESTRING:
7133 case ISN_CONCAT:
7134 case ISN_DCALL:
7135 case ISN_DROP:
7136 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007137 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007138 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007139 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007140 case ISN_EXECCONCAT:
7141 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007142 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007143 case ISN_GETITEM:
7144 case ISN_JUMP:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007145 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007146 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007147 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007148 case ISN_LOADBDICT:
7149 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007150 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007151 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007152 case ISN_LOADSCRIPT:
7153 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007154 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007155 case ISN_LOADWDICT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007156 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007157 case ISN_NEGATENR:
7158 case ISN_NEWDICT:
7159 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007160 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007161 case ISN_OPFLOAT:
7162 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007163 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007164 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007165 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007166 case ISN_PUSHF:
7167 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007168 case ISN_PUSHSPEC:
7169 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007170 case ISN_SHUFFLE:
7171 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007172 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007173 case ISN_STOREDICT:
7174 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007175 case ISN_STORENR:
7176 case ISN_STOREOUTER:
7177 case ISN_STOREREG:
7178 case ISN_STORESCRIPT:
7179 case ISN_STOREV:
7180 case ISN_STRINDEX:
7181 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007182 case ISN_THROW:
7183 case ISN_TRY:
7184 // nothing allocated
7185 break;
7186 }
7187}
7188
7189/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007190 * Free all instructions for "dfunc".
7191 */
7192 static void
7193delete_def_function_contents(dfunc_T *dfunc)
7194{
7195 int idx;
7196
7197 ga_clear(&dfunc->df_def_args_isn);
7198
7199 if (dfunc->df_instr != NULL)
7200 {
7201 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7202 delete_instr(dfunc->df_instr + idx);
7203 VIM_CLEAR(dfunc->df_instr);
7204 }
7205
7206 dfunc->df_deleted = TRUE;
7207}
7208
7209/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007210 * When a user function is deleted, clear the contents of any associated def
7211 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007212 */
7213 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007214clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007215{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007216 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007217 {
7218 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7219 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007220
Bram Moolenaar20431c92020-03-20 18:39:46 +01007221 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007222 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007223 }
7224}
7225
7226#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007227/*
7228 * Free all functions defined with ":def".
7229 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007230 void
7231free_def_functions(void)
7232{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007233 int idx;
7234
7235 for (idx = 0; idx < def_functions.ga_len; ++idx)
7236 {
7237 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7238
7239 delete_def_function_contents(dfunc);
7240 }
7241
7242 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007243}
7244#endif
7245
7246
7247#endif // FEAT_EVAL