blob: 5893e5ac0bb56fca66bb9016842e00394232baf9 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
111 int lv_from_outer; // when TRUE using ctx_outer scope
112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200129 int ctx_closure_count; // number of closures created in the
130 // function
131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144};
145
Bram Moolenaar20431c92020-03-20 18:39:46 +0100146static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100147
148/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200149 * Lookup variable "name" in the local scope and return it.
150 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200152 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153lookup_local(char_u *name, size_t len, cctx_T *cctx)
154{
155 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200156 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100158 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200159 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200160
161 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
163 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200164 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100165 if (STRNCMP(name, lvar->lv_name, len) == 0
166 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167 {
168 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200169 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200170 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200172
173 // Find local in outer function scope.
174 if (cctx->ctx_outer != NULL)
175 {
176 lvar = lookup_local(name, len, cctx->ctx_outer);
177 if (lvar != NULL)
178 {
179 // TODO: are there situations we should not mark the outer scope as
180 // used?
181 cctx->ctx_outer_used = TRUE;
182 lvar->lv_from_outer = TRUE;
183 return lvar;
184 }
185 }
186
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200187 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100188}
189
190/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200191 * Lookup an argument in the current function and an enclosing function.
192 * Returns the argument index in "idxp"
193 * Returns the argument type in "type"
194 * Sets "gen_load_outer" to TRUE if found in outer scope.
195 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196 */
197 static int
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200198lookup_arg(
199 char_u *name,
200 size_t len,
201 int *idxp,
202 type_T **type,
203 int *gen_load_outer,
204 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100205{
206 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200207 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100209 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200210 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
212 {
213 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
214
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200215 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
216 {
217 if (idxp != NULL)
218 {
219 // Arguments are located above the frame pointer. One further
220 // if there is a vararg argument
221 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
222 + STACK_FRAME_SIZE)
223 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
224
225 if (cctx->ctx_ufunc->uf_arg_types != NULL)
226 *type = cctx->ctx_ufunc->uf_arg_types[idx];
227 else
228 *type = &t_any;
229 }
230 return OK;
231 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100232 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100233
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200234 va_name = cctx->ctx_ufunc->uf_va_name;
235 if (va_name != NULL
236 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
237 {
238 if (idxp != NULL)
239 {
240 // varargs is always the last argument
241 *idxp = -STACK_FRAME_SIZE - 1;
242 *type = cctx->ctx_ufunc->uf_va_type;
243 }
244 return OK;
245 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200247 if (cctx->ctx_outer != NULL)
248 {
249 // Lookup the name for an argument of the outer function.
250 if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
251 == OK)
252 {
253 *gen_load_outer = TRUE;
254 return OK;
255 }
256 }
257
258 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100259}
260
261/*
Bram Moolenaar84367732020-08-23 15:21:55 +0200262 * Returnd TRUE if the script context is Vim9 script.
263 */
264 static int
265script_is_vim9()
266{
267 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
268}
269
270/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 * Lookup a variable in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200272 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
273 * without "s:".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274 * Returns OK or FAIL.
275 */
276 static int
Bram Moolenaar53900992020-08-22 19:02:02 +0200277lookup_script(char_u *name, size_t len, int vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278{
279 int cc;
280 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
281 dictitem_T *di;
282
Bram Moolenaar84367732020-08-23 15:21:55 +0200283 if (vim9script && !script_is_vim9())
Bram Moolenaar53900992020-08-22 19:02:02 +0200284 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100285 cc = name[len];
286 name[len] = NUL;
287 di = find_var_in_ht(ht, 0, name, TRUE);
288 name[len] = cc;
289 return di == NULL ? FAIL: OK;
290}
291
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100292/*
293 * Check if "p[len]" is already defined, either in script "import_sid" or in
294 * compilation context "cctx".
295 * Return FAIL and give an error if it defined.
296 */
297 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200298check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100299{
Bram Moolenaarad486a02020-08-01 23:22:18 +0200300 int c = p[len];
301
302 p[len] = NUL;
Bram Moolenaar53900992020-08-22 19:02:02 +0200303 if (lookup_script(p, len, FALSE) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100304 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200305 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaareef21022020-08-01 22:16:43 +0200306 || lookup_arg(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200307 || find_imported(p, len, cctx) != NULL
308 || find_func_even_dead(p, FALSE, cctx) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100309 {
Bram Moolenaarad486a02020-08-01 23:22:18 +0200310 p[len] = c;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200311 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100312 return FAIL;
313 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200314 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100315 return OK;
316}
317
Bram Moolenaar65b95452020-07-19 14:03:09 +0200318
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100319/////////////////////////////////////////////////////////////////////
320// Following generate_ functions expect the caller to call ga_grow().
321
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200322#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
323#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100324
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100325/*
326 * Generate an instruction without arguments.
327 * Returns a pointer to the new instruction, NULL if failed.
328 */
329 static isn_T *
330generate_instr(cctx_T *cctx, isntype_T isn_type)
331{
332 garray_T *instr = &cctx->ctx_instr;
333 isn_T *isn;
334
Bram Moolenaar080457c2020-03-03 21:53:32 +0100335 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100336 if (ga_grow(instr, 1) == FAIL)
337 return NULL;
338 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
339 isn->isn_type = isn_type;
340 isn->isn_lnum = cctx->ctx_lnum + 1;
341 ++instr->ga_len;
342
343 return isn;
344}
345
346/*
347 * Generate an instruction without arguments.
348 * "drop" will be removed from the stack.
349 * Returns a pointer to the new instruction, NULL if failed.
350 */
351 static isn_T *
352generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
353{
354 garray_T *stack = &cctx->ctx_type_stack;
355
Bram Moolenaar080457c2020-03-03 21:53:32 +0100356 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100357 stack->ga_len -= drop;
358 return generate_instr(cctx, isn_type);
359}
360
361/*
362 * Generate instruction "isn_type" and put "type" on the type stack.
363 */
364 static isn_T *
365generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
366{
367 isn_T *isn;
368 garray_T *stack = &cctx->ctx_type_stack;
369
370 if ((isn = generate_instr(cctx, isn_type)) == NULL)
371 return NULL;
372
373 if (ga_grow(stack, 1) == FAIL)
374 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200375 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100376 ++stack->ga_len;
377
378 return isn;
379}
380
381/*
382 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200383 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384 */
385 static int
386may_generate_2STRING(int offset, cctx_T *cctx)
387{
388 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200389 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100390 garray_T *stack = &cctx->ctx_type_stack;
391 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
392
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200393 switch ((*type)->tt_type)
394 {
395 // nothing to be done
396 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100397
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200398 // conversion possible
399 case VAR_SPECIAL:
400 case VAR_BOOL:
401 case VAR_NUMBER:
402 case VAR_FLOAT:
403 break;
404
405 // conversion possible (with runtime check)
406 case VAR_ANY:
407 case VAR_UNKNOWN:
408 isntype = ISN_2STRING_ANY;
409 break;
410
411 // conversion not possible
412 case VAR_VOID:
413 case VAR_BLOB:
414 case VAR_FUNC:
415 case VAR_PARTIAL:
416 case VAR_LIST:
417 case VAR_DICT:
418 case VAR_JOB:
419 case VAR_CHANNEL:
420 to_string_error((*type)->tt_type);
421 return FAIL;
422 }
423
424 *type = &t_string;
425 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100426 return FAIL;
427 isn->isn_arg.number = offset;
428
429 return OK;
430}
431
432 static int
433check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
434{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200435 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100436 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200437 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100438 {
439 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200440 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100441 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200442 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100443 return FAIL;
444 }
445 return OK;
446}
447
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200448 static int
449generate_add_instr(
450 cctx_T *cctx,
451 vartype_T vartype,
452 type_T *type1,
453 type_T *type2)
454{
455 isn_T *isn = generate_instr_drop(cctx,
456 vartype == VAR_NUMBER ? ISN_OPNR
457 : vartype == VAR_LIST ? ISN_ADDLIST
458 : vartype == VAR_BLOB ? ISN_ADDBLOB
459#ifdef FEAT_FLOAT
460 : vartype == VAR_FLOAT ? ISN_OPFLOAT
461#endif
462 : ISN_OPANY, 1);
463
464 if (vartype != VAR_LIST && vartype != VAR_BLOB
465 && type1->tt_type != VAR_ANY
466 && type2->tt_type != VAR_ANY
467 && check_number_or_float(
468 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
469 return FAIL;
470
471 if (isn != NULL)
472 isn->isn_arg.op.op_type = EXPR_ADD;
473 return isn == NULL ? FAIL : OK;
474}
475
476/*
477 * Get the type to use for an instruction for an operation on "type1" and
478 * "type2". If they are matching use a type-specific instruction. Otherwise
479 * fall back to runtime type checking.
480 */
481 static vartype_T
482operator_type(type_T *type1, type_T *type2)
483{
484 if (type1->tt_type == type2->tt_type
485 && (type1->tt_type == VAR_NUMBER
486 || type1->tt_type == VAR_LIST
487#ifdef FEAT_FLOAT
488 || type1->tt_type == VAR_FLOAT
489#endif
490 || type1->tt_type == VAR_BLOB))
491 return type1->tt_type;
492 return VAR_ANY;
493}
494
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100495/*
496 * Generate an instruction with two arguments. The instruction depends on the
497 * type of the arguments.
498 */
499 static int
500generate_two_op(cctx_T *cctx, char_u *op)
501{
502 garray_T *stack = &cctx->ctx_type_stack;
503 type_T *type1;
504 type_T *type2;
505 vartype_T vartype;
506 isn_T *isn;
507
Bram Moolenaar080457c2020-03-03 21:53:32 +0100508 RETURN_OK_IF_SKIP(cctx);
509
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200510 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100511 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
512 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200513 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100514
515 switch (*op)
516 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200517 case '+':
518 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100519 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100520 break;
521
522 case '-':
523 case '*':
524 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
525 op) == FAIL)
526 return FAIL;
527 if (vartype == VAR_NUMBER)
528 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
529#ifdef FEAT_FLOAT
530 else if (vartype == VAR_FLOAT)
531 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
532#endif
533 else
534 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
535 if (isn != NULL)
536 isn->isn_arg.op.op_type = *op == '*'
537 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
538 break;
539
Bram Moolenaar4c683752020-04-05 21:38:23 +0200540 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100541 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200542 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100543 && type2->tt_type != VAR_NUMBER))
544 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200545 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100546 return FAIL;
547 }
548 isn = generate_instr_drop(cctx,
549 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
550 if (isn != NULL)
551 isn->isn_arg.op.op_type = EXPR_REM;
552 break;
553 }
554
555 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200556 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100557 {
558 type_T *type = &t_any;
559
560#ifdef FEAT_FLOAT
561 // float+number and number+float results in float
562 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
563 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
564 type = &t_float;
565#endif
566 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
567 }
568
569 return OK;
570}
571
572/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200573 * Get the instruction to use for comparing "type1" with "type2"
574 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200576 static isntype_T
577get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100578{
579 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580
Bram Moolenaar4c683752020-04-05 21:38:23 +0200581 if (type1 == VAR_UNKNOWN)
582 type1 = VAR_ANY;
583 if (type2 == VAR_UNKNOWN)
584 type2 = VAR_ANY;
585
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586 if (type1 == type2)
587 {
588 switch (type1)
589 {
590 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
591 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
592 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
593 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
594 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
595 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
596 case VAR_LIST: isntype = ISN_COMPARELIST; break;
597 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
598 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100599 default: isntype = ISN_COMPAREANY; break;
600 }
601 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200602 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100603 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
604 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
605 isntype = ISN_COMPAREANY;
606
607 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
608 && (isntype == ISN_COMPAREBOOL
609 || isntype == ISN_COMPARESPECIAL
610 || isntype == ISN_COMPARENR
611 || isntype == ISN_COMPAREFLOAT))
612 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200613 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200615 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100616 }
617 if (isntype == ISN_DROP
618 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
619 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
620 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
621 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
622 && exptype != EXPR_IS && exptype != EXPR_ISNOT
623 && (type1 == VAR_BLOB || type2 == VAR_BLOB
624 || type1 == VAR_LIST || type2 == VAR_LIST))))
625 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200626 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100627 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200628 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100629 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200630 return isntype;
631}
632
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200633 int
634check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
635{
636 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
637 return FAIL;
638 return OK;
639}
640
Bram Moolenaara5565e42020-05-09 15:44:01 +0200641/*
642 * Generate an ISN_COMPARE* instruction with a boolean result.
643 */
644 static int
645generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
646{
647 isntype_T isntype;
648 isn_T *isn;
649 garray_T *stack = &cctx->ctx_type_stack;
650 vartype_T type1;
651 vartype_T type2;
652
653 RETURN_OK_IF_SKIP(cctx);
654
655 // Get the known type of the two items on the stack. If they are matching
656 // use a type-specific instruction. Otherwise fall back to runtime type
657 // checking.
658 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
659 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
660 isntype = get_compare_isn(exptype, type1, type2);
661 if (isntype == ISN_DROP)
662 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100663
664 if ((isn = generate_instr(cctx, isntype)) == NULL)
665 return FAIL;
666 isn->isn_arg.op.op_type = exptype;
667 isn->isn_arg.op.op_ic = ic;
668
669 // takes two arguments, puts one bool back
670 if (stack->ga_len >= 2)
671 {
672 --stack->ga_len;
673 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
674 }
675
676 return OK;
677}
678
679/*
680 * Generate an ISN_2BOOL instruction.
681 */
682 static int
683generate_2BOOL(cctx_T *cctx, int invert)
684{
685 isn_T *isn;
686 garray_T *stack = &cctx->ctx_type_stack;
687
Bram Moolenaar080457c2020-03-03 21:53:32 +0100688 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100689 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
690 return FAIL;
691 isn->isn_arg.number = invert;
692
693 // type becomes bool
694 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
695
696 return OK;
697}
698
699 static int
700generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
701{
702 isn_T *isn;
703 garray_T *stack = &cctx->ctx_type_stack;
704
Bram Moolenaar080457c2020-03-03 21:53:32 +0100705 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
707 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200708 // TODO: whole type, e.g. for a function also arg and return types
709 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100710 isn->isn_arg.type.ct_off = offset;
711
712 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200713 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100714
715 return OK;
716}
717
718/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200719 * Check that
720 * - "actual" is "expected" type or
721 * - "actual" is a type that can be "expected" type: add a runtime check; or
722 * - return FAIL.
723 */
724 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200725need_type(
726 type_T *actual,
727 type_T *expected,
728 int offset,
729 cctx_T *cctx,
730 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200731{
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200732 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200733 return OK;
734 if (actual->tt_type != VAR_ANY
735 && actual->tt_type != VAR_UNKNOWN
736 && !(actual->tt_type == VAR_FUNC
737 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
738 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200739 if (!silent)
740 type_mismatch(expected, actual);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200741 return FAIL;
742 }
743 generate_TYPECHECK(cctx, expected, offset);
744 return OK;
745}
746
747/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100748 * Generate an ISN_PUSHNR instruction.
749 */
750 static int
751generate_PUSHNR(cctx_T *cctx, varnumber_T number)
752{
753 isn_T *isn;
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.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001150 if (count == 0)
1151 member = &t_void;
1152 else
1153 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001154 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1155 cctx->ctx_type_list);
1156 type = get_list_type(member, cctx->ctx_type_list);
1157
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001158 // drop the value types
1159 stack->ga_len -= count;
1160
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001161 // add the list type to the type stack
1162 if (ga_grow(stack, 1) == FAIL)
1163 return FAIL;
1164 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1165 ++stack->ga_len;
1166
1167 return OK;
1168}
1169
1170/*
1171 * Generate an ISN_NEWDICT instruction.
1172 */
1173 static int
1174generate_NEWDICT(cctx_T *cctx, int count)
1175{
1176 isn_T *isn;
1177 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001178 type_T *type;
1179 type_T *member;
1180
Bram Moolenaar080457c2020-03-03 21:53:32 +01001181 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001182 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1183 return FAIL;
1184 isn->isn_arg.number = count;
1185
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001186 if (count == 0)
1187 member = &t_void;
1188 else
1189 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001190 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1191 cctx->ctx_type_list);
1192 type = get_dict_type(member, cctx->ctx_type_list);
1193
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194 // drop the key and value types
1195 stack->ga_len -= 2 * count;
1196
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001197 // add the dict type to the type stack
1198 if (ga_grow(stack, 1) == FAIL)
1199 return FAIL;
1200 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1201 ++stack->ga_len;
1202
1203 return OK;
1204}
1205
1206/*
1207 * Generate an ISN_FUNCREF instruction.
1208 */
1209 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001210generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001211{
1212 isn_T *isn;
1213 garray_T *stack = &cctx->ctx_type_stack;
1214
Bram Moolenaar080457c2020-03-03 21:53:32 +01001215 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001216 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1217 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001218 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001219 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220
1221 if (ga_grow(stack, 1) == FAIL)
1222 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001223 ((type_T **)stack->ga_data)[stack->ga_len] =
1224 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001225 ++stack->ga_len;
1226
1227 return OK;
1228}
1229
1230/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001231 * Generate an ISN_NEWFUNC instruction.
1232 */
1233 static int
1234generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1235{
1236 isn_T *isn;
1237 char_u *name;
1238
1239 RETURN_OK_IF_SKIP(cctx);
1240 name = vim_strsave(lambda_name);
1241 if (name == NULL)
1242 return FAIL;
1243 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1244 return FAIL;
1245 isn->isn_arg.newfunc.nf_lambda = name;
1246 isn->isn_arg.newfunc.nf_global = func_name;
1247
1248 return OK;
1249}
1250
1251/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001252 * Generate an ISN_JUMP instruction.
1253 */
1254 static int
1255generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1256{
1257 isn_T *isn;
1258 garray_T *stack = &cctx->ctx_type_stack;
1259
Bram Moolenaar080457c2020-03-03 21:53:32 +01001260 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1262 return FAIL;
1263 isn->isn_arg.jump.jump_when = when;
1264 isn->isn_arg.jump.jump_where = where;
1265
1266 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1267 --stack->ga_len;
1268
1269 return OK;
1270}
1271
1272 static int
1273generate_FOR(cctx_T *cctx, int loop_idx)
1274{
1275 isn_T *isn;
1276 garray_T *stack = &cctx->ctx_type_stack;
1277
Bram Moolenaar080457c2020-03-03 21:53:32 +01001278 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001279 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1280 return FAIL;
1281 isn->isn_arg.forloop.for_idx = loop_idx;
1282
1283 if (ga_grow(stack, 1) == FAIL)
1284 return FAIL;
1285 // type doesn't matter, will be stored next
1286 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1287 ++stack->ga_len;
1288
1289 return OK;
1290}
1291
1292/*
1293 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001294 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001295 * Return FAIL if the number of arguments is wrong.
1296 */
1297 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001298generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001299{
1300 isn_T *isn;
1301 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001302 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001303 type_T *argtypes[MAX_FUNC_ARGS];
1304 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305
Bram Moolenaar080457c2020-03-03 21:53:32 +01001306 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001307 argoff = check_internal_func(func_idx, argcount);
1308 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309 return FAIL;
1310
Bram Moolenaar389df252020-07-09 21:20:47 +02001311 if (method_call && argoff > 1)
1312 {
1313 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1314 return FAIL;
1315 isn->isn_arg.shuffle.shfl_item = argcount;
1316 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1317 }
1318
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001319 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1320 return FAIL;
1321 isn->isn_arg.bfunc.cbf_idx = func_idx;
1322 isn->isn_arg.bfunc.cbf_argcount = argcount;
1323
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001324 for (i = 0; i < argcount; ++i)
1325 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1326
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327 stack->ga_len -= argcount; // drop the arguments
1328 if (ga_grow(stack, 1) == FAIL)
1329 return FAIL;
1330 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001331 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001332 ++stack->ga_len; // add return value
1333
1334 return OK;
1335}
1336
1337/*
1338 * Generate an ISN_DCALL or ISN_UCALL instruction.
1339 * Return FAIL if the number of arguments is wrong.
1340 */
1341 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001342generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001343{
1344 isn_T *isn;
1345 garray_T *stack = &cctx->ctx_type_stack;
1346 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001347 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001348
Bram Moolenaar080457c2020-03-03 21:53:32 +01001349 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001350 if (argcount > regular_args && !has_varargs(ufunc))
1351 {
1352 semsg(_(e_toomanyarg), ufunc->uf_name);
1353 return FAIL;
1354 }
1355 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1356 {
1357 semsg(_(e_toofewarg), ufunc->uf_name);
1358 return FAIL;
1359 }
1360
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001361 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001362 {
1363 int i;
1364
1365 for (i = 0; i < argcount; ++i)
1366 {
1367 type_T *expected;
1368 type_T *actual;
1369
1370 if (i < regular_args)
1371 {
1372 if (ufunc->uf_arg_types == NULL)
1373 continue;
1374 expected = ufunc->uf_arg_types[i];
1375 }
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001376 else if (ufunc->uf_va_type == NULL)
1377 // possibly a lambda
1378 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001379 else
1380 expected = ufunc->uf_va_type->tt_member;
1381 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001382 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001383 {
1384 arg_type_mismatch(expected, actual, i + 1);
1385 return FAIL;
1386 }
1387 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001388 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001389 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1390 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001391 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001392 }
1393
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001394 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001395 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001396 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001398 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399 {
1400 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1401 isn->isn_arg.dfunc.cdf_argcount = argcount;
1402 }
1403 else
1404 {
1405 // A user function may be deleted and redefined later, can't use the
1406 // ufunc pointer, need to look it up again at runtime.
1407 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1408 isn->isn_arg.ufunc.cuf_argcount = argcount;
1409 }
1410
1411 stack->ga_len -= argcount; // drop the arguments
1412 if (ga_grow(stack, 1) == FAIL)
1413 return FAIL;
1414 // add return value
1415 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1416 ++stack->ga_len;
1417
1418 return OK;
1419}
1420
1421/*
1422 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1423 */
1424 static int
1425generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1426{
1427 isn_T *isn;
1428 garray_T *stack = &cctx->ctx_type_stack;
1429
Bram Moolenaar080457c2020-03-03 21:53:32 +01001430 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001431 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1432 return FAIL;
1433 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1434 isn->isn_arg.ufunc.cuf_argcount = argcount;
1435
1436 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001437 if (ga_grow(stack, 1) == FAIL)
1438 return FAIL;
1439 // add return value
1440 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1441 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442
1443 return OK;
1444}
1445
1446/*
1447 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001448 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001449 */
1450 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001451generate_PCALL(
1452 cctx_T *cctx,
1453 int argcount,
1454 char_u *name,
1455 type_T *type,
1456 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457{
1458 isn_T *isn;
1459 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001460 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461
Bram Moolenaar080457c2020-03-03 21:53:32 +01001462 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001463
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001464 if (type->tt_type == VAR_ANY)
1465 ret_type = &t_any;
1466 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001467 {
1468 if (type->tt_argcount != -1)
1469 {
1470 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1471
1472 if (argcount < type->tt_min_argcount - varargs)
1473 {
1474 semsg(_(e_toofewarg), "[reference]");
1475 return FAIL;
1476 }
1477 if (!varargs && argcount > type->tt_argcount)
1478 {
1479 semsg(_(e_toomanyarg), "[reference]");
1480 return FAIL;
1481 }
1482 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001483 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001484 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001485 else
1486 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001487 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001488 return FAIL;
1489 }
1490
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1492 return FAIL;
1493 isn->isn_arg.pfunc.cpf_top = at_top;
1494 isn->isn_arg.pfunc.cpf_argcount = argcount;
1495
1496 stack->ga_len -= argcount; // drop the arguments
1497
1498 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001499 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001500
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001501 // If partial is above the arguments it must be cleared and replaced with
1502 // the return value.
1503 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1504 return FAIL;
1505
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001506 return OK;
1507}
1508
1509/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001510 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001511 */
1512 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001513generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001514{
1515 isn_T *isn;
1516 garray_T *stack = &cctx->ctx_type_stack;
1517 type_T *type;
1518
Bram Moolenaar080457c2020-03-03 21:53:32 +01001519 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001520 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001521 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001522 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001523
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001524 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001525 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001526 if (type->tt_type != VAR_DICT && type != &t_any)
1527 {
1528 emsg(_(e_dictreq));
1529 return FAIL;
1530 }
1531 // change dict type to dict member type
1532 if (type->tt_type == VAR_DICT)
1533 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001534
1535 return OK;
1536}
1537
1538/*
1539 * Generate an ISN_ECHO instruction.
1540 */
1541 static int
1542generate_ECHO(cctx_T *cctx, int with_white, int count)
1543{
1544 isn_T *isn;
1545
Bram Moolenaar080457c2020-03-03 21:53:32 +01001546 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001547 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1548 return FAIL;
1549 isn->isn_arg.echo.echo_with_white = with_white;
1550 isn->isn_arg.echo.echo_count = count;
1551
1552 return OK;
1553}
1554
Bram Moolenaarad39c092020-02-26 18:23:43 +01001555/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001556 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001557 */
1558 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001559generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001560{
1561 isn_T *isn;
1562
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001563 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001564 return FAIL;
1565 isn->isn_arg.number = count;
1566
1567 return OK;
1568}
1569
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001570 static int
1571generate_EXEC(cctx_T *cctx, char_u *line)
1572{
1573 isn_T *isn;
1574
Bram Moolenaar080457c2020-03-03 21:53:32 +01001575 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001576 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1577 return FAIL;
1578 isn->isn_arg.string = vim_strsave(line);
1579 return OK;
1580}
1581
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001582 static int
1583generate_EXECCONCAT(cctx_T *cctx, int count)
1584{
1585 isn_T *isn;
1586
1587 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1588 return FAIL;
1589 isn->isn_arg.number = count;
1590 return OK;
1591}
1592
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001593/*
1594 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001595 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001597 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001598reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1599{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 lvar_T *lvar;
1601
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001602 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001604 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001605 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606 }
1607
1608 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001609 return NULL;
1610 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001612 // Every local variable uses the next entry on the stack. We could re-use
1613 // the last ones when leaving a scope, but then variables used in a closure
1614 // might get overwritten. To keep things simple do not re-use stack
1615 // entries. This is less efficient, but memory is cheap these days.
1616 lvar->lv_idx = cctx->ctx_locals_count++;
1617
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001618 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001619 lvar->lv_const = isConst;
1620 lvar->lv_type = type;
1621
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001622 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001623}
1624
1625/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001626 * Remove local variables above "new_top".
1627 */
1628 static void
1629unwind_locals(cctx_T *cctx, int new_top)
1630{
1631 if (cctx->ctx_locals.ga_len > new_top)
1632 {
1633 int idx;
1634 lvar_T *lvar;
1635
1636 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1637 {
1638 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1639 vim_free(lvar->lv_name);
1640 }
1641 }
1642 cctx->ctx_locals.ga_len = new_top;
1643}
1644
1645/*
1646 * Free all local variables.
1647 */
1648 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001649free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001650{
1651 unwind_locals(cctx, 0);
1652 ga_clear(&cctx->ctx_locals);
1653}
1654
1655/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001656 * Find "name" in script-local items of script "sid".
1657 * Returns the index in "sn_var_vals" if found.
1658 * If found but not in "sn_var_vals" returns -1.
1659 * If not found returns -2.
1660 */
1661 int
1662get_script_item_idx(int sid, char_u *name, int check_writable)
1663{
1664 hashtab_T *ht;
1665 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001666 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001667 int idx;
1668
1669 // First look the name up in the hashtable.
Bram Moolenaare3d46852020-08-29 13:39:17 +02001670 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671 return -1;
1672 ht = &SCRIPT_VARS(sid);
1673 di = find_var_in_ht(ht, 0, name, TRUE);
1674 if (di == NULL)
1675 return -2;
1676
1677 // Now find the svar_T index in sn_var_vals.
1678 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1679 {
1680 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1681
1682 if (sv->sv_tv == &di->di_tv)
1683 {
1684 if (check_writable && sv->sv_const)
1685 semsg(_(e_readonlyvar), name);
1686 return idx;
1687 }
1688 }
1689 return -1;
1690}
1691
1692/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001693 * Find "name" in imported items of the current script or in "cctx" if not
1694 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695 */
1696 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001697find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001698{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699 int idx;
1700
Bram Moolenaare3d46852020-08-29 13:39:17 +02001701 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001702 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001703 if (cctx != NULL)
1704 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1705 {
1706 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1707 + idx;
1708
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001709 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1710 : STRLEN(import->imp_name) == len
1711 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001712 return import;
1713 }
1714
Bram Moolenaarefa94442020-08-08 22:16:00 +02001715 return find_imported_in_script(name, len, current_sctx.sc_sid);
1716}
1717
1718 imported_T *
1719find_imported_in_script(char_u *name, size_t len, int sid)
1720{
Bram Moolenaare3d46852020-08-29 13:39:17 +02001721 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001722 int idx;
1723
Bram Moolenaare3d46852020-08-29 13:39:17 +02001724 if (!SCRIPT_ID_VALID(sid))
1725 return NULL;
1726 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1728 {
1729 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1730
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001731 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1732 : STRLEN(import->imp_name) == len
1733 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 return import;
1735 }
1736 return NULL;
1737}
1738
1739/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001740 * Free all imported variables.
1741 */
1742 static void
1743free_imported(cctx_T *cctx)
1744{
1745 int idx;
1746
1747 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1748 {
1749 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1750
1751 vim_free(import->imp_name);
1752 }
1753 ga_clear(&cctx->ctx_imports);
1754}
1755
1756/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02001757 * Return TRUE if "p" points at a "#" but not at "#{".
1758 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001759 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001760vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001761{
1762 return p[0] == '#' && p[1] != '{';
1763}
1764
1765/*
1766 * Return a pointer to the next line that isn't empty or only contains a
1767 * comment. Skips over white space.
1768 * Returns NULL if there is none.
1769 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001770 char_u *
1771peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001772{
1773 int lnum = cctx->ctx_lnum;
1774
1775 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
1776 {
1777 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001778 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001779
Bram Moolenaarba60cc42020-08-12 19:15:33 +02001780 // ignore NULLs inserted for continuation lines
1781 if (line != NULL)
1782 {
1783 p = skipwhite(line);
1784 if (*p != NUL && !vim9_comment_start(p))
1785 return p;
1786 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02001787 }
1788 return NULL;
1789}
1790
1791/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001792 * Called when checking for a following operator at "arg". When the rest of
1793 * the line is empty or only a comment, peek the next line. If there is a next
1794 * line return a pointer to it and set "nextp".
1795 * Otherwise skip over white space.
1796 */
1797 static char_u *
1798may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
1799{
1800 char_u *p = skipwhite(arg);
1801
1802 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001803 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001804 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001805 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001806 if (*nextp != NULL)
1807 return *nextp;
1808 }
1809 return p;
1810}
1811
1812/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02001813 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02001814 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02001815 * Returns NULL when at the end.
1816 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001817 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02001818next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02001819{
Bram Moolenaar7a092242020-04-16 22:10:49 +02001820 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001821
1822 do
1823 {
1824 ++cctx->ctx_lnum;
1825 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02001826 {
1827 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001828 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02001829 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02001830 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02001831 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001832 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001833 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02001834 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02001835 return line;
1836}
1837
1838/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001839 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02001840 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001841 * Return FAIL if beyond the last line, "*arg" is unmodified then.
1842 */
1843 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02001844may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001845{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001846 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001847 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02001848 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001849
1850 if (next == NULL)
1851 return FAIL;
1852 *arg = skipwhite(next);
1853 }
1854 return OK;
1855}
1856
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001857/*
1858 * Idem, and give an error when failed.
1859 */
1860 static int
1861may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
1862{
1863 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1864 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001865 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001866 return FAIL;
1867 }
1868 return OK;
1869}
1870
1871
Bram Moolenaara5565e42020-05-09 15:44:01 +02001872// Structure passed between the compile_expr* functions to keep track of
1873// constants that have been parsed but for which no code was produced yet. If
1874// possible expressions on these constants are applied at compile time. If
1875// that is not possible, the code to push the constants needs to be generated
1876// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02001877// Using 50 should be more than enough of 5 levels of ().
1878#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02001879typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02001880 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02001881 int pp_used; // active entries in pp_tv[]
1882} ppconst_T;
1883
Bram Moolenaar1c747212020-05-09 18:28:34 +02001884static int compile_expr0(char_u **arg, cctx_T *cctx);
1885static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1886
Bram Moolenaara5565e42020-05-09 15:44:01 +02001887/*
1888 * Generate a PUSH instruction for "tv".
1889 * "tv" will be consumed or cleared.
1890 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
1891 */
1892 static int
1893generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
1894{
1895 if (tv != NULL)
1896 {
1897 switch (tv->v_type)
1898 {
1899 case VAR_UNKNOWN:
1900 break;
1901 case VAR_BOOL:
1902 generate_PUSHBOOL(cctx, tv->vval.v_number);
1903 break;
1904 case VAR_SPECIAL:
1905 generate_PUSHSPEC(cctx, tv->vval.v_number);
1906 break;
1907 case VAR_NUMBER:
1908 generate_PUSHNR(cctx, tv->vval.v_number);
1909 break;
1910#ifdef FEAT_FLOAT
1911 case VAR_FLOAT:
1912 generate_PUSHF(cctx, tv->vval.v_float);
1913 break;
1914#endif
1915 case VAR_BLOB:
1916 generate_PUSHBLOB(cctx, tv->vval.v_blob);
1917 tv->vval.v_blob = NULL;
1918 break;
1919 case VAR_STRING:
1920 generate_PUSHS(cctx, tv->vval.v_string);
1921 tv->vval.v_string = NULL;
1922 break;
1923 default:
1924 iemsg("constant type not supported");
1925 clear_tv(tv);
1926 return FAIL;
1927 }
1928 tv->v_type = VAR_UNKNOWN;
1929 }
1930 return OK;
1931}
1932
1933/*
1934 * Generate code for any ppconst entries.
1935 */
1936 static int
1937generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
1938{
1939 int i;
1940 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001941 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001942
Bram Moolenaar9b68c822020-06-18 19:31:08 +02001943 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001944 for (i = 0; i < ppconst->pp_used; ++i)
1945 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
1946 ret = FAIL;
1947 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001948 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001949 return ret;
1950}
1951
1952/*
1953 * Clear ppconst constants. Used when failing.
1954 */
1955 static void
1956clear_ppconst(ppconst_T *ppconst)
1957{
1958 int i;
1959
1960 for (i = 0; i < ppconst->pp_used; ++i)
1961 clear_tv(&ppconst->pp_tv[i]);
1962 ppconst->pp_used = 0;
1963}
1964
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001965/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001966 * Generate an instruction to load script-local variable "name", without the
1967 * leading "s:".
1968 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001969 */
1970 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001971compile_load_scriptvar(
1972 cctx_T *cctx,
1973 char_u *name, // variable NUL terminated
1974 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001975 char_u **end, // end of variable
1976 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977{
Bram Moolenaare3d46852020-08-29 13:39:17 +02001978 scriptitem_T *si;
1979 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001980 imported_T *import;
1981
Bram Moolenaare3d46852020-08-29 13:39:17 +02001982 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
1983 return FAIL;
1984 si = SCRIPT_ITEM(current_sctx.sc_sid);
1985 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001986 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001988 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001989 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
1990 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001991 }
1992 if (idx >= 0)
1993 {
1994 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1995
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001996 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 current_sctx.sc_sid, idx, sv->sv_type);
1998 return OK;
1999 }
2000
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002001 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002002 if (import != NULL)
2003 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002004 if (import->imp_all)
2005 {
2006 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002007 char_u *exp_name;
2008 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002009 ufunc_T *ufunc;
2010 type_T *type;
2011
2012 // Used "import * as Name", need to lookup the member.
2013 if (*p != '.')
2014 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002015 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002016 return FAIL;
2017 }
2018 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002019 if (VIM_ISWHITE(*p))
2020 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002021 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002022 return FAIL;
2023 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002024
Bram Moolenaar1c991142020-07-04 13:15:31 +02002025 // isolate one name
2026 exp_name = p;
2027 while (eval_isnamec(*p))
2028 ++p;
2029 cc = *p;
2030 *p = NUL;
2031
2032 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2033 *p = cc;
2034 p = skipwhite(p);
2035
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002036 // TODO: what if it is a function?
2037 if (idx < 0)
2038 return FAIL;
2039 *end = p;
2040
2041 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2042 import->imp_sid,
2043 idx,
2044 type);
2045 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002046 else if (import->imp_funcname != NULL)
2047 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002048 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002049 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2050 import->imp_sid,
2051 import->imp_var_vals_idx,
2052 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002053 return OK;
2054 }
2055
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002056 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002057 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002058 return FAIL;
2059}
2060
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002061 static int
2062generate_funcref(cctx_T *cctx, char_u *name)
2063{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002064 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002065
2066 if (ufunc == NULL)
2067 return FAIL;
2068
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002069 // Need to compile any default values to get the argument types.
2070 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2071 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2072 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002073 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002074}
2075
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002076/*
2077 * Compile a variable name into a load instruction.
2078 * "end" points to just after the name.
2079 * When "error" is FALSE do not give an error when not found.
2080 */
2081 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002082compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002083{
2084 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002085 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002086 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002087 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002088 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089
2090 if (*(*arg + 1) == ':')
2091 {
2092 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002093 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002094 {
2095 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002096
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002097 switch (**arg)
2098 {
2099 case 'g': isn_type = ISN_LOADGDICT; break;
2100 case 'w': isn_type = ISN_LOADWDICT; break;
2101 case 't': isn_type = ISN_LOADTDICT; break;
2102 case 'b': isn_type = ISN_LOADBDICT; break;
2103 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002104 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002105 goto theend;
2106 }
2107 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2108 goto theend;
2109 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002110 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002111 else
2112 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002113 isntype_T isn_type = ISN_DROP;
2114
2115 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2116 if (name == NULL)
2117 return FAIL;
2118
2119 switch (**arg)
2120 {
2121 case 'v': res = generate_LOADV(cctx, name, error);
2122 break;
2123 case 's': res = compile_load_scriptvar(cctx, name,
2124 NULL, NULL, error);
2125 break;
2126 case 'g': isn_type = ISN_LOADG; break;
2127 case 'w': isn_type = ISN_LOADW; break;
2128 case 't': isn_type = ISN_LOADT; break;
2129 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002130 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002131 goto theend;
2132 }
2133 if (isn_type != ISN_DROP)
2134 {
2135 // Global, Buffer-local, Window-local and Tabpage-local
2136 // variables can be defined later, thus we don't check if it
2137 // exists, give error at runtime.
2138 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2139 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 }
2141 }
2142 else
2143 {
2144 size_t len = end - *arg;
2145 int idx;
2146 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002147 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002148
2149 name = vim_strnsave(*arg, end - *arg);
2150 if (name == NULL)
2151 return FAIL;
2152
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002153 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002154 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002155 if (!gen_load_outer)
2156 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002157 }
2158 else
2159 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002160 lvar_T *lvar = lookup_local(*arg, len, cctx);
2161
2162 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002163 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002164 type = lvar->lv_type;
2165 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002166 if (lvar->lv_from_outer)
2167 gen_load_outer = TRUE;
2168 else
2169 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002170 }
2171 else
2172 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002173 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002174 // already exists in a Vim9 script or when it's imported.
2175 if (lookup_script(*arg, len, TRUE) == OK
2176 || find_imported(name, 0, cctx) != NULL)
2177 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002178
Bram Moolenaara5565e42020-05-09 15:44:01 +02002179 // When the name starts with an uppercase letter or "x:" it
2180 // can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002181 // TODO: this is just guessing
Bram Moolenaara5565e42020-05-09 15:44:01 +02002182 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2183 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002184 }
2185 }
2186 if (gen_load)
2187 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002188 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002189 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002190 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002191 cctx->ctx_outer_used = TRUE;
2192 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002193 }
2194
2195 *arg = end;
2196
2197theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002198 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002199 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002200 vim_free(name);
2201 return res;
2202}
2203
2204/*
2205 * Compile the argument expressions.
2206 * "arg" points to just after the "(" and is advanced to after the ")"
2207 */
2208 static int
2209compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2210{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002211 char_u *p = *arg;
2212 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002213
Bram Moolenaare6085c52020-04-12 20:19:16 +02002214 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002215 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002216 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2217 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002218 if (*p == ')')
2219 {
2220 *arg = p + 1;
2221 return OK;
2222 }
2223
Bram Moolenaara5565e42020-05-09 15:44:01 +02002224 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002225 return FAIL;
2226 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002227
2228 if (*p != ',' && *skipwhite(p) == ',')
2229 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002230 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002231 p = skipwhite(p);
2232 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002233 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002234 {
2235 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002236 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002237 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002238 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002239 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002240 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002241 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002242failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002243 emsg(_(e_missing_close));
2244 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245}
2246
2247/*
2248 * Compile a function call: name(arg1, arg2)
2249 * "arg" points to "name", "arg + varlen" to the "(".
2250 * "argcount_init" is 1 for "value->method()"
2251 * Instructions:
2252 * EVAL arg1
2253 * EVAL arg2
2254 * BCALL / DCALL / UCALL
2255 */
2256 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002257compile_call(
2258 char_u **arg,
2259 size_t varlen,
2260 cctx_T *cctx,
2261 ppconst_T *ppconst,
2262 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002263{
2264 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002265 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002266 int argcount = argcount_init;
2267 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002268 char_u fname_buf[FLEN_FIXED + 1];
2269 char_u *tofree = NULL;
2270 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002272 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002273 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002274
Bram Moolenaara5565e42020-05-09 15:44:01 +02002275 // we can evaluate "has('name')" at compile time
2276 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2277 {
2278 char_u *s = skipwhite(*arg + varlen + 1);
2279 typval_T argvars[2];
2280
2281 argvars[0].v_type = VAR_UNKNOWN;
2282 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002283 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002284 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002285 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002286 s = skipwhite(s);
2287 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2288 {
2289 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2290
2291 *arg = s + 1;
2292 argvars[1].v_type = VAR_UNKNOWN;
2293 tv->v_type = VAR_NUMBER;
2294 tv->vval.v_number = 0;
2295 f_has(argvars, tv);
2296 clear_tv(&argvars[0]);
2297 ++ppconst->pp_used;
2298 return OK;
2299 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002300 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002301 }
2302
2303 if (generate_ppconst(cctx, ppconst) == FAIL)
2304 return FAIL;
2305
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002306 if (varlen >= sizeof(namebuf))
2307 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002308 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002309 return FAIL;
2310 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002311 vim_strncpy(namebuf, *arg, varlen);
2312 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313
2314 *arg = skipwhite(*arg + varlen + 1);
2315 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002316 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002317
Bram Moolenaara1773442020-08-12 15:21:22 +02002318 is_autoload = vim_strchr(name, '#') != NULL;
2319 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320 {
2321 int idx;
2322
2323 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002324 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002325 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002326 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002327 else
2328 semsg(_(e_unknownfunc), namebuf);
2329 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002330 }
2331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002332 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002333 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002334 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002335 {
2336 res = generate_CALL(cctx, ufunc, argcount);
2337 goto theend;
2338 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002339
2340 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002341 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002342 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002343 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002344 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaara26b9702020-04-18 19:53:28 +02002345 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002346 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002347 garray_T *stack = &cctx->ctx_type_stack;
2348 type_T *type;
2349
2350 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2351 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002352 goto theend;
2353 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002354
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002355 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002356 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002357 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002358 res = generate_UCALL(cctx, name, argcount);
2359 else
2360 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002361
2362theend:
2363 vim_free(tofree);
2364 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365}
2366
2367// like NAMESPACE_CHAR but with 'a' and 'l'.
2368#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2369
2370/*
2371 * Find the end of a variable or function name. Unlike find_name_end() this
2372 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002373 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002374 * Return a pointer to just after the name. Equal to "arg" if there is no
2375 * valid name.
2376 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002377 static char_u *
2378to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002379{
2380 char_u *p;
2381
2382 // Quick check for valid starting character.
2383 if (!eval_isnamec1(*arg))
2384 return arg;
2385
2386 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2387 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2388 // and can be used in slice "[n:]".
2389 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002390 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2392 break;
2393 return p;
2394}
2395
2396/*
2397 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002398 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002399 */
2400 char_u *
2401to_name_const_end(char_u *arg)
2402{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002403 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002404 typval_T rettv;
2405
2406 if (p == arg && *arg == '[')
2407 {
2408
2409 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002410 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002411 p = arg;
2412 }
2413 else if (p == arg && *arg == '#' && arg[1] == '{')
2414 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002415 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002417 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002418 p = arg;
2419 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002420
2421 return p;
2422}
2423
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002424/*
2425 * parse a list: [expr, expr]
2426 * "*arg" points to the '['.
2427 */
2428 static int
2429compile_list(char_u **arg, cctx_T *cctx)
2430{
2431 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002432 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002433 int count = 0;
2434
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002435 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002436 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002437 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002438 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002439 semsg(_(e_list_end), *arg);
2440 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002441 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002442 if (*p == ',')
2443 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002444 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002445 return FAIL;
2446 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002447 if (*p == ']')
2448 {
2449 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002450 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002451 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002452 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002453 break;
2454 ++count;
2455 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002456 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002457 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002458 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2459 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002460 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002461 return FAIL;
2462 }
2463 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002464 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002465 p = skipwhite(p);
2466 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002467 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002468
2469 generate_NEWLIST(cctx, count);
2470 return OK;
2471}
2472
2473/*
2474 * parse a lambda: {arg, arg -> expr}
2475 * "*arg" points to the '{'.
2476 */
2477 static int
2478compile_lambda(char_u **arg, cctx_T *cctx)
2479{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002480 typval_T rettv;
2481 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002482 evalarg_T evalarg;
2483
2484 CLEAR_FIELD(evalarg);
2485 evalarg.eval_flags = EVAL_EVALUATE;
2486 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487
2488 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002489 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002491
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002493 ++ufunc->uf_refcount;
2494 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002495 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496
2497 // The function will have one line: "return {expr}".
2498 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002499 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002500
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002501 clear_evalarg(&evalarg, NULL);
2502
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002503 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002504 {
2505 // The return type will now be known.
2506 set_function_type(ufunc);
2507
2508 return generate_FUNCREF(cctx, ufunc);
2509 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002510
2511 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512 return FAIL;
2513}
2514
2515/*
2516 * Compile a lamda call: expr->{lambda}(args)
2517 * "arg" points to the "{".
2518 */
2519 static int
2520compile_lambda_call(char_u **arg, cctx_T *cctx)
2521{
2522 ufunc_T *ufunc;
2523 typval_T rettv;
2524 int argcount = 1;
2525 int ret = FAIL;
2526
2527 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002528 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 return FAIL;
2530
2531 if (**arg != '(')
2532 {
2533 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002534 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002535 else
2536 semsg(_(e_missing_paren), "lambda");
2537 clear_tv(&rettv);
2538 return FAIL;
2539 }
2540
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002541 ufunc = rettv.vval.v_partial->pt_func;
2542 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002543 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002544 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002545
2546 // The function will have one line: "return {expr}".
2547 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002548 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549
2550 // compile the arguments
2551 *arg = skipwhite(*arg + 1);
2552 if (compile_arguments(arg, cctx, &argcount) == OK)
2553 // call the compiled function
2554 ret = generate_CALL(cctx, ufunc, argcount);
2555
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002556 if (ret == FAIL)
2557 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558 return ret;
2559}
2560
2561/*
2562 * parse a dict: {'key': val} or #{key: val}
2563 * "*arg" points to the '{'.
2564 */
2565 static int
2566compile_dict(char_u **arg, cctx_T *cctx, int literal)
2567{
2568 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002569 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002570 int count = 0;
2571 dict_T *d = dict_alloc();
2572 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002573 char_u *whitep = *arg;
2574 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575
2576 if (d == NULL)
2577 return FAIL;
2578 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002579 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002580 {
2581 char_u *key = NULL;
2582
Bram Moolenaar23c55272020-06-21 16:58:13 +02002583 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002584 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002585 *arg = NULL;
2586 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002587 }
2588
2589 if (**arg == '}')
2590 break;
2591
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002592 if (literal)
2593 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002594 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595
Bram Moolenaar2c330432020-04-13 14:41:35 +02002596 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002597 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002598 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 return FAIL;
2600 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002601 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 if (generate_PUSHS(cctx, key) == FAIL)
2603 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002604 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605 }
2606 else
2607 {
2608 isn_T *isn;
2609
Bram Moolenaara5565e42020-05-09 15:44:01 +02002610 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002612 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2613 if (isn->isn_type == ISN_PUSHS)
2614 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002615 else
2616 {
2617 type_T *keytype = ((type_T **)stack->ga_data)
2618 [stack->ga_len - 1];
2619 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2620 return FAIL;
2621 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002622 }
2623
2624 // Check for duplicate keys, if using string keys.
2625 if (key != NULL)
2626 {
2627 item = dict_find(d, key, -1);
2628 if (item != NULL)
2629 {
2630 semsg(_(e_duplicate_key), key);
2631 goto failret;
2632 }
2633 item = dictitem_alloc(key);
2634 if (item != NULL)
2635 {
2636 item->di_tv.v_type = VAR_UNKNOWN;
2637 item->di_tv.v_lock = 0;
2638 if (dict_add(d, item) == FAIL)
2639 dictitem_free(item);
2640 }
2641 }
2642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643 if (**arg != ':')
2644 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002645 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002646 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002647 else
2648 semsg(_(e_missing_dict_colon), *arg);
2649 return FAIL;
2650 }
2651 whitep = *arg + 1;
2652 if (!IS_WHITE_OR_NUL(*whitep))
2653 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002654 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655 return FAIL;
2656 }
2657
2658 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002659 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002660 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002661 *arg = NULL;
2662 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002663 }
2664
Bram Moolenaara5565e42020-05-09 15:44:01 +02002665 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666 return FAIL;
2667 ++count;
2668
Bram Moolenaar2c330432020-04-13 14:41:35 +02002669 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002670 *arg = skipwhite(*arg);
2671 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002672 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002673 *arg = NULL;
2674 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002675 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676 if (**arg == '}')
2677 break;
2678 if (**arg != ',')
2679 {
2680 semsg(_(e_missing_dict_comma), *arg);
2681 goto failret;
2682 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002683 if (IS_WHITE_OR_NUL(*whitep))
2684 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002685 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002686 return FAIL;
2687 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002688 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002689 *arg = skipwhite(*arg + 1);
2690 }
2691
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002692 *arg = *arg + 1;
2693
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002694 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002695 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002696 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002697 *arg += STRLEN(*arg);
2698
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699 dict_unref(d);
2700 return generate_NEWDICT(cctx, count);
2701
2702failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002703 if (*arg == NULL)
2704 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705 dict_unref(d);
2706 return FAIL;
2707}
2708
2709/*
2710 * Compile "&option".
2711 */
2712 static int
2713compile_get_option(char_u **arg, cctx_T *cctx)
2714{
2715 typval_T rettv;
2716 char_u *start = *arg;
2717 int ret;
2718
2719 // parse the option and get the current value to get the type.
2720 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002721 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002722 if (ret == OK)
2723 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002724 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002725 char_u *name = vim_strnsave(start, *arg - start);
2726 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2727
2728 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2729 vim_free(name);
2730 }
2731 clear_tv(&rettv);
2732
2733 return ret;
2734}
2735
2736/*
2737 * Compile "$VAR".
2738 */
2739 static int
2740compile_get_env(char_u **arg, cctx_T *cctx)
2741{
2742 char_u *start = *arg;
2743 int len;
2744 int ret;
2745 char_u *name;
2746
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002747 ++*arg;
2748 len = get_env_len(arg);
2749 if (len == 0)
2750 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002751 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752 return FAIL;
2753 }
2754
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002755 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756 name = vim_strnsave(start, len + 1);
2757 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2758 vim_free(name);
2759 return ret;
2760}
2761
2762/*
2763 * Compile "@r".
2764 */
2765 static int
2766compile_get_register(char_u **arg, cctx_T *cctx)
2767{
2768 int ret;
2769
2770 ++*arg;
2771 if (**arg == NUL)
2772 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002773 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 return FAIL;
2775 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002776 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777 {
2778 emsg_invreg(**arg);
2779 return FAIL;
2780 }
2781 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2782 ++*arg;
2783 return ret;
2784}
2785
2786/*
2787 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002788 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789 */
2790 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002791apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002792{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002793 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794
2795 // this works from end to start
2796 while (p > start)
2797 {
2798 --p;
2799 if (*p == '-' || *p == '+')
2800 {
2801 // only '-' has an effect, for '+' we only check the type
2802#ifdef FEAT_FLOAT
2803 if (rettv->v_type == VAR_FLOAT)
2804 {
2805 if (*p == '-')
2806 rettv->vval.v_float = -rettv->vval.v_float;
2807 }
2808 else
2809#endif
2810 {
2811 varnumber_T val;
2812 int error = FALSE;
2813
2814 // tv_get_number_chk() accepts a string, but we don't want that
2815 // here
2816 if (check_not_string(rettv) == FAIL)
2817 return FAIL;
2818 val = tv_get_number_chk(rettv, &error);
2819 clear_tv(rettv);
2820 if (error)
2821 return FAIL;
2822 if (*p == '-')
2823 val = -val;
2824 rettv->v_type = VAR_NUMBER;
2825 rettv->vval.v_number = val;
2826 }
2827 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002828 else if (numeric_only)
2829 {
2830 ++p;
2831 break;
2832 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002833 else
2834 {
2835 int v = tv2bool(rettv);
2836
2837 // '!' is permissive in the type.
2838 clear_tv(rettv);
2839 rettv->v_type = VAR_BOOL;
2840 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2841 }
2842 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002843 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002844 return OK;
2845}
2846
2847/*
2848 * Recognize v: variables that are constants and set "rettv".
2849 */
2850 static void
2851get_vim_constant(char_u **arg, typval_T *rettv)
2852{
2853 if (STRNCMP(*arg, "v:true", 6) == 0)
2854 {
2855 rettv->v_type = VAR_BOOL;
2856 rettv->vval.v_number = VVAL_TRUE;
2857 *arg += 6;
2858 }
2859 else if (STRNCMP(*arg, "v:false", 7) == 0)
2860 {
2861 rettv->v_type = VAR_BOOL;
2862 rettv->vval.v_number = VVAL_FALSE;
2863 *arg += 7;
2864 }
2865 else if (STRNCMP(*arg, "v:null", 6) == 0)
2866 {
2867 rettv->v_type = VAR_SPECIAL;
2868 rettv->vval.v_number = VVAL_NULL;
2869 *arg += 6;
2870 }
2871 else if (STRNCMP(*arg, "v:none", 6) == 0)
2872 {
2873 rettv->v_type = VAR_SPECIAL;
2874 rettv->vval.v_number = VVAL_NONE;
2875 *arg += 6;
2876 }
2877}
2878
Bram Moolenaar696ba232020-07-29 21:20:41 +02002879 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02002880get_compare_type(char_u *p, int *len, int *type_is)
2881{
2882 exptype_T type = EXPR_UNKNOWN;
2883 int i;
2884
2885 switch (p[0])
2886 {
2887 case '=': if (p[1] == '=')
2888 type = EXPR_EQUAL;
2889 else if (p[1] == '~')
2890 type = EXPR_MATCH;
2891 break;
2892 case '!': if (p[1] == '=')
2893 type = EXPR_NEQUAL;
2894 else if (p[1] == '~')
2895 type = EXPR_NOMATCH;
2896 break;
2897 case '>': if (p[1] != '=')
2898 {
2899 type = EXPR_GREATER;
2900 *len = 1;
2901 }
2902 else
2903 type = EXPR_GEQUAL;
2904 break;
2905 case '<': if (p[1] != '=')
2906 {
2907 type = EXPR_SMALLER;
2908 *len = 1;
2909 }
2910 else
2911 type = EXPR_SEQUAL;
2912 break;
2913 case 'i': if (p[1] == 's')
2914 {
2915 // "is" and "isnot"; but not a prefix of a name
2916 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2917 *len = 5;
2918 i = p[*len];
2919 if (!isalnum(i) && i != '_')
2920 {
2921 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2922 *type_is = TRUE;
2923 }
2924 }
2925 break;
2926 }
2927 return type;
2928}
2929
2930/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002932 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933 */
2934 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002935compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002937 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938
2939 // this works from end to start
2940 while (p > start)
2941 {
2942 --p;
2943 if (*p == '-' || *p == '+')
2944 {
2945 int negate = *p == '-';
2946 isn_T *isn;
2947
2948 // TODO: check type
2949 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2950 {
2951 --p;
2952 if (*p == '-')
2953 negate = !negate;
2954 }
2955 // only '-' has an effect, for '+' we only check the type
2956 if (negate)
2957 isn = generate_instr(cctx, ISN_NEGATENR);
2958 else
2959 isn = generate_instr(cctx, ISN_CHECKNR);
2960 if (isn == NULL)
2961 return FAIL;
2962 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002963 else if (numeric_only)
2964 {
2965 ++p;
2966 break;
2967 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 else
2969 {
2970 int invert = TRUE;
2971
2972 while (p > start && p[-1] == '!')
2973 {
2974 --p;
2975 invert = !invert;
2976 }
2977 if (generate_2BOOL(cctx, invert) == FAIL)
2978 return FAIL;
2979 }
2980 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002981 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982 return OK;
2983}
2984
2985/*
2986 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002987 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002988 */
2989 static int
2990compile_subscript(
2991 char_u **arg,
2992 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002993 char_u *start_leader,
2994 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02002995 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002997 char_u *name_start = *end_leader;
2998
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 for (;;)
3000 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003001 char_u *p = skipwhite(*arg);
3002
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003003 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003004 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003005 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003006
3007 // If a following line starts with "->{" or "->X" advance to that
3008 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003009 // Also if a following line starts with ".x".
3010 if (next != NULL &&
3011 ((next[0] == '-' && next[1] == '>'
3012 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003013 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003014 {
3015 next = next_line_from_context(cctx, TRUE);
3016 if (next == NULL)
3017 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003018 *arg = next;
3019 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003020 }
3021 }
3022
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003023 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3024 // not a function call.
3025 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003027 garray_T *stack = &cctx->ctx_type_stack;
3028 type_T *type;
3029 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003031 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003032 return FAIL;
3033
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003035 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3036
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003037 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3039 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003040 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041 return FAIL;
3042 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003043 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003045 char_u *pstart = p;
3046
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003047 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003048 return FAIL;
3049
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 // something->method()
3051 // Apply the '!', '-' and '+' first:
3052 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003053 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003056 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003057 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003058 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 if (**arg == '{')
3060 {
3061 // lambda call: list->{lambda}
3062 if (compile_lambda_call(arg, cctx) == FAIL)
3063 return FAIL;
3064 }
3065 else
3066 {
3067 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003068 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003069 if (!eval_isnamec1(*p))
3070 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003071 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003072 return FAIL;
3073 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003074 if (ASCII_ISALPHA(*p) && p[1] == ':')
3075 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003076 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003077 ;
3078 if (*p != '(')
3079 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003080 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081 return FAIL;
3082 }
3083 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003084 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085 return FAIL;
3086 }
3087 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003088 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003090 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003091 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003092 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003093 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003094 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003095
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003096 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003097 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003098 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003099 // TODO: blob index
3100 // TODO: more arguments
3101 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003102 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003103 return FAIL;
3104
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003105 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003106 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003107 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003108 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003109 if (**arg == ':')
3110 // missing first index is equal to zero
3111 generate_PUSHNR(cctx, 0);
3112 else
3113 {
3114 if (compile_expr0(arg, cctx) == FAIL)
3115 return FAIL;
3116 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3117 return FAIL;
3118 *arg = skipwhite(*arg);
3119 }
3120 if (**arg == ':')
3121 {
3122 *arg = skipwhite(*arg + 1);
3123 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3124 return FAIL;
3125 if (**arg == ']')
3126 // missing second index is equal to end of string
3127 generate_PUSHNR(cctx, -1);
3128 else
3129 {
3130 if (compile_expr0(arg, cctx) == FAIL)
3131 return FAIL;
3132 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3133 return FAIL;
3134 *arg = skipwhite(*arg);
3135 }
3136 is_slice = TRUE;
3137 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138
3139 if (**arg != ']')
3140 {
3141 emsg(_(e_missbrac));
3142 return FAIL;
3143 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003144 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003146 // We can index a list and a dict. If we don't know the type
3147 // we can use the index value type.
3148 // TODO: If we don't know use an instruction to figure it out at
3149 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003150 typep = ((type_T **)stack->ga_data) + stack->ga_len
3151 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003152 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003153 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3154 // If the index is a string, the variable must be a Dict.
3155 if (*typep == &t_any && valtype == &t_string)
3156 vtype = VAR_DICT;
3157 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003158 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003159 if (need_type(valtype, &t_number, -1, cctx, FALSE) == FAIL)
3160 return FAIL;
3161 if (is_slice)
3162 {
3163 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
3164 if (need_type(valtype, &t_number, -2, cctx, FALSE) == FAIL)
3165 return FAIL;
3166 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003167 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003168
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003169 if (vtype == VAR_DICT)
3170 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003171 if (is_slice)
3172 {
3173 emsg(_(e_cannot_slice_dictionary));
3174 return FAIL;
3175 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003176 if ((*typep)->tt_type == VAR_DICT)
3177 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003178 else
3179 {
3180 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3181 return FAIL;
3182 *typep = &t_any;
3183 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003184 if (may_generate_2STRING(-1, cctx) == FAIL)
3185 return FAIL;
3186 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3187 return FAIL;
3188 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003189 else if (vtype == VAR_STRING)
3190 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003191 *typep = &t_string;
3192 if ((is_slice
3193 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3194 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003195 return FAIL;
3196 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003197 else if (vtype == VAR_BLOB)
3198 {
3199 emsg("Sorry, blob index and slice not implemented yet");
3200 return FAIL;
3201 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003202 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003203 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003204 if (is_slice)
3205 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003206 if (generate_instr_drop(cctx,
3207 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3208 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003209 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003210 }
Bram Moolenaared591872020-08-15 22:14:53 +02003211 else
3212 {
3213 if ((*typep)->tt_type == VAR_LIST)
3214 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003215 if (generate_instr_drop(cctx,
3216 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3217 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003218 return FAIL;
3219 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003220 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003221 else
3222 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003223 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003224 return FAIL;
3225 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003227 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003229 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003230 return FAIL;
3231
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003232 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003233 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3234 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003236 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003237 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 while (eval_isnamec(*p))
3239 MB_PTR_ADV(p);
3240 if (p == *arg)
3241 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003242 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243 return FAIL;
3244 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003245 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 return FAIL;
3247 *arg = p;
3248 }
3249 else
3250 break;
3251 }
3252
3253 // TODO - see handle_subscript():
3254 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3255 // Don't do this when "Func" is already a partial that was bound
3256 // explicitly (pt_auto is FALSE).
3257
3258 return OK;
3259}
3260
3261/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003262 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3263 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003265 * If the value is a constant "ppconst->pp_ret" will be set.
3266 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003267 *
3268 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 */
3270
3271/*
3272 * number number constant
3273 * 0zFFFFFFFF Blob constant
3274 * "string" string constant
3275 * 'string' literal string constant
3276 * &option-name option value
3277 * @r register contents
3278 * identifier variable value
3279 * function() function call
3280 * $VAR environment variable
3281 * (expression) nested expression
3282 * [expr, expr] List
3283 * {key: val, key: val} Dictionary
3284 * #{key: val, key: val} Dictionary with literal keys
3285 *
3286 * Also handle:
3287 * ! in front logical NOT
3288 * - in front unary minus
3289 * + in front unary plus (ignored)
3290 * trailing (arg) funcref/partial call
3291 * trailing [] subscript in String or List
3292 * trailing .name entry in Dictionary
3293 * trailing ->name() method call
3294 */
3295 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003296compile_expr7(
3297 char_u **arg,
3298 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003299 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 char_u *start_leader, *end_leader;
3302 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003303 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003304 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305
3306 /*
3307 * Skip '!', '-' and '+' characters. They are handled later.
3308 */
3309 start_leader = *arg;
3310 while (**arg == '!' || **arg == '-' || **arg == '+')
3311 *arg = skipwhite(*arg + 1);
3312 end_leader = *arg;
3313
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003314 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003315 switch (**arg)
3316 {
3317 /*
3318 * Number constant.
3319 */
3320 case '0': // also for blob starting with 0z
3321 case '1':
3322 case '2':
3323 case '3':
3324 case '4':
3325 case '5':
3326 case '6':
3327 case '7':
3328 case '8':
3329 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003330 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003332 // Apply "-" and "+" just before the number now, right to
3333 // left. Matters especially when "->" follows. Stops at
3334 // '!'.
3335 if (apply_leader(rettv, TRUE,
3336 start_leader, &end_leader) == FAIL)
3337 {
3338 clear_tv(rettv);
3339 return FAIL;
3340 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 break;
3342
3343 /*
3344 * String constant: "string".
3345 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003346 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003347 return FAIL;
3348 break;
3349
3350 /*
3351 * Literal string constant: 'str''ing'.
3352 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003353 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 return FAIL;
3355 break;
3356
3357 /*
3358 * Constant Vim variable.
3359 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003360 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003361 ret = NOTDONE;
3362 break;
3363
3364 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003365 * "true" constant
3366 */
3367 case 't': if (STRNCMP(*arg, "true", 4) == 0
3368 && !eval_isnamec((*arg)[4]))
3369 {
3370 *arg += 4;
3371 rettv->v_type = VAR_BOOL;
3372 rettv->vval.v_number = VVAL_TRUE;
3373 }
3374 else
3375 ret = NOTDONE;
3376 break;
3377
3378 /*
3379 * "false" constant
3380 */
3381 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3382 && !eval_isnamec((*arg)[5]))
3383 {
3384 *arg += 5;
3385 rettv->v_type = VAR_BOOL;
3386 rettv->vval.v_number = VVAL_FALSE;
3387 }
3388 else
3389 ret = NOTDONE;
3390 break;
3391
3392 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393 * List: [expr, expr]
3394 */
3395 case '[': ret = compile_list(arg, cctx);
3396 break;
3397
3398 /*
3399 * Dictionary: #{key: val, key: val}
3400 */
3401 case '#': if ((*arg)[1] == '{')
3402 {
3403 ++*arg;
3404 ret = compile_dict(arg, cctx, TRUE);
3405 }
3406 else
3407 ret = NOTDONE;
3408 break;
3409
3410 /*
3411 * Lambda: {arg, arg -> expr}
3412 * Dictionary: {'key': val, 'key': val}
3413 */
3414 case '{': {
3415 char_u *start = skipwhite(*arg + 1);
3416
3417 // Find out what comes after the arguments.
3418 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003419 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 if (ret != FAIL && *start == '>')
3421 ret = compile_lambda(arg, cctx);
3422 else
3423 ret = compile_dict(arg, cctx, FALSE);
3424 }
3425 break;
3426
3427 /*
3428 * Option value: &name
3429 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003430 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3431 return FAIL;
3432 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003433 break;
3434
3435 /*
3436 * Environment variable: $VAR.
3437 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003438 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3439 return FAIL;
3440 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003441 break;
3442
3443 /*
3444 * Register contents: @r.
3445 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003446 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3447 return FAIL;
3448 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 break;
3450 /*
3451 * nested expression: (expression).
3452 */
3453 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003454
3455 // recursive!
3456 if (ppconst->pp_used <= PPSIZE - 10)
3457 {
3458 ret = compile_expr1(arg, cctx, ppconst);
3459 }
3460 else
3461 {
3462 // Not enough space in ppconst, flush constants.
3463 if (generate_ppconst(cctx, ppconst) == FAIL)
3464 return FAIL;
3465 ret = compile_expr0(arg, cctx);
3466 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 *arg = skipwhite(*arg);
3468 if (**arg == ')')
3469 ++*arg;
3470 else if (ret == OK)
3471 {
3472 emsg(_(e_missing_close));
3473 ret = FAIL;
3474 }
3475 break;
3476
3477 default: ret = NOTDONE;
3478 break;
3479 }
3480 if (ret == FAIL)
3481 return FAIL;
3482
Bram Moolenaar1c747212020-05-09 18:28:34 +02003483 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003485 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003486 clear_tv(rettv);
3487 else
3488 // A constant expression can possibly be handled compile time,
3489 // return the value instead of generating code.
3490 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003491 }
3492 else if (ret == NOTDONE)
3493 {
3494 char_u *p;
3495 int r;
3496
3497 if (!eval_isnamec1(**arg))
3498 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003499 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 return FAIL;
3501 }
3502
3503 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003504 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003505 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003506 {
3507 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3508 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003510 {
3511 if (generate_ppconst(cctx, ppconst) == FAIL)
3512 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003514 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515 if (r == FAIL)
3516 return FAIL;
3517 }
3518
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003519 // Handle following "[]", ".member", etc.
3520 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003521 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003522 ppconst) == FAIL)
3523 return FAIL;
3524 if (ppconst->pp_used > 0)
3525 {
3526 // apply the '!', '-' and '+' before the constant
3527 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003528 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003529 return FAIL;
3530 return OK;
3531 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003532 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003534 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535}
3536
3537/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003538 * Give the "white on both sides" error, taking the operator from "p[len]".
3539 */
3540 void
3541error_white_both(char_u *op, int len)
3542{
3543 char_u buf[10];
3544
3545 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003546 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003547}
3548
3549/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003550 * <type>expr7: runtime type check / conversion
3551 */
3552 static int
3553compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3554{
3555 type_T *want_type = NULL;
3556
3557 // Recognize <type>
3558 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3559 {
3560 int called_emsg_before = called_emsg;
3561
3562 ++*arg;
3563 want_type = parse_type(arg, cctx->ctx_type_list);
3564 if (called_emsg != called_emsg_before)
3565 return FAIL;
3566
3567 if (**arg != '>')
3568 {
3569 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003570 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003571 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003572 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003573 return FAIL;
3574 }
3575 ++*arg;
3576 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3577 return FAIL;
3578 }
3579
3580 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3581 return FAIL;
3582
3583 if (want_type != NULL)
3584 {
3585 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003586 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003587
Bram Moolenaard1103582020-08-14 22:44:25 +02003588 generate_ppconst(cctx, ppconst);
3589 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003590 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003591 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003592 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3593 return FAIL;
3594 }
3595 }
3596
3597 return OK;
3598}
3599
3600/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601 * * number multiplication
3602 * / number division
3603 * % number modulo
3604 */
3605 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003606compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607{
3608 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003609 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003610 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003612 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003613 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 return FAIL;
3615
3616 /*
3617 * Repeat computing, until no "*", "/" or "%" is following.
3618 */
3619 for (;;)
3620 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003621 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622 if (*op != '*' && *op != '/' && *op != '%')
3623 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003624 if (next != NULL)
3625 {
3626 *arg = next_line_from_context(cctx, TRUE);
3627 op = skipwhite(*arg);
3628 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003629
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003630 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003631 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003632 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003633 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634 }
3635 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003636 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003637 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003639 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003640 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003642
3643 if (ppconst->pp_used == ppconst_used + 2
3644 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3645 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003646 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003647 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3648 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003649 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003651 // both are numbers: compute the result
3652 switch (*op)
3653 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003654 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003655 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003656 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003657 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003658 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003659 break;
3660 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003661 tv1->vval.v_number = res;
3662 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003663 }
3664 else
3665 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003666 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003667 generate_two_op(cctx, op);
3668 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 }
3670
3671 return OK;
3672}
3673
3674/*
3675 * + number addition
3676 * - number subtraction
3677 * .. string concatenation
3678 */
3679 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003680compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681{
3682 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003683 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003685 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686
3687 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003688 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689 return FAIL;
3690
3691 /*
3692 * Repeat computing, until no "+", "-" or ".." is following.
3693 */
3694 for (;;)
3695 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003696 op = may_peek_next_line(cctx, *arg, &next);
3697 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 break;
3699 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003700 if (next != NULL)
3701 {
3702 *arg = next_line_from_context(cctx, TRUE);
3703 op = skipwhite(*arg);
3704 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003706 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003708 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003709 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 }
3711
3712 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003713 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003714 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003716 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003717 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003718 return FAIL;
3719
Bram Moolenaara5565e42020-05-09 15:44:01 +02003720 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003721 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003722 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3723 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3724 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3725 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003727 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3728 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003729
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003730 // concat/subtract/add constant numbers
3731 if (*op == '+')
3732 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3733 else if (*op == '-')
3734 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3735 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003736 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003737 // concatenate constant strings
3738 char_u *s1 = tv1->vval.v_string;
3739 char_u *s2 = tv2->vval.v_string;
3740 size_t len1 = STRLEN(s1);
3741
3742 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3743 if (tv1->vval.v_string == NULL)
3744 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003745 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003746 return FAIL;
3747 }
3748 mch_memmove(tv1->vval.v_string, s1, len1);
3749 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003750 vim_free(s1);
3751 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003752 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003753 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003754 }
3755 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003756 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003757 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003758 if (*op == '.')
3759 {
3760 if (may_generate_2STRING(-2, cctx) == FAIL
3761 || may_generate_2STRING(-1, cctx) == FAIL)
3762 return FAIL;
3763 generate_instr_drop(cctx, ISN_CONCAT, 1);
3764 }
3765 else
3766 generate_two_op(cctx, op);
3767 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 }
3769
3770 return OK;
3771}
3772
3773/*
3774 * expr5a == expr5b
3775 * expr5a =~ expr5b
3776 * expr5a != expr5b
3777 * expr5a !~ expr5b
3778 * expr5a > expr5b
3779 * expr5a >= expr5b
3780 * expr5a < expr5b
3781 * expr5a <= expr5b
3782 * expr5a is expr5b
3783 * expr5a isnot expr5b
3784 *
3785 * Produces instructions:
3786 * EVAL expr5a Push result of "expr5a"
3787 * EVAL expr5b Push result of "expr5b"
3788 * COMPARE one of the compare instructions
3789 */
3790 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003791compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792{
3793 exptype_T type = EXPR_UNKNOWN;
3794 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003795 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003796 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003798 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799
3800 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003801 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 return FAIL;
3803
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003804 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003805 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806
3807 /*
3808 * If there is a comparative operator, use it.
3809 */
3810 if (type != EXPR_UNKNOWN)
3811 {
3812 int ic = FALSE; // Default: do not ignore case
3813
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003814 if (next != NULL)
3815 {
3816 *arg = next_line_from_context(cctx, TRUE);
3817 p = skipwhite(*arg);
3818 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819 if (type_is && (p[len] == '?' || p[len] == '#'))
3820 {
3821 semsg(_(e_invexpr2), *arg);
3822 return FAIL;
3823 }
3824 // extra question mark appended: ignore case
3825 if (p[len] == '?')
3826 {
3827 ic = TRUE;
3828 ++len;
3829 }
3830 // extra '#' appended: match case (ignored)
3831 else if (p[len] == '#')
3832 ++len;
3833 // nothing appended: match case
3834
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003835 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003837 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003838 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839 }
3840
3841 // get the second variable
3842 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003843 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003844 return FAIL;
3845
Bram Moolenaara5565e42020-05-09 15:44:01 +02003846 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 return FAIL;
3848
Bram Moolenaara5565e42020-05-09 15:44:01 +02003849 if (ppconst->pp_used == ppconst_used + 2)
3850 {
3851 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3852 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3853 int ret;
3854
3855 // Both sides are a constant, compute the result now.
3856 // First check for a valid combination of types, this is more
3857 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003858 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003859 ret = FAIL;
3860 else
3861 {
3862 ret = typval_compare(tv1, tv2, type, ic);
3863 tv1->v_type = VAR_BOOL;
3864 tv1->vval.v_number = tv1->vval.v_number
3865 ? VVAL_TRUE : VVAL_FALSE;
3866 clear_tv(tv2);
3867 --ppconst->pp_used;
3868 }
3869 return ret;
3870 }
3871
3872 generate_ppconst(cctx, ppconst);
3873 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003874 }
3875
3876 return OK;
3877}
3878
Bram Moolenaar7f141552020-05-09 17:35:53 +02003879static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3880
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003881/*
3882 * Compile || or &&.
3883 */
3884 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003885compile_and_or(
3886 char_u **arg,
3887 cctx_T *cctx,
3888 char *op,
3889 ppconst_T *ppconst,
3890 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003891{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003892 char_u *next;
3893 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894 int opchar = *op;
3895
3896 if (p[0] == opchar && p[1] == opchar)
3897 {
3898 garray_T *instr = &cctx->ctx_instr;
3899 garray_T end_ga;
3900
3901 /*
3902 * Repeat until there is no following "||" or "&&"
3903 */
3904 ga_init2(&end_ga, sizeof(int), 10);
3905 while (p[0] == opchar && p[1] == opchar)
3906 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003907 if (next != NULL)
3908 {
3909 *arg = next_line_from_context(cctx, TRUE);
3910 p = skipwhite(*arg);
3911 }
3912
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003913 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3914 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003915 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003916 return FAIL;
3917 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918
Bram Moolenaara5565e42020-05-09 15:44:01 +02003919 // TODO: use ppconst if the value is a constant
3920 generate_ppconst(cctx, ppconst);
3921
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922 if (ga_grow(&end_ga, 1) == FAIL)
3923 {
3924 ga_clear(&end_ga);
3925 return FAIL;
3926 }
3927 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3928 ++end_ga.ga_len;
3929 generate_JUMP(cctx, opchar == '|'
3930 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3931
3932 // eval the next expression
3933 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003934 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003935 return FAIL;
3936
Bram Moolenaara5565e42020-05-09 15:44:01 +02003937 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3938 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939 {
3940 ga_clear(&end_ga);
3941 return FAIL;
3942 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003943
3944 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003946 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947
3948 // Fill in the end label in all jumps.
3949 while (end_ga.ga_len > 0)
3950 {
3951 isn_T *isn;
3952
3953 --end_ga.ga_len;
3954 isn = ((isn_T *)instr->ga_data)
3955 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3956 isn->isn_arg.jump.jump_where = instr->ga_len;
3957 }
3958 ga_clear(&end_ga);
3959 }
3960
3961 return OK;
3962}
3963
3964/*
3965 * expr4a && expr4a && expr4a logical AND
3966 *
3967 * Produces instructions:
3968 * EVAL expr4a Push result of "expr4a"
3969 * JUMP_AND_KEEP_IF_FALSE end
3970 * EVAL expr4b Push result of "expr4b"
3971 * JUMP_AND_KEEP_IF_FALSE end
3972 * EVAL expr4c Push result of "expr4c"
3973 * end:
3974 */
3975 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003976compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003977{
Bram Moolenaara5565e42020-05-09 15:44:01 +02003978 int ppconst_used = ppconst->pp_used;
3979
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003981 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982 return FAIL;
3983
3984 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02003985 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986}
3987
3988/*
3989 * expr3a || expr3b || expr3c logical OR
3990 *
3991 * Produces instructions:
3992 * EVAL expr3a Push result of "expr3a"
3993 * JUMP_AND_KEEP_IF_TRUE end
3994 * EVAL expr3b Push result of "expr3b"
3995 * JUMP_AND_KEEP_IF_TRUE end
3996 * EVAL expr3c Push result of "expr3c"
3997 * end:
3998 */
3999 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004000compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004002 int ppconst_used = ppconst->pp_used;
4003
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004005 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004006 return FAIL;
4007
4008 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004009 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010}
4011
4012/*
4013 * Toplevel expression: expr2 ? expr1a : expr1b
4014 *
4015 * Produces instructions:
4016 * EVAL expr2 Push result of "expr"
4017 * JUMP_IF_FALSE alt jump if false
4018 * EVAL expr1a
4019 * JUMP_ALWAYS end
4020 * alt: EVAL expr1b
4021 * end:
4022 */
4023 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004024compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025{
4026 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004027 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004028 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029
Bram Moolenaar3988f642020-08-27 22:43:03 +02004030 // Ignore all kinds of errors when not producing code.
4031 if (cctx->ctx_skip == SKIP_YES)
4032 {
4033 skip_expr(arg);
4034 return OK;
4035 }
4036
Bram Moolenaar61a89812020-05-07 16:58:17 +02004037 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004038 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039 return FAIL;
4040
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004041 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 if (*p == '?')
4043 {
4044 garray_T *instr = &cctx->ctx_instr;
4045 garray_T *stack = &cctx->ctx_type_stack;
4046 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004047 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004049 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004050 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004051 int has_const_expr = FALSE;
4052 int const_value = FALSE;
4053 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004055 if (next != NULL)
4056 {
4057 *arg = next_line_from_context(cctx, TRUE);
4058 p = skipwhite(*arg);
4059 }
4060
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004061 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4062 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004063 semsg(_(e_white_space_required_before_and_after_str), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004064 return FAIL;
4065 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004066
Bram Moolenaara5565e42020-05-09 15:44:01 +02004067 if (ppconst->pp_used == ppconst_used + 1)
4068 {
4069 // the condition is a constant, we know whether the ? or the :
4070 // expression is to be evaluated.
4071 has_const_expr = TRUE;
4072 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4073 clear_tv(&ppconst->pp_tv[ppconst_used]);
4074 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004075 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4076 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004077 }
4078 else
4079 {
4080 generate_ppconst(cctx, ppconst);
4081 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4082 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004083
4084 // evaluate the second expression; any type is accepted
4085 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004086 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004087 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004088 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004089 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090
Bram Moolenaara5565e42020-05-09 15:44:01 +02004091 if (!has_const_expr)
4092 {
4093 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004094
Bram Moolenaara5565e42020-05-09 15:44:01 +02004095 // remember the type and drop it
4096 --stack->ga_len;
4097 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098
Bram Moolenaara5565e42020-05-09 15:44:01 +02004099 end_idx = instr->ga_len;
4100 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4101
4102 // jump here from JUMP_IF_FALSE
4103 isn = ((isn_T *)instr->ga_data) + alt_idx;
4104 isn->isn_arg.jump.jump_where = instr->ga_len;
4105 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106
4107 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004108 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004109 if (*p != ':')
4110 {
4111 emsg(_(e_missing_colon));
4112 return FAIL;
4113 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004114 if (next != NULL)
4115 {
4116 *arg = next_line_from_context(cctx, TRUE);
4117 p = skipwhite(*arg);
4118 }
4119
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004120 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4121 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004122 semsg(_(e_white_space_required_before_and_after_str), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004123 return FAIL;
4124 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004125
4126 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004127 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004128 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4129 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004131 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004132 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004133 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004134 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004135
Bram Moolenaara5565e42020-05-09 15:44:01 +02004136 if (!has_const_expr)
4137 {
4138 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139
Bram Moolenaara5565e42020-05-09 15:44:01 +02004140 // If the types differ, the result has a more generic type.
4141 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4142 common_type(type1, type2, &type2, cctx->ctx_type_list);
4143
4144 // jump here from JUMP_ALWAYS
4145 isn = ((isn_T *)instr->ga_data) + end_idx;
4146 isn->isn_arg.jump.jump_where = instr->ga_len;
4147 }
4148
4149 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004150 }
4151 return OK;
4152}
4153
4154/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004155 * Toplevel expression.
4156 */
4157 static int
4158compile_expr0(char_u **arg, cctx_T *cctx)
4159{
4160 ppconst_T ppconst;
4161
4162 CLEAR_FIELD(ppconst);
4163 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4164 {
4165 clear_ppconst(&ppconst);
4166 return FAIL;
4167 }
4168 if (generate_ppconst(cctx, &ppconst) == FAIL)
4169 return FAIL;
4170 return OK;
4171}
4172
4173/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004174 * compile "return [expr]"
4175 */
4176 static char_u *
4177compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4178{
4179 char_u *p = arg;
4180 garray_T *stack = &cctx->ctx_type_stack;
4181 type_T *stack_type;
4182
4183 if (*p != NUL && *p != '|' && *p != '\n')
4184 {
4185 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004186 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004187 return NULL;
4188
4189 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4190 if (set_return_type)
4191 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004192 else
4193 {
4194 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4195 && stack_type->tt_type != VAR_VOID
4196 && stack_type->tt_type != VAR_UNKNOWN)
4197 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004198 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004199 return NULL;
4200 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004201 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4202 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004203 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004204 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 }
4206 else
4207 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004208 // "set_return_type" cannot be TRUE, only used for a lambda which
4209 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004210 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4211 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004213 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214 return NULL;
4215 }
4216
4217 // No argument, return zero.
4218 generate_PUSHNR(cctx, 0);
4219 }
4220
4221 if (generate_instr(cctx, ISN_RETURN) == NULL)
4222 return NULL;
4223
4224 // "return val | endif" is possible
4225 return skipwhite(p);
4226}
4227
4228/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004229 * Get a line from the compilation context, compatible with exarg_T getline().
4230 * Return a pointer to the line in allocated memory.
4231 * Return NULL for end-of-file or some error.
4232 */
4233 static char_u *
4234exarg_getline(
4235 int c UNUSED,
4236 void *cookie,
4237 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004238 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004239{
4240 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004241 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004242
Bram Moolenaar66250c92020-08-20 15:02:42 +02004243 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004244 {
Bram Moolenaar66250c92020-08-20 15:02:42 +02004245 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4246 return NULL;
4247 ++cctx->ctx_lnum;
4248 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4249 // Comment lines result in NULL pointers, skip them.
4250 if (p != NULL)
4251 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004252 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004253}
4254
4255/*
4256 * Compile a nested :def command.
4257 */
4258 static char_u *
4259compile_nested_function(exarg_T *eap, cctx_T *cctx)
4260{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004261 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004262 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004263 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004264 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004265 lvar_T *lvar;
4266 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004267 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004268
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004269 // Only g:Func() can use a namespace.
4270 if (name_start[1] == ':' && !is_global)
4271 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004272 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004273 return NULL;
4274 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004275 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4276 return NULL;
4277
Bram Moolenaar04b12692020-05-04 23:24:44 +02004278 eap->arg = name_end;
4279 eap->getline = exarg_getline;
4280 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004281 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004282 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004283 lambda_name = get_lambda_name();
4284 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004285
Bram Moolenaar822ba242020-05-24 23:00:18 +02004286 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004287 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004288 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004289 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004290 return NULL;
4291
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004292 if (is_global)
4293 {
4294 char_u *func_name = vim_strnsave(name_start + 2,
4295 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004296
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004297 if (func_name == NULL)
4298 r = FAIL;
4299 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004300 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004301 }
4302 else
4303 {
4304 // Define a local variable for the function reference.
4305 lvar = reserve_local(cctx, name_start, name_end - name_start,
4306 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004307 if (lvar == NULL)
4308 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004309 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004310 return NULL;
4311 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4312 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004313
Bram Moolenaar61a89812020-05-07 16:58:17 +02004314 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004315 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004316}
4317
4318/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 * Return the length of an assignment operator, or zero if there isn't one.
4320 */
4321 int
4322assignment_len(char_u *p, int *heredoc)
4323{
4324 if (*p == '=')
4325 {
4326 if (p[1] == '<' && p[2] == '<')
4327 {
4328 *heredoc = TRUE;
4329 return 3;
4330 }
4331 return 1;
4332 }
4333 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4334 return 2;
4335 if (STRNCMP(p, "..=", 3) == 0)
4336 return 3;
4337 return 0;
4338}
4339
4340// words that cannot be used as a variable
4341static char *reserved[] = {
4342 "true",
4343 "false",
4344 NULL
4345};
4346
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004347typedef enum {
4348 dest_local,
4349 dest_option,
4350 dest_env,
4351 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004352 dest_buffer,
4353 dest_window,
4354 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004355 dest_vimvar,
4356 dest_script,
4357 dest_reg,
4358} assign_dest_T;
4359
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004360/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004361 * Generate the load instruction for "name".
4362 */
4363 static void
4364generate_loadvar(
4365 cctx_T *cctx,
4366 assign_dest_T dest,
4367 char_u *name,
4368 lvar_T *lvar,
4369 type_T *type)
4370{
4371 switch (dest)
4372 {
4373 case dest_option:
4374 // TODO: check the option exists
4375 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4376 break;
4377 case dest_global:
4378 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4379 break;
4380 case dest_buffer:
4381 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4382 break;
4383 case dest_window:
4384 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4385 break;
4386 case dest_tab:
4387 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4388 break;
4389 case dest_script:
4390 compile_load_scriptvar(cctx,
4391 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4392 break;
4393 case dest_env:
4394 // Include $ in the name here
4395 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4396 break;
4397 case dest_reg:
4398 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4399 break;
4400 case dest_vimvar:
4401 generate_LOADV(cctx, name + 2, TRUE);
4402 break;
4403 case dest_local:
4404 if (lvar->lv_from_outer)
4405 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4406 NULL, type);
4407 else
4408 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4409 break;
4410 }
4411}
4412
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004413 void
4414vim9_declare_error(char_u *name)
4415{
4416 char *scope = "";
4417
4418 switch (*name)
4419 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004420 case 'g': scope = _("global"); break;
4421 case 'b': scope = _("buffer"); break;
4422 case 'w': scope = _("window"); break;
4423 case 't': scope = _("tab"); break;
4424 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004425 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004426 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004427 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004428 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004429 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004430 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004431 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004432 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004433 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004434}
4435
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004436/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004437 * Compile declaration and assignment:
4438 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004440 * Return NULL for an error.
4441 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 */
4443 static char_u *
4444compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4445{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004446 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004447 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004448 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004449 char_u *ret = NULL;
4450 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004451 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004452 int scriptvar_sid = 0;
4453 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004454 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004455 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004456 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004457 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004458 int oplen = 0;
4459 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004460 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004461 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004462 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004463 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004464 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004465
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004466 // Skip over the "var" or "[var, var]" to get to any "=".
4467 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4468 if (p == NULL)
4469 return *arg == '[' ? arg : NULL;
4470
4471 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004473 // TODO: should we allow this, and figure out type inference from list
4474 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004475 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476 return NULL;
4477 }
4478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004479 sp = p;
4480 p = skipwhite(p);
4481 op = p;
4482 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004483
4484 if (var_count > 0 && oplen == 0)
4485 // can be something like "[1, 2]->func()"
4486 return arg;
4487
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4489 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004490 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004491 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004492 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004493
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004494 if (heredoc)
4495 {
4496 list_T *l;
4497 listitem_T *li;
4498
4499 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004500 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004501 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004502 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503
4504 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004505 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004506 {
4507 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4508 li->li_tv.vval.v_string = NULL;
4509 }
4510 generate_NEWLIST(cctx, l->lv_len);
4511 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004512 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004513 list_free(l);
4514 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004515 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004517 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004518 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004519 // for "[var, var] = expr" evaluate the expression here, loop over the
4520 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004521
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004522 p = skipwhite(op + oplen);
4523 if (compile_expr0(&p, cctx) == FAIL)
4524 return NULL;
4525 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004526
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004527 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004528 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004529 type_T *stacktype;
4530
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004531 stacktype = stack->ga_len == 0 ? &t_void
4532 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004533 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004534 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004535 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004536 goto theend;
4537 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004538 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004539 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004540 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004541 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4542 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004543 }
4544 }
4545
4546 /*
4547 * Loop over variables in "[var, var] = expr".
4548 * For "var = expr" and "let var: type" this is done only once.
4549 */
4550 if (var_count > 0)
4551 var_start = skipwhite(arg + 1); // skip over the "["
4552 else
4553 var_start = arg;
4554 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4555 {
4556 char_u *var_end = skip_var_one(var_start, FALSE);
4557 size_t varlen;
4558 int new_local = FALSE;
4559 int opt_type;
4560 int opt_flags = 0;
4561 assign_dest_T dest = dest_local;
4562 int vimvaridx = -1;
4563 lvar_T *lvar = NULL;
4564 lvar_T arg_lvar;
4565 int has_type = FALSE;
4566 int has_index = FALSE;
4567 int instr_count = -1;
4568
Bram Moolenaar65821722020-08-02 18:58:54 +02004569 if (*var_start == '@')
4570 p = var_start + 2;
4571 else
4572 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02004573 // skip over the leading "&", "&l:", "&g:" and "$"
4574 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02004575 p = to_name_end(p, TRUE);
4576 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004577
4578 // "a: type" is declaring variable "a" with a type, not "a:".
4579 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4580 --var_end;
4581 if (is_decl && p == var_start + 2 && p[-1] == ':')
4582 --p;
4583
4584 varlen = p - var_start;
4585 vim_free(name);
4586 name = vim_strnsave(var_start, varlen);
4587 if (name == NULL)
4588 return NULL;
4589 if (!heredoc)
4590 type = &t_any;
4591
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004592 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004593 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004594 int declare_error = FALSE;
4595
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004596 if (*var_start == '&')
4597 {
4598 int cc;
4599 long numval;
4600
4601 dest = dest_option;
4602 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004603 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004604 emsg(_(e_const_option));
4605 goto theend;
4606 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004607 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004608 p = var_start;
4609 p = find_option_end(&p, &opt_flags);
4610 if (p == NULL)
4611 {
4612 // cannot happen?
4613 emsg(_(e_letunexp));
4614 goto theend;
4615 }
4616 cc = *p;
4617 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02004618 opt_type = get_option_value(skip_option_env_lead(var_start),
4619 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004620 *p = cc;
4621 if (opt_type == -3)
4622 {
4623 semsg(_(e_unknown_option), var_start);
4624 goto theend;
4625 }
4626 if (opt_type == -2 || opt_type == 0)
4627 type = &t_string;
4628 else
4629 type = &t_number; // both number and boolean option
4630 }
4631 else if (*var_start == '$')
4632 {
4633 dest = dest_env;
4634 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004635 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004636 }
4637 else if (*var_start == '@')
4638 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004639 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004640 {
4641 emsg_invreg(var_start[1]);
4642 goto theend;
4643 }
4644 dest = dest_reg;
4645 type = &t_string;
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, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004649 {
4650 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004651 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004652 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004653 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004654 {
4655 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004656 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004657 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004658 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004659 {
4660 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004661 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004662 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004663 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004664 {
4665 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004666 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004667 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004668 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004669 {
4670 typval_T *vtv;
4671 int di_flags;
4672
4673 vimvaridx = find_vim_var(name + 2, &di_flags);
4674 if (vimvaridx < 0)
4675 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004676 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004677 goto theend;
4678 }
4679 // We use the current value of "sandbox" here, is that OK?
4680 if (var_check_ro(di_flags, name, FALSE))
4681 goto theend;
4682 dest = dest_vimvar;
4683 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004684 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004685 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004686 }
4687 else
4688 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004689 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004690
4691 for (idx = 0; reserved[idx] != NULL; ++idx)
4692 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004693 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004694 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004695 goto theend;
4696 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004697
4698 lvar = lookup_local(var_start, varlen, cctx);
4699 if (lvar == NULL)
4700 {
4701 CLEAR_FIELD(arg_lvar);
4702 if (lookup_arg(var_start, varlen,
4703 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4704 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004705 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004706 if (is_decl)
4707 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004708 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004709 goto theend;
4710 }
4711 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004712 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004713 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004714 if (lvar != NULL)
4715 {
4716 if (is_decl)
4717 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004718 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004719 goto theend;
4720 }
4721 else if (lvar->lv_const)
4722 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004723 semsg(_(e_cannot_assign_to_constant), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004724 goto theend;
4725 }
4726 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004727 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004728 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004729 int script_namespace = varlen > 1
4730 && STRNCMP(var_start, "s:", 2) == 0;
4731 int script_var = (script_namespace
Bram Moolenaar53900992020-08-22 19:02:02 +02004732 ? lookup_script(var_start + 2, varlen - 2, FALSE)
4733 : lookup_script(var_start, varlen, TRUE)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004734 imported_T *import =
4735 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004736
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004737 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004738 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004739 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4740
4741 if (is_decl)
4742 {
4743 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004744 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004745 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004746 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004747 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004748 name);
4749 goto theend;
4750 }
4751 else if (cctx->ctx_ufunc->uf_script_ctx_version
4752 == SCRIPT_VERSION_VIM9
4753 && script_namespace
4754 && !script_var && import == NULL)
4755 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004756 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004757 goto theend;
4758 }
4759
4760 dest = dest_script;
4761
4762 // existing script-local variables should have a type
4763 scriptvar_sid = current_sctx.sc_sid;
4764 if (import != NULL)
4765 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004766 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004767 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02004768 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4769 rawname, TRUE);
4770 if (scriptvar_idx > 0)
4771 {
4772 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4773 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004774 ((svar_T *)si->sn_var_vals.ga_data)
4775 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004776 type = sv->sv_type;
4777 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004778 }
4779 }
4780 else if (name[1] == ':' && name[2] != NUL)
4781 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004782 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004783 goto theend;
4784 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004785 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004786 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004787 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004788 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004789 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004790 else if (check_defined(var_start, varlen, cctx) == FAIL)
4791 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004792 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004793 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004794
4795 if (declare_error)
4796 {
4797 vim9_declare_error(name);
4798 goto theend;
4799 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004800 }
4801
4802 // handle "a:name" as a name, not index "name" on "a"
4803 if (varlen > 1 || var_start[varlen] != ':')
4804 p = var_end;
4805
4806 if (dest != dest_option)
4807 {
4808 if (is_decl && *p == ':')
4809 {
4810 // parse optional type: "let var: type = expr"
4811 if (!VIM_ISWHITE(p[1]))
4812 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004813 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004814 goto theend;
4815 }
4816 p = skipwhite(p + 1);
4817 type = parse_type(&p, cctx->ctx_type_list);
4818 has_type = TRUE;
4819 }
4820 else if (lvar != NULL)
4821 type = lvar->lv_type;
4822 }
4823
4824 if (oplen == 3 && !heredoc && dest != dest_global
4825 && type->tt_type != VAR_STRING
4826 && type->tt_type != VAR_ANY)
4827 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004828 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004829 goto theend;
4830 }
4831
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004832 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004833 {
4834 if (oplen > 1 && !heredoc)
4835 {
4836 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004837 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004838 goto theend;
4839 }
4840
4841 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004842 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
4843 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004844 goto theend;
4845 lvar = reserve_local(cctx, var_start, varlen,
4846 cmdidx == CMD_const, type);
4847 if (lvar == NULL)
4848 goto theend;
4849 new_local = TRUE;
4850 }
4851
4852 member_type = type;
4853 if (var_end > var_start + varlen)
4854 {
4855 // Something follows after the variable: "var[idx]".
4856 if (is_decl)
4857 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004858 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004859 goto theend;
4860 }
4861
4862 if (var_start[varlen] == '[')
4863 {
4864 has_index = TRUE;
4865 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004866 member_type = &t_any;
4867 else
4868 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004869 }
4870 else
4871 {
4872 semsg("Not supported yet: %s", var_start);
4873 goto theend;
4874 }
4875 }
4876 else if (lvar == &arg_lvar)
4877 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004878 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004879 goto theend;
4880 }
4881
4882 if (!heredoc)
4883 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004884 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004885 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004886 if (oplen > 0 && var_count == 0)
4887 {
4888 // skip over the "=" and the expression
4889 p = skipwhite(op + oplen);
4890 compile_expr0(&p, cctx);
4891 }
4892 }
4893 else if (oplen > 0)
4894 {
4895 type_T *stacktype;
4896
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004897 // For "var = expr" evaluate the expression.
4898 if (var_count == 0)
4899 {
4900 int r;
4901
4902 // for "+=", "*=", "..=" etc. first load the current value
4903 if (*op != '=')
4904 {
4905 generate_loadvar(cctx, dest, name, lvar, type);
4906
4907 if (has_index)
4908 {
4909 // TODO: get member from list or dict
4910 emsg("Index with operation not supported yet");
4911 goto theend;
4912 }
4913 }
4914
4915 // Compile the expression. Temporarily hide the new local
4916 // variable here, it is not available to this expression.
4917 if (new_local)
4918 --cctx->ctx_locals.ga_len;
4919 instr_count = instr->ga_len;
4920 p = skipwhite(op + oplen);
4921 r = compile_expr0(&p, cctx);
4922 if (new_local)
4923 ++cctx->ctx_locals.ga_len;
4924 if (r == FAIL)
4925 goto theend;
4926 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004927 else if (semicolon && var_idx == var_count - 1)
4928 {
4929 // For "[var; var] = expr" get the rest of the list
4930 if (generate_SLICE(cctx, var_count - 1) == FAIL)
4931 goto theend;
4932 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004933 else
4934 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004935 // For "[var, var] = expr" get the "var_idx" item from the
4936 // list.
4937 if (generate_GETITEM(cctx, var_idx) == FAIL)
4938 return FAIL;
4939 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004940
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004941 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004942 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004943 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004944 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004945 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004946 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004947 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004948 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004949 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004950 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004951 }
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004952 else if ((stacktype->tt_type == VAR_FUNC
4953 || stacktype->tt_type == VAR_PARTIAL)
4954 && var_wrong_func_name(name, TRUE))
4955 {
4956 goto theend;
4957 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004958 else
4959 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004960 // An empty list or dict has a &t_void member,
4961 // for a variable that implies &t_any.
4962 if (stacktype == &t_list_empty)
4963 lvar->lv_type = &t_list_any;
4964 else if (stacktype == &t_dict_empty)
4965 lvar->lv_type = &t_dict_any;
4966 else
4967 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004968 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004969 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02004970 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004971 {
4972 type_T *use_type = lvar->lv_type;
4973
Bram Moolenaar93ad1472020-08-19 22:02:41 +02004974 // without operator type is here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004975 if (has_index)
4976 {
4977 use_type = use_type->tt_member;
4978 if (use_type == NULL)
4979 use_type = &t_void;
4980 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004981 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar93ad1472020-08-19 22:02:41 +02004982 == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004983 goto theend;
4984 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004985 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004986 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004987 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004988 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004989 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004990 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004991 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004992 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004993 goto theend;
4994 }
4995 else if (!has_type || dest == dest_option)
4996 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004997 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004998 goto theend;
4999 }
5000 else
5001 {
5002 // variables are always initialized
5003 if (ga_grow(instr, 1) == FAIL)
5004 goto theend;
5005 switch (member_type->tt_type)
5006 {
5007 case VAR_BOOL:
5008 generate_PUSHBOOL(cctx, VVAL_FALSE);
5009 break;
5010 case VAR_FLOAT:
5011#ifdef FEAT_FLOAT
5012 generate_PUSHF(cctx, 0.0);
5013#endif
5014 break;
5015 case VAR_STRING:
5016 generate_PUSHS(cctx, NULL);
5017 break;
5018 case VAR_BLOB:
5019 generate_PUSHBLOB(cctx, NULL);
5020 break;
5021 case VAR_FUNC:
5022 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5023 break;
5024 case VAR_LIST:
5025 generate_NEWLIST(cctx, 0);
5026 break;
5027 case VAR_DICT:
5028 generate_NEWDICT(cctx, 0);
5029 break;
5030 case VAR_JOB:
5031 generate_PUSHJOB(cctx, NULL);
5032 break;
5033 case VAR_CHANNEL:
5034 generate_PUSHCHANNEL(cctx, NULL);
5035 break;
5036 case VAR_NUMBER:
5037 case VAR_UNKNOWN:
5038 case VAR_ANY:
5039 case VAR_PARTIAL:
5040 case VAR_VOID:
5041 case VAR_SPECIAL: // cannot happen
5042 generate_PUSHNR(cctx, 0);
5043 break;
5044 }
5045 }
5046 if (var_count == 0)
5047 end = p;
5048 }
5049
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005050 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005051 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005052 break;
5053
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005054 if (oplen > 0 && *op != '=')
5055 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005056 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005057 type_T *stacktype;
5058
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005059 if (*op == '.')
5060 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005061 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005062 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005063 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005064 if (
5065#ifdef FEAT_FLOAT
5066 // If variable is float operation with number is OK.
5067 !(expected == &t_float && stacktype == &t_number) &&
5068#endif
5069 need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005070 goto theend;
5071
5072 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005073 {
5074 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5075 goto theend;
5076 }
5077 else if (*op == '+')
5078 {
5079 if (generate_add_instr(cctx,
5080 operator_type(member_type, stacktype),
5081 member_type, stacktype) == FAIL)
5082 goto theend;
5083 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005084 else if (generate_two_op(cctx, op) == FAIL)
5085 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005086 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005087
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005088 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005089 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005090 int r;
5091
5092 // Compile the "idx" in "var[idx]".
5093 if (new_local)
5094 --cctx->ctx_locals.ga_len;
5095 p = skipwhite(var_start + varlen + 1);
5096 r = compile_expr0(&p, cctx);
5097 if (new_local)
5098 ++cctx->ctx_locals.ga_len;
5099 if (r == FAIL)
5100 goto theend;
5101 if (*skipwhite(p) != ']')
5102 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005103 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005104 emsg(_(e_missbrac));
5105 goto theend;
5106 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005107 if (type == &t_any)
5108 {
5109 type_T *idx_type = ((type_T **)stack->ga_data)[
5110 stack->ga_len - 1];
5111 // Index on variable of unknown type: guess the type from the
5112 // index type: number is dict, otherwise dict.
5113 // TODO: should do the assignment at runtime
5114 if (idx_type->tt_type == VAR_NUMBER)
5115 type = &t_list_any;
5116 else
5117 type = &t_dict_any;
5118 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005119 if (type->tt_type == VAR_DICT
5120 && may_generate_2STRING(-1, cctx) == FAIL)
5121 goto theend;
5122 if (type->tt_type == VAR_LIST
5123 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005124 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005125 {
5126 emsg(_(e_number_exp));
5127 goto theend;
5128 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005129
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005130 // Load the dict or list. On the stack we then have:
5131 // - value
5132 // - index
5133 // - variable
5134 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005135
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005136 if (type->tt_type == VAR_LIST)
5137 {
5138 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5139 return FAIL;
5140 }
5141 else if (type->tt_type == VAR_DICT)
5142 {
5143 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5144 return FAIL;
5145 }
5146 else
5147 {
5148 emsg(_(e_listreq));
5149 goto theend;
5150 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005151 }
5152 else
5153 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005154 switch (dest)
5155 {
5156 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005157 generate_STOREOPT(cctx, skip_option_env_lead(name),
5158 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005159 break;
5160 case dest_global:
5161 // include g: with the name, easier to execute that way
5162 generate_STORE(cctx, ISN_STOREG, 0, name);
5163 break;
5164 case dest_buffer:
5165 // include b: with the name, easier to execute that way
5166 generate_STORE(cctx, ISN_STOREB, 0, name);
5167 break;
5168 case dest_window:
5169 // include w: with the name, easier to execute that way
5170 generate_STORE(cctx, ISN_STOREW, 0, name);
5171 break;
5172 case dest_tab:
5173 // include t: with the name, easier to execute that way
5174 generate_STORE(cctx, ISN_STORET, 0, name);
5175 break;
5176 case dest_env:
5177 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5178 break;
5179 case dest_reg:
5180 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5181 break;
5182 case dest_vimvar:
5183 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5184 break;
5185 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005186 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005187 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005188 {
5189 char_u *name_s = name;
5190
5191 // Include s: in the name for store_var()
5192 if (name[1] != ':')
5193 {
5194 int len = (int)STRLEN(name) + 3;
5195
5196 name_s = alloc(len);
5197 if (name_s == NULL)
5198 name_s = name;
5199 else
5200 vim_snprintf((char *)name_s, len,
5201 "s:%s", name);
5202 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005203 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5204 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005205 if (name_s != name)
5206 vim_free(name_s);
5207 }
5208 else
5209 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005210 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005211 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005212 break;
5213 case dest_local:
5214 if (lvar != NULL)
5215 {
5216 isn_T *isn = ((isn_T *)instr->ga_data)
5217 + instr->ga_len - 1;
5218
5219 // optimization: turn "var = 123" from ISN_PUSHNR +
5220 // ISN_STORE into ISN_STORENR
5221 if (!lvar->lv_from_outer
5222 && instr->ga_len == instr_count + 1
5223 && isn->isn_type == ISN_PUSHNR)
5224 {
5225 varnumber_T val = isn->isn_arg.number;
5226
5227 isn->isn_type = ISN_STORENR;
5228 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5229 isn->isn_arg.storenr.stnr_val = val;
5230 if (stack->ga_len > 0)
5231 --stack->ga_len;
5232 }
5233 else if (lvar->lv_from_outer)
5234 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005235 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005236 else
5237 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5238 }
5239 break;
5240 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005241 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005242
5243 if (var_idx + 1 < var_count)
5244 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005245 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005246
5247 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005248 if (var_count > 0 && !semicolon)
5249 {
5250 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5251 goto theend;
5252 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005253
Bram Moolenaarb2097502020-07-19 17:17:02 +02005254 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005255
5256theend:
5257 vim_free(name);
5258 return ret;
5259}
5260
5261/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005262 * Check if "name" can be "unlet".
5263 */
5264 int
5265check_vim9_unlet(char_u *name)
5266{
5267 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5268 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005269 // "unlet s:var" is allowed in legacy script.
5270 if (*name == 's' && !script_is_vim9())
5271 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005272 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005273 return FAIL;
5274 }
5275 return OK;
5276}
5277
5278/*
5279 * Callback passed to ex_unletlock().
5280 */
5281 static int
5282compile_unlet(
5283 lval_T *lvp,
5284 char_u *name_end,
5285 exarg_T *eap,
5286 int deep UNUSED,
5287 void *coookie)
5288{
5289 cctx_T *cctx = coookie;
5290
5291 if (lvp->ll_tv == NULL)
5292 {
5293 char_u *p = lvp->ll_name;
5294 int cc = *name_end;
5295 int ret = OK;
5296
5297 // Normal name. Only supports g:, w:, t: and b: namespaces.
5298 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005299 if (*p == '$')
5300 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5301 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005302 ret = FAIL;
5303 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005304 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005305
5306 *name_end = cc;
5307 return ret;
5308 }
5309
5310 // TODO: unlet {list}[idx]
5311 // TODO: unlet {dict}[key]
5312 emsg("Sorry, :unlet not fully implemented yet");
5313 return FAIL;
5314}
5315
5316/*
5317 * compile "unlet var", "lock var" and "unlock var"
5318 * "arg" points to "var".
5319 */
5320 static char_u *
5321compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5322{
5323 char_u *p = arg;
5324
5325 if (eap->cmdidx != CMD_unlet)
5326 {
5327 emsg("Sorry, :lock and unlock not implemented yet");
5328 return NULL;
5329 }
5330
5331 if (*p == '!')
5332 {
5333 p = skipwhite(p + 1);
5334 eap->forceit = TRUE;
5335 }
5336
5337 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5338 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5339}
5340
5341/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005342 * Compile an :import command.
5343 */
5344 static char_u *
5345compile_import(char_u *arg, cctx_T *cctx)
5346{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005347 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005348}
5349
5350/*
5351 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5352 */
5353 static int
5354compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5355{
5356 garray_T *instr = &cctx->ctx_instr;
5357 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5358
5359 if (endlabel == NULL)
5360 return FAIL;
5361 endlabel->el_next = *el;
5362 *el = endlabel;
5363 endlabel->el_end_label = instr->ga_len;
5364
5365 generate_JUMP(cctx, when, 0);
5366 return OK;
5367}
5368
5369 static void
5370compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5371{
5372 garray_T *instr = &cctx->ctx_instr;
5373
5374 while (*el != NULL)
5375 {
5376 endlabel_T *cur = (*el);
5377 isn_T *isn;
5378
5379 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5380 isn->isn_arg.jump.jump_where = instr->ga_len;
5381 *el = cur->el_next;
5382 vim_free(cur);
5383 }
5384}
5385
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005386 static void
5387compile_free_jump_to_end(endlabel_T **el)
5388{
5389 while (*el != NULL)
5390 {
5391 endlabel_T *cur = (*el);
5392
5393 *el = cur->el_next;
5394 vim_free(cur);
5395 }
5396}
5397
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005398/*
5399 * Create a new scope and set up the generic items.
5400 */
5401 static scope_T *
5402new_scope(cctx_T *cctx, scopetype_T type)
5403{
5404 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5405
5406 if (scope == NULL)
5407 return NULL;
5408 scope->se_outer = cctx->ctx_scope;
5409 cctx->ctx_scope = scope;
5410 scope->se_type = type;
5411 scope->se_local_count = cctx->ctx_locals.ga_len;
5412 return scope;
5413}
5414
5415/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005416 * Free the current scope and go back to the outer scope.
5417 */
5418 static void
5419drop_scope(cctx_T *cctx)
5420{
5421 scope_T *scope = cctx->ctx_scope;
5422
5423 if (scope == NULL)
5424 {
5425 iemsg("calling drop_scope() without a scope");
5426 return;
5427 }
5428 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005429 switch (scope->se_type)
5430 {
5431 case IF_SCOPE:
5432 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5433 case FOR_SCOPE:
5434 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5435 case WHILE_SCOPE:
5436 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5437 case TRY_SCOPE:
5438 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5439 case NO_SCOPE:
5440 case BLOCK_SCOPE:
5441 break;
5442 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005443 vim_free(scope);
5444}
5445
5446/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005447 * compile "if expr"
5448 *
5449 * "if expr" Produces instructions:
5450 * EVAL expr Push result of "expr"
5451 * JUMP_IF_FALSE end
5452 * ... body ...
5453 * end:
5454 *
5455 * "if expr | else" Produces instructions:
5456 * EVAL expr Push result of "expr"
5457 * JUMP_IF_FALSE else
5458 * ... body ...
5459 * JUMP_ALWAYS end
5460 * else:
5461 * ... body ...
5462 * end:
5463 *
5464 * "if expr1 | elseif expr2 | else" Produces instructions:
5465 * EVAL expr Push result of "expr"
5466 * JUMP_IF_FALSE elseif
5467 * ... body ...
5468 * JUMP_ALWAYS end
5469 * elseif:
5470 * EVAL expr Push result of "expr"
5471 * JUMP_IF_FALSE else
5472 * ... body ...
5473 * JUMP_ALWAYS end
5474 * else:
5475 * ... body ...
5476 * end:
5477 */
5478 static char_u *
5479compile_if(char_u *arg, cctx_T *cctx)
5480{
5481 char_u *p = arg;
5482 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005483 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005484 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005485 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005486 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005487
Bram Moolenaara5565e42020-05-09 15:44:01 +02005488 CLEAR_FIELD(ppconst);
5489 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005490 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005491 clear_ppconst(&ppconst);
5492 return NULL;
5493 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005494 if (cctx->ctx_skip == SKIP_YES)
5495 clear_ppconst(&ppconst);
5496 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005497 {
5498 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005499 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005500 clear_ppconst(&ppconst);
5501 }
5502 else
5503 {
5504 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005505 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005506 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005507 return NULL;
5508 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005509
5510 scope = new_scope(cctx, IF_SCOPE);
5511 if (scope == NULL)
5512 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005513 scope->se_skip_save = skip_save;
5514 // "is_had_return" will be reset if any block does not end in :return
5515 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005516
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005517 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005518 {
5519 // "where" is set when ":elseif", "else" or ":endif" is found
5520 scope->se_u.se_if.is_if_label = instr->ga_len;
5521 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5522 }
5523 else
5524 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005525
5526 return p;
5527}
5528
5529 static char_u *
5530compile_elseif(char_u *arg, cctx_T *cctx)
5531{
5532 char_u *p = arg;
5533 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005534 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005535 isn_T *isn;
5536 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005537 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02005538 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005539
5540 if (scope == NULL || scope->se_type != IF_SCOPE)
5541 {
5542 emsg(_(e_elseif_without_if));
5543 return NULL;
5544 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005545 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005546 if (!cctx->ctx_had_return)
5547 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005548
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005549 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005550 {
5551 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005552 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005553 return NULL;
5554 // previous "if" or "elseif" jumps here
5555 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5556 isn->isn_arg.jump.jump_where = instr->ga_len;
5557 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005558
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005559 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005560 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02005561 if (cctx->ctx_skip == SKIP_YES)
5562 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005563 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005564 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005565 clear_ppconst(&ppconst);
5566 return NULL;
5567 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02005568 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005569 if (scope->se_skip_save == SKIP_YES)
5570 clear_ppconst(&ppconst);
5571 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005572 {
5573 // The expression results in a constant.
5574 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005575 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005576 clear_ppconst(&ppconst);
5577 scope->se_u.se_if.is_if_label = -1;
5578 }
5579 else
5580 {
5581 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005582 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005583 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005584 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005585
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005586 // "where" is set when ":elseif", "else" or ":endif" is found
5587 scope->se_u.se_if.is_if_label = instr->ga_len;
5588 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5589 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005590
5591 return p;
5592}
5593
5594 static char_u *
5595compile_else(char_u *arg, cctx_T *cctx)
5596{
5597 char_u *p = arg;
5598 garray_T *instr = &cctx->ctx_instr;
5599 isn_T *isn;
5600 scope_T *scope = cctx->ctx_scope;
5601
5602 if (scope == NULL || scope->se_type != IF_SCOPE)
5603 {
5604 emsg(_(e_else_without_if));
5605 return NULL;
5606 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005607 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005608 if (!cctx->ctx_had_return)
5609 scope->se_u.se_if.is_had_return = FALSE;
5610 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005611
Bram Moolenaarefd88552020-06-18 20:50:10 +02005612 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005613 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005614 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005615 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005616 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005617 if (!cctx->ctx_had_return
5618 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5619 JUMP_ALWAYS, cctx) == FAIL)
5620 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005621 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005622
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005623 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005624 {
5625 if (scope->se_u.se_if.is_if_label >= 0)
5626 {
5627 // previous "if" or "elseif" jumps here
5628 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5629 isn->isn_arg.jump.jump_where = instr->ga_len;
5630 scope->se_u.se_if.is_if_label = -1;
5631 }
5632 }
5633
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005634 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005635 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5636 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005637
5638 return p;
5639}
5640
5641 static char_u *
5642compile_endif(char_u *arg, cctx_T *cctx)
5643{
5644 scope_T *scope = cctx->ctx_scope;
5645 ifscope_T *ifscope;
5646 garray_T *instr = &cctx->ctx_instr;
5647 isn_T *isn;
5648
5649 if (scope == NULL || scope->se_type != IF_SCOPE)
5650 {
5651 emsg(_(e_endif_without_if));
5652 return NULL;
5653 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005654 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005655 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005656 if (!cctx->ctx_had_return)
5657 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005658
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005659 if (scope->se_u.se_if.is_if_label >= 0)
5660 {
5661 // previous "if" or "elseif" jumps here
5662 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5663 isn->isn_arg.jump.jump_where = instr->ga_len;
5664 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005665 // Fill in the "end" label in jumps at the end of the blocks.
5666 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005667 cctx->ctx_skip = scope->se_skip_save;
5668
5669 // If all the blocks end in :return and there is an :else then the
5670 // had_return flag is set.
5671 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005672
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005673 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005674 return arg;
5675}
5676
5677/*
5678 * compile "for var in expr"
5679 *
5680 * Produces instructions:
5681 * PUSHNR -1
5682 * STORE loop-idx Set index to -1
5683 * EVAL expr Push result of "expr"
5684 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5685 * - if beyond end, jump to "end"
5686 * - otherwise get item from list and push it
5687 * STORE var Store item in "var"
5688 * ... body ...
5689 * JUMP top Jump back to repeat
5690 * end: DROP Drop the result of "expr"
5691 *
5692 */
5693 static char_u *
5694compile_for(char_u *arg, cctx_T *cctx)
5695{
5696 char_u *p;
5697 size_t varlen;
5698 garray_T *instr = &cctx->ctx_instr;
5699 garray_T *stack = &cctx->ctx_type_stack;
5700 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005701 lvar_T *loop_lvar; // loop iteration variable
5702 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005703 type_T *vartype;
5704
5705 // TODO: list of variables: "for [key, value] in dict"
5706 // parse "var"
5707 for (p = arg; eval_isnamec1(*p); ++p)
5708 ;
5709 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005710 var_lvar = lookup_local(arg, varlen, cctx);
5711 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005712 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005713 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005714 return NULL;
5715 }
5716
5717 // consume "in"
5718 p = skipwhite(p);
5719 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5720 {
5721 emsg(_(e_missing_in));
5722 return NULL;
5723 }
5724 p = skipwhite(p + 2);
5725
5726
5727 scope = new_scope(cctx, FOR_SCOPE);
5728 if (scope == NULL)
5729 return NULL;
5730
5731 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005732 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5733 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005734 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005735 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005736 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005737 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005738 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005739
5740 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005741 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5742 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005743 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005744 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005745 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005746 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005747 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005748
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005749 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005750
5751 // compile "expr", it remains on the stack until "endfor"
5752 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005753 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005754 {
5755 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005756 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005757 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005758
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005759 // Now that we know the type of "var", check that it is a list, now or at
5760 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005761 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005762 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005763 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005764 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005765 return NULL;
5766 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005767 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005768 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005769
5770 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005771 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005772
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005773 generate_FOR(cctx, loop_lvar->lv_idx);
5774 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005775
5776 return arg;
5777}
5778
5779/*
5780 * compile "endfor"
5781 */
5782 static char_u *
5783compile_endfor(char_u *arg, cctx_T *cctx)
5784{
5785 garray_T *instr = &cctx->ctx_instr;
5786 scope_T *scope = cctx->ctx_scope;
5787 forscope_T *forscope;
5788 isn_T *isn;
5789
5790 if (scope == NULL || scope->se_type != FOR_SCOPE)
5791 {
5792 emsg(_(e_for));
5793 return NULL;
5794 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005795 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005796 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005797 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005798
5799 // At end of ":for" scope jump back to the FOR instruction.
5800 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5801
5802 // Fill in the "end" label in the FOR statement so it can jump here
5803 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5804 isn->isn_arg.forloop.for_end = instr->ga_len;
5805
5806 // Fill in the "end" label any BREAK statements
5807 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5808
5809 // Below the ":for" scope drop the "expr" list from the stack.
5810 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5811 return NULL;
5812
5813 vim_free(scope);
5814
5815 return arg;
5816}
5817
5818/*
5819 * compile "while expr"
5820 *
5821 * Produces instructions:
5822 * top: EVAL expr Push result of "expr"
5823 * JUMP_IF_FALSE end jump if false
5824 * ... body ...
5825 * JUMP top Jump back to repeat
5826 * end:
5827 *
5828 */
5829 static char_u *
5830compile_while(char_u *arg, cctx_T *cctx)
5831{
5832 char_u *p = arg;
5833 garray_T *instr = &cctx->ctx_instr;
5834 scope_T *scope;
5835
5836 scope = new_scope(cctx, WHILE_SCOPE);
5837 if (scope == NULL)
5838 return NULL;
5839
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005840 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005841
5842 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005843 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005844 return NULL;
5845
5846 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005847 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005848 JUMP_IF_FALSE, cctx) == FAIL)
5849 return FAIL;
5850
5851 return p;
5852}
5853
5854/*
5855 * compile "endwhile"
5856 */
5857 static char_u *
5858compile_endwhile(char_u *arg, cctx_T *cctx)
5859{
5860 scope_T *scope = cctx->ctx_scope;
5861
5862 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5863 {
5864 emsg(_(e_while));
5865 return NULL;
5866 }
5867 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005868 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005869
5870 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005871 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005872
5873 // Fill in the "end" label in the WHILE statement so it can jump here.
5874 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005875 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005876
5877 vim_free(scope);
5878
5879 return arg;
5880}
5881
5882/*
5883 * compile "continue"
5884 */
5885 static char_u *
5886compile_continue(char_u *arg, cctx_T *cctx)
5887{
5888 scope_T *scope = cctx->ctx_scope;
5889
5890 for (;;)
5891 {
5892 if (scope == NULL)
5893 {
5894 emsg(_(e_continue));
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 back to the FOR or WHILE instruction.
5903 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005904 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5905 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005906 return arg;
5907}
5908
5909/*
5910 * compile "break"
5911 */
5912 static char_u *
5913compile_break(char_u *arg, cctx_T *cctx)
5914{
5915 scope_T *scope = cctx->ctx_scope;
5916 endlabel_T **el;
5917
5918 for (;;)
5919 {
5920 if (scope == NULL)
5921 {
5922 emsg(_(e_break));
5923 return NULL;
5924 }
5925 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5926 break;
5927 scope = scope->se_outer;
5928 }
5929
5930 // Jump to the end of the FOR or WHILE loop.
5931 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005932 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005933 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005934 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005935 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5936 return FAIL;
5937
5938 return arg;
5939}
5940
5941/*
5942 * compile "{" start of block
5943 */
5944 static char_u *
5945compile_block(char_u *arg, cctx_T *cctx)
5946{
5947 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5948 return NULL;
5949 return skipwhite(arg + 1);
5950}
5951
5952/*
5953 * compile end of block: drop one scope
5954 */
5955 static void
5956compile_endblock(cctx_T *cctx)
5957{
5958 scope_T *scope = cctx->ctx_scope;
5959
5960 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005961 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005962 vim_free(scope);
5963}
5964
5965/*
5966 * compile "try"
5967 * Creates a new scope for the try-endtry, pointing to the first catch and
5968 * finally.
5969 * Creates another scope for the "try" block itself.
5970 * TRY instruction sets up exception handling at runtime.
5971 *
5972 * "try"
5973 * TRY -> catch1, -> finally push trystack entry
5974 * ... try block
5975 * "throw {exception}"
5976 * EVAL {exception}
5977 * THROW create exception
5978 * ... try block
5979 * " catch {expr}"
5980 * JUMP -> finally
5981 * catch1: PUSH exeception
5982 * EVAL {expr}
5983 * MATCH
5984 * JUMP nomatch -> catch2
5985 * CATCH remove exception
5986 * ... catch block
5987 * " catch"
5988 * JUMP -> finally
5989 * catch2: CATCH remove exception
5990 * ... catch block
5991 * " finally"
5992 * finally:
5993 * ... finally block
5994 * " endtry"
5995 * ENDTRY pop trystack entry, may rethrow
5996 */
5997 static char_u *
5998compile_try(char_u *arg, cctx_T *cctx)
5999{
6000 garray_T *instr = &cctx->ctx_instr;
6001 scope_T *try_scope;
6002 scope_T *scope;
6003
6004 // scope that holds the jumps that go to catch/finally/endtry
6005 try_scope = new_scope(cctx, TRY_SCOPE);
6006 if (try_scope == NULL)
6007 return NULL;
6008
6009 // "catch" is set when the first ":catch" is found.
6010 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006011 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006012 if (generate_instr(cctx, ISN_TRY) == NULL)
6013 return NULL;
6014
6015 // scope for the try block itself
6016 scope = new_scope(cctx, BLOCK_SCOPE);
6017 if (scope == NULL)
6018 return NULL;
6019
6020 return arg;
6021}
6022
6023/*
6024 * compile "catch {expr}"
6025 */
6026 static char_u *
6027compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6028{
6029 scope_T *scope = cctx->ctx_scope;
6030 garray_T *instr = &cctx->ctx_instr;
6031 char_u *p;
6032 isn_T *isn;
6033
6034 // end block scope from :try or :catch
6035 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6036 compile_endblock(cctx);
6037 scope = cctx->ctx_scope;
6038
6039 // Error if not in a :try scope
6040 if (scope == NULL || scope->se_type != TRY_SCOPE)
6041 {
6042 emsg(_(e_catch));
6043 return NULL;
6044 }
6045
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006046 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006047 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006048 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006049 return NULL;
6050 }
6051
6052 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006053 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006054 JUMP_ALWAYS, cctx) == FAIL)
6055 return NULL;
6056
6057 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006058 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006059 if (isn->isn_arg.try.try_catch == 0)
6060 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006061 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006062 {
6063 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006064 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006065 isn->isn_arg.jump.jump_where = instr->ga_len;
6066 }
6067
6068 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006069 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006070 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006071 scope->se_u.se_try.ts_caught_all = TRUE;
6072 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006073 }
6074 else
6075 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006076 char_u *end;
6077 char_u *pat;
6078 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006079 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006080 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006081
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006082 // Push v:exception, push {expr} and MATCH
6083 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6084
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006085 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006086 if (*end != *p)
6087 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006088 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006089 vim_free(tofree);
6090 return FAIL;
6091 }
6092 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006093 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006094 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006095 len = (int)(end - tofree);
6096 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006097 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006098 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006099 if (pat == NULL)
6100 return FAIL;
6101 if (generate_PUSHS(cctx, pat) == FAIL)
6102 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006104 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6105 return NULL;
6106
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006107 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006108 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6109 return NULL;
6110 }
6111
6112 if (generate_instr(cctx, ISN_CATCH) == NULL)
6113 return NULL;
6114
6115 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6116 return NULL;
6117 return p;
6118}
6119
6120 static char_u *
6121compile_finally(char_u *arg, cctx_T *cctx)
6122{
6123 scope_T *scope = cctx->ctx_scope;
6124 garray_T *instr = &cctx->ctx_instr;
6125 isn_T *isn;
6126
6127 // end block scope from :try or :catch
6128 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6129 compile_endblock(cctx);
6130 scope = cctx->ctx_scope;
6131
6132 // Error if not in a :try scope
6133 if (scope == NULL || scope->se_type != TRY_SCOPE)
6134 {
6135 emsg(_(e_finally));
6136 return NULL;
6137 }
6138
6139 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006140 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006141 if (isn->isn_arg.try.try_finally != 0)
6142 {
6143 emsg(_(e_finally_dup));
6144 return NULL;
6145 }
6146
6147 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006148 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006149
Bram Moolenaar585fea72020-04-02 22:33:21 +02006150 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006151 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006152 {
6153 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006154 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006155 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006156 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006157 }
6158
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006159 // TODO: set index in ts_finally_label jumps
6160
6161 return arg;
6162}
6163
6164 static char_u *
6165compile_endtry(char_u *arg, cctx_T *cctx)
6166{
6167 scope_T *scope = cctx->ctx_scope;
6168 garray_T *instr = &cctx->ctx_instr;
6169 isn_T *isn;
6170
6171 // end block scope from :catch or :finally
6172 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6173 compile_endblock(cctx);
6174 scope = cctx->ctx_scope;
6175
6176 // Error if not in a :try scope
6177 if (scope == NULL || scope->se_type != TRY_SCOPE)
6178 {
6179 if (scope == NULL)
6180 emsg(_(e_no_endtry));
6181 else if (scope->se_type == WHILE_SCOPE)
6182 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006183 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006184 emsg(_(e_endfor));
6185 else
6186 emsg(_(e_endif));
6187 return NULL;
6188 }
6189
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006190 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006191 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6192 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006193 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006194 return NULL;
6195 }
6196
6197 // Fill in the "end" label in jumps at the end of the blocks, if not done
6198 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006199 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006200
6201 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006202 if (isn->isn_arg.try.try_catch == 0)
6203 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006204 if (isn->isn_arg.try.try_finally == 0)
6205 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006206
6207 if (scope->se_u.se_try.ts_catch_label != 0)
6208 {
6209 // Last catch without match jumps here
6210 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6211 isn->isn_arg.jump.jump_where = instr->ga_len;
6212 }
6213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006214 compile_endblock(cctx);
6215
6216 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6217 return NULL;
6218 return arg;
6219}
6220
6221/*
6222 * compile "throw {expr}"
6223 */
6224 static char_u *
6225compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6226{
6227 char_u *p = skipwhite(arg);
6228
Bram Moolenaara5565e42020-05-09 15:44:01 +02006229 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006230 return NULL;
6231 if (may_generate_2STRING(-1, cctx) == FAIL)
6232 return NULL;
6233 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6234 return NULL;
6235
6236 return p;
6237}
6238
6239/*
6240 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006241 * compile "echomsg expr"
6242 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006243 * compile "execute expr"
6244 */
6245 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006246compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006247{
6248 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006249 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006250 int count = 0;
6251
6252 for (;;)
6253 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006254 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006255 return NULL;
6256 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006257 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006258 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006259 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006260 break;
6261 }
6262
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006263 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6264 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6265 else if (cmdidx == CMD_execute)
6266 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6267 else if (cmdidx == CMD_echomsg)
6268 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6269 else
6270 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006271 return p;
6272}
6273
6274/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006275 * A command that is not compiled, execute with legacy code.
6276 */
6277 static char_u *
6278compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6279{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006280 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006281 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006282 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006283
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006284 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006285 goto theend;
6286
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006287 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006288 {
6289 long argt = excmd_get_argt(eap->cmdidx);
6290 int usefilter = FALSE;
6291
6292 has_expr = argt & (EX_XFILE | EX_EXPAND);
6293
6294 // If the command can be followed by a bar, find the bar and truncate
6295 // it, so that the following command can be compiled.
6296 // The '|' is overwritten with a NUL, it is put back below.
6297 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6298 && *eap->arg == '!')
6299 // :w !filter or :r !filter or :r! filter
6300 usefilter = TRUE;
6301 if ((argt & EX_TRLBAR) && !usefilter)
6302 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006303 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006304 separate_nextcmd(eap);
6305 if (eap->nextcmd != NULL)
6306 nextcmd = eap->nextcmd;
6307 }
6308 }
6309
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006310 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6311 {
6312 // expand filename in "syntax include [@group] filename"
6313 has_expr = TRUE;
6314 eap->arg = skipwhite(eap->arg + 7);
6315 if (*eap->arg == '@')
6316 eap->arg = skiptowhite(eap->arg);
6317 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006318
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006319 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006320 {
6321 int count = 0;
6322 char_u *start = skipwhite(line);
6323
6324 // :cmd xxx`=expr1`yyy`=expr2`zzz
6325 // PUSHS ":cmd xxx"
6326 // eval expr1
6327 // PUSHS "yyy"
6328 // eval expr2
6329 // PUSHS "zzz"
6330 // EXECCONCAT 5
6331 for (;;)
6332 {
6333 if (p > start)
6334 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006335 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006336 ++count;
6337 }
6338 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006339 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006340 return NULL;
6341 may_generate_2STRING(-1, cctx);
6342 ++count;
6343 p = skipwhite(p);
6344 if (*p != '`')
6345 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006346 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006347 return NULL;
6348 }
6349 start = p + 1;
6350
6351 p = (char_u *)strstr((char *)start, "`=");
6352 if (p == NULL)
6353 {
6354 if (*skipwhite(start) != NUL)
6355 {
6356 generate_PUSHS(cctx, vim_strsave(start));
6357 ++count;
6358 }
6359 break;
6360 }
6361 }
6362 generate_EXECCONCAT(cctx, count);
6363 }
6364 else
6365 generate_EXEC(cctx, line);
6366
6367theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006368 if (*nextcmd != NUL)
6369 {
6370 // the parser expects a pointer to the bar, put it back
6371 --nextcmd;
6372 *nextcmd = '|';
6373 }
6374
6375 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006376}
6377
6378/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006379 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006380 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006381 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006382 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006383add_def_function(ufunc_T *ufunc)
6384{
6385 dfunc_T *dfunc;
6386
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006387 if (def_functions.ga_len == 0)
6388 {
6389 // The first position is not used, so that a zero uf_dfunc_idx means it
6390 // wasn't set.
6391 if (ga_grow(&def_functions, 1) == FAIL)
6392 return FAIL;
6393 ++def_functions.ga_len;
6394 }
6395
Bram Moolenaar09689a02020-05-09 22:50:08 +02006396 // Add the function to "def_functions".
6397 if (ga_grow(&def_functions, 1) == FAIL)
6398 return FAIL;
6399 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6400 CLEAR_POINTER(dfunc);
6401 dfunc->df_idx = def_functions.ga_len;
6402 ufunc->uf_dfunc_idx = dfunc->df_idx;
6403 dfunc->df_ufunc = ufunc;
6404 ++def_functions.ga_len;
6405 return OK;
6406}
6407
6408/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006409 * After ex_function() has collected all the function lines: parse and compile
6410 * the lines into instructions.
6411 * Adds the function to "def_functions".
6412 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6413 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006414 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006415 * This can be used recursively through compile_lambda(), which may reallocate
6416 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006417 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006418 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006419 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006420compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006421{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006422 char_u *line = NULL;
6423 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006424 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006425 cctx_T cctx;
6426 garray_T *instr;
6427 int called_emsg_before = called_emsg;
6428 int ret = FAIL;
6429 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006430 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006431 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006432 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006433
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006434 // When using a function that was compiled before: Free old instructions.
6435 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006436 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006437 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006438 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6439 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006440 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006441 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006442 else
6443 {
6444 if (add_def_function(ufunc) == FAIL)
6445 return FAIL;
6446 new_def_function = TRUE;
6447 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006448
Bram Moolenaar985116a2020-07-12 17:31:09 +02006449 ufunc->uf_def_status = UF_COMPILING;
6450
Bram Moolenaara80faa82020-04-12 19:37:17 +02006451 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006452 cctx.ctx_ufunc = ufunc;
6453 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006454 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006455 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6456 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6457 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6458 cctx.ctx_type_list = &ufunc->uf_type_list;
6459 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6460 instr = &cctx.ctx_instr;
6461
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006462 // Set the context to the function, it may be compiled when called from
6463 // another script. Set the script version to the most modern one.
6464 // The line number will be set in next_line_from_context().
6465 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006466 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6467
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006468 // Make sure error messages are OK.
6469 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6470 if (do_estack_push)
6471 estack_push_ufunc(ufunc, 1);
6472
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006473 if (ufunc->uf_def_args.ga_len > 0)
6474 {
6475 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006476 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006477 int i;
6478 char_u *arg;
6479 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006480 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006481
6482 // Produce instructions for the default values of optional arguments.
6483 // Store the instruction index in uf_def_arg_idx[] so that we know
6484 // where to start when the function is called, depending on the number
6485 // of arguments.
6486 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6487 if (ufunc->uf_def_arg_idx == NULL)
6488 goto erret;
6489 for (i = 0; i < count; ++i)
6490 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006491 garray_T *stack = &cctx.ctx_type_stack;
6492 type_T *val_type;
6493 int arg_idx = first_def_arg + i;
6494
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006495 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6496 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006497 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006498 goto erret;
6499
6500 // If no type specified use the type of the default value.
6501 // Otherwise check that the default value type matches the
6502 // specified type.
6503 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6504 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006505 {
6506 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006507 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006508 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02006509 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
6510 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006511 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006512
6513 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006514 goto erret;
6515 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006516 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006517
6518 if (did_set_arg_type)
6519 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006520 }
6521
6522 /*
6523 * Loop over all the lines of the function and generate instructions.
6524 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006525 for (;;)
6526 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006527 exarg_T ea;
6528 cmdmod_T save_cmdmod;
6529 int starts_with_colon = FALSE;
6530 char_u *cmd;
6531 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006532
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006533 // Bail out on the first error to avoid a flood of errors and report
6534 // the right line number when inside try/catch.
6535 if (emsg_before != called_emsg)
6536 goto erret;
6537
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006538 if (line != NULL && *line == '|')
6539 // the line continues after a '|'
6540 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006541 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006542 && !(*line == '#' && (line == cctx.ctx_line_start
6543 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006544 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006545 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006546 goto erret;
6547 }
6548 else
6549 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006550 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006551 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006552 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006553 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006554 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006555 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006556
Bram Moolenaara80faa82020-04-12 19:37:17 +02006557 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006558 ea.cmdlinep = &line;
6559 ea.cmd = skipwhite(line);
6560
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006561 // Some things can be recognized by the first character.
6562 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006563 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006564 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006565 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006566 if (ea.cmd[1] != '{')
6567 {
6568 line = (char_u *)"";
6569 continue;
6570 }
6571 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006572
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006573 case '}':
6574 {
6575 // "}" ends a block scope
6576 scopetype_T stype = cctx.ctx_scope == NULL
6577 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006578
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006579 if (stype == BLOCK_SCOPE)
6580 {
6581 compile_endblock(&cctx);
6582 line = ea.cmd;
6583 }
6584 else
6585 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006586 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006587 goto erret;
6588 }
6589 if (line != NULL)
6590 line = skipwhite(ea.cmd + 1);
6591 continue;
6592 }
6593
6594 case '{':
6595 // "{" starts a block scope
6596 // "{'a': 1}->func() is something else
6597 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6598 {
6599 line = compile_block(ea.cmd, &cctx);
6600 continue;
6601 }
6602 break;
6603
6604 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006605 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006606 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006607 }
6608
6609 /*
6610 * COMMAND MODIFIERS
6611 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006612 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006613 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6614 {
6615 if (errormsg != NULL)
6616 goto erret;
6617 // empty line or comment
6618 line = (char_u *)"";
6619 continue;
6620 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006621 // TODO: use modifiers in the command
6622 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006623 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006624
6625 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006626 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006627 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006628 {
6629 if (*ea.cmd == '(')
6630 // not for "call()"
6631 ea.cmd = p;
6632 else
6633 ea.cmd = skipwhite(ea.cmd);
6634 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006635
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006636 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006637 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006638 char_u *pskip;
6639
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006640 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006641 // find what follows.
6642 // Skip over "var.member", "var[idx]" and the like.
6643 // Also "&opt = val", "$ENV = val" and "@r = val".
6644 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006645 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006646 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006647 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006648 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006649 char_u *var_end;
6650 int oplen;
6651 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006652
Bram Moolenaar65821722020-08-02 18:58:54 +02006653 if (ea.cmd[0] == '@')
6654 var_end = ea.cmd + 2;
6655 else
6656 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006657 FNE_CHECK_START | FNE_INCL_BR);
6658 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006659 if (oplen > 0)
6660 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006661 size_t len = p - ea.cmd;
6662
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006663 // Recognize an assignment if we recognize the variable
6664 // name:
6665 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006666 // "local = expr" where "local" is a local var.
6667 // "script = expr" where "script" is a script-local var.
6668 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006669 // "&opt = expr"
6670 // "$ENV = expr"
6671 // "@r = expr"
6672 if (*ea.cmd == '&'
6673 || *ea.cmd == '$'
6674 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006675 || ((len) > 2 && ea.cmd[1] == ':')
6676 || lookup_local(ea.cmd, len, &cctx) != NULL
6677 || lookup_arg(ea.cmd, len, NULL, NULL,
6678 NULL, &cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02006679 || lookup_script(ea.cmd, len, FALSE) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006680 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006681 {
6682 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006683 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006684 goto erret;
6685 continue;
6686 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006687 }
6688 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006689
6690 if (*ea.cmd == '[')
6691 {
6692 // [var, var] = expr
6693 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6694 if (line == NULL)
6695 goto erret;
6696 if (line != ea.cmd)
6697 continue;
6698 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006699 }
6700
6701 /*
6702 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006703 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006704 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006705 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006706 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006707 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006708 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006709 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006710 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006711 if (!starts_with_colon)
6712 {
6713 emsg(_(e_colon_required_before_a_range));
6714 goto erret;
6715 }
6716 if (ends_excmd2(line, ea.cmd))
6717 {
6718 // A range without a command: jump to the line.
6719 // TODO: compile to a more efficient command, possibly
6720 // calling parse_cmd_address().
6721 ea.cmdidx = CMD_SIZE;
6722 line = compile_exec(line, &ea, &cctx);
6723 goto nextline;
6724 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006725 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006726 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006727 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006728 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006729 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006730
6731 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6732 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006733 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006734 {
6735 line += STRLEN(line);
6736 continue;
6737 }
6738
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006739 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006740 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006741 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006742 // CMD_let cannot happen, compile_assignment() above is used
6743 iemsg("Command from find_ex_command() not handled");
6744 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006745 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006746 }
6747
6748 p = skipwhite(p);
6749
Bram Moolenaar3988f642020-08-27 22:43:03 +02006750 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006751 && ea.cmdidx != CMD_elseif
6752 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02006753 && ea.cmdidx != CMD_endif
6754 && ea.cmdidx != CMD_endfor
6755 && ea.cmdidx != CMD_endwhile
6756 && ea.cmdidx != CMD_catch
6757 && ea.cmdidx != CMD_finally
6758 && ea.cmdidx != CMD_endtry)
6759 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02006760 emsg(_(e_unreachable_code_after_return));
6761 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006762 }
6763
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006764 switch (ea.cmdidx)
6765 {
6766 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006767 ea.arg = p;
6768 line = compile_nested_function(&ea, &cctx);
6769 break;
6770
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006771 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006772 // TODO: should we allow this, e.g. to declare a global
6773 // function?
6774 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006775 goto erret;
6776
6777 case CMD_return:
6778 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006779 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006780 break;
6781
6782 case CMD_let:
6783 case CMD_const:
6784 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006785 if (line == p)
6786 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006787 break;
6788
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006789 case CMD_unlet:
6790 case CMD_unlockvar:
6791 case CMD_lockvar:
6792 line = compile_unletlock(p, &ea, &cctx);
6793 break;
6794
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006795 case CMD_import:
6796 line = compile_import(p, &cctx);
6797 break;
6798
6799 case CMD_if:
6800 line = compile_if(p, &cctx);
6801 break;
6802 case CMD_elseif:
6803 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006804 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006805 break;
6806 case CMD_else:
6807 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006808 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006809 break;
6810 case CMD_endif:
6811 line = compile_endif(p, &cctx);
6812 break;
6813
6814 case CMD_while:
6815 line = compile_while(p, &cctx);
6816 break;
6817 case CMD_endwhile:
6818 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006819 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006820 break;
6821
6822 case CMD_for:
6823 line = compile_for(p, &cctx);
6824 break;
6825 case CMD_endfor:
6826 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006827 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006828 break;
6829 case CMD_continue:
6830 line = compile_continue(p, &cctx);
6831 break;
6832 case CMD_break:
6833 line = compile_break(p, &cctx);
6834 break;
6835
6836 case CMD_try:
6837 line = compile_try(p, &cctx);
6838 break;
6839 case CMD_catch:
6840 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006841 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006842 break;
6843 case CMD_finally:
6844 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006845 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006846 break;
6847 case CMD_endtry:
6848 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006849 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006850 break;
6851 case CMD_throw:
6852 line = compile_throw(p, &cctx);
6853 break;
6854
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006855 case CMD_eval:
6856 if (compile_expr0(&p, &cctx) == FAIL)
6857 goto erret;
6858
Bram Moolenaar3988f642020-08-27 22:43:03 +02006859 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006860 generate_instr_drop(&cctx, ISN_DROP, 1);
6861
6862 line = skipwhite(p);
6863 break;
6864
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006865 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006866 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006867 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006868 case CMD_echomsg:
6869 case CMD_echoerr:
6870 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006871 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006872
Bram Moolenaar3988f642020-08-27 22:43:03 +02006873 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006874
Bram Moolenaarae616492020-07-28 20:07:27 +02006875 case CMD_append:
6876 case CMD_change:
6877 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02006878 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02006879 case CMD_xit:
6880 not_in_vim9(&ea);
6881 goto erret;
6882
Bram Moolenaar002262f2020-07-08 17:47:57 +02006883 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02006884 if (cctx.ctx_skip != SKIP_YES)
6885 {
6886 semsg(_(e_invalid_command_str), ea.cmd);
6887 goto erret;
6888 }
6889 // We don't check for a next command here.
6890 line = (char_u *)"";
6891 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02006892
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006893 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02006894 if (cctx.ctx_skip == SKIP_YES)
6895 {
6896 // We don't check for a next command here.
6897 line = (char_u *)"";
6898 }
6899 else
6900 {
6901 // Not recognized, execute with do_cmdline_cmd().
6902 ea.arg = p;
6903 line = compile_exec(line, &ea, &cctx);
6904 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006905 break;
6906 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006907nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006908 if (line == NULL)
6909 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006910 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006911
6912 if (cctx.ctx_type_stack.ga_len < 0)
6913 {
6914 iemsg("Type stack underflow");
6915 goto erret;
6916 }
6917 }
6918
6919 if (cctx.ctx_scope != NULL)
6920 {
6921 if (cctx.ctx_scope->se_type == IF_SCOPE)
6922 emsg(_(e_endif));
6923 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6924 emsg(_(e_endwhile));
6925 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6926 emsg(_(e_endfor));
6927 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006928 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006929 goto erret;
6930 }
6931
Bram Moolenaarefd88552020-06-18 20:50:10 +02006932 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006933 {
6934 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6935 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006936 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006937 goto erret;
6938 }
6939
6940 // Return zero if there is no return at the end.
6941 generate_PUSHNR(&cctx, 0);
6942 generate_instr(&cctx, ISN_RETURN);
6943 }
6944
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006945 {
6946 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6947 + ufunc->uf_dfunc_idx;
6948 dfunc->df_deleted = FALSE;
6949 dfunc->df_instr = instr->ga_data;
6950 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006951 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006952 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006953 if (cctx.ctx_outer_used)
6954 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006955 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006956 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006957
6958 ret = OK;
6959
6960erret:
6961 if (ret == FAIL)
6962 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006963 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006964 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6965 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006966
6967 for (idx = 0; idx < instr->ga_len; ++idx)
6968 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006969 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006970
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006971 // If using the last entry in the table and it was added above, we
6972 // might as well remove it.
6973 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02006974 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006975 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006976 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006977 ufunc->uf_dfunc_idx = 0;
6978 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006979 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006980
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006981 while (cctx.ctx_scope != NULL)
6982 drop_scope(&cctx);
6983
Bram Moolenaar20431c92020-03-20 18:39:46 +01006984 // Don't execute this function body.
6985 ga_clear_strings(&ufunc->uf_lines);
6986
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006987 if (errormsg != NULL)
6988 emsg(errormsg);
6989 else if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006990 emsg(_(e_compile_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006991 }
6992
6993 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006994 if (do_estack_push)
6995 estack_pop();
6996
Bram Moolenaar20431c92020-03-20 18:39:46 +01006997 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006998 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006999 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007000 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007001}
7002
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007003 void
7004set_function_type(ufunc_T *ufunc)
7005{
7006 int varargs = ufunc->uf_va_name != NULL;
7007 int argcount = ufunc->uf_args.ga_len;
7008
7009 // Create a type for the function, with the return type and any
7010 // argument types.
7011 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7012 // The type is included in "tt_args".
7013 if (argcount > 0 || varargs)
7014 {
7015 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7016 argcount, &ufunc->uf_type_list);
7017 // Add argument types to the function type.
7018 if (func_type_add_arg_types(ufunc->uf_func_type,
7019 argcount + varargs,
7020 &ufunc->uf_type_list) == FAIL)
7021 return;
7022 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7023 ufunc->uf_func_type->tt_min_argcount =
7024 argcount - ufunc->uf_def_args.ga_len;
7025 if (ufunc->uf_arg_types == NULL)
7026 {
7027 int i;
7028
7029 // lambda does not have argument types.
7030 for (i = 0; i < argcount; ++i)
7031 ufunc->uf_func_type->tt_args[i] = &t_any;
7032 }
7033 else
7034 mch_memmove(ufunc->uf_func_type->tt_args,
7035 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7036 if (varargs)
7037 {
7038 ufunc->uf_func_type->tt_args[argcount] =
7039 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7040 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7041 }
7042 }
7043 else
7044 // No arguments, can use a predefined type.
7045 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7046 argcount, &ufunc->uf_type_list);
7047}
7048
7049
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007050/*
7051 * Delete an instruction, free what it contains.
7052 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007053 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007054delete_instr(isn_T *isn)
7055{
7056 switch (isn->isn_type)
7057 {
7058 case ISN_EXEC:
7059 case ISN_LOADENV:
7060 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007061 case ISN_LOADB:
7062 case ISN_LOADW:
7063 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007064 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007065 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007066 case ISN_PUSHEXC:
7067 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007068 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007069 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007070 case ISN_STOREB:
7071 case ISN_STOREW:
7072 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007073 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007074 vim_free(isn->isn_arg.string);
7075 break;
7076
7077 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007078 case ISN_STORES:
7079 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007080 break;
7081
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007082 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007083 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007084 vim_free(isn->isn_arg.unlet.ul_name);
7085 break;
7086
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007087 case ISN_STOREOPT:
7088 vim_free(isn->isn_arg.storeopt.so_name);
7089 break;
7090
7091 case ISN_PUSHBLOB: // push blob isn_arg.blob
7092 blob_unref(isn->isn_arg.blob);
7093 break;
7094
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007095 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007096#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007097 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007098#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007099 break;
7100
7101 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007102#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007103 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007104#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007105 break;
7106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007107 case ISN_UCALL:
7108 vim_free(isn->isn_arg.ufunc.cuf_name);
7109 break;
7110
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007111 case ISN_FUNCREF:
7112 {
7113 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7114 + isn->isn_arg.funcref.fr_func;
7115 func_ptr_unref(dfunc->df_ufunc);
7116 }
7117 break;
7118
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007119 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007120 {
7121 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7122 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7123
7124 if (ufunc != NULL)
7125 {
7126 // Clear uf_dfunc_idx so that the function is deleted.
7127 clear_def_function(ufunc);
7128 ufunc->uf_dfunc_idx = 0;
7129 func_ptr_unref(ufunc);
7130 }
7131
7132 vim_free(lambda);
7133 vim_free(isn->isn_arg.newfunc.nf_global);
7134 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007135 break;
7136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007137 case ISN_2BOOL:
7138 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007139 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007140 case ISN_ADDBLOB:
7141 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007142 case ISN_ANYINDEX:
7143 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007144 case ISN_BCALL:
7145 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007146 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007147 case ISN_CHECKNR:
7148 case ISN_CHECKTYPE:
7149 case ISN_COMPAREANY:
7150 case ISN_COMPAREBLOB:
7151 case ISN_COMPAREBOOL:
7152 case ISN_COMPAREDICT:
7153 case ISN_COMPAREFLOAT:
7154 case ISN_COMPAREFUNC:
7155 case ISN_COMPARELIST:
7156 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007157 case ISN_COMPARESPECIAL:
7158 case ISN_COMPARESTRING:
7159 case ISN_CONCAT:
7160 case ISN_DCALL:
7161 case ISN_DROP:
7162 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007163 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007164 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007165 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007166 case ISN_EXECCONCAT:
7167 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007168 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007169 case ISN_GETITEM:
7170 case ISN_JUMP:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007171 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007172 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007173 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007174 case ISN_LOADBDICT:
7175 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007176 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007177 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007178 case ISN_LOADSCRIPT:
7179 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007180 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007181 case ISN_LOADWDICT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007182 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007183 case ISN_NEGATENR:
7184 case ISN_NEWDICT:
7185 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007186 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007187 case ISN_OPFLOAT:
7188 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007189 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007190 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007191 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007192 case ISN_PUSHF:
7193 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007194 case ISN_PUSHSPEC:
7195 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007196 case ISN_SHUFFLE:
7197 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007198 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007199 case ISN_STOREDICT:
7200 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007201 case ISN_STORENR:
7202 case ISN_STOREOUTER:
7203 case ISN_STOREREG:
7204 case ISN_STORESCRIPT:
7205 case ISN_STOREV:
7206 case ISN_STRINDEX:
7207 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007208 case ISN_THROW:
7209 case ISN_TRY:
7210 // nothing allocated
7211 break;
7212 }
7213}
7214
7215/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007216 * Free all instructions for "dfunc".
7217 */
7218 static void
7219delete_def_function_contents(dfunc_T *dfunc)
7220{
7221 int idx;
7222
7223 ga_clear(&dfunc->df_def_args_isn);
7224
7225 if (dfunc->df_instr != NULL)
7226 {
7227 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7228 delete_instr(dfunc->df_instr + idx);
7229 VIM_CLEAR(dfunc->df_instr);
7230 }
7231
7232 dfunc->df_deleted = TRUE;
7233}
7234
7235/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007236 * When a user function is deleted, clear the contents of any associated def
7237 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007238 */
7239 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007240clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007241{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007242 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007243 {
7244 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7245 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007246
Bram Moolenaar20431c92020-03-20 18:39:46 +01007247 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007248 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007249 }
7250}
7251
7252#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007253/*
7254 * Free all functions defined with ":def".
7255 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007256 void
7257free_def_functions(void)
7258{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007259 int idx;
7260
7261 for (idx = 0; idx < def_functions.ga_len; ++idx)
7262 {
7263 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7264
7265 delete_def_function_contents(dfunc);
7266 }
7267
7268 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007269}
7270#endif
7271
7272
7273#endif // FEAT_EVAL