blob: 32def6c7962cb0604119e475dcb57081ee000fad [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
709 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200710generate_TYPECHECK(
711 cctx_T *cctx,
712 type_T *expected,
713 int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100714{
715 isn_T *isn;
716 garray_T *stack = &cctx->ctx_type_stack;
717
Bram Moolenaar080457c2020-03-03 21:53:32 +0100718 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100719 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
720 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200721 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722 isn->isn_arg.type.ct_off = offset;
723
Bram Moolenaar5e654232020-09-16 15:22:00 +0200724 // type becomes expected
725 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100726
727 return OK;
728}
729
730/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200731 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200732 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200733 * - "actual" is a type that can be "expected" type: add a runtime check; or
734 * - return FAIL.
735 */
736 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200737need_type(
738 type_T *actual,
739 type_T *expected,
740 int offset,
741 cctx_T *cctx,
742 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200743{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200744 if (expected == &t_bool && actual != &t_bool
745 && (actual->tt_flags & TTFLAG_BOOL_OK))
746 {
747 // Using "0", "1" or the result of an expression with "&&" or "||" as a
748 // boolean is OK but requires a conversion.
749 generate_2BOOL(cctx, FALSE);
750 return OK;
751 }
752
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200753 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200754 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200755
756 // If the actual type can be the expected type add a runtime check.
757 // TODO: if it's a constant a runtime check makes no sense.
758 if (actual->tt_type == VAR_ANY
759 || actual->tt_type == VAR_UNKNOWN
760 || (actual->tt_type == VAR_FUNC
761 && (expected->tt_type == VAR_FUNC
762 || expected->tt_type == VAR_PARTIAL)
763 && (actual->tt_member == &t_any || actual->tt_argcount < 0))
764 || (actual->tt_type == VAR_LIST
765 && expected->tt_type == VAR_LIST
766 && actual->tt_member == &t_any)
767 || (actual->tt_type == VAR_DICT
768 && expected->tt_type == VAR_DICT
769 && actual->tt_member == &t_any))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200770 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200771 generate_TYPECHECK(cctx, expected, offset);
772 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200773 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200774
775 if (!silent)
776 type_mismatch(expected, actual);
777 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200778}
779
780/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100781 * Generate an ISN_PUSHNR instruction.
782 */
783 static int
784generate_PUSHNR(cctx_T *cctx, varnumber_T number)
785{
786 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200787 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100788
Bram Moolenaar080457c2020-03-03 21:53:32 +0100789 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100790 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
791 return FAIL;
792 isn->isn_arg.number = number;
793
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200794 if (number == 0 || number == 1)
795 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200796 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200797
798 // A 0 or 1 number can also be used as a bool.
799 if (type != NULL)
800 {
801 type->tt_type = VAR_NUMBER;
802 type->tt_flags = TTFLAG_BOOL_OK;
803 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
804 }
805 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806 return OK;
807}
808
809/*
810 * Generate an ISN_PUSHBOOL instruction.
811 */
812 static int
813generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
814{
815 isn_T *isn;
816
Bram Moolenaar080457c2020-03-03 21:53:32 +0100817 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100818 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
819 return FAIL;
820 isn->isn_arg.number = number;
821
822 return OK;
823}
824
825/*
826 * Generate an ISN_PUSHSPEC instruction.
827 */
828 static int
829generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
830{
831 isn_T *isn;
832
Bram Moolenaar080457c2020-03-03 21:53:32 +0100833 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100834 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
835 return FAIL;
836 isn->isn_arg.number = number;
837
838 return OK;
839}
840
841#ifdef FEAT_FLOAT
842/*
843 * Generate an ISN_PUSHF instruction.
844 */
845 static int
846generate_PUSHF(cctx_T *cctx, float_T fnumber)
847{
848 isn_T *isn;
849
Bram Moolenaar080457c2020-03-03 21:53:32 +0100850 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100851 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
852 return FAIL;
853 isn->isn_arg.fnumber = fnumber;
854
855 return OK;
856}
857#endif
858
859/*
860 * Generate an ISN_PUSHS instruction.
861 * Consumes "str".
862 */
863 static int
864generate_PUSHS(cctx_T *cctx, char_u *str)
865{
866 isn_T *isn;
867
Bram Moolenaar080457c2020-03-03 21:53:32 +0100868 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100869 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
870 return FAIL;
871 isn->isn_arg.string = str;
872
873 return OK;
874}
875
876/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100877 * Generate an ISN_PUSHCHANNEL instruction.
878 * Consumes "channel".
879 */
880 static int
881generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
882{
883 isn_T *isn;
884
Bram Moolenaar080457c2020-03-03 21:53:32 +0100885 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100886 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
887 return FAIL;
888 isn->isn_arg.channel = channel;
889
890 return OK;
891}
892
893/*
894 * Generate an ISN_PUSHJOB instruction.
895 * Consumes "job".
896 */
897 static int
898generate_PUSHJOB(cctx_T *cctx, job_T *job)
899{
900 isn_T *isn;
901
Bram Moolenaar080457c2020-03-03 21:53:32 +0100902 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100903 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100904 return FAIL;
905 isn->isn_arg.job = job;
906
907 return OK;
908}
909
910/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100911 * Generate an ISN_PUSHBLOB instruction.
912 * Consumes "blob".
913 */
914 static int
915generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
916{
917 isn_T *isn;
918
Bram Moolenaar080457c2020-03-03 21:53:32 +0100919 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100920 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
921 return FAIL;
922 isn->isn_arg.blob = blob;
923
924 return OK;
925}
926
927/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100928 * Generate an ISN_PUSHFUNC instruction with name "name".
929 * Consumes "name".
930 */
931 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200932generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100933{
934 isn_T *isn;
935
Bram Moolenaar080457c2020-03-03 21:53:32 +0100936 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200937 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100938 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200939 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100940
941 return OK;
942}
943
944/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200945 * Generate an ISN_GETITEM instruction with "index".
946 */
947 static int
948generate_GETITEM(cctx_T *cctx, int index)
949{
950 isn_T *isn;
951 garray_T *stack = &cctx->ctx_type_stack;
952 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
953 type_T *item_type = &t_any;
954
955 RETURN_OK_IF_SKIP(cctx);
956
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200957 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200958 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200959 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200960 emsg(_(e_listreq));
961 return FAIL;
962 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200963 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200964 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
965 return FAIL;
966 isn->isn_arg.number = index;
967
968 // add the item type to the type stack
969 if (ga_grow(stack, 1) == FAIL)
970 return FAIL;
971 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
972 ++stack->ga_len;
973 return OK;
974}
975
976/*
Bram Moolenaar9af78762020-06-16 11:34:42 +0200977 * Generate an ISN_SLICE instruction with "count".
978 */
979 static int
980generate_SLICE(cctx_T *cctx, int count)
981{
982 isn_T *isn;
983
984 RETURN_OK_IF_SKIP(cctx);
985 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
986 return FAIL;
987 isn->isn_arg.number = count;
988 return OK;
989}
990
991/*
992 * Generate an ISN_CHECKLEN instruction with "min_len".
993 */
994 static int
995generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
996{
997 isn_T *isn;
998
999 RETURN_OK_IF_SKIP(cctx);
1000
1001 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1002 return FAIL;
1003 isn->isn_arg.checklen.cl_min_len = min_len;
1004 isn->isn_arg.checklen.cl_more_OK = more_OK;
1005
1006 return OK;
1007}
1008
1009/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001010 * Generate an ISN_STORE instruction.
1011 */
1012 static int
1013generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1014{
1015 isn_T *isn;
1016
Bram Moolenaar080457c2020-03-03 21:53:32 +01001017 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001018 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1019 return FAIL;
1020 if (name != NULL)
1021 isn->isn_arg.string = vim_strsave(name);
1022 else
1023 isn->isn_arg.number = idx;
1024
1025 return OK;
1026}
1027
1028/*
1029 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1030 */
1031 static int
1032generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
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(cctx, ISN_STORENR)) == NULL)
1038 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001039 isn->isn_arg.storenr.stnr_idx = idx;
1040 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001041
1042 return OK;
1043}
1044
1045/*
1046 * Generate an ISN_STOREOPT instruction
1047 */
1048 static int
1049generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1050{
1051 isn_T *isn;
1052
Bram Moolenaar080457c2020-03-03 21:53:32 +01001053 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001054 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001055 return FAIL;
1056 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1057 isn->isn_arg.storeopt.so_flags = opt_flags;
1058
1059 return OK;
1060}
1061
1062/*
1063 * Generate an ISN_LOAD or similar instruction.
1064 */
1065 static int
1066generate_LOAD(
1067 cctx_T *cctx,
1068 isntype_T isn_type,
1069 int idx,
1070 char_u *name,
1071 type_T *type)
1072{
1073 isn_T *isn;
1074
Bram Moolenaar080457c2020-03-03 21:53:32 +01001075 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001076 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1077 return FAIL;
1078 if (name != NULL)
1079 isn->isn_arg.string = vim_strsave(name);
1080 else
1081 isn->isn_arg.number = idx;
1082
1083 return OK;
1084}
1085
1086/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001087 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001088 */
1089 static int
1090generate_LOADV(
1091 cctx_T *cctx,
1092 char_u *name,
1093 int error)
1094{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001095 int di_flags;
1096 int vidx = find_vim_var(name, &di_flags);
1097 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001098
Bram Moolenaar080457c2020-03-03 21:53:32 +01001099 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001100 if (vidx < 0)
1101 {
1102 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001103 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001104 return FAIL;
1105 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001106 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001107
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001108 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001109}
1110
1111/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001112 * Generate an ISN_UNLET instruction.
1113 */
1114 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001115generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001116{
1117 isn_T *isn;
1118
1119 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001120 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001121 return FAIL;
1122 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1123 isn->isn_arg.unlet.ul_forceit = forceit;
1124
1125 return OK;
1126}
1127
1128/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001129 * Generate an ISN_LOCKCONST instruction.
1130 */
1131 static int
1132generate_LOCKCONST(cctx_T *cctx)
1133{
1134 isn_T *isn;
1135
1136 RETURN_OK_IF_SKIP(cctx);
1137 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1138 return FAIL;
1139 return OK;
1140}
1141
1142/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001143 * Generate an ISN_LOADS instruction.
1144 */
1145 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001146generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001147 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001148 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001150 int sid,
1151 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001152{
1153 isn_T *isn;
1154
Bram Moolenaar080457c2020-03-03 21:53:32 +01001155 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001156 if (isn_type == ISN_LOADS)
1157 isn = generate_instr_type(cctx, isn_type, type);
1158 else
1159 isn = generate_instr_drop(cctx, isn_type, 1);
1160 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001161 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001162 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1163 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001164
1165 return OK;
1166}
1167
1168/*
1169 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1170 */
1171 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001172generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001173 cctx_T *cctx,
1174 isntype_T isn_type,
1175 int sid,
1176 int idx,
1177 type_T *type)
1178{
1179 isn_T *isn;
1180
Bram Moolenaar080457c2020-03-03 21:53:32 +01001181 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001182 if (isn_type == ISN_LOADSCRIPT)
1183 isn = generate_instr_type(cctx, isn_type, type);
1184 else
1185 isn = generate_instr_drop(cctx, isn_type, 1);
1186 if (isn == NULL)
1187 return FAIL;
1188 isn->isn_arg.script.script_sid = sid;
1189 isn->isn_arg.script.script_idx = idx;
1190 return OK;
1191}
1192
1193/*
1194 * Generate an ISN_NEWLIST instruction.
1195 */
1196 static int
1197generate_NEWLIST(cctx_T *cctx, int count)
1198{
1199 isn_T *isn;
1200 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001201 type_T *type;
1202 type_T *member;
1203
Bram Moolenaar080457c2020-03-03 21:53:32 +01001204 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001205 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1206 return FAIL;
1207 isn->isn_arg.number = count;
1208
Bram Moolenaar127542b2020-08-09 17:22:04 +02001209 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001210 if (count == 0)
1211 member = &t_void;
1212 else
1213 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001214 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1215 cctx->ctx_type_list);
1216 type = get_list_type(member, cctx->ctx_type_list);
1217
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001218 // drop the value types
1219 stack->ga_len -= count;
1220
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001221 // add the list type to the type stack
1222 if (ga_grow(stack, 1) == FAIL)
1223 return FAIL;
1224 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1225 ++stack->ga_len;
1226
1227 return OK;
1228}
1229
1230/*
1231 * Generate an ISN_NEWDICT instruction.
1232 */
1233 static int
1234generate_NEWDICT(cctx_T *cctx, int count)
1235{
1236 isn_T *isn;
1237 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001238 type_T *type;
1239 type_T *member;
1240
Bram Moolenaar080457c2020-03-03 21:53:32 +01001241 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001242 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1243 return FAIL;
1244 isn->isn_arg.number = count;
1245
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001246 if (count == 0)
1247 member = &t_void;
1248 else
1249 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001250 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1251 cctx->ctx_type_list);
1252 type = get_dict_type(member, cctx->ctx_type_list);
1253
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001254 // drop the key and value types
1255 stack->ga_len -= 2 * count;
1256
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257 // add the dict type to the type stack
1258 if (ga_grow(stack, 1) == FAIL)
1259 return FAIL;
1260 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1261 ++stack->ga_len;
1262
1263 return OK;
1264}
1265
1266/*
1267 * Generate an ISN_FUNCREF instruction.
1268 */
1269 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001270generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271{
1272 isn_T *isn;
1273 garray_T *stack = &cctx->ctx_type_stack;
1274
Bram Moolenaar080457c2020-03-03 21:53:32 +01001275 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1277 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001278 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001279 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001280
1281 if (ga_grow(stack, 1) == FAIL)
1282 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001283 ((type_T **)stack->ga_data)[stack->ga_len] =
1284 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001285 ++stack->ga_len;
1286
1287 return OK;
1288}
1289
1290/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001291 * Generate an ISN_NEWFUNC instruction.
1292 */
1293 static int
1294generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1295{
1296 isn_T *isn;
1297 char_u *name;
1298
1299 RETURN_OK_IF_SKIP(cctx);
1300 name = vim_strsave(lambda_name);
1301 if (name == NULL)
1302 return FAIL;
1303 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1304 return FAIL;
1305 isn->isn_arg.newfunc.nf_lambda = name;
1306 isn->isn_arg.newfunc.nf_global = func_name;
1307
1308 return OK;
1309}
1310
1311/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312 * Generate an ISN_JUMP instruction.
1313 */
1314 static int
1315generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1316{
1317 isn_T *isn;
1318 garray_T *stack = &cctx->ctx_type_stack;
1319
Bram Moolenaar080457c2020-03-03 21:53:32 +01001320 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1322 return FAIL;
1323 isn->isn_arg.jump.jump_when = when;
1324 isn->isn_arg.jump.jump_where = where;
1325
1326 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1327 --stack->ga_len;
1328
1329 return OK;
1330}
1331
1332 static int
1333generate_FOR(cctx_T *cctx, int loop_idx)
1334{
1335 isn_T *isn;
1336 garray_T *stack = &cctx->ctx_type_stack;
1337
Bram Moolenaar080457c2020-03-03 21:53:32 +01001338 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1340 return FAIL;
1341 isn->isn_arg.forloop.for_idx = loop_idx;
1342
1343 if (ga_grow(stack, 1) == FAIL)
1344 return FAIL;
1345 // type doesn't matter, will be stored next
1346 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1347 ++stack->ga_len;
1348
1349 return OK;
1350}
1351
1352/*
1353 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001354 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355 * Return FAIL if the number of arguments is wrong.
1356 */
1357 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001358generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359{
1360 isn_T *isn;
1361 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001362 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001363 type_T *argtypes[MAX_FUNC_ARGS];
1364 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001365
Bram Moolenaar080457c2020-03-03 21:53:32 +01001366 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001367 argoff = check_internal_func(func_idx, argcount);
1368 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001369 return FAIL;
1370
Bram Moolenaar389df252020-07-09 21:20:47 +02001371 if (method_call && argoff > 1)
1372 {
1373 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1374 return FAIL;
1375 isn->isn_arg.shuffle.shfl_item = argcount;
1376 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1377 }
1378
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001379 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1380 return FAIL;
1381 isn->isn_arg.bfunc.cbf_idx = func_idx;
1382 isn->isn_arg.bfunc.cbf_argcount = argcount;
1383
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001384 for (i = 0; i < argcount; ++i)
1385 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1386
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 stack->ga_len -= argcount; // drop the arguments
1388 if (ga_grow(stack, 1) == FAIL)
1389 return FAIL;
1390 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001391 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001392 ++stack->ga_len; // add return value
1393
1394 return OK;
1395}
1396
1397/*
1398 * Generate an ISN_DCALL or ISN_UCALL instruction.
1399 * Return FAIL if the number of arguments is wrong.
1400 */
1401 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001402generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403{
1404 isn_T *isn;
1405 garray_T *stack = &cctx->ctx_type_stack;
1406 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001407 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001408
Bram Moolenaar080457c2020-03-03 21:53:32 +01001409 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001410 if (argcount > regular_args && !has_varargs(ufunc))
1411 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001412 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413 return FAIL;
1414 }
1415 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1416 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001417 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001418 return FAIL;
1419 }
1420
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001421 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001422 {
1423 int i;
1424
1425 for (i = 0; i < argcount; ++i)
1426 {
1427 type_T *expected;
1428 type_T *actual;
1429
1430 if (i < regular_args)
1431 {
1432 if (ufunc->uf_arg_types == NULL)
1433 continue;
1434 expected = ufunc->uf_arg_types[i];
1435 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001436 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1437 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001438 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001439 else
1440 expected = ufunc->uf_va_type->tt_member;
1441 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001442 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001443 {
1444 arg_type_mismatch(expected, actual, i + 1);
1445 return FAIL;
1446 }
1447 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001448 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001449 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1450 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001451 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001452 }
1453
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001455 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001456 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001458 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001459 {
1460 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1461 isn->isn_arg.dfunc.cdf_argcount = argcount;
1462 }
1463 else
1464 {
1465 // A user function may be deleted and redefined later, can't use the
1466 // ufunc pointer, need to look it up again at runtime.
1467 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1468 isn->isn_arg.ufunc.cuf_argcount = argcount;
1469 }
1470
1471 stack->ga_len -= argcount; // drop the arguments
1472 if (ga_grow(stack, 1) == FAIL)
1473 return FAIL;
1474 // add return value
1475 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1476 ++stack->ga_len;
1477
1478 return OK;
1479}
1480
1481/*
1482 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1483 */
1484 static int
1485generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1486{
1487 isn_T *isn;
1488 garray_T *stack = &cctx->ctx_type_stack;
1489
Bram Moolenaar080457c2020-03-03 21:53:32 +01001490 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1492 return FAIL;
1493 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1494 isn->isn_arg.ufunc.cuf_argcount = argcount;
1495
1496 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001497 if (ga_grow(stack, 1) == FAIL)
1498 return FAIL;
1499 // add return value
1500 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1501 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001502
1503 return OK;
1504}
1505
1506/*
1507 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001508 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001509 */
1510 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001511generate_PCALL(
1512 cctx_T *cctx,
1513 int argcount,
1514 char_u *name,
1515 type_T *type,
1516 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001517{
1518 isn_T *isn;
1519 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001520 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001521
Bram Moolenaar080457c2020-03-03 21:53:32 +01001522 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001523
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001524 if (type->tt_type == VAR_ANY)
1525 ret_type = &t_any;
1526 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001527 {
1528 if (type->tt_argcount != -1)
1529 {
1530 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1531
1532 if (argcount < type->tt_min_argcount - varargs)
1533 {
1534 semsg(_(e_toofewarg), "[reference]");
1535 return FAIL;
1536 }
1537 if (!varargs && argcount > type->tt_argcount)
1538 {
1539 semsg(_(e_toomanyarg), "[reference]");
1540 return FAIL;
1541 }
1542 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001543 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001544 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001545 else
1546 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001547 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001548 return FAIL;
1549 }
1550
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1552 return FAIL;
1553 isn->isn_arg.pfunc.cpf_top = at_top;
1554 isn->isn_arg.pfunc.cpf_argcount = argcount;
1555
1556 stack->ga_len -= argcount; // drop the arguments
1557
1558 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001559 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001561 // If partial is above the arguments it must be cleared and replaced with
1562 // the return value.
1563 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1564 return FAIL;
1565
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001566 return OK;
1567}
1568
1569/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001570 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001571 */
1572 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001573generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001574{
1575 isn_T *isn;
1576 garray_T *stack = &cctx->ctx_type_stack;
1577 type_T *type;
1578
Bram Moolenaar080457c2020-03-03 21:53:32 +01001579 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001580 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001581 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001582 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001583
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001584 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001586 if (type->tt_type != VAR_DICT && type != &t_any)
1587 {
1588 emsg(_(e_dictreq));
1589 return FAIL;
1590 }
1591 // change dict type to dict member type
1592 if (type->tt_type == VAR_DICT)
1593 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594
1595 return OK;
1596}
1597
1598/*
1599 * Generate an ISN_ECHO instruction.
1600 */
1601 static int
1602generate_ECHO(cctx_T *cctx, int with_white, int count)
1603{
1604 isn_T *isn;
1605
Bram Moolenaar080457c2020-03-03 21:53:32 +01001606 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1608 return FAIL;
1609 isn->isn_arg.echo.echo_with_white = with_white;
1610 isn->isn_arg.echo.echo_count = count;
1611
1612 return OK;
1613}
1614
Bram Moolenaarad39c092020-02-26 18:23:43 +01001615/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001616 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001617 */
1618 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001619generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001620{
1621 isn_T *isn;
1622
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001623 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001624 return FAIL;
1625 isn->isn_arg.number = count;
1626
1627 return OK;
1628}
1629
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001630/*
1631 * Generate an ISN_PUT instruction.
1632 */
1633 static int
1634generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1635{
1636 isn_T *isn;
1637
1638 RETURN_OK_IF_SKIP(cctx);
1639 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1640 return FAIL;
1641 isn->isn_arg.put.put_regname = regname;
1642 isn->isn_arg.put.put_lnum = lnum;
1643 return OK;
1644}
1645
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001646 static int
1647generate_EXEC(cctx_T *cctx, char_u *line)
1648{
1649 isn_T *isn;
1650
Bram Moolenaar080457c2020-03-03 21:53:32 +01001651 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1653 return FAIL;
1654 isn->isn_arg.string = vim_strsave(line);
1655 return OK;
1656}
1657
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001658 static int
1659generate_EXECCONCAT(cctx_T *cctx, int count)
1660{
1661 isn_T *isn;
1662
1663 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1664 return FAIL;
1665 isn->isn_arg.number = count;
1666 return OK;
1667}
1668
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001669/*
1670 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001671 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001672 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001673 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001674reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1675{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001676 lvar_T *lvar;
1677
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001678 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001679 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001680 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001681 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001682 }
1683
1684 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001685 return NULL;
1686 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001688 // Every local variable uses the next entry on the stack. We could re-use
1689 // the last ones when leaving a scope, but then variables used in a closure
1690 // might get overwritten. To keep things simple do not re-use stack
1691 // entries. This is less efficient, but memory is cheap these days.
1692 lvar->lv_idx = cctx->ctx_locals_count++;
1693
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001694 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695 lvar->lv_const = isConst;
1696 lvar->lv_type = type;
1697
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001698 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699}
1700
1701/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001702 * Remove local variables above "new_top".
1703 */
1704 static void
1705unwind_locals(cctx_T *cctx, int new_top)
1706{
1707 if (cctx->ctx_locals.ga_len > new_top)
1708 {
1709 int idx;
1710 lvar_T *lvar;
1711
1712 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1713 {
1714 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1715 vim_free(lvar->lv_name);
1716 }
1717 }
1718 cctx->ctx_locals.ga_len = new_top;
1719}
1720
1721/*
1722 * Free all local variables.
1723 */
1724 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001725free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001726{
1727 unwind_locals(cctx, 0);
1728 ga_clear(&cctx->ctx_locals);
1729}
1730
1731/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001732 * Find "name" in script-local items of script "sid".
1733 * Returns the index in "sn_var_vals" if found.
1734 * If found but not in "sn_var_vals" returns -1.
1735 * If not found returns -2.
1736 */
1737 int
1738get_script_item_idx(int sid, char_u *name, int check_writable)
1739{
1740 hashtab_T *ht;
1741 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001742 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743 int idx;
1744
1745 // First look the name up in the hashtable.
Bram Moolenaare3d46852020-08-29 13:39:17 +02001746 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001747 return -1;
1748 ht = &SCRIPT_VARS(sid);
1749 di = find_var_in_ht(ht, 0, name, TRUE);
1750 if (di == NULL)
1751 return -2;
1752
1753 // Now find the svar_T index in sn_var_vals.
1754 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1755 {
1756 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1757
1758 if (sv->sv_tv == &di->di_tv)
1759 {
1760 if (check_writable && sv->sv_const)
1761 semsg(_(e_readonlyvar), name);
1762 return idx;
1763 }
1764 }
1765 return -1;
1766}
1767
1768/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001769 * Find "name" in imported items of the current script or in "cctx" if not
1770 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771 */
1772 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001773find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001774{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001775 int idx;
1776
Bram Moolenaare3d46852020-08-29 13:39:17 +02001777 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001778 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001779 if (cctx != NULL)
1780 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1781 {
1782 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1783 + idx;
1784
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001785 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1786 : STRLEN(import->imp_name) == len
1787 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788 return import;
1789 }
1790
Bram Moolenaarefa94442020-08-08 22:16:00 +02001791 return find_imported_in_script(name, len, current_sctx.sc_sid);
1792}
1793
1794 imported_T *
1795find_imported_in_script(char_u *name, size_t len, int sid)
1796{
Bram Moolenaare3d46852020-08-29 13:39:17 +02001797 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001798 int idx;
1799
Bram Moolenaare3d46852020-08-29 13:39:17 +02001800 if (!SCRIPT_ID_VALID(sid))
1801 return NULL;
1802 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1804 {
1805 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1806
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001807 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1808 : STRLEN(import->imp_name) == len
1809 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810 return import;
1811 }
1812 return NULL;
1813}
1814
1815/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001816 * Free all imported variables.
1817 */
1818 static void
1819free_imported(cctx_T *cctx)
1820{
1821 int idx;
1822
1823 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1824 {
1825 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1826
1827 vim_free(import->imp_name);
1828 }
1829 ga_clear(&cctx->ctx_imports);
1830}
1831
1832/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02001833 * Return TRUE if "p" points at a "#" but not at "#{".
1834 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001835 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001836vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001837{
1838 return p[0] == '#' && p[1] != '{';
1839}
1840
1841/*
1842 * Return a pointer to the next line that isn't empty or only contains a
1843 * comment. Skips over white space.
1844 * Returns NULL if there is none.
1845 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001846 char_u *
1847peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001848{
1849 int lnum = cctx->ctx_lnum;
1850
1851 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
1852 {
1853 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001854 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001855
Bram Moolenaarba60cc42020-08-12 19:15:33 +02001856 // ignore NULLs inserted for continuation lines
1857 if (line != NULL)
1858 {
1859 p = skipwhite(line);
1860 if (*p != NUL && !vim9_comment_start(p))
1861 return p;
1862 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02001863 }
1864 return NULL;
1865}
1866
1867/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001868 * Called when checking for a following operator at "arg". When the rest of
1869 * the line is empty or only a comment, peek the next line. If there is a next
1870 * line return a pointer to it and set "nextp".
1871 * Otherwise skip over white space.
1872 */
1873 static char_u *
1874may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
1875{
1876 char_u *p = skipwhite(arg);
1877
1878 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001879 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001880 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001881 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001882 if (*nextp != NULL)
1883 return *nextp;
1884 }
1885 return p;
1886}
1887
1888/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02001889 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02001890 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02001891 * Returns NULL when at the end.
1892 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001893 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02001894next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02001895{
Bram Moolenaar7a092242020-04-16 22:10:49 +02001896 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001897
1898 do
1899 {
1900 ++cctx->ctx_lnum;
1901 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02001902 {
1903 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001904 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02001905 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02001906 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02001907 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001908 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001909 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02001910 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02001911 return line;
1912}
1913
1914/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001915 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02001916 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001917 * Return FAIL if beyond the last line, "*arg" is unmodified then.
1918 */
1919 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02001920may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001921{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001922 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001923 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02001924 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001925
1926 if (next == NULL)
1927 return FAIL;
1928 *arg = skipwhite(next);
1929 }
1930 return OK;
1931}
1932
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001933/*
1934 * Idem, and give an error when failed.
1935 */
1936 static int
1937may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
1938{
1939 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1940 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001941 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001942 return FAIL;
1943 }
1944 return OK;
1945}
1946
1947
Bram Moolenaara5565e42020-05-09 15:44:01 +02001948// Structure passed between the compile_expr* functions to keep track of
1949// constants that have been parsed but for which no code was produced yet. If
1950// possible expressions on these constants are applied at compile time. If
1951// that is not possible, the code to push the constants needs to be generated
1952// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02001953// Using 50 should be more than enough of 5 levels of ().
1954#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02001955typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02001956 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02001957 int pp_used; // active entries in pp_tv[]
1958} ppconst_T;
1959
Bram Moolenaar1c747212020-05-09 18:28:34 +02001960static int compile_expr0(char_u **arg, cctx_T *cctx);
1961static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1962
Bram Moolenaara5565e42020-05-09 15:44:01 +02001963/*
1964 * Generate a PUSH instruction for "tv".
1965 * "tv" will be consumed or cleared.
1966 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
1967 */
1968 static int
1969generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
1970{
1971 if (tv != NULL)
1972 {
1973 switch (tv->v_type)
1974 {
1975 case VAR_UNKNOWN:
1976 break;
1977 case VAR_BOOL:
1978 generate_PUSHBOOL(cctx, tv->vval.v_number);
1979 break;
1980 case VAR_SPECIAL:
1981 generate_PUSHSPEC(cctx, tv->vval.v_number);
1982 break;
1983 case VAR_NUMBER:
1984 generate_PUSHNR(cctx, tv->vval.v_number);
1985 break;
1986#ifdef FEAT_FLOAT
1987 case VAR_FLOAT:
1988 generate_PUSHF(cctx, tv->vval.v_float);
1989 break;
1990#endif
1991 case VAR_BLOB:
1992 generate_PUSHBLOB(cctx, tv->vval.v_blob);
1993 tv->vval.v_blob = NULL;
1994 break;
1995 case VAR_STRING:
1996 generate_PUSHS(cctx, tv->vval.v_string);
1997 tv->vval.v_string = NULL;
1998 break;
1999 default:
2000 iemsg("constant type not supported");
2001 clear_tv(tv);
2002 return FAIL;
2003 }
2004 tv->v_type = VAR_UNKNOWN;
2005 }
2006 return OK;
2007}
2008
2009/*
2010 * Generate code for any ppconst entries.
2011 */
2012 static int
2013generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2014{
2015 int i;
2016 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002017 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002018
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002019 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002020 for (i = 0; i < ppconst->pp_used; ++i)
2021 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2022 ret = FAIL;
2023 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002024 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002025 return ret;
2026}
2027
2028/*
2029 * Clear ppconst constants. Used when failing.
2030 */
2031 static void
2032clear_ppconst(ppconst_T *ppconst)
2033{
2034 int i;
2035
2036 for (i = 0; i < ppconst->pp_used; ++i)
2037 clear_tv(&ppconst->pp_tv[i]);
2038 ppconst->pp_used = 0;
2039}
2040
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002041/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002042 * Generate an instruction to load script-local variable "name", without the
2043 * leading "s:".
2044 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002045 */
2046 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002047compile_load_scriptvar(
2048 cctx_T *cctx,
2049 char_u *name, // variable NUL terminated
2050 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002051 char_u **end, // end of variable
2052 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002053{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002054 scriptitem_T *si;
2055 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002056 imported_T *import;
2057
Bram Moolenaare3d46852020-08-29 13:39:17 +02002058 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2059 return FAIL;
2060 si = SCRIPT_ITEM(current_sctx.sc_sid);
2061 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002062 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002063 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002064 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002065 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2066 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002067 }
2068 if (idx >= 0)
2069 {
2070 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2071
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002072 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002073 current_sctx.sc_sid, idx, sv->sv_type);
2074 return OK;
2075 }
2076
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002077 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002078 if (import != NULL)
2079 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002080 if (import->imp_all)
2081 {
2082 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002083 char_u *exp_name;
2084 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002085 ufunc_T *ufunc;
2086 type_T *type;
2087
2088 // Used "import * as Name", need to lookup the member.
2089 if (*p != '.')
2090 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002091 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002092 return FAIL;
2093 }
2094 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002095 if (VIM_ISWHITE(*p))
2096 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002097 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002098 return FAIL;
2099 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002100
Bram Moolenaar1c991142020-07-04 13:15:31 +02002101 // isolate one name
2102 exp_name = p;
2103 while (eval_isnamec(*p))
2104 ++p;
2105 cc = *p;
2106 *p = NUL;
2107
2108 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2109 *p = cc;
2110 p = skipwhite(p);
2111
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002112 // TODO: what if it is a function?
2113 if (idx < 0)
2114 return FAIL;
2115 *end = p;
2116
2117 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2118 import->imp_sid,
2119 idx,
2120 type);
2121 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002122 else if (import->imp_funcname != NULL)
2123 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002124 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002125 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2126 import->imp_sid,
2127 import->imp_var_vals_idx,
2128 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002129 return OK;
2130 }
2131
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002132 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002133 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002134 return FAIL;
2135}
2136
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002137 static int
2138generate_funcref(cctx_T *cctx, char_u *name)
2139{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002140 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002141
2142 if (ufunc == NULL)
2143 return FAIL;
2144
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002145 // Need to compile any default values to get the argument types.
2146 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2147 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2148 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002149 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002150}
2151
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002152/*
2153 * Compile a variable name into a load instruction.
2154 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002155 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002156 * When "error" is FALSE do not give an error when not found.
2157 */
2158 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002159compile_load(
2160 char_u **arg,
2161 char_u *end_arg,
2162 cctx_T *cctx,
2163 int is_expr,
2164 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002165{
2166 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002167 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002168 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002170 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002171
2172 if (*(*arg + 1) == ':')
2173 {
2174 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002175 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002176 {
2177 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002178
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002179 switch (**arg)
2180 {
2181 case 'g': isn_type = ISN_LOADGDICT; break;
2182 case 'w': isn_type = ISN_LOADWDICT; break;
2183 case 't': isn_type = ISN_LOADTDICT; break;
2184 case 'b': isn_type = ISN_LOADBDICT; break;
2185 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002186 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002187 goto theend;
2188 }
2189 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2190 goto theend;
2191 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002192 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002193 else
2194 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002195 isntype_T isn_type = ISN_DROP;
2196
2197 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2198 if (name == NULL)
2199 return FAIL;
2200
2201 switch (**arg)
2202 {
2203 case 'v': res = generate_LOADV(cctx, name, error);
2204 break;
2205 case 's': res = compile_load_scriptvar(cctx, name,
2206 NULL, NULL, error);
2207 break;
2208 case 'g': isn_type = ISN_LOADG; break;
2209 case 'w': isn_type = ISN_LOADW; break;
2210 case 't': isn_type = ISN_LOADT; break;
2211 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002212 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002213 goto theend;
2214 }
2215 if (isn_type != ISN_DROP)
2216 {
2217 // Global, Buffer-local, Window-local and Tabpage-local
2218 // variables can be defined later, thus we don't check if it
2219 // exists, give error at runtime.
2220 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2221 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002222 }
2223 }
2224 else
2225 {
2226 size_t len = end - *arg;
2227 int idx;
2228 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002229 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002230
2231 name = vim_strnsave(*arg, end - *arg);
2232 if (name == NULL)
2233 return FAIL;
2234
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002235 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002236 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002237 if (!gen_load_outer)
2238 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002239 }
2240 else
2241 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002242 lvar_T *lvar = lookup_local(*arg, len, cctx);
2243
2244 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002246 type = lvar->lv_type;
2247 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002248 if (lvar->lv_from_outer)
2249 gen_load_outer = TRUE;
2250 else
2251 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002252 }
2253 else
2254 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002255 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002256 // already exists in a Vim9 script or when it's imported.
2257 if (lookup_script(*arg, len, TRUE) == OK
2258 || find_imported(name, 0, cctx) != NULL)
2259 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002260
Bram Moolenaar0f769812020-09-12 18:32:34 +02002261 // When evaluating an expression and the name starts with an
2262 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002263 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002264 if (res == FAIL && is_expr
2265 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002266 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002267 }
2268 }
2269 if (gen_load)
2270 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002271 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002272 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002273 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002274 cctx->ctx_outer_used = TRUE;
2275 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002276 }
2277
2278 *arg = end;
2279
2280theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002281 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002282 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002283 vim_free(name);
2284 return res;
2285}
2286
2287/*
2288 * Compile the argument expressions.
2289 * "arg" points to just after the "(" and is advanced to after the ")"
2290 */
2291 static int
2292compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2293{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002294 char_u *p = *arg;
2295 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002296 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002297
Bram Moolenaare6085c52020-04-12 20:19:16 +02002298 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002300 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2301 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002302 if (*p == ')')
2303 {
2304 *arg = p + 1;
2305 return OK;
2306 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002307 if (must_end)
2308 {
2309 semsg(_(e_missing_comma_before_argument_str), p);
2310 return FAIL;
2311 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002312
Bram Moolenaara5565e42020-05-09 15:44:01 +02002313 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002314 return FAIL;
2315 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002316
2317 if (*p != ',' && *skipwhite(p) == ',')
2318 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002319 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002320 p = skipwhite(p);
2321 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002323 {
2324 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002325 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002326 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002327 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002328 else
2329 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002330 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002331 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002332 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002333failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002334 emsg(_(e_missing_close));
2335 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002336}
2337
2338/*
2339 * Compile a function call: name(arg1, arg2)
2340 * "arg" points to "name", "arg + varlen" to the "(".
2341 * "argcount_init" is 1 for "value->method()"
2342 * Instructions:
2343 * EVAL arg1
2344 * EVAL arg2
2345 * BCALL / DCALL / UCALL
2346 */
2347 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002348compile_call(
2349 char_u **arg,
2350 size_t varlen,
2351 cctx_T *cctx,
2352 ppconst_T *ppconst,
2353 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002354{
2355 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002356 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002357 int argcount = argcount_init;
2358 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002359 char_u fname_buf[FLEN_FIXED + 1];
2360 char_u *tofree = NULL;
2361 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002362 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002363 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002364 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365
Bram Moolenaara5565e42020-05-09 15:44:01 +02002366 // we can evaluate "has('name')" at compile time
2367 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2368 {
2369 char_u *s = skipwhite(*arg + varlen + 1);
2370 typval_T argvars[2];
2371
2372 argvars[0].v_type = VAR_UNKNOWN;
2373 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002374 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002375 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002376 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002377 s = skipwhite(s);
2378 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2379 {
2380 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2381
2382 *arg = s + 1;
2383 argvars[1].v_type = VAR_UNKNOWN;
2384 tv->v_type = VAR_NUMBER;
2385 tv->vval.v_number = 0;
2386 f_has(argvars, tv);
2387 clear_tv(&argvars[0]);
2388 ++ppconst->pp_used;
2389 return OK;
2390 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002391 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002392 }
2393
2394 if (generate_ppconst(cctx, ppconst) == FAIL)
2395 return FAIL;
2396
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397 if (varlen >= sizeof(namebuf))
2398 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002399 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002400 return FAIL;
2401 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002402 vim_strncpy(namebuf, *arg, varlen);
2403 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002404
2405 *arg = skipwhite(*arg + varlen + 1);
2406 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002407 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002408
Bram Moolenaara1773442020-08-12 15:21:22 +02002409 is_autoload = vim_strchr(name, '#') != NULL;
2410 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002411 {
2412 int idx;
2413
2414 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002415 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002417 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002418 else
2419 semsg(_(e_unknownfunc), namebuf);
2420 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002421 }
2422
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 // If we can find the function by name generate the right call.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002424 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002425 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar0f769812020-09-12 18:32:34 +02002426 if (ufunc != NULL && !func_is_global(ufunc))
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002427 {
2428 res = generate_CALL(cctx, ufunc, argcount);
2429 goto theend;
2430 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002431
2432 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002433 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002434 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002436 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002437 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002438 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002439 garray_T *stack = &cctx->ctx_type_stack;
2440 type_T *type;
2441
2442 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2443 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002444 goto theend;
2445 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002446
Bram Moolenaar0f769812020-09-12 18:32:34 +02002447 // If we can find a global function by name generate the right call.
2448 if (ufunc != NULL)
2449 {
2450 res = generate_CALL(cctx, ufunc, argcount);
2451 goto theend;
2452 }
2453
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002454 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002455 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002456 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002457 res = generate_UCALL(cctx, name, argcount);
2458 else
2459 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002460
2461theend:
2462 vim_free(tofree);
2463 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002464}
2465
2466// like NAMESPACE_CHAR but with 'a' and 'l'.
2467#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2468
2469/*
2470 * Find the end of a variable or function name. Unlike find_name_end() this
2471 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002472 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 * Return a pointer to just after the name. Equal to "arg" if there is no
2474 * valid name.
2475 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002476 static char_u *
2477to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002478{
2479 char_u *p;
2480
2481 // Quick check for valid starting character.
2482 if (!eval_isnamec1(*arg))
2483 return arg;
2484
2485 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2486 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2487 // and can be used in slice "[n:]".
2488 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002489 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2491 break;
2492 return p;
2493}
2494
2495/*
2496 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002497 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 */
2499 char_u *
2500to_name_const_end(char_u *arg)
2501{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002502 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 typval_T rettv;
2504
2505 if (p == arg && *arg == '[')
2506 {
2507
2508 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002509 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510 p = arg;
2511 }
2512 else if (p == arg && *arg == '#' && arg[1] == '{')
2513 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002514 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002516 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 p = arg;
2518 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519
2520 return p;
2521}
2522
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523/*
2524 * parse a list: [expr, expr]
2525 * "*arg" points to the '['.
2526 */
2527 static int
2528compile_list(char_u **arg, cctx_T *cctx)
2529{
2530 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002531 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 int count = 0;
2533
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002534 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002535 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002536 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002537 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002538 semsg(_(e_list_end), *arg);
2539 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002540 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002541 if (*p == ',')
2542 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002543 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002544 return FAIL;
2545 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002546 if (*p == ']')
2547 {
2548 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002549 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002550 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002551 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002552 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553 ++count;
2554 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002555 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002556 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002557 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2558 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002559 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002560 return FAIL;
2561 }
2562 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002563 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 p = skipwhite(p);
2565 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002566 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567
2568 generate_NEWLIST(cctx, count);
2569 return OK;
2570}
2571
2572/*
2573 * parse a lambda: {arg, arg -> expr}
2574 * "*arg" points to the '{'.
2575 */
2576 static int
2577compile_lambda(char_u **arg, cctx_T *cctx)
2578{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579 typval_T rettv;
2580 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002581 evalarg_T evalarg;
2582
2583 CLEAR_FIELD(evalarg);
2584 evalarg.eval_flags = EVAL_EVALUATE;
2585 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586
2587 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002588 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002590
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002592 ++ufunc->uf_refcount;
2593 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002594 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595
2596 // The function will have one line: "return {expr}".
2597 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002598 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002600 clear_evalarg(&evalarg, NULL);
2601
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002602 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002603 {
2604 // The return type will now be known.
2605 set_function_type(ufunc);
2606
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002607 // The function reference count will be 1. When the ISN_FUNCREF
2608 // instruction is deleted the reference count is decremented and the
2609 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002610 return generate_FUNCREF(cctx, ufunc);
2611 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002612
2613 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002614 return FAIL;
2615}
2616
2617/*
2618 * Compile a lamda call: expr->{lambda}(args)
2619 * "arg" points to the "{".
2620 */
2621 static int
2622compile_lambda_call(char_u **arg, cctx_T *cctx)
2623{
2624 ufunc_T *ufunc;
2625 typval_T rettv;
2626 int argcount = 1;
2627 int ret = FAIL;
2628
2629 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002630 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002631 return FAIL;
2632
2633 if (**arg != '(')
2634 {
2635 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002636 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637 else
2638 semsg(_(e_missing_paren), "lambda");
2639 clear_tv(&rettv);
2640 return FAIL;
2641 }
2642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643 ufunc = rettv.vval.v_partial->pt_func;
2644 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002645 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002646 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002647
Bram Moolenaara05e5242020-09-19 18:19:19 +02002648 // The function will have one line: "return {expr}". Compile it into
2649 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002650 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651
2652 // compile the arguments
2653 *arg = skipwhite(*arg + 1);
2654 if (compile_arguments(arg, cctx, &argcount) == OK)
2655 // call the compiled function
2656 ret = generate_CALL(cctx, ufunc, argcount);
2657
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002658 if (ret == FAIL)
2659 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660 return ret;
2661}
2662
2663/*
2664 * parse a dict: {'key': val} or #{key: val}
2665 * "*arg" points to the '{'.
2666 */
2667 static int
2668compile_dict(char_u **arg, cctx_T *cctx, int literal)
2669{
2670 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002671 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672 int count = 0;
2673 dict_T *d = dict_alloc();
2674 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002675 char_u *whitep = *arg;
2676 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002677
2678 if (d == NULL)
2679 return FAIL;
2680 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002681 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002682 {
2683 char_u *key = NULL;
2684
Bram Moolenaar23c55272020-06-21 16:58:13 +02002685 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002686 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002687 *arg = NULL;
2688 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002689 }
2690
2691 if (**arg == '}')
2692 break;
2693
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002694 if (literal)
2695 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002696 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697
Bram Moolenaar2c330432020-04-13 14:41:35 +02002698 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002700 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002701 return FAIL;
2702 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002703 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704 if (generate_PUSHS(cctx, key) == FAIL)
2705 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002706 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707 }
2708 else
2709 {
2710 isn_T *isn;
2711
Bram Moolenaara5565e42020-05-09 15:44:01 +02002712 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002714 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2715 if (isn->isn_type == ISN_PUSHS)
2716 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002717 else
2718 {
2719 type_T *keytype = ((type_T **)stack->ga_data)
2720 [stack->ga_len - 1];
2721 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2722 return FAIL;
2723 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724 }
2725
2726 // Check for duplicate keys, if using string keys.
2727 if (key != NULL)
2728 {
2729 item = dict_find(d, key, -1);
2730 if (item != NULL)
2731 {
2732 semsg(_(e_duplicate_key), key);
2733 goto failret;
2734 }
2735 item = dictitem_alloc(key);
2736 if (item != NULL)
2737 {
2738 item->di_tv.v_type = VAR_UNKNOWN;
2739 item->di_tv.v_lock = 0;
2740 if (dict_add(d, item) == FAIL)
2741 dictitem_free(item);
2742 }
2743 }
2744
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002745 if (**arg != ':')
2746 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002747 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002748 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002749 else
2750 semsg(_(e_missing_dict_colon), *arg);
2751 return FAIL;
2752 }
2753 whitep = *arg + 1;
2754 if (!IS_WHITE_OR_NUL(*whitep))
2755 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002756 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002757 return FAIL;
2758 }
2759
2760 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002761 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002762 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002763 *arg = NULL;
2764 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002765 }
2766
Bram Moolenaara5565e42020-05-09 15:44:01 +02002767 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 return FAIL;
2769 ++count;
2770
Bram Moolenaar2c330432020-04-13 14:41:35 +02002771 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002772 *arg = skipwhite(*arg);
2773 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002774 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002775 *arg = NULL;
2776 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002777 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 if (**arg == '}')
2779 break;
2780 if (**arg != ',')
2781 {
2782 semsg(_(e_missing_dict_comma), *arg);
2783 goto failret;
2784 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002785 if (IS_WHITE_OR_NUL(*whitep))
2786 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002787 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002788 return FAIL;
2789 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002790 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002791 *arg = skipwhite(*arg + 1);
2792 }
2793
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794 *arg = *arg + 1;
2795
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002796 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002797 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002798 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002799 *arg += STRLEN(*arg);
2800
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801 dict_unref(d);
2802 return generate_NEWDICT(cctx, count);
2803
2804failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002805 if (*arg == NULL)
2806 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 dict_unref(d);
2808 return FAIL;
2809}
2810
2811/*
2812 * Compile "&option".
2813 */
2814 static int
2815compile_get_option(char_u **arg, cctx_T *cctx)
2816{
2817 typval_T rettv;
2818 char_u *start = *arg;
2819 int ret;
2820
2821 // parse the option and get the current value to get the type.
2822 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002823 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824 if (ret == OK)
2825 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002826 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002827 char_u *name = vim_strnsave(start, *arg - start);
2828 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2829
2830 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2831 vim_free(name);
2832 }
2833 clear_tv(&rettv);
2834
2835 return ret;
2836}
2837
2838/*
2839 * Compile "$VAR".
2840 */
2841 static int
2842compile_get_env(char_u **arg, cctx_T *cctx)
2843{
2844 char_u *start = *arg;
2845 int len;
2846 int ret;
2847 char_u *name;
2848
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 ++*arg;
2850 len = get_env_len(arg);
2851 if (len == 0)
2852 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002853 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854 return FAIL;
2855 }
2856
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002857 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 name = vim_strnsave(start, len + 1);
2859 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2860 vim_free(name);
2861 return ret;
2862}
2863
2864/*
2865 * Compile "@r".
2866 */
2867 static int
2868compile_get_register(char_u **arg, cctx_T *cctx)
2869{
2870 int ret;
2871
2872 ++*arg;
2873 if (**arg == NUL)
2874 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002875 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002876 return FAIL;
2877 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002878 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002879 {
2880 emsg_invreg(**arg);
2881 return FAIL;
2882 }
2883 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2884 ++*arg;
2885 return ret;
2886}
2887
2888/*
2889 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002890 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 */
2892 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002893apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002895 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896
2897 // this works from end to start
2898 while (p > start)
2899 {
2900 --p;
2901 if (*p == '-' || *p == '+')
2902 {
2903 // only '-' has an effect, for '+' we only check the type
2904#ifdef FEAT_FLOAT
2905 if (rettv->v_type == VAR_FLOAT)
2906 {
2907 if (*p == '-')
2908 rettv->vval.v_float = -rettv->vval.v_float;
2909 }
2910 else
2911#endif
2912 {
2913 varnumber_T val;
2914 int error = FALSE;
2915
2916 // tv_get_number_chk() accepts a string, but we don't want that
2917 // here
2918 if (check_not_string(rettv) == FAIL)
2919 return FAIL;
2920 val = tv_get_number_chk(rettv, &error);
2921 clear_tv(rettv);
2922 if (error)
2923 return FAIL;
2924 if (*p == '-')
2925 val = -val;
2926 rettv->v_type = VAR_NUMBER;
2927 rettv->vval.v_number = val;
2928 }
2929 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002930 else if (numeric_only)
2931 {
2932 ++p;
2933 break;
2934 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 else
2936 {
2937 int v = tv2bool(rettv);
2938
2939 // '!' is permissive in the type.
2940 clear_tv(rettv);
2941 rettv->v_type = VAR_BOOL;
2942 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2943 }
2944 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002945 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 return OK;
2947}
2948
2949/*
2950 * Recognize v: variables that are constants and set "rettv".
2951 */
2952 static void
2953get_vim_constant(char_u **arg, typval_T *rettv)
2954{
2955 if (STRNCMP(*arg, "v:true", 6) == 0)
2956 {
2957 rettv->v_type = VAR_BOOL;
2958 rettv->vval.v_number = VVAL_TRUE;
2959 *arg += 6;
2960 }
2961 else if (STRNCMP(*arg, "v:false", 7) == 0)
2962 {
2963 rettv->v_type = VAR_BOOL;
2964 rettv->vval.v_number = VVAL_FALSE;
2965 *arg += 7;
2966 }
2967 else if (STRNCMP(*arg, "v:null", 6) == 0)
2968 {
2969 rettv->v_type = VAR_SPECIAL;
2970 rettv->vval.v_number = VVAL_NULL;
2971 *arg += 6;
2972 }
2973 else if (STRNCMP(*arg, "v:none", 6) == 0)
2974 {
2975 rettv->v_type = VAR_SPECIAL;
2976 rettv->vval.v_number = VVAL_NONE;
2977 *arg += 6;
2978 }
2979}
2980
Bram Moolenaar696ba232020-07-29 21:20:41 +02002981 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02002982get_compare_type(char_u *p, int *len, int *type_is)
2983{
2984 exptype_T type = EXPR_UNKNOWN;
2985 int i;
2986
2987 switch (p[0])
2988 {
2989 case '=': if (p[1] == '=')
2990 type = EXPR_EQUAL;
2991 else if (p[1] == '~')
2992 type = EXPR_MATCH;
2993 break;
2994 case '!': if (p[1] == '=')
2995 type = EXPR_NEQUAL;
2996 else if (p[1] == '~')
2997 type = EXPR_NOMATCH;
2998 break;
2999 case '>': if (p[1] != '=')
3000 {
3001 type = EXPR_GREATER;
3002 *len = 1;
3003 }
3004 else
3005 type = EXPR_GEQUAL;
3006 break;
3007 case '<': if (p[1] != '=')
3008 {
3009 type = EXPR_SMALLER;
3010 *len = 1;
3011 }
3012 else
3013 type = EXPR_SEQUAL;
3014 break;
3015 case 'i': if (p[1] == 's')
3016 {
3017 // "is" and "isnot"; but not a prefix of a name
3018 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3019 *len = 5;
3020 i = p[*len];
3021 if (!isalnum(i) && i != '_')
3022 {
3023 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3024 *type_is = TRUE;
3025 }
3026 }
3027 break;
3028 }
3029 return type;
3030}
3031
3032/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003034 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035 */
3036 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003037compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003039 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040
3041 // this works from end to start
3042 while (p > start)
3043 {
3044 --p;
3045 if (*p == '-' || *p == '+')
3046 {
3047 int negate = *p == '-';
3048 isn_T *isn;
3049
3050 // TODO: check type
3051 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3052 {
3053 --p;
3054 if (*p == '-')
3055 negate = !negate;
3056 }
3057 // only '-' has an effect, for '+' we only check the type
3058 if (negate)
3059 isn = generate_instr(cctx, ISN_NEGATENR);
3060 else
3061 isn = generate_instr(cctx, ISN_CHECKNR);
3062 if (isn == NULL)
3063 return FAIL;
3064 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003065 else if (numeric_only)
3066 {
3067 ++p;
3068 break;
3069 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 else
3071 {
3072 int invert = TRUE;
3073
3074 while (p > start && p[-1] == '!')
3075 {
3076 --p;
3077 invert = !invert;
3078 }
3079 if (generate_2BOOL(cctx, invert) == FAIL)
3080 return FAIL;
3081 }
3082 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003083 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084 return OK;
3085}
3086
3087/*
3088 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003089 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090 */
3091 static int
3092compile_subscript(
3093 char_u **arg,
3094 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003095 char_u *start_leader,
3096 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003097 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003099 char_u *name_start = *end_leader;
3100
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 for (;;)
3102 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003103 char_u *p = skipwhite(*arg);
3104
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003105 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003106 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003107 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003108
3109 // If a following line starts with "->{" or "->X" advance to that
3110 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003111 // Also if a following line starts with ".x".
3112 if (next != NULL &&
3113 ((next[0] == '-' && next[1] == '>'
3114 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003115 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003116 {
3117 next = next_line_from_context(cctx, TRUE);
3118 if (next == NULL)
3119 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003120 *arg = next;
3121 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003122 }
3123 }
3124
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003125 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3126 // not a function call.
3127 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003129 garray_T *stack = &cctx->ctx_type_stack;
3130 type_T *type;
3131 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003133 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003134 return FAIL;
3135
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003137 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3138
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003139 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3141 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003142 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 return FAIL;
3144 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003145 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003147 char_u *pstart = p;
3148
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003149 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003150 return FAIL;
3151
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 // something->method()
3153 // Apply the '!', '-' and '+' first:
3154 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003155 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003158 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003159 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003160 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 if (**arg == '{')
3162 {
3163 // lambda call: list->{lambda}
3164 if (compile_lambda_call(arg, cctx) == FAIL)
3165 return FAIL;
3166 }
3167 else
3168 {
3169 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003170 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003171 if (!eval_isnamec1(*p))
3172 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003173 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003174 return FAIL;
3175 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003176 if (ASCII_ISALPHA(*p) && p[1] == ':')
3177 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003178 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179 ;
3180 if (*p != '(')
3181 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003182 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 return FAIL;
3184 }
3185 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003186 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 return FAIL;
3188 }
3189 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003190 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003192 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003193 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003194 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003195 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003196 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003197
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003198 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003199 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003200 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003201 // TODO: blob index
3202 // TODO: more arguments
3203 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003204 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003205 return FAIL;
3206
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003207 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003208 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003209 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003210 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003211 if (**arg == ':')
3212 // missing first index is equal to zero
3213 generate_PUSHNR(cctx, 0);
3214 else
3215 {
3216 if (compile_expr0(arg, cctx) == FAIL)
3217 return FAIL;
3218 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3219 return FAIL;
3220 *arg = skipwhite(*arg);
3221 }
3222 if (**arg == ':')
3223 {
3224 *arg = skipwhite(*arg + 1);
3225 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3226 return FAIL;
3227 if (**arg == ']')
3228 // missing second index is equal to end of string
3229 generate_PUSHNR(cctx, -1);
3230 else
3231 {
3232 if (compile_expr0(arg, cctx) == FAIL)
3233 return FAIL;
3234 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3235 return FAIL;
3236 *arg = skipwhite(*arg);
3237 }
3238 is_slice = TRUE;
3239 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240
3241 if (**arg != ']')
3242 {
3243 emsg(_(e_missbrac));
3244 return FAIL;
3245 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003246 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003248 // We can index a list and a dict. If we don't know the type
3249 // we can use the index value type.
3250 // TODO: If we don't know use an instruction to figure it out at
3251 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003252 typep = ((type_T **)stack->ga_data) + stack->ga_len
3253 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003254 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003255 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3256 // If the index is a string, the variable must be a Dict.
3257 if (*typep == &t_any && valtype == &t_string)
3258 vtype = VAR_DICT;
3259 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003260 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003261 if (need_type(valtype, &t_number, -1, cctx, FALSE) == FAIL)
3262 return FAIL;
3263 if (is_slice)
3264 {
3265 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
3266 if (need_type(valtype, &t_number, -2, cctx, FALSE) == FAIL)
3267 return FAIL;
3268 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003269 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003270
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003271 if (vtype == VAR_DICT)
3272 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003273 if (is_slice)
3274 {
3275 emsg(_(e_cannot_slice_dictionary));
3276 return FAIL;
3277 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003278 if ((*typep)->tt_type == VAR_DICT)
3279 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003280 else
3281 {
3282 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3283 return FAIL;
3284 *typep = &t_any;
3285 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003286 if (may_generate_2STRING(-1, cctx) == FAIL)
3287 return FAIL;
3288 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3289 return FAIL;
3290 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003291 else if (vtype == VAR_STRING)
3292 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003293 *typep = &t_string;
3294 if ((is_slice
3295 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3296 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003297 return FAIL;
3298 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003299 else if (vtype == VAR_BLOB)
3300 {
3301 emsg("Sorry, blob index and slice not implemented yet");
3302 return FAIL;
3303 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003304 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003305 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003306 if (is_slice)
3307 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003308 if (generate_instr_drop(cctx,
3309 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3310 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003311 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003312 }
Bram Moolenaared591872020-08-15 22:14:53 +02003313 else
3314 {
3315 if ((*typep)->tt_type == VAR_LIST)
3316 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003317 if (generate_instr_drop(cctx,
3318 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3319 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003320 return FAIL;
3321 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003322 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003323 else
3324 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003325 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003326 return FAIL;
3327 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003328 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003329 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003330 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003331 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003332 return FAIL;
3333
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003334 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003335 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003336 {
3337 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003338 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003339 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003341 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003342 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343 while (eval_isnamec(*p))
3344 MB_PTR_ADV(p);
3345 if (p == *arg)
3346 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003347 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348 return FAIL;
3349 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003350 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003351 return FAIL;
3352 *arg = p;
3353 }
3354 else
3355 break;
3356 }
3357
3358 // TODO - see handle_subscript():
3359 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3360 // Don't do this when "Func" is already a partial that was bound
3361 // explicitly (pt_auto is FALSE).
3362
3363 return OK;
3364}
3365
3366/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003367 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3368 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003369 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003370 * If the value is a constant "ppconst->pp_ret" will be set.
3371 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003372 *
3373 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374 */
3375
3376/*
3377 * number number constant
3378 * 0zFFFFFFFF Blob constant
3379 * "string" string constant
3380 * 'string' literal string constant
3381 * &option-name option value
3382 * @r register contents
3383 * identifier variable value
3384 * function() function call
3385 * $VAR environment variable
3386 * (expression) nested expression
3387 * [expr, expr] List
3388 * {key: val, key: val} Dictionary
3389 * #{key: val, key: val} Dictionary with literal keys
3390 *
3391 * Also handle:
3392 * ! in front logical NOT
3393 * - in front unary minus
3394 * + in front unary plus (ignored)
3395 * trailing (arg) funcref/partial call
3396 * trailing [] subscript in String or List
3397 * trailing .name entry in Dictionary
3398 * trailing ->name() method call
3399 */
3400 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003401compile_expr7(
3402 char_u **arg,
3403 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003404 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406 char_u *start_leader, *end_leader;
3407 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003408 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003409 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410
3411 /*
3412 * Skip '!', '-' and '+' characters. They are handled later.
3413 */
3414 start_leader = *arg;
3415 while (**arg == '!' || **arg == '-' || **arg == '+')
3416 *arg = skipwhite(*arg + 1);
3417 end_leader = *arg;
3418
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003419 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 switch (**arg)
3421 {
3422 /*
3423 * Number constant.
3424 */
3425 case '0': // also for blob starting with 0z
3426 case '1':
3427 case '2':
3428 case '3':
3429 case '4':
3430 case '5':
3431 case '6':
3432 case '7':
3433 case '8':
3434 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003435 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003437 // Apply "-" and "+" just before the number now, right to
3438 // left. Matters especially when "->" follows. Stops at
3439 // '!'.
3440 if (apply_leader(rettv, TRUE,
3441 start_leader, &end_leader) == FAIL)
3442 {
3443 clear_tv(rettv);
3444 return FAIL;
3445 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 break;
3447
3448 /*
3449 * String constant: "string".
3450 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003451 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452 return FAIL;
3453 break;
3454
3455 /*
3456 * Literal string constant: 'str''ing'.
3457 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003458 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003459 return FAIL;
3460 break;
3461
3462 /*
3463 * Constant Vim variable.
3464 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003465 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003466 ret = NOTDONE;
3467 break;
3468
3469 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003470 * "true" constant
3471 */
3472 case 't': if (STRNCMP(*arg, "true", 4) == 0
3473 && !eval_isnamec((*arg)[4]))
3474 {
3475 *arg += 4;
3476 rettv->v_type = VAR_BOOL;
3477 rettv->vval.v_number = VVAL_TRUE;
3478 }
3479 else
3480 ret = NOTDONE;
3481 break;
3482
3483 /*
3484 * "false" constant
3485 */
3486 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3487 && !eval_isnamec((*arg)[5]))
3488 {
3489 *arg += 5;
3490 rettv->v_type = VAR_BOOL;
3491 rettv->vval.v_number = VVAL_FALSE;
3492 }
3493 else
3494 ret = NOTDONE;
3495 break;
3496
3497 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 * List: [expr, expr]
3499 */
3500 case '[': ret = compile_list(arg, cctx);
3501 break;
3502
3503 /*
3504 * Dictionary: #{key: val, key: val}
3505 */
3506 case '#': if ((*arg)[1] == '{')
3507 {
3508 ++*arg;
3509 ret = compile_dict(arg, cctx, TRUE);
3510 }
3511 else
3512 ret = NOTDONE;
3513 break;
3514
3515 /*
3516 * Lambda: {arg, arg -> expr}
3517 * Dictionary: {'key': val, 'key': val}
3518 */
3519 case '{': {
3520 char_u *start = skipwhite(*arg + 1);
3521
3522 // Find out what comes after the arguments.
3523 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003524 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003525 if (ret != FAIL && *start == '>')
3526 ret = compile_lambda(arg, cctx);
3527 else
3528 ret = compile_dict(arg, cctx, FALSE);
3529 }
3530 break;
3531
3532 /*
3533 * Option value: &name
3534 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003535 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3536 return FAIL;
3537 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538 break;
3539
3540 /*
3541 * Environment variable: $VAR.
3542 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003543 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3544 return FAIL;
3545 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003546 break;
3547
3548 /*
3549 * Register contents: @r.
3550 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003551 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3552 return FAIL;
3553 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003554 break;
3555 /*
3556 * nested expression: (expression).
3557 */
3558 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003559
3560 // recursive!
3561 if (ppconst->pp_used <= PPSIZE - 10)
3562 {
3563 ret = compile_expr1(arg, cctx, ppconst);
3564 }
3565 else
3566 {
3567 // Not enough space in ppconst, flush constants.
3568 if (generate_ppconst(cctx, ppconst) == FAIL)
3569 return FAIL;
3570 ret = compile_expr0(arg, cctx);
3571 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572 *arg = skipwhite(*arg);
3573 if (**arg == ')')
3574 ++*arg;
3575 else if (ret == OK)
3576 {
3577 emsg(_(e_missing_close));
3578 ret = FAIL;
3579 }
3580 break;
3581
3582 default: ret = NOTDONE;
3583 break;
3584 }
3585 if (ret == FAIL)
3586 return FAIL;
3587
Bram Moolenaar1c747212020-05-09 18:28:34 +02003588 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003590 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003591 clear_tv(rettv);
3592 else
3593 // A constant expression can possibly be handled compile time,
3594 // return the value instead of generating code.
3595 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596 }
3597 else if (ret == NOTDONE)
3598 {
3599 char_u *p;
3600 int r;
3601
3602 if (!eval_isnamec1(**arg))
3603 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003604 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605 return FAIL;
3606 }
3607
3608 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003609 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003611 {
3612 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3613 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003615 {
3616 if (generate_ppconst(cctx, ppconst) == FAIL)
3617 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003618 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003619 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 if (r == FAIL)
3621 return FAIL;
3622 }
3623
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003624 // Handle following "[]", ".member", etc.
3625 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003626 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003627 ppconst) == FAIL)
3628 return FAIL;
3629 if (ppconst->pp_used > 0)
3630 {
3631 // apply the '!', '-' and '+' before the constant
3632 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003633 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003634 return FAIL;
3635 return OK;
3636 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003637 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003639 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640}
3641
3642/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003643 * Give the "white on both sides" error, taking the operator from "p[len]".
3644 */
3645 void
3646error_white_both(char_u *op, int len)
3647{
3648 char_u buf[10];
3649
3650 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003651 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003652}
3653
3654/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003655 * <type>expr7: runtime type check / conversion
3656 */
3657 static int
3658compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3659{
3660 type_T *want_type = NULL;
3661
3662 // Recognize <type>
3663 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3664 {
3665 int called_emsg_before = called_emsg;
3666
3667 ++*arg;
3668 want_type = parse_type(arg, cctx->ctx_type_list);
3669 if (called_emsg != called_emsg_before)
3670 return FAIL;
3671
3672 if (**arg != '>')
3673 {
3674 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003675 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003676 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003677 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003678 return FAIL;
3679 }
3680 ++*arg;
3681 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3682 return FAIL;
3683 }
3684
3685 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3686 return FAIL;
3687
3688 if (want_type != NULL)
3689 {
3690 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003691 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003692
Bram Moolenaard1103582020-08-14 22:44:25 +02003693 generate_ppconst(cctx, ppconst);
3694 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003695 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003696 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003697 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3698 return FAIL;
3699 }
3700 }
3701
3702 return OK;
3703}
3704
3705/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706 * * number multiplication
3707 * / number division
3708 * % number modulo
3709 */
3710 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003711compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712{
3713 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003714 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003715 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003716
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003717 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003718 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719 return FAIL;
3720
3721 /*
3722 * Repeat computing, until no "*", "/" or "%" is following.
3723 */
3724 for (;;)
3725 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003726 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727 if (*op != '*' && *op != '/' && *op != '%')
3728 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003729 if (next != NULL)
3730 {
3731 *arg = next_line_from_context(cctx, TRUE);
3732 op = skipwhite(*arg);
3733 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003734
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003735 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003737 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003738 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003739 }
3740 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003741 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003742 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003743
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003744 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003745 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003747
3748 if (ppconst->pp_used == ppconst_used + 2
3749 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3750 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003751 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003752 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3753 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003754 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003755
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003756 // both are numbers: compute the result
3757 switch (*op)
3758 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003759 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003760 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003761 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003762 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003763 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003764 break;
3765 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003766 tv1->vval.v_number = res;
3767 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003768 }
3769 else
3770 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003771 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003772 generate_two_op(cctx, op);
3773 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774 }
3775
3776 return OK;
3777}
3778
3779/*
3780 * + number addition
3781 * - number subtraction
3782 * .. string concatenation
3783 */
3784 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003785compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786{
3787 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003788 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003790 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791
3792 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003793 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 return FAIL;
3795
3796 /*
3797 * Repeat computing, until no "+", "-" or ".." is following.
3798 */
3799 for (;;)
3800 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003801 op = may_peek_next_line(cctx, *arg, &next);
3802 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803 break;
3804 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003805 if (next != NULL)
3806 {
3807 *arg = next_line_from_context(cctx, TRUE);
3808 op = skipwhite(*arg);
3809 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003811 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003812 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003813 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003814 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815 }
3816
3817 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003818 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003819 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003821 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003822 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 return FAIL;
3824
Bram Moolenaara5565e42020-05-09 15:44:01 +02003825 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003826 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003827 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3828 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3829 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3830 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003832 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3833 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003834
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003835 // concat/subtract/add constant numbers
3836 if (*op == '+')
3837 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3838 else if (*op == '-')
3839 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3840 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003841 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003842 // concatenate constant strings
3843 char_u *s1 = tv1->vval.v_string;
3844 char_u *s2 = tv2->vval.v_string;
3845 size_t len1 = STRLEN(s1);
3846
3847 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3848 if (tv1->vval.v_string == NULL)
3849 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003850 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003851 return FAIL;
3852 }
3853 mch_memmove(tv1->vval.v_string, s1, len1);
3854 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003855 vim_free(s1);
3856 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003857 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003858 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859 }
3860 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003861 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003862 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003863 if (*op == '.')
3864 {
3865 if (may_generate_2STRING(-2, cctx) == FAIL
3866 || may_generate_2STRING(-1, cctx) == FAIL)
3867 return FAIL;
3868 generate_instr_drop(cctx, ISN_CONCAT, 1);
3869 }
3870 else
3871 generate_two_op(cctx, op);
3872 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873 }
3874
3875 return OK;
3876}
3877
3878/*
3879 * expr5a == expr5b
3880 * expr5a =~ expr5b
3881 * expr5a != expr5b
3882 * expr5a !~ expr5b
3883 * expr5a > expr5b
3884 * expr5a >= expr5b
3885 * expr5a < expr5b
3886 * expr5a <= expr5b
3887 * expr5a is expr5b
3888 * expr5a isnot expr5b
3889 *
3890 * Produces instructions:
3891 * EVAL expr5a Push result of "expr5a"
3892 * EVAL expr5b Push result of "expr5b"
3893 * COMPARE one of the compare instructions
3894 */
3895 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003896compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897{
3898 exptype_T type = EXPR_UNKNOWN;
3899 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003900 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003902 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003903 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904
3905 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003906 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907 return FAIL;
3908
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003909 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003910 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911
3912 /*
3913 * If there is a comparative operator, use it.
3914 */
3915 if (type != EXPR_UNKNOWN)
3916 {
3917 int ic = FALSE; // Default: do not ignore case
3918
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003919 if (next != NULL)
3920 {
3921 *arg = next_line_from_context(cctx, TRUE);
3922 p = skipwhite(*arg);
3923 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 if (type_is && (p[len] == '?' || p[len] == '#'))
3925 {
3926 semsg(_(e_invexpr2), *arg);
3927 return FAIL;
3928 }
3929 // extra question mark appended: ignore case
3930 if (p[len] == '?')
3931 {
3932 ic = TRUE;
3933 ++len;
3934 }
3935 // extra '#' appended: match case (ignored)
3936 else if (p[len] == '#')
3937 ++len;
3938 // nothing appended: match case
3939
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003940 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003942 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003943 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 }
3945
3946 // get the second variable
3947 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003948 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003949 return FAIL;
3950
Bram Moolenaara5565e42020-05-09 15:44:01 +02003951 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 return FAIL;
3953
Bram Moolenaara5565e42020-05-09 15:44:01 +02003954 if (ppconst->pp_used == ppconst_used + 2)
3955 {
3956 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3957 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3958 int ret;
3959
3960 // Both sides are a constant, compute the result now.
3961 // First check for a valid combination of types, this is more
3962 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003963 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003964 ret = FAIL;
3965 else
3966 {
3967 ret = typval_compare(tv1, tv2, type, ic);
3968 tv1->v_type = VAR_BOOL;
3969 tv1->vval.v_number = tv1->vval.v_number
3970 ? VVAL_TRUE : VVAL_FALSE;
3971 clear_tv(tv2);
3972 --ppconst->pp_used;
3973 }
3974 return ret;
3975 }
3976
3977 generate_ppconst(cctx, ppconst);
3978 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979 }
3980
3981 return OK;
3982}
3983
Bram Moolenaar7f141552020-05-09 17:35:53 +02003984static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3985
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986/*
3987 * Compile || or &&.
3988 */
3989 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003990compile_and_or(
3991 char_u **arg,
3992 cctx_T *cctx,
3993 char *op,
3994 ppconst_T *ppconst,
3995 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003997 char_u *next;
3998 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003999 int opchar = *op;
4000
4001 if (p[0] == opchar && p[1] == opchar)
4002 {
4003 garray_T *instr = &cctx->ctx_instr;
4004 garray_T end_ga;
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004005 garray_T *stack = &cctx->ctx_type_stack;
4006 type_T **typep;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007
4008 /*
4009 * Repeat until there is no following "||" or "&&"
4010 */
4011 ga_init2(&end_ga, sizeof(int), 10);
4012 while (p[0] == opchar && p[1] == opchar)
4013 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004014 if (next != NULL)
4015 {
4016 *arg = next_line_from_context(cctx, TRUE);
4017 p = skipwhite(*arg);
4018 }
4019
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004020 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4021 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004022 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004023 return FAIL;
4024 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025
Bram Moolenaara5565e42020-05-09 15:44:01 +02004026 // TODO: use ppconst if the value is a constant
4027 generate_ppconst(cctx, ppconst);
4028
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029 if (ga_grow(&end_ga, 1) == FAIL)
4030 {
4031 ga_clear(&end_ga);
4032 return FAIL;
4033 }
4034 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4035 ++end_ga.ga_len;
4036 generate_JUMP(cctx, opchar == '|'
4037 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4038
4039 // eval the next expression
4040 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004041 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004042 return FAIL;
4043
Bram Moolenaara5565e42020-05-09 15:44:01 +02004044 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4045 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046 {
4047 ga_clear(&end_ga);
4048 return FAIL;
4049 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004050
4051 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004053 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054
4055 // Fill in the end label in all jumps.
4056 while (end_ga.ga_len > 0)
4057 {
4058 isn_T *isn;
4059
4060 --end_ga.ga_len;
4061 isn = ((isn_T *)instr->ga_data)
4062 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4063 isn->isn_arg.jump.jump_where = instr->ga_len;
4064 }
4065 ga_clear(&end_ga);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004066
4067 // The resulting type can be used as a bool.
4068 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4069 if (*typep != &t_bool)
4070 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004071 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004072
4073 if (type != NULL)
4074 {
4075 *type = **typep;
4076 type->tt_flags |= TTFLAG_BOOL_OK;
4077 *typep = type;
4078 }
4079 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004080 }
4081
4082 return OK;
4083}
4084
4085/*
4086 * expr4a && expr4a && expr4a logical AND
4087 *
4088 * Produces instructions:
4089 * EVAL expr4a Push result of "expr4a"
4090 * JUMP_AND_KEEP_IF_FALSE end
4091 * EVAL expr4b Push result of "expr4b"
4092 * JUMP_AND_KEEP_IF_FALSE end
4093 * EVAL expr4c Push result of "expr4c"
4094 * end:
4095 */
4096 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004097compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004099 int ppconst_used = ppconst->pp_used;
4100
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004102 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004103 return FAIL;
4104
4105 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004106 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107}
4108
4109/*
4110 * expr3a || expr3b || expr3c logical OR
4111 *
4112 * Produces instructions:
4113 * EVAL expr3a Push result of "expr3a"
4114 * JUMP_AND_KEEP_IF_TRUE end
4115 * EVAL expr3b Push result of "expr3b"
4116 * JUMP_AND_KEEP_IF_TRUE end
4117 * EVAL expr3c Push result of "expr3c"
4118 * end:
4119 */
4120 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004121compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004123 int ppconst_used = ppconst->pp_used;
4124
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004125 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004126 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127 return FAIL;
4128
4129 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004130 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004131}
4132
4133/*
4134 * Toplevel expression: expr2 ? expr1a : expr1b
4135 *
4136 * Produces instructions:
4137 * EVAL expr2 Push result of "expr"
4138 * JUMP_IF_FALSE alt jump if false
4139 * EVAL expr1a
4140 * JUMP_ALWAYS end
4141 * alt: EVAL expr1b
4142 * end:
4143 */
4144 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004145compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146{
4147 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004148 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004149 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004150
Bram Moolenaar3988f642020-08-27 22:43:03 +02004151 // Ignore all kinds of errors when not producing code.
4152 if (cctx->ctx_skip == SKIP_YES)
4153 {
4154 skip_expr(arg);
4155 return OK;
4156 }
4157
Bram Moolenaar61a89812020-05-07 16:58:17 +02004158 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004159 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 return FAIL;
4161
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004162 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 if (*p == '?')
4164 {
4165 garray_T *instr = &cctx->ctx_instr;
4166 garray_T *stack = &cctx->ctx_type_stack;
4167 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004168 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004169 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004170 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004172 int has_const_expr = FALSE;
4173 int const_value = FALSE;
4174 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004175
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004176 if (next != NULL)
4177 {
4178 *arg = next_line_from_context(cctx, TRUE);
4179 p = skipwhite(*arg);
4180 }
4181
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004182 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4183 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004184 semsg(_(e_white_space_required_before_and_after_str), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004185 return FAIL;
4186 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004187
Bram Moolenaara5565e42020-05-09 15:44:01 +02004188 if (ppconst->pp_used == ppconst_used + 1)
4189 {
4190 // the condition is a constant, we know whether the ? or the :
4191 // expression is to be evaluated.
4192 has_const_expr = TRUE;
4193 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4194 clear_tv(&ppconst->pp_tv[ppconst_used]);
4195 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004196 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4197 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004198 }
4199 else
4200 {
4201 generate_ppconst(cctx, ppconst);
4202 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4203 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204
4205 // evaluate the second expression; any type is accepted
4206 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004207 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004208 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004209 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004210 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211
Bram Moolenaara5565e42020-05-09 15:44:01 +02004212 if (!has_const_expr)
4213 {
4214 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215
Bram Moolenaara5565e42020-05-09 15:44:01 +02004216 // remember the type and drop it
4217 --stack->ga_len;
4218 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219
Bram Moolenaara5565e42020-05-09 15:44:01 +02004220 end_idx = instr->ga_len;
4221 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4222
4223 // jump here from JUMP_IF_FALSE
4224 isn = ((isn_T *)instr->ga_data) + alt_idx;
4225 isn->isn_arg.jump.jump_where = instr->ga_len;
4226 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227
4228 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004229 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004230 if (*p != ':')
4231 {
4232 emsg(_(e_missing_colon));
4233 return FAIL;
4234 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004235 if (next != NULL)
4236 {
4237 *arg = next_line_from_context(cctx, TRUE);
4238 p = skipwhite(*arg);
4239 }
4240
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004241 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4242 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004243 semsg(_(e_white_space_required_before_and_after_str), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004244 return FAIL;
4245 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004246
4247 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004248 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004249 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4250 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004251 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004252 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004253 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004254 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004255 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004256
Bram Moolenaara5565e42020-05-09 15:44:01 +02004257 if (!has_const_expr)
4258 {
4259 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260
Bram Moolenaara5565e42020-05-09 15:44:01 +02004261 // If the types differ, the result has a more generic type.
4262 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4263 common_type(type1, type2, &type2, cctx->ctx_type_list);
4264
4265 // jump here from JUMP_ALWAYS
4266 isn = ((isn_T *)instr->ga_data) + end_idx;
4267 isn->isn_arg.jump.jump_where = instr->ga_len;
4268 }
4269
4270 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004271 }
4272 return OK;
4273}
4274
4275/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004276 * Toplevel expression.
4277 */
4278 static int
4279compile_expr0(char_u **arg, cctx_T *cctx)
4280{
4281 ppconst_T ppconst;
4282
4283 CLEAR_FIELD(ppconst);
4284 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4285 {
4286 clear_ppconst(&ppconst);
4287 return FAIL;
4288 }
4289 if (generate_ppconst(cctx, &ppconst) == FAIL)
4290 return FAIL;
4291 return OK;
4292}
4293
4294/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004295 * compile "return [expr]"
4296 */
4297 static char_u *
4298compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4299{
4300 char_u *p = arg;
4301 garray_T *stack = &cctx->ctx_type_stack;
4302 type_T *stack_type;
4303
4304 if (*p != NUL && *p != '|' && *p != '\n')
4305 {
4306 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004307 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 return NULL;
4309
4310 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4311 if (set_return_type)
4312 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004313 else
4314 {
4315 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4316 && stack_type->tt_type != VAR_VOID
4317 && stack_type->tt_type != VAR_UNKNOWN)
4318 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004319 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004320 return NULL;
4321 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004322 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4323 cctx, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004324 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004325 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326 }
4327 else
4328 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004329 // "set_return_type" cannot be TRUE, only used for a lambda which
4330 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004331 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4332 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004333 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004334 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335 return NULL;
4336 }
4337
4338 // No argument, return zero.
4339 generate_PUSHNR(cctx, 0);
4340 }
4341
4342 if (generate_instr(cctx, ISN_RETURN) == NULL)
4343 return NULL;
4344
4345 // "return val | endif" is possible
4346 return skipwhite(p);
4347}
4348
4349/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004350 * Get a line from the compilation context, compatible with exarg_T getline().
4351 * Return a pointer to the line in allocated memory.
4352 * Return NULL for end-of-file or some error.
4353 */
4354 static char_u *
4355exarg_getline(
4356 int c UNUSED,
4357 void *cookie,
4358 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004359 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004360{
4361 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004362 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004363
Bram Moolenaar66250c92020-08-20 15:02:42 +02004364 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004365 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004366 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004367 return NULL;
4368 ++cctx->ctx_lnum;
4369 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4370 // Comment lines result in NULL pointers, skip them.
4371 if (p != NULL)
4372 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004373 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004374}
4375
4376/*
4377 * Compile a nested :def command.
4378 */
4379 static char_u *
4380compile_nested_function(exarg_T *eap, cctx_T *cctx)
4381{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004382 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004383 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004384 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004385 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004386 lvar_T *lvar;
4387 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004388 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004389
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004390 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004391 {
4392 emsg(_(e_cannot_use_bang_with_nested_def));
4393 return NULL;
4394 }
4395
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004396 // Only g:Func() can use a namespace.
4397 if (name_start[1] == ':' && !is_global)
4398 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004399 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004400 return NULL;
4401 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004402 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4403 return NULL;
4404
Bram Moolenaar04b12692020-05-04 23:24:44 +02004405 eap->arg = name_end;
4406 eap->getline = exarg_getline;
4407 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004408 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004409 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004410 lambda_name = get_lambda_name();
4411 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004412
Bram Moolenaar822ba242020-05-24 23:00:18 +02004413 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004414 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004415 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004416 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004417 {
4418 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004419 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004420 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004421
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004422 if (is_global)
4423 {
4424 char_u *func_name = vim_strnsave(name_start + 2,
4425 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004426
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004427 if (func_name == NULL)
4428 r = FAIL;
4429 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004430 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004431 }
4432 else
4433 {
4434 // Define a local variable for the function reference.
4435 lvar = reserve_local(cctx, name_start, name_end - name_start,
4436 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004437 if (lvar == NULL)
4438 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004439 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004440 return NULL;
4441 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4442 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004443
Bram Moolenaar61a89812020-05-07 16:58:17 +02004444 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004445 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004446}
4447
4448/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004449 * Return the length of an assignment operator, or zero if there isn't one.
4450 */
4451 int
4452assignment_len(char_u *p, int *heredoc)
4453{
4454 if (*p == '=')
4455 {
4456 if (p[1] == '<' && p[2] == '<')
4457 {
4458 *heredoc = TRUE;
4459 return 3;
4460 }
4461 return 1;
4462 }
4463 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4464 return 2;
4465 if (STRNCMP(p, "..=", 3) == 0)
4466 return 3;
4467 return 0;
4468}
4469
4470// words that cannot be used as a variable
4471static char *reserved[] = {
4472 "true",
4473 "false",
4474 NULL
4475};
4476
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004477typedef enum {
4478 dest_local,
4479 dest_option,
4480 dest_env,
4481 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004482 dest_buffer,
4483 dest_window,
4484 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004485 dest_vimvar,
4486 dest_script,
4487 dest_reg,
4488} assign_dest_T;
4489
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004491 * Generate the load instruction for "name".
4492 */
4493 static void
4494generate_loadvar(
4495 cctx_T *cctx,
4496 assign_dest_T dest,
4497 char_u *name,
4498 lvar_T *lvar,
4499 type_T *type)
4500{
4501 switch (dest)
4502 {
4503 case dest_option:
4504 // TODO: check the option exists
4505 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4506 break;
4507 case dest_global:
4508 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4509 break;
4510 case dest_buffer:
4511 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4512 break;
4513 case dest_window:
4514 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4515 break;
4516 case dest_tab:
4517 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4518 break;
4519 case dest_script:
4520 compile_load_scriptvar(cctx,
4521 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4522 break;
4523 case dest_env:
4524 // Include $ in the name here
4525 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4526 break;
4527 case dest_reg:
4528 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4529 break;
4530 case dest_vimvar:
4531 generate_LOADV(cctx, name + 2, TRUE);
4532 break;
4533 case dest_local:
4534 if (lvar->lv_from_outer)
4535 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4536 NULL, type);
4537 else
4538 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4539 break;
4540 }
4541}
4542
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004543 void
4544vim9_declare_error(char_u *name)
4545{
4546 char *scope = "";
4547
4548 switch (*name)
4549 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004550 case 'g': scope = _("global"); break;
4551 case 'b': scope = _("buffer"); break;
4552 case 'w': scope = _("window"); break;
4553 case 't': scope = _("tab"); break;
4554 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004555 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004556 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004557 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004558 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004559 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004560 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004561 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004562 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004563 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004564}
4565
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004566/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004567 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004568 * "let name"
4569 * "var name = expr"
4570 * "final name = expr"
4571 * "const name = expr"
4572 * "name = expr"
4573 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004574 * Return NULL for an error.
4575 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 */
4577 static char_u *
4578compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4579{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004580 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004581 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004582 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004583 char_u *ret = NULL;
4584 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004585 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004586 int scriptvar_sid = 0;
4587 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004588 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004590 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004591 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004592 int oplen = 0;
4593 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004594 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004595 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004596 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004598 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
4599 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004601 // Skip over the "var" or "[var, var]" to get to any "=".
4602 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4603 if (p == NULL)
4604 return *arg == '[' ? arg : NULL;
4605
4606 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004608 // TODO: should we allow this, and figure out type inference from list
4609 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004610 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004611 return NULL;
4612 }
4613
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614 sp = p;
4615 p = skipwhite(p);
4616 op = p;
4617 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004618
4619 if (var_count > 0 && oplen == 0)
4620 // can be something like "[1, 2]->func()"
4621 return arg;
4622
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004623 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4624 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004625 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004626 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004627 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004628
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629 if (heredoc)
4630 {
4631 list_T *l;
4632 listitem_T *li;
4633
4634 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004635 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004637 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02004638 if (l == NULL)
4639 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004640
Bram Moolenaar078269b2020-09-21 20:35:55 +02004641 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02004643 // Push each line and the create the list.
4644 FOR_ALL_LIST_ITEMS(l, li)
4645 {
4646 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4647 li->li_tv.vval.v_string = NULL;
4648 }
4649 generate_NEWLIST(cctx, l->lv_len);
4650 type = &t_list_string;
4651 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653 list_free(l);
4654 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004655 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004656 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004657 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004658 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004659 // for "[var, var] = expr" evaluate the expression here, loop over the
4660 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004661
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004662 p = skipwhite(op + oplen);
4663 if (compile_expr0(&p, cctx) == FAIL)
4664 return NULL;
4665 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004666
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004667 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004669 type_T *stacktype;
4670
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004671 stacktype = stack->ga_len == 0 ? &t_void
4672 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004673 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004674 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004675 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004676 goto theend;
4677 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004678 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004679 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004680 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004681 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4682 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004683 }
4684 }
4685
4686 /*
4687 * Loop over variables in "[var, var] = expr".
4688 * For "var = expr" and "let var: type" this is done only once.
4689 */
4690 if (var_count > 0)
4691 var_start = skipwhite(arg + 1); // skip over the "["
4692 else
4693 var_start = arg;
4694 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4695 {
4696 char_u *var_end = skip_var_one(var_start, FALSE);
4697 size_t varlen;
4698 int new_local = FALSE;
4699 int opt_type;
4700 int opt_flags = 0;
4701 assign_dest_T dest = dest_local;
4702 int vimvaridx = -1;
4703 lvar_T *lvar = NULL;
4704 lvar_T arg_lvar;
4705 int has_type = FALSE;
4706 int has_index = FALSE;
4707 int instr_count = -1;
4708
Bram Moolenaar65821722020-08-02 18:58:54 +02004709 if (*var_start == '@')
4710 p = var_start + 2;
4711 else
4712 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02004713 // skip over the leading "&", "&l:", "&g:" and "$"
4714 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02004715 p = to_name_end(p, TRUE);
4716 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004717
4718 // "a: type" is declaring variable "a" with a type, not "a:".
4719 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4720 --var_end;
4721 if (is_decl && p == var_start + 2 && p[-1] == ':')
4722 --p;
4723
4724 varlen = p - var_start;
4725 vim_free(name);
4726 name = vim_strnsave(var_start, varlen);
4727 if (name == NULL)
4728 return NULL;
4729 if (!heredoc)
4730 type = &t_any;
4731
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004732 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004733 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004734 int declare_error = FALSE;
4735
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004736 if (*var_start == '&')
4737 {
4738 int cc;
4739 long numval;
4740
4741 dest = dest_option;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004742 if (cmdidx == CMD_final || cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004743 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004744 emsg(_(e_const_option));
4745 goto theend;
4746 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004747 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004748 p = var_start;
4749 p = find_option_end(&p, &opt_flags);
4750 if (p == NULL)
4751 {
4752 // cannot happen?
4753 emsg(_(e_letunexp));
4754 goto theend;
4755 }
4756 cc = *p;
4757 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02004758 opt_type = get_option_value(skip_option_env_lead(var_start),
4759 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004760 *p = cc;
4761 if (opt_type == -3)
4762 {
4763 semsg(_(e_unknown_option), var_start);
4764 goto theend;
4765 }
4766 if (opt_type == -2 || opt_type == 0)
4767 type = &t_string;
4768 else
4769 type = &t_number; // both number and boolean option
4770 }
4771 else if (*var_start == '$')
4772 {
4773 dest = dest_env;
4774 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004775 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004776 }
4777 else if (*var_start == '@')
4778 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004779 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004780 {
4781 emsg_invreg(var_start[1]);
4782 goto theend;
4783 }
4784 dest = dest_reg;
4785 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004786 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004787 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004788 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004789 {
4790 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004791 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004792 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004793 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004794 {
4795 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004796 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004797 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004798 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004799 {
4800 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004801 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004802 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004803 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004804 {
4805 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004806 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004807 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004808 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004809 {
4810 typval_T *vtv;
4811 int di_flags;
4812
4813 vimvaridx = find_vim_var(name + 2, &di_flags);
4814 if (vimvaridx < 0)
4815 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004816 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004817 goto theend;
4818 }
4819 // We use the current value of "sandbox" here, is that OK?
4820 if (var_check_ro(di_flags, name, FALSE))
4821 goto theend;
4822 dest = dest_vimvar;
4823 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004824 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004825 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004826 }
4827 else
4828 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004829 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004830
4831 for (idx = 0; reserved[idx] != NULL; ++idx)
4832 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004833 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004834 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004835 goto theend;
4836 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004837
4838 lvar = lookup_local(var_start, varlen, cctx);
4839 if (lvar == NULL)
4840 {
4841 CLEAR_FIELD(arg_lvar);
4842 if (lookup_arg(var_start, varlen,
4843 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4844 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004845 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004846 if (is_decl)
4847 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004848 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004849 goto theend;
4850 }
4851 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004852 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004853 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004854 if (lvar != NULL)
4855 {
4856 if (is_decl)
4857 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004858 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004859 goto theend;
4860 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004861 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004862 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004863 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004864 int script_namespace = varlen > 1
4865 && STRNCMP(var_start, "s:", 2) == 0;
4866 int script_var = (script_namespace
Bram Moolenaar53900992020-08-22 19:02:02 +02004867 ? lookup_script(var_start + 2, varlen - 2, FALSE)
4868 : lookup_script(var_start, varlen, TRUE)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004869 imported_T *import =
4870 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004871
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004872 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004873 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004874 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4875
4876 if (is_decl)
4877 {
4878 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004879 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004880 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004881 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004882 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004883 name);
4884 goto theend;
4885 }
4886 else if (cctx->ctx_ufunc->uf_script_ctx_version
4887 == SCRIPT_VERSION_VIM9
4888 && script_namespace
4889 && !script_var && import == NULL)
4890 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004891 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004892 goto theend;
4893 }
4894
4895 dest = dest_script;
4896
4897 // existing script-local variables should have a type
4898 scriptvar_sid = current_sctx.sc_sid;
4899 if (import != NULL)
4900 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004901 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004902 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02004903 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4904 rawname, TRUE);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02004905 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02004906 {
4907 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4908 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004909 ((svar_T *)si->sn_var_vals.ga_data)
4910 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004911 type = sv->sv_type;
4912 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004913 }
4914 }
4915 else if (name[1] == ':' && name[2] != NUL)
4916 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004917 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004918 goto theend;
4919 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004920 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004921 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004922 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004923 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004924 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004925 else if (check_defined(var_start, varlen, cctx) == FAIL)
4926 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004927 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004928 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004929
4930 if (declare_error)
4931 {
4932 vim9_declare_error(name);
4933 goto theend;
4934 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004935 }
4936
4937 // handle "a:name" as a name, not index "name" on "a"
4938 if (varlen > 1 || var_start[varlen] != ':')
4939 p = var_end;
4940
4941 if (dest != dest_option)
4942 {
4943 if (is_decl && *p == ':')
4944 {
4945 // parse optional type: "let var: type = expr"
4946 if (!VIM_ISWHITE(p[1]))
4947 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004948 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004949 goto theend;
4950 }
4951 p = skipwhite(p + 1);
4952 type = parse_type(&p, cctx->ctx_type_list);
4953 has_type = TRUE;
4954 }
4955 else if (lvar != NULL)
4956 type = lvar->lv_type;
4957 }
4958
4959 if (oplen == 3 && !heredoc && dest != dest_global
4960 && type->tt_type != VAR_STRING
4961 && type->tt_type != VAR_ANY)
4962 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004963 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004964 goto theend;
4965 }
4966
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004967 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004968 {
4969 if (oplen > 1 && !heredoc)
4970 {
4971 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004972 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004973 goto theend;
4974 }
4975
4976 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004977 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
4978 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004979 goto theend;
4980 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004981 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004982 if (lvar == NULL)
4983 goto theend;
4984 new_local = TRUE;
4985 }
4986
4987 member_type = type;
4988 if (var_end > var_start + varlen)
4989 {
4990 // Something follows after the variable: "var[idx]".
4991 if (is_decl)
4992 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004993 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004994 goto theend;
4995 }
4996
4997 if (var_start[varlen] == '[')
4998 {
4999 has_index = TRUE;
5000 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005001 member_type = &t_any;
5002 else
5003 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005004 }
5005 else
5006 {
5007 semsg("Not supported yet: %s", var_start);
5008 goto theend;
5009 }
5010 }
5011 else if (lvar == &arg_lvar)
5012 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005013 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005014 goto theend;
5015 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005016 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5017 {
5018 semsg(_(e_cannot_assign_to_constant), name);
5019 goto theend;
5020 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005021
5022 if (!heredoc)
5023 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005024 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005025 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005026 if (oplen > 0 && var_count == 0)
5027 {
5028 // skip over the "=" and the expression
5029 p = skipwhite(op + oplen);
5030 compile_expr0(&p, cctx);
5031 }
5032 }
5033 else if (oplen > 0)
5034 {
5035 type_T *stacktype;
5036
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005037 // For "var = expr" evaluate the expression.
5038 if (var_count == 0)
5039 {
5040 int r;
5041
5042 // for "+=", "*=", "..=" etc. first load the current value
5043 if (*op != '=')
5044 {
5045 generate_loadvar(cctx, dest, name, lvar, type);
5046
5047 if (has_index)
5048 {
5049 // TODO: get member from list or dict
5050 emsg("Index with operation not supported yet");
5051 goto theend;
5052 }
5053 }
5054
5055 // Compile the expression. Temporarily hide the new local
5056 // variable here, it is not available to this expression.
5057 if (new_local)
5058 --cctx->ctx_locals.ga_len;
5059 instr_count = instr->ga_len;
5060 p = skipwhite(op + oplen);
5061 r = compile_expr0(&p, cctx);
5062 if (new_local)
5063 ++cctx->ctx_locals.ga_len;
5064 if (r == FAIL)
5065 goto theend;
5066 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005067 else if (semicolon && var_idx == var_count - 1)
5068 {
5069 // For "[var; var] = expr" get the rest of the list
5070 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5071 goto theend;
5072 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005073 else
5074 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005075 // For "[var, var] = expr" get the "var_idx" item from the
5076 // list.
5077 if (generate_GETITEM(cctx, var_idx) == FAIL)
5078 return FAIL;
5079 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005080
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005081 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005082 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005083 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005084 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005085 if ((stacktype->tt_type == VAR_FUNC
5086 || stacktype->tt_type == VAR_PARTIAL)
5087 && var_wrong_func_name(name, TRUE))
5088 goto theend;
5089
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005090 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005091 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005092 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005093 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005094 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005095 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005096 }
5097 else
5098 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005099 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005100 // for a variable that implies &t_any.
5101 if (stacktype == &t_list_empty)
5102 lvar->lv_type = &t_list_any;
5103 else if (stacktype == &t_dict_empty)
5104 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005105 else if (stacktype == &t_unknown)
5106 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005107 else
5108 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005109 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005110 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005111 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005112 {
5113 type_T *use_type = lvar->lv_type;
5114
Bram Moolenaar71abe482020-09-14 22:28:30 +02005115 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005116 if (has_index)
5117 {
5118 use_type = use_type->tt_member;
5119 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005120 // could be indexing "any"
5121 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005122 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005123 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005124 == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005125 goto theend;
5126 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005127 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005128 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005129 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005130 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005131 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005132 else if (cmdidx == CMD_final)
5133 {
5134 emsg(_(e_final_requires_a_value));
5135 goto theend;
5136 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005137 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005138 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005139 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005140 goto theend;
5141 }
5142 else if (!has_type || dest == dest_option)
5143 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005144 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005145 goto theend;
5146 }
5147 else
5148 {
5149 // variables are always initialized
5150 if (ga_grow(instr, 1) == FAIL)
5151 goto theend;
5152 switch (member_type->tt_type)
5153 {
5154 case VAR_BOOL:
5155 generate_PUSHBOOL(cctx, VVAL_FALSE);
5156 break;
5157 case VAR_FLOAT:
5158#ifdef FEAT_FLOAT
5159 generate_PUSHF(cctx, 0.0);
5160#endif
5161 break;
5162 case VAR_STRING:
5163 generate_PUSHS(cctx, NULL);
5164 break;
5165 case VAR_BLOB:
5166 generate_PUSHBLOB(cctx, NULL);
5167 break;
5168 case VAR_FUNC:
5169 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5170 break;
5171 case VAR_LIST:
5172 generate_NEWLIST(cctx, 0);
5173 break;
5174 case VAR_DICT:
5175 generate_NEWDICT(cctx, 0);
5176 break;
5177 case VAR_JOB:
5178 generate_PUSHJOB(cctx, NULL);
5179 break;
5180 case VAR_CHANNEL:
5181 generate_PUSHCHANNEL(cctx, NULL);
5182 break;
5183 case VAR_NUMBER:
5184 case VAR_UNKNOWN:
5185 case VAR_ANY:
5186 case VAR_PARTIAL:
5187 case VAR_VOID:
5188 case VAR_SPECIAL: // cannot happen
5189 generate_PUSHNR(cctx, 0);
5190 break;
5191 }
5192 }
5193 if (var_count == 0)
5194 end = p;
5195 }
5196
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005197 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005198 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005199 break;
5200
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005201 if (oplen > 0 && *op != '=')
5202 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005203 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005204 type_T *stacktype;
5205
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005206 if (*op == '.')
5207 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005208 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005209 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005210 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005211 if (
5212#ifdef FEAT_FLOAT
5213 // If variable is float operation with number is OK.
5214 !(expected == &t_float && stacktype == &t_number) &&
5215#endif
5216 need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005217 goto theend;
5218
5219 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005220 {
5221 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5222 goto theend;
5223 }
5224 else if (*op == '+')
5225 {
5226 if (generate_add_instr(cctx,
5227 operator_type(member_type, stacktype),
5228 member_type, stacktype) == FAIL)
5229 goto theend;
5230 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005231 else if (generate_two_op(cctx, op) == FAIL)
5232 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005233 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005234
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005235 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005236 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005237 int r;
5238
5239 // Compile the "idx" in "var[idx]".
5240 if (new_local)
5241 --cctx->ctx_locals.ga_len;
5242 p = skipwhite(var_start + varlen + 1);
5243 r = compile_expr0(&p, cctx);
5244 if (new_local)
5245 ++cctx->ctx_locals.ga_len;
5246 if (r == FAIL)
5247 goto theend;
5248 if (*skipwhite(p) != ']')
5249 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005250 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005251 emsg(_(e_missbrac));
5252 goto theend;
5253 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005254 if (type == &t_any)
5255 {
5256 type_T *idx_type = ((type_T **)stack->ga_data)[
5257 stack->ga_len - 1];
5258 // Index on variable of unknown type: guess the type from the
5259 // index type: number is dict, otherwise dict.
5260 // TODO: should do the assignment at runtime
5261 if (idx_type->tt_type == VAR_NUMBER)
5262 type = &t_list_any;
5263 else
5264 type = &t_dict_any;
5265 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005266 if (type->tt_type == VAR_DICT
5267 && may_generate_2STRING(-1, cctx) == FAIL)
5268 goto theend;
5269 if (type->tt_type == VAR_LIST
5270 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005271 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005272 {
5273 emsg(_(e_number_exp));
5274 goto theend;
5275 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005276
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005277 // Load the dict or list. On the stack we then have:
5278 // - value
5279 // - index
5280 // - variable
5281 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005282
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005283 if (type->tt_type == VAR_LIST)
5284 {
5285 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5286 return FAIL;
5287 }
5288 else if (type->tt_type == VAR_DICT)
5289 {
5290 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5291 return FAIL;
5292 }
5293 else
5294 {
5295 emsg(_(e_listreq));
5296 goto theend;
5297 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005298 }
5299 else
5300 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005301 if (is_decl && cmdidx == CMD_const
5302 && (dest == dest_script || dest == dest_local))
5303 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005304 generate_LOCKCONST(cctx);
5305
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005306 switch (dest)
5307 {
5308 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005309 generate_STOREOPT(cctx, skip_option_env_lead(name),
5310 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005311 break;
5312 case dest_global:
5313 // include g: with the name, easier to execute that way
5314 generate_STORE(cctx, ISN_STOREG, 0, name);
5315 break;
5316 case dest_buffer:
5317 // include b: with the name, easier to execute that way
5318 generate_STORE(cctx, ISN_STOREB, 0, name);
5319 break;
5320 case dest_window:
5321 // include w: with the name, easier to execute that way
5322 generate_STORE(cctx, ISN_STOREW, 0, name);
5323 break;
5324 case dest_tab:
5325 // include t: with the name, easier to execute that way
5326 generate_STORE(cctx, ISN_STORET, 0, name);
5327 break;
5328 case dest_env:
5329 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5330 break;
5331 case dest_reg:
5332 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5333 break;
5334 case dest_vimvar:
5335 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5336 break;
5337 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005338 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005339 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005340 {
5341 char_u *name_s = name;
5342
5343 // Include s: in the name for store_var()
5344 if (name[1] != ':')
5345 {
5346 int len = (int)STRLEN(name) + 3;
5347
5348 name_s = alloc(len);
5349 if (name_s == NULL)
5350 name_s = name;
5351 else
5352 vim_snprintf((char *)name_s, len,
5353 "s:%s", name);
5354 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005355 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5356 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005357 if (name_s != name)
5358 vim_free(name_s);
5359 }
5360 else
5361 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005362 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005363 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005364 break;
5365 case dest_local:
5366 if (lvar != NULL)
5367 {
5368 isn_T *isn = ((isn_T *)instr->ga_data)
5369 + instr->ga_len - 1;
5370
5371 // optimization: turn "var = 123" from ISN_PUSHNR +
5372 // ISN_STORE into ISN_STORENR
5373 if (!lvar->lv_from_outer
5374 && instr->ga_len == instr_count + 1
5375 && isn->isn_type == ISN_PUSHNR)
5376 {
5377 varnumber_T val = isn->isn_arg.number;
5378
5379 isn->isn_type = ISN_STORENR;
5380 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5381 isn->isn_arg.storenr.stnr_val = val;
5382 if (stack->ga_len > 0)
5383 --stack->ga_len;
5384 }
5385 else if (lvar->lv_from_outer)
5386 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005387 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005388 else
5389 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5390 }
5391 break;
5392 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005393 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005394
5395 if (var_idx + 1 < var_count)
5396 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005397 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005398
5399 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005400 if (var_count > 0 && !semicolon)
5401 {
5402 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5403 goto theend;
5404 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005405
Bram Moolenaarb2097502020-07-19 17:17:02 +02005406 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005407
5408theend:
5409 vim_free(name);
5410 return ret;
5411}
5412
5413/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005414 * Check if "name" can be "unlet".
5415 */
5416 int
5417check_vim9_unlet(char_u *name)
5418{
5419 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5420 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005421 // "unlet s:var" is allowed in legacy script.
5422 if (*name == 's' && !script_is_vim9())
5423 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005424 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005425 return FAIL;
5426 }
5427 return OK;
5428}
5429
5430/*
5431 * Callback passed to ex_unletlock().
5432 */
5433 static int
5434compile_unlet(
5435 lval_T *lvp,
5436 char_u *name_end,
5437 exarg_T *eap,
5438 int deep UNUSED,
5439 void *coookie)
5440{
5441 cctx_T *cctx = coookie;
5442
5443 if (lvp->ll_tv == NULL)
5444 {
5445 char_u *p = lvp->ll_name;
5446 int cc = *name_end;
5447 int ret = OK;
5448
5449 // Normal name. Only supports g:, w:, t: and b: namespaces.
5450 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005451 if (*p == '$')
5452 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5453 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005454 ret = FAIL;
5455 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005456 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005457
5458 *name_end = cc;
5459 return ret;
5460 }
5461
5462 // TODO: unlet {list}[idx]
5463 // TODO: unlet {dict}[key]
5464 emsg("Sorry, :unlet not fully implemented yet");
5465 return FAIL;
5466}
5467
5468/*
5469 * compile "unlet var", "lock var" and "unlock var"
5470 * "arg" points to "var".
5471 */
5472 static char_u *
5473compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5474{
5475 char_u *p = arg;
5476
5477 if (eap->cmdidx != CMD_unlet)
5478 {
5479 emsg("Sorry, :lock and unlock not implemented yet");
5480 return NULL;
5481 }
5482
5483 if (*p == '!')
5484 {
5485 p = skipwhite(p + 1);
5486 eap->forceit = TRUE;
5487 }
5488
5489 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5490 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5491}
5492
5493/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005494 * Compile an :import command.
5495 */
5496 static char_u *
5497compile_import(char_u *arg, cctx_T *cctx)
5498{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005499 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005500}
5501
5502/*
5503 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5504 */
5505 static int
5506compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5507{
5508 garray_T *instr = &cctx->ctx_instr;
5509 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5510
5511 if (endlabel == NULL)
5512 return FAIL;
5513 endlabel->el_next = *el;
5514 *el = endlabel;
5515 endlabel->el_end_label = instr->ga_len;
5516
5517 generate_JUMP(cctx, when, 0);
5518 return OK;
5519}
5520
5521 static void
5522compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5523{
5524 garray_T *instr = &cctx->ctx_instr;
5525
5526 while (*el != NULL)
5527 {
5528 endlabel_T *cur = (*el);
5529 isn_T *isn;
5530
5531 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5532 isn->isn_arg.jump.jump_where = instr->ga_len;
5533 *el = cur->el_next;
5534 vim_free(cur);
5535 }
5536}
5537
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005538 static void
5539compile_free_jump_to_end(endlabel_T **el)
5540{
5541 while (*el != NULL)
5542 {
5543 endlabel_T *cur = (*el);
5544
5545 *el = cur->el_next;
5546 vim_free(cur);
5547 }
5548}
5549
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005550/*
5551 * Create a new scope and set up the generic items.
5552 */
5553 static scope_T *
5554new_scope(cctx_T *cctx, scopetype_T type)
5555{
5556 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5557
5558 if (scope == NULL)
5559 return NULL;
5560 scope->se_outer = cctx->ctx_scope;
5561 cctx->ctx_scope = scope;
5562 scope->se_type = type;
5563 scope->se_local_count = cctx->ctx_locals.ga_len;
5564 return scope;
5565}
5566
5567/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005568 * Free the current scope and go back to the outer scope.
5569 */
5570 static void
5571drop_scope(cctx_T *cctx)
5572{
5573 scope_T *scope = cctx->ctx_scope;
5574
5575 if (scope == NULL)
5576 {
5577 iemsg("calling drop_scope() without a scope");
5578 return;
5579 }
5580 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005581 switch (scope->se_type)
5582 {
5583 case IF_SCOPE:
5584 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5585 case FOR_SCOPE:
5586 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5587 case WHILE_SCOPE:
5588 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5589 case TRY_SCOPE:
5590 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5591 case NO_SCOPE:
5592 case BLOCK_SCOPE:
5593 break;
5594 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005595 vim_free(scope);
5596}
5597
5598/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005599 * compile "if expr"
5600 *
5601 * "if expr" Produces instructions:
5602 * EVAL expr Push result of "expr"
5603 * JUMP_IF_FALSE end
5604 * ... body ...
5605 * end:
5606 *
5607 * "if expr | else" Produces instructions:
5608 * EVAL expr Push result of "expr"
5609 * JUMP_IF_FALSE else
5610 * ... body ...
5611 * JUMP_ALWAYS end
5612 * else:
5613 * ... body ...
5614 * end:
5615 *
5616 * "if expr1 | elseif expr2 | else" Produces instructions:
5617 * EVAL expr Push result of "expr"
5618 * JUMP_IF_FALSE elseif
5619 * ... body ...
5620 * JUMP_ALWAYS end
5621 * elseif:
5622 * EVAL expr Push result of "expr"
5623 * JUMP_IF_FALSE else
5624 * ... body ...
5625 * JUMP_ALWAYS end
5626 * else:
5627 * ... body ...
5628 * end:
5629 */
5630 static char_u *
5631compile_if(char_u *arg, cctx_T *cctx)
5632{
5633 char_u *p = arg;
5634 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005635 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005636 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005637 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005638 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005639
Bram Moolenaara5565e42020-05-09 15:44:01 +02005640 CLEAR_FIELD(ppconst);
5641 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005642 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005643 clear_ppconst(&ppconst);
5644 return NULL;
5645 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005646 if (cctx->ctx_skip == SKIP_YES)
5647 clear_ppconst(&ppconst);
5648 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005649 {
5650 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005651 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005652 clear_ppconst(&ppconst);
5653 }
5654 else
5655 {
5656 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005657 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005658 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005659 return NULL;
5660 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005661
5662 scope = new_scope(cctx, IF_SCOPE);
5663 if (scope == NULL)
5664 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005665 scope->se_skip_save = skip_save;
5666 // "is_had_return" will be reset if any block does not end in :return
5667 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005668
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005669 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005670 {
5671 // "where" is set when ":elseif", "else" or ":endif" is found
5672 scope->se_u.se_if.is_if_label = instr->ga_len;
5673 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5674 }
5675 else
5676 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005677
5678 return p;
5679}
5680
5681 static char_u *
5682compile_elseif(char_u *arg, cctx_T *cctx)
5683{
5684 char_u *p = arg;
5685 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005686 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005687 isn_T *isn;
5688 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005689 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02005690 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005691
5692 if (scope == NULL || scope->se_type != IF_SCOPE)
5693 {
5694 emsg(_(e_elseif_without_if));
5695 return NULL;
5696 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005697 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005698 if (!cctx->ctx_had_return)
5699 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005700
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005701 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005702 {
5703 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005704 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005705 return NULL;
5706 // previous "if" or "elseif" jumps here
5707 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5708 isn->isn_arg.jump.jump_where = instr->ga_len;
5709 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005710
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005711 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005712 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02005713 if (cctx->ctx_skip == SKIP_YES)
5714 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005715 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005716 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005717 clear_ppconst(&ppconst);
5718 return NULL;
5719 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02005720 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005721 if (scope->se_skip_save == SKIP_YES)
5722 clear_ppconst(&ppconst);
5723 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005724 {
5725 // The expression results in a constant.
5726 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005727 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005728 clear_ppconst(&ppconst);
5729 scope->se_u.se_if.is_if_label = -1;
5730 }
5731 else
5732 {
5733 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005734 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005735 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005736 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005737
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005738 // "where" is set when ":elseif", "else" or ":endif" is found
5739 scope->se_u.se_if.is_if_label = instr->ga_len;
5740 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5741 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005742
5743 return p;
5744}
5745
5746 static char_u *
5747compile_else(char_u *arg, cctx_T *cctx)
5748{
5749 char_u *p = arg;
5750 garray_T *instr = &cctx->ctx_instr;
5751 isn_T *isn;
5752 scope_T *scope = cctx->ctx_scope;
5753
5754 if (scope == NULL || scope->se_type != IF_SCOPE)
5755 {
5756 emsg(_(e_else_without_if));
5757 return NULL;
5758 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005759 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005760 if (!cctx->ctx_had_return)
5761 scope->se_u.se_if.is_had_return = FALSE;
5762 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005763
Bram Moolenaarefd88552020-06-18 20:50:10 +02005764 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005765 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005766 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005767 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005768 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005769 if (!cctx->ctx_had_return
5770 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5771 JUMP_ALWAYS, cctx) == FAIL)
5772 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005773 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005774
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005775 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005776 {
5777 if (scope->se_u.se_if.is_if_label >= 0)
5778 {
5779 // previous "if" or "elseif" jumps here
5780 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5781 isn->isn_arg.jump.jump_where = instr->ga_len;
5782 scope->se_u.se_if.is_if_label = -1;
5783 }
5784 }
5785
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005786 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005787 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5788 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005789
5790 return p;
5791}
5792
5793 static char_u *
5794compile_endif(char_u *arg, cctx_T *cctx)
5795{
5796 scope_T *scope = cctx->ctx_scope;
5797 ifscope_T *ifscope;
5798 garray_T *instr = &cctx->ctx_instr;
5799 isn_T *isn;
5800
5801 if (scope == NULL || scope->se_type != IF_SCOPE)
5802 {
5803 emsg(_(e_endif_without_if));
5804 return NULL;
5805 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005806 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005807 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005808 if (!cctx->ctx_had_return)
5809 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005810
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005811 if (scope->se_u.se_if.is_if_label >= 0)
5812 {
5813 // previous "if" or "elseif" jumps here
5814 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5815 isn->isn_arg.jump.jump_where = instr->ga_len;
5816 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005817 // Fill in the "end" label in jumps at the end of the blocks.
5818 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005819 cctx->ctx_skip = scope->se_skip_save;
5820
5821 // If all the blocks end in :return and there is an :else then the
5822 // had_return flag is set.
5823 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005824
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005825 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005826 return arg;
5827}
5828
5829/*
5830 * compile "for var in expr"
5831 *
5832 * Produces instructions:
5833 * PUSHNR -1
5834 * STORE loop-idx Set index to -1
5835 * EVAL expr Push result of "expr"
5836 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5837 * - if beyond end, jump to "end"
5838 * - otherwise get item from list and push it
5839 * STORE var Store item in "var"
5840 * ... body ...
5841 * JUMP top Jump back to repeat
5842 * end: DROP Drop the result of "expr"
5843 *
5844 */
5845 static char_u *
5846compile_for(char_u *arg, cctx_T *cctx)
5847{
5848 char_u *p;
5849 size_t varlen;
5850 garray_T *instr = &cctx->ctx_instr;
5851 garray_T *stack = &cctx->ctx_type_stack;
5852 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005853 lvar_T *loop_lvar; // loop iteration variable
5854 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005855 type_T *vartype;
5856
5857 // TODO: list of variables: "for [key, value] in dict"
5858 // parse "var"
5859 for (p = arg; eval_isnamec1(*p); ++p)
5860 ;
5861 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005862 var_lvar = lookup_local(arg, varlen, cctx);
5863 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005864 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005865 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005866 return NULL;
5867 }
5868
5869 // consume "in"
5870 p = skipwhite(p);
5871 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5872 {
5873 emsg(_(e_missing_in));
5874 return NULL;
5875 }
5876 p = skipwhite(p + 2);
5877
5878
5879 scope = new_scope(cctx, FOR_SCOPE);
5880 if (scope == NULL)
5881 return NULL;
5882
5883 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005884 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5885 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005886 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005887 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005888 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005889 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005890 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005891
5892 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005893 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5894 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005895 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005896 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005897 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005898 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005899 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005900
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005901 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005902
5903 // compile "expr", it remains on the stack until "endfor"
5904 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005905 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005906 {
5907 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005908 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005909 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005910
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005911 // Now that we know the type of "var", check that it is a list, now or at
5912 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005913 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005914 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005915 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005916 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005917 return NULL;
5918 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005919 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005920 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005921
5922 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005923 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005924
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005925 generate_FOR(cctx, loop_lvar->lv_idx);
5926 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005927
5928 return arg;
5929}
5930
5931/*
5932 * compile "endfor"
5933 */
5934 static char_u *
5935compile_endfor(char_u *arg, cctx_T *cctx)
5936{
5937 garray_T *instr = &cctx->ctx_instr;
5938 scope_T *scope = cctx->ctx_scope;
5939 forscope_T *forscope;
5940 isn_T *isn;
5941
5942 if (scope == NULL || scope->se_type != FOR_SCOPE)
5943 {
5944 emsg(_(e_for));
5945 return NULL;
5946 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005947 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005948 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005949 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005950
5951 // At end of ":for" scope jump back to the FOR instruction.
5952 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5953
5954 // Fill in the "end" label in the FOR statement so it can jump here
5955 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5956 isn->isn_arg.forloop.for_end = instr->ga_len;
5957
5958 // Fill in the "end" label any BREAK statements
5959 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5960
5961 // Below the ":for" scope drop the "expr" list from the stack.
5962 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5963 return NULL;
5964
5965 vim_free(scope);
5966
5967 return arg;
5968}
5969
5970/*
5971 * compile "while expr"
5972 *
5973 * Produces instructions:
5974 * top: EVAL expr Push result of "expr"
5975 * JUMP_IF_FALSE end jump if false
5976 * ... body ...
5977 * JUMP top Jump back to repeat
5978 * end:
5979 *
5980 */
5981 static char_u *
5982compile_while(char_u *arg, cctx_T *cctx)
5983{
5984 char_u *p = arg;
5985 garray_T *instr = &cctx->ctx_instr;
5986 scope_T *scope;
5987
5988 scope = new_scope(cctx, WHILE_SCOPE);
5989 if (scope == NULL)
5990 return NULL;
5991
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005992 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005993
5994 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005995 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005996 return NULL;
5997
5998 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005999 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006000 JUMP_IF_FALSE, cctx) == FAIL)
6001 return FAIL;
6002
6003 return p;
6004}
6005
6006/*
6007 * compile "endwhile"
6008 */
6009 static char_u *
6010compile_endwhile(char_u *arg, cctx_T *cctx)
6011{
6012 scope_T *scope = cctx->ctx_scope;
6013
6014 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6015 {
6016 emsg(_(e_while));
6017 return NULL;
6018 }
6019 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006020 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006021
6022 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006023 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006024
6025 // Fill in the "end" label in the WHILE statement so it can jump here.
6026 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006027 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006028
6029 vim_free(scope);
6030
6031 return arg;
6032}
6033
6034/*
6035 * compile "continue"
6036 */
6037 static char_u *
6038compile_continue(char_u *arg, cctx_T *cctx)
6039{
6040 scope_T *scope = cctx->ctx_scope;
6041
6042 for (;;)
6043 {
6044 if (scope == NULL)
6045 {
6046 emsg(_(e_continue));
6047 return NULL;
6048 }
6049 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6050 break;
6051 scope = scope->se_outer;
6052 }
6053
6054 // Jump back to the FOR or WHILE instruction.
6055 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006056 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6057 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006058 return arg;
6059}
6060
6061/*
6062 * compile "break"
6063 */
6064 static char_u *
6065compile_break(char_u *arg, cctx_T *cctx)
6066{
6067 scope_T *scope = cctx->ctx_scope;
6068 endlabel_T **el;
6069
6070 for (;;)
6071 {
6072 if (scope == NULL)
6073 {
6074 emsg(_(e_break));
6075 return NULL;
6076 }
6077 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6078 break;
6079 scope = scope->se_outer;
6080 }
6081
6082 // Jump to the end of the FOR or WHILE loop.
6083 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006084 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006085 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006086 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006087 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6088 return FAIL;
6089
6090 return arg;
6091}
6092
6093/*
6094 * compile "{" start of block
6095 */
6096 static char_u *
6097compile_block(char_u *arg, cctx_T *cctx)
6098{
6099 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6100 return NULL;
6101 return skipwhite(arg + 1);
6102}
6103
6104/*
6105 * compile end of block: drop one scope
6106 */
6107 static void
6108compile_endblock(cctx_T *cctx)
6109{
6110 scope_T *scope = cctx->ctx_scope;
6111
6112 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006113 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006114 vim_free(scope);
6115}
6116
6117/*
6118 * compile "try"
6119 * Creates a new scope for the try-endtry, pointing to the first catch and
6120 * finally.
6121 * Creates another scope for the "try" block itself.
6122 * TRY instruction sets up exception handling at runtime.
6123 *
6124 * "try"
6125 * TRY -> catch1, -> finally push trystack entry
6126 * ... try block
6127 * "throw {exception}"
6128 * EVAL {exception}
6129 * THROW create exception
6130 * ... try block
6131 * " catch {expr}"
6132 * JUMP -> finally
6133 * catch1: PUSH exeception
6134 * EVAL {expr}
6135 * MATCH
6136 * JUMP nomatch -> catch2
6137 * CATCH remove exception
6138 * ... catch block
6139 * " catch"
6140 * JUMP -> finally
6141 * catch2: CATCH remove exception
6142 * ... catch block
6143 * " finally"
6144 * finally:
6145 * ... finally block
6146 * " endtry"
6147 * ENDTRY pop trystack entry, may rethrow
6148 */
6149 static char_u *
6150compile_try(char_u *arg, cctx_T *cctx)
6151{
6152 garray_T *instr = &cctx->ctx_instr;
6153 scope_T *try_scope;
6154 scope_T *scope;
6155
6156 // scope that holds the jumps that go to catch/finally/endtry
6157 try_scope = new_scope(cctx, TRY_SCOPE);
6158 if (try_scope == NULL)
6159 return NULL;
6160
6161 // "catch" is set when the first ":catch" is found.
6162 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006163 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006164 if (generate_instr(cctx, ISN_TRY) == NULL)
6165 return NULL;
6166
6167 // scope for the try block itself
6168 scope = new_scope(cctx, BLOCK_SCOPE);
6169 if (scope == NULL)
6170 return NULL;
6171
6172 return arg;
6173}
6174
6175/*
6176 * compile "catch {expr}"
6177 */
6178 static char_u *
6179compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6180{
6181 scope_T *scope = cctx->ctx_scope;
6182 garray_T *instr = &cctx->ctx_instr;
6183 char_u *p;
6184 isn_T *isn;
6185
6186 // end block scope from :try or :catch
6187 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6188 compile_endblock(cctx);
6189 scope = cctx->ctx_scope;
6190
6191 // Error if not in a :try scope
6192 if (scope == NULL || scope->se_type != TRY_SCOPE)
6193 {
6194 emsg(_(e_catch));
6195 return NULL;
6196 }
6197
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006198 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006199 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006200 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006201 return NULL;
6202 }
6203
6204 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006205 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006206 JUMP_ALWAYS, cctx) == FAIL)
6207 return NULL;
6208
6209 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006210 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006211 if (isn->isn_arg.try.try_catch == 0)
6212 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006213 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006214 {
6215 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006216 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006217 isn->isn_arg.jump.jump_where = instr->ga_len;
6218 }
6219
6220 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006221 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006223 scope->se_u.se_try.ts_caught_all = TRUE;
6224 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006225 }
6226 else
6227 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006228 char_u *end;
6229 char_u *pat;
6230 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006231 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006232 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006233
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006234 // Push v:exception, push {expr} and MATCH
6235 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6236
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006237 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006238 if (*end != *p)
6239 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006240 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006241 vim_free(tofree);
6242 return FAIL;
6243 }
6244 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006245 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006246 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006247 len = (int)(end - tofree);
6248 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006249 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006250 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006251 if (pat == NULL)
6252 return FAIL;
6253 if (generate_PUSHS(cctx, pat) == FAIL)
6254 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006255
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006256 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6257 return NULL;
6258
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006259 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006260 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6261 return NULL;
6262 }
6263
6264 if (generate_instr(cctx, ISN_CATCH) == NULL)
6265 return NULL;
6266
6267 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6268 return NULL;
6269 return p;
6270}
6271
6272 static char_u *
6273compile_finally(char_u *arg, cctx_T *cctx)
6274{
6275 scope_T *scope = cctx->ctx_scope;
6276 garray_T *instr = &cctx->ctx_instr;
6277 isn_T *isn;
6278
6279 // end block scope from :try or :catch
6280 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6281 compile_endblock(cctx);
6282 scope = cctx->ctx_scope;
6283
6284 // Error if not in a :try scope
6285 if (scope == NULL || scope->se_type != TRY_SCOPE)
6286 {
6287 emsg(_(e_finally));
6288 return NULL;
6289 }
6290
6291 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006292 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006293 if (isn->isn_arg.try.try_finally != 0)
6294 {
6295 emsg(_(e_finally_dup));
6296 return NULL;
6297 }
6298
6299 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006300 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006301
Bram Moolenaar585fea72020-04-02 22:33:21 +02006302 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006303 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006304 {
6305 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006306 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006307 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006308 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006309 }
6310
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006311 // TODO: set index in ts_finally_label jumps
6312
6313 return arg;
6314}
6315
6316 static char_u *
6317compile_endtry(char_u *arg, cctx_T *cctx)
6318{
6319 scope_T *scope = cctx->ctx_scope;
6320 garray_T *instr = &cctx->ctx_instr;
6321 isn_T *isn;
6322
6323 // end block scope from :catch or :finally
6324 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6325 compile_endblock(cctx);
6326 scope = cctx->ctx_scope;
6327
6328 // Error if not in a :try scope
6329 if (scope == NULL || scope->se_type != TRY_SCOPE)
6330 {
6331 if (scope == NULL)
6332 emsg(_(e_no_endtry));
6333 else if (scope->se_type == WHILE_SCOPE)
6334 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006335 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006336 emsg(_(e_endfor));
6337 else
6338 emsg(_(e_endif));
6339 return NULL;
6340 }
6341
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006342 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006343 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6344 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006345 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006346 return NULL;
6347 }
6348
6349 // Fill in the "end" label in jumps at the end of the blocks, if not done
6350 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006351 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006352
6353 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006354 if (isn->isn_arg.try.try_catch == 0)
6355 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006356 if (isn->isn_arg.try.try_finally == 0)
6357 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006358
6359 if (scope->se_u.se_try.ts_catch_label != 0)
6360 {
6361 // Last catch without match jumps here
6362 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6363 isn->isn_arg.jump.jump_where = instr->ga_len;
6364 }
6365
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006366 compile_endblock(cctx);
6367
6368 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6369 return NULL;
6370 return arg;
6371}
6372
6373/*
6374 * compile "throw {expr}"
6375 */
6376 static char_u *
6377compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6378{
6379 char_u *p = skipwhite(arg);
6380
Bram Moolenaara5565e42020-05-09 15:44:01 +02006381 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006382 return NULL;
6383 if (may_generate_2STRING(-1, cctx) == FAIL)
6384 return NULL;
6385 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6386 return NULL;
6387
6388 return p;
6389}
6390
6391/*
6392 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006393 * compile "echomsg expr"
6394 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006395 * compile "execute expr"
6396 */
6397 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006398compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006399{
6400 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006401 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006402 int count = 0;
6403
6404 for (;;)
6405 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006406 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006407 return NULL;
6408 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006409 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006410 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006411 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006412 break;
6413 }
6414
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006415 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6416 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6417 else if (cmdidx == CMD_execute)
6418 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6419 else if (cmdidx == CMD_echomsg)
6420 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6421 else
6422 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006423 return p;
6424}
6425
6426/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006427 * :put r
6428 * :put ={expr}
6429 */
6430 static char_u *
6431compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6432{
6433 char_u *line = arg;
6434 linenr_T lnum;
6435 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006436 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006437
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006438 eap->regname = *line;
6439
6440 if (eap->regname == '=')
6441 {
6442 char_u *p = line + 1;
6443
6444 if (compile_expr0(&p, cctx) == FAIL)
6445 return NULL;
6446 line = p;
6447 }
6448 else if (eap->regname != NUL)
6449 ++line;
6450
6451 // TODO: if the range is something like "$" need to evaluate at runtime
6452 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6453 return NULL;
6454 if (eap->addr_count == 0)
6455 lnum = -1;
6456 else
6457 lnum = eap->line2;
6458 if (above)
6459 --lnum;
6460
6461 generate_PUT(cctx, eap->regname, lnum);
6462 return line;
6463}
6464
6465/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006466 * A command that is not compiled, execute with legacy code.
6467 */
6468 static char_u *
6469compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6470{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006471 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006472 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006473 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006474
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006475 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006476 goto theend;
6477
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006478 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006479 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006480 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006481 int usefilter = FALSE;
6482
6483 has_expr = argt & (EX_XFILE | EX_EXPAND);
6484
6485 // If the command can be followed by a bar, find the bar and truncate
6486 // it, so that the following command can be compiled.
6487 // The '|' is overwritten with a NUL, it is put back below.
6488 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6489 && *eap->arg == '!')
6490 // :w !filter or :r !filter or :r! filter
6491 usefilter = TRUE;
6492 if ((argt & EX_TRLBAR) && !usefilter)
6493 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006494 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006495 separate_nextcmd(eap);
6496 if (eap->nextcmd != NULL)
6497 nextcmd = eap->nextcmd;
6498 }
6499 }
6500
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006501 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6502 {
6503 // expand filename in "syntax include [@group] filename"
6504 has_expr = TRUE;
6505 eap->arg = skipwhite(eap->arg + 7);
6506 if (*eap->arg == '@')
6507 eap->arg = skiptowhite(eap->arg);
6508 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006509
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006510 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006511 {
6512 int count = 0;
6513 char_u *start = skipwhite(line);
6514
6515 // :cmd xxx`=expr1`yyy`=expr2`zzz
6516 // PUSHS ":cmd xxx"
6517 // eval expr1
6518 // PUSHS "yyy"
6519 // eval expr2
6520 // PUSHS "zzz"
6521 // EXECCONCAT 5
6522 for (;;)
6523 {
6524 if (p > start)
6525 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006526 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006527 ++count;
6528 }
6529 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006530 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006531 return NULL;
6532 may_generate_2STRING(-1, cctx);
6533 ++count;
6534 p = skipwhite(p);
6535 if (*p != '`')
6536 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006537 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006538 return NULL;
6539 }
6540 start = p + 1;
6541
6542 p = (char_u *)strstr((char *)start, "`=");
6543 if (p == NULL)
6544 {
6545 if (*skipwhite(start) != NUL)
6546 {
6547 generate_PUSHS(cctx, vim_strsave(start));
6548 ++count;
6549 }
6550 break;
6551 }
6552 }
6553 generate_EXECCONCAT(cctx, count);
6554 }
6555 else
6556 generate_EXEC(cctx, line);
6557
6558theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006559 if (*nextcmd != NUL)
6560 {
6561 // the parser expects a pointer to the bar, put it back
6562 --nextcmd;
6563 *nextcmd = '|';
6564 }
6565
6566 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006567}
6568
6569/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006570 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006571 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006572 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006573 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006574add_def_function(ufunc_T *ufunc)
6575{
6576 dfunc_T *dfunc;
6577
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006578 if (def_functions.ga_len == 0)
6579 {
6580 // The first position is not used, so that a zero uf_dfunc_idx means it
6581 // wasn't set.
6582 if (ga_grow(&def_functions, 1) == FAIL)
6583 return FAIL;
6584 ++def_functions.ga_len;
6585 }
6586
Bram Moolenaar09689a02020-05-09 22:50:08 +02006587 // Add the function to "def_functions".
6588 if (ga_grow(&def_functions, 1) == FAIL)
6589 return FAIL;
6590 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6591 CLEAR_POINTER(dfunc);
6592 dfunc->df_idx = def_functions.ga_len;
6593 ufunc->uf_dfunc_idx = dfunc->df_idx;
6594 dfunc->df_ufunc = ufunc;
6595 ++def_functions.ga_len;
6596 return OK;
6597}
6598
6599/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006600 * After ex_function() has collected all the function lines: parse and compile
6601 * the lines into instructions.
6602 * Adds the function to "def_functions".
6603 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6604 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006605 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006606 * This can be used recursively through compile_lambda(), which may reallocate
6607 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006608 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006609 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006610 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006611compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006612{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006613 char_u *line = NULL;
6614 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006615 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006616 cctx_T cctx;
6617 garray_T *instr;
6618 int called_emsg_before = called_emsg;
6619 int ret = FAIL;
6620 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006621 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006622 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006623 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006624
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006625 // When using a function that was compiled before: Free old instructions.
6626 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006627 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006628 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006629 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6630 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006631 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006632 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006633 else
6634 {
6635 if (add_def_function(ufunc) == FAIL)
6636 return FAIL;
6637 new_def_function = TRUE;
6638 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006639
Bram Moolenaar985116a2020-07-12 17:31:09 +02006640 ufunc->uf_def_status = UF_COMPILING;
6641
Bram Moolenaara80faa82020-04-12 19:37:17 +02006642 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006643 cctx.ctx_ufunc = ufunc;
6644 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006645 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006646 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6647 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6648 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6649 cctx.ctx_type_list = &ufunc->uf_type_list;
6650 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6651 instr = &cctx.ctx_instr;
6652
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006653 // Set the context to the function, it may be compiled when called from
6654 // another script. Set the script version to the most modern one.
6655 // The line number will be set in next_line_from_context().
6656 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006657 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6658
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006659 // Make sure error messages are OK.
6660 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6661 if (do_estack_push)
6662 estack_push_ufunc(ufunc, 1);
6663
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006664 if (ufunc->uf_def_args.ga_len > 0)
6665 {
6666 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006667 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006668 int i;
6669 char_u *arg;
6670 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006671 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006672
6673 // Produce instructions for the default values of optional arguments.
6674 // Store the instruction index in uf_def_arg_idx[] so that we know
6675 // where to start when the function is called, depending on the number
6676 // of arguments.
6677 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6678 if (ufunc->uf_def_arg_idx == NULL)
6679 goto erret;
6680 for (i = 0; i < count; ++i)
6681 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006682 garray_T *stack = &cctx.ctx_type_stack;
6683 type_T *val_type;
6684 int arg_idx = first_def_arg + i;
6685
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006686 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6687 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006688 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006689 goto erret;
6690
6691 // If no type specified use the type of the default value.
6692 // Otherwise check that the default value type matches the
6693 // specified type.
6694 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6695 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006696 {
6697 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006698 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006699 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02006700 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
6701 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006702 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006703
6704 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006705 goto erret;
6706 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006707 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006708
6709 if (did_set_arg_type)
6710 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006711 }
6712
6713 /*
6714 * Loop over all the lines of the function and generate instructions.
6715 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006716 for (;;)
6717 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006718 exarg_T ea;
6719 cmdmod_T save_cmdmod;
6720 int starts_with_colon = FALSE;
6721 char_u *cmd;
6722 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006723
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006724 // Bail out on the first error to avoid a flood of errors and report
6725 // the right line number when inside try/catch.
6726 if (emsg_before != called_emsg)
6727 goto erret;
6728
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006729 if (line != NULL && *line == '|')
6730 // the line continues after a '|'
6731 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006732 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006733 && !(*line == '#' && (line == cctx.ctx_line_start
6734 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006735 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006736 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006737 goto erret;
6738 }
6739 else
6740 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006741 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006742 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006743 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006744 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006745 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006746 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006747
Bram Moolenaara80faa82020-04-12 19:37:17 +02006748 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006749 ea.cmdlinep = &line;
6750 ea.cmd = skipwhite(line);
6751
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006752 // Some things can be recognized by the first character.
6753 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006754 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006755 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006756 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006757 if (ea.cmd[1] != '{')
6758 {
6759 line = (char_u *)"";
6760 continue;
6761 }
6762 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006763
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006764 case '}':
6765 {
6766 // "}" ends a block scope
6767 scopetype_T stype = cctx.ctx_scope == NULL
6768 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006769
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006770 if (stype == BLOCK_SCOPE)
6771 {
6772 compile_endblock(&cctx);
6773 line = ea.cmd;
6774 }
6775 else
6776 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006777 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006778 goto erret;
6779 }
6780 if (line != NULL)
6781 line = skipwhite(ea.cmd + 1);
6782 continue;
6783 }
6784
6785 case '{':
6786 // "{" starts a block scope
6787 // "{'a': 1}->func() is something else
6788 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6789 {
6790 line = compile_block(ea.cmd, &cctx);
6791 continue;
6792 }
6793 break;
6794
6795 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006796 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006797 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006798 }
6799
6800 /*
6801 * COMMAND MODIFIERS
6802 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006803 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006804 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6805 {
6806 if (errormsg != NULL)
6807 goto erret;
6808 // empty line or comment
6809 line = (char_u *)"";
6810 continue;
6811 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006812 // TODO: use modifiers in the command
6813 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006814 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006815
6816 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006817 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006818 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006819 {
6820 if (*ea.cmd == '(')
6821 // not for "call()"
6822 ea.cmd = p;
6823 else
6824 ea.cmd = skipwhite(ea.cmd);
6825 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006826
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006827 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006828 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006829 char_u *pskip;
6830
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006831 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006832 // find what follows.
6833 // Skip over "var.member", "var[idx]" and the like.
6834 // Also "&opt = val", "$ENV = val" and "@r = val".
6835 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006836 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006837 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006838 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006839 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006840 char_u *var_end;
6841 int oplen;
6842 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006843
Bram Moolenaar65821722020-08-02 18:58:54 +02006844 if (ea.cmd[0] == '@')
6845 var_end = ea.cmd + 2;
6846 else
6847 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006848 FNE_CHECK_START | FNE_INCL_BR);
6849 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006850 if (oplen > 0)
6851 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006852 size_t len = p - ea.cmd;
6853
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006854 // Recognize an assignment if we recognize the variable
6855 // name:
6856 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006857 // "local = expr" where "local" is a local var.
6858 // "script = expr" where "script" is a script-local var.
6859 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006860 // "&opt = expr"
6861 // "$ENV = expr"
6862 // "@r = expr"
6863 if (*ea.cmd == '&'
6864 || *ea.cmd == '$'
6865 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006866 || ((len) > 2 && ea.cmd[1] == ':')
6867 || lookup_local(ea.cmd, len, &cctx) != NULL
6868 || lookup_arg(ea.cmd, len, NULL, NULL,
6869 NULL, &cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02006870 || lookup_script(ea.cmd, len, FALSE) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006871 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006872 {
6873 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006874 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006875 goto erret;
6876 continue;
6877 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006878 }
6879 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006880
6881 if (*ea.cmd == '[')
6882 {
6883 // [var, var] = expr
6884 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6885 if (line == NULL)
6886 goto erret;
6887 if (line != ea.cmd)
6888 continue;
6889 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006890 }
6891
6892 /*
6893 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006894 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006895 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006896 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006897 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006898 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02006899 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006900 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006901 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006902 if (!starts_with_colon)
6903 {
6904 emsg(_(e_colon_required_before_a_range));
6905 goto erret;
6906 }
6907 if (ends_excmd2(line, ea.cmd))
6908 {
6909 // A range without a command: jump to the line.
6910 // TODO: compile to a more efficient command, possibly
6911 // calling parse_cmd_address().
6912 ea.cmdidx = CMD_SIZE;
6913 line = compile_exec(line, &ea, &cctx);
6914 goto nextline;
6915 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006916 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006917 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006918 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006919 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006920 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006921
6922 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6923 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006924 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006925 {
6926 line += STRLEN(line);
6927 continue;
6928 }
6929
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006930 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006931 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006932 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006933 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006934 iemsg("Command from find_ex_command() not handled");
6935 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006936 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006937 }
6938
Bram Moolenaar3988f642020-08-27 22:43:03 +02006939 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006940 && ea.cmdidx != CMD_elseif
6941 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02006942 && ea.cmdidx != CMD_endif
6943 && ea.cmdidx != CMD_endfor
6944 && ea.cmdidx != CMD_endwhile
6945 && ea.cmdidx != CMD_catch
6946 && ea.cmdidx != CMD_finally
6947 && ea.cmdidx != CMD_endtry)
6948 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02006949 emsg(_(e_unreachable_code_after_return));
6950 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006951 }
6952
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006953 p = skipwhite(p);
6954 if (ea.cmdidx != CMD_SIZE
6955 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
6956 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02006957 if (ea.cmdidx >= 0)
6958 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006959 if ((ea.argt & EX_BANG) && *p == '!')
6960 {
6961 ea.forceit = TRUE;
6962 p = skipwhite(p + 1);
6963 }
6964 }
6965
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006966 switch (ea.cmdidx)
6967 {
6968 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006969 ea.arg = p;
6970 line = compile_nested_function(&ea, &cctx);
6971 break;
6972
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006973 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006974 // TODO: should we allow this, e.g. to declare a global
6975 // function?
6976 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006977 goto erret;
6978
6979 case CMD_return:
6980 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006981 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006982 break;
6983
6984 case CMD_let:
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02006985 if (get_vim_var_nr(VV_DISALLOW_LET))
6986 {
6987 emsg(_(e_cannot_use_let_in_vim9_script));
6988 break;
6989 }
6990 // FALLTHROUGH
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006991 case CMD_var:
6992 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006993 case CMD_const:
6994 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006995 if (line == p)
6996 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006997 break;
6998
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006999 case CMD_unlet:
7000 case CMD_unlockvar:
7001 case CMD_lockvar:
7002 line = compile_unletlock(p, &ea, &cctx);
7003 break;
7004
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007005 case CMD_import:
7006 line = compile_import(p, &cctx);
7007 break;
7008
7009 case CMD_if:
7010 line = compile_if(p, &cctx);
7011 break;
7012 case CMD_elseif:
7013 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007014 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007015 break;
7016 case CMD_else:
7017 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007018 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007019 break;
7020 case CMD_endif:
7021 line = compile_endif(p, &cctx);
7022 break;
7023
7024 case CMD_while:
7025 line = compile_while(p, &cctx);
7026 break;
7027 case CMD_endwhile:
7028 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007029 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007030 break;
7031
7032 case CMD_for:
7033 line = compile_for(p, &cctx);
7034 break;
7035 case CMD_endfor:
7036 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007037 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007038 break;
7039 case CMD_continue:
7040 line = compile_continue(p, &cctx);
7041 break;
7042 case CMD_break:
7043 line = compile_break(p, &cctx);
7044 break;
7045
7046 case CMD_try:
7047 line = compile_try(p, &cctx);
7048 break;
7049 case CMD_catch:
7050 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007051 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007052 break;
7053 case CMD_finally:
7054 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007055 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007056 break;
7057 case CMD_endtry:
7058 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007059 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007060 break;
7061 case CMD_throw:
7062 line = compile_throw(p, &cctx);
7063 break;
7064
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007065 case CMD_eval:
7066 if (compile_expr0(&p, &cctx) == FAIL)
7067 goto erret;
7068
Bram Moolenaar3988f642020-08-27 22:43:03 +02007069 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007070 generate_instr_drop(&cctx, ISN_DROP, 1);
7071
7072 line = skipwhite(p);
7073 break;
7074
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007075 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007076 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007077 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007078 case CMD_echomsg:
7079 case CMD_echoerr:
7080 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007081 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007082
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007083 case CMD_put:
7084 ea.cmd = cmd;
7085 line = compile_put(p, &ea, &cctx);
7086 break;
7087
Bram Moolenaar3988f642020-08-27 22:43:03 +02007088 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007089
Bram Moolenaarae616492020-07-28 20:07:27 +02007090 case CMD_append:
7091 case CMD_change:
7092 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007093 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007094 case CMD_xit:
7095 not_in_vim9(&ea);
7096 goto erret;
7097
Bram Moolenaar002262f2020-07-08 17:47:57 +02007098 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007099 if (cctx.ctx_skip != SKIP_YES)
7100 {
7101 semsg(_(e_invalid_command_str), ea.cmd);
7102 goto erret;
7103 }
7104 // We don't check for a next command here.
7105 line = (char_u *)"";
7106 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007108 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007109 if (cctx.ctx_skip == SKIP_YES)
7110 {
7111 // We don't check for a next command here.
7112 line = (char_u *)"";
7113 }
7114 else
7115 {
7116 // Not recognized, execute with do_cmdline_cmd().
7117 ea.arg = p;
7118 line = compile_exec(line, &ea, &cctx);
7119 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007120 break;
7121 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007122nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007123 if (line == NULL)
7124 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007125 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007126
7127 if (cctx.ctx_type_stack.ga_len < 0)
7128 {
7129 iemsg("Type stack underflow");
7130 goto erret;
7131 }
7132 }
7133
7134 if (cctx.ctx_scope != NULL)
7135 {
7136 if (cctx.ctx_scope->se_type == IF_SCOPE)
7137 emsg(_(e_endif));
7138 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7139 emsg(_(e_endwhile));
7140 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7141 emsg(_(e_endfor));
7142 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007143 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007144 goto erret;
7145 }
7146
Bram Moolenaarefd88552020-06-18 20:50:10 +02007147 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007148 {
7149 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7150 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007151 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007152 goto erret;
7153 }
7154
7155 // Return zero if there is no return at the end.
7156 generate_PUSHNR(&cctx, 0);
7157 generate_instr(&cctx, ISN_RETURN);
7158 }
7159
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007160 {
7161 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7162 + ufunc->uf_dfunc_idx;
7163 dfunc->df_deleted = FALSE;
7164 dfunc->df_instr = instr->ga_data;
7165 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007166 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007167 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007168 if (cctx.ctx_outer_used)
7169 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007170 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007171 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007172
7173 ret = OK;
7174
7175erret:
7176 if (ret == FAIL)
7177 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007178 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007179 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7180 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007181
7182 for (idx = 0; idx < instr->ga_len; ++idx)
7183 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007184 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007185
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007186 // If using the last entry in the table and it was added above, we
7187 // might as well remove it.
7188 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007189 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007190 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007191 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007192 ufunc->uf_dfunc_idx = 0;
7193 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007194 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007195
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007196 while (cctx.ctx_scope != NULL)
7197 drop_scope(&cctx);
7198
Bram Moolenaar20431c92020-03-20 18:39:46 +01007199 // Don't execute this function body.
7200 ga_clear_strings(&ufunc->uf_lines);
7201
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007202 if (errormsg != NULL)
7203 emsg(errormsg);
7204 else if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007205 emsg(_(e_compile_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007206 }
7207
7208 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007209 if (do_estack_push)
7210 estack_pop();
7211
Bram Moolenaar20431c92020-03-20 18:39:46 +01007212 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007213 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007214 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007215 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007216}
7217
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007218 void
7219set_function_type(ufunc_T *ufunc)
7220{
7221 int varargs = ufunc->uf_va_name != NULL;
7222 int argcount = ufunc->uf_args.ga_len;
7223
7224 // Create a type for the function, with the return type and any
7225 // argument types.
7226 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7227 // The type is included in "tt_args".
7228 if (argcount > 0 || varargs)
7229 {
7230 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7231 argcount, &ufunc->uf_type_list);
7232 // Add argument types to the function type.
7233 if (func_type_add_arg_types(ufunc->uf_func_type,
7234 argcount + varargs,
7235 &ufunc->uf_type_list) == FAIL)
7236 return;
7237 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7238 ufunc->uf_func_type->tt_min_argcount =
7239 argcount - ufunc->uf_def_args.ga_len;
7240 if (ufunc->uf_arg_types == NULL)
7241 {
7242 int i;
7243
7244 // lambda does not have argument types.
7245 for (i = 0; i < argcount; ++i)
7246 ufunc->uf_func_type->tt_args[i] = &t_any;
7247 }
7248 else
7249 mch_memmove(ufunc->uf_func_type->tt_args,
7250 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7251 if (varargs)
7252 {
7253 ufunc->uf_func_type->tt_args[argcount] =
7254 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7255 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7256 }
7257 }
7258 else
7259 // No arguments, can use a predefined type.
7260 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7261 argcount, &ufunc->uf_type_list);
7262}
7263
7264
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007265/*
7266 * Delete an instruction, free what it contains.
7267 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007268 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007269delete_instr(isn_T *isn)
7270{
7271 switch (isn->isn_type)
7272 {
7273 case ISN_EXEC:
7274 case ISN_LOADENV:
7275 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007276 case ISN_LOADB:
7277 case ISN_LOADW:
7278 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007279 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007280 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007281 case ISN_PUSHEXC:
7282 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007283 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007284 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007285 case ISN_STOREB:
7286 case ISN_STOREW:
7287 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007288 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007289 vim_free(isn->isn_arg.string);
7290 break;
7291
7292 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007293 case ISN_STORES:
7294 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007295 break;
7296
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007297 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007298 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007299 vim_free(isn->isn_arg.unlet.ul_name);
7300 break;
7301
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007302 case ISN_STOREOPT:
7303 vim_free(isn->isn_arg.storeopt.so_name);
7304 break;
7305
7306 case ISN_PUSHBLOB: // push blob isn_arg.blob
7307 blob_unref(isn->isn_arg.blob);
7308 break;
7309
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007310 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007311#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007312 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007313#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007314 break;
7315
7316 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007317#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007318 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007319#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007320 break;
7321
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007322 case ISN_UCALL:
7323 vim_free(isn->isn_arg.ufunc.cuf_name);
7324 break;
7325
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007326 case ISN_FUNCREF:
7327 {
7328 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7329 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007330
7331 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7332 func_ptr_unref(dfunc->df_ufunc);
7333 }
7334 break;
7335
7336 case ISN_DCALL:
7337 {
7338 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7339 + isn->isn_arg.dfunc.cdf_idx;
7340
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007341 if (dfunc->df_ufunc != NULL
7342 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007343 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007344 }
7345 break;
7346
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007347 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007348 {
7349 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7350 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7351
7352 if (ufunc != NULL)
7353 {
7354 // Clear uf_dfunc_idx so that the function is deleted.
7355 clear_def_function(ufunc);
7356 ufunc->uf_dfunc_idx = 0;
7357 func_ptr_unref(ufunc);
7358 }
7359
7360 vim_free(lambda);
7361 vim_free(isn->isn_arg.newfunc.nf_global);
7362 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007363 break;
7364
Bram Moolenaar5e654232020-09-16 15:22:00 +02007365 case ISN_CHECKTYPE:
7366 free_type(isn->isn_arg.type.ct_type);
7367 break;
7368
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007369 case ISN_2BOOL:
7370 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007371 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007372 case ISN_ADDBLOB:
7373 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007374 case ISN_ANYINDEX:
7375 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007376 case ISN_BCALL:
7377 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007378 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007379 case ISN_CHECKNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007380 case ISN_COMPAREANY:
7381 case ISN_COMPAREBLOB:
7382 case ISN_COMPAREBOOL:
7383 case ISN_COMPAREDICT:
7384 case ISN_COMPAREFLOAT:
7385 case ISN_COMPAREFUNC:
7386 case ISN_COMPARELIST:
7387 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007388 case ISN_COMPARESPECIAL:
7389 case ISN_COMPARESTRING:
7390 case ISN_CONCAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007391 case ISN_DROP:
7392 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007393 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007394 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007395 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007396 case ISN_EXECCONCAT:
7397 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007398 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007399 case ISN_GETITEM:
7400 case ISN_JUMP:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007401 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007402 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007403 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007404 case ISN_LOADBDICT:
7405 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007406 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007407 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007408 case ISN_LOADSCRIPT:
7409 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007410 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007411 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007412 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007413 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007414 case ISN_NEGATENR:
7415 case ISN_NEWDICT:
7416 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007417 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007418 case ISN_OPFLOAT:
7419 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007420 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007421 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007422 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007423 case ISN_PUSHF:
7424 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007425 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007426 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007427 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007428 case ISN_SHUFFLE:
7429 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007430 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007431 case ISN_STOREDICT:
7432 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007433 case ISN_STORENR:
7434 case ISN_STOREOUTER:
7435 case ISN_STOREREG:
7436 case ISN_STORESCRIPT:
7437 case ISN_STOREV:
7438 case ISN_STRINDEX:
7439 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007440 case ISN_THROW:
7441 case ISN_TRY:
7442 // nothing allocated
7443 break;
7444 }
7445}
7446
7447/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007448 * Free all instructions for "dfunc".
7449 */
7450 static void
7451delete_def_function_contents(dfunc_T *dfunc)
7452{
7453 int idx;
7454
7455 ga_clear(&dfunc->df_def_args_isn);
7456
7457 if (dfunc->df_instr != NULL)
7458 {
7459 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7460 delete_instr(dfunc->df_instr + idx);
7461 VIM_CLEAR(dfunc->df_instr);
7462 }
7463
7464 dfunc->df_deleted = TRUE;
7465}
7466
7467/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007468 * When a user function is deleted, clear the contents of any associated def
7469 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007470 */
7471 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007472clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007473{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007474 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007475 {
7476 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7477 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007478
Bram Moolenaar20431c92020-03-20 18:39:46 +01007479 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007480 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007481 }
7482}
7483
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007484/*
7485 * Used when a user function is about to be deleted: remove the pointer to it.
7486 * The entry in def_functions is then unused.
7487 */
7488 void
7489unlink_def_function(ufunc_T *ufunc)
7490{
7491 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7492
7493 dfunc->df_ufunc = NULL;
7494}
7495
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007496#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007497/*
7498 * Free all functions defined with ":def".
7499 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007500 void
7501free_def_functions(void)
7502{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007503 int idx;
7504
7505 for (idx = 0; idx < def_functions.ga_len; ++idx)
7506 {
7507 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7508
7509 delete_def_function_contents(dfunc);
7510 }
7511
7512 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007513}
7514#endif
7515
7516
7517#endif // FEAT_EVAL