blob: 52651f36a1a898fef075e1ec1695c8c452f9fc72 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
111 int lv_from_outer; // when TRUE using ctx_outer scope
112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200129 int ctx_closure_count; // number of closures created in the
130 // function
131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144};
145
Bram Moolenaar20431c92020-03-20 18:39:46 +0100146static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100147
148/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200149 * Lookup variable "name" in the local scope and return it.
150 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200152 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153lookup_local(char_u *name, size_t len, cctx_T *cctx)
154{
155 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200156 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100158 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200159 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200160
161 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
163 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200164 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100165 if (STRNCMP(name, lvar->lv_name, len) == 0
166 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167 {
168 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200169 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200170 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200172
173 // Find local in outer function scope.
174 if (cctx->ctx_outer != NULL)
175 {
176 lvar = lookup_local(name, len, cctx->ctx_outer);
177 if (lvar != NULL)
178 {
179 // TODO: are there situations we should not mark the outer scope as
180 // used?
181 cctx->ctx_outer_used = TRUE;
182 lvar->lv_from_outer = TRUE;
183 return lvar;
184 }
185 }
186
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200187 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100188}
189
190/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200191 * Lookup an argument in the current function and an enclosing function.
192 * Returns the argument index in "idxp"
193 * Returns the argument type in "type"
194 * Sets "gen_load_outer" to TRUE if found in outer scope.
195 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196 */
197 static int
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200198lookup_arg(
199 char_u *name,
200 size_t len,
201 int *idxp,
202 type_T **type,
203 int *gen_load_outer,
204 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100205{
206 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200207 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100209 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200210 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
212 {
213 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
214
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200215 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
216 {
217 if (idxp != NULL)
218 {
219 // Arguments are located above the frame pointer. One further
220 // if there is a vararg argument
221 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
222 + STACK_FRAME_SIZE)
223 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
224
225 if (cctx->ctx_ufunc->uf_arg_types != NULL)
226 *type = cctx->ctx_ufunc->uf_arg_types[idx];
227 else
228 *type = &t_any;
229 }
230 return OK;
231 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100232 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100233
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200234 va_name = cctx->ctx_ufunc->uf_va_name;
235 if (va_name != NULL
236 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
237 {
238 if (idxp != NULL)
239 {
240 // varargs is always the last argument
241 *idxp = -STACK_FRAME_SIZE - 1;
242 *type = cctx->ctx_ufunc->uf_va_type;
243 }
244 return OK;
245 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200247 if (cctx->ctx_outer != NULL)
248 {
249 // Lookup the name for an argument of the outer function.
250 if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
251 == OK)
252 {
253 *gen_load_outer = TRUE;
254 return OK;
255 }
256 }
257
258 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100259}
260
261/*
Bram Moolenaar84367732020-08-23 15:21:55 +0200262 * Returnd TRUE if the script context is Vim9 script.
263 */
264 static int
265script_is_vim9()
266{
267 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
268}
269
270/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 * Lookup a variable in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200272 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
273 * without "s:".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274 * Returns OK or FAIL.
275 */
276 static int
Bram Moolenaar53900992020-08-22 19:02:02 +0200277lookup_script(char_u *name, size_t len, int vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278{
279 int cc;
280 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
281 dictitem_T *di;
282
Bram Moolenaar84367732020-08-23 15:21:55 +0200283 if (vim9script && !script_is_vim9())
Bram Moolenaar53900992020-08-22 19:02:02 +0200284 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100285 cc = name[len];
286 name[len] = NUL;
287 di = find_var_in_ht(ht, 0, name, TRUE);
288 name[len] = cc;
289 return di == NULL ? FAIL: OK;
290}
291
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100292/*
293 * Check if "p[len]" is already defined, either in script "import_sid" or in
294 * compilation context "cctx".
Bram Moolenaar0f769812020-09-12 18:32:34 +0200295 * Does not check the global namespace.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100296 * Return FAIL and give an error if it defined.
297 */
298 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200299check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100300{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200301 int c = p[len];
302 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200303
304 p[len] = NUL;
Bram Moolenaar53900992020-08-22 19:02:02 +0200305 if (lookup_script(p, len, FALSE) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100306 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200307 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaareef21022020-08-01 22:16:43 +0200308 || lookup_arg(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200309 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200310 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100311 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200312 // A local or script-local function can shadow a global function.
313 if (ufunc == NULL || !func_is_global(ufunc)
314 || (p[0] == 'g' && p[1] == ':'))
315 {
316 p[len] = c;
317 semsg(_(e_name_already_defined_str), p);
318 return FAIL;
319 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100320 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200321 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100322 return OK;
323}
324
Bram Moolenaar65b95452020-07-19 14:03:09 +0200325
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100326/////////////////////////////////////////////////////////////////////
327// Following generate_ functions expect the caller to call ga_grow().
328
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200329#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
330#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100332/*
333 * Generate an instruction without arguments.
334 * Returns a pointer to the new instruction, NULL if failed.
335 */
336 static isn_T *
337generate_instr(cctx_T *cctx, isntype_T isn_type)
338{
339 garray_T *instr = &cctx->ctx_instr;
340 isn_T *isn;
341
Bram Moolenaar080457c2020-03-03 21:53:32 +0100342 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100343 if (ga_grow(instr, 1) == FAIL)
344 return NULL;
345 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
346 isn->isn_type = isn_type;
347 isn->isn_lnum = cctx->ctx_lnum + 1;
348 ++instr->ga_len;
349
350 return isn;
351}
352
353/*
354 * Generate an instruction without arguments.
355 * "drop" will be removed from the stack.
356 * Returns a pointer to the new instruction, NULL if failed.
357 */
358 static isn_T *
359generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
360{
361 garray_T *stack = &cctx->ctx_type_stack;
362
Bram Moolenaar080457c2020-03-03 21:53:32 +0100363 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100364 stack->ga_len -= drop;
365 return generate_instr(cctx, isn_type);
366}
367
368/*
369 * Generate instruction "isn_type" and put "type" on the type stack.
370 */
371 static isn_T *
372generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
373{
374 isn_T *isn;
375 garray_T *stack = &cctx->ctx_type_stack;
376
377 if ((isn = generate_instr(cctx, isn_type)) == NULL)
378 return NULL;
379
380 if (ga_grow(stack, 1) == FAIL)
381 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200382 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100383 ++stack->ga_len;
384
385 return isn;
386}
387
388/*
389 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200390 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100391 */
392 static int
393may_generate_2STRING(int offset, cctx_T *cctx)
394{
395 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200396 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100397 garray_T *stack = &cctx->ctx_type_stack;
398 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
399
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200400 switch ((*type)->tt_type)
401 {
402 // nothing to be done
403 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100404
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200405 // conversion possible
406 case VAR_SPECIAL:
407 case VAR_BOOL:
408 case VAR_NUMBER:
409 case VAR_FLOAT:
410 break;
411
412 // conversion possible (with runtime check)
413 case VAR_ANY:
414 case VAR_UNKNOWN:
415 isntype = ISN_2STRING_ANY;
416 break;
417
418 // conversion not possible
419 case VAR_VOID:
420 case VAR_BLOB:
421 case VAR_FUNC:
422 case VAR_PARTIAL:
423 case VAR_LIST:
424 case VAR_DICT:
425 case VAR_JOB:
426 case VAR_CHANNEL:
427 to_string_error((*type)->tt_type);
428 return FAIL;
429 }
430
431 *type = &t_string;
432 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100433 return FAIL;
434 isn->isn_arg.number = offset;
435
436 return OK;
437}
438
439 static int
440check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
441{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200442 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100443 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200444 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100445 {
446 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200447 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100448 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200449 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100450 return FAIL;
451 }
452 return OK;
453}
454
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200455 static int
456generate_add_instr(
457 cctx_T *cctx,
458 vartype_T vartype,
459 type_T *type1,
460 type_T *type2)
461{
462 isn_T *isn = generate_instr_drop(cctx,
463 vartype == VAR_NUMBER ? ISN_OPNR
464 : vartype == VAR_LIST ? ISN_ADDLIST
465 : vartype == VAR_BLOB ? ISN_ADDBLOB
466#ifdef FEAT_FLOAT
467 : vartype == VAR_FLOAT ? ISN_OPFLOAT
468#endif
469 : ISN_OPANY, 1);
470
471 if (vartype != VAR_LIST && vartype != VAR_BLOB
472 && type1->tt_type != VAR_ANY
473 && type2->tt_type != VAR_ANY
474 && check_number_or_float(
475 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
476 return FAIL;
477
478 if (isn != NULL)
479 isn->isn_arg.op.op_type = EXPR_ADD;
480 return isn == NULL ? FAIL : OK;
481}
482
483/*
484 * Get the type to use for an instruction for an operation on "type1" and
485 * "type2". If they are matching use a type-specific instruction. Otherwise
486 * fall back to runtime type checking.
487 */
488 static vartype_T
489operator_type(type_T *type1, type_T *type2)
490{
491 if (type1->tt_type == type2->tt_type
492 && (type1->tt_type == VAR_NUMBER
493 || type1->tt_type == VAR_LIST
494#ifdef FEAT_FLOAT
495 || type1->tt_type == VAR_FLOAT
496#endif
497 || type1->tt_type == VAR_BLOB))
498 return type1->tt_type;
499 return VAR_ANY;
500}
501
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100502/*
503 * Generate an instruction with two arguments. The instruction depends on the
504 * type of the arguments.
505 */
506 static int
507generate_two_op(cctx_T *cctx, char_u *op)
508{
509 garray_T *stack = &cctx->ctx_type_stack;
510 type_T *type1;
511 type_T *type2;
512 vartype_T vartype;
513 isn_T *isn;
514
Bram Moolenaar080457c2020-03-03 21:53:32 +0100515 RETURN_OK_IF_SKIP(cctx);
516
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200517 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100518 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
519 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200520 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521
522 switch (*op)
523 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200524 case '+':
525 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100526 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527 break;
528
529 case '-':
530 case '*':
531 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
532 op) == FAIL)
533 return FAIL;
534 if (vartype == VAR_NUMBER)
535 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
536#ifdef FEAT_FLOAT
537 else if (vartype == VAR_FLOAT)
538 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
539#endif
540 else
541 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
542 if (isn != NULL)
543 isn->isn_arg.op.op_type = *op == '*'
544 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
545 break;
546
Bram Moolenaar4c683752020-04-05 21:38:23 +0200547 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100548 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200549 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100550 && type2->tt_type != VAR_NUMBER))
551 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200552 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100553 return FAIL;
554 }
555 isn = generate_instr_drop(cctx,
556 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
557 if (isn != NULL)
558 isn->isn_arg.op.op_type = EXPR_REM;
559 break;
560 }
561
562 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200563 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100564 {
565 type_T *type = &t_any;
566
567#ifdef FEAT_FLOAT
568 // float+number and number+float results in float
569 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
570 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
571 type = &t_float;
572#endif
573 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
574 }
575
576 return OK;
577}
578
579/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200580 * Get the instruction to use for comparing "type1" with "type2"
581 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100582 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200583 static isntype_T
584get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100585{
586 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100587
Bram Moolenaar4c683752020-04-05 21:38:23 +0200588 if (type1 == VAR_UNKNOWN)
589 type1 = VAR_ANY;
590 if (type2 == VAR_UNKNOWN)
591 type2 = VAR_ANY;
592
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100593 if (type1 == type2)
594 {
595 switch (type1)
596 {
597 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
598 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
599 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
600 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
601 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
602 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
603 case VAR_LIST: isntype = ISN_COMPARELIST; break;
604 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
605 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100606 default: isntype = ISN_COMPAREANY; break;
607 }
608 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200609 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100610 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
611 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
612 isntype = ISN_COMPAREANY;
613
614 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
615 && (isntype == ISN_COMPAREBOOL
616 || isntype == ISN_COMPARESPECIAL
617 || isntype == ISN_COMPARENR
618 || isntype == ISN_COMPAREFLOAT))
619 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200620 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200622 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100623 }
624 if (isntype == ISN_DROP
625 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
626 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
627 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
628 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
629 && exptype != EXPR_IS && exptype != EXPR_ISNOT
630 && (type1 == VAR_BLOB || type2 == VAR_BLOB
631 || type1 == VAR_LIST || type2 == VAR_LIST))))
632 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200633 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100634 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200635 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100636 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200637 return isntype;
638}
639
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200640 int
641check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
642{
643 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
644 return FAIL;
645 return OK;
646}
647
Bram Moolenaara5565e42020-05-09 15:44:01 +0200648/*
649 * Generate an ISN_COMPARE* instruction with a boolean result.
650 */
651 static int
652generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
653{
654 isntype_T isntype;
655 isn_T *isn;
656 garray_T *stack = &cctx->ctx_type_stack;
657 vartype_T type1;
658 vartype_T type2;
659
660 RETURN_OK_IF_SKIP(cctx);
661
662 // Get the known type of the two items on the stack. If they are matching
663 // use a type-specific instruction. Otherwise fall back to runtime type
664 // checking.
665 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
666 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
667 isntype = get_compare_isn(exptype, type1, type2);
668 if (isntype == ISN_DROP)
669 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670
671 if ((isn = generate_instr(cctx, isntype)) == NULL)
672 return FAIL;
673 isn->isn_arg.op.op_type = exptype;
674 isn->isn_arg.op.op_ic = ic;
675
676 // takes two arguments, puts one bool back
677 if (stack->ga_len >= 2)
678 {
679 --stack->ga_len;
680 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
681 }
682
683 return OK;
684}
685
686/*
687 * Generate an ISN_2BOOL instruction.
688 */
689 static int
690generate_2BOOL(cctx_T *cctx, int invert)
691{
692 isn_T *isn;
693 garray_T *stack = &cctx->ctx_type_stack;
694
Bram Moolenaar080457c2020-03-03 21:53:32 +0100695 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100696 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
697 return FAIL;
698 isn->isn_arg.number = invert;
699
700 // type becomes bool
701 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
702
703 return OK;
704}
705
706 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200707generate_TYPECHECK(
708 cctx_T *cctx,
709 type_T *expected,
710 int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711{
712 isn_T *isn;
713 garray_T *stack = &cctx->ctx_type_stack;
714
Bram Moolenaar080457c2020-03-03 21:53:32 +0100715 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100716 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
717 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200718 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100719 isn->isn_arg.type.ct_off = offset;
720
Bram Moolenaar5e654232020-09-16 15:22:00 +0200721 // type becomes expected
722 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100723
724 return OK;
725}
726
727/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200728 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200729 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200730 * - "actual" is a type that can be "expected" type: add a runtime check; or
731 * - return FAIL.
732 */
733 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200734need_type(
735 type_T *actual,
736 type_T *expected,
737 int offset,
738 cctx_T *cctx,
739 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200740{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200741 if (expected == &t_bool && actual != &t_bool
742 && (actual->tt_flags & TTFLAG_BOOL_OK))
743 {
744 // Using "0", "1" or the result of an expression with "&&" or "||" as a
745 // boolean is OK but requires a conversion.
746 generate_2BOOL(cctx, FALSE);
747 return OK;
748 }
749
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200750 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200751 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200752
753 // If the actual type can be the expected type add a runtime check.
754 // TODO: if it's a constant a runtime check makes no sense.
755 if (actual->tt_type == VAR_ANY
756 || actual->tt_type == VAR_UNKNOWN
757 || (actual->tt_type == VAR_FUNC
758 && (expected->tt_type == VAR_FUNC
759 || expected->tt_type == VAR_PARTIAL)
760 && (actual->tt_member == &t_any || actual->tt_argcount < 0))
761 || (actual->tt_type == VAR_LIST
762 && expected->tt_type == VAR_LIST
763 && actual->tt_member == &t_any)
764 || (actual->tt_type == VAR_DICT
765 && expected->tt_type == VAR_DICT
766 && actual->tt_member == &t_any))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200767 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200768 generate_TYPECHECK(cctx, expected, offset);
769 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200770 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200771
772 if (!silent)
773 type_mismatch(expected, actual);
774 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200775}
776
777/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100778 * Generate an ISN_PUSHNR instruction.
779 */
780 static int
781generate_PUSHNR(cctx_T *cctx, varnumber_T number)
782{
783 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200784 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100785
Bram Moolenaar080457c2020-03-03 21:53:32 +0100786 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100787 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
788 return FAIL;
789 isn->isn_arg.number = number;
790
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200791 if (number == 0 || number == 1)
792 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200793 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200794
795 // A 0 or 1 number can also be used as a bool.
796 if (type != NULL)
797 {
798 type->tt_type = VAR_NUMBER;
799 type->tt_flags = TTFLAG_BOOL_OK;
800 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
801 }
802 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100803 return OK;
804}
805
806/*
807 * Generate an ISN_PUSHBOOL instruction.
808 */
809 static int
810generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
811{
812 isn_T *isn;
813
Bram Moolenaar080457c2020-03-03 21:53:32 +0100814 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100815 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
816 return FAIL;
817 isn->isn_arg.number = number;
818
819 return OK;
820}
821
822/*
823 * Generate an ISN_PUSHSPEC instruction.
824 */
825 static int
826generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
827{
828 isn_T *isn;
829
Bram Moolenaar080457c2020-03-03 21:53:32 +0100830 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100831 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
832 return FAIL;
833 isn->isn_arg.number = number;
834
835 return OK;
836}
837
838#ifdef FEAT_FLOAT
839/*
840 * Generate an ISN_PUSHF instruction.
841 */
842 static int
843generate_PUSHF(cctx_T *cctx, float_T fnumber)
844{
845 isn_T *isn;
846
Bram Moolenaar080457c2020-03-03 21:53:32 +0100847 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100848 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
849 return FAIL;
850 isn->isn_arg.fnumber = fnumber;
851
852 return OK;
853}
854#endif
855
856/*
857 * Generate an ISN_PUSHS instruction.
858 * Consumes "str".
859 */
860 static int
861generate_PUSHS(cctx_T *cctx, char_u *str)
862{
863 isn_T *isn;
864
Bram Moolenaar080457c2020-03-03 21:53:32 +0100865 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100866 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
867 return FAIL;
868 isn->isn_arg.string = str;
869
870 return OK;
871}
872
873/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100874 * Generate an ISN_PUSHCHANNEL instruction.
875 * Consumes "channel".
876 */
877 static int
878generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
879{
880 isn_T *isn;
881
Bram Moolenaar080457c2020-03-03 21:53:32 +0100882 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100883 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
884 return FAIL;
885 isn->isn_arg.channel = channel;
886
887 return OK;
888}
889
890/*
891 * Generate an ISN_PUSHJOB instruction.
892 * Consumes "job".
893 */
894 static int
895generate_PUSHJOB(cctx_T *cctx, job_T *job)
896{
897 isn_T *isn;
898
Bram Moolenaar080457c2020-03-03 21:53:32 +0100899 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100900 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100901 return FAIL;
902 isn->isn_arg.job = job;
903
904 return OK;
905}
906
907/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100908 * Generate an ISN_PUSHBLOB instruction.
909 * Consumes "blob".
910 */
911 static int
912generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
913{
914 isn_T *isn;
915
Bram Moolenaar080457c2020-03-03 21:53:32 +0100916 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100917 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
918 return FAIL;
919 isn->isn_arg.blob = blob;
920
921 return OK;
922}
923
924/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100925 * Generate an ISN_PUSHFUNC instruction with name "name".
926 * Consumes "name".
927 */
928 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200929generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100930{
931 isn_T *isn;
932
Bram Moolenaar080457c2020-03-03 21:53:32 +0100933 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200934 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100935 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200936 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100937
938 return OK;
939}
940
941/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200942 * Generate an ISN_GETITEM instruction with "index".
943 */
944 static int
945generate_GETITEM(cctx_T *cctx, int index)
946{
947 isn_T *isn;
948 garray_T *stack = &cctx->ctx_type_stack;
949 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
950 type_T *item_type = &t_any;
951
952 RETURN_OK_IF_SKIP(cctx);
953
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200954 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200955 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200956 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200957 emsg(_(e_listreq));
958 return FAIL;
959 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200960 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200961 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
962 return FAIL;
963 isn->isn_arg.number = index;
964
965 // add the item type to the type stack
966 if (ga_grow(stack, 1) == FAIL)
967 return FAIL;
968 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
969 ++stack->ga_len;
970 return OK;
971}
972
973/*
Bram Moolenaar9af78762020-06-16 11:34:42 +0200974 * Generate an ISN_SLICE instruction with "count".
975 */
976 static int
977generate_SLICE(cctx_T *cctx, int count)
978{
979 isn_T *isn;
980
981 RETURN_OK_IF_SKIP(cctx);
982 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
983 return FAIL;
984 isn->isn_arg.number = count;
985 return OK;
986}
987
988/*
989 * Generate an ISN_CHECKLEN instruction with "min_len".
990 */
991 static int
992generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
993{
994 isn_T *isn;
995
996 RETURN_OK_IF_SKIP(cctx);
997
998 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
999 return FAIL;
1000 isn->isn_arg.checklen.cl_min_len = min_len;
1001 isn->isn_arg.checklen.cl_more_OK = more_OK;
1002
1003 return OK;
1004}
1005
1006/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001007 * Generate an ISN_STORE instruction.
1008 */
1009 static int
1010generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1011{
1012 isn_T *isn;
1013
Bram Moolenaar080457c2020-03-03 21:53:32 +01001014 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001015 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1016 return FAIL;
1017 if (name != NULL)
1018 isn->isn_arg.string = vim_strsave(name);
1019 else
1020 isn->isn_arg.number = idx;
1021
1022 return OK;
1023}
1024
1025/*
1026 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1027 */
1028 static int
1029generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1030{
1031 isn_T *isn;
1032
Bram Moolenaar080457c2020-03-03 21:53:32 +01001033 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001034 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1035 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001036 isn->isn_arg.storenr.stnr_idx = idx;
1037 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001038
1039 return OK;
1040}
1041
1042/*
1043 * Generate an ISN_STOREOPT instruction
1044 */
1045 static int
1046generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1047{
1048 isn_T *isn;
1049
Bram Moolenaar080457c2020-03-03 21:53:32 +01001050 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001051 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001052 return FAIL;
1053 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1054 isn->isn_arg.storeopt.so_flags = opt_flags;
1055
1056 return OK;
1057}
1058
1059/*
1060 * Generate an ISN_LOAD or similar instruction.
1061 */
1062 static int
1063generate_LOAD(
1064 cctx_T *cctx,
1065 isntype_T isn_type,
1066 int idx,
1067 char_u *name,
1068 type_T *type)
1069{
1070 isn_T *isn;
1071
Bram Moolenaar080457c2020-03-03 21:53:32 +01001072 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001073 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1074 return FAIL;
1075 if (name != NULL)
1076 isn->isn_arg.string = vim_strsave(name);
1077 else
1078 isn->isn_arg.number = idx;
1079
1080 return OK;
1081}
1082
1083/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001084 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001085 */
1086 static int
1087generate_LOADV(
1088 cctx_T *cctx,
1089 char_u *name,
1090 int error)
1091{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001092 int di_flags;
1093 int vidx = find_vim_var(name, &di_flags);
1094 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001095
Bram Moolenaar080457c2020-03-03 21:53:32 +01001096 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001097 if (vidx < 0)
1098 {
1099 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001100 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001101 return FAIL;
1102 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001103 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001104
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001105 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001106}
1107
1108/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001109 * Generate an ISN_UNLET instruction.
1110 */
1111 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001112generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001113{
1114 isn_T *isn;
1115
1116 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001117 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001118 return FAIL;
1119 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1120 isn->isn_arg.unlet.ul_forceit = forceit;
1121
1122 return OK;
1123}
1124
1125/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001126 * Generate an ISN_LOCKCONST instruction.
1127 */
1128 static int
1129generate_LOCKCONST(cctx_T *cctx)
1130{
1131 isn_T *isn;
1132
1133 RETURN_OK_IF_SKIP(cctx);
1134 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1135 return FAIL;
1136 return OK;
1137}
1138
1139/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001140 * Generate an ISN_LOADS instruction.
1141 */
1142 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001143generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001144 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001145 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001146 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001147 int sid,
1148 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149{
1150 isn_T *isn;
1151
Bram Moolenaar080457c2020-03-03 21:53:32 +01001152 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001153 if (isn_type == ISN_LOADS)
1154 isn = generate_instr_type(cctx, isn_type, type);
1155 else
1156 isn = generate_instr_drop(cctx, isn_type, 1);
1157 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001158 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001159 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1160 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001161
1162 return OK;
1163}
1164
1165/*
1166 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1167 */
1168 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001169generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001170 cctx_T *cctx,
1171 isntype_T isn_type,
1172 int sid,
1173 int idx,
1174 type_T *type)
1175{
1176 isn_T *isn;
1177
Bram Moolenaar080457c2020-03-03 21:53:32 +01001178 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179 if (isn_type == ISN_LOADSCRIPT)
1180 isn = generate_instr_type(cctx, isn_type, type);
1181 else
1182 isn = generate_instr_drop(cctx, isn_type, 1);
1183 if (isn == NULL)
1184 return FAIL;
1185 isn->isn_arg.script.script_sid = sid;
1186 isn->isn_arg.script.script_idx = idx;
1187 return OK;
1188}
1189
1190/*
1191 * Generate an ISN_NEWLIST instruction.
1192 */
1193 static int
1194generate_NEWLIST(cctx_T *cctx, int count)
1195{
1196 isn_T *isn;
1197 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001198 type_T *type;
1199 type_T *member;
1200
Bram Moolenaar080457c2020-03-03 21:53:32 +01001201 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001202 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1203 return FAIL;
1204 isn->isn_arg.number = count;
1205
Bram Moolenaar127542b2020-08-09 17:22:04 +02001206 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001207 if (count == 0)
1208 member = &t_void;
1209 else
1210 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001211 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1212 cctx->ctx_type_list);
1213 type = get_list_type(member, cctx->ctx_type_list);
1214
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215 // drop the value types
1216 stack->ga_len -= count;
1217
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001218 // add the list type to the type stack
1219 if (ga_grow(stack, 1) == FAIL)
1220 return FAIL;
1221 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1222 ++stack->ga_len;
1223
1224 return OK;
1225}
1226
1227/*
1228 * Generate an ISN_NEWDICT instruction.
1229 */
1230 static int
1231generate_NEWDICT(cctx_T *cctx, int count)
1232{
1233 isn_T *isn;
1234 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001235 type_T *type;
1236 type_T *member;
1237
Bram Moolenaar080457c2020-03-03 21:53:32 +01001238 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001239 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1240 return FAIL;
1241 isn->isn_arg.number = count;
1242
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001243 if (count == 0)
1244 member = &t_void;
1245 else
1246 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001247 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1248 cctx->ctx_type_list);
1249 type = get_dict_type(member, cctx->ctx_type_list);
1250
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001251 // drop the key and value types
1252 stack->ga_len -= 2 * count;
1253
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001254 // add the dict type to the type stack
1255 if (ga_grow(stack, 1) == FAIL)
1256 return FAIL;
1257 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1258 ++stack->ga_len;
1259
1260 return OK;
1261}
1262
1263/*
1264 * Generate an ISN_FUNCREF instruction.
1265 */
1266 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001267generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268{
1269 isn_T *isn;
1270 garray_T *stack = &cctx->ctx_type_stack;
1271
Bram Moolenaar080457c2020-03-03 21:53:32 +01001272 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1274 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001275 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001276 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277
1278 if (ga_grow(stack, 1) == FAIL)
1279 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001280 ((type_T **)stack->ga_data)[stack->ga_len] =
1281 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001282 ++stack->ga_len;
1283
1284 return OK;
1285}
1286
1287/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001288 * Generate an ISN_NEWFUNC instruction.
1289 */
1290 static int
1291generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1292{
1293 isn_T *isn;
1294 char_u *name;
1295
1296 RETURN_OK_IF_SKIP(cctx);
1297 name = vim_strsave(lambda_name);
1298 if (name == NULL)
1299 return FAIL;
1300 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1301 return FAIL;
1302 isn->isn_arg.newfunc.nf_lambda = name;
1303 isn->isn_arg.newfunc.nf_global = func_name;
1304
1305 return OK;
1306}
1307
1308/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309 * Generate an ISN_JUMP instruction.
1310 */
1311 static int
1312generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1313{
1314 isn_T *isn;
1315 garray_T *stack = &cctx->ctx_type_stack;
1316
Bram Moolenaar080457c2020-03-03 21:53:32 +01001317 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1319 return FAIL;
1320 isn->isn_arg.jump.jump_when = when;
1321 isn->isn_arg.jump.jump_where = where;
1322
1323 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1324 --stack->ga_len;
1325
1326 return OK;
1327}
1328
1329 static int
1330generate_FOR(cctx_T *cctx, int loop_idx)
1331{
1332 isn_T *isn;
1333 garray_T *stack = &cctx->ctx_type_stack;
1334
Bram Moolenaar080457c2020-03-03 21:53:32 +01001335 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1337 return FAIL;
1338 isn->isn_arg.forloop.for_idx = loop_idx;
1339
1340 if (ga_grow(stack, 1) == FAIL)
1341 return FAIL;
1342 // type doesn't matter, will be stored next
1343 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1344 ++stack->ga_len;
1345
1346 return OK;
1347}
1348
1349/*
1350 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001351 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 * Return FAIL if the number of arguments is wrong.
1353 */
1354 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001355generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356{
1357 isn_T *isn;
1358 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001359 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001360 type_T *argtypes[MAX_FUNC_ARGS];
1361 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362
Bram Moolenaar080457c2020-03-03 21:53:32 +01001363 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001364 argoff = check_internal_func(func_idx, argcount);
1365 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001366 return FAIL;
1367
Bram Moolenaar389df252020-07-09 21:20:47 +02001368 if (method_call && argoff > 1)
1369 {
1370 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1371 return FAIL;
1372 isn->isn_arg.shuffle.shfl_item = argcount;
1373 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1374 }
1375
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1377 return FAIL;
1378 isn->isn_arg.bfunc.cbf_idx = func_idx;
1379 isn->isn_arg.bfunc.cbf_argcount = argcount;
1380
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001381 for (i = 0; i < argcount; ++i)
1382 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1383
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001384 stack->ga_len -= argcount; // drop the arguments
1385 if (ga_grow(stack, 1) == FAIL)
1386 return FAIL;
1387 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001388 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001389 ++stack->ga_len; // add return value
1390
1391 return OK;
1392}
1393
1394/*
1395 * Generate an ISN_DCALL or ISN_UCALL instruction.
1396 * Return FAIL if the number of arguments is wrong.
1397 */
1398 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001399generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400{
1401 isn_T *isn;
1402 garray_T *stack = &cctx->ctx_type_stack;
1403 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001404 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001405
Bram Moolenaar080457c2020-03-03 21:53:32 +01001406 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407 if (argcount > regular_args && !has_varargs(ufunc))
1408 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001409 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001410 return FAIL;
1411 }
1412 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1413 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001414 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001415 return FAIL;
1416 }
1417
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001418 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001419 {
1420 int i;
1421
1422 for (i = 0; i < argcount; ++i)
1423 {
1424 type_T *expected;
1425 type_T *actual;
1426
1427 if (i < regular_args)
1428 {
1429 if (ufunc->uf_arg_types == NULL)
1430 continue;
1431 expected = ufunc->uf_arg_types[i];
1432 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001433 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1434 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001435 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001436 else
1437 expected = ufunc->uf_va_type->tt_member;
1438 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001439 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001440 {
1441 arg_type_mismatch(expected, actual, i + 1);
1442 return FAIL;
1443 }
1444 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001445 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001446 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1447 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001448 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001449 }
1450
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001451 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001452 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001453 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001455 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001456 {
1457 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1458 isn->isn_arg.dfunc.cdf_argcount = argcount;
1459 }
1460 else
1461 {
1462 // A user function may be deleted and redefined later, can't use the
1463 // ufunc pointer, need to look it up again at runtime.
1464 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1465 isn->isn_arg.ufunc.cuf_argcount = argcount;
1466 }
1467
1468 stack->ga_len -= argcount; // drop the arguments
1469 if (ga_grow(stack, 1) == FAIL)
1470 return FAIL;
1471 // add return value
1472 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1473 ++stack->ga_len;
1474
1475 return OK;
1476}
1477
1478/*
1479 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1480 */
1481 static int
1482generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1483{
1484 isn_T *isn;
1485 garray_T *stack = &cctx->ctx_type_stack;
1486
Bram Moolenaar080457c2020-03-03 21:53:32 +01001487 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1489 return FAIL;
1490 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1491 isn->isn_arg.ufunc.cuf_argcount = argcount;
1492
1493 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001494 if (ga_grow(stack, 1) == FAIL)
1495 return FAIL;
1496 // add return value
1497 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1498 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001499
1500 return OK;
1501}
1502
1503/*
1504 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001505 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001506 */
1507 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001508generate_PCALL(
1509 cctx_T *cctx,
1510 int argcount,
1511 char_u *name,
1512 type_T *type,
1513 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001514{
1515 isn_T *isn;
1516 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001517 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518
Bram Moolenaar080457c2020-03-03 21:53:32 +01001519 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001520
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001521 if (type->tt_type == VAR_ANY)
1522 ret_type = &t_any;
1523 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001524 {
1525 if (type->tt_argcount != -1)
1526 {
1527 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1528
1529 if (argcount < type->tt_min_argcount - varargs)
1530 {
1531 semsg(_(e_toofewarg), "[reference]");
1532 return FAIL;
1533 }
1534 if (!varargs && argcount > type->tt_argcount)
1535 {
1536 semsg(_(e_toomanyarg), "[reference]");
1537 return FAIL;
1538 }
1539 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001540 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001541 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001542 else
1543 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001544 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001545 return FAIL;
1546 }
1547
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001548 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1549 return FAIL;
1550 isn->isn_arg.pfunc.cpf_top = at_top;
1551 isn->isn_arg.pfunc.cpf_argcount = argcount;
1552
1553 stack->ga_len -= argcount; // drop the arguments
1554
1555 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001556 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001558 // If partial is above the arguments it must be cleared and replaced with
1559 // the return value.
1560 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1561 return FAIL;
1562
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 return OK;
1564}
1565
1566/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001567 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568 */
1569 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001570generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001571{
1572 isn_T *isn;
1573 garray_T *stack = &cctx->ctx_type_stack;
1574 type_T *type;
1575
Bram Moolenaar080457c2020-03-03 21:53:32 +01001576 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001577 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001578 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001579 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001580
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001581 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001582 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001583 if (type->tt_type != VAR_DICT && type != &t_any)
1584 {
1585 emsg(_(e_dictreq));
1586 return FAIL;
1587 }
1588 // change dict type to dict member type
1589 if (type->tt_type == VAR_DICT)
1590 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001591
1592 return OK;
1593}
1594
1595/*
1596 * Generate an ISN_ECHO instruction.
1597 */
1598 static int
1599generate_ECHO(cctx_T *cctx, int with_white, int count)
1600{
1601 isn_T *isn;
1602
Bram Moolenaar080457c2020-03-03 21:53:32 +01001603 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1605 return FAIL;
1606 isn->isn_arg.echo.echo_with_white = with_white;
1607 isn->isn_arg.echo.echo_count = count;
1608
1609 return OK;
1610}
1611
Bram Moolenaarad39c092020-02-26 18:23:43 +01001612/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001613 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001614 */
1615 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001616generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001617{
1618 isn_T *isn;
1619
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001620 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001621 return FAIL;
1622 isn->isn_arg.number = count;
1623
1624 return OK;
1625}
1626
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001627/*
1628 * Generate an ISN_PUT instruction.
1629 */
1630 static int
1631generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1632{
1633 isn_T *isn;
1634
1635 RETURN_OK_IF_SKIP(cctx);
1636 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1637 return FAIL;
1638 isn->isn_arg.put.put_regname = regname;
1639 isn->isn_arg.put.put_lnum = lnum;
1640 return OK;
1641}
1642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001643 static int
1644generate_EXEC(cctx_T *cctx, char_u *line)
1645{
1646 isn_T *isn;
1647
Bram Moolenaar080457c2020-03-03 21:53:32 +01001648 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001649 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1650 return FAIL;
1651 isn->isn_arg.string = vim_strsave(line);
1652 return OK;
1653}
1654
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001655 static int
1656generate_EXECCONCAT(cctx_T *cctx, int count)
1657{
1658 isn_T *isn;
1659
1660 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1661 return FAIL;
1662 isn->isn_arg.number = count;
1663 return OK;
1664}
1665
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001666/*
1667 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001668 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001669 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001670 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1672{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001673 lvar_T *lvar;
1674
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001675 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001676 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001677 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001678 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001679 }
1680
1681 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001682 return NULL;
1683 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001685 // Every local variable uses the next entry on the stack. We could re-use
1686 // the last ones when leaving a scope, but then variables used in a closure
1687 // might get overwritten. To keep things simple do not re-use stack
1688 // entries. This is less efficient, but memory is cheap these days.
1689 lvar->lv_idx = cctx->ctx_locals_count++;
1690
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001691 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001692 lvar->lv_const = isConst;
1693 lvar->lv_type = type;
1694
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001695 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001696}
1697
1698/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001699 * Remove local variables above "new_top".
1700 */
1701 static void
1702unwind_locals(cctx_T *cctx, int new_top)
1703{
1704 if (cctx->ctx_locals.ga_len > new_top)
1705 {
1706 int idx;
1707 lvar_T *lvar;
1708
1709 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1710 {
1711 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1712 vim_free(lvar->lv_name);
1713 }
1714 }
1715 cctx->ctx_locals.ga_len = new_top;
1716}
1717
1718/*
1719 * Free all local variables.
1720 */
1721 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001722free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001723{
1724 unwind_locals(cctx, 0);
1725 ga_clear(&cctx->ctx_locals);
1726}
1727
1728/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001729 * Find "name" in script-local items of script "sid".
1730 * Returns the index in "sn_var_vals" if found.
1731 * If found but not in "sn_var_vals" returns -1.
1732 * If not found returns -2.
1733 */
1734 int
1735get_script_item_idx(int sid, char_u *name, int check_writable)
1736{
1737 hashtab_T *ht;
1738 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001739 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001740 int idx;
1741
1742 // First look the name up in the hashtable.
Bram Moolenaare3d46852020-08-29 13:39:17 +02001743 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001744 return -1;
1745 ht = &SCRIPT_VARS(sid);
1746 di = find_var_in_ht(ht, 0, name, TRUE);
1747 if (di == NULL)
1748 return -2;
1749
1750 // Now find the svar_T index in sn_var_vals.
1751 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1752 {
1753 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1754
1755 if (sv->sv_tv == &di->di_tv)
1756 {
1757 if (check_writable && sv->sv_const)
1758 semsg(_(e_readonlyvar), name);
1759 return idx;
1760 }
1761 }
1762 return -1;
1763}
1764
1765/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001766 * Find "name" in imported items of the current script or in "cctx" if not
1767 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768 */
1769 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001770find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001772 int idx;
1773
Bram Moolenaare3d46852020-08-29 13:39:17 +02001774 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001775 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776 if (cctx != NULL)
1777 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1778 {
1779 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1780 + idx;
1781
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001782 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1783 : STRLEN(import->imp_name) == len
1784 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785 return import;
1786 }
1787
Bram Moolenaarefa94442020-08-08 22:16:00 +02001788 return find_imported_in_script(name, len, current_sctx.sc_sid);
1789}
1790
1791 imported_T *
1792find_imported_in_script(char_u *name, size_t len, int sid)
1793{
Bram Moolenaare3d46852020-08-29 13:39:17 +02001794 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001795 int idx;
1796
Bram Moolenaare3d46852020-08-29 13:39:17 +02001797 if (!SCRIPT_ID_VALID(sid))
1798 return NULL;
1799 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001800 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1801 {
1802 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1803
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001804 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1805 : STRLEN(import->imp_name) == len
1806 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807 return import;
1808 }
1809 return NULL;
1810}
1811
1812/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001813 * Free all imported variables.
1814 */
1815 static void
1816free_imported(cctx_T *cctx)
1817{
1818 int idx;
1819
1820 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1821 {
1822 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1823
1824 vim_free(import->imp_name);
1825 }
1826 ga_clear(&cctx->ctx_imports);
1827}
1828
1829/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02001830 * Return TRUE if "p" points at a "#" but not at "#{".
1831 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001832 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001833vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001834{
1835 return p[0] == '#' && p[1] != '{';
1836}
1837
1838/*
1839 * Return a pointer to the next line that isn't empty or only contains a
1840 * comment. Skips over white space.
1841 * Returns NULL if there is none.
1842 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001843 char_u *
1844peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001845{
1846 int lnum = cctx->ctx_lnum;
1847
1848 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
1849 {
1850 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001851 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001852
Bram Moolenaarba60cc42020-08-12 19:15:33 +02001853 // ignore NULLs inserted for continuation lines
1854 if (line != NULL)
1855 {
1856 p = skipwhite(line);
1857 if (*p != NUL && !vim9_comment_start(p))
1858 return p;
1859 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02001860 }
1861 return NULL;
1862}
1863
1864/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001865 * Called when checking for a following operator at "arg". When the rest of
1866 * the line is empty or only a comment, peek the next line. If there is a next
1867 * line return a pointer to it and set "nextp".
1868 * Otherwise skip over white space.
1869 */
1870 static char_u *
1871may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
1872{
1873 char_u *p = skipwhite(arg);
1874
1875 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001876 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001877 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001878 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001879 if (*nextp != NULL)
1880 return *nextp;
1881 }
1882 return p;
1883}
1884
1885/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02001886 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02001887 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02001888 * Returns NULL when at the end.
1889 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001890 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02001891next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02001892{
Bram Moolenaar7a092242020-04-16 22:10:49 +02001893 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001894
1895 do
1896 {
1897 ++cctx->ctx_lnum;
1898 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02001899 {
1900 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001901 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02001902 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02001903 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02001904 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001905 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001906 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02001907 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02001908 return line;
1909}
1910
1911/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001912 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02001913 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001914 * Return FAIL if beyond the last line, "*arg" is unmodified then.
1915 */
1916 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02001917may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001918{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001919 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001920 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02001921 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001922
1923 if (next == NULL)
1924 return FAIL;
1925 *arg = skipwhite(next);
1926 }
1927 return OK;
1928}
1929
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001930/*
1931 * Idem, and give an error when failed.
1932 */
1933 static int
1934may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
1935{
1936 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1937 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001938 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001939 return FAIL;
1940 }
1941 return OK;
1942}
1943
1944
Bram Moolenaara5565e42020-05-09 15:44:01 +02001945// Structure passed between the compile_expr* functions to keep track of
1946// constants that have been parsed but for which no code was produced yet. If
1947// possible expressions on these constants are applied at compile time. If
1948// that is not possible, the code to push the constants needs to be generated
1949// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02001950// Using 50 should be more than enough of 5 levels of ().
1951#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02001952typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02001953 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02001954 int pp_used; // active entries in pp_tv[]
1955} ppconst_T;
1956
Bram Moolenaar1c747212020-05-09 18:28:34 +02001957static int compile_expr0(char_u **arg, cctx_T *cctx);
1958static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1959
Bram Moolenaara5565e42020-05-09 15:44:01 +02001960/*
1961 * Generate a PUSH instruction for "tv".
1962 * "tv" will be consumed or cleared.
1963 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
1964 */
1965 static int
1966generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
1967{
1968 if (tv != NULL)
1969 {
1970 switch (tv->v_type)
1971 {
1972 case VAR_UNKNOWN:
1973 break;
1974 case VAR_BOOL:
1975 generate_PUSHBOOL(cctx, tv->vval.v_number);
1976 break;
1977 case VAR_SPECIAL:
1978 generate_PUSHSPEC(cctx, tv->vval.v_number);
1979 break;
1980 case VAR_NUMBER:
1981 generate_PUSHNR(cctx, tv->vval.v_number);
1982 break;
1983#ifdef FEAT_FLOAT
1984 case VAR_FLOAT:
1985 generate_PUSHF(cctx, tv->vval.v_float);
1986 break;
1987#endif
1988 case VAR_BLOB:
1989 generate_PUSHBLOB(cctx, tv->vval.v_blob);
1990 tv->vval.v_blob = NULL;
1991 break;
1992 case VAR_STRING:
1993 generate_PUSHS(cctx, tv->vval.v_string);
1994 tv->vval.v_string = NULL;
1995 break;
1996 default:
1997 iemsg("constant type not supported");
1998 clear_tv(tv);
1999 return FAIL;
2000 }
2001 tv->v_type = VAR_UNKNOWN;
2002 }
2003 return OK;
2004}
2005
2006/*
2007 * Generate code for any ppconst entries.
2008 */
2009 static int
2010generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2011{
2012 int i;
2013 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002014 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002015
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002016 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002017 for (i = 0; i < ppconst->pp_used; ++i)
2018 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2019 ret = FAIL;
2020 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002021 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002022 return ret;
2023}
2024
2025/*
2026 * Clear ppconst constants. Used when failing.
2027 */
2028 static void
2029clear_ppconst(ppconst_T *ppconst)
2030{
2031 int i;
2032
2033 for (i = 0; i < ppconst->pp_used; ++i)
2034 clear_tv(&ppconst->pp_tv[i]);
2035 ppconst->pp_used = 0;
2036}
2037
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002038/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002039 * Generate an instruction to load script-local variable "name", without the
2040 * leading "s:".
2041 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042 */
2043 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002044compile_load_scriptvar(
2045 cctx_T *cctx,
2046 char_u *name, // variable NUL terminated
2047 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002048 char_u **end, // end of variable
2049 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002050{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002051 scriptitem_T *si;
2052 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002053 imported_T *import;
2054
Bram Moolenaare3d46852020-08-29 13:39:17 +02002055 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2056 return FAIL;
2057 si = SCRIPT_ITEM(current_sctx.sc_sid);
2058 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002059 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002060 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002061 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002062 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2063 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064 }
2065 if (idx >= 0)
2066 {
2067 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2068
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002069 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070 current_sctx.sc_sid, idx, sv->sv_type);
2071 return OK;
2072 }
2073
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002074 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002075 if (import != NULL)
2076 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002077 if (import->imp_all)
2078 {
2079 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002080 char_u *exp_name;
2081 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002082 ufunc_T *ufunc;
2083 type_T *type;
2084
2085 // Used "import * as Name", need to lookup the member.
2086 if (*p != '.')
2087 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002088 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002089 return FAIL;
2090 }
2091 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002092 if (VIM_ISWHITE(*p))
2093 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002094 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002095 return FAIL;
2096 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002097
Bram Moolenaar1c991142020-07-04 13:15:31 +02002098 // isolate one name
2099 exp_name = p;
2100 while (eval_isnamec(*p))
2101 ++p;
2102 cc = *p;
2103 *p = NUL;
2104
2105 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2106 *p = cc;
2107 p = skipwhite(p);
2108
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002109 // TODO: what if it is a function?
2110 if (idx < 0)
2111 return FAIL;
2112 *end = p;
2113
2114 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2115 import->imp_sid,
2116 idx,
2117 type);
2118 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002119 else if (import->imp_funcname != NULL)
2120 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002121 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002122 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2123 import->imp_sid,
2124 import->imp_var_vals_idx,
2125 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002126 return OK;
2127 }
2128
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002129 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002130 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131 return FAIL;
2132}
2133
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002134 static int
2135generate_funcref(cctx_T *cctx, char_u *name)
2136{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002137 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002138
2139 if (ufunc == NULL)
2140 return FAIL;
2141
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002142 // Need to compile any default values to get the argument types.
2143 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2144 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2145 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002146 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002147}
2148
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002149/*
2150 * Compile a variable name into a load instruction.
2151 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002152 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002153 * When "error" is FALSE do not give an error when not found.
2154 */
2155 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002156compile_load(
2157 char_u **arg,
2158 char_u *end_arg,
2159 cctx_T *cctx,
2160 int is_expr,
2161 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162{
2163 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002164 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002165 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002166 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002167 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002168
2169 if (*(*arg + 1) == ':')
2170 {
2171 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002172 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002173 {
2174 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002176 switch (**arg)
2177 {
2178 case 'g': isn_type = ISN_LOADGDICT; break;
2179 case 'w': isn_type = ISN_LOADWDICT; break;
2180 case 't': isn_type = ISN_LOADTDICT; break;
2181 case 'b': isn_type = ISN_LOADBDICT; break;
2182 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002183 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002184 goto theend;
2185 }
2186 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2187 goto theend;
2188 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002189 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002190 else
2191 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002192 isntype_T isn_type = ISN_DROP;
2193
2194 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2195 if (name == NULL)
2196 return FAIL;
2197
2198 switch (**arg)
2199 {
2200 case 'v': res = generate_LOADV(cctx, name, error);
2201 break;
2202 case 's': res = compile_load_scriptvar(cctx, name,
2203 NULL, NULL, error);
2204 break;
2205 case 'g': isn_type = ISN_LOADG; break;
2206 case 'w': isn_type = ISN_LOADW; break;
2207 case 't': isn_type = ISN_LOADT; break;
2208 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002209 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002210 goto theend;
2211 }
2212 if (isn_type != ISN_DROP)
2213 {
2214 // Global, Buffer-local, Window-local and Tabpage-local
2215 // variables can be defined later, thus we don't check if it
2216 // exists, give error at runtime.
2217 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2218 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002219 }
2220 }
2221 else
2222 {
2223 size_t len = end - *arg;
2224 int idx;
2225 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002226 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002227
2228 name = vim_strnsave(*arg, end - *arg);
2229 if (name == NULL)
2230 return FAIL;
2231
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002232 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002233 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002234 if (!gen_load_outer)
2235 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002236 }
2237 else
2238 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002239 lvar_T *lvar = lookup_local(*arg, len, cctx);
2240
2241 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002242 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002243 type = lvar->lv_type;
2244 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002245 if (lvar->lv_from_outer)
2246 gen_load_outer = TRUE;
2247 else
2248 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002249 }
2250 else
2251 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002252 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002253 // already exists in a Vim9 script or when it's imported.
2254 if (lookup_script(*arg, len, TRUE) == OK
2255 || find_imported(name, 0, cctx) != NULL)
2256 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002257
Bram Moolenaar0f769812020-09-12 18:32:34 +02002258 // When evaluating an expression and the name starts with an
2259 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002260 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002261 if (res == FAIL && is_expr
2262 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002263 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264 }
2265 }
2266 if (gen_load)
2267 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002268 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002269 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002270 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002271 cctx->ctx_outer_used = TRUE;
2272 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002273 }
2274
2275 *arg = end;
2276
2277theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002278 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002279 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002280 vim_free(name);
2281 return res;
2282}
2283
2284/*
2285 * Compile the argument expressions.
2286 * "arg" points to just after the "(" and is advanced to after the ")"
2287 */
2288 static int
2289compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2290{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002291 char_u *p = *arg;
2292 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002293 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002294
Bram Moolenaare6085c52020-04-12 20:19:16 +02002295 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002296 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002297 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2298 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002299 if (*p == ')')
2300 {
2301 *arg = p + 1;
2302 return OK;
2303 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002304 if (must_end)
2305 {
2306 semsg(_(e_missing_comma_before_argument_str), p);
2307 return FAIL;
2308 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002309
Bram Moolenaara5565e42020-05-09 15:44:01 +02002310 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311 return FAIL;
2312 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002313
2314 if (*p != ',' && *skipwhite(p) == ',')
2315 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002316 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002317 p = skipwhite(p);
2318 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002319 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002320 {
2321 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002322 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002323 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002324 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002325 else
2326 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002327 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002328 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002329 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002330failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002331 emsg(_(e_missing_close));
2332 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333}
2334
2335/*
2336 * Compile a function call: name(arg1, arg2)
2337 * "arg" points to "name", "arg + varlen" to the "(".
2338 * "argcount_init" is 1 for "value->method()"
2339 * Instructions:
2340 * EVAL arg1
2341 * EVAL arg2
2342 * BCALL / DCALL / UCALL
2343 */
2344 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002345compile_call(
2346 char_u **arg,
2347 size_t varlen,
2348 cctx_T *cctx,
2349 ppconst_T *ppconst,
2350 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002351{
2352 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002353 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002354 int argcount = argcount_init;
2355 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002356 char_u fname_buf[FLEN_FIXED + 1];
2357 char_u *tofree = NULL;
2358 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002359 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002360 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002361 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002362
Bram Moolenaara5565e42020-05-09 15:44:01 +02002363 // we can evaluate "has('name')" at compile time
2364 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2365 {
2366 char_u *s = skipwhite(*arg + varlen + 1);
2367 typval_T argvars[2];
2368
2369 argvars[0].v_type = VAR_UNKNOWN;
2370 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002371 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002372 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002373 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002374 s = skipwhite(s);
2375 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2376 {
2377 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2378
2379 *arg = s + 1;
2380 argvars[1].v_type = VAR_UNKNOWN;
2381 tv->v_type = VAR_NUMBER;
2382 tv->vval.v_number = 0;
2383 f_has(argvars, tv);
2384 clear_tv(&argvars[0]);
2385 ++ppconst->pp_used;
2386 return OK;
2387 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002388 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002389 }
2390
2391 if (generate_ppconst(cctx, ppconst) == FAIL)
2392 return FAIL;
2393
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394 if (varlen >= sizeof(namebuf))
2395 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002396 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397 return FAIL;
2398 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002399 vim_strncpy(namebuf, *arg, varlen);
2400 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401
2402 *arg = skipwhite(*arg + varlen + 1);
2403 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002404 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405
Bram Moolenaara1773442020-08-12 15:21:22 +02002406 is_autoload = vim_strchr(name, '#') != NULL;
2407 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002408 {
2409 int idx;
2410
2411 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002412 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002414 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002415 else
2416 semsg(_(e_unknownfunc), namebuf);
2417 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002418 }
2419
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002420 // If we can find the function by name generate the right call.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002421 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002422 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar0f769812020-09-12 18:32:34 +02002423 if (ufunc != NULL && !func_is_global(ufunc))
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002424 {
2425 res = generate_CALL(cctx, ufunc, argcount);
2426 goto theend;
2427 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428
2429 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002430 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002431 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002433 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002434 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002435 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002436 garray_T *stack = &cctx->ctx_type_stack;
2437 type_T *type;
2438
2439 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2440 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002441 goto theend;
2442 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443
Bram Moolenaar0f769812020-09-12 18:32:34 +02002444 // If we can find a global function by name generate the right call.
2445 if (ufunc != NULL)
2446 {
2447 res = generate_CALL(cctx, ufunc, argcount);
2448 goto theend;
2449 }
2450
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002451 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002452 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002453 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002454 res = generate_UCALL(cctx, name, argcount);
2455 else
2456 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002457
2458theend:
2459 vim_free(tofree);
2460 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002461}
2462
2463// like NAMESPACE_CHAR but with 'a' and 'l'.
2464#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2465
2466/*
2467 * Find the end of a variable or function name. Unlike find_name_end() this
2468 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002469 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470 * Return a pointer to just after the name. Equal to "arg" if there is no
2471 * valid name.
2472 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002473 static char_u *
2474to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475{
2476 char_u *p;
2477
2478 // Quick check for valid starting character.
2479 if (!eval_isnamec1(*arg))
2480 return arg;
2481
2482 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2483 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2484 // and can be used in slice "[n:]".
2485 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002486 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2488 break;
2489 return p;
2490}
2491
2492/*
2493 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002494 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 */
2496 char_u *
2497to_name_const_end(char_u *arg)
2498{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002499 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002500 typval_T rettv;
2501
2502 if (p == arg && *arg == '[')
2503 {
2504
2505 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002506 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507 p = arg;
2508 }
2509 else if (p == arg && *arg == '#' && arg[1] == '{')
2510 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002511 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002513 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 p = arg;
2515 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516
2517 return p;
2518}
2519
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520/*
2521 * parse a list: [expr, expr]
2522 * "*arg" points to the '['.
2523 */
2524 static int
2525compile_list(char_u **arg, cctx_T *cctx)
2526{
2527 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002528 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 int count = 0;
2530
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002531 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002533 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002534 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002535 semsg(_(e_list_end), *arg);
2536 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002537 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002538 if (*p == ',')
2539 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002540 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002541 return FAIL;
2542 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002543 if (*p == ']')
2544 {
2545 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002546 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002547 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002548 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 break;
2550 ++count;
2551 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002552 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002554 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2555 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002556 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002557 return FAIL;
2558 }
2559 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002560 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561 p = skipwhite(p);
2562 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002563 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564
2565 generate_NEWLIST(cctx, count);
2566 return OK;
2567}
2568
2569/*
2570 * parse a lambda: {arg, arg -> expr}
2571 * "*arg" points to the '{'.
2572 */
2573 static int
2574compile_lambda(char_u **arg, cctx_T *cctx)
2575{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002576 typval_T rettv;
2577 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002578 evalarg_T evalarg;
2579
2580 CLEAR_FIELD(evalarg);
2581 evalarg.eval_flags = EVAL_EVALUATE;
2582 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002583
2584 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002585 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002587
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002589 ++ufunc->uf_refcount;
2590 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002591 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002592
2593 // The function will have one line: "return {expr}".
2594 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002595 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002597 clear_evalarg(&evalarg, NULL);
2598
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002599 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002600 {
2601 // The return type will now be known.
2602 set_function_type(ufunc);
2603
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002604 // The function reference count will be 1. When the ISN_FUNCREF
2605 // instruction is deleted the reference count is decremented and the
2606 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002607 return generate_FUNCREF(cctx, ufunc);
2608 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002609
2610 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611 return FAIL;
2612}
2613
2614/*
2615 * Compile a lamda call: expr->{lambda}(args)
2616 * "arg" points to the "{".
2617 */
2618 static int
2619compile_lambda_call(char_u **arg, cctx_T *cctx)
2620{
2621 ufunc_T *ufunc;
2622 typval_T rettv;
2623 int argcount = 1;
2624 int ret = FAIL;
2625
2626 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002627 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628 return FAIL;
2629
2630 if (**arg != '(')
2631 {
2632 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002633 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 else
2635 semsg(_(e_missing_paren), "lambda");
2636 clear_tv(&rettv);
2637 return FAIL;
2638 }
2639
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640 ufunc = rettv.vval.v_partial->pt_func;
2641 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002642 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002643 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002644
Bram Moolenaara05e5242020-09-19 18:19:19 +02002645 // The function will have one line: "return {expr}". Compile it into
2646 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002647 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648
2649 // compile the arguments
2650 *arg = skipwhite(*arg + 1);
2651 if (compile_arguments(arg, cctx, &argcount) == OK)
2652 // call the compiled function
2653 ret = generate_CALL(cctx, ufunc, argcount);
2654
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002655 if (ret == FAIL)
2656 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657 return ret;
2658}
2659
2660/*
2661 * parse a dict: {'key': val} or #{key: val}
2662 * "*arg" points to the '{'.
2663 */
2664 static int
2665compile_dict(char_u **arg, cctx_T *cctx, int literal)
2666{
2667 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002668 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002669 int count = 0;
2670 dict_T *d = dict_alloc();
2671 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002672 char_u *whitep = *arg;
2673 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002674
2675 if (d == NULL)
2676 return FAIL;
2677 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002678 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002679 {
2680 char_u *key = NULL;
2681
Bram Moolenaar23c55272020-06-21 16:58:13 +02002682 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002683 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002684 *arg = NULL;
2685 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002686 }
2687
2688 if (**arg == '}')
2689 break;
2690
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002691 if (literal)
2692 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002693 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002694
Bram Moolenaar2c330432020-04-13 14:41:35 +02002695 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002696 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002697 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002698 return FAIL;
2699 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002700 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002701 if (generate_PUSHS(cctx, key) == FAIL)
2702 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002703 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704 }
2705 else
2706 {
2707 isn_T *isn;
2708
Bram Moolenaara5565e42020-05-09 15:44:01 +02002709 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2712 if (isn->isn_type == ISN_PUSHS)
2713 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002714 else
2715 {
2716 type_T *keytype = ((type_T **)stack->ga_data)
2717 [stack->ga_len - 1];
2718 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2719 return FAIL;
2720 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002721 }
2722
2723 // Check for duplicate keys, if using string keys.
2724 if (key != NULL)
2725 {
2726 item = dict_find(d, key, -1);
2727 if (item != NULL)
2728 {
2729 semsg(_(e_duplicate_key), key);
2730 goto failret;
2731 }
2732 item = dictitem_alloc(key);
2733 if (item != NULL)
2734 {
2735 item->di_tv.v_type = VAR_UNKNOWN;
2736 item->di_tv.v_lock = 0;
2737 if (dict_add(d, item) == FAIL)
2738 dictitem_free(item);
2739 }
2740 }
2741
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742 if (**arg != ':')
2743 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002744 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002745 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002746 else
2747 semsg(_(e_missing_dict_colon), *arg);
2748 return FAIL;
2749 }
2750 whitep = *arg + 1;
2751 if (!IS_WHITE_OR_NUL(*whitep))
2752 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002753 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002754 return FAIL;
2755 }
2756
2757 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002758 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002759 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002760 *arg = NULL;
2761 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002762 }
2763
Bram Moolenaara5565e42020-05-09 15:44:01 +02002764 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765 return FAIL;
2766 ++count;
2767
Bram Moolenaar2c330432020-04-13 14:41:35 +02002768 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002769 *arg = skipwhite(*arg);
2770 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002771 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002772 *arg = NULL;
2773 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002774 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775 if (**arg == '}')
2776 break;
2777 if (**arg != ',')
2778 {
2779 semsg(_(e_missing_dict_comma), *arg);
2780 goto failret;
2781 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002782 if (IS_WHITE_OR_NUL(*whitep))
2783 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002784 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002785 return FAIL;
2786 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002787 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 *arg = skipwhite(*arg + 1);
2789 }
2790
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002791 *arg = *arg + 1;
2792
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002793 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002794 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002795 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002796 *arg += STRLEN(*arg);
2797
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 dict_unref(d);
2799 return generate_NEWDICT(cctx, count);
2800
2801failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002802 if (*arg == NULL)
2803 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 dict_unref(d);
2805 return FAIL;
2806}
2807
2808/*
2809 * Compile "&option".
2810 */
2811 static int
2812compile_get_option(char_u **arg, cctx_T *cctx)
2813{
2814 typval_T rettv;
2815 char_u *start = *arg;
2816 int ret;
2817
2818 // parse the option and get the current value to get the type.
2819 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002820 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821 if (ret == OK)
2822 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002823 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824 char_u *name = vim_strnsave(start, *arg - start);
2825 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2826
2827 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2828 vim_free(name);
2829 }
2830 clear_tv(&rettv);
2831
2832 return ret;
2833}
2834
2835/*
2836 * Compile "$VAR".
2837 */
2838 static int
2839compile_get_env(char_u **arg, cctx_T *cctx)
2840{
2841 char_u *start = *arg;
2842 int len;
2843 int ret;
2844 char_u *name;
2845
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846 ++*arg;
2847 len = get_env_len(arg);
2848 if (len == 0)
2849 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002850 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 return FAIL;
2852 }
2853
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002854 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855 name = vim_strnsave(start, len + 1);
2856 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2857 vim_free(name);
2858 return ret;
2859}
2860
2861/*
2862 * Compile "@r".
2863 */
2864 static int
2865compile_get_register(char_u **arg, cctx_T *cctx)
2866{
2867 int ret;
2868
2869 ++*arg;
2870 if (**arg == NUL)
2871 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002872 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873 return FAIL;
2874 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002875 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002876 {
2877 emsg_invreg(**arg);
2878 return FAIL;
2879 }
2880 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2881 ++*arg;
2882 return ret;
2883}
2884
2885/*
2886 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002887 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888 */
2889 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002890apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002892 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893
2894 // this works from end to start
2895 while (p > start)
2896 {
2897 --p;
2898 if (*p == '-' || *p == '+')
2899 {
2900 // only '-' has an effect, for '+' we only check the type
2901#ifdef FEAT_FLOAT
2902 if (rettv->v_type == VAR_FLOAT)
2903 {
2904 if (*p == '-')
2905 rettv->vval.v_float = -rettv->vval.v_float;
2906 }
2907 else
2908#endif
2909 {
2910 varnumber_T val;
2911 int error = FALSE;
2912
2913 // tv_get_number_chk() accepts a string, but we don't want that
2914 // here
2915 if (check_not_string(rettv) == FAIL)
2916 return FAIL;
2917 val = tv_get_number_chk(rettv, &error);
2918 clear_tv(rettv);
2919 if (error)
2920 return FAIL;
2921 if (*p == '-')
2922 val = -val;
2923 rettv->v_type = VAR_NUMBER;
2924 rettv->vval.v_number = val;
2925 }
2926 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002927 else if (numeric_only)
2928 {
2929 ++p;
2930 break;
2931 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 else
2933 {
2934 int v = tv2bool(rettv);
2935
2936 // '!' is permissive in the type.
2937 clear_tv(rettv);
2938 rettv->v_type = VAR_BOOL;
2939 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2940 }
2941 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002942 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943 return OK;
2944}
2945
2946/*
2947 * Recognize v: variables that are constants and set "rettv".
2948 */
2949 static void
2950get_vim_constant(char_u **arg, typval_T *rettv)
2951{
2952 if (STRNCMP(*arg, "v:true", 6) == 0)
2953 {
2954 rettv->v_type = VAR_BOOL;
2955 rettv->vval.v_number = VVAL_TRUE;
2956 *arg += 6;
2957 }
2958 else if (STRNCMP(*arg, "v:false", 7) == 0)
2959 {
2960 rettv->v_type = VAR_BOOL;
2961 rettv->vval.v_number = VVAL_FALSE;
2962 *arg += 7;
2963 }
2964 else if (STRNCMP(*arg, "v:null", 6) == 0)
2965 {
2966 rettv->v_type = VAR_SPECIAL;
2967 rettv->vval.v_number = VVAL_NULL;
2968 *arg += 6;
2969 }
2970 else if (STRNCMP(*arg, "v:none", 6) == 0)
2971 {
2972 rettv->v_type = VAR_SPECIAL;
2973 rettv->vval.v_number = VVAL_NONE;
2974 *arg += 6;
2975 }
2976}
2977
Bram Moolenaar696ba232020-07-29 21:20:41 +02002978 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02002979get_compare_type(char_u *p, int *len, int *type_is)
2980{
2981 exptype_T type = EXPR_UNKNOWN;
2982 int i;
2983
2984 switch (p[0])
2985 {
2986 case '=': if (p[1] == '=')
2987 type = EXPR_EQUAL;
2988 else if (p[1] == '~')
2989 type = EXPR_MATCH;
2990 break;
2991 case '!': if (p[1] == '=')
2992 type = EXPR_NEQUAL;
2993 else if (p[1] == '~')
2994 type = EXPR_NOMATCH;
2995 break;
2996 case '>': if (p[1] != '=')
2997 {
2998 type = EXPR_GREATER;
2999 *len = 1;
3000 }
3001 else
3002 type = EXPR_GEQUAL;
3003 break;
3004 case '<': if (p[1] != '=')
3005 {
3006 type = EXPR_SMALLER;
3007 *len = 1;
3008 }
3009 else
3010 type = EXPR_SEQUAL;
3011 break;
3012 case 'i': if (p[1] == 's')
3013 {
3014 // "is" and "isnot"; but not a prefix of a name
3015 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3016 *len = 5;
3017 i = p[*len];
3018 if (!isalnum(i) && i != '_')
3019 {
3020 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3021 *type_is = TRUE;
3022 }
3023 }
3024 break;
3025 }
3026 return type;
3027}
3028
3029/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003031 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 */
3033 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003034compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003036 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037
3038 // this works from end to start
3039 while (p > start)
3040 {
3041 --p;
3042 if (*p == '-' || *p == '+')
3043 {
3044 int negate = *p == '-';
3045 isn_T *isn;
3046
3047 // TODO: check type
3048 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3049 {
3050 --p;
3051 if (*p == '-')
3052 negate = !negate;
3053 }
3054 // only '-' has an effect, for '+' we only check the type
3055 if (negate)
3056 isn = generate_instr(cctx, ISN_NEGATENR);
3057 else
3058 isn = generate_instr(cctx, ISN_CHECKNR);
3059 if (isn == NULL)
3060 return FAIL;
3061 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003062 else if (numeric_only)
3063 {
3064 ++p;
3065 break;
3066 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 else
3068 {
3069 int invert = TRUE;
3070
3071 while (p > start && p[-1] == '!')
3072 {
3073 --p;
3074 invert = !invert;
3075 }
3076 if (generate_2BOOL(cctx, invert) == FAIL)
3077 return FAIL;
3078 }
3079 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003080 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081 return OK;
3082}
3083
3084/*
3085 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003086 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087 */
3088 static int
3089compile_subscript(
3090 char_u **arg,
3091 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003092 char_u *start_leader,
3093 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003094 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003095{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003096 char_u *name_start = *end_leader;
3097
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 for (;;)
3099 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003100 char_u *p = skipwhite(*arg);
3101
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003102 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003103 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003104 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003105
3106 // If a following line starts with "->{" or "->X" advance to that
3107 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003108 // Also if a following line starts with ".x".
3109 if (next != NULL &&
3110 ((next[0] == '-' && next[1] == '>'
3111 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003112 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003113 {
3114 next = next_line_from_context(cctx, TRUE);
3115 if (next == NULL)
3116 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003117 *arg = next;
3118 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003119 }
3120 }
3121
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003122 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3123 // not a function call.
3124 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003126 garray_T *stack = &cctx->ctx_type_stack;
3127 type_T *type;
3128 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003130 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003131 return FAIL;
3132
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003134 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3135
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003136 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3138 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003139 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140 return FAIL;
3141 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003142 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003144 char_u *pstart = p;
3145
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003146 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003147 return FAIL;
3148
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 // something->method()
3150 // Apply the '!', '-' and '+' first:
3151 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003152 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003153 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003155 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003156 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003157 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 if (**arg == '{')
3159 {
3160 // lambda call: list->{lambda}
3161 if (compile_lambda_call(arg, cctx) == FAIL)
3162 return FAIL;
3163 }
3164 else
3165 {
3166 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003167 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003168 if (!eval_isnamec1(*p))
3169 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003170 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003171 return FAIL;
3172 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003173 if (ASCII_ISALPHA(*p) && p[1] == ':')
3174 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003175 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 ;
3177 if (*p != '(')
3178 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003179 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 return FAIL;
3181 }
3182 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003183 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 return FAIL;
3185 }
3186 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003187 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003189 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003190 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003191 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003192 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003193 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003194
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003195 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003196 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003197 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003198 // TODO: blob index
3199 // TODO: more arguments
3200 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003201 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003202 return FAIL;
3203
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003204 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003205 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003206 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003207 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003208 if (**arg == ':')
3209 // missing first index is equal to zero
3210 generate_PUSHNR(cctx, 0);
3211 else
3212 {
3213 if (compile_expr0(arg, cctx) == FAIL)
3214 return FAIL;
3215 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3216 return FAIL;
3217 *arg = skipwhite(*arg);
3218 }
3219 if (**arg == ':')
3220 {
3221 *arg = skipwhite(*arg + 1);
3222 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3223 return FAIL;
3224 if (**arg == ']')
3225 // missing second index is equal to end of string
3226 generate_PUSHNR(cctx, -1);
3227 else
3228 {
3229 if (compile_expr0(arg, cctx) == FAIL)
3230 return FAIL;
3231 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3232 return FAIL;
3233 *arg = skipwhite(*arg);
3234 }
3235 is_slice = TRUE;
3236 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237
3238 if (**arg != ']')
3239 {
3240 emsg(_(e_missbrac));
3241 return FAIL;
3242 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003243 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003245 // We can index a list and a dict. If we don't know the type
3246 // we can use the index value type.
3247 // TODO: If we don't know use an instruction to figure it out at
3248 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003249 typep = ((type_T **)stack->ga_data) + stack->ga_len
3250 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003251 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003252 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3253 // If the index is a string, the variable must be a Dict.
3254 if (*typep == &t_any && valtype == &t_string)
3255 vtype = VAR_DICT;
3256 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003257 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003258 if (need_type(valtype, &t_number, -1, cctx, FALSE) == FAIL)
3259 return FAIL;
3260 if (is_slice)
3261 {
3262 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
3263 if (need_type(valtype, &t_number, -2, cctx, FALSE) == FAIL)
3264 return FAIL;
3265 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003266 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003267
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003268 if (vtype == VAR_DICT)
3269 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003270 if (is_slice)
3271 {
3272 emsg(_(e_cannot_slice_dictionary));
3273 return FAIL;
3274 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003275 if ((*typep)->tt_type == VAR_DICT)
3276 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003277 else
3278 {
3279 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3280 return FAIL;
3281 *typep = &t_any;
3282 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003283 if (may_generate_2STRING(-1, cctx) == FAIL)
3284 return FAIL;
3285 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3286 return FAIL;
3287 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003288 else if (vtype == VAR_STRING)
3289 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003290 *typep = &t_string;
3291 if ((is_slice
3292 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3293 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003294 return FAIL;
3295 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003296 else if (vtype == VAR_BLOB)
3297 {
3298 emsg("Sorry, blob index and slice not implemented yet");
3299 return FAIL;
3300 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003301 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003302 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003303 if (is_slice)
3304 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003305 if (generate_instr_drop(cctx,
3306 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3307 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003308 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003309 }
Bram Moolenaared591872020-08-15 22:14:53 +02003310 else
3311 {
3312 if ((*typep)->tt_type == VAR_LIST)
3313 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003314 if (generate_instr_drop(cctx,
3315 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3316 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003317 return FAIL;
3318 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003319 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003320 else
3321 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003322 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003323 return FAIL;
3324 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003325 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003326 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003328 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003329 return FAIL;
3330
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003331 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003332 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3333 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003334 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003335 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003336 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003337 while (eval_isnamec(*p))
3338 MB_PTR_ADV(p);
3339 if (p == *arg)
3340 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003341 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003342 return FAIL;
3343 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003344 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003345 return FAIL;
3346 *arg = p;
3347 }
3348 else
3349 break;
3350 }
3351
3352 // TODO - see handle_subscript():
3353 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3354 // Don't do this when "Func" is already a partial that was bound
3355 // explicitly (pt_auto is FALSE).
3356
3357 return OK;
3358}
3359
3360/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003361 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3362 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003364 * If the value is a constant "ppconst->pp_ret" will be set.
3365 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003366 *
3367 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 */
3369
3370/*
3371 * number number constant
3372 * 0zFFFFFFFF Blob constant
3373 * "string" string constant
3374 * 'string' literal string constant
3375 * &option-name option value
3376 * @r register contents
3377 * identifier variable value
3378 * function() function call
3379 * $VAR environment variable
3380 * (expression) nested expression
3381 * [expr, expr] List
3382 * {key: val, key: val} Dictionary
3383 * #{key: val, key: val} Dictionary with literal keys
3384 *
3385 * Also handle:
3386 * ! in front logical NOT
3387 * - in front unary minus
3388 * + in front unary plus (ignored)
3389 * trailing (arg) funcref/partial call
3390 * trailing [] subscript in String or List
3391 * trailing .name entry in Dictionary
3392 * trailing ->name() method call
3393 */
3394 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003395compile_expr7(
3396 char_u **arg,
3397 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003398 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003400 char_u *start_leader, *end_leader;
3401 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003402 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003403 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003404
3405 /*
3406 * Skip '!', '-' and '+' characters. They are handled later.
3407 */
3408 start_leader = *arg;
3409 while (**arg == '!' || **arg == '-' || **arg == '+')
3410 *arg = skipwhite(*arg + 1);
3411 end_leader = *arg;
3412
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003413 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414 switch (**arg)
3415 {
3416 /*
3417 * Number constant.
3418 */
3419 case '0': // also for blob starting with 0z
3420 case '1':
3421 case '2':
3422 case '3':
3423 case '4':
3424 case '5':
3425 case '6':
3426 case '7':
3427 case '8':
3428 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003429 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003431 // Apply "-" and "+" just before the number now, right to
3432 // left. Matters especially when "->" follows. Stops at
3433 // '!'.
3434 if (apply_leader(rettv, TRUE,
3435 start_leader, &end_leader) == FAIL)
3436 {
3437 clear_tv(rettv);
3438 return FAIL;
3439 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 break;
3441
3442 /*
3443 * String constant: "string".
3444 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003445 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 return FAIL;
3447 break;
3448
3449 /*
3450 * Literal string constant: 'str''ing'.
3451 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003452 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 return FAIL;
3454 break;
3455
3456 /*
3457 * Constant Vim variable.
3458 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003459 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460 ret = NOTDONE;
3461 break;
3462
3463 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003464 * "true" constant
3465 */
3466 case 't': if (STRNCMP(*arg, "true", 4) == 0
3467 && !eval_isnamec((*arg)[4]))
3468 {
3469 *arg += 4;
3470 rettv->v_type = VAR_BOOL;
3471 rettv->vval.v_number = VVAL_TRUE;
3472 }
3473 else
3474 ret = NOTDONE;
3475 break;
3476
3477 /*
3478 * "false" constant
3479 */
3480 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3481 && !eval_isnamec((*arg)[5]))
3482 {
3483 *arg += 5;
3484 rettv->v_type = VAR_BOOL;
3485 rettv->vval.v_number = VVAL_FALSE;
3486 }
3487 else
3488 ret = NOTDONE;
3489 break;
3490
3491 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003492 * List: [expr, expr]
3493 */
3494 case '[': ret = compile_list(arg, cctx);
3495 break;
3496
3497 /*
3498 * Dictionary: #{key: val, key: val}
3499 */
3500 case '#': if ((*arg)[1] == '{')
3501 {
3502 ++*arg;
3503 ret = compile_dict(arg, cctx, TRUE);
3504 }
3505 else
3506 ret = NOTDONE;
3507 break;
3508
3509 /*
3510 * Lambda: {arg, arg -> expr}
3511 * Dictionary: {'key': val, 'key': val}
3512 */
3513 case '{': {
3514 char_u *start = skipwhite(*arg + 1);
3515
3516 // Find out what comes after the arguments.
3517 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003518 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 if (ret != FAIL && *start == '>')
3520 ret = compile_lambda(arg, cctx);
3521 else
3522 ret = compile_dict(arg, cctx, FALSE);
3523 }
3524 break;
3525
3526 /*
3527 * Option value: &name
3528 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003529 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3530 return FAIL;
3531 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532 break;
3533
3534 /*
3535 * Environment variable: $VAR.
3536 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003537 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3538 return FAIL;
3539 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540 break;
3541
3542 /*
3543 * Register contents: @r.
3544 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003545 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3546 return FAIL;
3547 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548 break;
3549 /*
3550 * nested expression: (expression).
3551 */
3552 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003553
3554 // recursive!
3555 if (ppconst->pp_used <= PPSIZE - 10)
3556 {
3557 ret = compile_expr1(arg, cctx, ppconst);
3558 }
3559 else
3560 {
3561 // Not enough space in ppconst, flush constants.
3562 if (generate_ppconst(cctx, ppconst) == FAIL)
3563 return FAIL;
3564 ret = compile_expr0(arg, cctx);
3565 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 *arg = skipwhite(*arg);
3567 if (**arg == ')')
3568 ++*arg;
3569 else if (ret == OK)
3570 {
3571 emsg(_(e_missing_close));
3572 ret = FAIL;
3573 }
3574 break;
3575
3576 default: ret = NOTDONE;
3577 break;
3578 }
3579 if (ret == FAIL)
3580 return FAIL;
3581
Bram Moolenaar1c747212020-05-09 18:28:34 +02003582 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003584 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003585 clear_tv(rettv);
3586 else
3587 // A constant expression can possibly be handled compile time,
3588 // return the value instead of generating code.
3589 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590 }
3591 else if (ret == NOTDONE)
3592 {
3593 char_u *p;
3594 int r;
3595
3596 if (!eval_isnamec1(**arg))
3597 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003598 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003599 return FAIL;
3600 }
3601
3602 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003603 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003604 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003605 {
3606 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3607 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003609 {
3610 if (generate_ppconst(cctx, ppconst) == FAIL)
3611 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003612 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003613 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 if (r == FAIL)
3615 return FAIL;
3616 }
3617
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003618 // Handle following "[]", ".member", etc.
3619 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003620 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003621 ppconst) == FAIL)
3622 return FAIL;
3623 if (ppconst->pp_used > 0)
3624 {
3625 // apply the '!', '-' and '+' before the constant
3626 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003627 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003628 return FAIL;
3629 return OK;
3630 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003631 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003633 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634}
3635
3636/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003637 * Give the "white on both sides" error, taking the operator from "p[len]".
3638 */
3639 void
3640error_white_both(char_u *op, int len)
3641{
3642 char_u buf[10];
3643
3644 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003645 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003646}
3647
3648/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003649 * <type>expr7: runtime type check / conversion
3650 */
3651 static int
3652compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3653{
3654 type_T *want_type = NULL;
3655
3656 // Recognize <type>
3657 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3658 {
3659 int called_emsg_before = called_emsg;
3660
3661 ++*arg;
3662 want_type = parse_type(arg, cctx->ctx_type_list);
3663 if (called_emsg != called_emsg_before)
3664 return FAIL;
3665
3666 if (**arg != '>')
3667 {
3668 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003669 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003670 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003671 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003672 return FAIL;
3673 }
3674 ++*arg;
3675 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3676 return FAIL;
3677 }
3678
3679 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3680 return FAIL;
3681
3682 if (want_type != NULL)
3683 {
3684 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003685 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003686
Bram Moolenaard1103582020-08-14 22:44:25 +02003687 generate_ppconst(cctx, ppconst);
3688 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003689 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003690 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003691 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3692 return FAIL;
3693 }
3694 }
3695
3696 return OK;
3697}
3698
3699/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700 * * number multiplication
3701 * / number division
3702 * % number modulo
3703 */
3704 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003705compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706{
3707 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003708 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003709 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003711 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003712 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713 return FAIL;
3714
3715 /*
3716 * Repeat computing, until no "*", "/" or "%" is following.
3717 */
3718 for (;;)
3719 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003720 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 if (*op != '*' && *op != '/' && *op != '%')
3722 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003723 if (next != NULL)
3724 {
3725 *arg = next_line_from_context(cctx, TRUE);
3726 op = skipwhite(*arg);
3727 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003728
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003729 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003730 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003731 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003732 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 }
3734 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003735 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003736 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003737
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003738 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003739 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003741
3742 if (ppconst->pp_used == ppconst_used + 2
3743 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3744 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003745 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003746 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3747 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003748 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003750 // both are numbers: compute the result
3751 switch (*op)
3752 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003753 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003754 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003755 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003756 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003757 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003758 break;
3759 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003760 tv1->vval.v_number = res;
3761 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003762 }
3763 else
3764 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003765 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003766 generate_two_op(cctx, op);
3767 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 }
3769
3770 return OK;
3771}
3772
3773/*
3774 * + number addition
3775 * - number subtraction
3776 * .. string concatenation
3777 */
3778 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003779compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780{
3781 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003782 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003784 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003785
3786 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003787 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788 return FAIL;
3789
3790 /*
3791 * Repeat computing, until no "+", "-" or ".." is following.
3792 */
3793 for (;;)
3794 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003795 op = may_peek_next_line(cctx, *arg, &next);
3796 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797 break;
3798 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003799 if (next != NULL)
3800 {
3801 *arg = next_line_from_context(cctx, TRUE);
3802 op = skipwhite(*arg);
3803 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003804
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003805 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003807 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003808 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 }
3810
3811 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003812 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003813 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003815 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003816 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 return FAIL;
3818
Bram Moolenaara5565e42020-05-09 15:44:01 +02003819 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003820 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003821 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3822 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3823 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3824 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003826 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3827 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003828
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003829 // concat/subtract/add constant numbers
3830 if (*op == '+')
3831 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3832 else if (*op == '-')
3833 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3834 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003835 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003836 // concatenate constant strings
3837 char_u *s1 = tv1->vval.v_string;
3838 char_u *s2 = tv2->vval.v_string;
3839 size_t len1 = STRLEN(s1);
3840
3841 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3842 if (tv1->vval.v_string == NULL)
3843 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003844 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003845 return FAIL;
3846 }
3847 mch_memmove(tv1->vval.v_string, s1, len1);
3848 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003849 vim_free(s1);
3850 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003851 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003852 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853 }
3854 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003855 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003856 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003857 if (*op == '.')
3858 {
3859 if (may_generate_2STRING(-2, cctx) == FAIL
3860 || may_generate_2STRING(-1, cctx) == FAIL)
3861 return FAIL;
3862 generate_instr_drop(cctx, ISN_CONCAT, 1);
3863 }
3864 else
3865 generate_two_op(cctx, op);
3866 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 }
3868
3869 return OK;
3870}
3871
3872/*
3873 * expr5a == expr5b
3874 * expr5a =~ expr5b
3875 * expr5a != expr5b
3876 * expr5a !~ expr5b
3877 * expr5a > expr5b
3878 * expr5a >= expr5b
3879 * expr5a < expr5b
3880 * expr5a <= expr5b
3881 * expr5a is expr5b
3882 * expr5a isnot expr5b
3883 *
3884 * Produces instructions:
3885 * EVAL expr5a Push result of "expr5a"
3886 * EVAL expr5b Push result of "expr5b"
3887 * COMPARE one of the compare instructions
3888 */
3889 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003890compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003891{
3892 exptype_T type = EXPR_UNKNOWN;
3893 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003894 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003895 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003897 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898
3899 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003900 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901 return FAIL;
3902
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003903 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003904 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905
3906 /*
3907 * If there is a comparative operator, use it.
3908 */
3909 if (type != EXPR_UNKNOWN)
3910 {
3911 int ic = FALSE; // Default: do not ignore case
3912
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003913 if (next != NULL)
3914 {
3915 *arg = next_line_from_context(cctx, TRUE);
3916 p = skipwhite(*arg);
3917 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918 if (type_is && (p[len] == '?' || p[len] == '#'))
3919 {
3920 semsg(_(e_invexpr2), *arg);
3921 return FAIL;
3922 }
3923 // extra question mark appended: ignore case
3924 if (p[len] == '?')
3925 {
3926 ic = TRUE;
3927 ++len;
3928 }
3929 // extra '#' appended: match case (ignored)
3930 else if (p[len] == '#')
3931 ++len;
3932 // nothing appended: match case
3933
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003934 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003935 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003936 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003937 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 }
3939
3940 // get the second variable
3941 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003942 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003943 return FAIL;
3944
Bram Moolenaara5565e42020-05-09 15:44:01 +02003945 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 return FAIL;
3947
Bram Moolenaara5565e42020-05-09 15:44:01 +02003948 if (ppconst->pp_used == ppconst_used + 2)
3949 {
3950 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3951 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3952 int ret;
3953
3954 // Both sides are a constant, compute the result now.
3955 // First check for a valid combination of types, this is more
3956 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003957 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003958 ret = FAIL;
3959 else
3960 {
3961 ret = typval_compare(tv1, tv2, type, ic);
3962 tv1->v_type = VAR_BOOL;
3963 tv1->vval.v_number = tv1->vval.v_number
3964 ? VVAL_TRUE : VVAL_FALSE;
3965 clear_tv(tv2);
3966 --ppconst->pp_used;
3967 }
3968 return ret;
3969 }
3970
3971 generate_ppconst(cctx, ppconst);
3972 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 }
3974
3975 return OK;
3976}
3977
Bram Moolenaar7f141552020-05-09 17:35:53 +02003978static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3979
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980/*
3981 * Compile || or &&.
3982 */
3983 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003984compile_and_or(
3985 char_u **arg,
3986 cctx_T *cctx,
3987 char *op,
3988 ppconst_T *ppconst,
3989 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003991 char_u *next;
3992 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003993 int opchar = *op;
3994
3995 if (p[0] == opchar && p[1] == opchar)
3996 {
3997 garray_T *instr = &cctx->ctx_instr;
3998 garray_T end_ga;
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02003999 garray_T *stack = &cctx->ctx_type_stack;
4000 type_T **typep;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001
4002 /*
4003 * Repeat until there is no following "||" or "&&"
4004 */
4005 ga_init2(&end_ga, sizeof(int), 10);
4006 while (p[0] == opchar && p[1] == opchar)
4007 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004008 if (next != NULL)
4009 {
4010 *arg = next_line_from_context(cctx, TRUE);
4011 p = skipwhite(*arg);
4012 }
4013
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004014 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4015 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004016 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004017 return FAIL;
4018 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019
Bram Moolenaara5565e42020-05-09 15:44:01 +02004020 // TODO: use ppconst if the value is a constant
4021 generate_ppconst(cctx, ppconst);
4022
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023 if (ga_grow(&end_ga, 1) == FAIL)
4024 {
4025 ga_clear(&end_ga);
4026 return FAIL;
4027 }
4028 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4029 ++end_ga.ga_len;
4030 generate_JUMP(cctx, opchar == '|'
4031 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4032
4033 // eval the next expression
4034 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004035 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004036 return FAIL;
4037
Bram Moolenaara5565e42020-05-09 15:44:01 +02004038 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4039 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 {
4041 ga_clear(&end_ga);
4042 return FAIL;
4043 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004044
4045 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004047 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048
4049 // Fill in the end label in all jumps.
4050 while (end_ga.ga_len > 0)
4051 {
4052 isn_T *isn;
4053
4054 --end_ga.ga_len;
4055 isn = ((isn_T *)instr->ga_data)
4056 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4057 isn->isn_arg.jump.jump_where = instr->ga_len;
4058 }
4059 ga_clear(&end_ga);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004060
4061 // The resulting type can be used as a bool.
4062 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4063 if (*typep != &t_bool)
4064 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004065 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004066
4067 if (type != NULL)
4068 {
4069 *type = **typep;
4070 type->tt_flags |= TTFLAG_BOOL_OK;
4071 *typep = type;
4072 }
4073 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004074 }
4075
4076 return OK;
4077}
4078
4079/*
4080 * expr4a && expr4a && expr4a logical AND
4081 *
4082 * Produces instructions:
4083 * EVAL expr4a Push result of "expr4a"
4084 * JUMP_AND_KEEP_IF_FALSE end
4085 * EVAL expr4b Push result of "expr4b"
4086 * JUMP_AND_KEEP_IF_FALSE end
4087 * EVAL expr4c Push result of "expr4c"
4088 * end:
4089 */
4090 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004091compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004092{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004093 int ppconst_used = ppconst->pp_used;
4094
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004096 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 return FAIL;
4098
4099 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004100 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101}
4102
4103/*
4104 * expr3a || expr3b || expr3c logical OR
4105 *
4106 * Produces instructions:
4107 * EVAL expr3a Push result of "expr3a"
4108 * JUMP_AND_KEEP_IF_TRUE end
4109 * EVAL expr3b Push result of "expr3b"
4110 * JUMP_AND_KEEP_IF_TRUE end
4111 * EVAL expr3c Push result of "expr3c"
4112 * end:
4113 */
4114 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004115compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004116{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004117 int ppconst_used = ppconst->pp_used;
4118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004120 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121 return FAIL;
4122
4123 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004124 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004125}
4126
4127/*
4128 * Toplevel expression: expr2 ? expr1a : expr1b
4129 *
4130 * Produces instructions:
4131 * EVAL expr2 Push result of "expr"
4132 * JUMP_IF_FALSE alt jump if false
4133 * EVAL expr1a
4134 * JUMP_ALWAYS end
4135 * alt: EVAL expr1b
4136 * end:
4137 */
4138 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004139compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140{
4141 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004142 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004143 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144
Bram Moolenaar3988f642020-08-27 22:43:03 +02004145 // Ignore all kinds of errors when not producing code.
4146 if (cctx->ctx_skip == SKIP_YES)
4147 {
4148 skip_expr(arg);
4149 return OK;
4150 }
4151
Bram Moolenaar61a89812020-05-07 16:58:17 +02004152 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004153 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154 return FAIL;
4155
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004156 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 if (*p == '?')
4158 {
4159 garray_T *instr = &cctx->ctx_instr;
4160 garray_T *stack = &cctx->ctx_type_stack;
4161 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004162 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004164 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004166 int has_const_expr = FALSE;
4167 int const_value = FALSE;
4168 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004169
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004170 if (next != NULL)
4171 {
4172 *arg = next_line_from_context(cctx, TRUE);
4173 p = skipwhite(*arg);
4174 }
4175
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004176 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4177 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004178 semsg(_(e_white_space_required_before_and_after_str), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004179 return FAIL;
4180 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181
Bram Moolenaara5565e42020-05-09 15:44:01 +02004182 if (ppconst->pp_used == ppconst_used + 1)
4183 {
4184 // the condition is a constant, we know whether the ? or the :
4185 // expression is to be evaluated.
4186 has_const_expr = TRUE;
4187 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4188 clear_tv(&ppconst->pp_tv[ppconst_used]);
4189 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004190 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4191 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004192 }
4193 else
4194 {
4195 generate_ppconst(cctx, ppconst);
4196 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4197 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198
4199 // evaluate the second expression; any type is accepted
4200 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004201 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004202 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004203 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004204 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205
Bram Moolenaara5565e42020-05-09 15:44:01 +02004206 if (!has_const_expr)
4207 {
4208 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209
Bram Moolenaara5565e42020-05-09 15:44:01 +02004210 // remember the type and drop it
4211 --stack->ga_len;
4212 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213
Bram Moolenaara5565e42020-05-09 15:44:01 +02004214 end_idx = instr->ga_len;
4215 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4216
4217 // jump here from JUMP_IF_FALSE
4218 isn = ((isn_T *)instr->ga_data) + alt_idx;
4219 isn->isn_arg.jump.jump_where = instr->ga_len;
4220 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004221
4222 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004223 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224 if (*p != ':')
4225 {
4226 emsg(_(e_missing_colon));
4227 return FAIL;
4228 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004229 if (next != NULL)
4230 {
4231 *arg = next_line_from_context(cctx, TRUE);
4232 p = skipwhite(*arg);
4233 }
4234
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004235 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4236 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004237 semsg(_(e_white_space_required_before_and_after_str), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004238 return FAIL;
4239 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240
4241 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004242 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004243 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4244 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004246 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004247 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004248 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004249 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250
Bram Moolenaara5565e42020-05-09 15:44:01 +02004251 if (!has_const_expr)
4252 {
4253 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254
Bram Moolenaara5565e42020-05-09 15:44:01 +02004255 // If the types differ, the result has a more generic type.
4256 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4257 common_type(type1, type2, &type2, cctx->ctx_type_list);
4258
4259 // jump here from JUMP_ALWAYS
4260 isn = ((isn_T *)instr->ga_data) + end_idx;
4261 isn->isn_arg.jump.jump_where = instr->ga_len;
4262 }
4263
4264 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265 }
4266 return OK;
4267}
4268
4269/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004270 * Toplevel expression.
4271 */
4272 static int
4273compile_expr0(char_u **arg, cctx_T *cctx)
4274{
4275 ppconst_T ppconst;
4276
4277 CLEAR_FIELD(ppconst);
4278 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4279 {
4280 clear_ppconst(&ppconst);
4281 return FAIL;
4282 }
4283 if (generate_ppconst(cctx, &ppconst) == FAIL)
4284 return FAIL;
4285 return OK;
4286}
4287
4288/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004289 * compile "return [expr]"
4290 */
4291 static char_u *
4292compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4293{
4294 char_u *p = arg;
4295 garray_T *stack = &cctx->ctx_type_stack;
4296 type_T *stack_type;
4297
4298 if (*p != NUL && *p != '|' && *p != '\n')
4299 {
4300 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004301 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302 return NULL;
4303
4304 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4305 if (set_return_type)
4306 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004307 else
4308 {
4309 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4310 && stack_type->tt_type != VAR_VOID
4311 && stack_type->tt_type != VAR_UNKNOWN)
4312 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004313 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004314 return NULL;
4315 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004316 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4317 cctx, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004318 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004319 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 }
4321 else
4322 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004323 // "set_return_type" cannot be TRUE, only used for a lambda which
4324 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004325 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4326 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004328 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004329 return NULL;
4330 }
4331
4332 // No argument, return zero.
4333 generate_PUSHNR(cctx, 0);
4334 }
4335
4336 if (generate_instr(cctx, ISN_RETURN) == NULL)
4337 return NULL;
4338
4339 // "return val | endif" is possible
4340 return skipwhite(p);
4341}
4342
4343/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004344 * Get a line from the compilation context, compatible with exarg_T getline().
4345 * Return a pointer to the line in allocated memory.
4346 * Return NULL for end-of-file or some error.
4347 */
4348 static char_u *
4349exarg_getline(
4350 int c UNUSED,
4351 void *cookie,
4352 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004353 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004354{
4355 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004356 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004357
Bram Moolenaar66250c92020-08-20 15:02:42 +02004358 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004359 {
Bram Moolenaar66250c92020-08-20 15:02:42 +02004360 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4361 return NULL;
4362 ++cctx->ctx_lnum;
4363 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4364 // Comment lines result in NULL pointers, skip them.
4365 if (p != NULL)
4366 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004367 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004368}
4369
4370/*
4371 * Compile a nested :def command.
4372 */
4373 static char_u *
4374compile_nested_function(exarg_T *eap, cctx_T *cctx)
4375{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004376 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004377 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004378 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004379 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004380 lvar_T *lvar;
4381 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004382 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004383
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004384 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004385 {
4386 emsg(_(e_cannot_use_bang_with_nested_def));
4387 return NULL;
4388 }
4389
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004390 // Only g:Func() can use a namespace.
4391 if (name_start[1] == ':' && !is_global)
4392 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004393 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004394 return NULL;
4395 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004396 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4397 return NULL;
4398
Bram Moolenaar04b12692020-05-04 23:24:44 +02004399 eap->arg = name_end;
4400 eap->getline = exarg_getline;
4401 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004402 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004403 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004404 lambda_name = get_lambda_name();
4405 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004406
Bram Moolenaar822ba242020-05-24 23:00:18 +02004407 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004408 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004409 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004410 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004411 return NULL;
4412
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004413 if (is_global)
4414 {
4415 char_u *func_name = vim_strnsave(name_start + 2,
4416 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004417
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004418 if (func_name == NULL)
4419 r = FAIL;
4420 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004421 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004422 }
4423 else
4424 {
4425 // Define a local variable for the function reference.
4426 lvar = reserve_local(cctx, name_start, name_end - name_start,
4427 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004428 if (lvar == NULL)
4429 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004430 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004431 return NULL;
4432 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4433 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004434
Bram Moolenaar61a89812020-05-07 16:58:17 +02004435 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004436 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004437}
4438
4439/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 * Return the length of an assignment operator, or zero if there isn't one.
4441 */
4442 int
4443assignment_len(char_u *p, int *heredoc)
4444{
4445 if (*p == '=')
4446 {
4447 if (p[1] == '<' && p[2] == '<')
4448 {
4449 *heredoc = TRUE;
4450 return 3;
4451 }
4452 return 1;
4453 }
4454 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4455 return 2;
4456 if (STRNCMP(p, "..=", 3) == 0)
4457 return 3;
4458 return 0;
4459}
4460
4461// words that cannot be used as a variable
4462static char *reserved[] = {
4463 "true",
4464 "false",
4465 NULL
4466};
4467
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004468typedef enum {
4469 dest_local,
4470 dest_option,
4471 dest_env,
4472 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004473 dest_buffer,
4474 dest_window,
4475 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004476 dest_vimvar,
4477 dest_script,
4478 dest_reg,
4479} assign_dest_T;
4480
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004482 * Generate the load instruction for "name".
4483 */
4484 static void
4485generate_loadvar(
4486 cctx_T *cctx,
4487 assign_dest_T dest,
4488 char_u *name,
4489 lvar_T *lvar,
4490 type_T *type)
4491{
4492 switch (dest)
4493 {
4494 case dest_option:
4495 // TODO: check the option exists
4496 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4497 break;
4498 case dest_global:
4499 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4500 break;
4501 case dest_buffer:
4502 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4503 break;
4504 case dest_window:
4505 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4506 break;
4507 case dest_tab:
4508 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4509 break;
4510 case dest_script:
4511 compile_load_scriptvar(cctx,
4512 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4513 break;
4514 case dest_env:
4515 // Include $ in the name here
4516 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4517 break;
4518 case dest_reg:
4519 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4520 break;
4521 case dest_vimvar:
4522 generate_LOADV(cctx, name + 2, TRUE);
4523 break;
4524 case dest_local:
4525 if (lvar->lv_from_outer)
4526 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4527 NULL, type);
4528 else
4529 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4530 break;
4531 }
4532}
4533
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004534 void
4535vim9_declare_error(char_u *name)
4536{
4537 char *scope = "";
4538
4539 switch (*name)
4540 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004541 case 'g': scope = _("global"); break;
4542 case 'b': scope = _("buffer"); break;
4543 case 'w': scope = _("window"); break;
4544 case 't': scope = _("tab"); break;
4545 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004546 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004547 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004548 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004549 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004550 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004551 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004552 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004553 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004554 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004555}
4556
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004557/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004558 * Compile declaration and assignment:
4559 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004560 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004561 * Return NULL for an error.
4562 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004563 */
4564 static char_u *
4565compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4566{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004567 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004568 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004569 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570 char_u *ret = NULL;
4571 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004572 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004573 int scriptvar_sid = 0;
4574 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004577 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004578 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004579 int oplen = 0;
4580 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004581 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004582 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004583 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004586
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004587 // Skip over the "var" or "[var, var]" to get to any "=".
4588 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4589 if (p == NULL)
4590 return *arg == '[' ? arg : NULL;
4591
4592 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004593 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004594 // TODO: should we allow this, and figure out type inference from list
4595 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004596 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597 return NULL;
4598 }
4599
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600 sp = p;
4601 p = skipwhite(p);
4602 op = p;
4603 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004604
4605 if (var_count > 0 && oplen == 0)
4606 // can be something like "[1, 2]->func()"
4607 return arg;
4608
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004609 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4610 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004611 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004612 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004613 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004614
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004615 if (heredoc)
4616 {
4617 list_T *l;
4618 listitem_T *li;
4619
4620 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004621 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004623 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004624
Bram Moolenaar078269b2020-09-21 20:35:55 +02004625 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004626 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02004627 // Push each line and the create the list.
4628 FOR_ALL_LIST_ITEMS(l, li)
4629 {
4630 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4631 li->li_tv.vval.v_string = NULL;
4632 }
4633 generate_NEWLIST(cctx, l->lv_len);
4634 type = &t_list_string;
4635 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637 list_free(l);
4638 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004639 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004640 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004641 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004643 // for "[var, var] = expr" evaluate the expression here, loop over the
4644 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004645
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004646 p = skipwhite(op + oplen);
4647 if (compile_expr0(&p, cctx) == FAIL)
4648 return NULL;
4649 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004650
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004651 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004653 type_T *stacktype;
4654
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004655 stacktype = stack->ga_len == 0 ? &t_void
4656 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004657 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004658 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004659 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004660 goto theend;
4661 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004662 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004663 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004664 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004665 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4666 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004667 }
4668 }
4669
4670 /*
4671 * Loop over variables in "[var, var] = expr".
4672 * For "var = expr" and "let var: type" this is done only once.
4673 */
4674 if (var_count > 0)
4675 var_start = skipwhite(arg + 1); // skip over the "["
4676 else
4677 var_start = arg;
4678 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4679 {
4680 char_u *var_end = skip_var_one(var_start, FALSE);
4681 size_t varlen;
4682 int new_local = FALSE;
4683 int opt_type;
4684 int opt_flags = 0;
4685 assign_dest_T dest = dest_local;
4686 int vimvaridx = -1;
4687 lvar_T *lvar = NULL;
4688 lvar_T arg_lvar;
4689 int has_type = FALSE;
4690 int has_index = FALSE;
4691 int instr_count = -1;
4692
Bram Moolenaar65821722020-08-02 18:58:54 +02004693 if (*var_start == '@')
4694 p = var_start + 2;
4695 else
4696 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02004697 // skip over the leading "&", "&l:", "&g:" and "$"
4698 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02004699 p = to_name_end(p, TRUE);
4700 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004701
4702 // "a: type" is declaring variable "a" with a type, not "a:".
4703 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4704 --var_end;
4705 if (is_decl && p == var_start + 2 && p[-1] == ':')
4706 --p;
4707
4708 varlen = p - var_start;
4709 vim_free(name);
4710 name = vim_strnsave(var_start, varlen);
4711 if (name == NULL)
4712 return NULL;
4713 if (!heredoc)
4714 type = &t_any;
4715
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004716 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004717 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004718 int declare_error = FALSE;
4719
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004720 if (*var_start == '&')
4721 {
4722 int cc;
4723 long numval;
4724
4725 dest = dest_option;
4726 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004728 emsg(_(e_const_option));
4729 goto theend;
4730 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004731 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004732 p = var_start;
4733 p = find_option_end(&p, &opt_flags);
4734 if (p == NULL)
4735 {
4736 // cannot happen?
4737 emsg(_(e_letunexp));
4738 goto theend;
4739 }
4740 cc = *p;
4741 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02004742 opt_type = get_option_value(skip_option_env_lead(var_start),
4743 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004744 *p = cc;
4745 if (opt_type == -3)
4746 {
4747 semsg(_(e_unknown_option), var_start);
4748 goto theend;
4749 }
4750 if (opt_type == -2 || opt_type == 0)
4751 type = &t_string;
4752 else
4753 type = &t_number; // both number and boolean option
4754 }
4755 else if (*var_start == '$')
4756 {
4757 dest = dest_env;
4758 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004759 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004760 }
4761 else if (*var_start == '@')
4762 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004763 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004764 {
4765 emsg_invreg(var_start[1]);
4766 goto theend;
4767 }
4768 dest = dest_reg;
4769 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004770 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004771 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004772 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004773 {
4774 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004775 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004776 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004777 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004778 {
4779 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004780 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004781 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004782 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004783 {
4784 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004785 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004786 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004787 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004788 {
4789 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004790 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004791 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004792 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004793 {
4794 typval_T *vtv;
4795 int di_flags;
4796
4797 vimvaridx = find_vim_var(name + 2, &di_flags);
4798 if (vimvaridx < 0)
4799 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004800 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004801 goto theend;
4802 }
4803 // We use the current value of "sandbox" here, is that OK?
4804 if (var_check_ro(di_flags, name, FALSE))
4805 goto theend;
4806 dest = dest_vimvar;
4807 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004808 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004809 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004810 }
4811 else
4812 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004813 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004814
4815 for (idx = 0; reserved[idx] != NULL; ++idx)
4816 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004817 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004818 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004819 goto theend;
4820 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004821
4822 lvar = lookup_local(var_start, varlen, cctx);
4823 if (lvar == NULL)
4824 {
4825 CLEAR_FIELD(arg_lvar);
4826 if (lookup_arg(var_start, varlen,
4827 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4828 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004829 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004830 if (is_decl)
4831 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004832 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004833 goto theend;
4834 }
4835 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004836 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004837 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004838 if (lvar != NULL)
4839 {
4840 if (is_decl)
4841 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004842 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004843 goto theend;
4844 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004845 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004846 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004847 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004848 int script_namespace = varlen > 1
4849 && STRNCMP(var_start, "s:", 2) == 0;
4850 int script_var = (script_namespace
Bram Moolenaar53900992020-08-22 19:02:02 +02004851 ? lookup_script(var_start + 2, varlen - 2, FALSE)
4852 : lookup_script(var_start, varlen, TRUE)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004853 imported_T *import =
4854 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004855
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004856 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004857 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004858 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4859
4860 if (is_decl)
4861 {
4862 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004863 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004864 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004865 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004866 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004867 name);
4868 goto theend;
4869 }
4870 else if (cctx->ctx_ufunc->uf_script_ctx_version
4871 == SCRIPT_VERSION_VIM9
4872 && script_namespace
4873 && !script_var && import == NULL)
4874 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004875 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004876 goto theend;
4877 }
4878
4879 dest = dest_script;
4880
4881 // existing script-local variables should have a type
4882 scriptvar_sid = current_sctx.sc_sid;
4883 if (import != NULL)
4884 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004885 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004886 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02004887 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4888 rawname, TRUE);
4889 if (scriptvar_idx > 0)
4890 {
4891 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4892 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004893 ((svar_T *)si->sn_var_vals.ga_data)
4894 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004895 type = sv->sv_type;
4896 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004897 }
4898 }
4899 else if (name[1] == ':' && name[2] != NUL)
4900 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004901 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004902 goto theend;
4903 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004904 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004905 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004906 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004907 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004908 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004909 else if (check_defined(var_start, varlen, cctx) == FAIL)
4910 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004911 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004912 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004913
4914 if (declare_error)
4915 {
4916 vim9_declare_error(name);
4917 goto theend;
4918 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004919 }
4920
4921 // handle "a:name" as a name, not index "name" on "a"
4922 if (varlen > 1 || var_start[varlen] != ':')
4923 p = var_end;
4924
4925 if (dest != dest_option)
4926 {
4927 if (is_decl && *p == ':')
4928 {
4929 // parse optional type: "let var: type = expr"
4930 if (!VIM_ISWHITE(p[1]))
4931 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004932 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004933 goto theend;
4934 }
4935 p = skipwhite(p + 1);
4936 type = parse_type(&p, cctx->ctx_type_list);
4937 has_type = TRUE;
4938 }
4939 else if (lvar != NULL)
4940 type = lvar->lv_type;
4941 }
4942
4943 if (oplen == 3 && !heredoc && dest != dest_global
4944 && type->tt_type != VAR_STRING
4945 && type->tt_type != VAR_ANY)
4946 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004947 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004948 goto theend;
4949 }
4950
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004951 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004952 {
4953 if (oplen > 1 && !heredoc)
4954 {
4955 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004956 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004957 goto theend;
4958 }
4959
4960 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004961 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
4962 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004963 goto theend;
4964 lvar = reserve_local(cctx, var_start, varlen,
4965 cmdidx == CMD_const, type);
4966 if (lvar == NULL)
4967 goto theend;
4968 new_local = TRUE;
4969 }
4970
4971 member_type = type;
4972 if (var_end > var_start + varlen)
4973 {
4974 // Something follows after the variable: "var[idx]".
4975 if (is_decl)
4976 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004977 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004978 goto theend;
4979 }
4980
4981 if (var_start[varlen] == '[')
4982 {
4983 has_index = TRUE;
4984 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004985 member_type = &t_any;
4986 else
4987 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004988 }
4989 else
4990 {
4991 semsg("Not supported yet: %s", var_start);
4992 goto theend;
4993 }
4994 }
4995 else if (lvar == &arg_lvar)
4996 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004997 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004998 goto theend;
4999 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005000 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5001 {
5002 semsg(_(e_cannot_assign_to_constant), name);
5003 goto theend;
5004 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005005
5006 if (!heredoc)
5007 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005008 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005009 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005010 if (oplen > 0 && var_count == 0)
5011 {
5012 // skip over the "=" and the expression
5013 p = skipwhite(op + oplen);
5014 compile_expr0(&p, cctx);
5015 }
5016 }
5017 else if (oplen > 0)
5018 {
5019 type_T *stacktype;
5020
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005021 // For "var = expr" evaluate the expression.
5022 if (var_count == 0)
5023 {
5024 int r;
5025
5026 // for "+=", "*=", "..=" etc. first load the current value
5027 if (*op != '=')
5028 {
5029 generate_loadvar(cctx, dest, name, lvar, type);
5030
5031 if (has_index)
5032 {
5033 // TODO: get member from list or dict
5034 emsg("Index with operation not supported yet");
5035 goto theend;
5036 }
5037 }
5038
5039 // Compile the expression. Temporarily hide the new local
5040 // variable here, it is not available to this expression.
5041 if (new_local)
5042 --cctx->ctx_locals.ga_len;
5043 instr_count = instr->ga_len;
5044 p = skipwhite(op + oplen);
5045 r = compile_expr0(&p, cctx);
5046 if (new_local)
5047 ++cctx->ctx_locals.ga_len;
5048 if (r == FAIL)
5049 goto theend;
5050 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005051 else if (semicolon && var_idx == var_count - 1)
5052 {
5053 // For "[var; var] = expr" get the rest of the list
5054 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5055 goto theend;
5056 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005057 else
5058 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005059 // For "[var, var] = expr" get the "var_idx" item from the
5060 // list.
5061 if (generate_GETITEM(cctx, var_idx) == FAIL)
5062 return FAIL;
5063 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005064
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005065 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005066 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005067 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005068 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005069 if ((stacktype->tt_type == VAR_FUNC
5070 || stacktype->tt_type == VAR_PARTIAL)
5071 && var_wrong_func_name(name, TRUE))
5072 goto theend;
5073
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005074 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005075 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005076 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005077 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005078 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005079 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005080 }
5081 else
5082 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005083 // An empty list or dict has a &t_void member,
5084 // for a variable that implies &t_any.
5085 if (stacktype == &t_list_empty)
5086 lvar->lv_type = &t_list_any;
5087 else if (stacktype == &t_dict_empty)
5088 lvar->lv_type = &t_dict_any;
5089 else
5090 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005091 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005092 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005093 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005094 {
5095 type_T *use_type = lvar->lv_type;
5096
Bram Moolenaar71abe482020-09-14 22:28:30 +02005097 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005098 if (has_index)
5099 {
5100 use_type = use_type->tt_member;
5101 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005102 // could be indexing "any"
5103 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005104 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005105 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005106 == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005107 goto theend;
5108 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005109 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005110 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005111 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005112 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005113 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005114 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005115 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005116 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005117 goto theend;
5118 }
5119 else if (!has_type || dest == dest_option)
5120 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005121 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005122 goto theend;
5123 }
5124 else
5125 {
5126 // variables are always initialized
5127 if (ga_grow(instr, 1) == FAIL)
5128 goto theend;
5129 switch (member_type->tt_type)
5130 {
5131 case VAR_BOOL:
5132 generate_PUSHBOOL(cctx, VVAL_FALSE);
5133 break;
5134 case VAR_FLOAT:
5135#ifdef FEAT_FLOAT
5136 generate_PUSHF(cctx, 0.0);
5137#endif
5138 break;
5139 case VAR_STRING:
5140 generate_PUSHS(cctx, NULL);
5141 break;
5142 case VAR_BLOB:
5143 generate_PUSHBLOB(cctx, NULL);
5144 break;
5145 case VAR_FUNC:
5146 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5147 break;
5148 case VAR_LIST:
5149 generate_NEWLIST(cctx, 0);
5150 break;
5151 case VAR_DICT:
5152 generate_NEWDICT(cctx, 0);
5153 break;
5154 case VAR_JOB:
5155 generate_PUSHJOB(cctx, NULL);
5156 break;
5157 case VAR_CHANNEL:
5158 generate_PUSHCHANNEL(cctx, NULL);
5159 break;
5160 case VAR_NUMBER:
5161 case VAR_UNKNOWN:
5162 case VAR_ANY:
5163 case VAR_PARTIAL:
5164 case VAR_VOID:
5165 case VAR_SPECIAL: // cannot happen
5166 generate_PUSHNR(cctx, 0);
5167 break;
5168 }
5169 }
5170 if (var_count == 0)
5171 end = p;
5172 }
5173
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005174 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005175 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005176 break;
5177
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005178 if (oplen > 0 && *op != '=')
5179 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005180 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005181 type_T *stacktype;
5182
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005183 if (*op == '.')
5184 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005185 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005186 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005187 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005188 if (
5189#ifdef FEAT_FLOAT
5190 // If variable is float operation with number is OK.
5191 !(expected == &t_float && stacktype == &t_number) &&
5192#endif
5193 need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005194 goto theend;
5195
5196 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005197 {
5198 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5199 goto theend;
5200 }
5201 else if (*op == '+')
5202 {
5203 if (generate_add_instr(cctx,
5204 operator_type(member_type, stacktype),
5205 member_type, stacktype) == FAIL)
5206 goto theend;
5207 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005208 else if (generate_two_op(cctx, op) == FAIL)
5209 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005210 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005211
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005212 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005213 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005214 int r;
5215
5216 // Compile the "idx" in "var[idx]".
5217 if (new_local)
5218 --cctx->ctx_locals.ga_len;
5219 p = skipwhite(var_start + varlen + 1);
5220 r = compile_expr0(&p, cctx);
5221 if (new_local)
5222 ++cctx->ctx_locals.ga_len;
5223 if (r == FAIL)
5224 goto theend;
5225 if (*skipwhite(p) != ']')
5226 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005227 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005228 emsg(_(e_missbrac));
5229 goto theend;
5230 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005231 if (type == &t_any)
5232 {
5233 type_T *idx_type = ((type_T **)stack->ga_data)[
5234 stack->ga_len - 1];
5235 // Index on variable of unknown type: guess the type from the
5236 // index type: number is dict, otherwise dict.
5237 // TODO: should do the assignment at runtime
5238 if (idx_type->tt_type == VAR_NUMBER)
5239 type = &t_list_any;
5240 else
5241 type = &t_dict_any;
5242 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005243 if (type->tt_type == VAR_DICT
5244 && may_generate_2STRING(-1, cctx) == FAIL)
5245 goto theend;
5246 if (type->tt_type == VAR_LIST
5247 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005248 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005249 {
5250 emsg(_(e_number_exp));
5251 goto theend;
5252 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005253
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005254 // Load the dict or list. On the stack we then have:
5255 // - value
5256 // - index
5257 // - variable
5258 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005259
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005260 if (type->tt_type == VAR_LIST)
5261 {
5262 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5263 return FAIL;
5264 }
5265 else if (type->tt_type == VAR_DICT)
5266 {
5267 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5268 return FAIL;
5269 }
5270 else
5271 {
5272 emsg(_(e_listreq));
5273 goto theend;
5274 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005275 }
5276 else
5277 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005278 if (is_decl && eap->forceit && cmdidx == CMD_const
5279 && (dest == dest_script || dest == dest_local))
5280 // ":const! var": lock the value, but not referenced variables
5281 generate_LOCKCONST(cctx);
5282
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005283 switch (dest)
5284 {
5285 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005286 generate_STOREOPT(cctx, skip_option_env_lead(name),
5287 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005288 break;
5289 case dest_global:
5290 // include g: with the name, easier to execute that way
5291 generate_STORE(cctx, ISN_STOREG, 0, name);
5292 break;
5293 case dest_buffer:
5294 // include b: with the name, easier to execute that way
5295 generate_STORE(cctx, ISN_STOREB, 0, name);
5296 break;
5297 case dest_window:
5298 // include w: with the name, easier to execute that way
5299 generate_STORE(cctx, ISN_STOREW, 0, name);
5300 break;
5301 case dest_tab:
5302 // include t: with the name, easier to execute that way
5303 generate_STORE(cctx, ISN_STORET, 0, name);
5304 break;
5305 case dest_env:
5306 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5307 break;
5308 case dest_reg:
5309 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5310 break;
5311 case dest_vimvar:
5312 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5313 break;
5314 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005315 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005316 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005317 {
5318 char_u *name_s = name;
5319
5320 // Include s: in the name for store_var()
5321 if (name[1] != ':')
5322 {
5323 int len = (int)STRLEN(name) + 3;
5324
5325 name_s = alloc(len);
5326 if (name_s == NULL)
5327 name_s = name;
5328 else
5329 vim_snprintf((char *)name_s, len,
5330 "s:%s", name);
5331 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005332 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5333 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005334 if (name_s != name)
5335 vim_free(name_s);
5336 }
5337 else
5338 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005339 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005340 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005341 break;
5342 case dest_local:
5343 if (lvar != NULL)
5344 {
5345 isn_T *isn = ((isn_T *)instr->ga_data)
5346 + instr->ga_len - 1;
5347
5348 // optimization: turn "var = 123" from ISN_PUSHNR +
5349 // ISN_STORE into ISN_STORENR
5350 if (!lvar->lv_from_outer
5351 && instr->ga_len == instr_count + 1
5352 && isn->isn_type == ISN_PUSHNR)
5353 {
5354 varnumber_T val = isn->isn_arg.number;
5355
5356 isn->isn_type = ISN_STORENR;
5357 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5358 isn->isn_arg.storenr.stnr_val = val;
5359 if (stack->ga_len > 0)
5360 --stack->ga_len;
5361 }
5362 else if (lvar->lv_from_outer)
5363 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005364 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005365 else
5366 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5367 }
5368 break;
5369 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005370 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005371
5372 if (var_idx + 1 < var_count)
5373 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005374 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005375
5376 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005377 if (var_count > 0 && !semicolon)
5378 {
5379 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5380 goto theend;
5381 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005382
Bram Moolenaarb2097502020-07-19 17:17:02 +02005383 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005384
5385theend:
5386 vim_free(name);
5387 return ret;
5388}
5389
5390/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005391 * Check if "name" can be "unlet".
5392 */
5393 int
5394check_vim9_unlet(char_u *name)
5395{
5396 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5397 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005398 // "unlet s:var" is allowed in legacy script.
5399 if (*name == 's' && !script_is_vim9())
5400 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005401 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005402 return FAIL;
5403 }
5404 return OK;
5405}
5406
5407/*
5408 * Callback passed to ex_unletlock().
5409 */
5410 static int
5411compile_unlet(
5412 lval_T *lvp,
5413 char_u *name_end,
5414 exarg_T *eap,
5415 int deep UNUSED,
5416 void *coookie)
5417{
5418 cctx_T *cctx = coookie;
5419
5420 if (lvp->ll_tv == NULL)
5421 {
5422 char_u *p = lvp->ll_name;
5423 int cc = *name_end;
5424 int ret = OK;
5425
5426 // Normal name. Only supports g:, w:, t: and b: namespaces.
5427 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005428 if (*p == '$')
5429 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5430 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005431 ret = FAIL;
5432 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005433 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005434
5435 *name_end = cc;
5436 return ret;
5437 }
5438
5439 // TODO: unlet {list}[idx]
5440 // TODO: unlet {dict}[key]
5441 emsg("Sorry, :unlet not fully implemented yet");
5442 return FAIL;
5443}
5444
5445/*
5446 * compile "unlet var", "lock var" and "unlock var"
5447 * "arg" points to "var".
5448 */
5449 static char_u *
5450compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5451{
5452 char_u *p = arg;
5453
5454 if (eap->cmdidx != CMD_unlet)
5455 {
5456 emsg("Sorry, :lock and unlock not implemented yet");
5457 return NULL;
5458 }
5459
5460 if (*p == '!')
5461 {
5462 p = skipwhite(p + 1);
5463 eap->forceit = TRUE;
5464 }
5465
5466 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5467 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5468}
5469
5470/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005471 * Compile an :import command.
5472 */
5473 static char_u *
5474compile_import(char_u *arg, cctx_T *cctx)
5475{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005476 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005477}
5478
5479/*
5480 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5481 */
5482 static int
5483compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5484{
5485 garray_T *instr = &cctx->ctx_instr;
5486 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5487
5488 if (endlabel == NULL)
5489 return FAIL;
5490 endlabel->el_next = *el;
5491 *el = endlabel;
5492 endlabel->el_end_label = instr->ga_len;
5493
5494 generate_JUMP(cctx, when, 0);
5495 return OK;
5496}
5497
5498 static void
5499compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5500{
5501 garray_T *instr = &cctx->ctx_instr;
5502
5503 while (*el != NULL)
5504 {
5505 endlabel_T *cur = (*el);
5506 isn_T *isn;
5507
5508 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5509 isn->isn_arg.jump.jump_where = instr->ga_len;
5510 *el = cur->el_next;
5511 vim_free(cur);
5512 }
5513}
5514
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005515 static void
5516compile_free_jump_to_end(endlabel_T **el)
5517{
5518 while (*el != NULL)
5519 {
5520 endlabel_T *cur = (*el);
5521
5522 *el = cur->el_next;
5523 vim_free(cur);
5524 }
5525}
5526
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005527/*
5528 * Create a new scope and set up the generic items.
5529 */
5530 static scope_T *
5531new_scope(cctx_T *cctx, scopetype_T type)
5532{
5533 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5534
5535 if (scope == NULL)
5536 return NULL;
5537 scope->se_outer = cctx->ctx_scope;
5538 cctx->ctx_scope = scope;
5539 scope->se_type = type;
5540 scope->se_local_count = cctx->ctx_locals.ga_len;
5541 return scope;
5542}
5543
5544/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005545 * Free the current scope and go back to the outer scope.
5546 */
5547 static void
5548drop_scope(cctx_T *cctx)
5549{
5550 scope_T *scope = cctx->ctx_scope;
5551
5552 if (scope == NULL)
5553 {
5554 iemsg("calling drop_scope() without a scope");
5555 return;
5556 }
5557 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005558 switch (scope->se_type)
5559 {
5560 case IF_SCOPE:
5561 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5562 case FOR_SCOPE:
5563 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5564 case WHILE_SCOPE:
5565 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5566 case TRY_SCOPE:
5567 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5568 case NO_SCOPE:
5569 case BLOCK_SCOPE:
5570 break;
5571 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005572 vim_free(scope);
5573}
5574
5575/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005576 * compile "if expr"
5577 *
5578 * "if expr" Produces instructions:
5579 * EVAL expr Push result of "expr"
5580 * JUMP_IF_FALSE end
5581 * ... body ...
5582 * end:
5583 *
5584 * "if expr | else" Produces instructions:
5585 * EVAL expr Push result of "expr"
5586 * JUMP_IF_FALSE else
5587 * ... body ...
5588 * JUMP_ALWAYS end
5589 * else:
5590 * ... body ...
5591 * end:
5592 *
5593 * "if expr1 | elseif expr2 | else" Produces instructions:
5594 * EVAL expr Push result of "expr"
5595 * JUMP_IF_FALSE elseif
5596 * ... body ...
5597 * JUMP_ALWAYS end
5598 * elseif:
5599 * EVAL expr Push result of "expr"
5600 * JUMP_IF_FALSE else
5601 * ... body ...
5602 * JUMP_ALWAYS end
5603 * else:
5604 * ... body ...
5605 * end:
5606 */
5607 static char_u *
5608compile_if(char_u *arg, cctx_T *cctx)
5609{
5610 char_u *p = arg;
5611 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005612 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005613 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005614 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005615 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005616
Bram Moolenaara5565e42020-05-09 15:44:01 +02005617 CLEAR_FIELD(ppconst);
5618 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005619 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005620 clear_ppconst(&ppconst);
5621 return NULL;
5622 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005623 if (cctx->ctx_skip == SKIP_YES)
5624 clear_ppconst(&ppconst);
5625 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005626 {
5627 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005628 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005629 clear_ppconst(&ppconst);
5630 }
5631 else
5632 {
5633 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005634 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005635 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005636 return NULL;
5637 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005638
5639 scope = new_scope(cctx, IF_SCOPE);
5640 if (scope == NULL)
5641 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005642 scope->se_skip_save = skip_save;
5643 // "is_had_return" will be reset if any block does not end in :return
5644 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005645
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005646 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005647 {
5648 // "where" is set when ":elseif", "else" or ":endif" is found
5649 scope->se_u.se_if.is_if_label = instr->ga_len;
5650 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5651 }
5652 else
5653 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005654
5655 return p;
5656}
5657
5658 static char_u *
5659compile_elseif(char_u *arg, cctx_T *cctx)
5660{
5661 char_u *p = arg;
5662 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005663 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005664 isn_T *isn;
5665 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005666 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02005667 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005668
5669 if (scope == NULL || scope->se_type != IF_SCOPE)
5670 {
5671 emsg(_(e_elseif_without_if));
5672 return NULL;
5673 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005674 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005675 if (!cctx->ctx_had_return)
5676 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005677
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005678 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005679 {
5680 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005681 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005682 return NULL;
5683 // previous "if" or "elseif" jumps here
5684 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5685 isn->isn_arg.jump.jump_where = instr->ga_len;
5686 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005687
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005688 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005689 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02005690 if (cctx->ctx_skip == SKIP_YES)
5691 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005692 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005693 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005694 clear_ppconst(&ppconst);
5695 return NULL;
5696 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02005697 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005698 if (scope->se_skip_save == SKIP_YES)
5699 clear_ppconst(&ppconst);
5700 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005701 {
5702 // The expression results in a constant.
5703 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005704 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005705 clear_ppconst(&ppconst);
5706 scope->se_u.se_if.is_if_label = -1;
5707 }
5708 else
5709 {
5710 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005711 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005712 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005713 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005714
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005715 // "where" is set when ":elseif", "else" or ":endif" is found
5716 scope->se_u.se_if.is_if_label = instr->ga_len;
5717 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5718 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005719
5720 return p;
5721}
5722
5723 static char_u *
5724compile_else(char_u *arg, cctx_T *cctx)
5725{
5726 char_u *p = arg;
5727 garray_T *instr = &cctx->ctx_instr;
5728 isn_T *isn;
5729 scope_T *scope = cctx->ctx_scope;
5730
5731 if (scope == NULL || scope->se_type != IF_SCOPE)
5732 {
5733 emsg(_(e_else_without_if));
5734 return NULL;
5735 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005736 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005737 if (!cctx->ctx_had_return)
5738 scope->se_u.se_if.is_had_return = FALSE;
5739 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005740
Bram Moolenaarefd88552020-06-18 20:50:10 +02005741 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005742 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005743 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005744 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005745 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005746 if (!cctx->ctx_had_return
5747 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5748 JUMP_ALWAYS, cctx) == FAIL)
5749 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005750 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005751
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005752 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005753 {
5754 if (scope->se_u.se_if.is_if_label >= 0)
5755 {
5756 // previous "if" or "elseif" jumps here
5757 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5758 isn->isn_arg.jump.jump_where = instr->ga_len;
5759 scope->se_u.se_if.is_if_label = -1;
5760 }
5761 }
5762
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005763 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005764 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5765 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005766
5767 return p;
5768}
5769
5770 static char_u *
5771compile_endif(char_u *arg, cctx_T *cctx)
5772{
5773 scope_T *scope = cctx->ctx_scope;
5774 ifscope_T *ifscope;
5775 garray_T *instr = &cctx->ctx_instr;
5776 isn_T *isn;
5777
5778 if (scope == NULL || scope->se_type != IF_SCOPE)
5779 {
5780 emsg(_(e_endif_without_if));
5781 return NULL;
5782 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005783 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005784 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005785 if (!cctx->ctx_had_return)
5786 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005787
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005788 if (scope->se_u.se_if.is_if_label >= 0)
5789 {
5790 // previous "if" or "elseif" jumps here
5791 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5792 isn->isn_arg.jump.jump_where = instr->ga_len;
5793 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005794 // Fill in the "end" label in jumps at the end of the blocks.
5795 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005796 cctx->ctx_skip = scope->se_skip_save;
5797
5798 // If all the blocks end in :return and there is an :else then the
5799 // had_return flag is set.
5800 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005801
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005802 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005803 return arg;
5804}
5805
5806/*
5807 * compile "for var in expr"
5808 *
5809 * Produces instructions:
5810 * PUSHNR -1
5811 * STORE loop-idx Set index to -1
5812 * EVAL expr Push result of "expr"
5813 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5814 * - if beyond end, jump to "end"
5815 * - otherwise get item from list and push it
5816 * STORE var Store item in "var"
5817 * ... body ...
5818 * JUMP top Jump back to repeat
5819 * end: DROP Drop the result of "expr"
5820 *
5821 */
5822 static char_u *
5823compile_for(char_u *arg, cctx_T *cctx)
5824{
5825 char_u *p;
5826 size_t varlen;
5827 garray_T *instr = &cctx->ctx_instr;
5828 garray_T *stack = &cctx->ctx_type_stack;
5829 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005830 lvar_T *loop_lvar; // loop iteration variable
5831 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005832 type_T *vartype;
5833
5834 // TODO: list of variables: "for [key, value] in dict"
5835 // parse "var"
5836 for (p = arg; eval_isnamec1(*p); ++p)
5837 ;
5838 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005839 var_lvar = lookup_local(arg, varlen, cctx);
5840 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005841 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005842 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005843 return NULL;
5844 }
5845
5846 // consume "in"
5847 p = skipwhite(p);
5848 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5849 {
5850 emsg(_(e_missing_in));
5851 return NULL;
5852 }
5853 p = skipwhite(p + 2);
5854
5855
5856 scope = new_scope(cctx, FOR_SCOPE);
5857 if (scope == NULL)
5858 return NULL;
5859
5860 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005861 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5862 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005863 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005864 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005865 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005866 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005867 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005868
5869 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005870 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5871 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005872 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005873 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005874 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005875 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005876 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005877
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005878 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005879
5880 // compile "expr", it remains on the stack until "endfor"
5881 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005882 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005883 {
5884 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005885 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005886 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005887
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005888 // Now that we know the type of "var", check that it is a list, now or at
5889 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005890 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005891 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005892 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005893 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005894 return NULL;
5895 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005896 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005897 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005898
5899 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005900 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005901
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005902 generate_FOR(cctx, loop_lvar->lv_idx);
5903 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005904
5905 return arg;
5906}
5907
5908/*
5909 * compile "endfor"
5910 */
5911 static char_u *
5912compile_endfor(char_u *arg, cctx_T *cctx)
5913{
5914 garray_T *instr = &cctx->ctx_instr;
5915 scope_T *scope = cctx->ctx_scope;
5916 forscope_T *forscope;
5917 isn_T *isn;
5918
5919 if (scope == NULL || scope->se_type != FOR_SCOPE)
5920 {
5921 emsg(_(e_for));
5922 return NULL;
5923 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005924 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005925 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005926 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005927
5928 // At end of ":for" scope jump back to the FOR instruction.
5929 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5930
5931 // Fill in the "end" label in the FOR statement so it can jump here
5932 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5933 isn->isn_arg.forloop.for_end = instr->ga_len;
5934
5935 // Fill in the "end" label any BREAK statements
5936 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5937
5938 // Below the ":for" scope drop the "expr" list from the stack.
5939 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5940 return NULL;
5941
5942 vim_free(scope);
5943
5944 return arg;
5945}
5946
5947/*
5948 * compile "while expr"
5949 *
5950 * Produces instructions:
5951 * top: EVAL expr Push result of "expr"
5952 * JUMP_IF_FALSE end jump if false
5953 * ... body ...
5954 * JUMP top Jump back to repeat
5955 * end:
5956 *
5957 */
5958 static char_u *
5959compile_while(char_u *arg, cctx_T *cctx)
5960{
5961 char_u *p = arg;
5962 garray_T *instr = &cctx->ctx_instr;
5963 scope_T *scope;
5964
5965 scope = new_scope(cctx, WHILE_SCOPE);
5966 if (scope == NULL)
5967 return NULL;
5968
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005969 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005970
5971 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005972 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005973 return NULL;
5974
5975 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005976 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005977 JUMP_IF_FALSE, cctx) == FAIL)
5978 return FAIL;
5979
5980 return p;
5981}
5982
5983/*
5984 * compile "endwhile"
5985 */
5986 static char_u *
5987compile_endwhile(char_u *arg, cctx_T *cctx)
5988{
5989 scope_T *scope = cctx->ctx_scope;
5990
5991 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5992 {
5993 emsg(_(e_while));
5994 return NULL;
5995 }
5996 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005997 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005998
5999 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006000 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006001
6002 // Fill in the "end" label in the WHILE statement so it can jump here.
6003 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006004 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006005
6006 vim_free(scope);
6007
6008 return arg;
6009}
6010
6011/*
6012 * compile "continue"
6013 */
6014 static char_u *
6015compile_continue(char_u *arg, cctx_T *cctx)
6016{
6017 scope_T *scope = cctx->ctx_scope;
6018
6019 for (;;)
6020 {
6021 if (scope == NULL)
6022 {
6023 emsg(_(e_continue));
6024 return NULL;
6025 }
6026 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6027 break;
6028 scope = scope->se_outer;
6029 }
6030
6031 // Jump back to the FOR or WHILE instruction.
6032 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006033 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6034 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006035 return arg;
6036}
6037
6038/*
6039 * compile "break"
6040 */
6041 static char_u *
6042compile_break(char_u *arg, cctx_T *cctx)
6043{
6044 scope_T *scope = cctx->ctx_scope;
6045 endlabel_T **el;
6046
6047 for (;;)
6048 {
6049 if (scope == NULL)
6050 {
6051 emsg(_(e_break));
6052 return NULL;
6053 }
6054 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6055 break;
6056 scope = scope->se_outer;
6057 }
6058
6059 // Jump to the end of the FOR or WHILE loop.
6060 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006061 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006062 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006063 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006064 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6065 return FAIL;
6066
6067 return arg;
6068}
6069
6070/*
6071 * compile "{" start of block
6072 */
6073 static char_u *
6074compile_block(char_u *arg, cctx_T *cctx)
6075{
6076 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6077 return NULL;
6078 return skipwhite(arg + 1);
6079}
6080
6081/*
6082 * compile end of block: drop one scope
6083 */
6084 static void
6085compile_endblock(cctx_T *cctx)
6086{
6087 scope_T *scope = cctx->ctx_scope;
6088
6089 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006090 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006091 vim_free(scope);
6092}
6093
6094/*
6095 * compile "try"
6096 * Creates a new scope for the try-endtry, pointing to the first catch and
6097 * finally.
6098 * Creates another scope for the "try" block itself.
6099 * TRY instruction sets up exception handling at runtime.
6100 *
6101 * "try"
6102 * TRY -> catch1, -> finally push trystack entry
6103 * ... try block
6104 * "throw {exception}"
6105 * EVAL {exception}
6106 * THROW create exception
6107 * ... try block
6108 * " catch {expr}"
6109 * JUMP -> finally
6110 * catch1: PUSH exeception
6111 * EVAL {expr}
6112 * MATCH
6113 * JUMP nomatch -> catch2
6114 * CATCH remove exception
6115 * ... catch block
6116 * " catch"
6117 * JUMP -> finally
6118 * catch2: CATCH remove exception
6119 * ... catch block
6120 * " finally"
6121 * finally:
6122 * ... finally block
6123 * " endtry"
6124 * ENDTRY pop trystack entry, may rethrow
6125 */
6126 static char_u *
6127compile_try(char_u *arg, cctx_T *cctx)
6128{
6129 garray_T *instr = &cctx->ctx_instr;
6130 scope_T *try_scope;
6131 scope_T *scope;
6132
6133 // scope that holds the jumps that go to catch/finally/endtry
6134 try_scope = new_scope(cctx, TRY_SCOPE);
6135 if (try_scope == NULL)
6136 return NULL;
6137
6138 // "catch" is set when the first ":catch" is found.
6139 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006140 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006141 if (generate_instr(cctx, ISN_TRY) == NULL)
6142 return NULL;
6143
6144 // scope for the try block itself
6145 scope = new_scope(cctx, BLOCK_SCOPE);
6146 if (scope == NULL)
6147 return NULL;
6148
6149 return arg;
6150}
6151
6152/*
6153 * compile "catch {expr}"
6154 */
6155 static char_u *
6156compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6157{
6158 scope_T *scope = cctx->ctx_scope;
6159 garray_T *instr = &cctx->ctx_instr;
6160 char_u *p;
6161 isn_T *isn;
6162
6163 // end block scope from :try or :catch
6164 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6165 compile_endblock(cctx);
6166 scope = cctx->ctx_scope;
6167
6168 // Error if not in a :try scope
6169 if (scope == NULL || scope->se_type != TRY_SCOPE)
6170 {
6171 emsg(_(e_catch));
6172 return NULL;
6173 }
6174
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006175 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006176 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006177 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006178 return NULL;
6179 }
6180
6181 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006182 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006183 JUMP_ALWAYS, cctx) == FAIL)
6184 return NULL;
6185
6186 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006187 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006188 if (isn->isn_arg.try.try_catch == 0)
6189 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006190 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006191 {
6192 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006193 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006194 isn->isn_arg.jump.jump_where = instr->ga_len;
6195 }
6196
6197 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006198 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006199 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006200 scope->se_u.se_try.ts_caught_all = TRUE;
6201 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006202 }
6203 else
6204 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006205 char_u *end;
6206 char_u *pat;
6207 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006208 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006209 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006210
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006211 // Push v:exception, push {expr} and MATCH
6212 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6213
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006214 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006215 if (*end != *p)
6216 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006217 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006218 vim_free(tofree);
6219 return FAIL;
6220 }
6221 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006222 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006223 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006224 len = (int)(end - tofree);
6225 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006226 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006227 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006228 if (pat == NULL)
6229 return FAIL;
6230 if (generate_PUSHS(cctx, pat) == FAIL)
6231 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006232
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006233 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6234 return NULL;
6235
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006236 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006237 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6238 return NULL;
6239 }
6240
6241 if (generate_instr(cctx, ISN_CATCH) == NULL)
6242 return NULL;
6243
6244 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6245 return NULL;
6246 return p;
6247}
6248
6249 static char_u *
6250compile_finally(char_u *arg, cctx_T *cctx)
6251{
6252 scope_T *scope = cctx->ctx_scope;
6253 garray_T *instr = &cctx->ctx_instr;
6254 isn_T *isn;
6255
6256 // end block scope from :try or :catch
6257 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6258 compile_endblock(cctx);
6259 scope = cctx->ctx_scope;
6260
6261 // Error if not in a :try scope
6262 if (scope == NULL || scope->se_type != TRY_SCOPE)
6263 {
6264 emsg(_(e_finally));
6265 return NULL;
6266 }
6267
6268 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006269 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006270 if (isn->isn_arg.try.try_finally != 0)
6271 {
6272 emsg(_(e_finally_dup));
6273 return NULL;
6274 }
6275
6276 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006277 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006278
Bram Moolenaar585fea72020-04-02 22:33:21 +02006279 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006280 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006281 {
6282 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006283 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006284 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006285 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006286 }
6287
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006288 // TODO: set index in ts_finally_label jumps
6289
6290 return arg;
6291}
6292
6293 static char_u *
6294compile_endtry(char_u *arg, cctx_T *cctx)
6295{
6296 scope_T *scope = cctx->ctx_scope;
6297 garray_T *instr = &cctx->ctx_instr;
6298 isn_T *isn;
6299
6300 // end block scope from :catch or :finally
6301 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6302 compile_endblock(cctx);
6303 scope = cctx->ctx_scope;
6304
6305 // Error if not in a :try scope
6306 if (scope == NULL || scope->se_type != TRY_SCOPE)
6307 {
6308 if (scope == NULL)
6309 emsg(_(e_no_endtry));
6310 else if (scope->se_type == WHILE_SCOPE)
6311 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006312 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006313 emsg(_(e_endfor));
6314 else
6315 emsg(_(e_endif));
6316 return NULL;
6317 }
6318
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006319 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006320 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6321 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006322 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006323 return NULL;
6324 }
6325
6326 // Fill in the "end" label in jumps at the end of the blocks, if not done
6327 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006328 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006329
6330 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006331 if (isn->isn_arg.try.try_catch == 0)
6332 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006333 if (isn->isn_arg.try.try_finally == 0)
6334 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006335
6336 if (scope->se_u.se_try.ts_catch_label != 0)
6337 {
6338 // Last catch without match jumps here
6339 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6340 isn->isn_arg.jump.jump_where = instr->ga_len;
6341 }
6342
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006343 compile_endblock(cctx);
6344
6345 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6346 return NULL;
6347 return arg;
6348}
6349
6350/*
6351 * compile "throw {expr}"
6352 */
6353 static char_u *
6354compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6355{
6356 char_u *p = skipwhite(arg);
6357
Bram Moolenaara5565e42020-05-09 15:44:01 +02006358 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006359 return NULL;
6360 if (may_generate_2STRING(-1, cctx) == FAIL)
6361 return NULL;
6362 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6363 return NULL;
6364
6365 return p;
6366}
6367
6368/*
6369 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006370 * compile "echomsg expr"
6371 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006372 * compile "execute expr"
6373 */
6374 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006375compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006376{
6377 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006378 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006379 int count = 0;
6380
6381 for (;;)
6382 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006383 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006384 return NULL;
6385 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006386 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006387 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006388 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006389 break;
6390 }
6391
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006392 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6393 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6394 else if (cmdidx == CMD_execute)
6395 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6396 else if (cmdidx == CMD_echomsg)
6397 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6398 else
6399 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006400 return p;
6401}
6402
6403/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006404 * :put r
6405 * :put ={expr}
6406 */
6407 static char_u *
6408compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6409{
6410 char_u *line = arg;
6411 linenr_T lnum;
6412 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006413 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006414
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006415 eap->regname = *line;
6416
6417 if (eap->regname == '=')
6418 {
6419 char_u *p = line + 1;
6420
6421 if (compile_expr0(&p, cctx) == FAIL)
6422 return NULL;
6423 line = p;
6424 }
6425 else if (eap->regname != NUL)
6426 ++line;
6427
6428 // TODO: if the range is something like "$" need to evaluate at runtime
6429 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6430 return NULL;
6431 if (eap->addr_count == 0)
6432 lnum = -1;
6433 else
6434 lnum = eap->line2;
6435 if (above)
6436 --lnum;
6437
6438 generate_PUT(cctx, eap->regname, lnum);
6439 return line;
6440}
6441
6442/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006443 * A command that is not compiled, execute with legacy code.
6444 */
6445 static char_u *
6446compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6447{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006448 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006449 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006450 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006451
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006452 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006453 goto theend;
6454
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006455 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006456 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006457 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006458 int usefilter = FALSE;
6459
6460 has_expr = argt & (EX_XFILE | EX_EXPAND);
6461
6462 // If the command can be followed by a bar, find the bar and truncate
6463 // it, so that the following command can be compiled.
6464 // The '|' is overwritten with a NUL, it is put back below.
6465 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6466 && *eap->arg == '!')
6467 // :w !filter or :r !filter or :r! filter
6468 usefilter = TRUE;
6469 if ((argt & EX_TRLBAR) && !usefilter)
6470 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006471 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006472 separate_nextcmd(eap);
6473 if (eap->nextcmd != NULL)
6474 nextcmd = eap->nextcmd;
6475 }
6476 }
6477
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006478 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6479 {
6480 // expand filename in "syntax include [@group] filename"
6481 has_expr = TRUE;
6482 eap->arg = skipwhite(eap->arg + 7);
6483 if (*eap->arg == '@')
6484 eap->arg = skiptowhite(eap->arg);
6485 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006486
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006487 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006488 {
6489 int count = 0;
6490 char_u *start = skipwhite(line);
6491
6492 // :cmd xxx`=expr1`yyy`=expr2`zzz
6493 // PUSHS ":cmd xxx"
6494 // eval expr1
6495 // PUSHS "yyy"
6496 // eval expr2
6497 // PUSHS "zzz"
6498 // EXECCONCAT 5
6499 for (;;)
6500 {
6501 if (p > start)
6502 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006503 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006504 ++count;
6505 }
6506 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006507 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006508 return NULL;
6509 may_generate_2STRING(-1, cctx);
6510 ++count;
6511 p = skipwhite(p);
6512 if (*p != '`')
6513 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006514 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006515 return NULL;
6516 }
6517 start = p + 1;
6518
6519 p = (char_u *)strstr((char *)start, "`=");
6520 if (p == NULL)
6521 {
6522 if (*skipwhite(start) != NUL)
6523 {
6524 generate_PUSHS(cctx, vim_strsave(start));
6525 ++count;
6526 }
6527 break;
6528 }
6529 }
6530 generate_EXECCONCAT(cctx, count);
6531 }
6532 else
6533 generate_EXEC(cctx, line);
6534
6535theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006536 if (*nextcmd != NUL)
6537 {
6538 // the parser expects a pointer to the bar, put it back
6539 --nextcmd;
6540 *nextcmd = '|';
6541 }
6542
6543 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006544}
6545
6546/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006547 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006548 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006549 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006550 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006551add_def_function(ufunc_T *ufunc)
6552{
6553 dfunc_T *dfunc;
6554
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006555 if (def_functions.ga_len == 0)
6556 {
6557 // The first position is not used, so that a zero uf_dfunc_idx means it
6558 // wasn't set.
6559 if (ga_grow(&def_functions, 1) == FAIL)
6560 return FAIL;
6561 ++def_functions.ga_len;
6562 }
6563
Bram Moolenaar09689a02020-05-09 22:50:08 +02006564 // Add the function to "def_functions".
6565 if (ga_grow(&def_functions, 1) == FAIL)
6566 return FAIL;
6567 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6568 CLEAR_POINTER(dfunc);
6569 dfunc->df_idx = def_functions.ga_len;
6570 ufunc->uf_dfunc_idx = dfunc->df_idx;
6571 dfunc->df_ufunc = ufunc;
6572 ++def_functions.ga_len;
6573 return OK;
6574}
6575
6576/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006577 * After ex_function() has collected all the function lines: parse and compile
6578 * the lines into instructions.
6579 * Adds the function to "def_functions".
6580 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6581 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006582 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006583 * This can be used recursively through compile_lambda(), which may reallocate
6584 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006585 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006586 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006587 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006588compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006589{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006590 char_u *line = NULL;
6591 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006592 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006593 cctx_T cctx;
6594 garray_T *instr;
6595 int called_emsg_before = called_emsg;
6596 int ret = FAIL;
6597 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006598 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006599 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006600 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006601
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006602 // When using a function that was compiled before: Free old instructions.
6603 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006604 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006605 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006606 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6607 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006608 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006609 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006610 else
6611 {
6612 if (add_def_function(ufunc) == FAIL)
6613 return FAIL;
6614 new_def_function = TRUE;
6615 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006616
Bram Moolenaar985116a2020-07-12 17:31:09 +02006617 ufunc->uf_def_status = UF_COMPILING;
6618
Bram Moolenaara80faa82020-04-12 19:37:17 +02006619 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006620 cctx.ctx_ufunc = ufunc;
6621 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006622 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006623 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6624 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6625 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6626 cctx.ctx_type_list = &ufunc->uf_type_list;
6627 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6628 instr = &cctx.ctx_instr;
6629
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006630 // Set the context to the function, it may be compiled when called from
6631 // another script. Set the script version to the most modern one.
6632 // The line number will be set in next_line_from_context().
6633 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006634 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6635
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006636 // Make sure error messages are OK.
6637 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6638 if (do_estack_push)
6639 estack_push_ufunc(ufunc, 1);
6640
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006641 if (ufunc->uf_def_args.ga_len > 0)
6642 {
6643 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006644 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006645 int i;
6646 char_u *arg;
6647 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006648 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006649
6650 // Produce instructions for the default values of optional arguments.
6651 // Store the instruction index in uf_def_arg_idx[] so that we know
6652 // where to start when the function is called, depending on the number
6653 // of arguments.
6654 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6655 if (ufunc->uf_def_arg_idx == NULL)
6656 goto erret;
6657 for (i = 0; i < count; ++i)
6658 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006659 garray_T *stack = &cctx.ctx_type_stack;
6660 type_T *val_type;
6661 int arg_idx = first_def_arg + i;
6662
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006663 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6664 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006665 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006666 goto erret;
6667
6668 // If no type specified use the type of the default value.
6669 // Otherwise check that the default value type matches the
6670 // specified type.
6671 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6672 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006673 {
6674 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006675 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006676 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02006677 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
6678 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006679 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006680
6681 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006682 goto erret;
6683 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006684 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006685
6686 if (did_set_arg_type)
6687 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006688 }
6689
6690 /*
6691 * Loop over all the lines of the function and generate instructions.
6692 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006693 for (;;)
6694 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006695 exarg_T ea;
6696 cmdmod_T save_cmdmod;
6697 int starts_with_colon = FALSE;
6698 char_u *cmd;
6699 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006700
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006701 // Bail out on the first error to avoid a flood of errors and report
6702 // the right line number when inside try/catch.
6703 if (emsg_before != called_emsg)
6704 goto erret;
6705
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006706 if (line != NULL && *line == '|')
6707 // the line continues after a '|'
6708 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006709 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006710 && !(*line == '#' && (line == cctx.ctx_line_start
6711 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006712 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006713 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006714 goto erret;
6715 }
6716 else
6717 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006718 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006719 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006720 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006721 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006722 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006723 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006724
Bram Moolenaara80faa82020-04-12 19:37:17 +02006725 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006726 ea.cmdlinep = &line;
6727 ea.cmd = skipwhite(line);
6728
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006729 // Some things can be recognized by the first character.
6730 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006731 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006732 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006733 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006734 if (ea.cmd[1] != '{')
6735 {
6736 line = (char_u *)"";
6737 continue;
6738 }
6739 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006740
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006741 case '}':
6742 {
6743 // "}" ends a block scope
6744 scopetype_T stype = cctx.ctx_scope == NULL
6745 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006746
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006747 if (stype == BLOCK_SCOPE)
6748 {
6749 compile_endblock(&cctx);
6750 line = ea.cmd;
6751 }
6752 else
6753 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006754 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006755 goto erret;
6756 }
6757 if (line != NULL)
6758 line = skipwhite(ea.cmd + 1);
6759 continue;
6760 }
6761
6762 case '{':
6763 // "{" starts a block scope
6764 // "{'a': 1}->func() is something else
6765 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6766 {
6767 line = compile_block(ea.cmd, &cctx);
6768 continue;
6769 }
6770 break;
6771
6772 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006773 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006774 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006775 }
6776
6777 /*
6778 * COMMAND MODIFIERS
6779 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006780 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006781 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6782 {
6783 if (errormsg != NULL)
6784 goto erret;
6785 // empty line or comment
6786 line = (char_u *)"";
6787 continue;
6788 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006789 // TODO: use modifiers in the command
6790 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006791 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006792
6793 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006794 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006795 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006796 {
6797 if (*ea.cmd == '(')
6798 // not for "call()"
6799 ea.cmd = p;
6800 else
6801 ea.cmd = skipwhite(ea.cmd);
6802 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006803
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006804 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006805 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006806 char_u *pskip;
6807
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006808 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006809 // find what follows.
6810 // Skip over "var.member", "var[idx]" and the like.
6811 // Also "&opt = val", "$ENV = val" and "@r = val".
6812 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006813 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006814 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006815 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006816 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006817 char_u *var_end;
6818 int oplen;
6819 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006820
Bram Moolenaar65821722020-08-02 18:58:54 +02006821 if (ea.cmd[0] == '@')
6822 var_end = ea.cmd + 2;
6823 else
6824 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006825 FNE_CHECK_START | FNE_INCL_BR);
6826 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006827 if (oplen > 0)
6828 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006829 size_t len = p - ea.cmd;
6830
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006831 // Recognize an assignment if we recognize the variable
6832 // name:
6833 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006834 // "local = expr" where "local" is a local var.
6835 // "script = expr" where "script" is a script-local var.
6836 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006837 // "&opt = expr"
6838 // "$ENV = expr"
6839 // "@r = expr"
6840 if (*ea.cmd == '&'
6841 || *ea.cmd == '$'
6842 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006843 || ((len) > 2 && ea.cmd[1] == ':')
6844 || lookup_local(ea.cmd, len, &cctx) != NULL
6845 || lookup_arg(ea.cmd, len, NULL, NULL,
6846 NULL, &cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02006847 || lookup_script(ea.cmd, len, FALSE) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006848 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006849 {
6850 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006851 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006852 goto erret;
6853 continue;
6854 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006855 }
6856 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006857
6858 if (*ea.cmd == '[')
6859 {
6860 // [var, var] = expr
6861 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6862 if (line == NULL)
6863 goto erret;
6864 if (line != ea.cmd)
6865 continue;
6866 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006867 }
6868
6869 /*
6870 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006871 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006872 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006873 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006874 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006875 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02006876 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006877 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006878 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006879 if (!starts_with_colon)
6880 {
6881 emsg(_(e_colon_required_before_a_range));
6882 goto erret;
6883 }
6884 if (ends_excmd2(line, ea.cmd))
6885 {
6886 // A range without a command: jump to the line.
6887 // TODO: compile to a more efficient command, possibly
6888 // calling parse_cmd_address().
6889 ea.cmdidx = CMD_SIZE;
6890 line = compile_exec(line, &ea, &cctx);
6891 goto nextline;
6892 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006893 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006894 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006895 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006896 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006897 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006898
6899 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6900 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006901 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006902 {
6903 line += STRLEN(line);
6904 continue;
6905 }
6906
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006907 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006908 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006909 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006910 // CMD_let cannot happen, compile_assignment() above is used
6911 iemsg("Command from find_ex_command() not handled");
6912 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006913 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006914 }
6915
Bram Moolenaar3988f642020-08-27 22:43:03 +02006916 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006917 && ea.cmdidx != CMD_elseif
6918 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02006919 && ea.cmdidx != CMD_endif
6920 && ea.cmdidx != CMD_endfor
6921 && ea.cmdidx != CMD_endwhile
6922 && ea.cmdidx != CMD_catch
6923 && ea.cmdidx != CMD_finally
6924 && ea.cmdidx != CMD_endtry)
6925 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02006926 emsg(_(e_unreachable_code_after_return));
6927 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006928 }
6929
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006930 p = skipwhite(p);
6931 if (ea.cmdidx != CMD_SIZE
6932 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
6933 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02006934 if (ea.cmdidx >= 0)
6935 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006936 if ((ea.argt & EX_BANG) && *p == '!')
6937 {
6938 ea.forceit = TRUE;
6939 p = skipwhite(p + 1);
6940 }
6941 }
6942
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006943 switch (ea.cmdidx)
6944 {
6945 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006946 ea.arg = p;
6947 line = compile_nested_function(&ea, &cctx);
6948 break;
6949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006950 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006951 // TODO: should we allow this, e.g. to declare a global
6952 // function?
6953 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006954 goto erret;
6955
6956 case CMD_return:
6957 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006958 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006959 break;
6960
6961 case CMD_let:
6962 case CMD_const:
6963 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006964 if (line == p)
6965 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006966 break;
6967
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006968 case CMD_unlet:
6969 case CMD_unlockvar:
6970 case CMD_lockvar:
6971 line = compile_unletlock(p, &ea, &cctx);
6972 break;
6973
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006974 case CMD_import:
6975 line = compile_import(p, &cctx);
6976 break;
6977
6978 case CMD_if:
6979 line = compile_if(p, &cctx);
6980 break;
6981 case CMD_elseif:
6982 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006983 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006984 break;
6985 case CMD_else:
6986 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006987 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006988 break;
6989 case CMD_endif:
6990 line = compile_endif(p, &cctx);
6991 break;
6992
6993 case CMD_while:
6994 line = compile_while(p, &cctx);
6995 break;
6996 case CMD_endwhile:
6997 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006998 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006999 break;
7000
7001 case CMD_for:
7002 line = compile_for(p, &cctx);
7003 break;
7004 case CMD_endfor:
7005 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007006 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007007 break;
7008 case CMD_continue:
7009 line = compile_continue(p, &cctx);
7010 break;
7011 case CMD_break:
7012 line = compile_break(p, &cctx);
7013 break;
7014
7015 case CMD_try:
7016 line = compile_try(p, &cctx);
7017 break;
7018 case CMD_catch:
7019 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007020 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007021 break;
7022 case CMD_finally:
7023 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007024 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007025 break;
7026 case CMD_endtry:
7027 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007028 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007029 break;
7030 case CMD_throw:
7031 line = compile_throw(p, &cctx);
7032 break;
7033
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007034 case CMD_eval:
7035 if (compile_expr0(&p, &cctx) == FAIL)
7036 goto erret;
7037
Bram Moolenaar3988f642020-08-27 22:43:03 +02007038 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007039 generate_instr_drop(&cctx, ISN_DROP, 1);
7040
7041 line = skipwhite(p);
7042 break;
7043
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007044 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007045 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007046 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007047 case CMD_echomsg:
7048 case CMD_echoerr:
7049 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007050 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007051
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007052 case CMD_put:
7053 ea.cmd = cmd;
7054 line = compile_put(p, &ea, &cctx);
7055 break;
7056
Bram Moolenaar3988f642020-08-27 22:43:03 +02007057 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007058
Bram Moolenaarae616492020-07-28 20:07:27 +02007059 case CMD_append:
7060 case CMD_change:
7061 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007062 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007063 case CMD_xit:
7064 not_in_vim9(&ea);
7065 goto erret;
7066
Bram Moolenaar002262f2020-07-08 17:47:57 +02007067 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007068 if (cctx.ctx_skip != SKIP_YES)
7069 {
7070 semsg(_(e_invalid_command_str), ea.cmd);
7071 goto erret;
7072 }
7073 // We don't check for a next command here.
7074 line = (char_u *)"";
7075 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007076
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007077 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007078 if (cctx.ctx_skip == SKIP_YES)
7079 {
7080 // We don't check for a next command here.
7081 line = (char_u *)"";
7082 }
7083 else
7084 {
7085 // Not recognized, execute with do_cmdline_cmd().
7086 ea.arg = p;
7087 line = compile_exec(line, &ea, &cctx);
7088 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007089 break;
7090 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007091nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007092 if (line == NULL)
7093 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007094 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007095
7096 if (cctx.ctx_type_stack.ga_len < 0)
7097 {
7098 iemsg("Type stack underflow");
7099 goto erret;
7100 }
7101 }
7102
7103 if (cctx.ctx_scope != NULL)
7104 {
7105 if (cctx.ctx_scope->se_type == IF_SCOPE)
7106 emsg(_(e_endif));
7107 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7108 emsg(_(e_endwhile));
7109 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7110 emsg(_(e_endfor));
7111 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007112 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007113 goto erret;
7114 }
7115
Bram Moolenaarefd88552020-06-18 20:50:10 +02007116 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007117 {
7118 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7119 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007120 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007121 goto erret;
7122 }
7123
7124 // Return zero if there is no return at the end.
7125 generate_PUSHNR(&cctx, 0);
7126 generate_instr(&cctx, ISN_RETURN);
7127 }
7128
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007129 {
7130 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7131 + ufunc->uf_dfunc_idx;
7132 dfunc->df_deleted = FALSE;
7133 dfunc->df_instr = instr->ga_data;
7134 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007135 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007136 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007137 if (cctx.ctx_outer_used)
7138 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007139 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007140 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007141
7142 ret = OK;
7143
7144erret:
7145 if (ret == FAIL)
7146 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007147 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007148 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7149 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007150
7151 for (idx = 0; idx < instr->ga_len; ++idx)
7152 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007153 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007154
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007155 // If using the last entry in the table and it was added above, we
7156 // might as well remove it.
7157 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007158 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007159 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007160 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007161 ufunc->uf_dfunc_idx = 0;
7162 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007163 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007164
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007165 while (cctx.ctx_scope != NULL)
7166 drop_scope(&cctx);
7167
Bram Moolenaar20431c92020-03-20 18:39:46 +01007168 // Don't execute this function body.
7169 ga_clear_strings(&ufunc->uf_lines);
7170
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007171 if (errormsg != NULL)
7172 emsg(errormsg);
7173 else if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007174 emsg(_(e_compile_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007175 }
7176
7177 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007178 if (do_estack_push)
7179 estack_pop();
7180
Bram Moolenaar20431c92020-03-20 18:39:46 +01007181 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007182 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007183 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007184 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007185}
7186
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007187 void
7188set_function_type(ufunc_T *ufunc)
7189{
7190 int varargs = ufunc->uf_va_name != NULL;
7191 int argcount = ufunc->uf_args.ga_len;
7192
7193 // Create a type for the function, with the return type and any
7194 // argument types.
7195 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7196 // The type is included in "tt_args".
7197 if (argcount > 0 || varargs)
7198 {
7199 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7200 argcount, &ufunc->uf_type_list);
7201 // Add argument types to the function type.
7202 if (func_type_add_arg_types(ufunc->uf_func_type,
7203 argcount + varargs,
7204 &ufunc->uf_type_list) == FAIL)
7205 return;
7206 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7207 ufunc->uf_func_type->tt_min_argcount =
7208 argcount - ufunc->uf_def_args.ga_len;
7209 if (ufunc->uf_arg_types == NULL)
7210 {
7211 int i;
7212
7213 // lambda does not have argument types.
7214 for (i = 0; i < argcount; ++i)
7215 ufunc->uf_func_type->tt_args[i] = &t_any;
7216 }
7217 else
7218 mch_memmove(ufunc->uf_func_type->tt_args,
7219 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7220 if (varargs)
7221 {
7222 ufunc->uf_func_type->tt_args[argcount] =
7223 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7224 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7225 }
7226 }
7227 else
7228 // No arguments, can use a predefined type.
7229 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7230 argcount, &ufunc->uf_type_list);
7231}
7232
7233
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007234/*
7235 * Delete an instruction, free what it contains.
7236 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007237 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007238delete_instr(isn_T *isn)
7239{
7240 switch (isn->isn_type)
7241 {
7242 case ISN_EXEC:
7243 case ISN_LOADENV:
7244 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007245 case ISN_LOADB:
7246 case ISN_LOADW:
7247 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007248 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007249 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007250 case ISN_PUSHEXC:
7251 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007252 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007253 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007254 case ISN_STOREB:
7255 case ISN_STOREW:
7256 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007257 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007258 vim_free(isn->isn_arg.string);
7259 break;
7260
7261 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007262 case ISN_STORES:
7263 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007264 break;
7265
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007266 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007267 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007268 vim_free(isn->isn_arg.unlet.ul_name);
7269 break;
7270
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007271 case ISN_STOREOPT:
7272 vim_free(isn->isn_arg.storeopt.so_name);
7273 break;
7274
7275 case ISN_PUSHBLOB: // push blob isn_arg.blob
7276 blob_unref(isn->isn_arg.blob);
7277 break;
7278
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007279 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007280#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007281 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007282#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007283 break;
7284
7285 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007286#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007287 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007288#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007289 break;
7290
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007291 case ISN_UCALL:
7292 vim_free(isn->isn_arg.ufunc.cuf_name);
7293 break;
7294
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007295 case ISN_FUNCREF:
7296 {
7297 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7298 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007299
7300 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7301 func_ptr_unref(dfunc->df_ufunc);
7302 }
7303 break;
7304
7305 case ISN_DCALL:
7306 {
7307 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7308 + isn->isn_arg.dfunc.cdf_idx;
7309
7310 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7311 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007312 }
7313 break;
7314
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007315 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007316 {
7317 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7318 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7319
7320 if (ufunc != NULL)
7321 {
7322 // Clear uf_dfunc_idx so that the function is deleted.
7323 clear_def_function(ufunc);
7324 ufunc->uf_dfunc_idx = 0;
7325 func_ptr_unref(ufunc);
7326 }
7327
7328 vim_free(lambda);
7329 vim_free(isn->isn_arg.newfunc.nf_global);
7330 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007331 break;
7332
Bram Moolenaar5e654232020-09-16 15:22:00 +02007333 case ISN_CHECKTYPE:
7334 free_type(isn->isn_arg.type.ct_type);
7335 break;
7336
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007337 case ISN_2BOOL:
7338 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007339 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007340 case ISN_ADDBLOB:
7341 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007342 case ISN_ANYINDEX:
7343 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007344 case ISN_BCALL:
7345 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007346 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007347 case ISN_CHECKNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007348 case ISN_COMPAREANY:
7349 case ISN_COMPAREBLOB:
7350 case ISN_COMPAREBOOL:
7351 case ISN_COMPAREDICT:
7352 case ISN_COMPAREFLOAT:
7353 case ISN_COMPAREFUNC:
7354 case ISN_COMPARELIST:
7355 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007356 case ISN_COMPARESPECIAL:
7357 case ISN_COMPARESTRING:
7358 case ISN_CONCAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007359 case ISN_DROP:
7360 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007361 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007362 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007363 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007364 case ISN_EXECCONCAT:
7365 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007366 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007367 case ISN_GETITEM:
7368 case ISN_JUMP:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007369 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007370 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007371 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007372 case ISN_LOADBDICT:
7373 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007374 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007375 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007376 case ISN_LOADSCRIPT:
7377 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007378 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007379 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007380 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007381 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007382 case ISN_NEGATENR:
7383 case ISN_NEWDICT:
7384 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007385 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007386 case ISN_OPFLOAT:
7387 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007388 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007389 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007390 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007391 case ISN_PUSHF:
7392 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007393 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007394 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007395 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007396 case ISN_SHUFFLE:
7397 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007398 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007399 case ISN_STOREDICT:
7400 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007401 case ISN_STORENR:
7402 case ISN_STOREOUTER:
7403 case ISN_STOREREG:
7404 case ISN_STORESCRIPT:
7405 case ISN_STOREV:
7406 case ISN_STRINDEX:
7407 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007408 case ISN_THROW:
7409 case ISN_TRY:
7410 // nothing allocated
7411 break;
7412 }
7413}
7414
7415/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007416 * Free all instructions for "dfunc".
7417 */
7418 static void
7419delete_def_function_contents(dfunc_T *dfunc)
7420{
7421 int idx;
7422
7423 ga_clear(&dfunc->df_def_args_isn);
7424
7425 if (dfunc->df_instr != NULL)
7426 {
7427 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7428 delete_instr(dfunc->df_instr + idx);
7429 VIM_CLEAR(dfunc->df_instr);
7430 }
7431
7432 dfunc->df_deleted = TRUE;
7433}
7434
7435/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007436 * When a user function is deleted, clear the contents of any associated def
7437 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007438 */
7439 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007440clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007441{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007442 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007443 {
7444 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7445 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007446
Bram Moolenaar20431c92020-03-20 18:39:46 +01007447 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007448 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007449 }
7450}
7451
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007452/*
7453 * Used when a user function is about to be deleted: remove the pointer to it.
7454 * The entry in def_functions is then unused.
7455 */
7456 void
7457unlink_def_function(ufunc_T *ufunc)
7458{
7459 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7460
7461 dfunc->df_ufunc = NULL;
7462}
7463
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007464#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007465/*
7466 * Free all functions defined with ":def".
7467 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007468 void
7469free_def_functions(void)
7470{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007471 int idx;
7472
7473 for (idx = 0; idx < def_functions.ga_len; ++idx)
7474 {
7475 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7476
7477 delete_def_function_contents(dfunc);
7478 }
7479
7480 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007481}
7482#endif
7483
7484
7485#endif // FEAT_EVAL