blob: 8d362ab1d09872ec89b70606ea187f51e2899ad2 [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 Moolenaar148ce7a2020-09-23 21:57:23 +0200129 int ctx_has_closure; // set to one if a closures was created in
130 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200131
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;
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200280 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100281 dictitem_T *di;
282
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200283 if (current_sctx.sc_sid <= 0)
284 return FAIL;
285 ht = &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar84367732020-08-23 15:21:55 +0200286 if (vim9script && !script_is_vim9())
Bram Moolenaar53900992020-08-22 19:02:02 +0200287 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100288 cc = name[len];
289 name[len] = NUL;
290 di = find_var_in_ht(ht, 0, name, TRUE);
291 name[len] = cc;
292 return di == NULL ? FAIL: OK;
293}
294
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100295/*
296 * Check if "p[len]" is already defined, either in script "import_sid" or in
297 * compilation context "cctx".
Bram Moolenaar0f769812020-09-12 18:32:34 +0200298 * Does not check the global namespace.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100299 * Return FAIL and give an error if it defined.
300 */
301 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200302check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100303{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200304 int c = p[len];
305 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200306
307 p[len] = NUL;
Bram Moolenaar53900992020-08-22 19:02:02 +0200308 if (lookup_script(p, len, FALSE) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100309 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200310 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaareef21022020-08-01 22:16:43 +0200311 || lookup_arg(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200312 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200313 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100314 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200315 // A local or script-local function can shadow a global function.
316 if (ufunc == NULL || !func_is_global(ufunc)
317 || (p[0] == 'g' && p[1] == ':'))
318 {
319 p[len] = c;
320 semsg(_(e_name_already_defined_str), p);
321 return FAIL;
322 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100323 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200324 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100325 return OK;
326}
327
Bram Moolenaar65b95452020-07-19 14:03:09 +0200328
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100329/////////////////////////////////////////////////////////////////////
330// Following generate_ functions expect the caller to call ga_grow().
331
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200332#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
333#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100334
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100335/*
336 * Generate an instruction without arguments.
337 * Returns a pointer to the new instruction, NULL if failed.
338 */
339 static isn_T *
340generate_instr(cctx_T *cctx, isntype_T isn_type)
341{
342 garray_T *instr = &cctx->ctx_instr;
343 isn_T *isn;
344
Bram Moolenaar080457c2020-03-03 21:53:32 +0100345 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100346 if (ga_grow(instr, 1) == FAIL)
347 return NULL;
348 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
349 isn->isn_type = isn_type;
350 isn->isn_lnum = cctx->ctx_lnum + 1;
351 ++instr->ga_len;
352
353 return isn;
354}
355
356/*
357 * Generate an instruction without arguments.
358 * "drop" will be removed from the stack.
359 * Returns a pointer to the new instruction, NULL if failed.
360 */
361 static isn_T *
362generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
363{
364 garray_T *stack = &cctx->ctx_type_stack;
365
Bram Moolenaar080457c2020-03-03 21:53:32 +0100366 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100367 stack->ga_len -= drop;
368 return generate_instr(cctx, isn_type);
369}
370
371/*
372 * Generate instruction "isn_type" and put "type" on the type stack.
373 */
374 static isn_T *
375generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
376{
377 isn_T *isn;
378 garray_T *stack = &cctx->ctx_type_stack;
379
380 if ((isn = generate_instr(cctx, isn_type)) == NULL)
381 return NULL;
382
383 if (ga_grow(stack, 1) == FAIL)
384 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200385 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100386 ++stack->ga_len;
387
388 return isn;
389}
390
391/*
392 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200393 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100394 */
395 static int
396may_generate_2STRING(int offset, cctx_T *cctx)
397{
398 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200399 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100400 garray_T *stack = &cctx->ctx_type_stack;
401 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
402
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200403 switch ((*type)->tt_type)
404 {
405 // nothing to be done
406 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100407
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200408 // conversion possible
409 case VAR_SPECIAL:
410 case VAR_BOOL:
411 case VAR_NUMBER:
412 case VAR_FLOAT:
413 break;
414
415 // conversion possible (with runtime check)
416 case VAR_ANY:
417 case VAR_UNKNOWN:
418 isntype = ISN_2STRING_ANY;
419 break;
420
421 // conversion not possible
422 case VAR_VOID:
423 case VAR_BLOB:
424 case VAR_FUNC:
425 case VAR_PARTIAL:
426 case VAR_LIST:
427 case VAR_DICT:
428 case VAR_JOB:
429 case VAR_CHANNEL:
430 to_string_error((*type)->tt_type);
431 return FAIL;
432 }
433
434 *type = &t_string;
435 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100436 return FAIL;
437 isn->isn_arg.number = offset;
438
439 return OK;
440}
441
442 static int
443check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
444{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200445 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100446 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200447 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100448 {
449 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200450 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100451 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200452 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100453 return FAIL;
454 }
455 return OK;
456}
457
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200458 static int
459generate_add_instr(
460 cctx_T *cctx,
461 vartype_T vartype,
462 type_T *type1,
463 type_T *type2)
464{
465 isn_T *isn = generate_instr_drop(cctx,
466 vartype == VAR_NUMBER ? ISN_OPNR
467 : vartype == VAR_LIST ? ISN_ADDLIST
468 : vartype == VAR_BLOB ? ISN_ADDBLOB
469#ifdef FEAT_FLOAT
470 : vartype == VAR_FLOAT ? ISN_OPFLOAT
471#endif
472 : ISN_OPANY, 1);
473
474 if (vartype != VAR_LIST && vartype != VAR_BLOB
475 && type1->tt_type != VAR_ANY
476 && type2->tt_type != VAR_ANY
477 && check_number_or_float(
478 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
479 return FAIL;
480
481 if (isn != NULL)
482 isn->isn_arg.op.op_type = EXPR_ADD;
483 return isn == NULL ? FAIL : OK;
484}
485
486/*
487 * Get the type to use for an instruction for an operation on "type1" and
488 * "type2". If they are matching use a type-specific instruction. Otherwise
489 * fall back to runtime type checking.
490 */
491 static vartype_T
492operator_type(type_T *type1, type_T *type2)
493{
494 if (type1->tt_type == type2->tt_type
495 && (type1->tt_type == VAR_NUMBER
496 || type1->tt_type == VAR_LIST
497#ifdef FEAT_FLOAT
498 || type1->tt_type == VAR_FLOAT
499#endif
500 || type1->tt_type == VAR_BLOB))
501 return type1->tt_type;
502 return VAR_ANY;
503}
504
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100505/*
506 * Generate an instruction with two arguments. The instruction depends on the
507 * type of the arguments.
508 */
509 static int
510generate_two_op(cctx_T *cctx, char_u *op)
511{
512 garray_T *stack = &cctx->ctx_type_stack;
513 type_T *type1;
514 type_T *type2;
515 vartype_T vartype;
516 isn_T *isn;
517
Bram Moolenaar080457c2020-03-03 21:53:32 +0100518 RETURN_OK_IF_SKIP(cctx);
519
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200520 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
522 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200523 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524
525 switch (*op)
526 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200527 case '+':
528 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530 break;
531
532 case '-':
533 case '*':
534 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
535 op) == FAIL)
536 return FAIL;
537 if (vartype == VAR_NUMBER)
538 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
539#ifdef FEAT_FLOAT
540 else if (vartype == VAR_FLOAT)
541 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
542#endif
543 else
544 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
545 if (isn != NULL)
546 isn->isn_arg.op.op_type = *op == '*'
547 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
548 break;
549
Bram Moolenaar4c683752020-04-05 21:38:23 +0200550 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200552 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100553 && type2->tt_type != VAR_NUMBER))
554 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200555 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100556 return FAIL;
557 }
558 isn = generate_instr_drop(cctx,
559 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
560 if (isn != NULL)
561 isn->isn_arg.op.op_type = EXPR_REM;
562 break;
563 }
564
565 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200566 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100567 {
568 type_T *type = &t_any;
569
570#ifdef FEAT_FLOAT
571 // float+number and number+float results in float
572 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
573 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
574 type = &t_float;
575#endif
576 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
577 }
578
579 return OK;
580}
581
582/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200583 * Get the instruction to use for comparing "type1" with "type2"
584 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100585 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200586 static isntype_T
587get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100588{
589 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100590
Bram Moolenaar4c683752020-04-05 21:38:23 +0200591 if (type1 == VAR_UNKNOWN)
592 type1 = VAR_ANY;
593 if (type2 == VAR_UNKNOWN)
594 type2 = VAR_ANY;
595
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100596 if (type1 == type2)
597 {
598 switch (type1)
599 {
600 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
601 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
602 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
603 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
604 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
605 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
606 case VAR_LIST: isntype = ISN_COMPARELIST; break;
607 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
608 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100609 default: isntype = ISN_COMPAREANY; break;
610 }
611 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200612 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100613 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
614 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
615 isntype = ISN_COMPAREANY;
616
617 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
618 && (isntype == ISN_COMPAREBOOL
619 || isntype == ISN_COMPARESPECIAL
620 || isntype == ISN_COMPARENR
621 || isntype == ISN_COMPAREFLOAT))
622 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200623 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200625 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100626 }
627 if (isntype == ISN_DROP
628 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
629 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
630 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
631 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
632 && exptype != EXPR_IS && exptype != EXPR_ISNOT
633 && (type1 == VAR_BLOB || type2 == VAR_BLOB
634 || type1 == VAR_LIST || type2 == VAR_LIST))))
635 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200636 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100637 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200638 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100639 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200640 return isntype;
641}
642
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200643 int
644check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
645{
646 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
647 return FAIL;
648 return OK;
649}
650
Bram Moolenaara5565e42020-05-09 15:44:01 +0200651/*
652 * Generate an ISN_COMPARE* instruction with a boolean result.
653 */
654 static int
655generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
656{
657 isntype_T isntype;
658 isn_T *isn;
659 garray_T *stack = &cctx->ctx_type_stack;
660 vartype_T type1;
661 vartype_T type2;
662
663 RETURN_OK_IF_SKIP(cctx);
664
665 // Get the known type of the two items on the stack. If they are matching
666 // use a type-specific instruction. Otherwise fall back to runtime type
667 // checking.
668 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
669 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
670 isntype = get_compare_isn(exptype, type1, type2);
671 if (isntype == ISN_DROP)
672 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673
674 if ((isn = generate_instr(cctx, isntype)) == NULL)
675 return FAIL;
676 isn->isn_arg.op.op_type = exptype;
677 isn->isn_arg.op.op_ic = ic;
678
679 // takes two arguments, puts one bool back
680 if (stack->ga_len >= 2)
681 {
682 --stack->ga_len;
683 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
684 }
685
686 return OK;
687}
688
689/*
690 * Generate an ISN_2BOOL instruction.
691 */
692 static int
693generate_2BOOL(cctx_T *cctx, int invert)
694{
695 isn_T *isn;
696 garray_T *stack = &cctx->ctx_type_stack;
697
Bram Moolenaar080457c2020-03-03 21:53:32 +0100698 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100699 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
700 return FAIL;
701 isn->isn_arg.number = invert;
702
703 // type becomes bool
704 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
705
706 return OK;
707}
708
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200709/*
710 * Generate an ISN_COND2BOOL instruction.
711 */
712 static int
713generate_COND2BOOL(cctx_T *cctx)
714{
715 isn_T *isn;
716 garray_T *stack = &cctx->ctx_type_stack;
717
718 RETURN_OK_IF_SKIP(cctx);
719 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
720 return FAIL;
721
722 // type becomes bool
723 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
724
725 return OK;
726}
727
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200729generate_TYPECHECK(
730 cctx_T *cctx,
731 type_T *expected,
732 int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100733{
734 isn_T *isn;
735 garray_T *stack = &cctx->ctx_type_stack;
736
Bram Moolenaar080457c2020-03-03 21:53:32 +0100737 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100738 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
739 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200740 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100741 isn->isn_arg.type.ct_off = offset;
742
Bram Moolenaar5e654232020-09-16 15:22:00 +0200743 // type becomes expected
744 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100745
746 return OK;
747}
748
749/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200750 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200751 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200752 * - "actual" is a type that can be "expected" type: add a runtime check; or
753 * - return FAIL.
754 */
755 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200756need_type(
757 type_T *actual,
758 type_T *expected,
759 int offset,
760 cctx_T *cctx,
761 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200762{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200763 if (expected == &t_bool && actual != &t_bool
764 && (actual->tt_flags & TTFLAG_BOOL_OK))
765 {
766 // Using "0", "1" or the result of an expression with "&&" or "||" as a
767 // boolean is OK but requires a conversion.
768 generate_2BOOL(cctx, FALSE);
769 return OK;
770 }
771
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200772 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200773 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200774
775 // If the actual type can be the expected type add a runtime check.
776 // TODO: if it's a constant a runtime check makes no sense.
777 if (actual->tt_type == VAR_ANY
778 || actual->tt_type == VAR_UNKNOWN
779 || (actual->tt_type == VAR_FUNC
780 && (expected->tt_type == VAR_FUNC
781 || expected->tt_type == VAR_PARTIAL)
782 && (actual->tt_member == &t_any || actual->tt_argcount < 0))
783 || (actual->tt_type == VAR_LIST
784 && expected->tt_type == VAR_LIST
785 && actual->tt_member == &t_any)
786 || (actual->tt_type == VAR_DICT
787 && expected->tt_type == VAR_DICT
788 && actual->tt_member == &t_any))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200789 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200790 generate_TYPECHECK(cctx, expected, offset);
791 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200792 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200793
794 if (!silent)
795 type_mismatch(expected, actual);
796 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200797}
798
799/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100800 * Generate an ISN_PUSHNR instruction.
801 */
802 static int
803generate_PUSHNR(cctx_T *cctx, varnumber_T number)
804{
805 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200806 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100807
Bram Moolenaar080457c2020-03-03 21:53:32 +0100808 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100809 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
810 return FAIL;
811 isn->isn_arg.number = number;
812
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200813 if (number == 0 || number == 1)
814 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200815 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200816
817 // A 0 or 1 number can also be used as a bool.
818 if (type != NULL)
819 {
820 type->tt_type = VAR_NUMBER;
821 type->tt_flags = TTFLAG_BOOL_OK;
822 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
823 }
824 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100825 return OK;
826}
827
828/*
829 * Generate an ISN_PUSHBOOL instruction.
830 */
831 static int
832generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
833{
834 isn_T *isn;
835
Bram Moolenaar080457c2020-03-03 21:53:32 +0100836 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
838 return FAIL;
839 isn->isn_arg.number = number;
840
841 return OK;
842}
843
844/*
845 * Generate an ISN_PUSHSPEC instruction.
846 */
847 static int
848generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
849{
850 isn_T *isn;
851
Bram Moolenaar080457c2020-03-03 21:53:32 +0100852 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
854 return FAIL;
855 isn->isn_arg.number = number;
856
857 return OK;
858}
859
860#ifdef FEAT_FLOAT
861/*
862 * Generate an ISN_PUSHF instruction.
863 */
864 static int
865generate_PUSHF(cctx_T *cctx, float_T fnumber)
866{
867 isn_T *isn;
868
Bram Moolenaar080457c2020-03-03 21:53:32 +0100869 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100870 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
871 return FAIL;
872 isn->isn_arg.fnumber = fnumber;
873
874 return OK;
875}
876#endif
877
878/*
879 * Generate an ISN_PUSHS instruction.
880 * Consumes "str".
881 */
882 static int
883generate_PUSHS(cctx_T *cctx, char_u *str)
884{
885 isn_T *isn;
886
Bram Moolenaar080457c2020-03-03 21:53:32 +0100887 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100888 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
889 return FAIL;
890 isn->isn_arg.string = str;
891
892 return OK;
893}
894
895/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100896 * Generate an ISN_PUSHCHANNEL instruction.
897 * Consumes "channel".
898 */
899 static int
900generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
901{
902 isn_T *isn;
903
Bram Moolenaar080457c2020-03-03 21:53:32 +0100904 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100905 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
906 return FAIL;
907 isn->isn_arg.channel = channel;
908
909 return OK;
910}
911
912/*
913 * Generate an ISN_PUSHJOB instruction.
914 * Consumes "job".
915 */
916 static int
917generate_PUSHJOB(cctx_T *cctx, job_T *job)
918{
919 isn_T *isn;
920
Bram Moolenaar080457c2020-03-03 21:53:32 +0100921 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100922 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100923 return FAIL;
924 isn->isn_arg.job = job;
925
926 return OK;
927}
928
929/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930 * Generate an ISN_PUSHBLOB instruction.
931 * Consumes "blob".
932 */
933 static int
934generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
935{
936 isn_T *isn;
937
Bram Moolenaar080457c2020-03-03 21:53:32 +0100938 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100939 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
940 return FAIL;
941 isn->isn_arg.blob = blob;
942
943 return OK;
944}
945
946/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100947 * Generate an ISN_PUSHFUNC instruction with name "name".
948 * Consumes "name".
949 */
950 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200951generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100952{
953 isn_T *isn;
954
Bram Moolenaar080457c2020-03-03 21:53:32 +0100955 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200956 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100957 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200958 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100959
960 return OK;
961}
962
963/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200964 * Generate an ISN_GETITEM instruction with "index".
965 */
966 static int
967generate_GETITEM(cctx_T *cctx, int index)
968{
969 isn_T *isn;
970 garray_T *stack = &cctx->ctx_type_stack;
971 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
972 type_T *item_type = &t_any;
973
974 RETURN_OK_IF_SKIP(cctx);
975
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200976 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200977 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200978 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200979 emsg(_(e_listreq));
980 return FAIL;
981 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200982 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200983 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
984 return FAIL;
985 isn->isn_arg.number = index;
986
987 // add the item type to the type stack
988 if (ga_grow(stack, 1) == FAIL)
989 return FAIL;
990 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
991 ++stack->ga_len;
992 return OK;
993}
994
995/*
Bram Moolenaar9af78762020-06-16 11:34:42 +0200996 * Generate an ISN_SLICE instruction with "count".
997 */
998 static int
999generate_SLICE(cctx_T *cctx, int count)
1000{
1001 isn_T *isn;
1002
1003 RETURN_OK_IF_SKIP(cctx);
1004 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1005 return FAIL;
1006 isn->isn_arg.number = count;
1007 return OK;
1008}
1009
1010/*
1011 * Generate an ISN_CHECKLEN instruction with "min_len".
1012 */
1013 static int
1014generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1015{
1016 isn_T *isn;
1017
1018 RETURN_OK_IF_SKIP(cctx);
1019
1020 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1021 return FAIL;
1022 isn->isn_arg.checklen.cl_min_len = min_len;
1023 isn->isn_arg.checklen.cl_more_OK = more_OK;
1024
1025 return OK;
1026}
1027
1028/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001029 * Generate an ISN_STORE instruction.
1030 */
1031 static int
1032generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1033{
1034 isn_T *isn;
1035
Bram Moolenaar080457c2020-03-03 21:53:32 +01001036 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001037 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1038 return FAIL;
1039 if (name != NULL)
1040 isn->isn_arg.string = vim_strsave(name);
1041 else
1042 isn->isn_arg.number = idx;
1043
1044 return OK;
1045}
1046
1047/*
1048 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1049 */
1050 static int
1051generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1052{
1053 isn_T *isn;
1054
Bram Moolenaar080457c2020-03-03 21:53:32 +01001055 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1057 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001058 isn->isn_arg.storenr.stnr_idx = idx;
1059 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001060
1061 return OK;
1062}
1063
1064/*
1065 * Generate an ISN_STOREOPT instruction
1066 */
1067 static int
1068generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1069{
1070 isn_T *isn;
1071
Bram Moolenaar080457c2020-03-03 21:53:32 +01001072 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001073 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001074 return FAIL;
1075 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1076 isn->isn_arg.storeopt.so_flags = opt_flags;
1077
1078 return OK;
1079}
1080
1081/*
1082 * Generate an ISN_LOAD or similar instruction.
1083 */
1084 static int
1085generate_LOAD(
1086 cctx_T *cctx,
1087 isntype_T isn_type,
1088 int idx,
1089 char_u *name,
1090 type_T *type)
1091{
1092 isn_T *isn;
1093
Bram Moolenaar080457c2020-03-03 21:53:32 +01001094 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001095 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1096 return FAIL;
1097 if (name != NULL)
1098 isn->isn_arg.string = vim_strsave(name);
1099 else
1100 isn->isn_arg.number = idx;
1101
1102 return OK;
1103}
1104
1105/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001106 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001107 */
1108 static int
1109generate_LOADV(
1110 cctx_T *cctx,
1111 char_u *name,
1112 int error)
1113{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001114 int di_flags;
1115 int vidx = find_vim_var(name, &di_flags);
1116 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001117
Bram Moolenaar080457c2020-03-03 21:53:32 +01001118 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001119 if (vidx < 0)
1120 {
1121 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001122 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001123 return FAIL;
1124 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001125 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001126
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001127 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001128}
1129
1130/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001131 * Generate an ISN_UNLET instruction.
1132 */
1133 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001134generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001135{
1136 isn_T *isn;
1137
1138 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001139 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001140 return FAIL;
1141 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1142 isn->isn_arg.unlet.ul_forceit = forceit;
1143
1144 return OK;
1145}
1146
1147/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001148 * Generate an ISN_LOCKCONST instruction.
1149 */
1150 static int
1151generate_LOCKCONST(cctx_T *cctx)
1152{
1153 isn_T *isn;
1154
1155 RETURN_OK_IF_SKIP(cctx);
1156 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1157 return FAIL;
1158 return OK;
1159}
1160
1161/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162 * Generate an ISN_LOADS instruction.
1163 */
1164 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001165generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001166 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001167 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001168 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001169 int sid,
1170 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171{
1172 isn_T *isn;
1173
Bram Moolenaar080457c2020-03-03 21:53:32 +01001174 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001175 if (isn_type == ISN_LOADS)
1176 isn = generate_instr_type(cctx, isn_type, type);
1177 else
1178 isn = generate_instr_drop(cctx, isn_type, 1);
1179 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001180 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001181 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1182 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001183
1184 return OK;
1185}
1186
1187/*
1188 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1189 */
1190 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001191generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001192 cctx_T *cctx,
1193 isntype_T isn_type,
1194 int sid,
1195 int idx,
1196 type_T *type)
1197{
1198 isn_T *isn;
1199
Bram Moolenaar080457c2020-03-03 21:53:32 +01001200 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001201 if (isn_type == ISN_LOADSCRIPT)
1202 isn = generate_instr_type(cctx, isn_type, type);
1203 else
1204 isn = generate_instr_drop(cctx, isn_type, 1);
1205 if (isn == NULL)
1206 return FAIL;
1207 isn->isn_arg.script.script_sid = sid;
1208 isn->isn_arg.script.script_idx = idx;
1209 return OK;
1210}
1211
1212/*
1213 * Generate an ISN_NEWLIST instruction.
1214 */
1215 static int
1216generate_NEWLIST(cctx_T *cctx, int count)
1217{
1218 isn_T *isn;
1219 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220 type_T *type;
1221 type_T *member;
1222
Bram Moolenaar080457c2020-03-03 21:53:32 +01001223 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001224 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1225 return FAIL;
1226 isn->isn_arg.number = count;
1227
Bram Moolenaar127542b2020-08-09 17:22:04 +02001228 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001229 if (count == 0)
1230 member = &t_void;
1231 else
1232 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001233 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1234 cctx->ctx_type_list);
1235 type = get_list_type(member, cctx->ctx_type_list);
1236
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001237 // drop the value types
1238 stack->ga_len -= count;
1239
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240 // add the list type to the type stack
1241 if (ga_grow(stack, 1) == FAIL)
1242 return FAIL;
1243 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1244 ++stack->ga_len;
1245
1246 return OK;
1247}
1248
1249/*
1250 * Generate an ISN_NEWDICT instruction.
1251 */
1252 static int
1253generate_NEWDICT(cctx_T *cctx, int count)
1254{
1255 isn_T *isn;
1256 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257 type_T *type;
1258 type_T *member;
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_NEWDICT)) == NULL)
1262 return FAIL;
1263 isn->isn_arg.number = count;
1264
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001265 if (count == 0)
1266 member = &t_void;
1267 else
1268 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001269 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1270 cctx->ctx_type_list);
1271 type = get_dict_type(member, cctx->ctx_type_list);
1272
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 // drop the key and value types
1274 stack->ga_len -= 2 * count;
1275
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276 // add the dict type to the type stack
1277 if (ga_grow(stack, 1) == FAIL)
1278 return FAIL;
1279 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1280 ++stack->ga_len;
1281
1282 return OK;
1283}
1284
1285/*
1286 * Generate an ISN_FUNCREF instruction.
1287 */
1288 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001289generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001290{
1291 isn_T *isn;
1292 garray_T *stack = &cctx->ctx_type_stack;
1293
Bram Moolenaar080457c2020-03-03 21:53:32 +01001294 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001295 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1296 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001297 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001298 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001299
1300 if (ga_grow(stack, 1) == FAIL)
1301 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001302 ((type_T **)stack->ga_data)[stack->ga_len] =
1303 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001304 ++stack->ga_len;
1305
1306 return OK;
1307}
1308
1309/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001310 * Generate an ISN_NEWFUNC instruction.
1311 */
1312 static int
1313generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1314{
1315 isn_T *isn;
1316 char_u *name;
1317
1318 RETURN_OK_IF_SKIP(cctx);
1319 name = vim_strsave(lambda_name);
1320 if (name == NULL)
1321 return FAIL;
1322 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1323 return FAIL;
1324 isn->isn_arg.newfunc.nf_lambda = name;
1325 isn->isn_arg.newfunc.nf_global = func_name;
1326
1327 return OK;
1328}
1329
1330/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331 * Generate an ISN_JUMP instruction.
1332 */
1333 static int
1334generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1335{
1336 isn_T *isn;
1337 garray_T *stack = &cctx->ctx_type_stack;
1338
Bram Moolenaar080457c2020-03-03 21:53:32 +01001339 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1341 return FAIL;
1342 isn->isn_arg.jump.jump_when = when;
1343 isn->isn_arg.jump.jump_where = where;
1344
1345 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1346 --stack->ga_len;
1347
1348 return OK;
1349}
1350
1351 static int
1352generate_FOR(cctx_T *cctx, int loop_idx)
1353{
1354 isn_T *isn;
1355 garray_T *stack = &cctx->ctx_type_stack;
1356
Bram Moolenaar080457c2020-03-03 21:53:32 +01001357 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001358 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1359 return FAIL;
1360 isn->isn_arg.forloop.for_idx = loop_idx;
1361
1362 if (ga_grow(stack, 1) == FAIL)
1363 return FAIL;
1364 // type doesn't matter, will be stored next
1365 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1366 ++stack->ga_len;
1367
1368 return OK;
1369}
1370
1371/*
1372 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001373 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001374 * Return FAIL if the number of arguments is wrong.
1375 */
1376 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001377generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378{
1379 isn_T *isn;
1380 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001381 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001382 type_T *argtypes[MAX_FUNC_ARGS];
1383 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001384
Bram Moolenaar080457c2020-03-03 21:53:32 +01001385 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001386 argoff = check_internal_func(func_idx, argcount);
1387 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001388 return FAIL;
1389
Bram Moolenaar389df252020-07-09 21:20:47 +02001390 if (method_call && argoff > 1)
1391 {
1392 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1393 return FAIL;
1394 isn->isn_arg.shuffle.shfl_item = argcount;
1395 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1396 }
1397
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001398 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1399 return FAIL;
1400 isn->isn_arg.bfunc.cbf_idx = func_idx;
1401 isn->isn_arg.bfunc.cbf_argcount = argcount;
1402
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001403 for (i = 0; i < argcount; ++i)
1404 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1405
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406 stack->ga_len -= argcount; // drop the arguments
1407 if (ga_grow(stack, 1) == FAIL)
1408 return FAIL;
1409 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001410 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411 ++stack->ga_len; // add return value
1412
1413 return OK;
1414}
1415
1416/*
1417 * Generate an ISN_DCALL or ISN_UCALL instruction.
1418 * Return FAIL if the number of arguments is wrong.
1419 */
1420 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001421generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001422{
1423 isn_T *isn;
1424 garray_T *stack = &cctx->ctx_type_stack;
1425 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001426 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001427
Bram Moolenaar080457c2020-03-03 21:53:32 +01001428 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429 if (argcount > regular_args && !has_varargs(ufunc))
1430 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001431 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432 return FAIL;
1433 }
1434 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1435 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001436 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001437 return FAIL;
1438 }
1439
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001440 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001441 {
1442 int i;
1443
1444 for (i = 0; i < argcount; ++i)
1445 {
1446 type_T *expected;
1447 type_T *actual;
1448
1449 if (i < regular_args)
1450 {
1451 if (ufunc->uf_arg_types == NULL)
1452 continue;
1453 expected = ufunc->uf_arg_types[i];
1454 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001455 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1456 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001457 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001458 else
1459 expected = ufunc->uf_va_type->tt_member;
1460 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001461 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001462 {
1463 arg_type_mismatch(expected, actual, i + 1);
1464 return FAIL;
1465 }
1466 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001467 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001468 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1469 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001470 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001471 }
1472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001474 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001475 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001476 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001477 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001478 {
1479 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1480 isn->isn_arg.dfunc.cdf_argcount = argcount;
1481 }
1482 else
1483 {
1484 // A user function may be deleted and redefined later, can't use the
1485 // ufunc pointer, need to look it up again at runtime.
1486 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1487 isn->isn_arg.ufunc.cuf_argcount = argcount;
1488 }
1489
1490 stack->ga_len -= argcount; // drop the arguments
1491 if (ga_grow(stack, 1) == FAIL)
1492 return FAIL;
1493 // add return value
1494 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1495 ++stack->ga_len;
1496
1497 return OK;
1498}
1499
1500/*
1501 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1502 */
1503 static int
1504generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1505{
1506 isn_T *isn;
1507 garray_T *stack = &cctx->ctx_type_stack;
1508
Bram Moolenaar080457c2020-03-03 21:53:32 +01001509 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1511 return FAIL;
1512 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1513 isn->isn_arg.ufunc.cuf_argcount = argcount;
1514
1515 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001516 if (ga_grow(stack, 1) == FAIL)
1517 return FAIL;
1518 // add return value
1519 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1520 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001521
1522 return OK;
1523}
1524
1525/*
1526 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001527 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528 */
1529 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001530generate_PCALL(
1531 cctx_T *cctx,
1532 int argcount,
1533 char_u *name,
1534 type_T *type,
1535 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536{
1537 isn_T *isn;
1538 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001539 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001540
Bram Moolenaar080457c2020-03-03 21:53:32 +01001541 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001542
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001543 if (type->tt_type == VAR_ANY)
1544 ret_type = &t_any;
1545 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001546 {
1547 if (type->tt_argcount != -1)
1548 {
1549 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1550
1551 if (argcount < type->tt_min_argcount - varargs)
1552 {
1553 semsg(_(e_toofewarg), "[reference]");
1554 return FAIL;
1555 }
1556 if (!varargs && argcount > type->tt_argcount)
1557 {
1558 semsg(_(e_toomanyarg), "[reference]");
1559 return FAIL;
1560 }
1561 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001562 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001563 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001564 else
1565 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001566 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001567 return FAIL;
1568 }
1569
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001570 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1571 return FAIL;
1572 isn->isn_arg.pfunc.cpf_top = at_top;
1573 isn->isn_arg.pfunc.cpf_argcount = argcount;
1574
1575 stack->ga_len -= argcount; // drop the arguments
1576
1577 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001578 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001579
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001580 // If partial is above the arguments it must be cleared and replaced with
1581 // the return value.
1582 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1583 return FAIL;
1584
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585 return OK;
1586}
1587
1588/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001589 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590 */
1591 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001592generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001593{
1594 isn_T *isn;
1595 garray_T *stack = &cctx->ctx_type_stack;
1596 type_T *type;
1597
Bram Moolenaar080457c2020-03-03 21:53:32 +01001598 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001599 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001601 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001602
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001603 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001605 if (type->tt_type != VAR_DICT && type != &t_any)
1606 {
1607 emsg(_(e_dictreq));
1608 return FAIL;
1609 }
1610 // change dict type to dict member type
1611 if (type->tt_type == VAR_DICT)
1612 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613
1614 return OK;
1615}
1616
1617/*
1618 * Generate an ISN_ECHO instruction.
1619 */
1620 static int
1621generate_ECHO(cctx_T *cctx, int with_white, int count)
1622{
1623 isn_T *isn;
1624
Bram Moolenaar080457c2020-03-03 21:53:32 +01001625 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001626 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1627 return FAIL;
1628 isn->isn_arg.echo.echo_with_white = with_white;
1629 isn->isn_arg.echo.echo_count = count;
1630
1631 return OK;
1632}
1633
Bram Moolenaarad39c092020-02-26 18:23:43 +01001634/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001635 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001636 */
1637 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001638generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001639{
1640 isn_T *isn;
1641
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001642 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001643 return FAIL;
1644 isn->isn_arg.number = count;
1645
1646 return OK;
1647}
1648
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001649/*
1650 * Generate an ISN_PUT instruction.
1651 */
1652 static int
1653generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1654{
1655 isn_T *isn;
1656
1657 RETURN_OK_IF_SKIP(cctx);
1658 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1659 return FAIL;
1660 isn->isn_arg.put.put_regname = regname;
1661 isn->isn_arg.put.put_lnum = lnum;
1662 return OK;
1663}
1664
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665 static int
1666generate_EXEC(cctx_T *cctx, char_u *line)
1667{
1668 isn_T *isn;
1669
Bram Moolenaar080457c2020-03-03 21:53:32 +01001670 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1672 return FAIL;
1673 isn->isn_arg.string = vim_strsave(line);
1674 return OK;
1675}
1676
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001677 static int
1678generate_EXECCONCAT(cctx_T *cctx, int count)
1679{
1680 isn_T *isn;
1681
1682 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1683 return FAIL;
1684 isn->isn_arg.number = count;
1685 return OK;
1686}
1687
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001688/*
1689 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001690 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001692 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001693reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1694{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695 lvar_T *lvar;
1696
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001697 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001698 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001699 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001700 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001701 }
1702
1703 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001704 return NULL;
1705 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001706
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001707 // Every local variable uses the next entry on the stack. We could re-use
1708 // the last ones when leaving a scope, but then variables used in a closure
1709 // might get overwritten. To keep things simple do not re-use stack
1710 // entries. This is less efficient, but memory is cheap these days.
1711 lvar->lv_idx = cctx->ctx_locals_count++;
1712
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001713 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001714 lvar->lv_const = isConst;
1715 lvar->lv_type = type;
1716
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001717 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001718}
1719
1720/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001721 * Remove local variables above "new_top".
1722 */
1723 static void
1724unwind_locals(cctx_T *cctx, int new_top)
1725{
1726 if (cctx->ctx_locals.ga_len > new_top)
1727 {
1728 int idx;
1729 lvar_T *lvar;
1730
1731 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1732 {
1733 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1734 vim_free(lvar->lv_name);
1735 }
1736 }
1737 cctx->ctx_locals.ga_len = new_top;
1738}
1739
1740/*
1741 * Free all local variables.
1742 */
1743 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001744free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001745{
1746 unwind_locals(cctx, 0);
1747 ga_clear(&cctx->ctx_locals);
1748}
1749
1750/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751 * Find "name" in script-local items of script "sid".
1752 * Returns the index in "sn_var_vals" if found.
1753 * If found but not in "sn_var_vals" returns -1.
1754 * If not found returns -2.
1755 */
1756 int
1757get_script_item_idx(int sid, char_u *name, int check_writable)
1758{
1759 hashtab_T *ht;
1760 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001761 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762 int idx;
1763
1764 // First look the name up in the hashtable.
Bram Moolenaare3d46852020-08-29 13:39:17 +02001765 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001766 return -1;
1767 ht = &SCRIPT_VARS(sid);
1768 di = find_var_in_ht(ht, 0, name, TRUE);
1769 if (di == NULL)
1770 return -2;
1771
1772 // Now find the svar_T index in sn_var_vals.
1773 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1774 {
1775 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1776
1777 if (sv->sv_tv == &di->di_tv)
1778 {
1779 if (check_writable && sv->sv_const)
1780 semsg(_(e_readonlyvar), name);
1781 return idx;
1782 }
1783 }
1784 return -1;
1785}
1786
1787/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001788 * Find "name" in imported items of the current script or in "cctx" if not
1789 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001790 */
1791 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001792find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001794 int idx;
1795
Bram Moolenaare3d46852020-08-29 13:39:17 +02001796 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001797 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001798 if (cctx != NULL)
1799 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1800 {
1801 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1802 + idx;
1803
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001804 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1805 : STRLEN(import->imp_name) == len
1806 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807 return import;
1808 }
1809
Bram Moolenaarefa94442020-08-08 22:16:00 +02001810 return find_imported_in_script(name, len, current_sctx.sc_sid);
1811}
1812
1813 imported_T *
1814find_imported_in_script(char_u *name, size_t len, int sid)
1815{
Bram Moolenaare3d46852020-08-29 13:39:17 +02001816 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001817 int idx;
1818
Bram Moolenaare3d46852020-08-29 13:39:17 +02001819 if (!SCRIPT_ID_VALID(sid))
1820 return NULL;
1821 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1823 {
1824 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1825
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001826 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1827 : STRLEN(import->imp_name) == len
1828 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829 return import;
1830 }
1831 return NULL;
1832}
1833
1834/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001835 * Free all imported variables.
1836 */
1837 static void
1838free_imported(cctx_T *cctx)
1839{
1840 int idx;
1841
1842 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1843 {
1844 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1845
1846 vim_free(import->imp_name);
1847 }
1848 ga_clear(&cctx->ctx_imports);
1849}
1850
1851/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02001852 * Return TRUE if "p" points at a "#" but not at "#{".
1853 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001854 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001855vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001856{
1857 return p[0] == '#' && p[1] != '{';
1858}
1859
1860/*
1861 * Return a pointer to the next line that isn't empty or only contains a
1862 * comment. Skips over white space.
1863 * Returns NULL if there is none.
1864 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001865 char_u *
1866peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001867{
1868 int lnum = cctx->ctx_lnum;
1869
1870 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
1871 {
1872 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001873 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001874
Bram Moolenaarba60cc42020-08-12 19:15:33 +02001875 // ignore NULLs inserted for continuation lines
1876 if (line != NULL)
1877 {
1878 p = skipwhite(line);
1879 if (*p != NUL && !vim9_comment_start(p))
1880 return p;
1881 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02001882 }
1883 return NULL;
1884}
1885
1886/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001887 * Called when checking for a following operator at "arg". When the rest of
1888 * the line is empty or only a comment, peek the next line. If there is a next
1889 * line return a pointer to it and set "nextp".
1890 * Otherwise skip over white space.
1891 */
1892 static char_u *
1893may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
1894{
1895 char_u *p = skipwhite(arg);
1896
1897 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001898 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001899 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001900 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001901 if (*nextp != NULL)
1902 return *nextp;
1903 }
1904 return p;
1905}
1906
1907/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02001908 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02001909 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02001910 * Returns NULL when at the end.
1911 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001912 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02001913next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02001914{
Bram Moolenaar7a092242020-04-16 22:10:49 +02001915 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001916
1917 do
1918 {
1919 ++cctx->ctx_lnum;
1920 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02001921 {
1922 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001923 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02001924 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02001925 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02001926 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001927 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001928 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02001929 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02001930 return line;
1931}
1932
1933/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001934 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02001935 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001936 * Return FAIL if beyond the last line, "*arg" is unmodified then.
1937 */
1938 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02001939may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001940{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001941 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001942 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02001943 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001944
1945 if (next == NULL)
1946 return FAIL;
1947 *arg = skipwhite(next);
1948 }
1949 return OK;
1950}
1951
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001952/*
1953 * Idem, and give an error when failed.
1954 */
1955 static int
1956may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
1957{
1958 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1959 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001960 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001961 return FAIL;
1962 }
1963 return OK;
1964}
1965
1966
Bram Moolenaara5565e42020-05-09 15:44:01 +02001967// Structure passed between the compile_expr* functions to keep track of
1968// constants that have been parsed but for which no code was produced yet. If
1969// possible expressions on these constants are applied at compile time. If
1970// that is not possible, the code to push the constants needs to be generated
1971// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02001972// Using 50 should be more than enough of 5 levels of ().
1973#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02001974typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02001975 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02001976 int pp_used; // active entries in pp_tv[]
1977} ppconst_T;
1978
Bram Moolenaar1c747212020-05-09 18:28:34 +02001979static int compile_expr0(char_u **arg, cctx_T *cctx);
1980static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1981
Bram Moolenaara5565e42020-05-09 15:44:01 +02001982/*
1983 * Generate a PUSH instruction for "tv".
1984 * "tv" will be consumed or cleared.
1985 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
1986 */
1987 static int
1988generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
1989{
1990 if (tv != NULL)
1991 {
1992 switch (tv->v_type)
1993 {
1994 case VAR_UNKNOWN:
1995 break;
1996 case VAR_BOOL:
1997 generate_PUSHBOOL(cctx, tv->vval.v_number);
1998 break;
1999 case VAR_SPECIAL:
2000 generate_PUSHSPEC(cctx, tv->vval.v_number);
2001 break;
2002 case VAR_NUMBER:
2003 generate_PUSHNR(cctx, tv->vval.v_number);
2004 break;
2005#ifdef FEAT_FLOAT
2006 case VAR_FLOAT:
2007 generate_PUSHF(cctx, tv->vval.v_float);
2008 break;
2009#endif
2010 case VAR_BLOB:
2011 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2012 tv->vval.v_blob = NULL;
2013 break;
2014 case VAR_STRING:
2015 generate_PUSHS(cctx, tv->vval.v_string);
2016 tv->vval.v_string = NULL;
2017 break;
2018 default:
2019 iemsg("constant type not supported");
2020 clear_tv(tv);
2021 return FAIL;
2022 }
2023 tv->v_type = VAR_UNKNOWN;
2024 }
2025 return OK;
2026}
2027
2028/*
2029 * Generate code for any ppconst entries.
2030 */
2031 static int
2032generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2033{
2034 int i;
2035 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002036 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002037
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002038 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002039 for (i = 0; i < ppconst->pp_used; ++i)
2040 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2041 ret = FAIL;
2042 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002043 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002044 return ret;
2045}
2046
2047/*
2048 * Clear ppconst constants. Used when failing.
2049 */
2050 static void
2051clear_ppconst(ppconst_T *ppconst)
2052{
2053 int i;
2054
2055 for (i = 0; i < ppconst->pp_used; ++i)
2056 clear_tv(&ppconst->pp_tv[i]);
2057 ppconst->pp_used = 0;
2058}
2059
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002060/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002061 * Generate an instruction to load script-local variable "name", without the
2062 * leading "s:".
2063 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064 */
2065 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002066compile_load_scriptvar(
2067 cctx_T *cctx,
2068 char_u *name, // variable NUL terminated
2069 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002070 char_u **end, // end of variable
2071 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002073 scriptitem_T *si;
2074 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002075 imported_T *import;
2076
Bram Moolenaare3d46852020-08-29 13:39:17 +02002077 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2078 return FAIL;
2079 si = SCRIPT_ITEM(current_sctx.sc_sid);
2080 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002081 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002082 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002083 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002084 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2085 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002086 }
2087 if (idx >= 0)
2088 {
2089 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2090
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002091 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002092 current_sctx.sc_sid, idx, sv->sv_type);
2093 return OK;
2094 }
2095
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002096 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002097 if (import != NULL)
2098 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002099 if (import->imp_all)
2100 {
2101 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002102 char_u *exp_name;
2103 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002104 ufunc_T *ufunc;
2105 type_T *type;
2106
2107 // Used "import * as Name", need to lookup the member.
2108 if (*p != '.')
2109 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002110 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002111 return FAIL;
2112 }
2113 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002114 if (VIM_ISWHITE(*p))
2115 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002116 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002117 return FAIL;
2118 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002119
Bram Moolenaar1c991142020-07-04 13:15:31 +02002120 // isolate one name
2121 exp_name = p;
2122 while (eval_isnamec(*p))
2123 ++p;
2124 cc = *p;
2125 *p = NUL;
2126
2127 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2128 *p = cc;
2129 p = skipwhite(p);
2130
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002131 // TODO: what if it is a function?
2132 if (idx < 0)
2133 return FAIL;
2134 *end = p;
2135
2136 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2137 import->imp_sid,
2138 idx,
2139 type);
2140 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002141 else if (import->imp_funcname != NULL)
2142 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002143 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002144 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2145 import->imp_sid,
2146 import->imp_var_vals_idx,
2147 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002148 return OK;
2149 }
2150
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002151 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002152 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002153 return FAIL;
2154}
2155
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002156 static int
2157generate_funcref(cctx_T *cctx, char_u *name)
2158{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002159 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002160
2161 if (ufunc == NULL)
2162 return FAIL;
2163
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002164 // Need to compile any default values to get the argument types.
2165 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2166 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2167 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002168 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002169}
2170
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002171/*
2172 * Compile a variable name into a load instruction.
2173 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002174 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175 * When "error" is FALSE do not give an error when not found.
2176 */
2177 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002178compile_load(
2179 char_u **arg,
2180 char_u *end_arg,
2181 cctx_T *cctx,
2182 int is_expr,
2183 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002184{
2185 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002186 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002187 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002188 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002189 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002190
2191 if (*(*arg + 1) == ':')
2192 {
2193 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002194 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002195 {
2196 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002197
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002198 switch (**arg)
2199 {
2200 case 'g': isn_type = ISN_LOADGDICT; break;
2201 case 'w': isn_type = ISN_LOADWDICT; break;
2202 case 't': isn_type = ISN_LOADTDICT; break;
2203 case 'b': isn_type = ISN_LOADBDICT; break;
2204 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002205 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002206 goto theend;
2207 }
2208 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2209 goto theend;
2210 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002211 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212 else
2213 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002214 isntype_T isn_type = ISN_DROP;
2215
2216 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2217 if (name == NULL)
2218 return FAIL;
2219
2220 switch (**arg)
2221 {
2222 case 'v': res = generate_LOADV(cctx, name, error);
2223 break;
2224 case 's': res = compile_load_scriptvar(cctx, name,
2225 NULL, NULL, error);
2226 break;
2227 case 'g': isn_type = ISN_LOADG; break;
2228 case 'w': isn_type = ISN_LOADW; break;
2229 case 't': isn_type = ISN_LOADT; break;
2230 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002231 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002232 goto theend;
2233 }
2234 if (isn_type != ISN_DROP)
2235 {
2236 // Global, Buffer-local, Window-local and Tabpage-local
2237 // variables can be defined later, thus we don't check if it
2238 // exists, give error at runtime.
2239 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2240 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002241 }
2242 }
2243 else
2244 {
2245 size_t len = end - *arg;
2246 int idx;
2247 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002248 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002249
2250 name = vim_strnsave(*arg, end - *arg);
2251 if (name == NULL)
2252 return FAIL;
2253
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002254 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002255 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002256 if (!gen_load_outer)
2257 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258 }
2259 else
2260 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002261 lvar_T *lvar = lookup_local(*arg, len, cctx);
2262
2263 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002265 type = lvar->lv_type;
2266 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002267 if (lvar->lv_from_outer)
2268 gen_load_outer = TRUE;
2269 else
2270 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271 }
2272 else
2273 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002274 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002275 // already exists in a Vim9 script or when it's imported.
2276 if (lookup_script(*arg, len, TRUE) == OK
2277 || find_imported(name, 0, cctx) != NULL)
2278 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002279
Bram Moolenaar0f769812020-09-12 18:32:34 +02002280 // When evaluating an expression and the name starts with an
2281 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002282 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002283 if (res == FAIL && is_expr
2284 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002285 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002286 }
2287 }
2288 if (gen_load)
2289 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002290 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002291 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002292 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002293 cctx->ctx_outer_used = TRUE;
2294 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002295 }
2296
2297 *arg = end;
2298
2299theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002300 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002301 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002302 vim_free(name);
2303 return res;
2304}
2305
2306/*
2307 * Compile the argument expressions.
2308 * "arg" points to just after the "(" and is advanced to after the ")"
2309 */
2310 static int
2311compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2312{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002313 char_u *p = *arg;
2314 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002315 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316
Bram Moolenaare6085c52020-04-12 20:19:16 +02002317 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002318 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002319 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2320 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002321 if (*p == ')')
2322 {
2323 *arg = p + 1;
2324 return OK;
2325 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002326 if (must_end)
2327 {
2328 semsg(_(e_missing_comma_before_argument_str), p);
2329 return FAIL;
2330 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002331
Bram Moolenaara5565e42020-05-09 15:44:01 +02002332 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333 return FAIL;
2334 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002335
2336 if (*p != ',' && *skipwhite(p) == ',')
2337 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002338 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002339 p = skipwhite(p);
2340 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002341 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002342 {
2343 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002344 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002345 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002346 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002347 else
2348 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002349 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002350 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002351 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002352failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002353 emsg(_(e_missing_close));
2354 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002355}
2356
2357/*
2358 * Compile a function call: name(arg1, arg2)
2359 * "arg" points to "name", "arg + varlen" to the "(".
2360 * "argcount_init" is 1 for "value->method()"
2361 * Instructions:
2362 * EVAL arg1
2363 * EVAL arg2
2364 * BCALL / DCALL / UCALL
2365 */
2366 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002367compile_call(
2368 char_u **arg,
2369 size_t varlen,
2370 cctx_T *cctx,
2371 ppconst_T *ppconst,
2372 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002373{
2374 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002375 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002376 int argcount = argcount_init;
2377 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002378 char_u fname_buf[FLEN_FIXED + 1];
2379 char_u *tofree = NULL;
2380 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002381 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002382 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002383 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384
Bram Moolenaara5565e42020-05-09 15:44:01 +02002385 // we can evaluate "has('name')" at compile time
2386 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2387 {
2388 char_u *s = skipwhite(*arg + varlen + 1);
2389 typval_T argvars[2];
2390
2391 argvars[0].v_type = VAR_UNKNOWN;
2392 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002393 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002394 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002395 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002396 s = skipwhite(s);
2397 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2398 {
2399 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2400
2401 *arg = s + 1;
2402 argvars[1].v_type = VAR_UNKNOWN;
2403 tv->v_type = VAR_NUMBER;
2404 tv->vval.v_number = 0;
2405 f_has(argvars, tv);
2406 clear_tv(&argvars[0]);
2407 ++ppconst->pp_used;
2408 return OK;
2409 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002410 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002411 }
2412
2413 if (generate_ppconst(cctx, ppconst) == FAIL)
2414 return FAIL;
2415
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 if (varlen >= sizeof(namebuf))
2417 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002418 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002419 return FAIL;
2420 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002421 vim_strncpy(namebuf, *arg, varlen);
2422 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423
2424 *arg = skipwhite(*arg + varlen + 1);
2425 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002426 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002427
Bram Moolenaara1773442020-08-12 15:21:22 +02002428 is_autoload = vim_strchr(name, '#') != NULL;
2429 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002430 {
2431 int idx;
2432
2433 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002434 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002436 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002437 else
2438 semsg(_(e_unknownfunc), namebuf);
2439 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002440 }
2441
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442 // If we can find the function by name generate the right call.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002443 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002444 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar0f769812020-09-12 18:32:34 +02002445 if (ufunc != NULL && !func_is_global(ufunc))
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002446 {
2447 res = generate_CALL(cctx, ufunc, argcount);
2448 goto theend;
2449 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002450
2451 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002452 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002453 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002454 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002455 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002456 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002457 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002458 garray_T *stack = &cctx->ctx_type_stack;
2459 type_T *type;
2460
2461 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2462 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002463 goto theend;
2464 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002465
Bram Moolenaar0f769812020-09-12 18:32:34 +02002466 // If we can find a global function by name generate the right call.
2467 if (ufunc != NULL)
2468 {
2469 res = generate_CALL(cctx, ufunc, argcount);
2470 goto theend;
2471 }
2472
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002473 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002474 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002475 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002476 res = generate_UCALL(cctx, name, argcount);
2477 else
2478 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002479
2480theend:
2481 vim_free(tofree);
2482 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483}
2484
2485// like NAMESPACE_CHAR but with 'a' and 'l'.
2486#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2487
2488/*
2489 * Find the end of a variable or function name. Unlike find_name_end() this
2490 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002491 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 * Return a pointer to just after the name. Equal to "arg" if there is no
2493 * valid name.
2494 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002495 static char_u *
2496to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497{
2498 char_u *p;
2499
2500 // Quick check for valid starting character.
2501 if (!eval_isnamec1(*arg))
2502 return arg;
2503
2504 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2505 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2506 // and can be used in slice "[n:]".
2507 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002508 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2510 break;
2511 return p;
2512}
2513
2514/*
2515 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002516 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 */
2518 char_u *
2519to_name_const_end(char_u *arg)
2520{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002521 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522 typval_T rettv;
2523
2524 if (p == arg && *arg == '[')
2525 {
2526
2527 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002528 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 p = arg;
2530 }
2531 else if (p == arg && *arg == '#' && arg[1] == '{')
2532 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002533 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002535 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536 p = arg;
2537 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538
2539 return p;
2540}
2541
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542/*
2543 * parse a list: [expr, expr]
2544 * "*arg" points to the '['.
2545 */
2546 static int
2547compile_list(char_u **arg, cctx_T *cctx)
2548{
2549 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002550 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002551 int count = 0;
2552
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002553 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002555 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002556 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002557 semsg(_(e_list_end), *arg);
2558 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002559 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002560 if (*p == ',')
2561 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002562 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002563 return FAIL;
2564 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002565 if (*p == ']')
2566 {
2567 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002568 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002569 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002570 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002571 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 ++count;
2573 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002574 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002576 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2577 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002578 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002579 return FAIL;
2580 }
2581 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002582 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002583 p = skipwhite(p);
2584 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002585 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586
2587 generate_NEWLIST(cctx, count);
2588 return OK;
2589}
2590
2591/*
2592 * parse a lambda: {arg, arg -> expr}
2593 * "*arg" points to the '{'.
2594 */
2595 static int
2596compile_lambda(char_u **arg, cctx_T *cctx)
2597{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002598 typval_T rettv;
2599 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002600 evalarg_T evalarg;
2601
2602 CLEAR_FIELD(evalarg);
2603 evalarg.eval_flags = EVAL_EVALUATE;
2604 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605
2606 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002607 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002608 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002609
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002610 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002611 ++ufunc->uf_refcount;
2612 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002613 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002614
2615 // The function will have one line: "return {expr}".
2616 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002617 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002618
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002619 clear_evalarg(&evalarg, NULL);
2620
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002621 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002622 {
2623 // The return type will now be known.
2624 set_function_type(ufunc);
2625
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002626 // The function reference count will be 1. When the ISN_FUNCREF
2627 // instruction is deleted the reference count is decremented and the
2628 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002629 return generate_FUNCREF(cctx, ufunc);
2630 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002631
2632 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633 return FAIL;
2634}
2635
2636/*
2637 * Compile a lamda call: expr->{lambda}(args)
2638 * "arg" points to the "{".
2639 */
2640 static int
2641compile_lambda_call(char_u **arg, cctx_T *cctx)
2642{
2643 ufunc_T *ufunc;
2644 typval_T rettv;
2645 int argcount = 1;
2646 int ret = FAIL;
2647
2648 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002649 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002650 return FAIL;
2651
2652 if (**arg != '(')
2653 {
2654 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002655 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002656 else
2657 semsg(_(e_missing_paren), "lambda");
2658 clear_tv(&rettv);
2659 return FAIL;
2660 }
2661
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002662 ufunc = rettv.vval.v_partial->pt_func;
2663 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002664 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002665 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002666
Bram Moolenaara05e5242020-09-19 18:19:19 +02002667 // The function will have one line: "return {expr}". Compile it into
2668 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002669 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002670
2671 // compile the arguments
2672 *arg = skipwhite(*arg + 1);
2673 if (compile_arguments(arg, cctx, &argcount) == OK)
2674 // call the compiled function
2675 ret = generate_CALL(cctx, ufunc, argcount);
2676
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002677 if (ret == FAIL)
2678 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002679 return ret;
2680}
2681
2682/*
2683 * parse a dict: {'key': val} or #{key: val}
2684 * "*arg" points to the '{'.
2685 */
2686 static int
2687compile_dict(char_u **arg, cctx_T *cctx, int literal)
2688{
2689 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002690 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002691 int count = 0;
2692 dict_T *d = dict_alloc();
2693 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002694 char_u *whitep = *arg;
2695 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002696
2697 if (d == NULL)
2698 return FAIL;
2699 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002700 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002701 {
2702 char_u *key = NULL;
2703
Bram Moolenaar23c55272020-06-21 16:58:13 +02002704 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002705 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002706 *arg = NULL;
2707 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002708 }
2709
2710 if (**arg == '}')
2711 break;
2712
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713 if (literal)
2714 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002715 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716
Bram Moolenaar2c330432020-04-13 14:41:35 +02002717 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002718 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002719 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002720 return FAIL;
2721 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002722 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002723 if (generate_PUSHS(cctx, key) == FAIL)
2724 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002725 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002726 }
2727 else
2728 {
2729 isn_T *isn;
2730
Bram Moolenaara5565e42020-05-09 15:44:01 +02002731 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002732 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2734 if (isn->isn_type == ISN_PUSHS)
2735 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002736 else
2737 {
2738 type_T *keytype = ((type_T **)stack->ga_data)
2739 [stack->ga_len - 1];
2740 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2741 return FAIL;
2742 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002743 }
2744
2745 // Check for duplicate keys, if using string keys.
2746 if (key != NULL)
2747 {
2748 item = dict_find(d, key, -1);
2749 if (item != NULL)
2750 {
2751 semsg(_(e_duplicate_key), key);
2752 goto failret;
2753 }
2754 item = dictitem_alloc(key);
2755 if (item != NULL)
2756 {
2757 item->di_tv.v_type = VAR_UNKNOWN;
2758 item->di_tv.v_lock = 0;
2759 if (dict_add(d, item) == FAIL)
2760 dictitem_free(item);
2761 }
2762 }
2763
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764 if (**arg != ':')
2765 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002766 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002767 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002768 else
2769 semsg(_(e_missing_dict_colon), *arg);
2770 return FAIL;
2771 }
2772 whitep = *arg + 1;
2773 if (!IS_WHITE_OR_NUL(*whitep))
2774 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002775 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776 return FAIL;
2777 }
2778
2779 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002780 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002781 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002782 *arg = NULL;
2783 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002784 }
2785
Bram Moolenaara5565e42020-05-09 15:44:01 +02002786 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 return FAIL;
2788 ++count;
2789
Bram Moolenaar2c330432020-04-13 14:41:35 +02002790 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002791 *arg = skipwhite(*arg);
2792 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002793 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002794 *arg = NULL;
2795 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002796 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797 if (**arg == '}')
2798 break;
2799 if (**arg != ',')
2800 {
2801 semsg(_(e_missing_dict_comma), *arg);
2802 goto failret;
2803 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002804 if (IS_WHITE_OR_NUL(*whitep))
2805 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002806 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002807 return FAIL;
2808 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002809 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002810 *arg = skipwhite(*arg + 1);
2811 }
2812
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 *arg = *arg + 1;
2814
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002815 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002816 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002817 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002818 *arg += STRLEN(*arg);
2819
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820 dict_unref(d);
2821 return generate_NEWDICT(cctx, count);
2822
2823failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002824 if (*arg == NULL)
2825 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826 dict_unref(d);
2827 return FAIL;
2828}
2829
2830/*
2831 * Compile "&option".
2832 */
2833 static int
2834compile_get_option(char_u **arg, cctx_T *cctx)
2835{
2836 typval_T rettv;
2837 char_u *start = *arg;
2838 int ret;
2839
2840 // parse the option and get the current value to get the type.
2841 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002842 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843 if (ret == OK)
2844 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002845 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846 char_u *name = vim_strnsave(start, *arg - start);
2847 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2848
2849 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2850 vim_free(name);
2851 }
2852 clear_tv(&rettv);
2853
2854 return ret;
2855}
2856
2857/*
2858 * Compile "$VAR".
2859 */
2860 static int
2861compile_get_env(char_u **arg, cctx_T *cctx)
2862{
2863 char_u *start = *arg;
2864 int len;
2865 int ret;
2866 char_u *name;
2867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002868 ++*arg;
2869 len = get_env_len(arg);
2870 if (len == 0)
2871 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002872 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873 return FAIL;
2874 }
2875
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002876 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 name = vim_strnsave(start, len + 1);
2878 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2879 vim_free(name);
2880 return ret;
2881}
2882
2883/*
2884 * Compile "@r".
2885 */
2886 static int
2887compile_get_register(char_u **arg, cctx_T *cctx)
2888{
2889 int ret;
2890
2891 ++*arg;
2892 if (**arg == NUL)
2893 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002894 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002895 return FAIL;
2896 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002897 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002898 {
2899 emsg_invreg(**arg);
2900 return FAIL;
2901 }
2902 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2903 ++*arg;
2904 return ret;
2905}
2906
2907/*
2908 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002909 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910 */
2911 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002912apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002914 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915
2916 // this works from end to start
2917 while (p > start)
2918 {
2919 --p;
2920 if (*p == '-' || *p == '+')
2921 {
2922 // only '-' has an effect, for '+' we only check the type
2923#ifdef FEAT_FLOAT
2924 if (rettv->v_type == VAR_FLOAT)
2925 {
2926 if (*p == '-')
2927 rettv->vval.v_float = -rettv->vval.v_float;
2928 }
2929 else
2930#endif
2931 {
2932 varnumber_T val;
2933 int error = FALSE;
2934
2935 // tv_get_number_chk() accepts a string, but we don't want that
2936 // here
2937 if (check_not_string(rettv) == FAIL)
2938 return FAIL;
2939 val = tv_get_number_chk(rettv, &error);
2940 clear_tv(rettv);
2941 if (error)
2942 return FAIL;
2943 if (*p == '-')
2944 val = -val;
2945 rettv->v_type = VAR_NUMBER;
2946 rettv->vval.v_number = val;
2947 }
2948 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002949 else if (numeric_only)
2950 {
2951 ++p;
2952 break;
2953 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002954 else
2955 {
2956 int v = tv2bool(rettv);
2957
2958 // '!' is permissive in the type.
2959 clear_tv(rettv);
2960 rettv->v_type = VAR_BOOL;
2961 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2962 }
2963 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002964 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 return OK;
2966}
2967
2968/*
2969 * Recognize v: variables that are constants and set "rettv".
2970 */
2971 static void
2972get_vim_constant(char_u **arg, typval_T *rettv)
2973{
2974 if (STRNCMP(*arg, "v:true", 6) == 0)
2975 {
2976 rettv->v_type = VAR_BOOL;
2977 rettv->vval.v_number = VVAL_TRUE;
2978 *arg += 6;
2979 }
2980 else if (STRNCMP(*arg, "v:false", 7) == 0)
2981 {
2982 rettv->v_type = VAR_BOOL;
2983 rettv->vval.v_number = VVAL_FALSE;
2984 *arg += 7;
2985 }
2986 else if (STRNCMP(*arg, "v:null", 6) == 0)
2987 {
2988 rettv->v_type = VAR_SPECIAL;
2989 rettv->vval.v_number = VVAL_NULL;
2990 *arg += 6;
2991 }
2992 else if (STRNCMP(*arg, "v:none", 6) == 0)
2993 {
2994 rettv->v_type = VAR_SPECIAL;
2995 rettv->vval.v_number = VVAL_NONE;
2996 *arg += 6;
2997 }
2998}
2999
Bram Moolenaar696ba232020-07-29 21:20:41 +02003000 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003001get_compare_type(char_u *p, int *len, int *type_is)
3002{
3003 exptype_T type = EXPR_UNKNOWN;
3004 int i;
3005
3006 switch (p[0])
3007 {
3008 case '=': if (p[1] == '=')
3009 type = EXPR_EQUAL;
3010 else if (p[1] == '~')
3011 type = EXPR_MATCH;
3012 break;
3013 case '!': if (p[1] == '=')
3014 type = EXPR_NEQUAL;
3015 else if (p[1] == '~')
3016 type = EXPR_NOMATCH;
3017 break;
3018 case '>': if (p[1] != '=')
3019 {
3020 type = EXPR_GREATER;
3021 *len = 1;
3022 }
3023 else
3024 type = EXPR_GEQUAL;
3025 break;
3026 case '<': if (p[1] != '=')
3027 {
3028 type = EXPR_SMALLER;
3029 *len = 1;
3030 }
3031 else
3032 type = EXPR_SEQUAL;
3033 break;
3034 case 'i': if (p[1] == 's')
3035 {
3036 // "is" and "isnot"; but not a prefix of a name
3037 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3038 *len = 5;
3039 i = p[*len];
3040 if (!isalnum(i) && i != '_')
3041 {
3042 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3043 *type_is = TRUE;
3044 }
3045 }
3046 break;
3047 }
3048 return type;
3049}
3050
3051/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003053 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 */
3055 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003056compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003057{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003058 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059
3060 // this works from end to start
3061 while (p > start)
3062 {
3063 --p;
3064 if (*p == '-' || *p == '+')
3065 {
3066 int negate = *p == '-';
3067 isn_T *isn;
3068
3069 // TODO: check type
3070 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3071 {
3072 --p;
3073 if (*p == '-')
3074 negate = !negate;
3075 }
3076 // only '-' has an effect, for '+' we only check the type
3077 if (negate)
3078 isn = generate_instr(cctx, ISN_NEGATENR);
3079 else
3080 isn = generate_instr(cctx, ISN_CHECKNR);
3081 if (isn == NULL)
3082 return FAIL;
3083 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003084 else if (numeric_only)
3085 {
3086 ++p;
3087 break;
3088 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 else
3090 {
3091 int invert = TRUE;
3092
3093 while (p > start && p[-1] == '!')
3094 {
3095 --p;
3096 invert = !invert;
3097 }
3098 if (generate_2BOOL(cctx, invert) == FAIL)
3099 return FAIL;
3100 }
3101 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003102 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103 return OK;
3104}
3105
3106/*
3107 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003108 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109 */
3110 static int
3111compile_subscript(
3112 char_u **arg,
3113 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003114 char_u *start_leader,
3115 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003116 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003118 char_u *name_start = *end_leader;
3119
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 for (;;)
3121 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003122 char_u *p = skipwhite(*arg);
3123
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003124 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003125 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003126 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003127
3128 // If a following line starts with "->{" or "->X" advance to that
3129 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003130 // Also if a following line starts with ".x".
3131 if (next != NULL &&
3132 ((next[0] == '-' && next[1] == '>'
3133 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003134 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003135 {
3136 next = next_line_from_context(cctx, TRUE);
3137 if (next == NULL)
3138 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003139 *arg = next;
3140 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003141 }
3142 }
3143
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003144 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3145 // not a function call.
3146 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003148 garray_T *stack = &cctx->ctx_type_stack;
3149 type_T *type;
3150 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003152 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003153 return FAIL;
3154
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003156 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3157
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003158 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3160 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003161 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 return FAIL;
3163 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003164 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003166 char_u *pstart = p;
3167
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003168 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003169 return FAIL;
3170
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 // something->method()
3172 // Apply the '!', '-' and '+' first:
3173 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003174 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003177 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003178 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003179 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 if (**arg == '{')
3181 {
3182 // lambda call: list->{lambda}
3183 if (compile_lambda_call(arg, cctx) == FAIL)
3184 return FAIL;
3185 }
3186 else
3187 {
3188 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003189 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003190 if (!eval_isnamec1(*p))
3191 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003192 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003193 return FAIL;
3194 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003195 if (ASCII_ISALPHA(*p) && p[1] == ':')
3196 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003197 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198 ;
3199 if (*p != '(')
3200 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003201 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 return FAIL;
3203 }
3204 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003205 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 return FAIL;
3207 }
3208 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003209 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003211 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003212 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003213 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003214 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003215 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003216
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003217 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003218 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003219 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003220 // TODO: blob index
3221 // TODO: more arguments
3222 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003223 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003224 return FAIL;
3225
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003226 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003227 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003228 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003229 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003230 if (**arg == ':')
3231 // missing first index is equal to zero
3232 generate_PUSHNR(cctx, 0);
3233 else
3234 {
3235 if (compile_expr0(arg, cctx) == FAIL)
3236 return FAIL;
3237 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3238 return FAIL;
3239 *arg = skipwhite(*arg);
3240 }
3241 if (**arg == ':')
3242 {
3243 *arg = skipwhite(*arg + 1);
3244 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3245 return FAIL;
3246 if (**arg == ']')
3247 // missing second index is equal to end of string
3248 generate_PUSHNR(cctx, -1);
3249 else
3250 {
3251 if (compile_expr0(arg, cctx) == FAIL)
3252 return FAIL;
3253 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3254 return FAIL;
3255 *arg = skipwhite(*arg);
3256 }
3257 is_slice = TRUE;
3258 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259
3260 if (**arg != ']')
3261 {
3262 emsg(_(e_missbrac));
3263 return FAIL;
3264 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003265 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003267 // We can index a list and a dict. If we don't know the type
3268 // we can use the index value type.
3269 // TODO: If we don't know use an instruction to figure it out at
3270 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003271 typep = ((type_T **)stack->ga_data) + stack->ga_len
3272 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003273 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003274 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3275 // If the index is a string, the variable must be a Dict.
3276 if (*typep == &t_any && valtype == &t_string)
3277 vtype = VAR_DICT;
3278 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003279 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003280 if (need_type(valtype, &t_number, -1, cctx, FALSE) == FAIL)
3281 return FAIL;
3282 if (is_slice)
3283 {
3284 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
3285 if (need_type(valtype, &t_number, -2, cctx, FALSE) == FAIL)
3286 return FAIL;
3287 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003288 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003289
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003290 if (vtype == VAR_DICT)
3291 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003292 if (is_slice)
3293 {
3294 emsg(_(e_cannot_slice_dictionary));
3295 return FAIL;
3296 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003297 if ((*typep)->tt_type == VAR_DICT)
3298 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003299 else
3300 {
3301 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3302 return FAIL;
3303 *typep = &t_any;
3304 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003305 if (may_generate_2STRING(-1, cctx) == FAIL)
3306 return FAIL;
3307 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3308 return FAIL;
3309 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003310 else if (vtype == VAR_STRING)
3311 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003312 *typep = &t_string;
3313 if ((is_slice
3314 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3315 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003316 return FAIL;
3317 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003318 else if (vtype == VAR_BLOB)
3319 {
3320 emsg("Sorry, blob index and slice not implemented yet");
3321 return FAIL;
3322 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003323 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003324 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003325 if (is_slice)
3326 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003327 if (generate_instr_drop(cctx,
3328 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3329 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003330 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003331 }
Bram Moolenaared591872020-08-15 22:14:53 +02003332 else
3333 {
3334 if ((*typep)->tt_type == VAR_LIST)
3335 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003336 if (generate_instr_drop(cctx,
3337 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3338 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003339 return FAIL;
3340 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003341 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003342 else
3343 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003344 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003345 return FAIL;
3346 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003347 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003348 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003350 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003351 return FAIL;
3352
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003353 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003354 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003355 {
3356 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003357 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003358 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003360 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003361 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 while (eval_isnamec(*p))
3363 MB_PTR_ADV(p);
3364 if (p == *arg)
3365 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003366 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 return FAIL;
3368 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003369 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370 return FAIL;
3371 *arg = p;
3372 }
3373 else
3374 break;
3375 }
3376
3377 // TODO - see handle_subscript():
3378 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3379 // Don't do this when "Func" is already a partial that was bound
3380 // explicitly (pt_auto is FALSE).
3381
3382 return OK;
3383}
3384
3385/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003386 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3387 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003389 * If the value is a constant "ppconst->pp_ret" will be set.
3390 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003391 *
3392 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393 */
3394
3395/*
3396 * number number constant
3397 * 0zFFFFFFFF Blob constant
3398 * "string" string constant
3399 * 'string' literal string constant
3400 * &option-name option value
3401 * @r register contents
3402 * identifier variable value
3403 * function() function call
3404 * $VAR environment variable
3405 * (expression) nested expression
3406 * [expr, expr] List
3407 * {key: val, key: val} Dictionary
3408 * #{key: val, key: val} Dictionary with literal keys
3409 *
3410 * Also handle:
3411 * ! in front logical NOT
3412 * - in front unary minus
3413 * + in front unary plus (ignored)
3414 * trailing (arg) funcref/partial call
3415 * trailing [] subscript in String or List
3416 * trailing .name entry in Dictionary
3417 * trailing ->name() method call
3418 */
3419 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003420compile_expr7(
3421 char_u **arg,
3422 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003423 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003424{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425 char_u *start_leader, *end_leader;
3426 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003427 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003428 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429
3430 /*
3431 * Skip '!', '-' and '+' characters. They are handled later.
3432 */
3433 start_leader = *arg;
3434 while (**arg == '!' || **arg == '-' || **arg == '+')
3435 *arg = skipwhite(*arg + 1);
3436 end_leader = *arg;
3437
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003438 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439 switch (**arg)
3440 {
3441 /*
3442 * Number constant.
3443 */
3444 case '0': // also for blob starting with 0z
3445 case '1':
3446 case '2':
3447 case '3':
3448 case '4':
3449 case '5':
3450 case '6':
3451 case '7':
3452 case '8':
3453 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003454 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003455 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003456 // Apply "-" and "+" just before the number now, right to
3457 // left. Matters especially when "->" follows. Stops at
3458 // '!'.
3459 if (apply_leader(rettv, TRUE,
3460 start_leader, &end_leader) == FAIL)
3461 {
3462 clear_tv(rettv);
3463 return FAIL;
3464 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003465 break;
3466
3467 /*
3468 * String constant: "string".
3469 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003470 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003471 return FAIL;
3472 break;
3473
3474 /*
3475 * Literal string constant: 'str''ing'.
3476 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003477 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 return FAIL;
3479 break;
3480
3481 /*
3482 * Constant Vim variable.
3483 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003484 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 ret = NOTDONE;
3486 break;
3487
3488 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003489 * "true" constant
3490 */
3491 case 't': if (STRNCMP(*arg, "true", 4) == 0
3492 && !eval_isnamec((*arg)[4]))
3493 {
3494 *arg += 4;
3495 rettv->v_type = VAR_BOOL;
3496 rettv->vval.v_number = VVAL_TRUE;
3497 }
3498 else
3499 ret = NOTDONE;
3500 break;
3501
3502 /*
3503 * "false" constant
3504 */
3505 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3506 && !eval_isnamec((*arg)[5]))
3507 {
3508 *arg += 5;
3509 rettv->v_type = VAR_BOOL;
3510 rettv->vval.v_number = VVAL_FALSE;
3511 }
3512 else
3513 ret = NOTDONE;
3514 break;
3515
3516 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003517 * List: [expr, expr]
3518 */
3519 case '[': ret = compile_list(arg, cctx);
3520 break;
3521
3522 /*
3523 * Dictionary: #{key: val, key: val}
3524 */
3525 case '#': if ((*arg)[1] == '{')
3526 {
3527 ++*arg;
3528 ret = compile_dict(arg, cctx, TRUE);
3529 }
3530 else
3531 ret = NOTDONE;
3532 break;
3533
3534 /*
3535 * Lambda: {arg, arg -> expr}
3536 * Dictionary: {'key': val, 'key': val}
3537 */
3538 case '{': {
3539 char_u *start = skipwhite(*arg + 1);
3540
3541 // Find out what comes after the arguments.
3542 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003543 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003544 if (ret != FAIL && *start == '>')
3545 ret = compile_lambda(arg, cctx);
3546 else
3547 ret = compile_dict(arg, cctx, FALSE);
3548 }
3549 break;
3550
3551 /*
3552 * Option value: &name
3553 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003554 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3555 return FAIL;
3556 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003557 break;
3558
3559 /*
3560 * Environment variable: $VAR.
3561 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003562 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3563 return FAIL;
3564 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565 break;
3566
3567 /*
3568 * Register contents: @r.
3569 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003570 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3571 return FAIL;
3572 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573 break;
3574 /*
3575 * nested expression: (expression).
3576 */
3577 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003578
3579 // recursive!
3580 if (ppconst->pp_used <= PPSIZE - 10)
3581 {
3582 ret = compile_expr1(arg, cctx, ppconst);
3583 }
3584 else
3585 {
3586 // Not enough space in ppconst, flush constants.
3587 if (generate_ppconst(cctx, ppconst) == FAIL)
3588 return FAIL;
3589 ret = compile_expr0(arg, cctx);
3590 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591 *arg = skipwhite(*arg);
3592 if (**arg == ')')
3593 ++*arg;
3594 else if (ret == OK)
3595 {
3596 emsg(_(e_missing_close));
3597 ret = FAIL;
3598 }
3599 break;
3600
3601 default: ret = NOTDONE;
3602 break;
3603 }
3604 if (ret == FAIL)
3605 return FAIL;
3606
Bram Moolenaar1c747212020-05-09 18:28:34 +02003607 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003609 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003610 clear_tv(rettv);
3611 else
3612 // A constant expression can possibly be handled compile time,
3613 // return the value instead of generating code.
3614 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 }
3616 else if (ret == NOTDONE)
3617 {
3618 char_u *p;
3619 int r;
3620
3621 if (!eval_isnamec1(**arg))
3622 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003623 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624 return FAIL;
3625 }
3626
3627 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003628 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003630 {
3631 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3632 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003634 {
3635 if (generate_ppconst(cctx, ppconst) == FAIL)
3636 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003637 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003638 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639 if (r == FAIL)
3640 return FAIL;
3641 }
3642
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003643 // Handle following "[]", ".member", etc.
3644 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003645 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003646 ppconst) == FAIL)
3647 return FAIL;
3648 if (ppconst->pp_used > 0)
3649 {
3650 // apply the '!', '-' and '+' before the constant
3651 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003652 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003653 return FAIL;
3654 return OK;
3655 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003656 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003658 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659}
3660
3661/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003662 * Give the "white on both sides" error, taking the operator from "p[len]".
3663 */
3664 void
3665error_white_both(char_u *op, int len)
3666{
3667 char_u buf[10];
3668
3669 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003670 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003671}
3672
3673/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003674 * <type>expr7: runtime type check / conversion
3675 */
3676 static int
3677compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3678{
3679 type_T *want_type = NULL;
3680
3681 // Recognize <type>
3682 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3683 {
3684 int called_emsg_before = called_emsg;
3685
3686 ++*arg;
3687 want_type = parse_type(arg, cctx->ctx_type_list);
3688 if (called_emsg != called_emsg_before)
3689 return FAIL;
3690
3691 if (**arg != '>')
3692 {
3693 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003694 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003695 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003696 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003697 return FAIL;
3698 }
3699 ++*arg;
3700 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3701 return FAIL;
3702 }
3703
3704 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3705 return FAIL;
3706
3707 if (want_type != NULL)
3708 {
3709 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003710 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003711
Bram Moolenaard1103582020-08-14 22:44:25 +02003712 generate_ppconst(cctx, ppconst);
3713 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003714 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003715 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003716 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3717 return FAIL;
3718 }
3719 }
3720
3721 return OK;
3722}
3723
3724/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 * * number multiplication
3726 * / number division
3727 * % number modulo
3728 */
3729 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003730compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731{
3732 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003733 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003734 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003736 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003737 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738 return FAIL;
3739
3740 /*
3741 * Repeat computing, until no "*", "/" or "%" is following.
3742 */
3743 for (;;)
3744 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003745 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746 if (*op != '*' && *op != '/' && *op != '%')
3747 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003748 if (next != NULL)
3749 {
3750 *arg = next_line_from_context(cctx, TRUE);
3751 op = skipwhite(*arg);
3752 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003753
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003754 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003755 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003756 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003757 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003758 }
3759 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003760 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003761 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003763 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003764 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003766
3767 if (ppconst->pp_used == ppconst_used + 2
3768 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3769 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003770 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003771 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3772 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003773 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003775 // both are numbers: compute the result
3776 switch (*op)
3777 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003778 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003779 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003780 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003781 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003782 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003783 break;
3784 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003785 tv1->vval.v_number = res;
3786 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003787 }
3788 else
3789 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003790 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003791 generate_two_op(cctx, op);
3792 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003793 }
3794
3795 return OK;
3796}
3797
3798/*
3799 * + number addition
3800 * - number subtraction
3801 * .. string concatenation
3802 */
3803 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003804compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003805{
3806 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003807 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003809 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810
3811 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003812 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813 return FAIL;
3814
3815 /*
3816 * Repeat computing, until no "+", "-" or ".." is following.
3817 */
3818 for (;;)
3819 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003820 op = may_peek_next_line(cctx, *arg, &next);
3821 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822 break;
3823 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003824 if (next != NULL)
3825 {
3826 *arg = next_line_from_context(cctx, TRUE);
3827 op = skipwhite(*arg);
3828 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003830 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003832 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003833 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 }
3835
3836 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003837 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003838 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003840 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003841 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842 return FAIL;
3843
Bram Moolenaara5565e42020-05-09 15:44:01 +02003844 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003845 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003846 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3847 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3848 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3849 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003851 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3852 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003853
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003854 // concat/subtract/add constant numbers
3855 if (*op == '+')
3856 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3857 else if (*op == '-')
3858 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3859 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003860 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003861 // concatenate constant strings
3862 char_u *s1 = tv1->vval.v_string;
3863 char_u *s2 = tv2->vval.v_string;
3864 size_t len1 = STRLEN(s1);
3865
3866 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3867 if (tv1->vval.v_string == NULL)
3868 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003869 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003870 return FAIL;
3871 }
3872 mch_memmove(tv1->vval.v_string, s1, len1);
3873 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003874 vim_free(s1);
3875 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003876 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003877 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878 }
3879 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003880 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003881 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003882 if (*op == '.')
3883 {
3884 if (may_generate_2STRING(-2, cctx) == FAIL
3885 || may_generate_2STRING(-1, cctx) == FAIL)
3886 return FAIL;
3887 generate_instr_drop(cctx, ISN_CONCAT, 1);
3888 }
3889 else
3890 generate_two_op(cctx, op);
3891 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003892 }
3893
3894 return OK;
3895}
3896
3897/*
3898 * expr5a == expr5b
3899 * expr5a =~ expr5b
3900 * expr5a != expr5b
3901 * expr5a !~ expr5b
3902 * expr5a > expr5b
3903 * expr5a >= expr5b
3904 * expr5a < expr5b
3905 * expr5a <= expr5b
3906 * expr5a is expr5b
3907 * expr5a isnot expr5b
3908 *
3909 * Produces instructions:
3910 * EVAL expr5a Push result of "expr5a"
3911 * EVAL expr5b Push result of "expr5b"
3912 * COMPARE one of the compare instructions
3913 */
3914 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003915compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916{
3917 exptype_T type = EXPR_UNKNOWN;
3918 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003919 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003922 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923
3924 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003925 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926 return FAIL;
3927
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003928 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003929 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003930
3931 /*
3932 * If there is a comparative operator, use it.
3933 */
3934 if (type != EXPR_UNKNOWN)
3935 {
3936 int ic = FALSE; // Default: do not ignore case
3937
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003938 if (next != NULL)
3939 {
3940 *arg = next_line_from_context(cctx, TRUE);
3941 p = skipwhite(*arg);
3942 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943 if (type_is && (p[len] == '?' || p[len] == '#'))
3944 {
3945 semsg(_(e_invexpr2), *arg);
3946 return FAIL;
3947 }
3948 // extra question mark appended: ignore case
3949 if (p[len] == '?')
3950 {
3951 ic = TRUE;
3952 ++len;
3953 }
3954 // extra '#' appended: match case (ignored)
3955 else if (p[len] == '#')
3956 ++len;
3957 // nothing appended: match case
3958
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003959 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003960 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003961 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003962 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 }
3964
3965 // get the second variable
3966 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003967 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003968 return FAIL;
3969
Bram Moolenaara5565e42020-05-09 15:44:01 +02003970 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003971 return FAIL;
3972
Bram Moolenaara5565e42020-05-09 15:44:01 +02003973 if (ppconst->pp_used == ppconst_used + 2)
3974 {
3975 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3976 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3977 int ret;
3978
3979 // Both sides are a constant, compute the result now.
3980 // First check for a valid combination of types, this is more
3981 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003982 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003983 ret = FAIL;
3984 else
3985 {
3986 ret = typval_compare(tv1, tv2, type, ic);
3987 tv1->v_type = VAR_BOOL;
3988 tv1->vval.v_number = tv1->vval.v_number
3989 ? VVAL_TRUE : VVAL_FALSE;
3990 clear_tv(tv2);
3991 --ppconst->pp_used;
3992 }
3993 return ret;
3994 }
3995
3996 generate_ppconst(cctx, ppconst);
3997 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003998 }
3999
4000 return OK;
4001}
4002
Bram Moolenaar7f141552020-05-09 17:35:53 +02004003static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4004
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005/*
4006 * Compile || or &&.
4007 */
4008 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004009compile_and_or(
4010 char_u **arg,
4011 cctx_T *cctx,
4012 char *op,
4013 ppconst_T *ppconst,
4014 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004015{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004016 char_u *next;
4017 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018 int opchar = *op;
4019
4020 if (p[0] == opchar && p[1] == opchar)
4021 {
4022 garray_T *instr = &cctx->ctx_instr;
4023 garray_T end_ga;
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004024 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004025 int all_bool_values = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026
4027 /*
4028 * Repeat until there is no following "||" or "&&"
4029 */
4030 ga_init2(&end_ga, sizeof(int), 10);
4031 while (p[0] == opchar && p[1] == opchar)
4032 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004033 if (next != NULL)
4034 {
4035 *arg = next_line_from_context(cctx, TRUE);
4036 p = skipwhite(*arg);
4037 }
4038
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004039 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4040 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004041 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004042 return FAIL;
4043 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004044
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004045 // TODO: use ppconst if the value is a constant and check
4046 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004047 generate_ppconst(cctx, ppconst);
4048
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004049 if (((type_T **)stack->ga_data)[stack->ga_len - 1] != &t_bool)
4050 all_bool_values = FALSE;
4051
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052 if (ga_grow(&end_ga, 1) == FAIL)
4053 {
4054 ga_clear(&end_ga);
4055 return FAIL;
4056 }
4057 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4058 ++end_ga.ga_len;
4059 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004060 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061
4062 // eval the next expression
4063 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004064 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004065 return FAIL;
4066
Bram Moolenaara5565e42020-05-09 15:44:01 +02004067 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4068 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 {
4070 ga_clear(&end_ga);
4071 return FAIL;
4072 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004073
4074 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004076 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077
4078 // Fill in the end label in all jumps.
4079 while (end_ga.ga_len > 0)
4080 {
4081 isn_T *isn;
4082
4083 --end_ga.ga_len;
4084 isn = ((isn_T *)instr->ga_data)
4085 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4086 isn->isn_arg.jump.jump_where = instr->ga_len;
4087 }
4088 ga_clear(&end_ga);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004089
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004090 // The resulting type is converted to bool if needed.
4091 if (!all_bool_values)
4092 generate_COND2BOOL(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093 }
4094
4095 return OK;
4096}
4097
4098/*
4099 * expr4a && expr4a && expr4a logical AND
4100 *
4101 * Produces instructions:
4102 * EVAL expr4a Push result of "expr4a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004103 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004105 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 * EVAL expr4c Push result of "expr4c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004107 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108 * end:
4109 */
4110 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004111compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004113 int ppconst_used = ppconst->pp_used;
4114
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004116 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004117 return FAIL;
4118
4119 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004120 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121}
4122
4123/*
4124 * expr3a || expr3b || expr3c logical OR
4125 *
4126 * Produces instructions:
4127 * EVAL expr3a Push result of "expr3a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004128 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004130 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004131 * EVAL expr3c Push result of "expr3c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004132 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 * end:
4134 */
4135 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004136compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004138 int ppconst_used = ppconst->pp_used;
4139
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004141 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142 return FAIL;
4143
4144 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004145 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146}
4147
4148/*
4149 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004150 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004151 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152 * JUMP_IF_FALSE alt jump if false
4153 * EVAL expr1a
4154 * JUMP_ALWAYS end
4155 * alt: EVAL expr1b
4156 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004157 *
4158 * Toplevel expression: expr2 ?? expr1
4159 * Produces instructions:
4160 * EVAL expr2 Push result of "expr2"
4161 * JUMP_AND_KEEP_IF_TRUE end jump if true
4162 * EVAL expr1
4163 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004164 */
4165 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004166compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004167{
4168 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004169 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004170 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171
Bram Moolenaar3988f642020-08-27 22:43:03 +02004172 // Ignore all kinds of errors when not producing code.
4173 if (cctx->ctx_skip == SKIP_YES)
4174 {
4175 skip_expr(arg);
4176 return OK;
4177 }
4178
Bram Moolenaar61a89812020-05-07 16:58:17 +02004179 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004180 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181 return FAIL;
4182
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004183 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184 if (*p == '?')
4185 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004186 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004187 garray_T *instr = &cctx->ctx_instr;
4188 garray_T *stack = &cctx->ctx_type_stack;
4189 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004190 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004191 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004192 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004193 int has_const_expr = FALSE;
4194 int const_value = FALSE;
4195 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004197 if (next != NULL)
4198 {
4199 *arg = next_line_from_context(cctx, TRUE);
4200 p = skipwhite(*arg);
4201 }
4202
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004203 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004204 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004205 semsg(_(e_white_space_required_before_and_after_str),
4206 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004207 return FAIL;
4208 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209
Bram Moolenaara5565e42020-05-09 15:44:01 +02004210 if (ppconst->pp_used == ppconst_used + 1)
4211 {
4212 // the condition is a constant, we know whether the ? or the :
4213 // expression is to be evaluated.
4214 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004215 if (op_falsy)
4216 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4217 else
4218 {
4219 int error = FALSE;
4220
4221 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4222 &error);
4223 if (error)
4224 return FAIL;
4225 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004226 cctx->ctx_skip = save_skip == SKIP_YES ||
4227 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4228
4229 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4230 // "left ?? right" and "left" is truthy: produce "left"
4231 generate_ppconst(cctx, ppconst);
4232 else
4233 {
4234 clear_tv(&ppconst->pp_tv[ppconst_used]);
4235 --ppconst->pp_used;
4236 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004237 }
4238 else
4239 {
4240 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004241 if (op_falsy)
4242 end_idx = instr->ga_len;
4243 generate_JUMP(cctx, op_falsy
4244 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4245 if (op_falsy)
4246 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004247 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248
4249 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004250 *arg = skipwhite(p + 1 + op_falsy);
4251 if (may_get_next_line(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004252 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004253 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004254 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255
Bram Moolenaara5565e42020-05-09 15:44:01 +02004256 if (!has_const_expr)
4257 {
4258 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004260 if (!op_falsy)
4261 {
4262 // remember the type and drop it
4263 --stack->ga_len;
4264 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004266 end_idx = instr->ga_len;
4267 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004268
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004269 // jump here from JUMP_IF_FALSE
4270 isn = ((isn_T *)instr->ga_data) + alt_idx;
4271 isn->isn_arg.jump.jump_where = instr->ga_len;
4272 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004273 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004275 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004277 // Check for the ":".
4278 p = may_peek_next_line(cctx, *arg, &next);
4279 if (*p != ':')
4280 {
4281 emsg(_(e_missing_colon));
4282 return FAIL;
4283 }
4284 if (next != NULL)
4285 {
4286 *arg = next_line_from_context(cctx, TRUE);
4287 p = skipwhite(*arg);
4288 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004289
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004290 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4291 {
4292 semsg(_(e_white_space_required_before_and_after_str), ":");
4293 return FAIL;
4294 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004295
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004296 // evaluate the third expression
4297 if (has_const_expr)
4298 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004299 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004300 *arg = skipwhite(p + 1);
4301 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4302 return FAIL;
4303 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4304 return FAIL;
4305 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004306
Bram Moolenaara5565e42020-05-09 15:44:01 +02004307 if (!has_const_expr)
4308 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004309 type_T **typep;
4310
Bram Moolenaara5565e42020-05-09 15:44:01 +02004311 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312
Bram Moolenaara5565e42020-05-09 15:44:01 +02004313 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004314 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4315 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004316
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004317 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004318 isn = ((isn_T *)instr->ga_data) + end_idx;
4319 isn->isn_arg.jump.jump_where = instr->ga_len;
4320 }
4321
4322 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323 }
4324 return OK;
4325}
4326
4327/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004328 * Toplevel expression.
4329 */
4330 static int
4331compile_expr0(char_u **arg, cctx_T *cctx)
4332{
4333 ppconst_T ppconst;
4334
4335 CLEAR_FIELD(ppconst);
4336 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4337 {
4338 clear_ppconst(&ppconst);
4339 return FAIL;
4340 }
4341 if (generate_ppconst(cctx, &ppconst) == FAIL)
4342 return FAIL;
4343 return OK;
4344}
4345
4346/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347 * compile "return [expr]"
4348 */
4349 static char_u *
4350compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4351{
4352 char_u *p = arg;
4353 garray_T *stack = &cctx->ctx_type_stack;
4354 type_T *stack_type;
4355
4356 if (*p != NUL && *p != '|' && *p != '\n')
4357 {
4358 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004359 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004360 return NULL;
4361
4362 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4363 if (set_return_type)
4364 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004365 else
4366 {
4367 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4368 && stack_type->tt_type != VAR_VOID
4369 && stack_type->tt_type != VAR_UNKNOWN)
4370 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004371 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004372 return NULL;
4373 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004374 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4375 cctx, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004376 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004377 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378 }
4379 else
4380 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004381 // "set_return_type" cannot be TRUE, only used for a lambda which
4382 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004383 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4384 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004386 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387 return NULL;
4388 }
4389
4390 // No argument, return zero.
4391 generate_PUSHNR(cctx, 0);
4392 }
4393
4394 if (generate_instr(cctx, ISN_RETURN) == NULL)
4395 return NULL;
4396
4397 // "return val | endif" is possible
4398 return skipwhite(p);
4399}
4400
4401/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004402 * Get a line from the compilation context, compatible with exarg_T getline().
4403 * Return a pointer to the line in allocated memory.
4404 * Return NULL for end-of-file or some error.
4405 */
4406 static char_u *
4407exarg_getline(
4408 int c UNUSED,
4409 void *cookie,
4410 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004411 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004412{
4413 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004414 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004415
Bram Moolenaar66250c92020-08-20 15:02:42 +02004416 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004417 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004418 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004419 return NULL;
4420 ++cctx->ctx_lnum;
4421 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4422 // Comment lines result in NULL pointers, skip them.
4423 if (p != NULL)
4424 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004425 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004426}
4427
4428/*
4429 * Compile a nested :def command.
4430 */
4431 static char_u *
4432compile_nested_function(exarg_T *eap, cctx_T *cctx)
4433{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004434 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004435 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004436 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004437 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004438 lvar_T *lvar;
4439 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004440 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004441
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004442 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004443 {
4444 emsg(_(e_cannot_use_bang_with_nested_def));
4445 return NULL;
4446 }
4447
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004448 // Only g:Func() can use a namespace.
4449 if (name_start[1] == ':' && !is_global)
4450 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004451 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004452 return NULL;
4453 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004454 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4455 return NULL;
4456
Bram Moolenaar04b12692020-05-04 23:24:44 +02004457 eap->arg = name_end;
4458 eap->getline = exarg_getline;
4459 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004460 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004461 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004462 lambda_name = get_lambda_name();
4463 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004464
Bram Moolenaar822ba242020-05-24 23:00:18 +02004465 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004466 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004467 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004468 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004469 {
4470 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004471 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004472 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004473
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004474 if (is_global)
4475 {
4476 char_u *func_name = vim_strnsave(name_start + 2,
4477 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004478
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004479 if (func_name == NULL)
4480 r = FAIL;
4481 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004482 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004483 }
4484 else
4485 {
4486 // Define a local variable for the function reference.
4487 lvar = reserve_local(cctx, name_start, name_end - name_start,
4488 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004489 if (lvar == NULL)
4490 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004491 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004492 return NULL;
4493 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4494 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004495
Bram Moolenaar61a89812020-05-07 16:58:17 +02004496 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004497 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004498}
4499
4500/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004501 * Return the length of an assignment operator, or zero if there isn't one.
4502 */
4503 int
4504assignment_len(char_u *p, int *heredoc)
4505{
4506 if (*p == '=')
4507 {
4508 if (p[1] == '<' && p[2] == '<')
4509 {
4510 *heredoc = TRUE;
4511 return 3;
4512 }
4513 return 1;
4514 }
4515 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4516 return 2;
4517 if (STRNCMP(p, "..=", 3) == 0)
4518 return 3;
4519 return 0;
4520}
4521
4522// words that cannot be used as a variable
4523static char *reserved[] = {
4524 "true",
4525 "false",
4526 NULL
4527};
4528
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004529typedef enum {
4530 dest_local,
4531 dest_option,
4532 dest_env,
4533 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004534 dest_buffer,
4535 dest_window,
4536 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004537 dest_vimvar,
4538 dest_script,
4539 dest_reg,
4540} assign_dest_T;
4541
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004543 * Generate the load instruction for "name".
4544 */
4545 static void
4546generate_loadvar(
4547 cctx_T *cctx,
4548 assign_dest_T dest,
4549 char_u *name,
4550 lvar_T *lvar,
4551 type_T *type)
4552{
4553 switch (dest)
4554 {
4555 case dest_option:
4556 // TODO: check the option exists
4557 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4558 break;
4559 case dest_global:
4560 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4561 break;
4562 case dest_buffer:
4563 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4564 break;
4565 case dest_window:
4566 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4567 break;
4568 case dest_tab:
4569 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4570 break;
4571 case dest_script:
4572 compile_load_scriptvar(cctx,
4573 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4574 break;
4575 case dest_env:
4576 // Include $ in the name here
4577 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4578 break;
4579 case dest_reg:
4580 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4581 break;
4582 case dest_vimvar:
4583 generate_LOADV(cctx, name + 2, TRUE);
4584 break;
4585 case dest_local:
4586 if (lvar->lv_from_outer)
4587 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4588 NULL, type);
4589 else
4590 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4591 break;
4592 }
4593}
4594
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004595 void
4596vim9_declare_error(char_u *name)
4597{
4598 char *scope = "";
4599
4600 switch (*name)
4601 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004602 case 'g': scope = _("global"); break;
4603 case 'b': scope = _("buffer"); break;
4604 case 'w': scope = _("window"); break;
4605 case 't': scope = _("tab"); break;
4606 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004607 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004608 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004609 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004610 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004611 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004612 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004613 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004614 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004615 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004616}
4617
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004618/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004619 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004620 * "let name"
4621 * "var name = expr"
4622 * "final name = expr"
4623 * "const name = expr"
4624 * "name = expr"
4625 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004626 * Return NULL for an error.
4627 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628 */
4629 static char_u *
4630compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4631{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004632 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004633 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004634 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635 char_u *ret = NULL;
4636 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004637 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004638 int scriptvar_sid = 0;
4639 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004640 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004641 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004642 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 int oplen = 0;
4645 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004646 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004647 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004648 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004649 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004650 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
4651 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004653 // Skip over the "var" or "[var, var]" to get to any "=".
4654 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4655 if (p == NULL)
4656 return *arg == '[' ? arg : NULL;
4657
4658 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004659 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004660 // TODO: should we allow this, and figure out type inference from list
4661 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004662 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004663 return NULL;
4664 }
4665
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004666 sp = p;
4667 p = skipwhite(p);
4668 op = p;
4669 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004670
4671 if (var_count > 0 && oplen == 0)
4672 // can be something like "[1, 2]->func()"
4673 return arg;
4674
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004675 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4676 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004677 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004678 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004679 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004680
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004681 if (heredoc)
4682 {
4683 list_T *l;
4684 listitem_T *li;
4685
4686 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004687 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004688 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004689 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02004690 if (l == NULL)
4691 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004692
Bram Moolenaar078269b2020-09-21 20:35:55 +02004693 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02004695 // Push each line and the create the list.
4696 FOR_ALL_LIST_ITEMS(l, li)
4697 {
4698 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4699 li->li_tv.vval.v_string = NULL;
4700 }
4701 generate_NEWLIST(cctx, l->lv_len);
4702 type = &t_list_string;
4703 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004704 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705 list_free(l);
4706 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004707 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004708 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004709 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004711 // for "[var, var] = expr" evaluate the expression here, loop over the
4712 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004713
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004714 p = skipwhite(op + oplen);
4715 if (compile_expr0(&p, cctx) == FAIL)
4716 return NULL;
4717 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004718
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004719 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004720 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004721 type_T *stacktype;
4722
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004723 stacktype = stack->ga_len == 0 ? &t_void
4724 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004725 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004726 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004727 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004728 goto theend;
4729 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004730 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004731 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004732 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004733 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4734 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004735 }
4736 }
4737
4738 /*
4739 * Loop over variables in "[var, var] = expr".
4740 * For "var = expr" and "let var: type" this is done only once.
4741 */
4742 if (var_count > 0)
4743 var_start = skipwhite(arg + 1); // skip over the "["
4744 else
4745 var_start = arg;
4746 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4747 {
4748 char_u *var_end = skip_var_one(var_start, FALSE);
4749 size_t varlen;
4750 int new_local = FALSE;
4751 int opt_type;
4752 int opt_flags = 0;
4753 assign_dest_T dest = dest_local;
4754 int vimvaridx = -1;
4755 lvar_T *lvar = NULL;
4756 lvar_T arg_lvar;
4757 int has_type = FALSE;
4758 int has_index = FALSE;
4759 int instr_count = -1;
4760
Bram Moolenaar65821722020-08-02 18:58:54 +02004761 if (*var_start == '@')
4762 p = var_start + 2;
4763 else
4764 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02004765 // skip over the leading "&", "&l:", "&g:" and "$"
4766 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02004767 p = to_name_end(p, TRUE);
4768 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004769
4770 // "a: type" is declaring variable "a" with a type, not "a:".
4771 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4772 --var_end;
4773 if (is_decl && p == var_start + 2 && p[-1] == ':')
4774 --p;
4775
4776 varlen = p - var_start;
4777 vim_free(name);
4778 name = vim_strnsave(var_start, varlen);
4779 if (name == NULL)
4780 return NULL;
4781 if (!heredoc)
4782 type = &t_any;
4783
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004784 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004785 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004786 int declare_error = FALSE;
4787
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004788 if (*var_start == '&')
4789 {
4790 int cc;
4791 long numval;
4792
4793 dest = dest_option;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004794 if (cmdidx == CMD_final || cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004795 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004796 emsg(_(e_const_option));
4797 goto theend;
4798 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004799 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004800 p = var_start;
4801 p = find_option_end(&p, &opt_flags);
4802 if (p == NULL)
4803 {
4804 // cannot happen?
4805 emsg(_(e_letunexp));
4806 goto theend;
4807 }
4808 cc = *p;
4809 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02004810 opt_type = get_option_value(skip_option_env_lead(var_start),
4811 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004812 *p = cc;
4813 if (opt_type == -3)
4814 {
4815 semsg(_(e_unknown_option), var_start);
4816 goto theend;
4817 }
4818 if (opt_type == -2 || opt_type == 0)
4819 type = &t_string;
4820 else
4821 type = &t_number; // both number and boolean option
4822 }
4823 else if (*var_start == '$')
4824 {
4825 dest = dest_env;
4826 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004827 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004828 }
4829 else if (*var_start == '@')
4830 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004831 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004832 {
4833 emsg_invreg(var_start[1]);
4834 goto theend;
4835 }
4836 dest = dest_reg;
4837 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004838 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004839 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004840 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004841 {
4842 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004843 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004844 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004845 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004846 {
4847 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004848 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004849 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004850 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004851 {
4852 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004853 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004854 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004855 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004856 {
4857 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004858 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004859 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004860 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004861 {
4862 typval_T *vtv;
4863 int di_flags;
4864
4865 vimvaridx = find_vim_var(name + 2, &di_flags);
4866 if (vimvaridx < 0)
4867 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004868 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004869 goto theend;
4870 }
4871 // We use the current value of "sandbox" here, is that OK?
4872 if (var_check_ro(di_flags, name, FALSE))
4873 goto theend;
4874 dest = dest_vimvar;
4875 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004876 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004877 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004878 }
4879 else
4880 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004881 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004882
4883 for (idx = 0; reserved[idx] != NULL; ++idx)
4884 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004885 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004886 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004887 goto theend;
4888 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004889
4890 lvar = lookup_local(var_start, varlen, cctx);
4891 if (lvar == NULL)
4892 {
4893 CLEAR_FIELD(arg_lvar);
4894 if (lookup_arg(var_start, varlen,
4895 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4896 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004897 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004898 if (is_decl)
4899 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004900 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004901 goto theend;
4902 }
4903 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004904 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004905 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004906 if (lvar != NULL)
4907 {
4908 if (is_decl)
4909 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004910 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004911 goto theend;
4912 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004913 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004914 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004915 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004916 int script_namespace = varlen > 1
4917 && STRNCMP(var_start, "s:", 2) == 0;
4918 int script_var = (script_namespace
Bram Moolenaar53900992020-08-22 19:02:02 +02004919 ? lookup_script(var_start + 2, varlen - 2, FALSE)
4920 : lookup_script(var_start, varlen, TRUE)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004921 imported_T *import =
4922 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004923
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004924 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004925 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004926 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4927
4928 if (is_decl)
4929 {
4930 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004931 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004932 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004933 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004934 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004935 name);
4936 goto theend;
4937 }
4938 else if (cctx->ctx_ufunc->uf_script_ctx_version
4939 == SCRIPT_VERSION_VIM9
4940 && script_namespace
4941 && !script_var && import == NULL)
4942 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004943 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004944 goto theend;
4945 }
4946
4947 dest = dest_script;
4948
4949 // existing script-local variables should have a type
4950 scriptvar_sid = current_sctx.sc_sid;
4951 if (import != NULL)
4952 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004953 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004954 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02004955 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4956 rawname, TRUE);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02004957 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02004958 {
4959 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4960 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004961 ((svar_T *)si->sn_var_vals.ga_data)
4962 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004963 type = sv->sv_type;
4964 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004965 }
4966 }
4967 else if (name[1] == ':' && name[2] != NUL)
4968 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004969 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004970 goto theend;
4971 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004972 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004973 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004974 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004975 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004976 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004977 else if (check_defined(var_start, varlen, cctx) == FAIL)
4978 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004979 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004980 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004981
4982 if (declare_error)
4983 {
4984 vim9_declare_error(name);
4985 goto theend;
4986 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004987 }
4988
4989 // handle "a:name" as a name, not index "name" on "a"
4990 if (varlen > 1 || var_start[varlen] != ':')
4991 p = var_end;
4992
4993 if (dest != dest_option)
4994 {
4995 if (is_decl && *p == ':')
4996 {
4997 // parse optional type: "let var: type = expr"
4998 if (!VIM_ISWHITE(p[1]))
4999 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005000 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005001 goto theend;
5002 }
5003 p = skipwhite(p + 1);
5004 type = parse_type(&p, cctx->ctx_type_list);
5005 has_type = TRUE;
5006 }
5007 else if (lvar != NULL)
5008 type = lvar->lv_type;
5009 }
5010
5011 if (oplen == 3 && !heredoc && dest != dest_global
5012 && type->tt_type != VAR_STRING
5013 && type->tt_type != VAR_ANY)
5014 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005015 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005016 goto theend;
5017 }
5018
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005019 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005020 {
5021 if (oplen > 1 && !heredoc)
5022 {
5023 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005024 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005025 goto theend;
5026 }
5027
5028 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005029 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5030 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005031 goto theend;
5032 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005033 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005034 if (lvar == NULL)
5035 goto theend;
5036 new_local = TRUE;
5037 }
5038
5039 member_type = type;
5040 if (var_end > var_start + varlen)
5041 {
5042 // Something follows after the variable: "var[idx]".
5043 if (is_decl)
5044 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005045 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005046 goto theend;
5047 }
5048
5049 if (var_start[varlen] == '[')
5050 {
5051 has_index = TRUE;
5052 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005053 member_type = &t_any;
5054 else
5055 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005056 }
5057 else
5058 {
5059 semsg("Not supported yet: %s", var_start);
5060 goto theend;
5061 }
5062 }
5063 else if (lvar == &arg_lvar)
5064 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005065 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005066 goto theend;
5067 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005068 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5069 {
5070 semsg(_(e_cannot_assign_to_constant), name);
5071 goto theend;
5072 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005073
5074 if (!heredoc)
5075 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005076 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005077 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005078 if (oplen > 0 && var_count == 0)
5079 {
5080 // skip over the "=" and the expression
5081 p = skipwhite(op + oplen);
5082 compile_expr0(&p, cctx);
5083 }
5084 }
5085 else if (oplen > 0)
5086 {
5087 type_T *stacktype;
5088
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005089 // For "var = expr" evaluate the expression.
5090 if (var_count == 0)
5091 {
5092 int r;
5093
5094 // for "+=", "*=", "..=" etc. first load the current value
5095 if (*op != '=')
5096 {
5097 generate_loadvar(cctx, dest, name, lvar, type);
5098
5099 if (has_index)
5100 {
5101 // TODO: get member from list or dict
5102 emsg("Index with operation not supported yet");
5103 goto theend;
5104 }
5105 }
5106
5107 // Compile the expression. Temporarily hide the new local
5108 // variable here, it is not available to this expression.
5109 if (new_local)
5110 --cctx->ctx_locals.ga_len;
5111 instr_count = instr->ga_len;
5112 p = skipwhite(op + oplen);
5113 r = compile_expr0(&p, cctx);
5114 if (new_local)
5115 ++cctx->ctx_locals.ga_len;
5116 if (r == FAIL)
5117 goto theend;
5118 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005119 else if (semicolon && var_idx == var_count - 1)
5120 {
5121 // For "[var; var] = expr" get the rest of the list
5122 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5123 goto theend;
5124 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005125 else
5126 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005127 // For "[var, var] = expr" get the "var_idx" item from the
5128 // list.
5129 if (generate_GETITEM(cctx, var_idx) == FAIL)
5130 return FAIL;
5131 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005132
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005133 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005134 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005135 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005136 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005137 if ((stacktype->tt_type == VAR_FUNC
5138 || stacktype->tt_type == VAR_PARTIAL)
5139 && var_wrong_func_name(name, TRUE))
5140 goto theend;
5141
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005142 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005143 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005144 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005145 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005146 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005147 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005148 }
5149 else
5150 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005151 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005152 // for a variable that implies &t_any.
5153 if (stacktype == &t_list_empty)
5154 lvar->lv_type = &t_list_any;
5155 else if (stacktype == &t_dict_empty)
5156 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005157 else if (stacktype == &t_unknown)
5158 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005159 else
5160 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005161 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005162 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005163 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005164 {
5165 type_T *use_type = lvar->lv_type;
5166
Bram Moolenaar71abe482020-09-14 22:28:30 +02005167 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005168 if (has_index)
5169 {
5170 use_type = use_type->tt_member;
5171 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005172 // could be indexing "any"
5173 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005174 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005175 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005176 == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005177 goto theend;
5178 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005179 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005180 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005181 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005182 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005183 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005184 else if (cmdidx == CMD_final)
5185 {
5186 emsg(_(e_final_requires_a_value));
5187 goto theend;
5188 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005189 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005190 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005191 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005192 goto theend;
5193 }
5194 else if (!has_type || dest == dest_option)
5195 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005196 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005197 goto theend;
5198 }
5199 else
5200 {
5201 // variables are always initialized
5202 if (ga_grow(instr, 1) == FAIL)
5203 goto theend;
5204 switch (member_type->tt_type)
5205 {
5206 case VAR_BOOL:
5207 generate_PUSHBOOL(cctx, VVAL_FALSE);
5208 break;
5209 case VAR_FLOAT:
5210#ifdef FEAT_FLOAT
5211 generate_PUSHF(cctx, 0.0);
5212#endif
5213 break;
5214 case VAR_STRING:
5215 generate_PUSHS(cctx, NULL);
5216 break;
5217 case VAR_BLOB:
5218 generate_PUSHBLOB(cctx, NULL);
5219 break;
5220 case VAR_FUNC:
5221 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5222 break;
5223 case VAR_LIST:
5224 generate_NEWLIST(cctx, 0);
5225 break;
5226 case VAR_DICT:
5227 generate_NEWDICT(cctx, 0);
5228 break;
5229 case VAR_JOB:
5230 generate_PUSHJOB(cctx, NULL);
5231 break;
5232 case VAR_CHANNEL:
5233 generate_PUSHCHANNEL(cctx, NULL);
5234 break;
5235 case VAR_NUMBER:
5236 case VAR_UNKNOWN:
5237 case VAR_ANY:
5238 case VAR_PARTIAL:
5239 case VAR_VOID:
5240 case VAR_SPECIAL: // cannot happen
5241 generate_PUSHNR(cctx, 0);
5242 break;
5243 }
5244 }
5245 if (var_count == 0)
5246 end = p;
5247 }
5248
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005249 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005250 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005251 break;
5252
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005253 if (oplen > 0 && *op != '=')
5254 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005255 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005256 type_T *stacktype;
5257
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005258 if (*op == '.')
5259 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005260 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005261 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005262 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005263 if (
5264#ifdef FEAT_FLOAT
5265 // If variable is float operation with number is OK.
5266 !(expected == &t_float && stacktype == &t_number) &&
5267#endif
5268 need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005269 goto theend;
5270
5271 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005272 {
5273 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5274 goto theend;
5275 }
5276 else if (*op == '+')
5277 {
5278 if (generate_add_instr(cctx,
5279 operator_type(member_type, stacktype),
5280 member_type, stacktype) == FAIL)
5281 goto theend;
5282 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005283 else if (generate_two_op(cctx, op) == FAIL)
5284 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005285 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005286
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005287 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005288 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005289 int r;
5290
5291 // Compile the "idx" in "var[idx]".
5292 if (new_local)
5293 --cctx->ctx_locals.ga_len;
5294 p = skipwhite(var_start + varlen + 1);
5295 r = compile_expr0(&p, cctx);
5296 if (new_local)
5297 ++cctx->ctx_locals.ga_len;
5298 if (r == FAIL)
5299 goto theend;
5300 if (*skipwhite(p) != ']')
5301 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005302 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005303 emsg(_(e_missbrac));
5304 goto theend;
5305 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005306 if (type == &t_any)
5307 {
5308 type_T *idx_type = ((type_T **)stack->ga_data)[
5309 stack->ga_len - 1];
5310 // Index on variable of unknown type: guess the type from the
5311 // index type: number is dict, otherwise dict.
5312 // TODO: should do the assignment at runtime
5313 if (idx_type->tt_type == VAR_NUMBER)
5314 type = &t_list_any;
5315 else
5316 type = &t_dict_any;
5317 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005318 if (type->tt_type == VAR_DICT
5319 && may_generate_2STRING(-1, cctx) == FAIL)
5320 goto theend;
5321 if (type->tt_type == VAR_LIST
5322 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005323 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005324 {
5325 emsg(_(e_number_exp));
5326 goto theend;
5327 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005328
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005329 // Load the dict or list. On the stack we then have:
5330 // - value
5331 // - index
5332 // - variable
5333 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005334
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005335 if (type->tt_type == VAR_LIST)
5336 {
5337 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5338 return FAIL;
5339 }
5340 else if (type->tt_type == VAR_DICT)
5341 {
5342 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5343 return FAIL;
5344 }
5345 else
5346 {
5347 emsg(_(e_listreq));
5348 goto theend;
5349 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005350 }
5351 else
5352 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005353 if (is_decl && cmdidx == CMD_const
5354 && (dest == dest_script || dest == dest_local))
5355 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005356 generate_LOCKCONST(cctx);
5357
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005358 switch (dest)
5359 {
5360 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005361 generate_STOREOPT(cctx, skip_option_env_lead(name),
5362 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005363 break;
5364 case dest_global:
5365 // include g: with the name, easier to execute that way
5366 generate_STORE(cctx, ISN_STOREG, 0, name);
5367 break;
5368 case dest_buffer:
5369 // include b: with the name, easier to execute that way
5370 generate_STORE(cctx, ISN_STOREB, 0, name);
5371 break;
5372 case dest_window:
5373 // include w: with the name, easier to execute that way
5374 generate_STORE(cctx, ISN_STOREW, 0, name);
5375 break;
5376 case dest_tab:
5377 // include t: with the name, easier to execute that way
5378 generate_STORE(cctx, ISN_STORET, 0, name);
5379 break;
5380 case dest_env:
5381 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5382 break;
5383 case dest_reg:
5384 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5385 break;
5386 case dest_vimvar:
5387 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5388 break;
5389 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005390 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005391 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005392 {
5393 char_u *name_s = name;
5394
5395 // Include s: in the name for store_var()
5396 if (name[1] != ':')
5397 {
5398 int len = (int)STRLEN(name) + 3;
5399
5400 name_s = alloc(len);
5401 if (name_s == NULL)
5402 name_s = name;
5403 else
5404 vim_snprintf((char *)name_s, len,
5405 "s:%s", name);
5406 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005407 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5408 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005409 if (name_s != name)
5410 vim_free(name_s);
5411 }
5412 else
5413 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005414 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005415 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005416 break;
5417 case dest_local:
5418 if (lvar != NULL)
5419 {
5420 isn_T *isn = ((isn_T *)instr->ga_data)
5421 + instr->ga_len - 1;
5422
5423 // optimization: turn "var = 123" from ISN_PUSHNR +
5424 // ISN_STORE into ISN_STORENR
5425 if (!lvar->lv_from_outer
5426 && instr->ga_len == instr_count + 1
5427 && isn->isn_type == ISN_PUSHNR)
5428 {
5429 varnumber_T val = isn->isn_arg.number;
5430
5431 isn->isn_type = ISN_STORENR;
5432 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5433 isn->isn_arg.storenr.stnr_val = val;
5434 if (stack->ga_len > 0)
5435 --stack->ga_len;
5436 }
5437 else if (lvar->lv_from_outer)
5438 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005439 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005440 else
5441 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5442 }
5443 break;
5444 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005445 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005446
5447 if (var_idx + 1 < var_count)
5448 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005449 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005450
5451 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005452 if (var_count > 0 && !semicolon)
5453 {
5454 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5455 goto theend;
5456 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005457
Bram Moolenaarb2097502020-07-19 17:17:02 +02005458 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005459
5460theend:
5461 vim_free(name);
5462 return ret;
5463}
5464
5465/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005466 * Check if "name" can be "unlet".
5467 */
5468 int
5469check_vim9_unlet(char_u *name)
5470{
5471 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5472 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005473 // "unlet s:var" is allowed in legacy script.
5474 if (*name == 's' && !script_is_vim9())
5475 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005476 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005477 return FAIL;
5478 }
5479 return OK;
5480}
5481
5482/*
5483 * Callback passed to ex_unletlock().
5484 */
5485 static int
5486compile_unlet(
5487 lval_T *lvp,
5488 char_u *name_end,
5489 exarg_T *eap,
5490 int deep UNUSED,
5491 void *coookie)
5492{
5493 cctx_T *cctx = coookie;
5494
5495 if (lvp->ll_tv == NULL)
5496 {
5497 char_u *p = lvp->ll_name;
5498 int cc = *name_end;
5499 int ret = OK;
5500
5501 // Normal name. Only supports g:, w:, t: and b: namespaces.
5502 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005503 if (*p == '$')
5504 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5505 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005506 ret = FAIL;
5507 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005508 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005509
5510 *name_end = cc;
5511 return ret;
5512 }
5513
5514 // TODO: unlet {list}[idx]
5515 // TODO: unlet {dict}[key]
5516 emsg("Sorry, :unlet not fully implemented yet");
5517 return FAIL;
5518}
5519
5520/*
5521 * compile "unlet var", "lock var" and "unlock var"
5522 * "arg" points to "var".
5523 */
5524 static char_u *
5525compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5526{
5527 char_u *p = arg;
5528
5529 if (eap->cmdidx != CMD_unlet)
5530 {
5531 emsg("Sorry, :lock and unlock not implemented yet");
5532 return NULL;
5533 }
5534
5535 if (*p == '!')
5536 {
5537 p = skipwhite(p + 1);
5538 eap->forceit = TRUE;
5539 }
5540
5541 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5542 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5543}
5544
5545/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005546 * Compile an :import command.
5547 */
5548 static char_u *
5549compile_import(char_u *arg, cctx_T *cctx)
5550{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005551 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005552}
5553
5554/*
5555 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5556 */
5557 static int
5558compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5559{
5560 garray_T *instr = &cctx->ctx_instr;
5561 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5562
5563 if (endlabel == NULL)
5564 return FAIL;
5565 endlabel->el_next = *el;
5566 *el = endlabel;
5567 endlabel->el_end_label = instr->ga_len;
5568
5569 generate_JUMP(cctx, when, 0);
5570 return OK;
5571}
5572
5573 static void
5574compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5575{
5576 garray_T *instr = &cctx->ctx_instr;
5577
5578 while (*el != NULL)
5579 {
5580 endlabel_T *cur = (*el);
5581 isn_T *isn;
5582
5583 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5584 isn->isn_arg.jump.jump_where = instr->ga_len;
5585 *el = cur->el_next;
5586 vim_free(cur);
5587 }
5588}
5589
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005590 static void
5591compile_free_jump_to_end(endlabel_T **el)
5592{
5593 while (*el != NULL)
5594 {
5595 endlabel_T *cur = (*el);
5596
5597 *el = cur->el_next;
5598 vim_free(cur);
5599 }
5600}
5601
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005602/*
5603 * Create a new scope and set up the generic items.
5604 */
5605 static scope_T *
5606new_scope(cctx_T *cctx, scopetype_T type)
5607{
5608 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5609
5610 if (scope == NULL)
5611 return NULL;
5612 scope->se_outer = cctx->ctx_scope;
5613 cctx->ctx_scope = scope;
5614 scope->se_type = type;
5615 scope->se_local_count = cctx->ctx_locals.ga_len;
5616 return scope;
5617}
5618
5619/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005620 * Free the current scope and go back to the outer scope.
5621 */
5622 static void
5623drop_scope(cctx_T *cctx)
5624{
5625 scope_T *scope = cctx->ctx_scope;
5626
5627 if (scope == NULL)
5628 {
5629 iemsg("calling drop_scope() without a scope");
5630 return;
5631 }
5632 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005633 switch (scope->se_type)
5634 {
5635 case IF_SCOPE:
5636 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5637 case FOR_SCOPE:
5638 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5639 case WHILE_SCOPE:
5640 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5641 case TRY_SCOPE:
5642 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5643 case NO_SCOPE:
5644 case BLOCK_SCOPE:
5645 break;
5646 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005647 vim_free(scope);
5648}
5649
5650/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005651 * Check that the top of the type stack has a type that can be used as a
5652 * condition. Give an error and return FAIL if not.
5653 */
5654 static int
5655bool_on_stack(cctx_T *cctx)
5656{
5657 garray_T *stack = &cctx->ctx_type_stack;
5658 type_T *type;
5659
5660 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5661 if (type != &t_bool && type != &t_number && type != &t_any
5662 && need_type(type, &t_bool, -1, cctx, FALSE) == FAIL)
5663 return FAIL;
5664 return OK;
5665}
5666
5667/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005668 * compile "if expr"
5669 *
5670 * "if expr" Produces instructions:
5671 * EVAL expr Push result of "expr"
5672 * JUMP_IF_FALSE end
5673 * ... body ...
5674 * end:
5675 *
5676 * "if expr | else" Produces instructions:
5677 * EVAL expr Push result of "expr"
5678 * JUMP_IF_FALSE else
5679 * ... body ...
5680 * JUMP_ALWAYS end
5681 * else:
5682 * ... body ...
5683 * end:
5684 *
5685 * "if expr1 | elseif expr2 | else" Produces instructions:
5686 * EVAL expr Push result of "expr"
5687 * JUMP_IF_FALSE elseif
5688 * ... body ...
5689 * JUMP_ALWAYS end
5690 * elseif:
5691 * EVAL expr Push result of "expr"
5692 * JUMP_IF_FALSE else
5693 * ... body ...
5694 * JUMP_ALWAYS end
5695 * else:
5696 * ... body ...
5697 * end:
5698 */
5699 static char_u *
5700compile_if(char_u *arg, cctx_T *cctx)
5701{
5702 char_u *p = arg;
5703 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005704 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005705 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005706 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005707 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005708
Bram Moolenaara5565e42020-05-09 15:44:01 +02005709 CLEAR_FIELD(ppconst);
5710 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005711 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005712 clear_ppconst(&ppconst);
5713 return NULL;
5714 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005715 if (cctx->ctx_skip == SKIP_YES)
5716 clear_ppconst(&ppconst);
5717 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005718 {
Bram Moolenaar13106602020-10-04 16:06:05 +02005719 int error = FALSE;
5720 int v;
5721
Bram Moolenaara5565e42020-05-09 15:44:01 +02005722 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02005723 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
5724 if (error)
5725 return NULL;
5726 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005727 clear_ppconst(&ppconst);
5728 }
5729 else
5730 {
5731 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005732 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005733 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005734 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02005735 if (bool_on_stack(cctx) == FAIL)
5736 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005737 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005738
5739 scope = new_scope(cctx, IF_SCOPE);
5740 if (scope == NULL)
5741 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005742 scope->se_skip_save = skip_save;
5743 // "is_had_return" will be reset if any block does not end in :return
5744 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005745
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005746 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005747 {
5748 // "where" is set when ":elseif", "else" or ":endif" is found
5749 scope->se_u.se_if.is_if_label = instr->ga_len;
5750 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5751 }
5752 else
5753 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005754
5755 return p;
5756}
5757
5758 static char_u *
5759compile_elseif(char_u *arg, cctx_T *cctx)
5760{
5761 char_u *p = arg;
5762 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005763 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005764 isn_T *isn;
5765 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005766 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02005767 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005768
5769 if (scope == NULL || scope->se_type != IF_SCOPE)
5770 {
5771 emsg(_(e_elseif_without_if));
5772 return NULL;
5773 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005774 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005775 if (!cctx->ctx_had_return)
5776 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005777
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005778 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005779 {
5780 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005781 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005782 return NULL;
5783 // previous "if" or "elseif" jumps here
5784 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5785 isn->isn_arg.jump.jump_where = instr->ga_len;
5786 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005787
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005788 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005789 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02005790 if (cctx->ctx_skip == SKIP_YES)
5791 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005792 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005793 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005794 clear_ppconst(&ppconst);
5795 return NULL;
5796 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02005797 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005798 if (scope->se_skip_save == SKIP_YES)
5799 clear_ppconst(&ppconst);
5800 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005801 {
Bram Moolenaar13106602020-10-04 16:06:05 +02005802 int error = FALSE;
5803 int v;
5804
Bram Moolenaar7f141552020-05-09 17:35:53 +02005805 // The expression results in a constant.
5806 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02005807 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
5808 if (error)
5809 return NULL;
5810 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005811 clear_ppconst(&ppconst);
5812 scope->se_u.se_if.is_if_label = -1;
5813 }
5814 else
5815 {
5816 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005817 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005818 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005819 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02005820 if (bool_on_stack(cctx) == FAIL)
5821 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005822
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005823 // "where" is set when ":elseif", "else" or ":endif" is found
5824 scope->se_u.se_if.is_if_label = instr->ga_len;
5825 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5826 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005827
5828 return p;
5829}
5830
5831 static char_u *
5832compile_else(char_u *arg, cctx_T *cctx)
5833{
5834 char_u *p = arg;
5835 garray_T *instr = &cctx->ctx_instr;
5836 isn_T *isn;
5837 scope_T *scope = cctx->ctx_scope;
5838
5839 if (scope == NULL || scope->se_type != IF_SCOPE)
5840 {
5841 emsg(_(e_else_without_if));
5842 return NULL;
5843 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005844 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005845 if (!cctx->ctx_had_return)
5846 scope->se_u.se_if.is_had_return = FALSE;
5847 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005848
Bram Moolenaarefd88552020-06-18 20:50:10 +02005849 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005850 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005851 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005852 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005853 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005854 if (!cctx->ctx_had_return
5855 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5856 JUMP_ALWAYS, cctx) == FAIL)
5857 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005858 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005859
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005860 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005861 {
5862 if (scope->se_u.se_if.is_if_label >= 0)
5863 {
5864 // previous "if" or "elseif" jumps here
5865 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5866 isn->isn_arg.jump.jump_where = instr->ga_len;
5867 scope->se_u.se_if.is_if_label = -1;
5868 }
5869 }
5870
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005871 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005872 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5873 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005874
5875 return p;
5876}
5877
5878 static char_u *
5879compile_endif(char_u *arg, cctx_T *cctx)
5880{
5881 scope_T *scope = cctx->ctx_scope;
5882 ifscope_T *ifscope;
5883 garray_T *instr = &cctx->ctx_instr;
5884 isn_T *isn;
5885
5886 if (scope == NULL || scope->se_type != IF_SCOPE)
5887 {
5888 emsg(_(e_endif_without_if));
5889 return NULL;
5890 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005891 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005892 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005893 if (!cctx->ctx_had_return)
5894 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005895
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005896 if (scope->se_u.se_if.is_if_label >= 0)
5897 {
5898 // previous "if" or "elseif" jumps here
5899 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5900 isn->isn_arg.jump.jump_where = instr->ga_len;
5901 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005902 // Fill in the "end" label in jumps at the end of the blocks.
5903 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005904 cctx->ctx_skip = scope->se_skip_save;
5905
5906 // If all the blocks end in :return and there is an :else then the
5907 // had_return flag is set.
5908 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005909
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005910 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005911 return arg;
5912}
5913
5914/*
5915 * compile "for var in expr"
5916 *
5917 * Produces instructions:
5918 * PUSHNR -1
5919 * STORE loop-idx Set index to -1
5920 * EVAL expr Push result of "expr"
5921 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5922 * - if beyond end, jump to "end"
5923 * - otherwise get item from list and push it
5924 * STORE var Store item in "var"
5925 * ... body ...
5926 * JUMP top Jump back to repeat
5927 * end: DROP Drop the result of "expr"
5928 *
5929 */
5930 static char_u *
5931compile_for(char_u *arg, cctx_T *cctx)
5932{
5933 char_u *p;
5934 size_t varlen;
5935 garray_T *instr = &cctx->ctx_instr;
5936 garray_T *stack = &cctx->ctx_type_stack;
5937 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005938 lvar_T *loop_lvar; // loop iteration variable
5939 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005940 type_T *vartype;
5941
5942 // TODO: list of variables: "for [key, value] in dict"
5943 // parse "var"
5944 for (p = arg; eval_isnamec1(*p); ++p)
5945 ;
5946 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005947 var_lvar = lookup_local(arg, varlen, cctx);
5948 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005949 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005950 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005951 return NULL;
5952 }
5953
5954 // consume "in"
5955 p = skipwhite(p);
5956 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5957 {
5958 emsg(_(e_missing_in));
5959 return NULL;
5960 }
5961 p = skipwhite(p + 2);
5962
5963
5964 scope = new_scope(cctx, FOR_SCOPE);
5965 if (scope == NULL)
5966 return NULL;
5967
5968 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005969 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5970 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005971 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005972 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005973 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005974 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005975 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005976
5977 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005978 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5979 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005980 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005981 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005982 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005983 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005984 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005985
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005986 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005987
5988 // compile "expr", it remains on the stack until "endfor"
5989 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005990 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005991 {
5992 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005993 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005994 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005995
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005996 // Now that we know the type of "var", check that it is a list, now or at
5997 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005998 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005999 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006000 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006001 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006002 return NULL;
6003 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006004 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006005 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006006
6007 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006008 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006009
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006010 generate_FOR(cctx, loop_lvar->lv_idx);
6011 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006012
6013 return arg;
6014}
6015
6016/*
6017 * compile "endfor"
6018 */
6019 static char_u *
6020compile_endfor(char_u *arg, cctx_T *cctx)
6021{
6022 garray_T *instr = &cctx->ctx_instr;
6023 scope_T *scope = cctx->ctx_scope;
6024 forscope_T *forscope;
6025 isn_T *isn;
6026
6027 if (scope == NULL || scope->se_type != FOR_SCOPE)
6028 {
6029 emsg(_(e_for));
6030 return NULL;
6031 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006032 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006033 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006034 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006035
6036 // At end of ":for" scope jump back to the FOR instruction.
6037 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6038
6039 // Fill in the "end" label in the FOR statement so it can jump here
6040 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6041 isn->isn_arg.forloop.for_end = instr->ga_len;
6042
6043 // Fill in the "end" label any BREAK statements
6044 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6045
6046 // Below the ":for" scope drop the "expr" list from the stack.
6047 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6048 return NULL;
6049
6050 vim_free(scope);
6051
6052 return arg;
6053}
6054
6055/*
6056 * compile "while expr"
6057 *
6058 * Produces instructions:
6059 * top: EVAL expr Push result of "expr"
6060 * JUMP_IF_FALSE end jump if false
6061 * ... body ...
6062 * JUMP top Jump back to repeat
6063 * end:
6064 *
6065 */
6066 static char_u *
6067compile_while(char_u *arg, cctx_T *cctx)
6068{
6069 char_u *p = arg;
6070 garray_T *instr = &cctx->ctx_instr;
6071 scope_T *scope;
6072
6073 scope = new_scope(cctx, WHILE_SCOPE);
6074 if (scope == NULL)
6075 return NULL;
6076
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006077 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006078
6079 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006080 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006081 return NULL;
6082
Bram Moolenaar13106602020-10-04 16:06:05 +02006083 if (bool_on_stack(cctx) == FAIL)
6084 return FAIL;
6085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006086 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006087 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006088 JUMP_IF_FALSE, cctx) == FAIL)
6089 return FAIL;
6090
6091 return p;
6092}
6093
6094/*
6095 * compile "endwhile"
6096 */
6097 static char_u *
6098compile_endwhile(char_u *arg, cctx_T *cctx)
6099{
6100 scope_T *scope = cctx->ctx_scope;
6101
6102 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6103 {
6104 emsg(_(e_while));
6105 return NULL;
6106 }
6107 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006108 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006109
6110 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006111 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006112
6113 // Fill in the "end" label in the WHILE statement so it can jump here.
6114 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006115 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006116
6117 vim_free(scope);
6118
6119 return arg;
6120}
6121
6122/*
6123 * compile "continue"
6124 */
6125 static char_u *
6126compile_continue(char_u *arg, cctx_T *cctx)
6127{
6128 scope_T *scope = cctx->ctx_scope;
6129
6130 for (;;)
6131 {
6132 if (scope == NULL)
6133 {
6134 emsg(_(e_continue));
6135 return NULL;
6136 }
6137 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6138 break;
6139 scope = scope->se_outer;
6140 }
6141
6142 // Jump back to the FOR or WHILE instruction.
6143 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006144 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6145 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006146 return arg;
6147}
6148
6149/*
6150 * compile "break"
6151 */
6152 static char_u *
6153compile_break(char_u *arg, cctx_T *cctx)
6154{
6155 scope_T *scope = cctx->ctx_scope;
6156 endlabel_T **el;
6157
6158 for (;;)
6159 {
6160 if (scope == NULL)
6161 {
6162 emsg(_(e_break));
6163 return NULL;
6164 }
6165 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6166 break;
6167 scope = scope->se_outer;
6168 }
6169
6170 // Jump to the end of the FOR or WHILE loop.
6171 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006172 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006173 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006174 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006175 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6176 return FAIL;
6177
6178 return arg;
6179}
6180
6181/*
6182 * compile "{" start of block
6183 */
6184 static char_u *
6185compile_block(char_u *arg, cctx_T *cctx)
6186{
6187 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6188 return NULL;
6189 return skipwhite(arg + 1);
6190}
6191
6192/*
6193 * compile end of block: drop one scope
6194 */
6195 static void
6196compile_endblock(cctx_T *cctx)
6197{
6198 scope_T *scope = cctx->ctx_scope;
6199
6200 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006201 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006202 vim_free(scope);
6203}
6204
6205/*
6206 * compile "try"
6207 * Creates a new scope for the try-endtry, pointing to the first catch and
6208 * finally.
6209 * Creates another scope for the "try" block itself.
6210 * TRY instruction sets up exception handling at runtime.
6211 *
6212 * "try"
6213 * TRY -> catch1, -> finally push trystack entry
6214 * ... try block
6215 * "throw {exception}"
6216 * EVAL {exception}
6217 * THROW create exception
6218 * ... try block
6219 * " catch {expr}"
6220 * JUMP -> finally
6221 * catch1: PUSH exeception
6222 * EVAL {expr}
6223 * MATCH
6224 * JUMP nomatch -> catch2
6225 * CATCH remove exception
6226 * ... catch block
6227 * " catch"
6228 * JUMP -> finally
6229 * catch2: CATCH remove exception
6230 * ... catch block
6231 * " finally"
6232 * finally:
6233 * ... finally block
6234 * " endtry"
6235 * ENDTRY pop trystack entry, may rethrow
6236 */
6237 static char_u *
6238compile_try(char_u *arg, cctx_T *cctx)
6239{
6240 garray_T *instr = &cctx->ctx_instr;
6241 scope_T *try_scope;
6242 scope_T *scope;
6243
6244 // scope that holds the jumps that go to catch/finally/endtry
6245 try_scope = new_scope(cctx, TRY_SCOPE);
6246 if (try_scope == NULL)
6247 return NULL;
6248
6249 // "catch" is set when the first ":catch" is found.
6250 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006251 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006252 if (generate_instr(cctx, ISN_TRY) == NULL)
6253 return NULL;
6254
6255 // scope for the try block itself
6256 scope = new_scope(cctx, BLOCK_SCOPE);
6257 if (scope == NULL)
6258 return NULL;
6259
6260 return arg;
6261}
6262
6263/*
6264 * compile "catch {expr}"
6265 */
6266 static char_u *
6267compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6268{
6269 scope_T *scope = cctx->ctx_scope;
6270 garray_T *instr = &cctx->ctx_instr;
6271 char_u *p;
6272 isn_T *isn;
6273
6274 // end block scope from :try or :catch
6275 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6276 compile_endblock(cctx);
6277 scope = cctx->ctx_scope;
6278
6279 // Error if not in a :try scope
6280 if (scope == NULL || scope->se_type != TRY_SCOPE)
6281 {
6282 emsg(_(e_catch));
6283 return NULL;
6284 }
6285
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006286 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006287 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006288 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006289 return NULL;
6290 }
6291
6292 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006293 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006294 JUMP_ALWAYS, cctx) == FAIL)
6295 return NULL;
6296
6297 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006298 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006299 if (isn->isn_arg.try.try_catch == 0)
6300 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006301 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006302 {
6303 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006304 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006305 isn->isn_arg.jump.jump_where = instr->ga_len;
6306 }
6307
6308 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006309 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006310 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006311 scope->se_u.se_try.ts_caught_all = TRUE;
6312 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006313 }
6314 else
6315 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006316 char_u *end;
6317 char_u *pat;
6318 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006319 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006320 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006321
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006322 // Push v:exception, push {expr} and MATCH
6323 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6324
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006325 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006326 if (*end != *p)
6327 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006328 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006329 vim_free(tofree);
6330 return FAIL;
6331 }
6332 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006333 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006334 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006335 len = (int)(end - tofree);
6336 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006337 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006338 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006339 if (pat == NULL)
6340 return FAIL;
6341 if (generate_PUSHS(cctx, pat) == FAIL)
6342 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006343
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006344 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6345 return NULL;
6346
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006347 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006348 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6349 return NULL;
6350 }
6351
6352 if (generate_instr(cctx, ISN_CATCH) == NULL)
6353 return NULL;
6354
6355 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6356 return NULL;
6357 return p;
6358}
6359
6360 static char_u *
6361compile_finally(char_u *arg, cctx_T *cctx)
6362{
6363 scope_T *scope = cctx->ctx_scope;
6364 garray_T *instr = &cctx->ctx_instr;
6365 isn_T *isn;
6366
6367 // end block scope from :try or :catch
6368 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6369 compile_endblock(cctx);
6370 scope = cctx->ctx_scope;
6371
6372 // Error if not in a :try scope
6373 if (scope == NULL || scope->se_type != TRY_SCOPE)
6374 {
6375 emsg(_(e_finally));
6376 return NULL;
6377 }
6378
6379 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006380 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006381 if (isn->isn_arg.try.try_finally != 0)
6382 {
6383 emsg(_(e_finally_dup));
6384 return NULL;
6385 }
6386
6387 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006388 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006389
Bram Moolenaar585fea72020-04-02 22:33:21 +02006390 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006391 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006392 {
6393 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006394 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006395 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006396 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006397 }
6398
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006399 // TODO: set index in ts_finally_label jumps
6400
6401 return arg;
6402}
6403
6404 static char_u *
6405compile_endtry(char_u *arg, cctx_T *cctx)
6406{
6407 scope_T *scope = cctx->ctx_scope;
6408 garray_T *instr = &cctx->ctx_instr;
6409 isn_T *isn;
6410
6411 // end block scope from :catch or :finally
6412 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6413 compile_endblock(cctx);
6414 scope = cctx->ctx_scope;
6415
6416 // Error if not in a :try scope
6417 if (scope == NULL || scope->se_type != TRY_SCOPE)
6418 {
6419 if (scope == NULL)
6420 emsg(_(e_no_endtry));
6421 else if (scope->se_type == WHILE_SCOPE)
6422 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006423 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006424 emsg(_(e_endfor));
6425 else
6426 emsg(_(e_endif));
6427 return NULL;
6428 }
6429
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006430 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006431 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6432 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006433 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006434 return NULL;
6435 }
6436
6437 // Fill in the "end" label in jumps at the end of the blocks, if not done
6438 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006439 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006440
6441 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006442 if (isn->isn_arg.try.try_catch == 0)
6443 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006444 if (isn->isn_arg.try.try_finally == 0)
6445 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006446
6447 if (scope->se_u.se_try.ts_catch_label != 0)
6448 {
6449 // Last catch without match jumps here
6450 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6451 isn->isn_arg.jump.jump_where = instr->ga_len;
6452 }
6453
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006454 compile_endblock(cctx);
6455
6456 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6457 return NULL;
6458 return arg;
6459}
6460
6461/*
6462 * compile "throw {expr}"
6463 */
6464 static char_u *
6465compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6466{
6467 char_u *p = skipwhite(arg);
6468
Bram Moolenaara5565e42020-05-09 15:44:01 +02006469 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006470 return NULL;
6471 if (may_generate_2STRING(-1, cctx) == FAIL)
6472 return NULL;
6473 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6474 return NULL;
6475
6476 return p;
6477}
6478
6479/*
6480 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006481 * compile "echomsg expr"
6482 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006483 * compile "execute expr"
6484 */
6485 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006486compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006487{
6488 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006489 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006490 int count = 0;
6491
6492 for (;;)
6493 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006494 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006495 return NULL;
6496 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006497 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006498 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006499 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006500 break;
6501 }
6502
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006503 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6504 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6505 else if (cmdidx == CMD_execute)
6506 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6507 else if (cmdidx == CMD_echomsg)
6508 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6509 else
6510 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006511 return p;
6512}
6513
6514/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006515 * :put r
6516 * :put ={expr}
6517 */
6518 static char_u *
6519compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6520{
6521 char_u *line = arg;
6522 linenr_T lnum;
6523 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006524 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006525
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006526 eap->regname = *line;
6527
6528 if (eap->regname == '=')
6529 {
6530 char_u *p = line + 1;
6531
6532 if (compile_expr0(&p, cctx) == FAIL)
6533 return NULL;
6534 line = p;
6535 }
6536 else if (eap->regname != NUL)
6537 ++line;
6538
6539 // TODO: if the range is something like "$" need to evaluate at runtime
6540 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6541 return NULL;
6542 if (eap->addr_count == 0)
6543 lnum = -1;
6544 else
6545 lnum = eap->line2;
6546 if (above)
6547 --lnum;
6548
6549 generate_PUT(cctx, eap->regname, lnum);
6550 return line;
6551}
6552
6553/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006554 * A command that is not compiled, execute with legacy code.
6555 */
6556 static char_u *
6557compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6558{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006559 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006560 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006561 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006562
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006563 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006564 goto theend;
6565
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006566 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006567 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006568 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006569 int usefilter = FALSE;
6570
6571 has_expr = argt & (EX_XFILE | EX_EXPAND);
6572
6573 // If the command can be followed by a bar, find the bar and truncate
6574 // it, so that the following command can be compiled.
6575 // The '|' is overwritten with a NUL, it is put back below.
6576 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6577 && *eap->arg == '!')
6578 // :w !filter or :r !filter or :r! filter
6579 usefilter = TRUE;
6580 if ((argt & EX_TRLBAR) && !usefilter)
6581 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006582 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006583 separate_nextcmd(eap);
6584 if (eap->nextcmd != NULL)
6585 nextcmd = eap->nextcmd;
6586 }
6587 }
6588
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006589 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6590 {
6591 // expand filename in "syntax include [@group] filename"
6592 has_expr = TRUE;
6593 eap->arg = skipwhite(eap->arg + 7);
6594 if (*eap->arg == '@')
6595 eap->arg = skiptowhite(eap->arg);
6596 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006597
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006598 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006599 {
6600 int count = 0;
6601 char_u *start = skipwhite(line);
6602
6603 // :cmd xxx`=expr1`yyy`=expr2`zzz
6604 // PUSHS ":cmd xxx"
6605 // eval expr1
6606 // PUSHS "yyy"
6607 // eval expr2
6608 // PUSHS "zzz"
6609 // EXECCONCAT 5
6610 for (;;)
6611 {
6612 if (p > start)
6613 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006614 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006615 ++count;
6616 }
6617 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006618 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006619 return NULL;
6620 may_generate_2STRING(-1, cctx);
6621 ++count;
6622 p = skipwhite(p);
6623 if (*p != '`')
6624 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006625 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006626 return NULL;
6627 }
6628 start = p + 1;
6629
6630 p = (char_u *)strstr((char *)start, "`=");
6631 if (p == NULL)
6632 {
6633 if (*skipwhite(start) != NUL)
6634 {
6635 generate_PUSHS(cctx, vim_strsave(start));
6636 ++count;
6637 }
6638 break;
6639 }
6640 }
6641 generate_EXECCONCAT(cctx, count);
6642 }
6643 else
6644 generate_EXEC(cctx, line);
6645
6646theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006647 if (*nextcmd != NUL)
6648 {
6649 // the parser expects a pointer to the bar, put it back
6650 --nextcmd;
6651 *nextcmd = '|';
6652 }
6653
6654 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006655}
6656
6657/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006658 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006659 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006660 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006661 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006662add_def_function(ufunc_T *ufunc)
6663{
6664 dfunc_T *dfunc;
6665
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006666 if (def_functions.ga_len == 0)
6667 {
6668 // The first position is not used, so that a zero uf_dfunc_idx means it
6669 // wasn't set.
6670 if (ga_grow(&def_functions, 1) == FAIL)
6671 return FAIL;
6672 ++def_functions.ga_len;
6673 }
6674
Bram Moolenaar09689a02020-05-09 22:50:08 +02006675 // Add the function to "def_functions".
6676 if (ga_grow(&def_functions, 1) == FAIL)
6677 return FAIL;
6678 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6679 CLEAR_POINTER(dfunc);
6680 dfunc->df_idx = def_functions.ga_len;
6681 ufunc->uf_dfunc_idx = dfunc->df_idx;
6682 dfunc->df_ufunc = ufunc;
6683 ++def_functions.ga_len;
6684 return OK;
6685}
6686
6687/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006688 * After ex_function() has collected all the function lines: parse and compile
6689 * the lines into instructions.
6690 * Adds the function to "def_functions".
6691 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6692 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006693 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006694 * This can be used recursively through compile_lambda(), which may reallocate
6695 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006696 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006697 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006698 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006699compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006700{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006701 char_u *line = NULL;
6702 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006703 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006704 cctx_T cctx;
6705 garray_T *instr;
6706 int called_emsg_before = called_emsg;
6707 int ret = FAIL;
6708 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006709 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006710 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006711 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006712
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006713 // When using a function that was compiled before: Free old instructions.
6714 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006715 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006716 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006717 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6718 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006719 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006720 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006721 else
6722 {
6723 if (add_def_function(ufunc) == FAIL)
6724 return FAIL;
6725 new_def_function = TRUE;
6726 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006727
Bram Moolenaar985116a2020-07-12 17:31:09 +02006728 ufunc->uf_def_status = UF_COMPILING;
6729
Bram Moolenaara80faa82020-04-12 19:37:17 +02006730 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006731 cctx.ctx_ufunc = ufunc;
6732 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006733 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006734 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6735 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6736 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6737 cctx.ctx_type_list = &ufunc->uf_type_list;
6738 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6739 instr = &cctx.ctx_instr;
6740
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006741 // Set the context to the function, it may be compiled when called from
6742 // another script. Set the script version to the most modern one.
6743 // The line number will be set in next_line_from_context().
6744 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006745 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6746
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006747 // Make sure error messages are OK.
6748 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6749 if (do_estack_push)
6750 estack_push_ufunc(ufunc, 1);
6751
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006752 if (ufunc->uf_def_args.ga_len > 0)
6753 {
6754 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006755 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006756 int i;
6757 char_u *arg;
6758 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006759 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006760
6761 // Produce instructions for the default values of optional arguments.
6762 // Store the instruction index in uf_def_arg_idx[] so that we know
6763 // where to start when the function is called, depending on the number
6764 // of arguments.
6765 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6766 if (ufunc->uf_def_arg_idx == NULL)
6767 goto erret;
6768 for (i = 0; i < count; ++i)
6769 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006770 garray_T *stack = &cctx.ctx_type_stack;
6771 type_T *val_type;
6772 int arg_idx = first_def_arg + i;
6773
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006774 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6775 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006776 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006777 goto erret;
6778
6779 // If no type specified use the type of the default value.
6780 // Otherwise check that the default value type matches the
6781 // specified type.
6782 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6783 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006784 {
6785 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006786 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006787 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02006788 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
6789 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006790 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006791
6792 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006793 goto erret;
6794 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006795 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006796
6797 if (did_set_arg_type)
6798 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006799 }
6800
6801 /*
6802 * Loop over all the lines of the function and generate instructions.
6803 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006804 for (;;)
6805 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006806 exarg_T ea;
6807 cmdmod_T save_cmdmod;
6808 int starts_with_colon = FALSE;
6809 char_u *cmd;
6810 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006811
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006812 // Bail out on the first error to avoid a flood of errors and report
6813 // the right line number when inside try/catch.
6814 if (emsg_before != called_emsg)
6815 goto erret;
6816
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006817 if (line != NULL && *line == '|')
6818 // the line continues after a '|'
6819 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006820 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006821 && !(*line == '#' && (line == cctx.ctx_line_start
6822 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006823 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006824 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006825 goto erret;
6826 }
6827 else
6828 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006829 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006830 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006831 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006832 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006833 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006834 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006835
Bram Moolenaara80faa82020-04-12 19:37:17 +02006836 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006837 ea.cmdlinep = &line;
6838 ea.cmd = skipwhite(line);
6839
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006840 // Some things can be recognized by the first character.
6841 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006842 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006843 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006844 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006845 if (ea.cmd[1] != '{')
6846 {
6847 line = (char_u *)"";
6848 continue;
6849 }
6850 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006851
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006852 case '}':
6853 {
6854 // "}" ends a block scope
6855 scopetype_T stype = cctx.ctx_scope == NULL
6856 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006857
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006858 if (stype == BLOCK_SCOPE)
6859 {
6860 compile_endblock(&cctx);
6861 line = ea.cmd;
6862 }
6863 else
6864 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006865 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006866 goto erret;
6867 }
6868 if (line != NULL)
6869 line = skipwhite(ea.cmd + 1);
6870 continue;
6871 }
6872
6873 case '{':
6874 // "{" starts a block scope
6875 // "{'a': 1}->func() is something else
6876 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6877 {
6878 line = compile_block(ea.cmd, &cctx);
6879 continue;
6880 }
6881 break;
6882
6883 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006884 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006885 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006886 }
6887
6888 /*
6889 * COMMAND MODIFIERS
6890 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006891 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006892 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6893 {
6894 if (errormsg != NULL)
6895 goto erret;
6896 // empty line or comment
6897 line = (char_u *)"";
6898 continue;
6899 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006900 // TODO: use modifiers in the command
6901 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006902 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006903
6904 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006905 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006906 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006907 {
6908 if (*ea.cmd == '(')
6909 // not for "call()"
6910 ea.cmd = p;
6911 else
6912 ea.cmd = skipwhite(ea.cmd);
6913 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006914
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006915 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006916 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006917 char_u *pskip;
6918
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006919 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006920 // find what follows.
6921 // Skip over "var.member", "var[idx]" and the like.
6922 // Also "&opt = val", "$ENV = val" and "@r = val".
6923 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006924 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006925 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006926 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006927 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006928 char_u *var_end;
6929 int oplen;
6930 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006931
Bram Moolenaar65821722020-08-02 18:58:54 +02006932 if (ea.cmd[0] == '@')
6933 var_end = ea.cmd + 2;
6934 else
6935 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006936 FNE_CHECK_START | FNE_INCL_BR);
6937 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006938 if (oplen > 0)
6939 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006940 size_t len = p - ea.cmd;
6941
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006942 // Recognize an assignment if we recognize the variable
6943 // name:
6944 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006945 // "local = expr" where "local" is a local var.
6946 // "script = expr" where "script" is a script-local var.
6947 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006948 // "&opt = expr"
6949 // "$ENV = expr"
6950 // "@r = expr"
6951 if (*ea.cmd == '&'
6952 || *ea.cmd == '$'
6953 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006954 || ((len) > 2 && ea.cmd[1] == ':')
6955 || lookup_local(ea.cmd, len, &cctx) != NULL
6956 || lookup_arg(ea.cmd, len, NULL, NULL,
6957 NULL, &cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02006958 || lookup_script(ea.cmd, len, FALSE) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006959 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006960 {
6961 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006962 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006963 goto erret;
6964 continue;
6965 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006966 }
6967 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006968
6969 if (*ea.cmd == '[')
6970 {
6971 // [var, var] = expr
6972 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6973 if (line == NULL)
6974 goto erret;
6975 if (line != ea.cmd)
6976 continue;
6977 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006978 }
6979
6980 /*
6981 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006982 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006983 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006984 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006985 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006986 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02006987 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006988 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006989 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006990 if (!starts_with_colon)
6991 {
6992 emsg(_(e_colon_required_before_a_range));
6993 goto erret;
6994 }
6995 if (ends_excmd2(line, ea.cmd))
6996 {
6997 // A range without a command: jump to the line.
6998 // TODO: compile to a more efficient command, possibly
6999 // calling parse_cmd_address().
7000 ea.cmdidx = CMD_SIZE;
7001 line = compile_exec(line, &ea, &cctx);
7002 goto nextline;
7003 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007004 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007005 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007006 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007007 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007008 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007009
7010 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7011 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007012 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007013 {
7014 line += STRLEN(line);
7015 continue;
7016 }
7017
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007018 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007019 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007020 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007021 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007022 iemsg("Command from find_ex_command() not handled");
7023 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007024 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007025 }
7026
Bram Moolenaar3988f642020-08-27 22:43:03 +02007027 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007028 && ea.cmdidx != CMD_elseif
7029 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007030 && ea.cmdidx != CMD_endif
7031 && ea.cmdidx != CMD_endfor
7032 && ea.cmdidx != CMD_endwhile
7033 && ea.cmdidx != CMD_catch
7034 && ea.cmdidx != CMD_finally
7035 && ea.cmdidx != CMD_endtry)
7036 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007037 emsg(_(e_unreachable_code_after_return));
7038 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007039 }
7040
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007041 p = skipwhite(p);
7042 if (ea.cmdidx != CMD_SIZE
7043 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7044 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007045 if (ea.cmdidx >= 0)
7046 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007047 if ((ea.argt & EX_BANG) && *p == '!')
7048 {
7049 ea.forceit = TRUE;
7050 p = skipwhite(p + 1);
7051 }
7052 }
7053
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007054 switch (ea.cmdidx)
7055 {
7056 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007057 ea.arg = p;
7058 line = compile_nested_function(&ea, &cctx);
7059 break;
7060
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007061 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007062 // TODO: should we allow this, e.g. to declare a global
7063 // function?
7064 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007065 goto erret;
7066
7067 case CMD_return:
7068 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007069 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007070 break;
7071
7072 case CMD_let:
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02007073 if (get_vim_var_nr(VV_DISALLOW_LET))
7074 {
7075 emsg(_(e_cannot_use_let_in_vim9_script));
7076 break;
7077 }
7078 // FALLTHROUGH
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007079 case CMD_var:
7080 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007081 case CMD_const:
7082 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007083 if (line == p)
7084 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007085 break;
7086
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007087 case CMD_unlet:
7088 case CMD_unlockvar:
7089 case CMD_lockvar:
7090 line = compile_unletlock(p, &ea, &cctx);
7091 break;
7092
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007093 case CMD_import:
7094 line = compile_import(p, &cctx);
7095 break;
7096
7097 case CMD_if:
7098 line = compile_if(p, &cctx);
7099 break;
7100 case CMD_elseif:
7101 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007102 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007103 break;
7104 case CMD_else:
7105 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007106 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007107 break;
7108 case CMD_endif:
7109 line = compile_endif(p, &cctx);
7110 break;
7111
7112 case CMD_while:
7113 line = compile_while(p, &cctx);
7114 break;
7115 case CMD_endwhile:
7116 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007117 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007118 break;
7119
7120 case CMD_for:
7121 line = compile_for(p, &cctx);
7122 break;
7123 case CMD_endfor:
7124 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007125 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007126 break;
7127 case CMD_continue:
7128 line = compile_continue(p, &cctx);
7129 break;
7130 case CMD_break:
7131 line = compile_break(p, &cctx);
7132 break;
7133
7134 case CMD_try:
7135 line = compile_try(p, &cctx);
7136 break;
7137 case CMD_catch:
7138 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007139 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007140 break;
7141 case CMD_finally:
7142 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007143 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007144 break;
7145 case CMD_endtry:
7146 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007147 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007148 break;
7149 case CMD_throw:
7150 line = compile_throw(p, &cctx);
7151 break;
7152
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007153 case CMD_eval:
7154 if (compile_expr0(&p, &cctx) == FAIL)
7155 goto erret;
7156
Bram Moolenaar3988f642020-08-27 22:43:03 +02007157 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007158 generate_instr_drop(&cctx, ISN_DROP, 1);
7159
7160 line = skipwhite(p);
7161 break;
7162
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007163 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007164 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007165 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007166 case CMD_echomsg:
7167 case CMD_echoerr:
7168 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007169 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007170
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007171 case CMD_put:
7172 ea.cmd = cmd;
7173 line = compile_put(p, &ea, &cctx);
7174 break;
7175
Bram Moolenaar3988f642020-08-27 22:43:03 +02007176 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007177
Bram Moolenaarae616492020-07-28 20:07:27 +02007178 case CMD_append:
7179 case CMD_change:
7180 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007181 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007182 case CMD_xit:
7183 not_in_vim9(&ea);
7184 goto erret;
7185
Bram Moolenaar002262f2020-07-08 17:47:57 +02007186 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007187 if (cctx.ctx_skip != SKIP_YES)
7188 {
7189 semsg(_(e_invalid_command_str), ea.cmd);
7190 goto erret;
7191 }
7192 // We don't check for a next command here.
7193 line = (char_u *)"";
7194 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007195
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007196 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007197 if (cctx.ctx_skip == SKIP_YES)
7198 {
7199 // We don't check for a next command here.
7200 line = (char_u *)"";
7201 }
7202 else
7203 {
7204 // Not recognized, execute with do_cmdline_cmd().
7205 ea.arg = p;
7206 line = compile_exec(line, &ea, &cctx);
7207 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007208 break;
7209 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007210nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007211 if (line == NULL)
7212 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007213 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007214
7215 if (cctx.ctx_type_stack.ga_len < 0)
7216 {
7217 iemsg("Type stack underflow");
7218 goto erret;
7219 }
7220 }
7221
7222 if (cctx.ctx_scope != NULL)
7223 {
7224 if (cctx.ctx_scope->se_type == IF_SCOPE)
7225 emsg(_(e_endif));
7226 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7227 emsg(_(e_endwhile));
7228 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7229 emsg(_(e_endfor));
7230 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007231 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007232 goto erret;
7233 }
7234
Bram Moolenaarefd88552020-06-18 20:50:10 +02007235 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007236 {
7237 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7238 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007239 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007240 goto erret;
7241 }
7242
7243 // Return zero if there is no return at the end.
7244 generate_PUSHNR(&cctx, 0);
7245 generate_instr(&cctx, ISN_RETURN);
7246 }
7247
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007248 {
7249 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7250 + ufunc->uf_dfunc_idx;
7251 dfunc->df_deleted = FALSE;
7252 dfunc->df_instr = instr->ga_data;
7253 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007254 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007255 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007256 if (cctx.ctx_outer_used)
7257 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007258 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007259 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007260
7261 ret = OK;
7262
7263erret:
7264 if (ret == FAIL)
7265 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007266 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007267 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7268 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007269
7270 for (idx = 0; idx < instr->ga_len; ++idx)
7271 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007272 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007273
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007274 // If using the last entry in the table and it was added above, we
7275 // might as well remove it.
7276 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007277 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007278 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007279 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007280 ufunc->uf_dfunc_idx = 0;
7281 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007282 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007283
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007284 while (cctx.ctx_scope != NULL)
7285 drop_scope(&cctx);
7286
Bram Moolenaar20431c92020-03-20 18:39:46 +01007287 // Don't execute this function body.
7288 ga_clear_strings(&ufunc->uf_lines);
7289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007290 if (errormsg != NULL)
7291 emsg(errormsg);
7292 else if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007293 emsg(_(e_compile_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007294 }
7295
7296 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007297 if (do_estack_push)
7298 estack_pop();
7299
Bram Moolenaar20431c92020-03-20 18:39:46 +01007300 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007301 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007302 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007303 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007304}
7305
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007306 void
7307set_function_type(ufunc_T *ufunc)
7308{
7309 int varargs = ufunc->uf_va_name != NULL;
7310 int argcount = ufunc->uf_args.ga_len;
7311
7312 // Create a type for the function, with the return type and any
7313 // argument types.
7314 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7315 // The type is included in "tt_args".
7316 if (argcount > 0 || varargs)
7317 {
7318 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7319 argcount, &ufunc->uf_type_list);
7320 // Add argument types to the function type.
7321 if (func_type_add_arg_types(ufunc->uf_func_type,
7322 argcount + varargs,
7323 &ufunc->uf_type_list) == FAIL)
7324 return;
7325 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7326 ufunc->uf_func_type->tt_min_argcount =
7327 argcount - ufunc->uf_def_args.ga_len;
7328 if (ufunc->uf_arg_types == NULL)
7329 {
7330 int i;
7331
7332 // lambda does not have argument types.
7333 for (i = 0; i < argcount; ++i)
7334 ufunc->uf_func_type->tt_args[i] = &t_any;
7335 }
7336 else
7337 mch_memmove(ufunc->uf_func_type->tt_args,
7338 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7339 if (varargs)
7340 {
7341 ufunc->uf_func_type->tt_args[argcount] =
7342 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7343 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7344 }
7345 }
7346 else
7347 // No arguments, can use a predefined type.
7348 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7349 argcount, &ufunc->uf_type_list);
7350}
7351
7352
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007353/*
7354 * Delete an instruction, free what it contains.
7355 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007356 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007357delete_instr(isn_T *isn)
7358{
7359 switch (isn->isn_type)
7360 {
7361 case ISN_EXEC:
7362 case ISN_LOADENV:
7363 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007364 case ISN_LOADB:
7365 case ISN_LOADW:
7366 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007367 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007368 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007369 case ISN_PUSHEXC:
7370 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007371 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007372 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007373 case ISN_STOREB:
7374 case ISN_STOREW:
7375 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007376 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007377 vim_free(isn->isn_arg.string);
7378 break;
7379
7380 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007381 case ISN_STORES:
7382 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007383 break;
7384
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007385 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007386 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007387 vim_free(isn->isn_arg.unlet.ul_name);
7388 break;
7389
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007390 case ISN_STOREOPT:
7391 vim_free(isn->isn_arg.storeopt.so_name);
7392 break;
7393
7394 case ISN_PUSHBLOB: // push blob isn_arg.blob
7395 blob_unref(isn->isn_arg.blob);
7396 break;
7397
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007398 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007399#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007400 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007401#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007402 break;
7403
7404 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007405#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007406 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007407#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007408 break;
7409
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007410 case ISN_UCALL:
7411 vim_free(isn->isn_arg.ufunc.cuf_name);
7412 break;
7413
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007414 case ISN_FUNCREF:
7415 {
7416 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7417 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007418
7419 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7420 func_ptr_unref(dfunc->df_ufunc);
7421 }
7422 break;
7423
7424 case ISN_DCALL:
7425 {
7426 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7427 + isn->isn_arg.dfunc.cdf_idx;
7428
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007429 if (dfunc->df_ufunc != NULL
7430 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007431 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007432 }
7433 break;
7434
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007435 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007436 {
7437 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7438 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7439
7440 if (ufunc != NULL)
7441 {
7442 // Clear uf_dfunc_idx so that the function is deleted.
7443 clear_def_function(ufunc);
7444 ufunc->uf_dfunc_idx = 0;
7445 func_ptr_unref(ufunc);
7446 }
7447
7448 vim_free(lambda);
7449 vim_free(isn->isn_arg.newfunc.nf_global);
7450 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007451 break;
7452
Bram Moolenaar5e654232020-09-16 15:22:00 +02007453 case ISN_CHECKTYPE:
7454 free_type(isn->isn_arg.type.ct_type);
7455 break;
7456
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007457 case ISN_2BOOL:
7458 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007459 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007460 case ISN_ADDBLOB:
7461 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007462 case ISN_ANYINDEX:
7463 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007464 case ISN_BCALL:
7465 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007466 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007467 case ISN_CHECKNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007468 case ISN_COMPAREANY:
7469 case ISN_COMPAREBLOB:
7470 case ISN_COMPAREBOOL:
7471 case ISN_COMPAREDICT:
7472 case ISN_COMPAREFLOAT:
7473 case ISN_COMPAREFUNC:
7474 case ISN_COMPARELIST:
7475 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007476 case ISN_COMPARESPECIAL:
7477 case ISN_COMPARESTRING:
7478 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02007479 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007480 case ISN_DROP:
7481 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007482 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007483 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007484 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007485 case ISN_EXECCONCAT:
7486 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007487 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007488 case ISN_GETITEM:
7489 case ISN_JUMP:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007490 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007491 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007492 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007493 case ISN_LOADBDICT:
7494 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007495 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007496 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007497 case ISN_LOADSCRIPT:
7498 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007499 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007500 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007501 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007502 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007503 case ISN_NEGATENR:
7504 case ISN_NEWDICT:
7505 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007506 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007507 case ISN_OPFLOAT:
7508 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007509 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007510 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007511 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007512 case ISN_PUSHF:
7513 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007514 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007515 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007516 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007517 case ISN_SHUFFLE:
7518 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007519 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007520 case ISN_STOREDICT:
7521 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007522 case ISN_STORENR:
7523 case ISN_STOREOUTER:
7524 case ISN_STOREREG:
7525 case ISN_STORESCRIPT:
7526 case ISN_STOREV:
7527 case ISN_STRINDEX:
7528 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007529 case ISN_THROW:
7530 case ISN_TRY:
7531 // nothing allocated
7532 break;
7533 }
7534}
7535
7536/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007537 * Free all instructions for "dfunc".
7538 */
7539 static void
7540delete_def_function_contents(dfunc_T *dfunc)
7541{
7542 int idx;
7543
7544 ga_clear(&dfunc->df_def_args_isn);
7545
7546 if (dfunc->df_instr != NULL)
7547 {
7548 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7549 delete_instr(dfunc->df_instr + idx);
7550 VIM_CLEAR(dfunc->df_instr);
7551 }
7552
7553 dfunc->df_deleted = TRUE;
7554}
7555
7556/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007557 * When a user function is deleted, clear the contents of any associated def
7558 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007559 */
7560 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007561clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007562{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007563 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007564 {
7565 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7566 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007567
Bram Moolenaar20431c92020-03-20 18:39:46 +01007568 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007569 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007570 }
7571}
7572
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007573/*
7574 * Used when a user function is about to be deleted: remove the pointer to it.
7575 * The entry in def_functions is then unused.
7576 */
7577 void
7578unlink_def_function(ufunc_T *ufunc)
7579{
7580 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7581
7582 dfunc->df_ufunc = NULL;
7583}
7584
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007585#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007586/*
7587 * Free all functions defined with ":def".
7588 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007589 void
7590free_def_functions(void)
7591{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007592 int idx;
7593
7594 for (idx = 0; idx < def_functions.ga_len; ++idx)
7595 {
7596 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7597
7598 delete_def_function_contents(dfunc);
7599 }
7600
7601 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007602}
7603#endif
7604
7605
7606#endif // FEAT_EVAL