blob: f7dc9df2030941f34986fe7f6a53eaaf4b9ca806 [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;
4215 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004216 cctx->ctx_skip = save_skip == SKIP_YES ||
4217 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4218
4219 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4220 // "left ?? right" and "left" is truthy: produce "left"
4221 generate_ppconst(cctx, ppconst);
4222 else
4223 {
4224 clear_tv(&ppconst->pp_tv[ppconst_used]);
4225 --ppconst->pp_used;
4226 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004227 }
4228 else
4229 {
4230 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004231 if (op_falsy)
4232 end_idx = instr->ga_len;
4233 generate_JUMP(cctx, op_falsy
4234 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4235 if (op_falsy)
4236 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004237 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004238
4239 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004240 *arg = skipwhite(p + 1 + op_falsy);
4241 if (may_get_next_line(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004242 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004243 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004244 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245
Bram Moolenaara5565e42020-05-09 15:44:01 +02004246 if (!has_const_expr)
4247 {
4248 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004249
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004250 if (!op_falsy)
4251 {
4252 // remember the type and drop it
4253 --stack->ga_len;
4254 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004256 end_idx = instr->ga_len;
4257 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004258
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004259 // jump here from JUMP_IF_FALSE
4260 isn = ((isn_T *)instr->ga_data) + alt_idx;
4261 isn->isn_arg.jump.jump_where = instr->ga_len;
4262 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004263 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004265 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004267 // Check for the ":".
4268 p = may_peek_next_line(cctx, *arg, &next);
4269 if (*p != ':')
4270 {
4271 emsg(_(e_missing_colon));
4272 return FAIL;
4273 }
4274 if (next != NULL)
4275 {
4276 *arg = next_line_from_context(cctx, TRUE);
4277 p = skipwhite(*arg);
4278 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004279
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004280 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4281 {
4282 semsg(_(e_white_space_required_before_and_after_str), ":");
4283 return FAIL;
4284 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004285
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004286 // evaluate the third expression
4287 if (has_const_expr)
4288 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004289 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004290 *arg = skipwhite(p + 1);
4291 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4292 return FAIL;
4293 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4294 return FAIL;
4295 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296
Bram Moolenaara5565e42020-05-09 15:44:01 +02004297 if (!has_const_expr)
4298 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004299 type_T **typep;
4300
Bram Moolenaara5565e42020-05-09 15:44:01 +02004301 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302
Bram Moolenaara5565e42020-05-09 15:44:01 +02004303 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004304 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4305 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004306
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004307 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004308 isn = ((isn_T *)instr->ga_data) + end_idx;
4309 isn->isn_arg.jump.jump_where = instr->ga_len;
4310 }
4311
4312 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 }
4314 return OK;
4315}
4316
4317/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004318 * Toplevel expression.
4319 */
4320 static int
4321compile_expr0(char_u **arg, cctx_T *cctx)
4322{
4323 ppconst_T ppconst;
4324
4325 CLEAR_FIELD(ppconst);
4326 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4327 {
4328 clear_ppconst(&ppconst);
4329 return FAIL;
4330 }
4331 if (generate_ppconst(cctx, &ppconst) == FAIL)
4332 return FAIL;
4333 return OK;
4334}
4335
4336/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004337 * compile "return [expr]"
4338 */
4339 static char_u *
4340compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4341{
4342 char_u *p = arg;
4343 garray_T *stack = &cctx->ctx_type_stack;
4344 type_T *stack_type;
4345
4346 if (*p != NUL && *p != '|' && *p != '\n')
4347 {
4348 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004349 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 return NULL;
4351
4352 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4353 if (set_return_type)
4354 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004355 else
4356 {
4357 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4358 && stack_type->tt_type != VAR_VOID
4359 && stack_type->tt_type != VAR_UNKNOWN)
4360 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004361 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004362 return NULL;
4363 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004364 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4365 cctx, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004366 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004367 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004368 }
4369 else
4370 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004371 // "set_return_type" cannot be TRUE, only used for a lambda which
4372 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004373 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4374 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004376 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 return NULL;
4378 }
4379
4380 // No argument, return zero.
4381 generate_PUSHNR(cctx, 0);
4382 }
4383
4384 if (generate_instr(cctx, ISN_RETURN) == NULL)
4385 return NULL;
4386
4387 // "return val | endif" is possible
4388 return skipwhite(p);
4389}
4390
4391/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004392 * Get a line from the compilation context, compatible with exarg_T getline().
4393 * Return a pointer to the line in allocated memory.
4394 * Return NULL for end-of-file or some error.
4395 */
4396 static char_u *
4397exarg_getline(
4398 int c UNUSED,
4399 void *cookie,
4400 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004401 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004402{
4403 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004404 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004405
Bram Moolenaar66250c92020-08-20 15:02:42 +02004406 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004407 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004408 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004409 return NULL;
4410 ++cctx->ctx_lnum;
4411 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4412 // Comment lines result in NULL pointers, skip them.
4413 if (p != NULL)
4414 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004415 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004416}
4417
4418/*
4419 * Compile a nested :def command.
4420 */
4421 static char_u *
4422compile_nested_function(exarg_T *eap, cctx_T *cctx)
4423{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004424 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004425 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004426 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004427 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004428 lvar_T *lvar;
4429 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004430 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004431
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004432 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004433 {
4434 emsg(_(e_cannot_use_bang_with_nested_def));
4435 return NULL;
4436 }
4437
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004438 // Only g:Func() can use a namespace.
4439 if (name_start[1] == ':' && !is_global)
4440 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004441 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004442 return NULL;
4443 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004444 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4445 return NULL;
4446
Bram Moolenaar04b12692020-05-04 23:24:44 +02004447 eap->arg = name_end;
4448 eap->getline = exarg_getline;
4449 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004450 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004451 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004452 lambda_name = get_lambda_name();
4453 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004454
Bram Moolenaar822ba242020-05-24 23:00:18 +02004455 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004456 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004457 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004458 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004459 {
4460 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004461 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004462 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004463
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004464 if (is_global)
4465 {
4466 char_u *func_name = vim_strnsave(name_start + 2,
4467 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004468
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004469 if (func_name == NULL)
4470 r = FAIL;
4471 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004472 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004473 }
4474 else
4475 {
4476 // Define a local variable for the function reference.
4477 lvar = reserve_local(cctx, name_start, name_end - name_start,
4478 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004479 if (lvar == NULL)
4480 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004481 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004482 return NULL;
4483 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4484 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004485
Bram Moolenaar61a89812020-05-07 16:58:17 +02004486 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004487 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004488}
4489
4490/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491 * Return the length of an assignment operator, or zero if there isn't one.
4492 */
4493 int
4494assignment_len(char_u *p, int *heredoc)
4495{
4496 if (*p == '=')
4497 {
4498 if (p[1] == '<' && p[2] == '<')
4499 {
4500 *heredoc = TRUE;
4501 return 3;
4502 }
4503 return 1;
4504 }
4505 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4506 return 2;
4507 if (STRNCMP(p, "..=", 3) == 0)
4508 return 3;
4509 return 0;
4510}
4511
4512// words that cannot be used as a variable
4513static char *reserved[] = {
4514 "true",
4515 "false",
4516 NULL
4517};
4518
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004519typedef enum {
4520 dest_local,
4521 dest_option,
4522 dest_env,
4523 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004524 dest_buffer,
4525 dest_window,
4526 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004527 dest_vimvar,
4528 dest_script,
4529 dest_reg,
4530} assign_dest_T;
4531
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004532/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004533 * Generate the load instruction for "name".
4534 */
4535 static void
4536generate_loadvar(
4537 cctx_T *cctx,
4538 assign_dest_T dest,
4539 char_u *name,
4540 lvar_T *lvar,
4541 type_T *type)
4542{
4543 switch (dest)
4544 {
4545 case dest_option:
4546 // TODO: check the option exists
4547 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4548 break;
4549 case dest_global:
4550 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4551 break;
4552 case dest_buffer:
4553 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4554 break;
4555 case dest_window:
4556 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4557 break;
4558 case dest_tab:
4559 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4560 break;
4561 case dest_script:
4562 compile_load_scriptvar(cctx,
4563 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4564 break;
4565 case dest_env:
4566 // Include $ in the name here
4567 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4568 break;
4569 case dest_reg:
4570 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4571 break;
4572 case dest_vimvar:
4573 generate_LOADV(cctx, name + 2, TRUE);
4574 break;
4575 case dest_local:
4576 if (lvar->lv_from_outer)
4577 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4578 NULL, type);
4579 else
4580 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4581 break;
4582 }
4583}
4584
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004585 void
4586vim9_declare_error(char_u *name)
4587{
4588 char *scope = "";
4589
4590 switch (*name)
4591 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004592 case 'g': scope = _("global"); break;
4593 case 'b': scope = _("buffer"); break;
4594 case 'w': scope = _("window"); break;
4595 case 't': scope = _("tab"); break;
4596 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004597 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004598 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004599 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004600 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004601 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004602 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004603 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004604 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004605 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004606}
4607
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004608/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004609 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004610 * "let name"
4611 * "var name = expr"
4612 * "final name = expr"
4613 * "const name = expr"
4614 * "name = expr"
4615 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004616 * Return NULL for an error.
4617 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618 */
4619 static char_u *
4620compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4621{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004622 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004623 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004624 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004625 char_u *ret = NULL;
4626 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004627 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004628 int scriptvar_sid = 0;
4629 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004630 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004631 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004632 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004633 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634 int oplen = 0;
4635 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004636 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004637 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004638 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004640 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
4641 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004643 // Skip over the "var" or "[var, var]" to get to any "=".
4644 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4645 if (p == NULL)
4646 return *arg == '[' ? arg : NULL;
4647
4648 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004649 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004650 // TODO: should we allow this, and figure out type inference from list
4651 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004652 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653 return NULL;
4654 }
4655
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004656 sp = p;
4657 p = skipwhite(p);
4658 op = p;
4659 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004660
4661 if (var_count > 0 && oplen == 0)
4662 // can be something like "[1, 2]->func()"
4663 return arg;
4664
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4666 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004667 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004668 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004669 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004670
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004671 if (heredoc)
4672 {
4673 list_T *l;
4674 listitem_T *li;
4675
4676 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004677 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004679 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02004680 if (l == NULL)
4681 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004682
Bram Moolenaar078269b2020-09-21 20:35:55 +02004683 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02004685 // Push each line and the create the list.
4686 FOR_ALL_LIST_ITEMS(l, li)
4687 {
4688 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4689 li->li_tv.vval.v_string = NULL;
4690 }
4691 generate_NEWLIST(cctx, l->lv_len);
4692 type = &t_list_string;
4693 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004695 list_free(l);
4696 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004697 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004699 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004700 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004701 // for "[var, var] = expr" evaluate the expression here, loop over the
4702 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004703
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004704 p = skipwhite(op + oplen);
4705 if (compile_expr0(&p, cctx) == FAIL)
4706 return NULL;
4707 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004708
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004709 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004711 type_T *stacktype;
4712
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004713 stacktype = stack->ga_len == 0 ? &t_void
4714 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004715 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004716 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004717 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004718 goto theend;
4719 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004720 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004721 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004722 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004723 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4724 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004725 }
4726 }
4727
4728 /*
4729 * Loop over variables in "[var, var] = expr".
4730 * For "var = expr" and "let var: type" this is done only once.
4731 */
4732 if (var_count > 0)
4733 var_start = skipwhite(arg + 1); // skip over the "["
4734 else
4735 var_start = arg;
4736 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4737 {
4738 char_u *var_end = skip_var_one(var_start, FALSE);
4739 size_t varlen;
4740 int new_local = FALSE;
4741 int opt_type;
4742 int opt_flags = 0;
4743 assign_dest_T dest = dest_local;
4744 int vimvaridx = -1;
4745 lvar_T *lvar = NULL;
4746 lvar_T arg_lvar;
4747 int has_type = FALSE;
4748 int has_index = FALSE;
4749 int instr_count = -1;
4750
Bram Moolenaar65821722020-08-02 18:58:54 +02004751 if (*var_start == '@')
4752 p = var_start + 2;
4753 else
4754 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02004755 // skip over the leading "&", "&l:", "&g:" and "$"
4756 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02004757 p = to_name_end(p, TRUE);
4758 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004759
4760 // "a: type" is declaring variable "a" with a type, not "a:".
4761 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4762 --var_end;
4763 if (is_decl && p == var_start + 2 && p[-1] == ':')
4764 --p;
4765
4766 varlen = p - var_start;
4767 vim_free(name);
4768 name = vim_strnsave(var_start, varlen);
4769 if (name == NULL)
4770 return NULL;
4771 if (!heredoc)
4772 type = &t_any;
4773
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004774 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004775 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004776 int declare_error = FALSE;
4777
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004778 if (*var_start == '&')
4779 {
4780 int cc;
4781 long numval;
4782
4783 dest = dest_option;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004784 if (cmdidx == CMD_final || cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004785 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004786 emsg(_(e_const_option));
4787 goto theend;
4788 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004789 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004790 p = var_start;
4791 p = find_option_end(&p, &opt_flags);
4792 if (p == NULL)
4793 {
4794 // cannot happen?
4795 emsg(_(e_letunexp));
4796 goto theend;
4797 }
4798 cc = *p;
4799 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02004800 opt_type = get_option_value(skip_option_env_lead(var_start),
4801 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004802 *p = cc;
4803 if (opt_type == -3)
4804 {
4805 semsg(_(e_unknown_option), var_start);
4806 goto theend;
4807 }
4808 if (opt_type == -2 || opt_type == 0)
4809 type = &t_string;
4810 else
4811 type = &t_number; // both number and boolean option
4812 }
4813 else if (*var_start == '$')
4814 {
4815 dest = dest_env;
4816 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004817 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004818 }
4819 else if (*var_start == '@')
4820 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004821 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004822 {
4823 emsg_invreg(var_start[1]);
4824 goto theend;
4825 }
4826 dest = dest_reg;
4827 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004828 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004829 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004830 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004831 {
4832 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004833 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004834 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004835 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004836 {
4837 dest = dest_buffer;
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, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004841 {
4842 dest = dest_window;
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, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004846 {
4847 dest = dest_tab;
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, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004851 {
4852 typval_T *vtv;
4853 int di_flags;
4854
4855 vimvaridx = find_vim_var(name + 2, &di_flags);
4856 if (vimvaridx < 0)
4857 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004858 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004859 goto theend;
4860 }
4861 // We use the current value of "sandbox" here, is that OK?
4862 if (var_check_ro(di_flags, name, FALSE))
4863 goto theend;
4864 dest = dest_vimvar;
4865 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004866 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004867 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004868 }
4869 else
4870 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004871 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004872
4873 for (idx = 0; reserved[idx] != NULL; ++idx)
4874 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004875 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004876 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004877 goto theend;
4878 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004879
4880 lvar = lookup_local(var_start, varlen, cctx);
4881 if (lvar == NULL)
4882 {
4883 CLEAR_FIELD(arg_lvar);
4884 if (lookup_arg(var_start, varlen,
4885 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4886 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004887 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004888 if (is_decl)
4889 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004890 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004891 goto theend;
4892 }
4893 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004894 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004895 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004896 if (lvar != NULL)
4897 {
4898 if (is_decl)
4899 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004900 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004901 goto theend;
4902 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004903 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004904 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004905 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004906 int script_namespace = varlen > 1
4907 && STRNCMP(var_start, "s:", 2) == 0;
4908 int script_var = (script_namespace
Bram Moolenaar53900992020-08-22 19:02:02 +02004909 ? lookup_script(var_start + 2, varlen - 2, FALSE)
4910 : lookup_script(var_start, varlen, TRUE)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004911 imported_T *import =
4912 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004913
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004914 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004915 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004916 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4917
4918 if (is_decl)
4919 {
4920 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004921 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004922 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004923 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004924 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004925 name);
4926 goto theend;
4927 }
4928 else if (cctx->ctx_ufunc->uf_script_ctx_version
4929 == SCRIPT_VERSION_VIM9
4930 && script_namespace
4931 && !script_var && import == NULL)
4932 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004933 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004934 goto theend;
4935 }
4936
4937 dest = dest_script;
4938
4939 // existing script-local variables should have a type
4940 scriptvar_sid = current_sctx.sc_sid;
4941 if (import != NULL)
4942 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004943 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004944 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02004945 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4946 rawname, TRUE);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02004947 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02004948 {
4949 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4950 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004951 ((svar_T *)si->sn_var_vals.ga_data)
4952 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004953 type = sv->sv_type;
4954 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004955 }
4956 }
4957 else if (name[1] == ':' && name[2] != NUL)
4958 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004959 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004960 goto theend;
4961 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004962 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004963 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004964 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004965 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004966 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004967 else if (check_defined(var_start, varlen, cctx) == FAIL)
4968 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004969 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004970 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004971
4972 if (declare_error)
4973 {
4974 vim9_declare_error(name);
4975 goto theend;
4976 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004977 }
4978
4979 // handle "a:name" as a name, not index "name" on "a"
4980 if (varlen > 1 || var_start[varlen] != ':')
4981 p = var_end;
4982
4983 if (dest != dest_option)
4984 {
4985 if (is_decl && *p == ':')
4986 {
4987 // parse optional type: "let var: type = expr"
4988 if (!VIM_ISWHITE(p[1]))
4989 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004990 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004991 goto theend;
4992 }
4993 p = skipwhite(p + 1);
4994 type = parse_type(&p, cctx->ctx_type_list);
4995 has_type = TRUE;
4996 }
4997 else if (lvar != NULL)
4998 type = lvar->lv_type;
4999 }
5000
5001 if (oplen == 3 && !heredoc && dest != dest_global
5002 && type->tt_type != VAR_STRING
5003 && type->tt_type != VAR_ANY)
5004 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005005 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005006 goto theend;
5007 }
5008
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005009 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005010 {
5011 if (oplen > 1 && !heredoc)
5012 {
5013 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005014 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005015 goto theend;
5016 }
5017
5018 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005019 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5020 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005021 goto theend;
5022 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005023 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005024 if (lvar == NULL)
5025 goto theend;
5026 new_local = TRUE;
5027 }
5028
5029 member_type = type;
5030 if (var_end > var_start + varlen)
5031 {
5032 // Something follows after the variable: "var[idx]".
5033 if (is_decl)
5034 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005035 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005036 goto theend;
5037 }
5038
5039 if (var_start[varlen] == '[')
5040 {
5041 has_index = TRUE;
5042 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005043 member_type = &t_any;
5044 else
5045 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005046 }
5047 else
5048 {
5049 semsg("Not supported yet: %s", var_start);
5050 goto theend;
5051 }
5052 }
5053 else if (lvar == &arg_lvar)
5054 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005055 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005056 goto theend;
5057 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005058 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5059 {
5060 semsg(_(e_cannot_assign_to_constant), name);
5061 goto theend;
5062 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005063
5064 if (!heredoc)
5065 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005066 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005067 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005068 if (oplen > 0 && var_count == 0)
5069 {
5070 // skip over the "=" and the expression
5071 p = skipwhite(op + oplen);
5072 compile_expr0(&p, cctx);
5073 }
5074 }
5075 else if (oplen > 0)
5076 {
5077 type_T *stacktype;
5078
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005079 // For "var = expr" evaluate the expression.
5080 if (var_count == 0)
5081 {
5082 int r;
5083
5084 // for "+=", "*=", "..=" etc. first load the current value
5085 if (*op != '=')
5086 {
5087 generate_loadvar(cctx, dest, name, lvar, type);
5088
5089 if (has_index)
5090 {
5091 // TODO: get member from list or dict
5092 emsg("Index with operation not supported yet");
5093 goto theend;
5094 }
5095 }
5096
5097 // Compile the expression. Temporarily hide the new local
5098 // variable here, it is not available to this expression.
5099 if (new_local)
5100 --cctx->ctx_locals.ga_len;
5101 instr_count = instr->ga_len;
5102 p = skipwhite(op + oplen);
5103 r = compile_expr0(&p, cctx);
5104 if (new_local)
5105 ++cctx->ctx_locals.ga_len;
5106 if (r == FAIL)
5107 goto theend;
5108 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005109 else if (semicolon && var_idx == var_count - 1)
5110 {
5111 // For "[var; var] = expr" get the rest of the list
5112 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5113 goto theend;
5114 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005115 else
5116 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005117 // For "[var, var] = expr" get the "var_idx" item from the
5118 // list.
5119 if (generate_GETITEM(cctx, var_idx) == FAIL)
5120 return FAIL;
5121 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005122
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005123 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005124 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005125 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005126 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005127 if ((stacktype->tt_type == VAR_FUNC
5128 || stacktype->tt_type == VAR_PARTIAL)
5129 && var_wrong_func_name(name, TRUE))
5130 goto theend;
5131
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005132 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005133 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005134 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005135 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005136 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005137 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005138 }
5139 else
5140 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005141 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005142 // for a variable that implies &t_any.
5143 if (stacktype == &t_list_empty)
5144 lvar->lv_type = &t_list_any;
5145 else if (stacktype == &t_dict_empty)
5146 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005147 else if (stacktype == &t_unknown)
5148 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005149 else
5150 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005151 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005152 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005153 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005154 {
5155 type_T *use_type = lvar->lv_type;
5156
Bram Moolenaar71abe482020-09-14 22:28:30 +02005157 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005158 if (has_index)
5159 {
5160 use_type = use_type->tt_member;
5161 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005162 // could be indexing "any"
5163 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005164 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005165 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005166 == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005167 goto theend;
5168 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005169 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005170 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005171 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005172 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005173 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005174 else if (cmdidx == CMD_final)
5175 {
5176 emsg(_(e_final_requires_a_value));
5177 goto theend;
5178 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005179 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005180 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005181 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005182 goto theend;
5183 }
5184 else if (!has_type || dest == dest_option)
5185 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005186 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005187 goto theend;
5188 }
5189 else
5190 {
5191 // variables are always initialized
5192 if (ga_grow(instr, 1) == FAIL)
5193 goto theend;
5194 switch (member_type->tt_type)
5195 {
5196 case VAR_BOOL:
5197 generate_PUSHBOOL(cctx, VVAL_FALSE);
5198 break;
5199 case VAR_FLOAT:
5200#ifdef FEAT_FLOAT
5201 generate_PUSHF(cctx, 0.0);
5202#endif
5203 break;
5204 case VAR_STRING:
5205 generate_PUSHS(cctx, NULL);
5206 break;
5207 case VAR_BLOB:
5208 generate_PUSHBLOB(cctx, NULL);
5209 break;
5210 case VAR_FUNC:
5211 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5212 break;
5213 case VAR_LIST:
5214 generate_NEWLIST(cctx, 0);
5215 break;
5216 case VAR_DICT:
5217 generate_NEWDICT(cctx, 0);
5218 break;
5219 case VAR_JOB:
5220 generate_PUSHJOB(cctx, NULL);
5221 break;
5222 case VAR_CHANNEL:
5223 generate_PUSHCHANNEL(cctx, NULL);
5224 break;
5225 case VAR_NUMBER:
5226 case VAR_UNKNOWN:
5227 case VAR_ANY:
5228 case VAR_PARTIAL:
5229 case VAR_VOID:
5230 case VAR_SPECIAL: // cannot happen
5231 generate_PUSHNR(cctx, 0);
5232 break;
5233 }
5234 }
5235 if (var_count == 0)
5236 end = p;
5237 }
5238
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005239 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005240 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005241 break;
5242
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005243 if (oplen > 0 && *op != '=')
5244 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005245 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005246 type_T *stacktype;
5247
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005248 if (*op == '.')
5249 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005250 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005251 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005252 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005253 if (
5254#ifdef FEAT_FLOAT
5255 // If variable is float operation with number is OK.
5256 !(expected == &t_float && stacktype == &t_number) &&
5257#endif
5258 need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005259 goto theend;
5260
5261 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005262 {
5263 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5264 goto theend;
5265 }
5266 else if (*op == '+')
5267 {
5268 if (generate_add_instr(cctx,
5269 operator_type(member_type, stacktype),
5270 member_type, stacktype) == FAIL)
5271 goto theend;
5272 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005273 else if (generate_two_op(cctx, op) == FAIL)
5274 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005275 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005276
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005277 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005278 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005279 int r;
5280
5281 // Compile the "idx" in "var[idx]".
5282 if (new_local)
5283 --cctx->ctx_locals.ga_len;
5284 p = skipwhite(var_start + varlen + 1);
5285 r = compile_expr0(&p, cctx);
5286 if (new_local)
5287 ++cctx->ctx_locals.ga_len;
5288 if (r == FAIL)
5289 goto theend;
5290 if (*skipwhite(p) != ']')
5291 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005292 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005293 emsg(_(e_missbrac));
5294 goto theend;
5295 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005296 if (type == &t_any)
5297 {
5298 type_T *idx_type = ((type_T **)stack->ga_data)[
5299 stack->ga_len - 1];
5300 // Index on variable of unknown type: guess the type from the
5301 // index type: number is dict, otherwise dict.
5302 // TODO: should do the assignment at runtime
5303 if (idx_type->tt_type == VAR_NUMBER)
5304 type = &t_list_any;
5305 else
5306 type = &t_dict_any;
5307 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005308 if (type->tt_type == VAR_DICT
5309 && may_generate_2STRING(-1, cctx) == FAIL)
5310 goto theend;
5311 if (type->tt_type == VAR_LIST
5312 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005313 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005314 {
5315 emsg(_(e_number_exp));
5316 goto theend;
5317 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005318
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005319 // Load the dict or list. On the stack we then have:
5320 // - value
5321 // - index
5322 // - variable
5323 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005324
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005325 if (type->tt_type == VAR_LIST)
5326 {
5327 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5328 return FAIL;
5329 }
5330 else if (type->tt_type == VAR_DICT)
5331 {
5332 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5333 return FAIL;
5334 }
5335 else
5336 {
5337 emsg(_(e_listreq));
5338 goto theend;
5339 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005340 }
5341 else
5342 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005343 if (is_decl && cmdidx == CMD_const
5344 && (dest == dest_script || dest == dest_local))
5345 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005346 generate_LOCKCONST(cctx);
5347
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005348 switch (dest)
5349 {
5350 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005351 generate_STOREOPT(cctx, skip_option_env_lead(name),
5352 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005353 break;
5354 case dest_global:
5355 // include g: with the name, easier to execute that way
5356 generate_STORE(cctx, ISN_STOREG, 0, name);
5357 break;
5358 case dest_buffer:
5359 // include b: with the name, easier to execute that way
5360 generate_STORE(cctx, ISN_STOREB, 0, name);
5361 break;
5362 case dest_window:
5363 // include w: with the name, easier to execute that way
5364 generate_STORE(cctx, ISN_STOREW, 0, name);
5365 break;
5366 case dest_tab:
5367 // include t: with the name, easier to execute that way
5368 generate_STORE(cctx, ISN_STORET, 0, name);
5369 break;
5370 case dest_env:
5371 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5372 break;
5373 case dest_reg:
5374 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5375 break;
5376 case dest_vimvar:
5377 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5378 break;
5379 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005380 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005381 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005382 {
5383 char_u *name_s = name;
5384
5385 // Include s: in the name for store_var()
5386 if (name[1] != ':')
5387 {
5388 int len = (int)STRLEN(name) + 3;
5389
5390 name_s = alloc(len);
5391 if (name_s == NULL)
5392 name_s = name;
5393 else
5394 vim_snprintf((char *)name_s, len,
5395 "s:%s", name);
5396 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005397 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5398 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005399 if (name_s != name)
5400 vim_free(name_s);
5401 }
5402 else
5403 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005404 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005405 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005406 break;
5407 case dest_local:
5408 if (lvar != NULL)
5409 {
5410 isn_T *isn = ((isn_T *)instr->ga_data)
5411 + instr->ga_len - 1;
5412
5413 // optimization: turn "var = 123" from ISN_PUSHNR +
5414 // ISN_STORE into ISN_STORENR
5415 if (!lvar->lv_from_outer
5416 && instr->ga_len == instr_count + 1
5417 && isn->isn_type == ISN_PUSHNR)
5418 {
5419 varnumber_T val = isn->isn_arg.number;
5420
5421 isn->isn_type = ISN_STORENR;
5422 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5423 isn->isn_arg.storenr.stnr_val = val;
5424 if (stack->ga_len > 0)
5425 --stack->ga_len;
5426 }
5427 else if (lvar->lv_from_outer)
5428 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005429 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005430 else
5431 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5432 }
5433 break;
5434 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005435 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005436
5437 if (var_idx + 1 < var_count)
5438 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005439 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005440
5441 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005442 if (var_count > 0 && !semicolon)
5443 {
5444 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5445 goto theend;
5446 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005447
Bram Moolenaarb2097502020-07-19 17:17:02 +02005448 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005449
5450theend:
5451 vim_free(name);
5452 return ret;
5453}
5454
5455/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005456 * Check if "name" can be "unlet".
5457 */
5458 int
5459check_vim9_unlet(char_u *name)
5460{
5461 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5462 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005463 // "unlet s:var" is allowed in legacy script.
5464 if (*name == 's' && !script_is_vim9())
5465 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005466 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005467 return FAIL;
5468 }
5469 return OK;
5470}
5471
5472/*
5473 * Callback passed to ex_unletlock().
5474 */
5475 static int
5476compile_unlet(
5477 lval_T *lvp,
5478 char_u *name_end,
5479 exarg_T *eap,
5480 int deep UNUSED,
5481 void *coookie)
5482{
5483 cctx_T *cctx = coookie;
5484
5485 if (lvp->ll_tv == NULL)
5486 {
5487 char_u *p = lvp->ll_name;
5488 int cc = *name_end;
5489 int ret = OK;
5490
5491 // Normal name. Only supports g:, w:, t: and b: namespaces.
5492 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005493 if (*p == '$')
5494 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5495 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005496 ret = FAIL;
5497 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005498 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005499
5500 *name_end = cc;
5501 return ret;
5502 }
5503
5504 // TODO: unlet {list}[idx]
5505 // TODO: unlet {dict}[key]
5506 emsg("Sorry, :unlet not fully implemented yet");
5507 return FAIL;
5508}
5509
5510/*
5511 * compile "unlet var", "lock var" and "unlock var"
5512 * "arg" points to "var".
5513 */
5514 static char_u *
5515compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5516{
5517 char_u *p = arg;
5518
5519 if (eap->cmdidx != CMD_unlet)
5520 {
5521 emsg("Sorry, :lock and unlock not implemented yet");
5522 return NULL;
5523 }
5524
5525 if (*p == '!')
5526 {
5527 p = skipwhite(p + 1);
5528 eap->forceit = TRUE;
5529 }
5530
5531 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5532 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5533}
5534
5535/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005536 * Compile an :import command.
5537 */
5538 static char_u *
5539compile_import(char_u *arg, cctx_T *cctx)
5540{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005541 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005542}
5543
5544/*
5545 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5546 */
5547 static int
5548compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5549{
5550 garray_T *instr = &cctx->ctx_instr;
5551 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5552
5553 if (endlabel == NULL)
5554 return FAIL;
5555 endlabel->el_next = *el;
5556 *el = endlabel;
5557 endlabel->el_end_label = instr->ga_len;
5558
5559 generate_JUMP(cctx, when, 0);
5560 return OK;
5561}
5562
5563 static void
5564compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5565{
5566 garray_T *instr = &cctx->ctx_instr;
5567
5568 while (*el != NULL)
5569 {
5570 endlabel_T *cur = (*el);
5571 isn_T *isn;
5572
5573 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5574 isn->isn_arg.jump.jump_where = instr->ga_len;
5575 *el = cur->el_next;
5576 vim_free(cur);
5577 }
5578}
5579
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005580 static void
5581compile_free_jump_to_end(endlabel_T **el)
5582{
5583 while (*el != NULL)
5584 {
5585 endlabel_T *cur = (*el);
5586
5587 *el = cur->el_next;
5588 vim_free(cur);
5589 }
5590}
5591
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005592/*
5593 * Create a new scope and set up the generic items.
5594 */
5595 static scope_T *
5596new_scope(cctx_T *cctx, scopetype_T type)
5597{
5598 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5599
5600 if (scope == NULL)
5601 return NULL;
5602 scope->se_outer = cctx->ctx_scope;
5603 cctx->ctx_scope = scope;
5604 scope->se_type = type;
5605 scope->se_local_count = cctx->ctx_locals.ga_len;
5606 return scope;
5607}
5608
5609/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005610 * Free the current scope and go back to the outer scope.
5611 */
5612 static void
5613drop_scope(cctx_T *cctx)
5614{
5615 scope_T *scope = cctx->ctx_scope;
5616
5617 if (scope == NULL)
5618 {
5619 iemsg("calling drop_scope() without a scope");
5620 return;
5621 }
5622 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005623 switch (scope->se_type)
5624 {
5625 case IF_SCOPE:
5626 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5627 case FOR_SCOPE:
5628 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5629 case WHILE_SCOPE:
5630 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5631 case TRY_SCOPE:
5632 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5633 case NO_SCOPE:
5634 case BLOCK_SCOPE:
5635 break;
5636 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005637 vim_free(scope);
5638}
5639
5640/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005641 * compile "if expr"
5642 *
5643 * "if expr" Produces instructions:
5644 * EVAL expr Push result of "expr"
5645 * JUMP_IF_FALSE end
5646 * ... body ...
5647 * end:
5648 *
5649 * "if expr | else" Produces instructions:
5650 * EVAL expr Push result of "expr"
5651 * JUMP_IF_FALSE else
5652 * ... body ...
5653 * JUMP_ALWAYS end
5654 * else:
5655 * ... body ...
5656 * end:
5657 *
5658 * "if expr1 | elseif expr2 | else" Produces instructions:
5659 * EVAL expr Push result of "expr"
5660 * JUMP_IF_FALSE elseif
5661 * ... body ...
5662 * JUMP_ALWAYS end
5663 * elseif:
5664 * EVAL expr Push result of "expr"
5665 * JUMP_IF_FALSE else
5666 * ... body ...
5667 * JUMP_ALWAYS end
5668 * else:
5669 * ... body ...
5670 * end:
5671 */
5672 static char_u *
5673compile_if(char_u *arg, cctx_T *cctx)
5674{
5675 char_u *p = arg;
5676 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005677 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005678 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005679 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005680 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005681
Bram Moolenaara5565e42020-05-09 15:44:01 +02005682 CLEAR_FIELD(ppconst);
5683 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005684 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005685 clear_ppconst(&ppconst);
5686 return NULL;
5687 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005688 if (cctx->ctx_skip == SKIP_YES)
5689 clear_ppconst(&ppconst);
5690 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005691 {
5692 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005693 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005694 clear_ppconst(&ppconst);
5695 }
5696 else
5697 {
5698 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005699 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005700 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005701 return NULL;
5702 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005703
5704 scope = new_scope(cctx, IF_SCOPE);
5705 if (scope == NULL)
5706 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005707 scope->se_skip_save = skip_save;
5708 // "is_had_return" will be reset if any block does not end in :return
5709 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005710
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005711 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005712 {
5713 // "where" is set when ":elseif", "else" or ":endif" is found
5714 scope->se_u.se_if.is_if_label = instr->ga_len;
5715 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5716 }
5717 else
5718 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005719
5720 return p;
5721}
5722
5723 static char_u *
5724compile_elseif(char_u *arg, cctx_T *cctx)
5725{
5726 char_u *p = arg;
5727 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005728 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005729 isn_T *isn;
5730 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005731 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02005732 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005733
5734 if (scope == NULL || scope->se_type != IF_SCOPE)
5735 {
5736 emsg(_(e_elseif_without_if));
5737 return NULL;
5738 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005739 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005740 if (!cctx->ctx_had_return)
5741 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005742
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005743 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005744 {
5745 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005746 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005747 return NULL;
5748 // previous "if" or "elseif" jumps here
5749 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5750 isn->isn_arg.jump.jump_where = instr->ga_len;
5751 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005752
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005753 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005754 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02005755 if (cctx->ctx_skip == SKIP_YES)
5756 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005757 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005758 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005759 clear_ppconst(&ppconst);
5760 return NULL;
5761 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02005762 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005763 if (scope->se_skip_save == SKIP_YES)
5764 clear_ppconst(&ppconst);
5765 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005766 {
5767 // The expression results in a constant.
5768 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005769 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005770 clear_ppconst(&ppconst);
5771 scope->se_u.se_if.is_if_label = -1;
5772 }
5773 else
5774 {
5775 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005776 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005777 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005778 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005779
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005780 // "where" is set when ":elseif", "else" or ":endif" is found
5781 scope->se_u.se_if.is_if_label = instr->ga_len;
5782 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5783 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005784
5785 return p;
5786}
5787
5788 static char_u *
5789compile_else(char_u *arg, cctx_T *cctx)
5790{
5791 char_u *p = arg;
5792 garray_T *instr = &cctx->ctx_instr;
5793 isn_T *isn;
5794 scope_T *scope = cctx->ctx_scope;
5795
5796 if (scope == NULL || scope->se_type != IF_SCOPE)
5797 {
5798 emsg(_(e_else_without_if));
5799 return NULL;
5800 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005801 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005802 if (!cctx->ctx_had_return)
5803 scope->se_u.se_if.is_had_return = FALSE;
5804 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005805
Bram Moolenaarefd88552020-06-18 20:50:10 +02005806 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005807 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005808 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005809 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005810 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005811 if (!cctx->ctx_had_return
5812 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5813 JUMP_ALWAYS, cctx) == FAIL)
5814 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005815 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005816
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005817 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005818 {
5819 if (scope->se_u.se_if.is_if_label >= 0)
5820 {
5821 // previous "if" or "elseif" jumps here
5822 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5823 isn->isn_arg.jump.jump_where = instr->ga_len;
5824 scope->se_u.se_if.is_if_label = -1;
5825 }
5826 }
5827
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005828 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005829 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5830 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005831
5832 return p;
5833}
5834
5835 static char_u *
5836compile_endif(char_u *arg, cctx_T *cctx)
5837{
5838 scope_T *scope = cctx->ctx_scope;
5839 ifscope_T *ifscope;
5840 garray_T *instr = &cctx->ctx_instr;
5841 isn_T *isn;
5842
5843 if (scope == NULL || scope->se_type != IF_SCOPE)
5844 {
5845 emsg(_(e_endif_without_if));
5846 return NULL;
5847 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005848 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005849 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005850 if (!cctx->ctx_had_return)
5851 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005852
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005853 if (scope->se_u.se_if.is_if_label >= 0)
5854 {
5855 // previous "if" or "elseif" jumps here
5856 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5857 isn->isn_arg.jump.jump_where = instr->ga_len;
5858 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005859 // Fill in the "end" label in jumps at the end of the blocks.
5860 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005861 cctx->ctx_skip = scope->se_skip_save;
5862
5863 // If all the blocks end in :return and there is an :else then the
5864 // had_return flag is set.
5865 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005866
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005867 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005868 return arg;
5869}
5870
5871/*
5872 * compile "for var in expr"
5873 *
5874 * Produces instructions:
5875 * PUSHNR -1
5876 * STORE loop-idx Set index to -1
5877 * EVAL expr Push result of "expr"
5878 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5879 * - if beyond end, jump to "end"
5880 * - otherwise get item from list and push it
5881 * STORE var Store item in "var"
5882 * ... body ...
5883 * JUMP top Jump back to repeat
5884 * end: DROP Drop the result of "expr"
5885 *
5886 */
5887 static char_u *
5888compile_for(char_u *arg, cctx_T *cctx)
5889{
5890 char_u *p;
5891 size_t varlen;
5892 garray_T *instr = &cctx->ctx_instr;
5893 garray_T *stack = &cctx->ctx_type_stack;
5894 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005895 lvar_T *loop_lvar; // loop iteration variable
5896 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005897 type_T *vartype;
5898
5899 // TODO: list of variables: "for [key, value] in dict"
5900 // parse "var"
5901 for (p = arg; eval_isnamec1(*p); ++p)
5902 ;
5903 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005904 var_lvar = lookup_local(arg, varlen, cctx);
5905 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005906 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005907 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005908 return NULL;
5909 }
5910
5911 // consume "in"
5912 p = skipwhite(p);
5913 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5914 {
5915 emsg(_(e_missing_in));
5916 return NULL;
5917 }
5918 p = skipwhite(p + 2);
5919
5920
5921 scope = new_scope(cctx, FOR_SCOPE);
5922 if (scope == NULL)
5923 return NULL;
5924
5925 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005926 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5927 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005928 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005929 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005930 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005931 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005932 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005933
5934 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005935 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5936 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005937 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005938 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005939 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005940 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005941 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005942
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005943 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005944
5945 // compile "expr", it remains on the stack until "endfor"
5946 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005947 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005948 {
5949 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005950 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005951 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005952
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005953 // Now that we know the type of "var", check that it is a list, now or at
5954 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005955 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005956 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005957 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005958 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005959 return NULL;
5960 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005961 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005962 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005963
5964 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005965 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005966
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005967 generate_FOR(cctx, loop_lvar->lv_idx);
5968 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005969
5970 return arg;
5971}
5972
5973/*
5974 * compile "endfor"
5975 */
5976 static char_u *
5977compile_endfor(char_u *arg, cctx_T *cctx)
5978{
5979 garray_T *instr = &cctx->ctx_instr;
5980 scope_T *scope = cctx->ctx_scope;
5981 forscope_T *forscope;
5982 isn_T *isn;
5983
5984 if (scope == NULL || scope->se_type != FOR_SCOPE)
5985 {
5986 emsg(_(e_for));
5987 return NULL;
5988 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005989 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005990 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005991 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005992
5993 // At end of ":for" scope jump back to the FOR instruction.
5994 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5995
5996 // Fill in the "end" label in the FOR statement so it can jump here
5997 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5998 isn->isn_arg.forloop.for_end = instr->ga_len;
5999
6000 // Fill in the "end" label any BREAK statements
6001 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6002
6003 // Below the ":for" scope drop the "expr" list from the stack.
6004 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6005 return NULL;
6006
6007 vim_free(scope);
6008
6009 return arg;
6010}
6011
6012/*
6013 * compile "while expr"
6014 *
6015 * Produces instructions:
6016 * top: EVAL expr Push result of "expr"
6017 * JUMP_IF_FALSE end jump if false
6018 * ... body ...
6019 * JUMP top Jump back to repeat
6020 * end:
6021 *
6022 */
6023 static char_u *
6024compile_while(char_u *arg, cctx_T *cctx)
6025{
6026 char_u *p = arg;
6027 garray_T *instr = &cctx->ctx_instr;
6028 scope_T *scope;
6029
6030 scope = new_scope(cctx, WHILE_SCOPE);
6031 if (scope == NULL)
6032 return NULL;
6033
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006034 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006035
6036 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006037 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006038 return NULL;
6039
6040 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006041 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006042 JUMP_IF_FALSE, cctx) == FAIL)
6043 return FAIL;
6044
6045 return p;
6046}
6047
6048/*
6049 * compile "endwhile"
6050 */
6051 static char_u *
6052compile_endwhile(char_u *arg, cctx_T *cctx)
6053{
6054 scope_T *scope = cctx->ctx_scope;
6055
6056 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6057 {
6058 emsg(_(e_while));
6059 return NULL;
6060 }
6061 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006062 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006063
6064 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006065 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006066
6067 // Fill in the "end" label in the WHILE statement so it can jump here.
6068 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006069 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006070
6071 vim_free(scope);
6072
6073 return arg;
6074}
6075
6076/*
6077 * compile "continue"
6078 */
6079 static char_u *
6080compile_continue(char_u *arg, cctx_T *cctx)
6081{
6082 scope_T *scope = cctx->ctx_scope;
6083
6084 for (;;)
6085 {
6086 if (scope == NULL)
6087 {
6088 emsg(_(e_continue));
6089 return NULL;
6090 }
6091 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6092 break;
6093 scope = scope->se_outer;
6094 }
6095
6096 // Jump back to the FOR or WHILE instruction.
6097 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006098 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6099 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006100 return arg;
6101}
6102
6103/*
6104 * compile "break"
6105 */
6106 static char_u *
6107compile_break(char_u *arg, cctx_T *cctx)
6108{
6109 scope_T *scope = cctx->ctx_scope;
6110 endlabel_T **el;
6111
6112 for (;;)
6113 {
6114 if (scope == NULL)
6115 {
6116 emsg(_(e_break));
6117 return NULL;
6118 }
6119 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6120 break;
6121 scope = scope->se_outer;
6122 }
6123
6124 // Jump to the end of the FOR or WHILE loop.
6125 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006126 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006127 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006128 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006129 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6130 return FAIL;
6131
6132 return arg;
6133}
6134
6135/*
6136 * compile "{" start of block
6137 */
6138 static char_u *
6139compile_block(char_u *arg, cctx_T *cctx)
6140{
6141 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6142 return NULL;
6143 return skipwhite(arg + 1);
6144}
6145
6146/*
6147 * compile end of block: drop one scope
6148 */
6149 static void
6150compile_endblock(cctx_T *cctx)
6151{
6152 scope_T *scope = cctx->ctx_scope;
6153
6154 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006155 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006156 vim_free(scope);
6157}
6158
6159/*
6160 * compile "try"
6161 * Creates a new scope for the try-endtry, pointing to the first catch and
6162 * finally.
6163 * Creates another scope for the "try" block itself.
6164 * TRY instruction sets up exception handling at runtime.
6165 *
6166 * "try"
6167 * TRY -> catch1, -> finally push trystack entry
6168 * ... try block
6169 * "throw {exception}"
6170 * EVAL {exception}
6171 * THROW create exception
6172 * ... try block
6173 * " catch {expr}"
6174 * JUMP -> finally
6175 * catch1: PUSH exeception
6176 * EVAL {expr}
6177 * MATCH
6178 * JUMP nomatch -> catch2
6179 * CATCH remove exception
6180 * ... catch block
6181 * " catch"
6182 * JUMP -> finally
6183 * catch2: CATCH remove exception
6184 * ... catch block
6185 * " finally"
6186 * finally:
6187 * ... finally block
6188 * " endtry"
6189 * ENDTRY pop trystack entry, may rethrow
6190 */
6191 static char_u *
6192compile_try(char_u *arg, cctx_T *cctx)
6193{
6194 garray_T *instr = &cctx->ctx_instr;
6195 scope_T *try_scope;
6196 scope_T *scope;
6197
6198 // scope that holds the jumps that go to catch/finally/endtry
6199 try_scope = new_scope(cctx, TRY_SCOPE);
6200 if (try_scope == NULL)
6201 return NULL;
6202
6203 // "catch" is set when the first ":catch" is found.
6204 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006205 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006206 if (generate_instr(cctx, ISN_TRY) == NULL)
6207 return NULL;
6208
6209 // scope for the try block itself
6210 scope = new_scope(cctx, BLOCK_SCOPE);
6211 if (scope == NULL)
6212 return NULL;
6213
6214 return arg;
6215}
6216
6217/*
6218 * compile "catch {expr}"
6219 */
6220 static char_u *
6221compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6222{
6223 scope_T *scope = cctx->ctx_scope;
6224 garray_T *instr = &cctx->ctx_instr;
6225 char_u *p;
6226 isn_T *isn;
6227
6228 // end block scope from :try or :catch
6229 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6230 compile_endblock(cctx);
6231 scope = cctx->ctx_scope;
6232
6233 // Error if not in a :try scope
6234 if (scope == NULL || scope->se_type != TRY_SCOPE)
6235 {
6236 emsg(_(e_catch));
6237 return NULL;
6238 }
6239
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006240 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006241 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006242 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006243 return NULL;
6244 }
6245
6246 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006247 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006248 JUMP_ALWAYS, cctx) == FAIL)
6249 return NULL;
6250
6251 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006252 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006253 if (isn->isn_arg.try.try_catch == 0)
6254 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006255 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006256 {
6257 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006258 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006259 isn->isn_arg.jump.jump_where = instr->ga_len;
6260 }
6261
6262 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006263 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006264 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006265 scope->se_u.se_try.ts_caught_all = TRUE;
6266 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006267 }
6268 else
6269 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006270 char_u *end;
6271 char_u *pat;
6272 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006273 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006274 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006275
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006276 // Push v:exception, push {expr} and MATCH
6277 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6278
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006279 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006280 if (*end != *p)
6281 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006282 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006283 vim_free(tofree);
6284 return FAIL;
6285 }
6286 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006287 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006288 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006289 len = (int)(end - tofree);
6290 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006291 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006292 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006293 if (pat == NULL)
6294 return FAIL;
6295 if (generate_PUSHS(cctx, pat) == FAIL)
6296 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006297
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006298 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6299 return NULL;
6300
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006301 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006302 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6303 return NULL;
6304 }
6305
6306 if (generate_instr(cctx, ISN_CATCH) == NULL)
6307 return NULL;
6308
6309 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6310 return NULL;
6311 return p;
6312}
6313
6314 static char_u *
6315compile_finally(char_u *arg, cctx_T *cctx)
6316{
6317 scope_T *scope = cctx->ctx_scope;
6318 garray_T *instr = &cctx->ctx_instr;
6319 isn_T *isn;
6320
6321 // end block scope from :try or :catch
6322 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6323 compile_endblock(cctx);
6324 scope = cctx->ctx_scope;
6325
6326 // Error if not in a :try scope
6327 if (scope == NULL || scope->se_type != TRY_SCOPE)
6328 {
6329 emsg(_(e_finally));
6330 return NULL;
6331 }
6332
6333 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006334 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006335 if (isn->isn_arg.try.try_finally != 0)
6336 {
6337 emsg(_(e_finally_dup));
6338 return NULL;
6339 }
6340
6341 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006342 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006343
Bram Moolenaar585fea72020-04-02 22:33:21 +02006344 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006345 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006346 {
6347 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006348 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006349 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006350 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006351 }
6352
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006353 // TODO: set index in ts_finally_label jumps
6354
6355 return arg;
6356}
6357
6358 static char_u *
6359compile_endtry(char_u *arg, cctx_T *cctx)
6360{
6361 scope_T *scope = cctx->ctx_scope;
6362 garray_T *instr = &cctx->ctx_instr;
6363 isn_T *isn;
6364
6365 // end block scope from :catch or :finally
6366 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6367 compile_endblock(cctx);
6368 scope = cctx->ctx_scope;
6369
6370 // Error if not in a :try scope
6371 if (scope == NULL || scope->se_type != TRY_SCOPE)
6372 {
6373 if (scope == NULL)
6374 emsg(_(e_no_endtry));
6375 else if (scope->se_type == WHILE_SCOPE)
6376 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006377 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006378 emsg(_(e_endfor));
6379 else
6380 emsg(_(e_endif));
6381 return NULL;
6382 }
6383
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006384 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006385 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6386 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006387 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006388 return NULL;
6389 }
6390
6391 // Fill in the "end" label in jumps at the end of the blocks, if not done
6392 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006393 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006394
6395 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006396 if (isn->isn_arg.try.try_catch == 0)
6397 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006398 if (isn->isn_arg.try.try_finally == 0)
6399 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006400
6401 if (scope->se_u.se_try.ts_catch_label != 0)
6402 {
6403 // Last catch without match jumps here
6404 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6405 isn->isn_arg.jump.jump_where = instr->ga_len;
6406 }
6407
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006408 compile_endblock(cctx);
6409
6410 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6411 return NULL;
6412 return arg;
6413}
6414
6415/*
6416 * compile "throw {expr}"
6417 */
6418 static char_u *
6419compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6420{
6421 char_u *p = skipwhite(arg);
6422
Bram Moolenaara5565e42020-05-09 15:44:01 +02006423 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006424 return NULL;
6425 if (may_generate_2STRING(-1, cctx) == FAIL)
6426 return NULL;
6427 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6428 return NULL;
6429
6430 return p;
6431}
6432
6433/*
6434 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006435 * compile "echomsg expr"
6436 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006437 * compile "execute expr"
6438 */
6439 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006440compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006441{
6442 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006443 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006444 int count = 0;
6445
6446 for (;;)
6447 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006448 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006449 return NULL;
6450 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006451 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006452 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006453 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006454 break;
6455 }
6456
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006457 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6458 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6459 else if (cmdidx == CMD_execute)
6460 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6461 else if (cmdidx == CMD_echomsg)
6462 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6463 else
6464 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006465 return p;
6466}
6467
6468/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006469 * :put r
6470 * :put ={expr}
6471 */
6472 static char_u *
6473compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6474{
6475 char_u *line = arg;
6476 linenr_T lnum;
6477 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006478 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006479
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006480 eap->regname = *line;
6481
6482 if (eap->regname == '=')
6483 {
6484 char_u *p = line + 1;
6485
6486 if (compile_expr0(&p, cctx) == FAIL)
6487 return NULL;
6488 line = p;
6489 }
6490 else if (eap->regname != NUL)
6491 ++line;
6492
6493 // TODO: if the range is something like "$" need to evaluate at runtime
6494 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6495 return NULL;
6496 if (eap->addr_count == 0)
6497 lnum = -1;
6498 else
6499 lnum = eap->line2;
6500 if (above)
6501 --lnum;
6502
6503 generate_PUT(cctx, eap->regname, lnum);
6504 return line;
6505}
6506
6507/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006508 * A command that is not compiled, execute with legacy code.
6509 */
6510 static char_u *
6511compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6512{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006513 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006514 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006515 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006516
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006517 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006518 goto theend;
6519
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006520 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006521 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006522 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006523 int usefilter = FALSE;
6524
6525 has_expr = argt & (EX_XFILE | EX_EXPAND);
6526
6527 // If the command can be followed by a bar, find the bar and truncate
6528 // it, so that the following command can be compiled.
6529 // The '|' is overwritten with a NUL, it is put back below.
6530 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6531 && *eap->arg == '!')
6532 // :w !filter or :r !filter or :r! filter
6533 usefilter = TRUE;
6534 if ((argt & EX_TRLBAR) && !usefilter)
6535 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006536 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006537 separate_nextcmd(eap);
6538 if (eap->nextcmd != NULL)
6539 nextcmd = eap->nextcmd;
6540 }
6541 }
6542
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006543 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6544 {
6545 // expand filename in "syntax include [@group] filename"
6546 has_expr = TRUE;
6547 eap->arg = skipwhite(eap->arg + 7);
6548 if (*eap->arg == '@')
6549 eap->arg = skiptowhite(eap->arg);
6550 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006551
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006552 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006553 {
6554 int count = 0;
6555 char_u *start = skipwhite(line);
6556
6557 // :cmd xxx`=expr1`yyy`=expr2`zzz
6558 // PUSHS ":cmd xxx"
6559 // eval expr1
6560 // PUSHS "yyy"
6561 // eval expr2
6562 // PUSHS "zzz"
6563 // EXECCONCAT 5
6564 for (;;)
6565 {
6566 if (p > start)
6567 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006568 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006569 ++count;
6570 }
6571 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006572 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006573 return NULL;
6574 may_generate_2STRING(-1, cctx);
6575 ++count;
6576 p = skipwhite(p);
6577 if (*p != '`')
6578 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006579 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006580 return NULL;
6581 }
6582 start = p + 1;
6583
6584 p = (char_u *)strstr((char *)start, "`=");
6585 if (p == NULL)
6586 {
6587 if (*skipwhite(start) != NUL)
6588 {
6589 generate_PUSHS(cctx, vim_strsave(start));
6590 ++count;
6591 }
6592 break;
6593 }
6594 }
6595 generate_EXECCONCAT(cctx, count);
6596 }
6597 else
6598 generate_EXEC(cctx, line);
6599
6600theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006601 if (*nextcmd != NUL)
6602 {
6603 // the parser expects a pointer to the bar, put it back
6604 --nextcmd;
6605 *nextcmd = '|';
6606 }
6607
6608 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006609}
6610
6611/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006612 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006613 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006614 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006615 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006616add_def_function(ufunc_T *ufunc)
6617{
6618 dfunc_T *dfunc;
6619
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006620 if (def_functions.ga_len == 0)
6621 {
6622 // The first position is not used, so that a zero uf_dfunc_idx means it
6623 // wasn't set.
6624 if (ga_grow(&def_functions, 1) == FAIL)
6625 return FAIL;
6626 ++def_functions.ga_len;
6627 }
6628
Bram Moolenaar09689a02020-05-09 22:50:08 +02006629 // Add the function to "def_functions".
6630 if (ga_grow(&def_functions, 1) == FAIL)
6631 return FAIL;
6632 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6633 CLEAR_POINTER(dfunc);
6634 dfunc->df_idx = def_functions.ga_len;
6635 ufunc->uf_dfunc_idx = dfunc->df_idx;
6636 dfunc->df_ufunc = ufunc;
6637 ++def_functions.ga_len;
6638 return OK;
6639}
6640
6641/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006642 * After ex_function() has collected all the function lines: parse and compile
6643 * the lines into instructions.
6644 * Adds the function to "def_functions".
6645 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6646 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006647 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006648 * This can be used recursively through compile_lambda(), which may reallocate
6649 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006650 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006651 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006652 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006653compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006654{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006655 char_u *line = NULL;
6656 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006657 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006658 cctx_T cctx;
6659 garray_T *instr;
6660 int called_emsg_before = called_emsg;
6661 int ret = FAIL;
6662 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006663 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006664 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006665 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006666
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006667 // When using a function that was compiled before: Free old instructions.
6668 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006669 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006670 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006671 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6672 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006673 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006674 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006675 else
6676 {
6677 if (add_def_function(ufunc) == FAIL)
6678 return FAIL;
6679 new_def_function = TRUE;
6680 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006681
Bram Moolenaar985116a2020-07-12 17:31:09 +02006682 ufunc->uf_def_status = UF_COMPILING;
6683
Bram Moolenaara80faa82020-04-12 19:37:17 +02006684 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006685 cctx.ctx_ufunc = ufunc;
6686 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006687 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006688 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6689 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6690 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6691 cctx.ctx_type_list = &ufunc->uf_type_list;
6692 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6693 instr = &cctx.ctx_instr;
6694
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006695 // Set the context to the function, it may be compiled when called from
6696 // another script. Set the script version to the most modern one.
6697 // The line number will be set in next_line_from_context().
6698 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006699 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6700
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006701 // Make sure error messages are OK.
6702 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6703 if (do_estack_push)
6704 estack_push_ufunc(ufunc, 1);
6705
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006706 if (ufunc->uf_def_args.ga_len > 0)
6707 {
6708 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006709 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006710 int i;
6711 char_u *arg;
6712 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006713 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006714
6715 // Produce instructions for the default values of optional arguments.
6716 // Store the instruction index in uf_def_arg_idx[] so that we know
6717 // where to start when the function is called, depending on the number
6718 // of arguments.
6719 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6720 if (ufunc->uf_def_arg_idx == NULL)
6721 goto erret;
6722 for (i = 0; i < count; ++i)
6723 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006724 garray_T *stack = &cctx.ctx_type_stack;
6725 type_T *val_type;
6726 int arg_idx = first_def_arg + i;
6727
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006728 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6729 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006730 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006731 goto erret;
6732
6733 // If no type specified use the type of the default value.
6734 // Otherwise check that the default value type matches the
6735 // specified type.
6736 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6737 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006738 {
6739 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006740 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006741 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02006742 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
6743 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006744 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006745
6746 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006747 goto erret;
6748 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006749 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006750
6751 if (did_set_arg_type)
6752 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006753 }
6754
6755 /*
6756 * Loop over all the lines of the function and generate instructions.
6757 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006758 for (;;)
6759 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006760 exarg_T ea;
6761 cmdmod_T save_cmdmod;
6762 int starts_with_colon = FALSE;
6763 char_u *cmd;
6764 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006765
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006766 // Bail out on the first error to avoid a flood of errors and report
6767 // the right line number when inside try/catch.
6768 if (emsg_before != called_emsg)
6769 goto erret;
6770
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006771 if (line != NULL && *line == '|')
6772 // the line continues after a '|'
6773 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006774 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006775 && !(*line == '#' && (line == cctx.ctx_line_start
6776 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006777 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006778 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006779 goto erret;
6780 }
6781 else
6782 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006783 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006784 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006785 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006786 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006787 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006788 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006789
Bram Moolenaara80faa82020-04-12 19:37:17 +02006790 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006791 ea.cmdlinep = &line;
6792 ea.cmd = skipwhite(line);
6793
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006794 // Some things can be recognized by the first character.
6795 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006796 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006797 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006798 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006799 if (ea.cmd[1] != '{')
6800 {
6801 line = (char_u *)"";
6802 continue;
6803 }
6804 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006805
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006806 case '}':
6807 {
6808 // "}" ends a block scope
6809 scopetype_T stype = cctx.ctx_scope == NULL
6810 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006811
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006812 if (stype == BLOCK_SCOPE)
6813 {
6814 compile_endblock(&cctx);
6815 line = ea.cmd;
6816 }
6817 else
6818 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006819 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006820 goto erret;
6821 }
6822 if (line != NULL)
6823 line = skipwhite(ea.cmd + 1);
6824 continue;
6825 }
6826
6827 case '{':
6828 // "{" starts a block scope
6829 // "{'a': 1}->func() is something else
6830 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6831 {
6832 line = compile_block(ea.cmd, &cctx);
6833 continue;
6834 }
6835 break;
6836
6837 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006838 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006839 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006840 }
6841
6842 /*
6843 * COMMAND MODIFIERS
6844 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006845 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006846 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6847 {
6848 if (errormsg != NULL)
6849 goto erret;
6850 // empty line or comment
6851 line = (char_u *)"";
6852 continue;
6853 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006854 // TODO: use modifiers in the command
6855 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006856 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006857
6858 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006859 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006860 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006861 {
6862 if (*ea.cmd == '(')
6863 // not for "call()"
6864 ea.cmd = p;
6865 else
6866 ea.cmd = skipwhite(ea.cmd);
6867 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006868
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006869 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006870 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006871 char_u *pskip;
6872
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006873 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006874 // find what follows.
6875 // Skip over "var.member", "var[idx]" and the like.
6876 // Also "&opt = val", "$ENV = val" and "@r = val".
6877 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006878 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006879 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006880 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006881 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006882 char_u *var_end;
6883 int oplen;
6884 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006885
Bram Moolenaar65821722020-08-02 18:58:54 +02006886 if (ea.cmd[0] == '@')
6887 var_end = ea.cmd + 2;
6888 else
6889 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006890 FNE_CHECK_START | FNE_INCL_BR);
6891 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006892 if (oplen > 0)
6893 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006894 size_t len = p - ea.cmd;
6895
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006896 // Recognize an assignment if we recognize the variable
6897 // name:
6898 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006899 // "local = expr" where "local" is a local var.
6900 // "script = expr" where "script" is a script-local var.
6901 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006902 // "&opt = expr"
6903 // "$ENV = expr"
6904 // "@r = expr"
6905 if (*ea.cmd == '&'
6906 || *ea.cmd == '$'
6907 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006908 || ((len) > 2 && ea.cmd[1] == ':')
6909 || lookup_local(ea.cmd, len, &cctx) != NULL
6910 || lookup_arg(ea.cmd, len, NULL, NULL,
6911 NULL, &cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02006912 || lookup_script(ea.cmd, len, FALSE) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006913 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006914 {
6915 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006916 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006917 goto erret;
6918 continue;
6919 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006920 }
6921 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006922
6923 if (*ea.cmd == '[')
6924 {
6925 // [var, var] = expr
6926 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6927 if (line == NULL)
6928 goto erret;
6929 if (line != ea.cmd)
6930 continue;
6931 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006932 }
6933
6934 /*
6935 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006936 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006937 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006938 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006939 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006940 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02006941 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006942 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006943 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006944 if (!starts_with_colon)
6945 {
6946 emsg(_(e_colon_required_before_a_range));
6947 goto erret;
6948 }
6949 if (ends_excmd2(line, ea.cmd))
6950 {
6951 // A range without a command: jump to the line.
6952 // TODO: compile to a more efficient command, possibly
6953 // calling parse_cmd_address().
6954 ea.cmdidx = CMD_SIZE;
6955 line = compile_exec(line, &ea, &cctx);
6956 goto nextline;
6957 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006958 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006959 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006960 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006961 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006962 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006963
6964 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6965 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006966 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006967 {
6968 line += STRLEN(line);
6969 continue;
6970 }
6971
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006972 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006973 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006974 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006975 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006976 iemsg("Command from find_ex_command() not handled");
6977 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006978 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006979 }
6980
Bram Moolenaar3988f642020-08-27 22:43:03 +02006981 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006982 && ea.cmdidx != CMD_elseif
6983 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02006984 && ea.cmdidx != CMD_endif
6985 && ea.cmdidx != CMD_endfor
6986 && ea.cmdidx != CMD_endwhile
6987 && ea.cmdidx != CMD_catch
6988 && ea.cmdidx != CMD_finally
6989 && ea.cmdidx != CMD_endtry)
6990 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02006991 emsg(_(e_unreachable_code_after_return));
6992 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006993 }
6994
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006995 p = skipwhite(p);
6996 if (ea.cmdidx != CMD_SIZE
6997 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
6998 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02006999 if (ea.cmdidx >= 0)
7000 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007001 if ((ea.argt & EX_BANG) && *p == '!')
7002 {
7003 ea.forceit = TRUE;
7004 p = skipwhite(p + 1);
7005 }
7006 }
7007
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007008 switch (ea.cmdidx)
7009 {
7010 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007011 ea.arg = p;
7012 line = compile_nested_function(&ea, &cctx);
7013 break;
7014
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007015 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007016 // TODO: should we allow this, e.g. to declare a global
7017 // function?
7018 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007019 goto erret;
7020
7021 case CMD_return:
7022 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007023 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007024 break;
7025
7026 case CMD_let:
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02007027 if (get_vim_var_nr(VV_DISALLOW_LET))
7028 {
7029 emsg(_(e_cannot_use_let_in_vim9_script));
7030 break;
7031 }
7032 // FALLTHROUGH
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007033 case CMD_var:
7034 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007035 case CMD_const:
7036 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007037 if (line == p)
7038 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007039 break;
7040
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007041 case CMD_unlet:
7042 case CMD_unlockvar:
7043 case CMD_lockvar:
7044 line = compile_unletlock(p, &ea, &cctx);
7045 break;
7046
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007047 case CMD_import:
7048 line = compile_import(p, &cctx);
7049 break;
7050
7051 case CMD_if:
7052 line = compile_if(p, &cctx);
7053 break;
7054 case CMD_elseif:
7055 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007056 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007057 break;
7058 case CMD_else:
7059 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007060 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007061 break;
7062 case CMD_endif:
7063 line = compile_endif(p, &cctx);
7064 break;
7065
7066 case CMD_while:
7067 line = compile_while(p, &cctx);
7068 break;
7069 case CMD_endwhile:
7070 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007071 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007072 break;
7073
7074 case CMD_for:
7075 line = compile_for(p, &cctx);
7076 break;
7077 case CMD_endfor:
7078 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007079 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007080 break;
7081 case CMD_continue:
7082 line = compile_continue(p, &cctx);
7083 break;
7084 case CMD_break:
7085 line = compile_break(p, &cctx);
7086 break;
7087
7088 case CMD_try:
7089 line = compile_try(p, &cctx);
7090 break;
7091 case CMD_catch:
7092 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007093 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007094 break;
7095 case CMD_finally:
7096 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007097 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007098 break;
7099 case CMD_endtry:
7100 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007101 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007102 break;
7103 case CMD_throw:
7104 line = compile_throw(p, &cctx);
7105 break;
7106
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007107 case CMD_eval:
7108 if (compile_expr0(&p, &cctx) == FAIL)
7109 goto erret;
7110
Bram Moolenaar3988f642020-08-27 22:43:03 +02007111 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007112 generate_instr_drop(&cctx, ISN_DROP, 1);
7113
7114 line = skipwhite(p);
7115 break;
7116
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007117 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007118 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007119 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007120 case CMD_echomsg:
7121 case CMD_echoerr:
7122 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007123 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007124
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007125 case CMD_put:
7126 ea.cmd = cmd;
7127 line = compile_put(p, &ea, &cctx);
7128 break;
7129
Bram Moolenaar3988f642020-08-27 22:43:03 +02007130 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007131
Bram Moolenaarae616492020-07-28 20:07:27 +02007132 case CMD_append:
7133 case CMD_change:
7134 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007135 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007136 case CMD_xit:
7137 not_in_vim9(&ea);
7138 goto erret;
7139
Bram Moolenaar002262f2020-07-08 17:47:57 +02007140 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007141 if (cctx.ctx_skip != SKIP_YES)
7142 {
7143 semsg(_(e_invalid_command_str), ea.cmd);
7144 goto erret;
7145 }
7146 // We don't check for a next command here.
7147 line = (char_u *)"";
7148 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007149
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007150 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007151 if (cctx.ctx_skip == SKIP_YES)
7152 {
7153 // We don't check for a next command here.
7154 line = (char_u *)"";
7155 }
7156 else
7157 {
7158 // Not recognized, execute with do_cmdline_cmd().
7159 ea.arg = p;
7160 line = compile_exec(line, &ea, &cctx);
7161 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007162 break;
7163 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007164nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007165 if (line == NULL)
7166 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007167 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007168
7169 if (cctx.ctx_type_stack.ga_len < 0)
7170 {
7171 iemsg("Type stack underflow");
7172 goto erret;
7173 }
7174 }
7175
7176 if (cctx.ctx_scope != NULL)
7177 {
7178 if (cctx.ctx_scope->se_type == IF_SCOPE)
7179 emsg(_(e_endif));
7180 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7181 emsg(_(e_endwhile));
7182 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7183 emsg(_(e_endfor));
7184 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007185 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007186 goto erret;
7187 }
7188
Bram Moolenaarefd88552020-06-18 20:50:10 +02007189 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007190 {
7191 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7192 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007193 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007194 goto erret;
7195 }
7196
7197 // Return zero if there is no return at the end.
7198 generate_PUSHNR(&cctx, 0);
7199 generate_instr(&cctx, ISN_RETURN);
7200 }
7201
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007202 {
7203 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7204 + ufunc->uf_dfunc_idx;
7205 dfunc->df_deleted = FALSE;
7206 dfunc->df_instr = instr->ga_data;
7207 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007208 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007209 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007210 if (cctx.ctx_outer_used)
7211 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007212 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007213 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007214
7215 ret = OK;
7216
7217erret:
7218 if (ret == FAIL)
7219 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007220 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007221 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7222 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007223
7224 for (idx = 0; idx < instr->ga_len; ++idx)
7225 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007226 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007227
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007228 // If using the last entry in the table and it was added above, we
7229 // might as well remove it.
7230 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007231 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007232 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007233 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007234 ufunc->uf_dfunc_idx = 0;
7235 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007236 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007237
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007238 while (cctx.ctx_scope != NULL)
7239 drop_scope(&cctx);
7240
Bram Moolenaar20431c92020-03-20 18:39:46 +01007241 // Don't execute this function body.
7242 ga_clear_strings(&ufunc->uf_lines);
7243
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007244 if (errormsg != NULL)
7245 emsg(errormsg);
7246 else if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007247 emsg(_(e_compile_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007248 }
7249
7250 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007251 if (do_estack_push)
7252 estack_pop();
7253
Bram Moolenaar20431c92020-03-20 18:39:46 +01007254 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007255 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007256 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007257 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007258}
7259
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007260 void
7261set_function_type(ufunc_T *ufunc)
7262{
7263 int varargs = ufunc->uf_va_name != NULL;
7264 int argcount = ufunc->uf_args.ga_len;
7265
7266 // Create a type for the function, with the return type and any
7267 // argument types.
7268 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7269 // The type is included in "tt_args".
7270 if (argcount > 0 || varargs)
7271 {
7272 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7273 argcount, &ufunc->uf_type_list);
7274 // Add argument types to the function type.
7275 if (func_type_add_arg_types(ufunc->uf_func_type,
7276 argcount + varargs,
7277 &ufunc->uf_type_list) == FAIL)
7278 return;
7279 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7280 ufunc->uf_func_type->tt_min_argcount =
7281 argcount - ufunc->uf_def_args.ga_len;
7282 if (ufunc->uf_arg_types == NULL)
7283 {
7284 int i;
7285
7286 // lambda does not have argument types.
7287 for (i = 0; i < argcount; ++i)
7288 ufunc->uf_func_type->tt_args[i] = &t_any;
7289 }
7290 else
7291 mch_memmove(ufunc->uf_func_type->tt_args,
7292 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7293 if (varargs)
7294 {
7295 ufunc->uf_func_type->tt_args[argcount] =
7296 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7297 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7298 }
7299 }
7300 else
7301 // No arguments, can use a predefined type.
7302 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7303 argcount, &ufunc->uf_type_list);
7304}
7305
7306
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007307/*
7308 * Delete an instruction, free what it contains.
7309 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007310 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007311delete_instr(isn_T *isn)
7312{
7313 switch (isn->isn_type)
7314 {
7315 case ISN_EXEC:
7316 case ISN_LOADENV:
7317 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007318 case ISN_LOADB:
7319 case ISN_LOADW:
7320 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007321 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007322 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007323 case ISN_PUSHEXC:
7324 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007325 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007326 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007327 case ISN_STOREB:
7328 case ISN_STOREW:
7329 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007330 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007331 vim_free(isn->isn_arg.string);
7332 break;
7333
7334 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007335 case ISN_STORES:
7336 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007337 break;
7338
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007339 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007340 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007341 vim_free(isn->isn_arg.unlet.ul_name);
7342 break;
7343
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007344 case ISN_STOREOPT:
7345 vim_free(isn->isn_arg.storeopt.so_name);
7346 break;
7347
7348 case ISN_PUSHBLOB: // push blob isn_arg.blob
7349 blob_unref(isn->isn_arg.blob);
7350 break;
7351
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007352 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007353#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007354 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007355#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007356 break;
7357
7358 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007359#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007360 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007361#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007362 break;
7363
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007364 case ISN_UCALL:
7365 vim_free(isn->isn_arg.ufunc.cuf_name);
7366 break;
7367
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007368 case ISN_FUNCREF:
7369 {
7370 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7371 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007372
7373 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7374 func_ptr_unref(dfunc->df_ufunc);
7375 }
7376 break;
7377
7378 case ISN_DCALL:
7379 {
7380 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7381 + isn->isn_arg.dfunc.cdf_idx;
7382
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007383 if (dfunc->df_ufunc != NULL
7384 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007385 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007386 }
7387 break;
7388
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007389 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007390 {
7391 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7392 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7393
7394 if (ufunc != NULL)
7395 {
7396 // Clear uf_dfunc_idx so that the function is deleted.
7397 clear_def_function(ufunc);
7398 ufunc->uf_dfunc_idx = 0;
7399 func_ptr_unref(ufunc);
7400 }
7401
7402 vim_free(lambda);
7403 vim_free(isn->isn_arg.newfunc.nf_global);
7404 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007405 break;
7406
Bram Moolenaar5e654232020-09-16 15:22:00 +02007407 case ISN_CHECKTYPE:
7408 free_type(isn->isn_arg.type.ct_type);
7409 break;
7410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007411 case ISN_2BOOL:
7412 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007413 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007414 case ISN_ADDBLOB:
7415 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007416 case ISN_ANYINDEX:
7417 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007418 case ISN_BCALL:
7419 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007420 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007421 case ISN_CHECKNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007422 case ISN_COMPAREANY:
7423 case ISN_COMPAREBLOB:
7424 case ISN_COMPAREBOOL:
7425 case ISN_COMPAREDICT:
7426 case ISN_COMPAREFLOAT:
7427 case ISN_COMPAREFUNC:
7428 case ISN_COMPARELIST:
7429 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007430 case ISN_COMPARESPECIAL:
7431 case ISN_COMPARESTRING:
7432 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02007433 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007434 case ISN_DROP:
7435 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007436 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007437 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007438 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007439 case ISN_EXECCONCAT:
7440 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007441 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007442 case ISN_GETITEM:
7443 case ISN_JUMP:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007444 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007445 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007446 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007447 case ISN_LOADBDICT:
7448 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007449 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007450 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007451 case ISN_LOADSCRIPT:
7452 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007453 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007454 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007455 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007456 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007457 case ISN_NEGATENR:
7458 case ISN_NEWDICT:
7459 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007460 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007461 case ISN_OPFLOAT:
7462 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007463 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007464 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007465 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007466 case ISN_PUSHF:
7467 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007468 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007469 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007470 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007471 case ISN_SHUFFLE:
7472 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007473 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007474 case ISN_STOREDICT:
7475 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007476 case ISN_STORENR:
7477 case ISN_STOREOUTER:
7478 case ISN_STOREREG:
7479 case ISN_STORESCRIPT:
7480 case ISN_STOREV:
7481 case ISN_STRINDEX:
7482 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007483 case ISN_THROW:
7484 case ISN_TRY:
7485 // nothing allocated
7486 break;
7487 }
7488}
7489
7490/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007491 * Free all instructions for "dfunc".
7492 */
7493 static void
7494delete_def_function_contents(dfunc_T *dfunc)
7495{
7496 int idx;
7497
7498 ga_clear(&dfunc->df_def_args_isn);
7499
7500 if (dfunc->df_instr != NULL)
7501 {
7502 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7503 delete_instr(dfunc->df_instr + idx);
7504 VIM_CLEAR(dfunc->df_instr);
7505 }
7506
7507 dfunc->df_deleted = TRUE;
7508}
7509
7510/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007511 * When a user function is deleted, clear the contents of any associated def
7512 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007513 */
7514 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007515clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007516{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007517 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007518 {
7519 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7520 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007521
Bram Moolenaar20431c92020-03-20 18:39:46 +01007522 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007523 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007524 }
7525}
7526
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007527/*
7528 * Used when a user function is about to be deleted: remove the pointer to it.
7529 * The entry in def_functions is then unused.
7530 */
7531 void
7532unlink_def_function(ufunc_T *ufunc)
7533{
7534 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7535
7536 dfunc->df_ufunc = NULL;
7537}
7538
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007539#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007540/*
7541 * Free all functions defined with ":def".
7542 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007543 void
7544free_def_functions(void)
7545{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007546 int idx;
7547
7548 for (idx = 0; idx < def_functions.ga_len; ++idx)
7549 {
7550 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7551
7552 delete_def_function_contents(dfunc);
7553 }
7554
7555 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007556}
7557#endif
7558
7559
7560#endif // FEAT_EVAL