blob: e4f3a906e3942f451fed4d12b8384d5665589a8a [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 Moolenaar8a7d6542020-01-26 15:56:19 +01002552 break;
2553 ++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)
3336 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003337 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003338 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003339 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340 while (eval_isnamec(*p))
3341 MB_PTR_ADV(p);
3342 if (p == *arg)
3343 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003344 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003345 return FAIL;
3346 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003347 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348 return FAIL;
3349 *arg = p;
3350 }
3351 else
3352 break;
3353 }
3354
3355 // TODO - see handle_subscript():
3356 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3357 // Don't do this when "Func" is already a partial that was bound
3358 // explicitly (pt_auto is FALSE).
3359
3360 return OK;
3361}
3362
3363/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003364 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3365 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003366 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003367 * If the value is a constant "ppconst->pp_ret" will be set.
3368 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003369 *
3370 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371 */
3372
3373/*
3374 * number number constant
3375 * 0zFFFFFFFF Blob constant
3376 * "string" string constant
3377 * 'string' literal string constant
3378 * &option-name option value
3379 * @r register contents
3380 * identifier variable value
3381 * function() function call
3382 * $VAR environment variable
3383 * (expression) nested expression
3384 * [expr, expr] List
3385 * {key: val, key: val} Dictionary
3386 * #{key: val, key: val} Dictionary with literal keys
3387 *
3388 * Also handle:
3389 * ! in front logical NOT
3390 * - in front unary minus
3391 * + in front unary plus (ignored)
3392 * trailing (arg) funcref/partial call
3393 * trailing [] subscript in String or List
3394 * trailing .name entry in Dictionary
3395 * trailing ->name() method call
3396 */
3397 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003398compile_expr7(
3399 char_u **arg,
3400 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003401 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403 char_u *start_leader, *end_leader;
3404 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003405 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003406 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407
3408 /*
3409 * Skip '!', '-' and '+' characters. They are handled later.
3410 */
3411 start_leader = *arg;
3412 while (**arg == '!' || **arg == '-' || **arg == '+')
3413 *arg = skipwhite(*arg + 1);
3414 end_leader = *arg;
3415
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003416 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417 switch (**arg)
3418 {
3419 /*
3420 * Number constant.
3421 */
3422 case '0': // also for blob starting with 0z
3423 case '1':
3424 case '2':
3425 case '3':
3426 case '4':
3427 case '5':
3428 case '6':
3429 case '7':
3430 case '8':
3431 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003432 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003433 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003434 // Apply "-" and "+" just before the number now, right to
3435 // left. Matters especially when "->" follows. Stops at
3436 // '!'.
3437 if (apply_leader(rettv, TRUE,
3438 start_leader, &end_leader) == FAIL)
3439 {
3440 clear_tv(rettv);
3441 return FAIL;
3442 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003443 break;
3444
3445 /*
3446 * String constant: "string".
3447 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003448 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 return FAIL;
3450 break;
3451
3452 /*
3453 * Literal string constant: 'str''ing'.
3454 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003455 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 return FAIL;
3457 break;
3458
3459 /*
3460 * Constant Vim variable.
3461 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003462 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 ret = NOTDONE;
3464 break;
3465
3466 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003467 * "true" constant
3468 */
3469 case 't': if (STRNCMP(*arg, "true", 4) == 0
3470 && !eval_isnamec((*arg)[4]))
3471 {
3472 *arg += 4;
3473 rettv->v_type = VAR_BOOL;
3474 rettv->vval.v_number = VVAL_TRUE;
3475 }
3476 else
3477 ret = NOTDONE;
3478 break;
3479
3480 /*
3481 * "false" constant
3482 */
3483 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3484 && !eval_isnamec((*arg)[5]))
3485 {
3486 *arg += 5;
3487 rettv->v_type = VAR_BOOL;
3488 rettv->vval.v_number = VVAL_FALSE;
3489 }
3490 else
3491 ret = NOTDONE;
3492 break;
3493
3494 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495 * List: [expr, expr]
3496 */
3497 case '[': ret = compile_list(arg, cctx);
3498 break;
3499
3500 /*
3501 * Dictionary: #{key: val, key: val}
3502 */
3503 case '#': if ((*arg)[1] == '{')
3504 {
3505 ++*arg;
3506 ret = compile_dict(arg, cctx, TRUE);
3507 }
3508 else
3509 ret = NOTDONE;
3510 break;
3511
3512 /*
3513 * Lambda: {arg, arg -> expr}
3514 * Dictionary: {'key': val, 'key': val}
3515 */
3516 case '{': {
3517 char_u *start = skipwhite(*arg + 1);
3518
3519 // Find out what comes after the arguments.
3520 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003521 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003522 if (ret != FAIL && *start == '>')
3523 ret = compile_lambda(arg, cctx);
3524 else
3525 ret = compile_dict(arg, cctx, FALSE);
3526 }
3527 break;
3528
3529 /*
3530 * Option value: &name
3531 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003532 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3533 return FAIL;
3534 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 break;
3536
3537 /*
3538 * Environment variable: $VAR.
3539 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003540 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3541 return FAIL;
3542 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543 break;
3544
3545 /*
3546 * Register contents: @r.
3547 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003548 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3549 return FAIL;
3550 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551 break;
3552 /*
3553 * nested expression: (expression).
3554 */
3555 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003556
3557 // recursive!
3558 if (ppconst->pp_used <= PPSIZE - 10)
3559 {
3560 ret = compile_expr1(arg, cctx, ppconst);
3561 }
3562 else
3563 {
3564 // Not enough space in ppconst, flush constants.
3565 if (generate_ppconst(cctx, ppconst) == FAIL)
3566 return FAIL;
3567 ret = compile_expr0(arg, cctx);
3568 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 *arg = skipwhite(*arg);
3570 if (**arg == ')')
3571 ++*arg;
3572 else if (ret == OK)
3573 {
3574 emsg(_(e_missing_close));
3575 ret = FAIL;
3576 }
3577 break;
3578
3579 default: ret = NOTDONE;
3580 break;
3581 }
3582 if (ret == FAIL)
3583 return FAIL;
3584
Bram Moolenaar1c747212020-05-09 18:28:34 +02003585 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003586 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003587 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003588 clear_tv(rettv);
3589 else
3590 // A constant expression can possibly be handled compile time,
3591 // return the value instead of generating code.
3592 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593 }
3594 else if (ret == NOTDONE)
3595 {
3596 char_u *p;
3597 int r;
3598
3599 if (!eval_isnamec1(**arg))
3600 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003601 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003602 return FAIL;
3603 }
3604
3605 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003606 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003608 {
3609 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3610 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003612 {
3613 if (generate_ppconst(cctx, ppconst) == FAIL)
3614 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003615 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003616 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003617 if (r == FAIL)
3618 return FAIL;
3619 }
3620
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003621 // Handle following "[]", ".member", etc.
3622 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003623 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003624 ppconst) == FAIL)
3625 return FAIL;
3626 if (ppconst->pp_used > 0)
3627 {
3628 // apply the '!', '-' and '+' before the constant
3629 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003630 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003631 return FAIL;
3632 return OK;
3633 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003634 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003636 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637}
3638
3639/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003640 * Give the "white on both sides" error, taking the operator from "p[len]".
3641 */
3642 void
3643error_white_both(char_u *op, int len)
3644{
3645 char_u buf[10];
3646
3647 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003648 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003649}
3650
3651/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003652 * <type>expr7: runtime type check / conversion
3653 */
3654 static int
3655compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3656{
3657 type_T *want_type = NULL;
3658
3659 // Recognize <type>
3660 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3661 {
3662 int called_emsg_before = called_emsg;
3663
3664 ++*arg;
3665 want_type = parse_type(arg, cctx->ctx_type_list);
3666 if (called_emsg != called_emsg_before)
3667 return FAIL;
3668
3669 if (**arg != '>')
3670 {
3671 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003672 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003673 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003674 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003675 return FAIL;
3676 }
3677 ++*arg;
3678 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3679 return FAIL;
3680 }
3681
3682 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3683 return FAIL;
3684
3685 if (want_type != NULL)
3686 {
3687 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003688 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003689
Bram Moolenaard1103582020-08-14 22:44:25 +02003690 generate_ppconst(cctx, ppconst);
3691 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003692 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003693 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003694 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3695 return FAIL;
3696 }
3697 }
3698
3699 return OK;
3700}
3701
3702/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703 * * number multiplication
3704 * / number division
3705 * % number modulo
3706 */
3707 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003708compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003709{
3710 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003711 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003712 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003714 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003715 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003716 return FAIL;
3717
3718 /*
3719 * Repeat computing, until no "*", "/" or "%" is following.
3720 */
3721 for (;;)
3722 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003723 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724 if (*op != '*' && *op != '/' && *op != '%')
3725 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003726 if (next != NULL)
3727 {
3728 *arg = next_line_from_context(cctx, TRUE);
3729 op = skipwhite(*arg);
3730 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003731
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003732 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003734 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003735 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 }
3737 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003738 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003739 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003741 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003742 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003743 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003744
3745 if (ppconst->pp_used == ppconst_used + 2
3746 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3747 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003748 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003749 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3750 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003751 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003752
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003753 // both are numbers: compute the result
3754 switch (*op)
3755 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003756 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003757 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003758 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003759 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003760 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003761 break;
3762 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003763 tv1->vval.v_number = res;
3764 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003765 }
3766 else
3767 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003768 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003769 generate_two_op(cctx, op);
3770 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771 }
3772
3773 return OK;
3774}
3775
3776/*
3777 * + number addition
3778 * - number subtraction
3779 * .. string concatenation
3780 */
3781 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003782compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783{
3784 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003785 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003787 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788
3789 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003790 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 return FAIL;
3792
3793 /*
3794 * Repeat computing, until no "+", "-" or ".." is following.
3795 */
3796 for (;;)
3797 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003798 op = may_peek_next_line(cctx, *arg, &next);
3799 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 break;
3801 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003802 if (next != NULL)
3803 {
3804 *arg = next_line_from_context(cctx, TRUE);
3805 op = skipwhite(*arg);
3806 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003808 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003810 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003811 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003812 }
3813
3814 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003815 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003816 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003818 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003819 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 return FAIL;
3821
Bram Moolenaara5565e42020-05-09 15:44:01 +02003822 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003823 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003824 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3825 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3826 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3827 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003829 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3830 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003831
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003832 // concat/subtract/add constant numbers
3833 if (*op == '+')
3834 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3835 else if (*op == '-')
3836 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3837 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003838 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003839 // concatenate constant strings
3840 char_u *s1 = tv1->vval.v_string;
3841 char_u *s2 = tv2->vval.v_string;
3842 size_t len1 = STRLEN(s1);
3843
3844 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3845 if (tv1->vval.v_string == NULL)
3846 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003847 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003848 return FAIL;
3849 }
3850 mch_memmove(tv1->vval.v_string, s1, len1);
3851 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003852 vim_free(s1);
3853 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003854 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003855 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 }
3857 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003858 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003859 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003860 if (*op == '.')
3861 {
3862 if (may_generate_2STRING(-2, cctx) == FAIL
3863 || may_generate_2STRING(-1, cctx) == FAIL)
3864 return FAIL;
3865 generate_instr_drop(cctx, ISN_CONCAT, 1);
3866 }
3867 else
3868 generate_two_op(cctx, op);
3869 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 }
3871
3872 return OK;
3873}
3874
3875/*
3876 * expr5a == expr5b
3877 * expr5a =~ expr5b
3878 * expr5a != expr5b
3879 * expr5a !~ expr5b
3880 * expr5a > expr5b
3881 * expr5a >= expr5b
3882 * expr5a < expr5b
3883 * expr5a <= expr5b
3884 * expr5a is expr5b
3885 * expr5a isnot expr5b
3886 *
3887 * Produces instructions:
3888 * EVAL expr5a Push result of "expr5a"
3889 * EVAL expr5b Push result of "expr5b"
3890 * COMPARE one of the compare instructions
3891 */
3892 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003893compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894{
3895 exptype_T type = EXPR_UNKNOWN;
3896 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003897 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003900 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901
3902 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003903 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904 return FAIL;
3905
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003906 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003907 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908
3909 /*
3910 * If there is a comparative operator, use it.
3911 */
3912 if (type != EXPR_UNKNOWN)
3913 {
3914 int ic = FALSE; // Default: do not ignore case
3915
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003916 if (next != NULL)
3917 {
3918 *arg = next_line_from_context(cctx, TRUE);
3919 p = skipwhite(*arg);
3920 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 if (type_is && (p[len] == '?' || p[len] == '#'))
3922 {
3923 semsg(_(e_invexpr2), *arg);
3924 return FAIL;
3925 }
3926 // extra question mark appended: ignore case
3927 if (p[len] == '?')
3928 {
3929 ic = TRUE;
3930 ++len;
3931 }
3932 // extra '#' appended: match case (ignored)
3933 else if (p[len] == '#')
3934 ++len;
3935 // nothing appended: match case
3936
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003937 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003939 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003940 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941 }
3942
3943 // get the second variable
3944 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003945 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003946 return FAIL;
3947
Bram Moolenaara5565e42020-05-09 15:44:01 +02003948 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 return FAIL;
3950
Bram Moolenaara5565e42020-05-09 15:44:01 +02003951 if (ppconst->pp_used == ppconst_used + 2)
3952 {
3953 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3954 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3955 int ret;
3956
3957 // Both sides are a constant, compute the result now.
3958 // First check for a valid combination of types, this is more
3959 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003960 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003961 ret = FAIL;
3962 else
3963 {
3964 ret = typval_compare(tv1, tv2, type, ic);
3965 tv1->v_type = VAR_BOOL;
3966 tv1->vval.v_number = tv1->vval.v_number
3967 ? VVAL_TRUE : VVAL_FALSE;
3968 clear_tv(tv2);
3969 --ppconst->pp_used;
3970 }
3971 return ret;
3972 }
3973
3974 generate_ppconst(cctx, ppconst);
3975 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976 }
3977
3978 return OK;
3979}
3980
Bram Moolenaar7f141552020-05-09 17:35:53 +02003981static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3982
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003983/*
3984 * Compile || or &&.
3985 */
3986 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003987compile_and_or(
3988 char_u **arg,
3989 cctx_T *cctx,
3990 char *op,
3991 ppconst_T *ppconst,
3992 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003993{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003994 char_u *next;
3995 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996 int opchar = *op;
3997
3998 if (p[0] == opchar && p[1] == opchar)
3999 {
4000 garray_T *instr = &cctx->ctx_instr;
4001 garray_T end_ga;
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004002 garray_T *stack = &cctx->ctx_type_stack;
4003 type_T **typep;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004
4005 /*
4006 * Repeat until there is no following "||" or "&&"
4007 */
4008 ga_init2(&end_ga, sizeof(int), 10);
4009 while (p[0] == opchar && p[1] == opchar)
4010 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004011 if (next != NULL)
4012 {
4013 *arg = next_line_from_context(cctx, TRUE);
4014 p = skipwhite(*arg);
4015 }
4016
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004017 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4018 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004019 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004020 return FAIL;
4021 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022
Bram Moolenaara5565e42020-05-09 15:44:01 +02004023 // TODO: use ppconst if the value is a constant
4024 generate_ppconst(cctx, ppconst);
4025
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 if (ga_grow(&end_ga, 1) == FAIL)
4027 {
4028 ga_clear(&end_ga);
4029 return FAIL;
4030 }
4031 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4032 ++end_ga.ga_len;
4033 generate_JUMP(cctx, opchar == '|'
4034 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4035
4036 // eval the next expression
4037 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004038 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004039 return FAIL;
4040
Bram Moolenaara5565e42020-05-09 15:44:01 +02004041 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4042 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043 {
4044 ga_clear(&end_ga);
4045 return FAIL;
4046 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004047
4048 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004050 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051
4052 // Fill in the end label in all jumps.
4053 while (end_ga.ga_len > 0)
4054 {
4055 isn_T *isn;
4056
4057 --end_ga.ga_len;
4058 isn = ((isn_T *)instr->ga_data)
4059 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4060 isn->isn_arg.jump.jump_where = instr->ga_len;
4061 }
4062 ga_clear(&end_ga);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004063
4064 // The resulting type can be used as a bool.
4065 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4066 if (*typep != &t_bool)
4067 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004068 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004069
4070 if (type != NULL)
4071 {
4072 *type = **typep;
4073 type->tt_flags |= TTFLAG_BOOL_OK;
4074 *typep = type;
4075 }
4076 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077 }
4078
4079 return OK;
4080}
4081
4082/*
4083 * expr4a && expr4a && expr4a logical AND
4084 *
4085 * Produces instructions:
4086 * EVAL expr4a Push result of "expr4a"
4087 * JUMP_AND_KEEP_IF_FALSE end
4088 * EVAL expr4b Push result of "expr4b"
4089 * JUMP_AND_KEEP_IF_FALSE end
4090 * EVAL expr4c Push result of "expr4c"
4091 * end:
4092 */
4093 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004094compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004096 int ppconst_used = ppconst->pp_used;
4097
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004099 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004100 return FAIL;
4101
4102 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004103 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104}
4105
4106/*
4107 * expr3a || expr3b || expr3c logical OR
4108 *
4109 * Produces instructions:
4110 * EVAL expr3a Push result of "expr3a"
4111 * JUMP_AND_KEEP_IF_TRUE end
4112 * EVAL expr3b Push result of "expr3b"
4113 * JUMP_AND_KEEP_IF_TRUE end
4114 * EVAL expr3c Push result of "expr3c"
4115 * end:
4116 */
4117 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004118compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004120 int ppconst_used = ppconst->pp_used;
4121
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004123 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124 return FAIL;
4125
4126 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004127 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004128}
4129
4130/*
4131 * Toplevel expression: expr2 ? expr1a : expr1b
4132 *
4133 * Produces instructions:
4134 * EVAL expr2 Push result of "expr"
4135 * JUMP_IF_FALSE alt jump if false
4136 * EVAL expr1a
4137 * JUMP_ALWAYS end
4138 * alt: EVAL expr1b
4139 * end:
4140 */
4141 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004142compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004143{
4144 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004145 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004146 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147
Bram Moolenaar3988f642020-08-27 22:43:03 +02004148 // Ignore all kinds of errors when not producing code.
4149 if (cctx->ctx_skip == SKIP_YES)
4150 {
4151 skip_expr(arg);
4152 return OK;
4153 }
4154
Bram Moolenaar61a89812020-05-07 16:58:17 +02004155 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004156 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 return FAIL;
4158
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004159 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 if (*p == '?')
4161 {
4162 garray_T *instr = &cctx->ctx_instr;
4163 garray_T *stack = &cctx->ctx_type_stack;
4164 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004165 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004166 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004167 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004169 int has_const_expr = FALSE;
4170 int const_value = FALSE;
4171 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004172
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004173 if (next != NULL)
4174 {
4175 *arg = next_line_from_context(cctx, TRUE);
4176 p = skipwhite(*arg);
4177 }
4178
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004179 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4180 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004181 semsg(_(e_white_space_required_before_and_after_str), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004182 return FAIL;
4183 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184
Bram Moolenaara5565e42020-05-09 15:44:01 +02004185 if (ppconst->pp_used == ppconst_used + 1)
4186 {
4187 // the condition is a constant, we know whether the ? or the :
4188 // expression is to be evaluated.
4189 has_const_expr = TRUE;
4190 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4191 clear_tv(&ppconst->pp_tv[ppconst_used]);
4192 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004193 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4194 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004195 }
4196 else
4197 {
4198 generate_ppconst(cctx, ppconst);
4199 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4200 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201
4202 // evaluate the second expression; any type is accepted
4203 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004204 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004205 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004206 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004207 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004208
Bram Moolenaara5565e42020-05-09 15:44:01 +02004209 if (!has_const_expr)
4210 {
4211 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212
Bram Moolenaara5565e42020-05-09 15:44:01 +02004213 // remember the type and drop it
4214 --stack->ga_len;
4215 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216
Bram Moolenaara5565e42020-05-09 15:44:01 +02004217 end_idx = instr->ga_len;
4218 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4219
4220 // jump here from JUMP_IF_FALSE
4221 isn = ((isn_T *)instr->ga_data) + alt_idx;
4222 isn->isn_arg.jump.jump_where = instr->ga_len;
4223 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224
4225 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004226 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227 if (*p != ':')
4228 {
4229 emsg(_(e_missing_colon));
4230 return FAIL;
4231 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004232 if (next != NULL)
4233 {
4234 *arg = next_line_from_context(cctx, TRUE);
4235 p = skipwhite(*arg);
4236 }
4237
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004238 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4239 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004240 semsg(_(e_white_space_required_before_and_after_str), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004241 return FAIL;
4242 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004243
4244 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004245 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004246 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4247 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004249 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004250 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004251 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004252 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004253
Bram Moolenaara5565e42020-05-09 15:44:01 +02004254 if (!has_const_expr)
4255 {
4256 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004257
Bram Moolenaara5565e42020-05-09 15:44:01 +02004258 // If the types differ, the result has a more generic type.
4259 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4260 common_type(type1, type2, &type2, cctx->ctx_type_list);
4261
4262 // jump here from JUMP_ALWAYS
4263 isn = ((isn_T *)instr->ga_data) + end_idx;
4264 isn->isn_arg.jump.jump_where = instr->ga_len;
4265 }
4266
4267 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004268 }
4269 return OK;
4270}
4271
4272/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004273 * Toplevel expression.
4274 */
4275 static int
4276compile_expr0(char_u **arg, cctx_T *cctx)
4277{
4278 ppconst_T ppconst;
4279
4280 CLEAR_FIELD(ppconst);
4281 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4282 {
4283 clear_ppconst(&ppconst);
4284 return FAIL;
4285 }
4286 if (generate_ppconst(cctx, &ppconst) == FAIL)
4287 return FAIL;
4288 return OK;
4289}
4290
4291/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004292 * compile "return [expr]"
4293 */
4294 static char_u *
4295compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4296{
4297 char_u *p = arg;
4298 garray_T *stack = &cctx->ctx_type_stack;
4299 type_T *stack_type;
4300
4301 if (*p != NUL && *p != '|' && *p != '\n')
4302 {
4303 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004304 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 return NULL;
4306
4307 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4308 if (set_return_type)
4309 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004310 else
4311 {
4312 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4313 && stack_type->tt_type != VAR_VOID
4314 && stack_type->tt_type != VAR_UNKNOWN)
4315 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004316 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004317 return NULL;
4318 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004319 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4320 cctx, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004321 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004322 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323 }
4324 else
4325 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004326 // "set_return_type" cannot be TRUE, only used for a lambda which
4327 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004328 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4329 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004331 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 return NULL;
4333 }
4334
4335 // No argument, return zero.
4336 generate_PUSHNR(cctx, 0);
4337 }
4338
4339 if (generate_instr(cctx, ISN_RETURN) == NULL)
4340 return NULL;
4341
4342 // "return val | endif" is possible
4343 return skipwhite(p);
4344}
4345
4346/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004347 * Get a line from the compilation context, compatible with exarg_T getline().
4348 * Return a pointer to the line in allocated memory.
4349 * Return NULL for end-of-file or some error.
4350 */
4351 static char_u *
4352exarg_getline(
4353 int c UNUSED,
4354 void *cookie,
4355 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004356 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004357{
4358 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004359 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004360
Bram Moolenaar66250c92020-08-20 15:02:42 +02004361 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004362 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004363 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004364 return NULL;
4365 ++cctx->ctx_lnum;
4366 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4367 // Comment lines result in NULL pointers, skip them.
4368 if (p != NULL)
4369 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004370 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004371}
4372
4373/*
4374 * Compile a nested :def command.
4375 */
4376 static char_u *
4377compile_nested_function(exarg_T *eap, cctx_T *cctx)
4378{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004379 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004380 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004381 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004382 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004383 lvar_T *lvar;
4384 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004385 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004386
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004387 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004388 {
4389 emsg(_(e_cannot_use_bang_with_nested_def));
4390 return NULL;
4391 }
4392
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004393 // Only g:Func() can use a namespace.
4394 if (name_start[1] == ':' && !is_global)
4395 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004396 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004397 return NULL;
4398 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004399 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4400 return NULL;
4401
Bram Moolenaar04b12692020-05-04 23:24:44 +02004402 eap->arg = name_end;
4403 eap->getline = exarg_getline;
4404 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004405 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004406 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004407 lambda_name = get_lambda_name();
4408 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004409
Bram Moolenaar822ba242020-05-24 23:00:18 +02004410 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004411 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004412 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004413 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004414 {
4415 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004416 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004417 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004418
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004419 if (is_global)
4420 {
4421 char_u *func_name = vim_strnsave(name_start + 2,
4422 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004423
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004424 if (func_name == NULL)
4425 r = FAIL;
4426 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004427 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004428 }
4429 else
4430 {
4431 // Define a local variable for the function reference.
4432 lvar = reserve_local(cctx, name_start, name_end - name_start,
4433 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004434 if (lvar == NULL)
4435 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004436 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004437 return NULL;
4438 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4439 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004440
Bram Moolenaar61a89812020-05-07 16:58:17 +02004441 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004442 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004443}
4444
4445/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004446 * Return the length of an assignment operator, or zero if there isn't one.
4447 */
4448 int
4449assignment_len(char_u *p, int *heredoc)
4450{
4451 if (*p == '=')
4452 {
4453 if (p[1] == '<' && p[2] == '<')
4454 {
4455 *heredoc = TRUE;
4456 return 3;
4457 }
4458 return 1;
4459 }
4460 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4461 return 2;
4462 if (STRNCMP(p, "..=", 3) == 0)
4463 return 3;
4464 return 0;
4465}
4466
4467// words that cannot be used as a variable
4468static char *reserved[] = {
4469 "true",
4470 "false",
4471 NULL
4472};
4473
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004474typedef enum {
4475 dest_local,
4476 dest_option,
4477 dest_env,
4478 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004479 dest_buffer,
4480 dest_window,
4481 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004482 dest_vimvar,
4483 dest_script,
4484 dest_reg,
4485} assign_dest_T;
4486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004488 * Generate the load instruction for "name".
4489 */
4490 static void
4491generate_loadvar(
4492 cctx_T *cctx,
4493 assign_dest_T dest,
4494 char_u *name,
4495 lvar_T *lvar,
4496 type_T *type)
4497{
4498 switch (dest)
4499 {
4500 case dest_option:
4501 // TODO: check the option exists
4502 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4503 break;
4504 case dest_global:
4505 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4506 break;
4507 case dest_buffer:
4508 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4509 break;
4510 case dest_window:
4511 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4512 break;
4513 case dest_tab:
4514 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4515 break;
4516 case dest_script:
4517 compile_load_scriptvar(cctx,
4518 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4519 break;
4520 case dest_env:
4521 // Include $ in the name here
4522 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4523 break;
4524 case dest_reg:
4525 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4526 break;
4527 case dest_vimvar:
4528 generate_LOADV(cctx, name + 2, TRUE);
4529 break;
4530 case dest_local:
4531 if (lvar->lv_from_outer)
4532 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4533 NULL, type);
4534 else
4535 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4536 break;
4537 }
4538}
4539
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004540 void
4541vim9_declare_error(char_u *name)
4542{
4543 char *scope = "";
4544
4545 switch (*name)
4546 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004547 case 'g': scope = _("global"); break;
4548 case 'b': scope = _("buffer"); break;
4549 case 'w': scope = _("window"); break;
4550 case 't': scope = _("tab"); break;
4551 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004552 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004553 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004554 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004555 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004556 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004557 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004558 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004559 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004560 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004561}
4562
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004563/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004564 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004565 * "let name"
4566 * "var name = expr"
4567 * "final name = expr"
4568 * "const name = expr"
4569 * "name = expr"
4570 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004571 * Return NULL for an error.
4572 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573 */
4574 static char_u *
4575compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4576{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004577 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004578 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004579 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580 char_u *ret = NULL;
4581 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004582 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004583 int scriptvar_sid = 0;
4584 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004586 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004587 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004588 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589 int oplen = 0;
4590 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004591 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004592 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004593 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004595 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
4596 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004598 // Skip over the "var" or "[var, var]" to get to any "=".
4599 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4600 if (p == NULL)
4601 return *arg == '[' ? arg : NULL;
4602
4603 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004605 // TODO: should we allow this, and figure out type inference from list
4606 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004607 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 return NULL;
4609 }
4610
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004611 sp = p;
4612 p = skipwhite(p);
4613 op = p;
4614 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004615
4616 if (var_count > 0 && oplen == 0)
4617 // can be something like "[1, 2]->func()"
4618 return arg;
4619
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004620 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4621 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004622 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004623 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004624 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004625
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004626 if (heredoc)
4627 {
4628 list_T *l;
4629 listitem_T *li;
4630
4631 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004632 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004633 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004634 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02004635 if (l == NULL)
4636 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637
Bram Moolenaar078269b2020-09-21 20:35:55 +02004638 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02004640 // Push each line and the create the list.
4641 FOR_ALL_LIST_ITEMS(l, li)
4642 {
4643 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4644 li->li_tv.vval.v_string = NULL;
4645 }
4646 generate_NEWLIST(cctx, l->lv_len);
4647 type = &t_list_string;
4648 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004649 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004650 list_free(l);
4651 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004652 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004654 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004655 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004656 // for "[var, var] = expr" evaluate the expression here, loop over the
4657 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004658
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004659 p = skipwhite(op + oplen);
4660 if (compile_expr0(&p, cctx) == FAIL)
4661 return NULL;
4662 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004663
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004664 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004666 type_T *stacktype;
4667
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004668 stacktype = stack->ga_len == 0 ? &t_void
4669 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004670 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004671 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004672 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004673 goto theend;
4674 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004675 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004676 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004677 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004678 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4679 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004680 }
4681 }
4682
4683 /*
4684 * Loop over variables in "[var, var] = expr".
4685 * For "var = expr" and "let var: type" this is done only once.
4686 */
4687 if (var_count > 0)
4688 var_start = skipwhite(arg + 1); // skip over the "["
4689 else
4690 var_start = arg;
4691 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4692 {
4693 char_u *var_end = skip_var_one(var_start, FALSE);
4694 size_t varlen;
4695 int new_local = FALSE;
4696 int opt_type;
4697 int opt_flags = 0;
4698 assign_dest_T dest = dest_local;
4699 int vimvaridx = -1;
4700 lvar_T *lvar = NULL;
4701 lvar_T arg_lvar;
4702 int has_type = FALSE;
4703 int has_index = FALSE;
4704 int instr_count = -1;
4705
Bram Moolenaar65821722020-08-02 18:58:54 +02004706 if (*var_start == '@')
4707 p = var_start + 2;
4708 else
4709 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02004710 // skip over the leading "&", "&l:", "&g:" and "$"
4711 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02004712 p = to_name_end(p, TRUE);
4713 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004714
4715 // "a: type" is declaring variable "a" with a type, not "a:".
4716 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4717 --var_end;
4718 if (is_decl && p == var_start + 2 && p[-1] == ':')
4719 --p;
4720
4721 varlen = p - var_start;
4722 vim_free(name);
4723 name = vim_strnsave(var_start, varlen);
4724 if (name == NULL)
4725 return NULL;
4726 if (!heredoc)
4727 type = &t_any;
4728
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004729 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004730 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004731 int declare_error = FALSE;
4732
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004733 if (*var_start == '&')
4734 {
4735 int cc;
4736 long numval;
4737
4738 dest = dest_option;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004739 if (cmdidx == CMD_final || cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004740 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004741 emsg(_(e_const_option));
4742 goto theend;
4743 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004744 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004745 p = var_start;
4746 p = find_option_end(&p, &opt_flags);
4747 if (p == NULL)
4748 {
4749 // cannot happen?
4750 emsg(_(e_letunexp));
4751 goto theend;
4752 }
4753 cc = *p;
4754 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02004755 opt_type = get_option_value(skip_option_env_lead(var_start),
4756 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004757 *p = cc;
4758 if (opt_type == -3)
4759 {
4760 semsg(_(e_unknown_option), var_start);
4761 goto theend;
4762 }
4763 if (opt_type == -2 || opt_type == 0)
4764 type = &t_string;
4765 else
4766 type = &t_number; // both number and boolean option
4767 }
4768 else if (*var_start == '$')
4769 {
4770 dest = dest_env;
4771 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004772 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004773 }
4774 else if (*var_start == '@')
4775 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004776 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004777 {
4778 emsg_invreg(var_start[1]);
4779 goto theend;
4780 }
4781 dest = dest_reg;
4782 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004783 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004784 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004785 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004786 {
4787 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004788 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004789 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004790 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004791 {
4792 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004793 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004794 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004795 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004796 {
4797 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004798 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004799 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004800 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004801 {
4802 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004803 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004804 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004805 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004806 {
4807 typval_T *vtv;
4808 int di_flags;
4809
4810 vimvaridx = find_vim_var(name + 2, &di_flags);
4811 if (vimvaridx < 0)
4812 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004813 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004814 goto theend;
4815 }
4816 // We use the current value of "sandbox" here, is that OK?
4817 if (var_check_ro(di_flags, name, FALSE))
4818 goto theend;
4819 dest = dest_vimvar;
4820 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004821 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004822 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004823 }
4824 else
4825 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004826 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004827
4828 for (idx = 0; reserved[idx] != NULL; ++idx)
4829 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004830 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004831 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004832 goto theend;
4833 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004834
4835 lvar = lookup_local(var_start, varlen, cctx);
4836 if (lvar == NULL)
4837 {
4838 CLEAR_FIELD(arg_lvar);
4839 if (lookup_arg(var_start, varlen,
4840 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4841 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004842 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004843 if (is_decl)
4844 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004845 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004846 goto theend;
4847 }
4848 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004849 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004850 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004851 if (lvar != NULL)
4852 {
4853 if (is_decl)
4854 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004855 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004856 goto theend;
4857 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004858 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004859 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004860 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004861 int script_namespace = varlen > 1
4862 && STRNCMP(var_start, "s:", 2) == 0;
4863 int script_var = (script_namespace
Bram Moolenaar53900992020-08-22 19:02:02 +02004864 ? lookup_script(var_start + 2, varlen - 2, FALSE)
4865 : lookup_script(var_start, varlen, TRUE)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004866 imported_T *import =
4867 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004868
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004869 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004870 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004871 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4872
4873 if (is_decl)
4874 {
4875 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004876 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004877 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004878 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004879 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004880 name);
4881 goto theend;
4882 }
4883 else if (cctx->ctx_ufunc->uf_script_ctx_version
4884 == SCRIPT_VERSION_VIM9
4885 && script_namespace
4886 && !script_var && import == NULL)
4887 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004888 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004889 goto theend;
4890 }
4891
4892 dest = dest_script;
4893
4894 // existing script-local variables should have a type
4895 scriptvar_sid = current_sctx.sc_sid;
4896 if (import != NULL)
4897 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004898 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004899 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02004900 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4901 rawname, TRUE);
4902 if (scriptvar_idx > 0)
4903 {
4904 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4905 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004906 ((svar_T *)si->sn_var_vals.ga_data)
4907 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004908 type = sv->sv_type;
4909 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004910 }
4911 }
4912 else if (name[1] == ':' && name[2] != NUL)
4913 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004914 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004915 goto theend;
4916 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004917 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004918 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004919 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004920 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004921 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004922 else if (check_defined(var_start, varlen, cctx) == FAIL)
4923 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004924 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004925 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004926
4927 if (declare_error)
4928 {
4929 vim9_declare_error(name);
4930 goto theend;
4931 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004932 }
4933
4934 // handle "a:name" as a name, not index "name" on "a"
4935 if (varlen > 1 || var_start[varlen] != ':')
4936 p = var_end;
4937
4938 if (dest != dest_option)
4939 {
4940 if (is_decl && *p == ':')
4941 {
4942 // parse optional type: "let var: type = expr"
4943 if (!VIM_ISWHITE(p[1]))
4944 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004945 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004946 goto theend;
4947 }
4948 p = skipwhite(p + 1);
4949 type = parse_type(&p, cctx->ctx_type_list);
4950 has_type = TRUE;
4951 }
4952 else if (lvar != NULL)
4953 type = lvar->lv_type;
4954 }
4955
4956 if (oplen == 3 && !heredoc && dest != dest_global
4957 && type->tt_type != VAR_STRING
4958 && type->tt_type != VAR_ANY)
4959 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004960 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004961 goto theend;
4962 }
4963
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004964 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004965 {
4966 if (oplen > 1 && !heredoc)
4967 {
4968 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004969 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004970 goto theend;
4971 }
4972
4973 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004974 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
4975 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004976 goto theend;
4977 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004978 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004979 if (lvar == NULL)
4980 goto theend;
4981 new_local = TRUE;
4982 }
4983
4984 member_type = type;
4985 if (var_end > var_start + varlen)
4986 {
4987 // Something follows after the variable: "var[idx]".
4988 if (is_decl)
4989 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004990 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004991 goto theend;
4992 }
4993
4994 if (var_start[varlen] == '[')
4995 {
4996 has_index = TRUE;
4997 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004998 member_type = &t_any;
4999 else
5000 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005001 }
5002 else
5003 {
5004 semsg("Not supported yet: %s", var_start);
5005 goto theend;
5006 }
5007 }
5008 else if (lvar == &arg_lvar)
5009 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005010 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005011 goto theend;
5012 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005013 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5014 {
5015 semsg(_(e_cannot_assign_to_constant), name);
5016 goto theend;
5017 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005018
5019 if (!heredoc)
5020 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005021 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005022 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005023 if (oplen > 0 && var_count == 0)
5024 {
5025 // skip over the "=" and the expression
5026 p = skipwhite(op + oplen);
5027 compile_expr0(&p, cctx);
5028 }
5029 }
5030 else if (oplen > 0)
5031 {
5032 type_T *stacktype;
5033
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005034 // For "var = expr" evaluate the expression.
5035 if (var_count == 0)
5036 {
5037 int r;
5038
5039 // for "+=", "*=", "..=" etc. first load the current value
5040 if (*op != '=')
5041 {
5042 generate_loadvar(cctx, dest, name, lvar, type);
5043
5044 if (has_index)
5045 {
5046 // TODO: get member from list or dict
5047 emsg("Index with operation not supported yet");
5048 goto theend;
5049 }
5050 }
5051
5052 // Compile the expression. Temporarily hide the new local
5053 // variable here, it is not available to this expression.
5054 if (new_local)
5055 --cctx->ctx_locals.ga_len;
5056 instr_count = instr->ga_len;
5057 p = skipwhite(op + oplen);
5058 r = compile_expr0(&p, cctx);
5059 if (new_local)
5060 ++cctx->ctx_locals.ga_len;
5061 if (r == FAIL)
5062 goto theend;
5063 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005064 else if (semicolon && var_idx == var_count - 1)
5065 {
5066 // For "[var; var] = expr" get the rest of the list
5067 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5068 goto theend;
5069 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005070 else
5071 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005072 // For "[var, var] = expr" get the "var_idx" item from the
5073 // list.
5074 if (generate_GETITEM(cctx, var_idx) == FAIL)
5075 return FAIL;
5076 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005077
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005078 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005079 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005080 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005081 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005082 if ((stacktype->tt_type == VAR_FUNC
5083 || stacktype->tt_type == VAR_PARTIAL)
5084 && var_wrong_func_name(name, TRUE))
5085 goto theend;
5086
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005087 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005088 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005089 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005090 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005091 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005092 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005093 }
5094 else
5095 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005096 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005097 // for a variable that implies &t_any.
5098 if (stacktype == &t_list_empty)
5099 lvar->lv_type = &t_list_any;
5100 else if (stacktype == &t_dict_empty)
5101 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005102 else if (stacktype == &t_unknown)
5103 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005104 else
5105 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005106 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005107 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005108 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005109 {
5110 type_T *use_type = lvar->lv_type;
5111
Bram Moolenaar71abe482020-09-14 22:28:30 +02005112 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005113 if (has_index)
5114 {
5115 use_type = use_type->tt_member;
5116 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005117 // could be indexing "any"
5118 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005119 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005120 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005121 == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005122 goto theend;
5123 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005124 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005125 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005126 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005127 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005128 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005129 else if (cmdidx == CMD_final)
5130 {
5131 emsg(_(e_final_requires_a_value));
5132 goto theend;
5133 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005134 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005135 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005136 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005137 goto theend;
5138 }
5139 else if (!has_type || dest == dest_option)
5140 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005141 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005142 goto theend;
5143 }
5144 else
5145 {
5146 // variables are always initialized
5147 if (ga_grow(instr, 1) == FAIL)
5148 goto theend;
5149 switch (member_type->tt_type)
5150 {
5151 case VAR_BOOL:
5152 generate_PUSHBOOL(cctx, VVAL_FALSE);
5153 break;
5154 case VAR_FLOAT:
5155#ifdef FEAT_FLOAT
5156 generate_PUSHF(cctx, 0.0);
5157#endif
5158 break;
5159 case VAR_STRING:
5160 generate_PUSHS(cctx, NULL);
5161 break;
5162 case VAR_BLOB:
5163 generate_PUSHBLOB(cctx, NULL);
5164 break;
5165 case VAR_FUNC:
5166 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5167 break;
5168 case VAR_LIST:
5169 generate_NEWLIST(cctx, 0);
5170 break;
5171 case VAR_DICT:
5172 generate_NEWDICT(cctx, 0);
5173 break;
5174 case VAR_JOB:
5175 generate_PUSHJOB(cctx, NULL);
5176 break;
5177 case VAR_CHANNEL:
5178 generate_PUSHCHANNEL(cctx, NULL);
5179 break;
5180 case VAR_NUMBER:
5181 case VAR_UNKNOWN:
5182 case VAR_ANY:
5183 case VAR_PARTIAL:
5184 case VAR_VOID:
5185 case VAR_SPECIAL: // cannot happen
5186 generate_PUSHNR(cctx, 0);
5187 break;
5188 }
5189 }
5190 if (var_count == 0)
5191 end = p;
5192 }
5193
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005194 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005195 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005196 break;
5197
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005198 if (oplen > 0 && *op != '=')
5199 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005200 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005201 type_T *stacktype;
5202
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005203 if (*op == '.')
5204 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005205 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005206 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005207 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005208 if (
5209#ifdef FEAT_FLOAT
5210 // If variable is float operation with number is OK.
5211 !(expected == &t_float && stacktype == &t_number) &&
5212#endif
5213 need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005214 goto theend;
5215
5216 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005217 {
5218 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5219 goto theend;
5220 }
5221 else if (*op == '+')
5222 {
5223 if (generate_add_instr(cctx,
5224 operator_type(member_type, stacktype),
5225 member_type, stacktype) == FAIL)
5226 goto theend;
5227 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005228 else if (generate_two_op(cctx, op) == FAIL)
5229 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005230 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005231
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005232 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005233 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005234 int r;
5235
5236 // Compile the "idx" in "var[idx]".
5237 if (new_local)
5238 --cctx->ctx_locals.ga_len;
5239 p = skipwhite(var_start + varlen + 1);
5240 r = compile_expr0(&p, cctx);
5241 if (new_local)
5242 ++cctx->ctx_locals.ga_len;
5243 if (r == FAIL)
5244 goto theend;
5245 if (*skipwhite(p) != ']')
5246 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005247 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005248 emsg(_(e_missbrac));
5249 goto theend;
5250 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005251 if (type == &t_any)
5252 {
5253 type_T *idx_type = ((type_T **)stack->ga_data)[
5254 stack->ga_len - 1];
5255 // Index on variable of unknown type: guess the type from the
5256 // index type: number is dict, otherwise dict.
5257 // TODO: should do the assignment at runtime
5258 if (idx_type->tt_type == VAR_NUMBER)
5259 type = &t_list_any;
5260 else
5261 type = &t_dict_any;
5262 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005263 if (type->tt_type == VAR_DICT
5264 && may_generate_2STRING(-1, cctx) == FAIL)
5265 goto theend;
5266 if (type->tt_type == VAR_LIST
5267 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005268 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005269 {
5270 emsg(_(e_number_exp));
5271 goto theend;
5272 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005273
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005274 // Load the dict or list. On the stack we then have:
5275 // - value
5276 // - index
5277 // - variable
5278 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005279
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005280 if (type->tt_type == VAR_LIST)
5281 {
5282 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5283 return FAIL;
5284 }
5285 else if (type->tt_type == VAR_DICT)
5286 {
5287 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5288 return FAIL;
5289 }
5290 else
5291 {
5292 emsg(_(e_listreq));
5293 goto theend;
5294 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005295 }
5296 else
5297 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005298 if (is_decl && cmdidx == CMD_const
5299 && (dest == dest_script || dest == dest_local))
5300 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005301 generate_LOCKCONST(cctx);
5302
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005303 switch (dest)
5304 {
5305 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005306 generate_STOREOPT(cctx, skip_option_env_lead(name),
5307 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005308 break;
5309 case dest_global:
5310 // include g: with the name, easier to execute that way
5311 generate_STORE(cctx, ISN_STOREG, 0, name);
5312 break;
5313 case dest_buffer:
5314 // include b: with the name, easier to execute that way
5315 generate_STORE(cctx, ISN_STOREB, 0, name);
5316 break;
5317 case dest_window:
5318 // include w: with the name, easier to execute that way
5319 generate_STORE(cctx, ISN_STOREW, 0, name);
5320 break;
5321 case dest_tab:
5322 // include t: with the name, easier to execute that way
5323 generate_STORE(cctx, ISN_STORET, 0, name);
5324 break;
5325 case dest_env:
5326 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5327 break;
5328 case dest_reg:
5329 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5330 break;
5331 case dest_vimvar:
5332 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5333 break;
5334 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005335 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005336 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005337 {
5338 char_u *name_s = name;
5339
5340 // Include s: in the name for store_var()
5341 if (name[1] != ':')
5342 {
5343 int len = (int)STRLEN(name) + 3;
5344
5345 name_s = alloc(len);
5346 if (name_s == NULL)
5347 name_s = name;
5348 else
5349 vim_snprintf((char *)name_s, len,
5350 "s:%s", name);
5351 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005352 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5353 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005354 if (name_s != name)
5355 vim_free(name_s);
5356 }
5357 else
5358 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005359 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005360 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005361 break;
5362 case dest_local:
5363 if (lvar != NULL)
5364 {
5365 isn_T *isn = ((isn_T *)instr->ga_data)
5366 + instr->ga_len - 1;
5367
5368 // optimization: turn "var = 123" from ISN_PUSHNR +
5369 // ISN_STORE into ISN_STORENR
5370 if (!lvar->lv_from_outer
5371 && instr->ga_len == instr_count + 1
5372 && isn->isn_type == ISN_PUSHNR)
5373 {
5374 varnumber_T val = isn->isn_arg.number;
5375
5376 isn->isn_type = ISN_STORENR;
5377 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5378 isn->isn_arg.storenr.stnr_val = val;
5379 if (stack->ga_len > 0)
5380 --stack->ga_len;
5381 }
5382 else if (lvar->lv_from_outer)
5383 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005384 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005385 else
5386 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5387 }
5388 break;
5389 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005390 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005391
5392 if (var_idx + 1 < var_count)
5393 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005394 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005395
5396 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005397 if (var_count > 0 && !semicolon)
5398 {
5399 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5400 goto theend;
5401 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005402
Bram Moolenaarb2097502020-07-19 17:17:02 +02005403 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005404
5405theend:
5406 vim_free(name);
5407 return ret;
5408}
5409
5410/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005411 * Check if "name" can be "unlet".
5412 */
5413 int
5414check_vim9_unlet(char_u *name)
5415{
5416 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5417 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005418 // "unlet s:var" is allowed in legacy script.
5419 if (*name == 's' && !script_is_vim9())
5420 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005421 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005422 return FAIL;
5423 }
5424 return OK;
5425}
5426
5427/*
5428 * Callback passed to ex_unletlock().
5429 */
5430 static int
5431compile_unlet(
5432 lval_T *lvp,
5433 char_u *name_end,
5434 exarg_T *eap,
5435 int deep UNUSED,
5436 void *coookie)
5437{
5438 cctx_T *cctx = coookie;
5439
5440 if (lvp->ll_tv == NULL)
5441 {
5442 char_u *p = lvp->ll_name;
5443 int cc = *name_end;
5444 int ret = OK;
5445
5446 // Normal name. Only supports g:, w:, t: and b: namespaces.
5447 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005448 if (*p == '$')
5449 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5450 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005451 ret = FAIL;
5452 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005453 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005454
5455 *name_end = cc;
5456 return ret;
5457 }
5458
5459 // TODO: unlet {list}[idx]
5460 // TODO: unlet {dict}[key]
5461 emsg("Sorry, :unlet not fully implemented yet");
5462 return FAIL;
5463}
5464
5465/*
5466 * compile "unlet var", "lock var" and "unlock var"
5467 * "arg" points to "var".
5468 */
5469 static char_u *
5470compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5471{
5472 char_u *p = arg;
5473
5474 if (eap->cmdidx != CMD_unlet)
5475 {
5476 emsg("Sorry, :lock and unlock not implemented yet");
5477 return NULL;
5478 }
5479
5480 if (*p == '!')
5481 {
5482 p = skipwhite(p + 1);
5483 eap->forceit = TRUE;
5484 }
5485
5486 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5487 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5488}
5489
5490/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005491 * Compile an :import command.
5492 */
5493 static char_u *
5494compile_import(char_u *arg, cctx_T *cctx)
5495{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005496 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005497}
5498
5499/*
5500 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5501 */
5502 static int
5503compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5504{
5505 garray_T *instr = &cctx->ctx_instr;
5506 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5507
5508 if (endlabel == NULL)
5509 return FAIL;
5510 endlabel->el_next = *el;
5511 *el = endlabel;
5512 endlabel->el_end_label = instr->ga_len;
5513
5514 generate_JUMP(cctx, when, 0);
5515 return OK;
5516}
5517
5518 static void
5519compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5520{
5521 garray_T *instr = &cctx->ctx_instr;
5522
5523 while (*el != NULL)
5524 {
5525 endlabel_T *cur = (*el);
5526 isn_T *isn;
5527
5528 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5529 isn->isn_arg.jump.jump_where = instr->ga_len;
5530 *el = cur->el_next;
5531 vim_free(cur);
5532 }
5533}
5534
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005535 static void
5536compile_free_jump_to_end(endlabel_T **el)
5537{
5538 while (*el != NULL)
5539 {
5540 endlabel_T *cur = (*el);
5541
5542 *el = cur->el_next;
5543 vim_free(cur);
5544 }
5545}
5546
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005547/*
5548 * Create a new scope and set up the generic items.
5549 */
5550 static scope_T *
5551new_scope(cctx_T *cctx, scopetype_T type)
5552{
5553 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5554
5555 if (scope == NULL)
5556 return NULL;
5557 scope->se_outer = cctx->ctx_scope;
5558 cctx->ctx_scope = scope;
5559 scope->se_type = type;
5560 scope->se_local_count = cctx->ctx_locals.ga_len;
5561 return scope;
5562}
5563
5564/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005565 * Free the current scope and go back to the outer scope.
5566 */
5567 static void
5568drop_scope(cctx_T *cctx)
5569{
5570 scope_T *scope = cctx->ctx_scope;
5571
5572 if (scope == NULL)
5573 {
5574 iemsg("calling drop_scope() without a scope");
5575 return;
5576 }
5577 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005578 switch (scope->se_type)
5579 {
5580 case IF_SCOPE:
5581 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5582 case FOR_SCOPE:
5583 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5584 case WHILE_SCOPE:
5585 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5586 case TRY_SCOPE:
5587 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5588 case NO_SCOPE:
5589 case BLOCK_SCOPE:
5590 break;
5591 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005592 vim_free(scope);
5593}
5594
5595/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005596 * compile "if expr"
5597 *
5598 * "if expr" Produces instructions:
5599 * EVAL expr Push result of "expr"
5600 * JUMP_IF_FALSE end
5601 * ... body ...
5602 * end:
5603 *
5604 * "if expr | else" Produces instructions:
5605 * EVAL expr Push result of "expr"
5606 * JUMP_IF_FALSE else
5607 * ... body ...
5608 * JUMP_ALWAYS end
5609 * else:
5610 * ... body ...
5611 * end:
5612 *
5613 * "if expr1 | elseif expr2 | else" Produces instructions:
5614 * EVAL expr Push result of "expr"
5615 * JUMP_IF_FALSE elseif
5616 * ... body ...
5617 * JUMP_ALWAYS end
5618 * elseif:
5619 * EVAL expr Push result of "expr"
5620 * JUMP_IF_FALSE else
5621 * ... body ...
5622 * JUMP_ALWAYS end
5623 * else:
5624 * ... body ...
5625 * end:
5626 */
5627 static char_u *
5628compile_if(char_u *arg, cctx_T *cctx)
5629{
5630 char_u *p = arg;
5631 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005632 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005633 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005634 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005635 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005636
Bram Moolenaara5565e42020-05-09 15:44:01 +02005637 CLEAR_FIELD(ppconst);
5638 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005639 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005640 clear_ppconst(&ppconst);
5641 return NULL;
5642 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005643 if (cctx->ctx_skip == SKIP_YES)
5644 clear_ppconst(&ppconst);
5645 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005646 {
5647 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005648 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005649 clear_ppconst(&ppconst);
5650 }
5651 else
5652 {
5653 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005654 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005655 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005656 return NULL;
5657 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005658
5659 scope = new_scope(cctx, IF_SCOPE);
5660 if (scope == NULL)
5661 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005662 scope->se_skip_save = skip_save;
5663 // "is_had_return" will be reset if any block does not end in :return
5664 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005665
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005666 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005667 {
5668 // "where" is set when ":elseif", "else" or ":endif" is found
5669 scope->se_u.se_if.is_if_label = instr->ga_len;
5670 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5671 }
5672 else
5673 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005674
5675 return p;
5676}
5677
5678 static char_u *
5679compile_elseif(char_u *arg, cctx_T *cctx)
5680{
5681 char_u *p = arg;
5682 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005683 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005684 isn_T *isn;
5685 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005686 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02005687 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005688
5689 if (scope == NULL || scope->se_type != IF_SCOPE)
5690 {
5691 emsg(_(e_elseif_without_if));
5692 return NULL;
5693 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005694 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005695 if (!cctx->ctx_had_return)
5696 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005697
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005698 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005699 {
5700 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005701 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005702 return NULL;
5703 // previous "if" or "elseif" jumps here
5704 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5705 isn->isn_arg.jump.jump_where = instr->ga_len;
5706 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005707
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005708 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005709 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02005710 if (cctx->ctx_skip == SKIP_YES)
5711 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005712 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005713 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005714 clear_ppconst(&ppconst);
5715 return NULL;
5716 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02005717 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005718 if (scope->se_skip_save == SKIP_YES)
5719 clear_ppconst(&ppconst);
5720 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005721 {
5722 // The expression results in a constant.
5723 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005724 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005725 clear_ppconst(&ppconst);
5726 scope->se_u.se_if.is_if_label = -1;
5727 }
5728 else
5729 {
5730 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005731 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005732 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005733 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005734
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005735 // "where" is set when ":elseif", "else" or ":endif" is found
5736 scope->se_u.se_if.is_if_label = instr->ga_len;
5737 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5738 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005739
5740 return p;
5741}
5742
5743 static char_u *
5744compile_else(char_u *arg, cctx_T *cctx)
5745{
5746 char_u *p = arg;
5747 garray_T *instr = &cctx->ctx_instr;
5748 isn_T *isn;
5749 scope_T *scope = cctx->ctx_scope;
5750
5751 if (scope == NULL || scope->se_type != IF_SCOPE)
5752 {
5753 emsg(_(e_else_without_if));
5754 return NULL;
5755 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005756 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005757 if (!cctx->ctx_had_return)
5758 scope->se_u.se_if.is_had_return = FALSE;
5759 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005760
Bram Moolenaarefd88552020-06-18 20:50:10 +02005761 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005762 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005763 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005764 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005765 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005766 if (!cctx->ctx_had_return
5767 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5768 JUMP_ALWAYS, cctx) == FAIL)
5769 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005770 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005771
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005772 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005773 {
5774 if (scope->se_u.se_if.is_if_label >= 0)
5775 {
5776 // previous "if" or "elseif" jumps here
5777 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5778 isn->isn_arg.jump.jump_where = instr->ga_len;
5779 scope->se_u.se_if.is_if_label = -1;
5780 }
5781 }
5782
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005783 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005784 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5785 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005786
5787 return p;
5788}
5789
5790 static char_u *
5791compile_endif(char_u *arg, cctx_T *cctx)
5792{
5793 scope_T *scope = cctx->ctx_scope;
5794 ifscope_T *ifscope;
5795 garray_T *instr = &cctx->ctx_instr;
5796 isn_T *isn;
5797
5798 if (scope == NULL || scope->se_type != IF_SCOPE)
5799 {
5800 emsg(_(e_endif_without_if));
5801 return NULL;
5802 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005803 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005804 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005805 if (!cctx->ctx_had_return)
5806 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005807
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005808 if (scope->se_u.se_if.is_if_label >= 0)
5809 {
5810 // previous "if" or "elseif" jumps here
5811 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5812 isn->isn_arg.jump.jump_where = instr->ga_len;
5813 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005814 // Fill in the "end" label in jumps at the end of the blocks.
5815 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005816 cctx->ctx_skip = scope->se_skip_save;
5817
5818 // If all the blocks end in :return and there is an :else then the
5819 // had_return flag is set.
5820 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005821
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005822 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005823 return arg;
5824}
5825
5826/*
5827 * compile "for var in expr"
5828 *
5829 * Produces instructions:
5830 * PUSHNR -1
5831 * STORE loop-idx Set index to -1
5832 * EVAL expr Push result of "expr"
5833 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5834 * - if beyond end, jump to "end"
5835 * - otherwise get item from list and push it
5836 * STORE var Store item in "var"
5837 * ... body ...
5838 * JUMP top Jump back to repeat
5839 * end: DROP Drop the result of "expr"
5840 *
5841 */
5842 static char_u *
5843compile_for(char_u *arg, cctx_T *cctx)
5844{
5845 char_u *p;
5846 size_t varlen;
5847 garray_T *instr = &cctx->ctx_instr;
5848 garray_T *stack = &cctx->ctx_type_stack;
5849 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005850 lvar_T *loop_lvar; // loop iteration variable
5851 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005852 type_T *vartype;
5853
5854 // TODO: list of variables: "for [key, value] in dict"
5855 // parse "var"
5856 for (p = arg; eval_isnamec1(*p); ++p)
5857 ;
5858 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005859 var_lvar = lookup_local(arg, varlen, cctx);
5860 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005861 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005862 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005863 return NULL;
5864 }
5865
5866 // consume "in"
5867 p = skipwhite(p);
5868 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5869 {
5870 emsg(_(e_missing_in));
5871 return NULL;
5872 }
5873 p = skipwhite(p + 2);
5874
5875
5876 scope = new_scope(cctx, FOR_SCOPE);
5877 if (scope == NULL)
5878 return NULL;
5879
5880 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005881 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5882 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005883 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005884 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005885 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005886 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005887 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005888
5889 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005890 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5891 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005892 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005893 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005894 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005895 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005896 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005897
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005898 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005899
5900 // compile "expr", it remains on the stack until "endfor"
5901 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005902 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005903 {
5904 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005905 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005906 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005907
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005908 // Now that we know the type of "var", check that it is a list, now or at
5909 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005910 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005911 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005912 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005913 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005914 return NULL;
5915 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005916 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005917 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005918
5919 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005920 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005921
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005922 generate_FOR(cctx, loop_lvar->lv_idx);
5923 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005924
5925 return arg;
5926}
5927
5928/*
5929 * compile "endfor"
5930 */
5931 static char_u *
5932compile_endfor(char_u *arg, cctx_T *cctx)
5933{
5934 garray_T *instr = &cctx->ctx_instr;
5935 scope_T *scope = cctx->ctx_scope;
5936 forscope_T *forscope;
5937 isn_T *isn;
5938
5939 if (scope == NULL || scope->se_type != FOR_SCOPE)
5940 {
5941 emsg(_(e_for));
5942 return NULL;
5943 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005944 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005945 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005946 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005947
5948 // At end of ":for" scope jump back to the FOR instruction.
5949 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5950
5951 // Fill in the "end" label in the FOR statement so it can jump here
5952 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5953 isn->isn_arg.forloop.for_end = instr->ga_len;
5954
5955 // Fill in the "end" label any BREAK statements
5956 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5957
5958 // Below the ":for" scope drop the "expr" list from the stack.
5959 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5960 return NULL;
5961
5962 vim_free(scope);
5963
5964 return arg;
5965}
5966
5967/*
5968 * compile "while expr"
5969 *
5970 * Produces instructions:
5971 * top: EVAL expr Push result of "expr"
5972 * JUMP_IF_FALSE end jump if false
5973 * ... body ...
5974 * JUMP top Jump back to repeat
5975 * end:
5976 *
5977 */
5978 static char_u *
5979compile_while(char_u *arg, cctx_T *cctx)
5980{
5981 char_u *p = arg;
5982 garray_T *instr = &cctx->ctx_instr;
5983 scope_T *scope;
5984
5985 scope = new_scope(cctx, WHILE_SCOPE);
5986 if (scope == NULL)
5987 return NULL;
5988
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005989 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005990
5991 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005992 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005993 return NULL;
5994
5995 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005996 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005997 JUMP_IF_FALSE, cctx) == FAIL)
5998 return FAIL;
5999
6000 return p;
6001}
6002
6003/*
6004 * compile "endwhile"
6005 */
6006 static char_u *
6007compile_endwhile(char_u *arg, cctx_T *cctx)
6008{
6009 scope_T *scope = cctx->ctx_scope;
6010
6011 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6012 {
6013 emsg(_(e_while));
6014 return NULL;
6015 }
6016 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006017 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006018
6019 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006020 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006021
6022 // Fill in the "end" label in the WHILE statement so it can jump here.
6023 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006024 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006025
6026 vim_free(scope);
6027
6028 return arg;
6029}
6030
6031/*
6032 * compile "continue"
6033 */
6034 static char_u *
6035compile_continue(char_u *arg, cctx_T *cctx)
6036{
6037 scope_T *scope = cctx->ctx_scope;
6038
6039 for (;;)
6040 {
6041 if (scope == NULL)
6042 {
6043 emsg(_(e_continue));
6044 return NULL;
6045 }
6046 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6047 break;
6048 scope = scope->se_outer;
6049 }
6050
6051 // Jump back to the FOR or WHILE instruction.
6052 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006053 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6054 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006055 return arg;
6056}
6057
6058/*
6059 * compile "break"
6060 */
6061 static char_u *
6062compile_break(char_u *arg, cctx_T *cctx)
6063{
6064 scope_T *scope = cctx->ctx_scope;
6065 endlabel_T **el;
6066
6067 for (;;)
6068 {
6069 if (scope == NULL)
6070 {
6071 emsg(_(e_break));
6072 return NULL;
6073 }
6074 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6075 break;
6076 scope = scope->se_outer;
6077 }
6078
6079 // Jump to the end of the FOR or WHILE loop.
6080 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006081 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006082 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006083 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006084 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6085 return FAIL;
6086
6087 return arg;
6088}
6089
6090/*
6091 * compile "{" start of block
6092 */
6093 static char_u *
6094compile_block(char_u *arg, cctx_T *cctx)
6095{
6096 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6097 return NULL;
6098 return skipwhite(arg + 1);
6099}
6100
6101/*
6102 * compile end of block: drop one scope
6103 */
6104 static void
6105compile_endblock(cctx_T *cctx)
6106{
6107 scope_T *scope = cctx->ctx_scope;
6108
6109 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006110 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006111 vim_free(scope);
6112}
6113
6114/*
6115 * compile "try"
6116 * Creates a new scope for the try-endtry, pointing to the first catch and
6117 * finally.
6118 * Creates another scope for the "try" block itself.
6119 * TRY instruction sets up exception handling at runtime.
6120 *
6121 * "try"
6122 * TRY -> catch1, -> finally push trystack entry
6123 * ... try block
6124 * "throw {exception}"
6125 * EVAL {exception}
6126 * THROW create exception
6127 * ... try block
6128 * " catch {expr}"
6129 * JUMP -> finally
6130 * catch1: PUSH exeception
6131 * EVAL {expr}
6132 * MATCH
6133 * JUMP nomatch -> catch2
6134 * CATCH remove exception
6135 * ... catch block
6136 * " catch"
6137 * JUMP -> finally
6138 * catch2: CATCH remove exception
6139 * ... catch block
6140 * " finally"
6141 * finally:
6142 * ... finally block
6143 * " endtry"
6144 * ENDTRY pop trystack entry, may rethrow
6145 */
6146 static char_u *
6147compile_try(char_u *arg, cctx_T *cctx)
6148{
6149 garray_T *instr = &cctx->ctx_instr;
6150 scope_T *try_scope;
6151 scope_T *scope;
6152
6153 // scope that holds the jumps that go to catch/finally/endtry
6154 try_scope = new_scope(cctx, TRY_SCOPE);
6155 if (try_scope == NULL)
6156 return NULL;
6157
6158 // "catch" is set when the first ":catch" is found.
6159 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006160 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006161 if (generate_instr(cctx, ISN_TRY) == NULL)
6162 return NULL;
6163
6164 // scope for the try block itself
6165 scope = new_scope(cctx, BLOCK_SCOPE);
6166 if (scope == NULL)
6167 return NULL;
6168
6169 return arg;
6170}
6171
6172/*
6173 * compile "catch {expr}"
6174 */
6175 static char_u *
6176compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6177{
6178 scope_T *scope = cctx->ctx_scope;
6179 garray_T *instr = &cctx->ctx_instr;
6180 char_u *p;
6181 isn_T *isn;
6182
6183 // end block scope from :try or :catch
6184 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6185 compile_endblock(cctx);
6186 scope = cctx->ctx_scope;
6187
6188 // Error if not in a :try scope
6189 if (scope == NULL || scope->se_type != TRY_SCOPE)
6190 {
6191 emsg(_(e_catch));
6192 return NULL;
6193 }
6194
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006195 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006196 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006197 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006198 return NULL;
6199 }
6200
6201 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006202 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006203 JUMP_ALWAYS, cctx) == FAIL)
6204 return NULL;
6205
6206 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006207 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006208 if (isn->isn_arg.try.try_catch == 0)
6209 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006210 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006211 {
6212 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006213 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006214 isn->isn_arg.jump.jump_where = instr->ga_len;
6215 }
6216
6217 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006218 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006219 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006220 scope->se_u.se_try.ts_caught_all = TRUE;
6221 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222 }
6223 else
6224 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006225 char_u *end;
6226 char_u *pat;
6227 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006228 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006229 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006230
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006231 // Push v:exception, push {expr} and MATCH
6232 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6233
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006234 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006235 if (*end != *p)
6236 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006237 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006238 vim_free(tofree);
6239 return FAIL;
6240 }
6241 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006242 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006243 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006244 len = (int)(end - tofree);
6245 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006246 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006247 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006248 if (pat == NULL)
6249 return FAIL;
6250 if (generate_PUSHS(cctx, pat) == FAIL)
6251 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006252
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006253 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6254 return NULL;
6255
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006256 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006257 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6258 return NULL;
6259 }
6260
6261 if (generate_instr(cctx, ISN_CATCH) == NULL)
6262 return NULL;
6263
6264 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6265 return NULL;
6266 return p;
6267}
6268
6269 static char_u *
6270compile_finally(char_u *arg, cctx_T *cctx)
6271{
6272 scope_T *scope = cctx->ctx_scope;
6273 garray_T *instr = &cctx->ctx_instr;
6274 isn_T *isn;
6275
6276 // end block scope from :try or :catch
6277 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6278 compile_endblock(cctx);
6279 scope = cctx->ctx_scope;
6280
6281 // Error if not in a :try scope
6282 if (scope == NULL || scope->se_type != TRY_SCOPE)
6283 {
6284 emsg(_(e_finally));
6285 return NULL;
6286 }
6287
6288 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006289 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006290 if (isn->isn_arg.try.try_finally != 0)
6291 {
6292 emsg(_(e_finally_dup));
6293 return NULL;
6294 }
6295
6296 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006297 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006298
Bram Moolenaar585fea72020-04-02 22:33:21 +02006299 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006300 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006301 {
6302 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006303 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006304 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006305 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006306 }
6307
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006308 // TODO: set index in ts_finally_label jumps
6309
6310 return arg;
6311}
6312
6313 static char_u *
6314compile_endtry(char_u *arg, cctx_T *cctx)
6315{
6316 scope_T *scope = cctx->ctx_scope;
6317 garray_T *instr = &cctx->ctx_instr;
6318 isn_T *isn;
6319
6320 // end block scope from :catch or :finally
6321 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6322 compile_endblock(cctx);
6323 scope = cctx->ctx_scope;
6324
6325 // Error if not in a :try scope
6326 if (scope == NULL || scope->se_type != TRY_SCOPE)
6327 {
6328 if (scope == NULL)
6329 emsg(_(e_no_endtry));
6330 else if (scope->se_type == WHILE_SCOPE)
6331 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006332 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006333 emsg(_(e_endfor));
6334 else
6335 emsg(_(e_endif));
6336 return NULL;
6337 }
6338
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006339 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006340 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6341 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006342 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006343 return NULL;
6344 }
6345
6346 // Fill in the "end" label in jumps at the end of the blocks, if not done
6347 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006348 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006349
6350 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006351 if (isn->isn_arg.try.try_catch == 0)
6352 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006353 if (isn->isn_arg.try.try_finally == 0)
6354 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006355
6356 if (scope->se_u.se_try.ts_catch_label != 0)
6357 {
6358 // Last catch without match jumps here
6359 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6360 isn->isn_arg.jump.jump_where = instr->ga_len;
6361 }
6362
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006363 compile_endblock(cctx);
6364
6365 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6366 return NULL;
6367 return arg;
6368}
6369
6370/*
6371 * compile "throw {expr}"
6372 */
6373 static char_u *
6374compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6375{
6376 char_u *p = skipwhite(arg);
6377
Bram Moolenaara5565e42020-05-09 15:44:01 +02006378 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006379 return NULL;
6380 if (may_generate_2STRING(-1, cctx) == FAIL)
6381 return NULL;
6382 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6383 return NULL;
6384
6385 return p;
6386}
6387
6388/*
6389 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006390 * compile "echomsg expr"
6391 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006392 * compile "execute expr"
6393 */
6394 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006395compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006396{
6397 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006398 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006399 int count = 0;
6400
6401 for (;;)
6402 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006403 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006404 return NULL;
6405 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006406 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006407 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006408 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006409 break;
6410 }
6411
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006412 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6413 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6414 else if (cmdidx == CMD_execute)
6415 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6416 else if (cmdidx == CMD_echomsg)
6417 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6418 else
6419 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006420 return p;
6421}
6422
6423/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006424 * :put r
6425 * :put ={expr}
6426 */
6427 static char_u *
6428compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6429{
6430 char_u *line = arg;
6431 linenr_T lnum;
6432 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006433 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006434
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006435 eap->regname = *line;
6436
6437 if (eap->regname == '=')
6438 {
6439 char_u *p = line + 1;
6440
6441 if (compile_expr0(&p, cctx) == FAIL)
6442 return NULL;
6443 line = p;
6444 }
6445 else if (eap->regname != NUL)
6446 ++line;
6447
6448 // TODO: if the range is something like "$" need to evaluate at runtime
6449 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6450 return NULL;
6451 if (eap->addr_count == 0)
6452 lnum = -1;
6453 else
6454 lnum = eap->line2;
6455 if (above)
6456 --lnum;
6457
6458 generate_PUT(cctx, eap->regname, lnum);
6459 return line;
6460}
6461
6462/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006463 * A command that is not compiled, execute with legacy code.
6464 */
6465 static char_u *
6466compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6467{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006468 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006469 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006470 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006471
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006472 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006473 goto theend;
6474
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006475 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006476 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006477 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006478 int usefilter = FALSE;
6479
6480 has_expr = argt & (EX_XFILE | EX_EXPAND);
6481
6482 // If the command can be followed by a bar, find the bar and truncate
6483 // it, so that the following command can be compiled.
6484 // The '|' is overwritten with a NUL, it is put back below.
6485 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6486 && *eap->arg == '!')
6487 // :w !filter or :r !filter or :r! filter
6488 usefilter = TRUE;
6489 if ((argt & EX_TRLBAR) && !usefilter)
6490 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006491 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006492 separate_nextcmd(eap);
6493 if (eap->nextcmd != NULL)
6494 nextcmd = eap->nextcmd;
6495 }
6496 }
6497
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006498 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6499 {
6500 // expand filename in "syntax include [@group] filename"
6501 has_expr = TRUE;
6502 eap->arg = skipwhite(eap->arg + 7);
6503 if (*eap->arg == '@')
6504 eap->arg = skiptowhite(eap->arg);
6505 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006506
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006507 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006508 {
6509 int count = 0;
6510 char_u *start = skipwhite(line);
6511
6512 // :cmd xxx`=expr1`yyy`=expr2`zzz
6513 // PUSHS ":cmd xxx"
6514 // eval expr1
6515 // PUSHS "yyy"
6516 // eval expr2
6517 // PUSHS "zzz"
6518 // EXECCONCAT 5
6519 for (;;)
6520 {
6521 if (p > start)
6522 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006523 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006524 ++count;
6525 }
6526 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006527 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006528 return NULL;
6529 may_generate_2STRING(-1, cctx);
6530 ++count;
6531 p = skipwhite(p);
6532 if (*p != '`')
6533 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006534 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006535 return NULL;
6536 }
6537 start = p + 1;
6538
6539 p = (char_u *)strstr((char *)start, "`=");
6540 if (p == NULL)
6541 {
6542 if (*skipwhite(start) != NUL)
6543 {
6544 generate_PUSHS(cctx, vim_strsave(start));
6545 ++count;
6546 }
6547 break;
6548 }
6549 }
6550 generate_EXECCONCAT(cctx, count);
6551 }
6552 else
6553 generate_EXEC(cctx, line);
6554
6555theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006556 if (*nextcmd != NUL)
6557 {
6558 // the parser expects a pointer to the bar, put it back
6559 --nextcmd;
6560 *nextcmd = '|';
6561 }
6562
6563 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006564}
6565
6566/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006567 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006568 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006569 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006570 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006571add_def_function(ufunc_T *ufunc)
6572{
6573 dfunc_T *dfunc;
6574
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006575 if (def_functions.ga_len == 0)
6576 {
6577 // The first position is not used, so that a zero uf_dfunc_idx means it
6578 // wasn't set.
6579 if (ga_grow(&def_functions, 1) == FAIL)
6580 return FAIL;
6581 ++def_functions.ga_len;
6582 }
6583
Bram Moolenaar09689a02020-05-09 22:50:08 +02006584 // Add the function to "def_functions".
6585 if (ga_grow(&def_functions, 1) == FAIL)
6586 return FAIL;
6587 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6588 CLEAR_POINTER(dfunc);
6589 dfunc->df_idx = def_functions.ga_len;
6590 ufunc->uf_dfunc_idx = dfunc->df_idx;
6591 dfunc->df_ufunc = ufunc;
6592 ++def_functions.ga_len;
6593 return OK;
6594}
6595
6596/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006597 * After ex_function() has collected all the function lines: parse and compile
6598 * the lines into instructions.
6599 * Adds the function to "def_functions".
6600 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6601 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006602 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006603 * This can be used recursively through compile_lambda(), which may reallocate
6604 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006605 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006606 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006607 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006608compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006609{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006610 char_u *line = NULL;
6611 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006612 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006613 cctx_T cctx;
6614 garray_T *instr;
6615 int called_emsg_before = called_emsg;
6616 int ret = FAIL;
6617 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006618 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006619 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006620 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006621
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006622 // When using a function that was compiled before: Free old instructions.
6623 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006624 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006625 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006626 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6627 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006628 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006629 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006630 else
6631 {
6632 if (add_def_function(ufunc) == FAIL)
6633 return FAIL;
6634 new_def_function = TRUE;
6635 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006636
Bram Moolenaar985116a2020-07-12 17:31:09 +02006637 ufunc->uf_def_status = UF_COMPILING;
6638
Bram Moolenaara80faa82020-04-12 19:37:17 +02006639 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006640 cctx.ctx_ufunc = ufunc;
6641 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006642 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006643 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6644 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6645 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6646 cctx.ctx_type_list = &ufunc->uf_type_list;
6647 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6648 instr = &cctx.ctx_instr;
6649
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006650 // Set the context to the function, it may be compiled when called from
6651 // another script. Set the script version to the most modern one.
6652 // The line number will be set in next_line_from_context().
6653 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006654 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6655
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006656 // Make sure error messages are OK.
6657 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6658 if (do_estack_push)
6659 estack_push_ufunc(ufunc, 1);
6660
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006661 if (ufunc->uf_def_args.ga_len > 0)
6662 {
6663 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006664 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006665 int i;
6666 char_u *arg;
6667 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006668 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006669
6670 // Produce instructions for the default values of optional arguments.
6671 // Store the instruction index in uf_def_arg_idx[] so that we know
6672 // where to start when the function is called, depending on the number
6673 // of arguments.
6674 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6675 if (ufunc->uf_def_arg_idx == NULL)
6676 goto erret;
6677 for (i = 0; i < count; ++i)
6678 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006679 garray_T *stack = &cctx.ctx_type_stack;
6680 type_T *val_type;
6681 int arg_idx = first_def_arg + i;
6682
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006683 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6684 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006685 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006686 goto erret;
6687
6688 // If no type specified use the type of the default value.
6689 // Otherwise check that the default value type matches the
6690 // specified type.
6691 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6692 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006693 {
6694 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006695 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006696 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02006697 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
6698 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006699 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006700
6701 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006702 goto erret;
6703 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006704 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006705
6706 if (did_set_arg_type)
6707 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006708 }
6709
6710 /*
6711 * Loop over all the lines of the function and generate instructions.
6712 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006713 for (;;)
6714 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006715 exarg_T ea;
6716 cmdmod_T save_cmdmod;
6717 int starts_with_colon = FALSE;
6718 char_u *cmd;
6719 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006720
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006721 // Bail out on the first error to avoid a flood of errors and report
6722 // the right line number when inside try/catch.
6723 if (emsg_before != called_emsg)
6724 goto erret;
6725
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006726 if (line != NULL && *line == '|')
6727 // the line continues after a '|'
6728 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006729 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006730 && !(*line == '#' && (line == cctx.ctx_line_start
6731 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006732 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006733 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006734 goto erret;
6735 }
6736 else
6737 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006738 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006739 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006740 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006741 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006742 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006743 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006744
Bram Moolenaara80faa82020-04-12 19:37:17 +02006745 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006746 ea.cmdlinep = &line;
6747 ea.cmd = skipwhite(line);
6748
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006749 // Some things can be recognized by the first character.
6750 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006751 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006752 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006753 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006754 if (ea.cmd[1] != '{')
6755 {
6756 line = (char_u *)"";
6757 continue;
6758 }
6759 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006760
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006761 case '}':
6762 {
6763 // "}" ends a block scope
6764 scopetype_T stype = cctx.ctx_scope == NULL
6765 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006766
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006767 if (stype == BLOCK_SCOPE)
6768 {
6769 compile_endblock(&cctx);
6770 line = ea.cmd;
6771 }
6772 else
6773 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006774 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006775 goto erret;
6776 }
6777 if (line != NULL)
6778 line = skipwhite(ea.cmd + 1);
6779 continue;
6780 }
6781
6782 case '{':
6783 // "{" starts a block scope
6784 // "{'a': 1}->func() is something else
6785 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6786 {
6787 line = compile_block(ea.cmd, &cctx);
6788 continue;
6789 }
6790 break;
6791
6792 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006793 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006794 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006795 }
6796
6797 /*
6798 * COMMAND MODIFIERS
6799 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006800 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006801 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6802 {
6803 if (errormsg != NULL)
6804 goto erret;
6805 // empty line or comment
6806 line = (char_u *)"";
6807 continue;
6808 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006809 // TODO: use modifiers in the command
6810 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006811 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006812
6813 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006814 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006815 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006816 {
6817 if (*ea.cmd == '(')
6818 // not for "call()"
6819 ea.cmd = p;
6820 else
6821 ea.cmd = skipwhite(ea.cmd);
6822 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006823
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006824 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006825 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006826 char_u *pskip;
6827
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006828 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006829 // find what follows.
6830 // Skip over "var.member", "var[idx]" and the like.
6831 // Also "&opt = val", "$ENV = val" and "@r = val".
6832 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006833 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006834 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006835 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006836 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006837 char_u *var_end;
6838 int oplen;
6839 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006840
Bram Moolenaar65821722020-08-02 18:58:54 +02006841 if (ea.cmd[0] == '@')
6842 var_end = ea.cmd + 2;
6843 else
6844 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006845 FNE_CHECK_START | FNE_INCL_BR);
6846 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006847 if (oplen > 0)
6848 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006849 size_t len = p - ea.cmd;
6850
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006851 // Recognize an assignment if we recognize the variable
6852 // name:
6853 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006854 // "local = expr" where "local" is a local var.
6855 // "script = expr" where "script" is a script-local var.
6856 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006857 // "&opt = expr"
6858 // "$ENV = expr"
6859 // "@r = expr"
6860 if (*ea.cmd == '&'
6861 || *ea.cmd == '$'
6862 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006863 || ((len) > 2 && ea.cmd[1] == ':')
6864 || lookup_local(ea.cmd, len, &cctx) != NULL
6865 || lookup_arg(ea.cmd, len, NULL, NULL,
6866 NULL, &cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02006867 || lookup_script(ea.cmd, len, FALSE) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006868 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006869 {
6870 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006871 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006872 goto erret;
6873 continue;
6874 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006875 }
6876 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006877
6878 if (*ea.cmd == '[')
6879 {
6880 // [var, var] = expr
6881 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6882 if (line == NULL)
6883 goto erret;
6884 if (line != ea.cmd)
6885 continue;
6886 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006887 }
6888
6889 /*
6890 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006891 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006892 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006893 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006894 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006895 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02006896 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006897 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006898 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006899 if (!starts_with_colon)
6900 {
6901 emsg(_(e_colon_required_before_a_range));
6902 goto erret;
6903 }
6904 if (ends_excmd2(line, ea.cmd))
6905 {
6906 // A range without a command: jump to the line.
6907 // TODO: compile to a more efficient command, possibly
6908 // calling parse_cmd_address().
6909 ea.cmdidx = CMD_SIZE;
6910 line = compile_exec(line, &ea, &cctx);
6911 goto nextline;
6912 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006913 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006914 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006915 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006916 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006917 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006918
6919 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6920 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006921 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006922 {
6923 line += STRLEN(line);
6924 continue;
6925 }
6926
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006927 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006928 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006929 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006930 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006931 iemsg("Command from find_ex_command() not handled");
6932 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006933 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006934 }
6935
Bram Moolenaar3988f642020-08-27 22:43:03 +02006936 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006937 && ea.cmdidx != CMD_elseif
6938 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02006939 && ea.cmdidx != CMD_endif
6940 && ea.cmdidx != CMD_endfor
6941 && ea.cmdidx != CMD_endwhile
6942 && ea.cmdidx != CMD_catch
6943 && ea.cmdidx != CMD_finally
6944 && ea.cmdidx != CMD_endtry)
6945 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02006946 emsg(_(e_unreachable_code_after_return));
6947 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006948 }
6949
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006950 p = skipwhite(p);
6951 if (ea.cmdidx != CMD_SIZE
6952 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
6953 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02006954 if (ea.cmdidx >= 0)
6955 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006956 if ((ea.argt & EX_BANG) && *p == '!')
6957 {
6958 ea.forceit = TRUE;
6959 p = skipwhite(p + 1);
6960 }
6961 }
6962
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006963 switch (ea.cmdidx)
6964 {
6965 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006966 ea.arg = p;
6967 line = compile_nested_function(&ea, &cctx);
6968 break;
6969
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006970 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006971 // TODO: should we allow this, e.g. to declare a global
6972 // function?
6973 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006974 goto erret;
6975
6976 case CMD_return:
6977 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006978 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006979 break;
6980
6981 case CMD_let:
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02006982 if (get_vim_var_nr(VV_DISALLOW_LET))
6983 {
6984 emsg(_(e_cannot_use_let_in_vim9_script));
6985 break;
6986 }
6987 // FALLTHROUGH
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006988 case CMD_var:
6989 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006990 case CMD_const:
6991 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006992 if (line == p)
6993 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006994 break;
6995
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006996 case CMD_unlet:
6997 case CMD_unlockvar:
6998 case CMD_lockvar:
6999 line = compile_unletlock(p, &ea, &cctx);
7000 break;
7001
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007002 case CMD_import:
7003 line = compile_import(p, &cctx);
7004 break;
7005
7006 case CMD_if:
7007 line = compile_if(p, &cctx);
7008 break;
7009 case CMD_elseif:
7010 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007011 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007012 break;
7013 case CMD_else:
7014 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007015 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007016 break;
7017 case CMD_endif:
7018 line = compile_endif(p, &cctx);
7019 break;
7020
7021 case CMD_while:
7022 line = compile_while(p, &cctx);
7023 break;
7024 case CMD_endwhile:
7025 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007026 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007027 break;
7028
7029 case CMD_for:
7030 line = compile_for(p, &cctx);
7031 break;
7032 case CMD_endfor:
7033 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007034 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007035 break;
7036 case CMD_continue:
7037 line = compile_continue(p, &cctx);
7038 break;
7039 case CMD_break:
7040 line = compile_break(p, &cctx);
7041 break;
7042
7043 case CMD_try:
7044 line = compile_try(p, &cctx);
7045 break;
7046 case CMD_catch:
7047 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007048 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007049 break;
7050 case CMD_finally:
7051 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007052 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007053 break;
7054 case CMD_endtry:
7055 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007056 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007057 break;
7058 case CMD_throw:
7059 line = compile_throw(p, &cctx);
7060 break;
7061
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007062 case CMD_eval:
7063 if (compile_expr0(&p, &cctx) == FAIL)
7064 goto erret;
7065
Bram Moolenaar3988f642020-08-27 22:43:03 +02007066 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007067 generate_instr_drop(&cctx, ISN_DROP, 1);
7068
7069 line = skipwhite(p);
7070 break;
7071
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007072 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007073 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007074 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007075 case CMD_echomsg:
7076 case CMD_echoerr:
7077 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007078 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007079
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007080 case CMD_put:
7081 ea.cmd = cmd;
7082 line = compile_put(p, &ea, &cctx);
7083 break;
7084
Bram Moolenaar3988f642020-08-27 22:43:03 +02007085 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007086
Bram Moolenaarae616492020-07-28 20:07:27 +02007087 case CMD_append:
7088 case CMD_change:
7089 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007090 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007091 case CMD_xit:
7092 not_in_vim9(&ea);
7093 goto erret;
7094
Bram Moolenaar002262f2020-07-08 17:47:57 +02007095 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007096 if (cctx.ctx_skip != SKIP_YES)
7097 {
7098 semsg(_(e_invalid_command_str), ea.cmd);
7099 goto erret;
7100 }
7101 // We don't check for a next command here.
7102 line = (char_u *)"";
7103 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007104
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007105 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007106 if (cctx.ctx_skip == SKIP_YES)
7107 {
7108 // We don't check for a next command here.
7109 line = (char_u *)"";
7110 }
7111 else
7112 {
7113 // Not recognized, execute with do_cmdline_cmd().
7114 ea.arg = p;
7115 line = compile_exec(line, &ea, &cctx);
7116 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007117 break;
7118 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007119nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007120 if (line == NULL)
7121 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007122 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007123
7124 if (cctx.ctx_type_stack.ga_len < 0)
7125 {
7126 iemsg("Type stack underflow");
7127 goto erret;
7128 }
7129 }
7130
7131 if (cctx.ctx_scope != NULL)
7132 {
7133 if (cctx.ctx_scope->se_type == IF_SCOPE)
7134 emsg(_(e_endif));
7135 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7136 emsg(_(e_endwhile));
7137 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7138 emsg(_(e_endfor));
7139 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007140 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007141 goto erret;
7142 }
7143
Bram Moolenaarefd88552020-06-18 20:50:10 +02007144 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007145 {
7146 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7147 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007148 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007149 goto erret;
7150 }
7151
7152 // Return zero if there is no return at the end.
7153 generate_PUSHNR(&cctx, 0);
7154 generate_instr(&cctx, ISN_RETURN);
7155 }
7156
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007157 {
7158 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7159 + ufunc->uf_dfunc_idx;
7160 dfunc->df_deleted = FALSE;
7161 dfunc->df_instr = instr->ga_data;
7162 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007163 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007164 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007165 if (cctx.ctx_outer_used)
7166 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007167 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007168 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007169
7170 ret = OK;
7171
7172erret:
7173 if (ret == FAIL)
7174 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007175 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007176 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7177 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007178
7179 for (idx = 0; idx < instr->ga_len; ++idx)
7180 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007181 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007182
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007183 // If using the last entry in the table and it was added above, we
7184 // might as well remove it.
7185 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007186 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007187 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007188 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007189 ufunc->uf_dfunc_idx = 0;
7190 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007191 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007192
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007193 while (cctx.ctx_scope != NULL)
7194 drop_scope(&cctx);
7195
Bram Moolenaar20431c92020-03-20 18:39:46 +01007196 // Don't execute this function body.
7197 ga_clear_strings(&ufunc->uf_lines);
7198
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007199 if (errormsg != NULL)
7200 emsg(errormsg);
7201 else if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007202 emsg(_(e_compile_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007203 }
7204
7205 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007206 if (do_estack_push)
7207 estack_pop();
7208
Bram Moolenaar20431c92020-03-20 18:39:46 +01007209 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007210 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007211 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007212 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007213}
7214
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007215 void
7216set_function_type(ufunc_T *ufunc)
7217{
7218 int varargs = ufunc->uf_va_name != NULL;
7219 int argcount = ufunc->uf_args.ga_len;
7220
7221 // Create a type for the function, with the return type and any
7222 // argument types.
7223 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7224 // The type is included in "tt_args".
7225 if (argcount > 0 || varargs)
7226 {
7227 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7228 argcount, &ufunc->uf_type_list);
7229 // Add argument types to the function type.
7230 if (func_type_add_arg_types(ufunc->uf_func_type,
7231 argcount + varargs,
7232 &ufunc->uf_type_list) == FAIL)
7233 return;
7234 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7235 ufunc->uf_func_type->tt_min_argcount =
7236 argcount - ufunc->uf_def_args.ga_len;
7237 if (ufunc->uf_arg_types == NULL)
7238 {
7239 int i;
7240
7241 // lambda does not have argument types.
7242 for (i = 0; i < argcount; ++i)
7243 ufunc->uf_func_type->tt_args[i] = &t_any;
7244 }
7245 else
7246 mch_memmove(ufunc->uf_func_type->tt_args,
7247 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7248 if (varargs)
7249 {
7250 ufunc->uf_func_type->tt_args[argcount] =
7251 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7252 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7253 }
7254 }
7255 else
7256 // No arguments, can use a predefined type.
7257 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7258 argcount, &ufunc->uf_type_list);
7259}
7260
7261
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007262/*
7263 * Delete an instruction, free what it contains.
7264 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007265 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007266delete_instr(isn_T *isn)
7267{
7268 switch (isn->isn_type)
7269 {
7270 case ISN_EXEC:
7271 case ISN_LOADENV:
7272 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007273 case ISN_LOADB:
7274 case ISN_LOADW:
7275 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007276 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007277 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007278 case ISN_PUSHEXC:
7279 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007280 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007281 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007282 case ISN_STOREB:
7283 case ISN_STOREW:
7284 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007285 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007286 vim_free(isn->isn_arg.string);
7287 break;
7288
7289 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007290 case ISN_STORES:
7291 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007292 break;
7293
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007294 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007295 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007296 vim_free(isn->isn_arg.unlet.ul_name);
7297 break;
7298
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007299 case ISN_STOREOPT:
7300 vim_free(isn->isn_arg.storeopt.so_name);
7301 break;
7302
7303 case ISN_PUSHBLOB: // push blob isn_arg.blob
7304 blob_unref(isn->isn_arg.blob);
7305 break;
7306
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007307 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007308#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007309 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007310#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007311 break;
7312
7313 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007314#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007315 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007316#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007317 break;
7318
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007319 case ISN_UCALL:
7320 vim_free(isn->isn_arg.ufunc.cuf_name);
7321 break;
7322
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007323 case ISN_FUNCREF:
7324 {
7325 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7326 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007327
7328 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7329 func_ptr_unref(dfunc->df_ufunc);
7330 }
7331 break;
7332
7333 case ISN_DCALL:
7334 {
7335 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7336 + isn->isn_arg.dfunc.cdf_idx;
7337
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007338 if (dfunc->df_ufunc != NULL
7339 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007340 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007341 }
7342 break;
7343
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007344 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007345 {
7346 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7347 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7348
7349 if (ufunc != NULL)
7350 {
7351 // Clear uf_dfunc_idx so that the function is deleted.
7352 clear_def_function(ufunc);
7353 ufunc->uf_dfunc_idx = 0;
7354 func_ptr_unref(ufunc);
7355 }
7356
7357 vim_free(lambda);
7358 vim_free(isn->isn_arg.newfunc.nf_global);
7359 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007360 break;
7361
Bram Moolenaar5e654232020-09-16 15:22:00 +02007362 case ISN_CHECKTYPE:
7363 free_type(isn->isn_arg.type.ct_type);
7364 break;
7365
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007366 case ISN_2BOOL:
7367 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007368 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007369 case ISN_ADDBLOB:
7370 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007371 case ISN_ANYINDEX:
7372 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007373 case ISN_BCALL:
7374 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007375 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007376 case ISN_CHECKNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007377 case ISN_COMPAREANY:
7378 case ISN_COMPAREBLOB:
7379 case ISN_COMPAREBOOL:
7380 case ISN_COMPAREDICT:
7381 case ISN_COMPAREFLOAT:
7382 case ISN_COMPAREFUNC:
7383 case ISN_COMPARELIST:
7384 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007385 case ISN_COMPARESPECIAL:
7386 case ISN_COMPARESTRING:
7387 case ISN_CONCAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007388 case ISN_DROP:
7389 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007390 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007391 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007392 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007393 case ISN_EXECCONCAT:
7394 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007395 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007396 case ISN_GETITEM:
7397 case ISN_JUMP:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007398 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007399 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007400 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007401 case ISN_LOADBDICT:
7402 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007403 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007404 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007405 case ISN_LOADSCRIPT:
7406 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007407 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007408 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007409 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007410 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007411 case ISN_NEGATENR:
7412 case ISN_NEWDICT:
7413 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007414 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007415 case ISN_OPFLOAT:
7416 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007417 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007418 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007419 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007420 case ISN_PUSHF:
7421 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007422 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007423 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007424 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007425 case ISN_SHUFFLE:
7426 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007427 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007428 case ISN_STOREDICT:
7429 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007430 case ISN_STORENR:
7431 case ISN_STOREOUTER:
7432 case ISN_STOREREG:
7433 case ISN_STORESCRIPT:
7434 case ISN_STOREV:
7435 case ISN_STRINDEX:
7436 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007437 case ISN_THROW:
7438 case ISN_TRY:
7439 // nothing allocated
7440 break;
7441 }
7442}
7443
7444/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007445 * Free all instructions for "dfunc".
7446 */
7447 static void
7448delete_def_function_contents(dfunc_T *dfunc)
7449{
7450 int idx;
7451
7452 ga_clear(&dfunc->df_def_args_isn);
7453
7454 if (dfunc->df_instr != NULL)
7455 {
7456 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7457 delete_instr(dfunc->df_instr + idx);
7458 VIM_CLEAR(dfunc->df_instr);
7459 }
7460
7461 dfunc->df_deleted = TRUE;
7462}
7463
7464/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007465 * When a user function is deleted, clear the contents of any associated def
7466 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007467 */
7468 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007469clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007470{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007471 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007472 {
7473 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7474 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007475
Bram Moolenaar20431c92020-03-20 18:39:46 +01007476 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007477 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007478 }
7479}
7480
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007481/*
7482 * Used when a user function is about to be deleted: remove the pointer to it.
7483 * The entry in def_functions is then unused.
7484 */
7485 void
7486unlink_def_function(ufunc_T *ufunc)
7487{
7488 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7489
7490 dfunc->df_ufunc = NULL;
7491}
7492
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007493#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007494/*
7495 * Free all functions defined with ":def".
7496 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007497 void
7498free_def_functions(void)
7499{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007500 int idx;
7501
7502 for (idx = 0; idx < def_functions.ga_len; ++idx)
7503 {
7504 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7505
7506 delete_def_function_contents(dfunc);
7507 }
7508
7509 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007510}
7511#endif
7512
7513
7514#endif // FEAT_EVAL