blob: a6b4ba4098b996aeb85aedb4d3c2e805f58faad3 [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 Moolenaar0cb5bcf2020-06-20 18:19:09 +02001455 if (ufunc->uf_def_status != UF_NOT_COMPILED)
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 Moolenaar8a7d6542020-01-26 15:56:19 +01002293
Bram Moolenaare6085c52020-04-12 20:19:16 +02002294 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002295 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002296 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2297 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002298 if (*p == ')')
2299 {
2300 *arg = p + 1;
2301 return OK;
2302 }
2303
Bram Moolenaara5565e42020-05-09 15:44:01 +02002304 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305 return FAIL;
2306 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002307
2308 if (*p != ',' && *skipwhite(p) == ',')
2309 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002310 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002311 p = skipwhite(p);
2312 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002314 {
2315 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002316 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002317 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002318 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002319 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002320 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002321 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002322failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002323 emsg(_(e_missing_close));
2324 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002325}
2326
2327/*
2328 * Compile a function call: name(arg1, arg2)
2329 * "arg" points to "name", "arg + varlen" to the "(".
2330 * "argcount_init" is 1 for "value->method()"
2331 * Instructions:
2332 * EVAL arg1
2333 * EVAL arg2
2334 * BCALL / DCALL / UCALL
2335 */
2336 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002337compile_call(
2338 char_u **arg,
2339 size_t varlen,
2340 cctx_T *cctx,
2341 ppconst_T *ppconst,
2342 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002343{
2344 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002345 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002346 int argcount = argcount_init;
2347 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002348 char_u fname_buf[FLEN_FIXED + 1];
2349 char_u *tofree = NULL;
2350 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002351 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002352 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002353 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002354
Bram Moolenaara5565e42020-05-09 15:44:01 +02002355 // we can evaluate "has('name')" at compile time
2356 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2357 {
2358 char_u *s = skipwhite(*arg + varlen + 1);
2359 typval_T argvars[2];
2360
2361 argvars[0].v_type = VAR_UNKNOWN;
2362 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002363 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002364 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002365 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002366 s = skipwhite(s);
2367 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2368 {
2369 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2370
2371 *arg = s + 1;
2372 argvars[1].v_type = VAR_UNKNOWN;
2373 tv->v_type = VAR_NUMBER;
2374 tv->vval.v_number = 0;
2375 f_has(argvars, tv);
2376 clear_tv(&argvars[0]);
2377 ++ppconst->pp_used;
2378 return OK;
2379 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002380 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002381 }
2382
2383 if (generate_ppconst(cctx, ppconst) == FAIL)
2384 return FAIL;
2385
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002386 if (varlen >= sizeof(namebuf))
2387 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002388 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002389 return FAIL;
2390 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002391 vim_strncpy(namebuf, *arg, varlen);
2392 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393
2394 *arg = skipwhite(*arg + varlen + 1);
2395 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002396 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397
Bram Moolenaara1773442020-08-12 15:21:22 +02002398 is_autoload = vim_strchr(name, '#') != NULL;
2399 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002400 {
2401 int idx;
2402
2403 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002404 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002406 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002407 else
2408 semsg(_(e_unknownfunc), namebuf);
2409 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002410 }
2411
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002412 // If we can find the function by name generate the right call.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002413 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002414 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar0f769812020-09-12 18:32:34 +02002415 if (ufunc != NULL && !func_is_global(ufunc))
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002416 {
2417 res = generate_CALL(cctx, ufunc, argcount);
2418 goto theend;
2419 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002420
2421 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002422 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002423 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002424 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002425 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002426 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002427 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002428 garray_T *stack = &cctx->ctx_type_stack;
2429 type_T *type;
2430
2431 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2432 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002433 goto theend;
2434 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435
Bram Moolenaar0f769812020-09-12 18:32:34 +02002436 // If we can find a global function by name generate the right call.
2437 if (ufunc != NULL)
2438 {
2439 res = generate_CALL(cctx, ufunc, argcount);
2440 goto theend;
2441 }
2442
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002443 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002444 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002445 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002446 res = generate_UCALL(cctx, name, argcount);
2447 else
2448 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002449
2450theend:
2451 vim_free(tofree);
2452 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002453}
2454
2455// like NAMESPACE_CHAR but with 'a' and 'l'.
2456#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2457
2458/*
2459 * Find the end of a variable or function name. Unlike find_name_end() this
2460 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002461 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002462 * Return a pointer to just after the name. Equal to "arg" if there is no
2463 * valid name.
2464 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002465 static char_u *
2466to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467{
2468 char_u *p;
2469
2470 // Quick check for valid starting character.
2471 if (!eval_isnamec1(*arg))
2472 return arg;
2473
2474 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2475 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2476 // and can be used in slice "[n:]".
2477 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002478 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2480 break;
2481 return p;
2482}
2483
2484/*
2485 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002486 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487 */
2488 char_u *
2489to_name_const_end(char_u *arg)
2490{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002491 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 typval_T rettv;
2493
2494 if (p == arg && *arg == '[')
2495 {
2496
2497 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002498 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002499 p = arg;
2500 }
2501 else if (p == arg && *arg == '#' && arg[1] == '{')
2502 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002503 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002504 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002505 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506 p = arg;
2507 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002508
2509 return p;
2510}
2511
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512/*
2513 * parse a list: [expr, expr]
2514 * "*arg" points to the '['.
2515 */
2516 static int
2517compile_list(char_u **arg, cctx_T *cctx)
2518{
2519 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002520 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521 int count = 0;
2522
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002523 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002524 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002525 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002526 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002527 semsg(_(e_list_end), *arg);
2528 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002529 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002530 if (*p == ',')
2531 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002532 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002533 return FAIL;
2534 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002535 if (*p == ']')
2536 {
2537 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002538 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002539 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002540 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002541 break;
2542 ++count;
2543 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002544 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002546 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2547 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002548 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002549 return FAIL;
2550 }
2551 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002552 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553 p = skipwhite(p);
2554 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002555 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002556
2557 generate_NEWLIST(cctx, count);
2558 return OK;
2559}
2560
2561/*
2562 * parse a lambda: {arg, arg -> expr}
2563 * "*arg" points to the '{'.
2564 */
2565 static int
2566compile_lambda(char_u **arg, cctx_T *cctx)
2567{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568 typval_T rettv;
2569 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002570 evalarg_T evalarg;
2571
2572 CLEAR_FIELD(evalarg);
2573 evalarg.eval_flags = EVAL_EVALUATE;
2574 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575
2576 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002577 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002579
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002580 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002581 ++ufunc->uf_refcount;
2582 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002583 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584
2585 // The function will have one line: "return {expr}".
2586 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002587 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002589 clear_evalarg(&evalarg, NULL);
2590
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002591 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002592 {
2593 // The return type will now be known.
2594 set_function_type(ufunc);
2595
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002596 // The function reference count will be 1. When the ISN_FUNCREF
2597 // instruction is deleted the reference count is decremented and the
2598 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002599 return generate_FUNCREF(cctx, ufunc);
2600 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002601
2602 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603 return FAIL;
2604}
2605
2606/*
2607 * Compile a lamda call: expr->{lambda}(args)
2608 * "arg" points to the "{".
2609 */
2610 static int
2611compile_lambda_call(char_u **arg, cctx_T *cctx)
2612{
2613 ufunc_T *ufunc;
2614 typval_T rettv;
2615 int argcount = 1;
2616 int ret = FAIL;
2617
2618 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002619 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002620 return FAIL;
2621
2622 if (**arg != '(')
2623 {
2624 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002625 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002626 else
2627 semsg(_(e_missing_paren), "lambda");
2628 clear_tv(&rettv);
2629 return FAIL;
2630 }
2631
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632 ufunc = rettv.vval.v_partial->pt_func;
2633 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002634 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002635 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002636
2637 // The function will have one line: "return {expr}".
2638 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002639 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640
2641 // compile the arguments
2642 *arg = skipwhite(*arg + 1);
2643 if (compile_arguments(arg, cctx, &argcount) == OK)
2644 // call the compiled function
2645 ret = generate_CALL(cctx, ufunc, argcount);
2646
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002647 if (ret == FAIL)
2648 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649 return ret;
2650}
2651
2652/*
2653 * parse a dict: {'key': val} or #{key: val}
2654 * "*arg" points to the '{'.
2655 */
2656 static int
2657compile_dict(char_u **arg, cctx_T *cctx, int literal)
2658{
2659 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002660 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661 int count = 0;
2662 dict_T *d = dict_alloc();
2663 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002664 char_u *whitep = *arg;
2665 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666
2667 if (d == NULL)
2668 return FAIL;
2669 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002670 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002671 {
2672 char_u *key = NULL;
2673
Bram Moolenaar23c55272020-06-21 16:58:13 +02002674 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002675 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002676 *arg = NULL;
2677 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002678 }
2679
2680 if (**arg == '}')
2681 break;
2682
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002683 if (literal)
2684 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002685 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002686
Bram Moolenaar2c330432020-04-13 14:41:35 +02002687 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002688 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002689 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002690 return FAIL;
2691 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002692 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002693 if (generate_PUSHS(cctx, key) == FAIL)
2694 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002695 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002696 }
2697 else
2698 {
2699 isn_T *isn;
2700
Bram Moolenaara5565e42020-05-09 15:44:01 +02002701 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002703 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2704 if (isn->isn_type == ISN_PUSHS)
2705 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002706 else
2707 {
2708 type_T *keytype = ((type_T **)stack->ga_data)
2709 [stack->ga_len - 1];
2710 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2711 return FAIL;
2712 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713 }
2714
2715 // Check for duplicate keys, if using string keys.
2716 if (key != NULL)
2717 {
2718 item = dict_find(d, key, -1);
2719 if (item != NULL)
2720 {
2721 semsg(_(e_duplicate_key), key);
2722 goto failret;
2723 }
2724 item = dictitem_alloc(key);
2725 if (item != NULL)
2726 {
2727 item->di_tv.v_type = VAR_UNKNOWN;
2728 item->di_tv.v_lock = 0;
2729 if (dict_add(d, item) == FAIL)
2730 dictitem_free(item);
2731 }
2732 }
2733
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002734 if (**arg != ':')
2735 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002736 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002737 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002738 else
2739 semsg(_(e_missing_dict_colon), *arg);
2740 return FAIL;
2741 }
2742 whitep = *arg + 1;
2743 if (!IS_WHITE_OR_NUL(*whitep))
2744 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002745 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002746 return FAIL;
2747 }
2748
2749 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002750 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002751 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002752 *arg = NULL;
2753 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002754 }
2755
Bram Moolenaara5565e42020-05-09 15:44:01 +02002756 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002757 return FAIL;
2758 ++count;
2759
Bram Moolenaar2c330432020-04-13 14:41:35 +02002760 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002761 *arg = skipwhite(*arg);
2762 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002763 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002764 *arg = NULL;
2765 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002766 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002767 if (**arg == '}')
2768 break;
2769 if (**arg != ',')
2770 {
2771 semsg(_(e_missing_dict_comma), *arg);
2772 goto failret;
2773 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002774 if (IS_WHITE_OR_NUL(*whitep))
2775 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002776 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002777 return FAIL;
2778 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002779 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002780 *arg = skipwhite(*arg + 1);
2781 }
2782
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783 *arg = *arg + 1;
2784
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002785 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002786 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002787 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002788 *arg += STRLEN(*arg);
2789
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002790 dict_unref(d);
2791 return generate_NEWDICT(cctx, count);
2792
2793failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002794 if (*arg == NULL)
2795 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796 dict_unref(d);
2797 return FAIL;
2798}
2799
2800/*
2801 * Compile "&option".
2802 */
2803 static int
2804compile_get_option(char_u **arg, cctx_T *cctx)
2805{
2806 typval_T rettv;
2807 char_u *start = *arg;
2808 int ret;
2809
2810 // parse the option and get the current value to get the type.
2811 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002812 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 if (ret == OK)
2814 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002815 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 char_u *name = vim_strnsave(start, *arg - start);
2817 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2818
2819 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2820 vim_free(name);
2821 }
2822 clear_tv(&rettv);
2823
2824 return ret;
2825}
2826
2827/*
2828 * Compile "$VAR".
2829 */
2830 static int
2831compile_get_env(char_u **arg, cctx_T *cctx)
2832{
2833 char_u *start = *arg;
2834 int len;
2835 int ret;
2836 char_u *name;
2837
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 ++*arg;
2839 len = get_env_len(arg);
2840 if (len == 0)
2841 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002842 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843 return FAIL;
2844 }
2845
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002846 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847 name = vim_strnsave(start, len + 1);
2848 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2849 vim_free(name);
2850 return ret;
2851}
2852
2853/*
2854 * Compile "@r".
2855 */
2856 static int
2857compile_get_register(char_u **arg, cctx_T *cctx)
2858{
2859 int ret;
2860
2861 ++*arg;
2862 if (**arg == NUL)
2863 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002864 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002865 return FAIL;
2866 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002867 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002868 {
2869 emsg_invreg(**arg);
2870 return FAIL;
2871 }
2872 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2873 ++*arg;
2874 return ret;
2875}
2876
2877/*
2878 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002879 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002880 */
2881 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002882apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002884 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885
2886 // this works from end to start
2887 while (p > start)
2888 {
2889 --p;
2890 if (*p == '-' || *p == '+')
2891 {
2892 // only '-' has an effect, for '+' we only check the type
2893#ifdef FEAT_FLOAT
2894 if (rettv->v_type == VAR_FLOAT)
2895 {
2896 if (*p == '-')
2897 rettv->vval.v_float = -rettv->vval.v_float;
2898 }
2899 else
2900#endif
2901 {
2902 varnumber_T val;
2903 int error = FALSE;
2904
2905 // tv_get_number_chk() accepts a string, but we don't want that
2906 // here
2907 if (check_not_string(rettv) == FAIL)
2908 return FAIL;
2909 val = tv_get_number_chk(rettv, &error);
2910 clear_tv(rettv);
2911 if (error)
2912 return FAIL;
2913 if (*p == '-')
2914 val = -val;
2915 rettv->v_type = VAR_NUMBER;
2916 rettv->vval.v_number = val;
2917 }
2918 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002919 else if (numeric_only)
2920 {
2921 ++p;
2922 break;
2923 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 else
2925 {
2926 int v = tv2bool(rettv);
2927
2928 // '!' is permissive in the type.
2929 clear_tv(rettv);
2930 rettv->v_type = VAR_BOOL;
2931 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2932 }
2933 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002934 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 return OK;
2936}
2937
2938/*
2939 * Recognize v: variables that are constants and set "rettv".
2940 */
2941 static void
2942get_vim_constant(char_u **arg, typval_T *rettv)
2943{
2944 if (STRNCMP(*arg, "v:true", 6) == 0)
2945 {
2946 rettv->v_type = VAR_BOOL;
2947 rettv->vval.v_number = VVAL_TRUE;
2948 *arg += 6;
2949 }
2950 else if (STRNCMP(*arg, "v:false", 7) == 0)
2951 {
2952 rettv->v_type = VAR_BOOL;
2953 rettv->vval.v_number = VVAL_FALSE;
2954 *arg += 7;
2955 }
2956 else if (STRNCMP(*arg, "v:null", 6) == 0)
2957 {
2958 rettv->v_type = VAR_SPECIAL;
2959 rettv->vval.v_number = VVAL_NULL;
2960 *arg += 6;
2961 }
2962 else if (STRNCMP(*arg, "v:none", 6) == 0)
2963 {
2964 rettv->v_type = VAR_SPECIAL;
2965 rettv->vval.v_number = VVAL_NONE;
2966 *arg += 6;
2967 }
2968}
2969
Bram Moolenaar696ba232020-07-29 21:20:41 +02002970 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02002971get_compare_type(char_u *p, int *len, int *type_is)
2972{
2973 exptype_T type = EXPR_UNKNOWN;
2974 int i;
2975
2976 switch (p[0])
2977 {
2978 case '=': if (p[1] == '=')
2979 type = EXPR_EQUAL;
2980 else if (p[1] == '~')
2981 type = EXPR_MATCH;
2982 break;
2983 case '!': if (p[1] == '=')
2984 type = EXPR_NEQUAL;
2985 else if (p[1] == '~')
2986 type = EXPR_NOMATCH;
2987 break;
2988 case '>': if (p[1] != '=')
2989 {
2990 type = EXPR_GREATER;
2991 *len = 1;
2992 }
2993 else
2994 type = EXPR_GEQUAL;
2995 break;
2996 case '<': if (p[1] != '=')
2997 {
2998 type = EXPR_SMALLER;
2999 *len = 1;
3000 }
3001 else
3002 type = EXPR_SEQUAL;
3003 break;
3004 case 'i': if (p[1] == 's')
3005 {
3006 // "is" and "isnot"; but not a prefix of a name
3007 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3008 *len = 5;
3009 i = p[*len];
3010 if (!isalnum(i) && i != '_')
3011 {
3012 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3013 *type_is = TRUE;
3014 }
3015 }
3016 break;
3017 }
3018 return type;
3019}
3020
3021/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003023 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 */
3025 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003026compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003028 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029
3030 // this works from end to start
3031 while (p > start)
3032 {
3033 --p;
3034 if (*p == '-' || *p == '+')
3035 {
3036 int negate = *p == '-';
3037 isn_T *isn;
3038
3039 // TODO: check type
3040 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3041 {
3042 --p;
3043 if (*p == '-')
3044 negate = !negate;
3045 }
3046 // only '-' has an effect, for '+' we only check the type
3047 if (negate)
3048 isn = generate_instr(cctx, ISN_NEGATENR);
3049 else
3050 isn = generate_instr(cctx, ISN_CHECKNR);
3051 if (isn == NULL)
3052 return FAIL;
3053 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003054 else if (numeric_only)
3055 {
3056 ++p;
3057 break;
3058 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 else
3060 {
3061 int invert = TRUE;
3062
3063 while (p > start && p[-1] == '!')
3064 {
3065 --p;
3066 invert = !invert;
3067 }
3068 if (generate_2BOOL(cctx, invert) == FAIL)
3069 return FAIL;
3070 }
3071 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003072 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003073 return OK;
3074}
3075
3076/*
3077 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003078 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 */
3080 static int
3081compile_subscript(
3082 char_u **arg,
3083 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003084 char_u *start_leader,
3085 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003086 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003088 char_u *name_start = *end_leader;
3089
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090 for (;;)
3091 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003092 char_u *p = skipwhite(*arg);
3093
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003094 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003095 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003096 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003097
3098 // If a following line starts with "->{" or "->X" advance to that
3099 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003100 // Also if a following line starts with ".x".
3101 if (next != NULL &&
3102 ((next[0] == '-' && next[1] == '>'
3103 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003104 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003105 {
3106 next = next_line_from_context(cctx, TRUE);
3107 if (next == NULL)
3108 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003109 *arg = next;
3110 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003111 }
3112 }
3113
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003114 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3115 // not a function call.
3116 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003118 garray_T *stack = &cctx->ctx_type_stack;
3119 type_T *type;
3120 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003122 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003123 return FAIL;
3124
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003126 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3127
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003128 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3130 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003131 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 return FAIL;
3133 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003134 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003136 char_u *pstart = p;
3137
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003138 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003139 return FAIL;
3140
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 // something->method()
3142 // Apply the '!', '-' and '+' first:
3143 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003144 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003147 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003148 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003149 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 if (**arg == '{')
3151 {
3152 // lambda call: list->{lambda}
3153 if (compile_lambda_call(arg, cctx) == FAIL)
3154 return FAIL;
3155 }
3156 else
3157 {
3158 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003159 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003160 if (!eval_isnamec1(*p))
3161 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003162 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003163 return FAIL;
3164 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003165 if (ASCII_ISALPHA(*p) && p[1] == ':')
3166 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003167 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168 ;
3169 if (*p != '(')
3170 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003171 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 return FAIL;
3173 }
3174 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003175 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 return FAIL;
3177 }
3178 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003179 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003181 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003182 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003183 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003184 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003185 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003186
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003187 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003188 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003189 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003190 // TODO: blob index
3191 // TODO: more arguments
3192 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003193 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003194 return FAIL;
3195
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003196 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003197 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003198 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003199 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003200 if (**arg == ':')
3201 // missing first index is equal to zero
3202 generate_PUSHNR(cctx, 0);
3203 else
3204 {
3205 if (compile_expr0(arg, cctx) == FAIL)
3206 return FAIL;
3207 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3208 return FAIL;
3209 *arg = skipwhite(*arg);
3210 }
3211 if (**arg == ':')
3212 {
3213 *arg = skipwhite(*arg + 1);
3214 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3215 return FAIL;
3216 if (**arg == ']')
3217 // missing second index is equal to end of string
3218 generate_PUSHNR(cctx, -1);
3219 else
3220 {
3221 if (compile_expr0(arg, cctx) == FAIL)
3222 return FAIL;
3223 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3224 return FAIL;
3225 *arg = skipwhite(*arg);
3226 }
3227 is_slice = TRUE;
3228 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229
3230 if (**arg != ']')
3231 {
3232 emsg(_(e_missbrac));
3233 return FAIL;
3234 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003235 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003237 // We can index a list and a dict. If we don't know the type
3238 // we can use the index value type.
3239 // TODO: If we don't know use an instruction to figure it out at
3240 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003241 typep = ((type_T **)stack->ga_data) + stack->ga_len
3242 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003243 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003244 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3245 // If the index is a string, the variable must be a Dict.
3246 if (*typep == &t_any && valtype == &t_string)
3247 vtype = VAR_DICT;
3248 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003249 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003250 if (need_type(valtype, &t_number, -1, cctx, FALSE) == FAIL)
3251 return FAIL;
3252 if (is_slice)
3253 {
3254 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
3255 if (need_type(valtype, &t_number, -2, cctx, FALSE) == FAIL)
3256 return FAIL;
3257 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003258 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003259
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003260 if (vtype == VAR_DICT)
3261 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003262 if (is_slice)
3263 {
3264 emsg(_(e_cannot_slice_dictionary));
3265 return FAIL;
3266 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003267 if ((*typep)->tt_type == VAR_DICT)
3268 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003269 else
3270 {
3271 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3272 return FAIL;
3273 *typep = &t_any;
3274 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003275 if (may_generate_2STRING(-1, cctx) == FAIL)
3276 return FAIL;
3277 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3278 return FAIL;
3279 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003280 else if (vtype == VAR_STRING)
3281 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003282 *typep = &t_string;
3283 if ((is_slice
3284 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3285 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003286 return FAIL;
3287 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003288 else if (vtype == VAR_BLOB)
3289 {
3290 emsg("Sorry, blob index and slice not implemented yet");
3291 return FAIL;
3292 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003293 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003294 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003295 if (is_slice)
3296 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003297 if (generate_instr_drop(cctx,
3298 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3299 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003300 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003301 }
Bram Moolenaared591872020-08-15 22:14:53 +02003302 else
3303 {
3304 if ((*typep)->tt_type == VAR_LIST)
3305 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003306 if (generate_instr_drop(cctx,
3307 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3308 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003309 return FAIL;
3310 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003311 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003312 else
3313 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003314 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003315 return FAIL;
3316 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003318 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003320 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003321 return FAIL;
3322
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003323 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003324 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3325 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003326 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003327 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003328 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329 while (eval_isnamec(*p))
3330 MB_PTR_ADV(p);
3331 if (p == *arg)
3332 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003333 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003334 return FAIL;
3335 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003336 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003337 return FAIL;
3338 *arg = p;
3339 }
3340 else
3341 break;
3342 }
3343
3344 // TODO - see handle_subscript():
3345 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3346 // Don't do this when "Func" is already a partial that was bound
3347 // explicitly (pt_auto is FALSE).
3348
3349 return OK;
3350}
3351
3352/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003353 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3354 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003356 * If the value is a constant "ppconst->pp_ret" will be set.
3357 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003358 *
3359 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 */
3361
3362/*
3363 * number number constant
3364 * 0zFFFFFFFF Blob constant
3365 * "string" string constant
3366 * 'string' literal string constant
3367 * &option-name option value
3368 * @r register contents
3369 * identifier variable value
3370 * function() function call
3371 * $VAR environment variable
3372 * (expression) nested expression
3373 * [expr, expr] List
3374 * {key: val, key: val} Dictionary
3375 * #{key: val, key: val} Dictionary with literal keys
3376 *
3377 * Also handle:
3378 * ! in front logical NOT
3379 * - in front unary minus
3380 * + in front unary plus (ignored)
3381 * trailing (arg) funcref/partial call
3382 * trailing [] subscript in String or List
3383 * trailing .name entry in Dictionary
3384 * trailing ->name() method call
3385 */
3386 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003387compile_expr7(
3388 char_u **arg,
3389 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003390 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 char_u *start_leader, *end_leader;
3393 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003394 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003395 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396
3397 /*
3398 * Skip '!', '-' and '+' characters. They are handled later.
3399 */
3400 start_leader = *arg;
3401 while (**arg == '!' || **arg == '-' || **arg == '+')
3402 *arg = skipwhite(*arg + 1);
3403 end_leader = *arg;
3404
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003405 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406 switch (**arg)
3407 {
3408 /*
3409 * Number constant.
3410 */
3411 case '0': // also for blob starting with 0z
3412 case '1':
3413 case '2':
3414 case '3':
3415 case '4':
3416 case '5':
3417 case '6':
3418 case '7':
3419 case '8':
3420 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003421 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003423 // Apply "-" and "+" just before the number now, right to
3424 // left. Matters especially when "->" follows. Stops at
3425 // '!'.
3426 if (apply_leader(rettv, TRUE,
3427 start_leader, &end_leader) == FAIL)
3428 {
3429 clear_tv(rettv);
3430 return FAIL;
3431 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432 break;
3433
3434 /*
3435 * String constant: "string".
3436 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003437 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438 return FAIL;
3439 break;
3440
3441 /*
3442 * Literal string constant: 'str''ing'.
3443 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003444 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 return FAIL;
3446 break;
3447
3448 /*
3449 * Constant Vim variable.
3450 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003451 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452 ret = NOTDONE;
3453 break;
3454
3455 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003456 * "true" constant
3457 */
3458 case 't': if (STRNCMP(*arg, "true", 4) == 0
3459 && !eval_isnamec((*arg)[4]))
3460 {
3461 *arg += 4;
3462 rettv->v_type = VAR_BOOL;
3463 rettv->vval.v_number = VVAL_TRUE;
3464 }
3465 else
3466 ret = NOTDONE;
3467 break;
3468
3469 /*
3470 * "false" constant
3471 */
3472 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3473 && !eval_isnamec((*arg)[5]))
3474 {
3475 *arg += 5;
3476 rettv->v_type = VAR_BOOL;
3477 rettv->vval.v_number = VVAL_FALSE;
3478 }
3479 else
3480 ret = NOTDONE;
3481 break;
3482
3483 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484 * List: [expr, expr]
3485 */
3486 case '[': ret = compile_list(arg, cctx);
3487 break;
3488
3489 /*
3490 * Dictionary: #{key: val, key: val}
3491 */
3492 case '#': if ((*arg)[1] == '{')
3493 {
3494 ++*arg;
3495 ret = compile_dict(arg, cctx, TRUE);
3496 }
3497 else
3498 ret = NOTDONE;
3499 break;
3500
3501 /*
3502 * Lambda: {arg, arg -> expr}
3503 * Dictionary: {'key': val, 'key': val}
3504 */
3505 case '{': {
3506 char_u *start = skipwhite(*arg + 1);
3507
3508 // Find out what comes after the arguments.
3509 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003510 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511 if (ret != FAIL && *start == '>')
3512 ret = compile_lambda(arg, cctx);
3513 else
3514 ret = compile_dict(arg, cctx, FALSE);
3515 }
3516 break;
3517
3518 /*
3519 * Option value: &name
3520 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003521 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3522 return FAIL;
3523 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 break;
3525
3526 /*
3527 * Environment variable: $VAR.
3528 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003529 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3530 return FAIL;
3531 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532 break;
3533
3534 /*
3535 * Register contents: @r.
3536 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003537 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3538 return FAIL;
3539 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540 break;
3541 /*
3542 * nested expression: (expression).
3543 */
3544 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003545
3546 // recursive!
3547 if (ppconst->pp_used <= PPSIZE - 10)
3548 {
3549 ret = compile_expr1(arg, cctx, ppconst);
3550 }
3551 else
3552 {
3553 // Not enough space in ppconst, flush constants.
3554 if (generate_ppconst(cctx, ppconst) == FAIL)
3555 return FAIL;
3556 ret = compile_expr0(arg, cctx);
3557 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 *arg = skipwhite(*arg);
3559 if (**arg == ')')
3560 ++*arg;
3561 else if (ret == OK)
3562 {
3563 emsg(_(e_missing_close));
3564 ret = FAIL;
3565 }
3566 break;
3567
3568 default: ret = NOTDONE;
3569 break;
3570 }
3571 if (ret == FAIL)
3572 return FAIL;
3573
Bram Moolenaar1c747212020-05-09 18:28:34 +02003574 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003576 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003577 clear_tv(rettv);
3578 else
3579 // A constant expression can possibly be handled compile time,
3580 // return the value instead of generating code.
3581 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003582 }
3583 else if (ret == NOTDONE)
3584 {
3585 char_u *p;
3586 int r;
3587
3588 if (!eval_isnamec1(**arg))
3589 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003590 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591 return FAIL;
3592 }
3593
3594 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003595 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003597 {
3598 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3599 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003601 {
3602 if (generate_ppconst(cctx, ppconst) == FAIL)
3603 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003604 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003605 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 if (r == FAIL)
3607 return FAIL;
3608 }
3609
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003610 // Handle following "[]", ".member", etc.
3611 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003612 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003613 ppconst) == FAIL)
3614 return FAIL;
3615 if (ppconst->pp_used > 0)
3616 {
3617 // apply the '!', '-' and '+' before the constant
3618 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003619 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003620 return FAIL;
3621 return OK;
3622 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003623 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003625 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626}
3627
3628/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003629 * Give the "white on both sides" error, taking the operator from "p[len]".
3630 */
3631 void
3632error_white_both(char_u *op, int len)
3633{
3634 char_u buf[10];
3635
3636 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003637 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003638}
3639
3640/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003641 * <type>expr7: runtime type check / conversion
3642 */
3643 static int
3644compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3645{
3646 type_T *want_type = NULL;
3647
3648 // Recognize <type>
3649 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3650 {
3651 int called_emsg_before = called_emsg;
3652
3653 ++*arg;
3654 want_type = parse_type(arg, cctx->ctx_type_list);
3655 if (called_emsg != called_emsg_before)
3656 return FAIL;
3657
3658 if (**arg != '>')
3659 {
3660 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003661 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003662 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003663 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003664 return FAIL;
3665 }
3666 ++*arg;
3667 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3668 return FAIL;
3669 }
3670
3671 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3672 return FAIL;
3673
3674 if (want_type != NULL)
3675 {
3676 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003677 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003678
Bram Moolenaard1103582020-08-14 22:44:25 +02003679 generate_ppconst(cctx, ppconst);
3680 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003681 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003682 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003683 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3684 return FAIL;
3685 }
3686 }
3687
3688 return OK;
3689}
3690
3691/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692 * * number multiplication
3693 * / number division
3694 * % number modulo
3695 */
3696 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003697compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698{
3699 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003700 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003701 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003703 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003704 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 return FAIL;
3706
3707 /*
3708 * Repeat computing, until no "*", "/" or "%" is following.
3709 */
3710 for (;;)
3711 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003712 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713 if (*op != '*' && *op != '/' && *op != '%')
3714 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003715 if (next != NULL)
3716 {
3717 *arg = next_line_from_context(cctx, TRUE);
3718 op = skipwhite(*arg);
3719 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003720
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003721 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003722 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003723 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003724 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 }
3726 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003727 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003728 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003730 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003731 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003733
3734 if (ppconst->pp_used == ppconst_used + 2
3735 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3736 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003737 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003738 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3739 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003740 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003742 // both are numbers: compute the result
3743 switch (*op)
3744 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003745 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003746 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003747 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003748 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003749 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003750 break;
3751 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003752 tv1->vval.v_number = res;
3753 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003754 }
3755 else
3756 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003757 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003758 generate_two_op(cctx, op);
3759 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 }
3761
3762 return OK;
3763}
3764
3765/*
3766 * + number addition
3767 * - number subtraction
3768 * .. string concatenation
3769 */
3770 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003771compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772{
3773 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003774 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003775 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003776 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777
3778 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003779 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780 return FAIL;
3781
3782 /*
3783 * Repeat computing, until no "+", "-" or ".." is following.
3784 */
3785 for (;;)
3786 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003787 op = may_peek_next_line(cctx, *arg, &next);
3788 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789 break;
3790 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003791 if (next != NULL)
3792 {
3793 *arg = next_line_from_context(cctx, TRUE);
3794 op = skipwhite(*arg);
3795 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003796
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003797 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003799 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003800 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 }
3802
3803 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003804 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003805 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003807 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003808 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 return FAIL;
3810
Bram Moolenaara5565e42020-05-09 15:44:01 +02003811 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003812 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003813 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3814 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3815 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3816 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003818 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3819 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003820
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003821 // concat/subtract/add constant numbers
3822 if (*op == '+')
3823 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3824 else if (*op == '-')
3825 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3826 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003827 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003828 // concatenate constant strings
3829 char_u *s1 = tv1->vval.v_string;
3830 char_u *s2 = tv2->vval.v_string;
3831 size_t len1 = STRLEN(s1);
3832
3833 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3834 if (tv1->vval.v_string == NULL)
3835 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003836 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003837 return FAIL;
3838 }
3839 mch_memmove(tv1->vval.v_string, s1, len1);
3840 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003841 vim_free(s1);
3842 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003843 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003844 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 }
3846 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003847 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003848 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003849 if (*op == '.')
3850 {
3851 if (may_generate_2STRING(-2, cctx) == FAIL
3852 || may_generate_2STRING(-1, cctx) == FAIL)
3853 return FAIL;
3854 generate_instr_drop(cctx, ISN_CONCAT, 1);
3855 }
3856 else
3857 generate_two_op(cctx, op);
3858 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859 }
3860
3861 return OK;
3862}
3863
3864/*
3865 * expr5a == expr5b
3866 * expr5a =~ expr5b
3867 * expr5a != expr5b
3868 * expr5a !~ expr5b
3869 * expr5a > expr5b
3870 * expr5a >= expr5b
3871 * expr5a < expr5b
3872 * expr5a <= expr5b
3873 * expr5a is expr5b
3874 * expr5a isnot expr5b
3875 *
3876 * Produces instructions:
3877 * EVAL expr5a Push result of "expr5a"
3878 * EVAL expr5b Push result of "expr5b"
3879 * COMPARE one of the compare instructions
3880 */
3881 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003882compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003883{
3884 exptype_T type = EXPR_UNKNOWN;
3885 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003886 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003887 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003889 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003890
3891 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003892 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893 return FAIL;
3894
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003895 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003896 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897
3898 /*
3899 * If there is a comparative operator, use it.
3900 */
3901 if (type != EXPR_UNKNOWN)
3902 {
3903 int ic = FALSE; // Default: do not ignore case
3904
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003905 if (next != NULL)
3906 {
3907 *arg = next_line_from_context(cctx, TRUE);
3908 p = skipwhite(*arg);
3909 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003910 if (type_is && (p[len] == '?' || p[len] == '#'))
3911 {
3912 semsg(_(e_invexpr2), *arg);
3913 return FAIL;
3914 }
3915 // extra question mark appended: ignore case
3916 if (p[len] == '?')
3917 {
3918 ic = TRUE;
3919 ++len;
3920 }
3921 // extra '#' appended: match case (ignored)
3922 else if (p[len] == '#')
3923 ++len;
3924 // nothing appended: match case
3925
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003926 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003928 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003929 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003930 }
3931
3932 // get the second variable
3933 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003934 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003935 return FAIL;
3936
Bram Moolenaara5565e42020-05-09 15:44:01 +02003937 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 return FAIL;
3939
Bram Moolenaara5565e42020-05-09 15:44:01 +02003940 if (ppconst->pp_used == ppconst_used + 2)
3941 {
3942 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3943 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3944 int ret;
3945
3946 // Both sides are a constant, compute the result now.
3947 // First check for a valid combination of types, this is more
3948 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003949 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003950 ret = FAIL;
3951 else
3952 {
3953 ret = typval_compare(tv1, tv2, type, ic);
3954 tv1->v_type = VAR_BOOL;
3955 tv1->vval.v_number = tv1->vval.v_number
3956 ? VVAL_TRUE : VVAL_FALSE;
3957 clear_tv(tv2);
3958 --ppconst->pp_used;
3959 }
3960 return ret;
3961 }
3962
3963 generate_ppconst(cctx, ppconst);
3964 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965 }
3966
3967 return OK;
3968}
3969
Bram Moolenaar7f141552020-05-09 17:35:53 +02003970static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3971
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972/*
3973 * Compile || or &&.
3974 */
3975 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003976compile_and_or(
3977 char_u **arg,
3978 cctx_T *cctx,
3979 char *op,
3980 ppconst_T *ppconst,
3981 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003983 char_u *next;
3984 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003985 int opchar = *op;
3986
3987 if (p[0] == opchar && p[1] == opchar)
3988 {
3989 garray_T *instr = &cctx->ctx_instr;
3990 garray_T end_ga;
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02003991 garray_T *stack = &cctx->ctx_type_stack;
3992 type_T **typep;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003993
3994 /*
3995 * Repeat until there is no following "||" or "&&"
3996 */
3997 ga_init2(&end_ga, sizeof(int), 10);
3998 while (p[0] == opchar && p[1] == opchar)
3999 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004000 if (next != NULL)
4001 {
4002 *arg = next_line_from_context(cctx, TRUE);
4003 p = skipwhite(*arg);
4004 }
4005
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004006 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4007 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004008 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004009 return FAIL;
4010 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011
Bram Moolenaara5565e42020-05-09 15:44:01 +02004012 // TODO: use ppconst if the value is a constant
4013 generate_ppconst(cctx, ppconst);
4014
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004015 if (ga_grow(&end_ga, 1) == FAIL)
4016 {
4017 ga_clear(&end_ga);
4018 return FAIL;
4019 }
4020 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4021 ++end_ga.ga_len;
4022 generate_JUMP(cctx, opchar == '|'
4023 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4024
4025 // eval the next expression
4026 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004027 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004028 return FAIL;
4029
Bram Moolenaara5565e42020-05-09 15:44:01 +02004030 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4031 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032 {
4033 ga_clear(&end_ga);
4034 return FAIL;
4035 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004036
4037 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004038 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004039 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040
4041 // Fill in the end label in all jumps.
4042 while (end_ga.ga_len > 0)
4043 {
4044 isn_T *isn;
4045
4046 --end_ga.ga_len;
4047 isn = ((isn_T *)instr->ga_data)
4048 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4049 isn->isn_arg.jump.jump_where = instr->ga_len;
4050 }
4051 ga_clear(&end_ga);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004052
4053 // The resulting type can be used as a bool.
4054 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4055 if (*typep != &t_bool)
4056 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004057 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004058
4059 if (type != NULL)
4060 {
4061 *type = **typep;
4062 type->tt_flags |= TTFLAG_BOOL_OK;
4063 *typep = type;
4064 }
4065 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004066 }
4067
4068 return OK;
4069}
4070
4071/*
4072 * expr4a && expr4a && expr4a logical AND
4073 *
4074 * Produces instructions:
4075 * EVAL expr4a Push result of "expr4a"
4076 * JUMP_AND_KEEP_IF_FALSE end
4077 * EVAL expr4b Push result of "expr4b"
4078 * JUMP_AND_KEEP_IF_FALSE end
4079 * EVAL expr4c Push result of "expr4c"
4080 * end:
4081 */
4082 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004083compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004085 int ppconst_used = ppconst->pp_used;
4086
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004088 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089 return FAIL;
4090
4091 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004092 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093}
4094
4095/*
4096 * expr3a || expr3b || expr3c logical OR
4097 *
4098 * Produces instructions:
4099 * EVAL expr3a Push result of "expr3a"
4100 * JUMP_AND_KEEP_IF_TRUE end
4101 * EVAL expr3b Push result of "expr3b"
4102 * JUMP_AND_KEEP_IF_TRUE end
4103 * EVAL expr3c Push result of "expr3c"
4104 * end:
4105 */
4106 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004107compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004109 int ppconst_used = ppconst->pp_used;
4110
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004111 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004112 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113 return FAIL;
4114
4115 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004116 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004117}
4118
4119/*
4120 * Toplevel expression: expr2 ? expr1a : expr1b
4121 *
4122 * Produces instructions:
4123 * EVAL expr2 Push result of "expr"
4124 * JUMP_IF_FALSE alt jump if false
4125 * EVAL expr1a
4126 * JUMP_ALWAYS end
4127 * alt: EVAL expr1b
4128 * end:
4129 */
4130 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004131compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004132{
4133 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004134 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004135 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136
Bram Moolenaar3988f642020-08-27 22:43:03 +02004137 // Ignore all kinds of errors when not producing code.
4138 if (cctx->ctx_skip == SKIP_YES)
4139 {
4140 skip_expr(arg);
4141 return OK;
4142 }
4143
Bram Moolenaar61a89812020-05-07 16:58:17 +02004144 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004145 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146 return FAIL;
4147
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004148 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004149 if (*p == '?')
4150 {
4151 garray_T *instr = &cctx->ctx_instr;
4152 garray_T *stack = &cctx->ctx_type_stack;
4153 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004154 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004156 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004158 int has_const_expr = FALSE;
4159 int const_value = FALSE;
4160 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004162 if (next != NULL)
4163 {
4164 *arg = next_line_from_context(cctx, TRUE);
4165 p = skipwhite(*arg);
4166 }
4167
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004168 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4169 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004170 semsg(_(e_white_space_required_before_and_after_str), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004171 return FAIL;
4172 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004173
Bram Moolenaara5565e42020-05-09 15:44:01 +02004174 if (ppconst->pp_used == ppconst_used + 1)
4175 {
4176 // the condition is a constant, we know whether the ? or the :
4177 // expression is to be evaluated.
4178 has_const_expr = TRUE;
4179 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4180 clear_tv(&ppconst->pp_tv[ppconst_used]);
4181 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004182 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4183 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004184 }
4185 else
4186 {
4187 generate_ppconst(cctx, ppconst);
4188 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4189 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004190
4191 // evaluate the second expression; any type is accepted
4192 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004193 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004194 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004195 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004196 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004197
Bram Moolenaara5565e42020-05-09 15:44:01 +02004198 if (!has_const_expr)
4199 {
4200 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201
Bram Moolenaara5565e42020-05-09 15:44:01 +02004202 // remember the type and drop it
4203 --stack->ga_len;
4204 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205
Bram Moolenaara5565e42020-05-09 15:44:01 +02004206 end_idx = instr->ga_len;
4207 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4208
4209 // jump here from JUMP_IF_FALSE
4210 isn = ((isn_T *)instr->ga_data) + alt_idx;
4211 isn->isn_arg.jump.jump_where = instr->ga_len;
4212 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213
4214 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004215 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216 if (*p != ':')
4217 {
4218 emsg(_(e_missing_colon));
4219 return FAIL;
4220 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004221 if (next != NULL)
4222 {
4223 *arg = next_line_from_context(cctx, TRUE);
4224 p = skipwhite(*arg);
4225 }
4226
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004227 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4228 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004229 semsg(_(e_white_space_required_before_and_after_str), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004230 return FAIL;
4231 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232
4233 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004234 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004235 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4236 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004237 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004238 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004239 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004240 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004241 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004242
Bram Moolenaara5565e42020-05-09 15:44:01 +02004243 if (!has_const_expr)
4244 {
4245 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004246
Bram Moolenaara5565e42020-05-09 15:44:01 +02004247 // If the types differ, the result has a more generic type.
4248 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4249 common_type(type1, type2, &type2, cctx->ctx_type_list);
4250
4251 // jump here from JUMP_ALWAYS
4252 isn = ((isn_T *)instr->ga_data) + end_idx;
4253 isn->isn_arg.jump.jump_where = instr->ga_len;
4254 }
4255
4256 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004257 }
4258 return OK;
4259}
4260
4261/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004262 * Toplevel expression.
4263 */
4264 static int
4265compile_expr0(char_u **arg, cctx_T *cctx)
4266{
4267 ppconst_T ppconst;
4268
4269 CLEAR_FIELD(ppconst);
4270 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4271 {
4272 clear_ppconst(&ppconst);
4273 return FAIL;
4274 }
4275 if (generate_ppconst(cctx, &ppconst) == FAIL)
4276 return FAIL;
4277 return OK;
4278}
4279
4280/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004281 * compile "return [expr]"
4282 */
4283 static char_u *
4284compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4285{
4286 char_u *p = arg;
4287 garray_T *stack = &cctx->ctx_type_stack;
4288 type_T *stack_type;
4289
4290 if (*p != NUL && *p != '|' && *p != '\n')
4291 {
4292 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004293 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004294 return NULL;
4295
4296 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4297 if (set_return_type)
4298 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004299 else
4300 {
4301 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4302 && stack_type->tt_type != VAR_VOID
4303 && stack_type->tt_type != VAR_UNKNOWN)
4304 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004305 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004306 return NULL;
4307 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004308 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4309 cctx, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004310 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004311 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312 }
4313 else
4314 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004315 // "set_return_type" cannot be TRUE, only used for a lambda which
4316 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004317 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4318 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004320 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321 return NULL;
4322 }
4323
4324 // No argument, return zero.
4325 generate_PUSHNR(cctx, 0);
4326 }
4327
4328 if (generate_instr(cctx, ISN_RETURN) == NULL)
4329 return NULL;
4330
4331 // "return val | endif" is possible
4332 return skipwhite(p);
4333}
4334
4335/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004336 * Get a line from the compilation context, compatible with exarg_T getline().
4337 * Return a pointer to the line in allocated memory.
4338 * Return NULL for end-of-file or some error.
4339 */
4340 static char_u *
4341exarg_getline(
4342 int c UNUSED,
4343 void *cookie,
4344 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004345 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004346{
4347 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004348 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004349
Bram Moolenaar66250c92020-08-20 15:02:42 +02004350 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004351 {
Bram Moolenaar66250c92020-08-20 15:02:42 +02004352 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4353 return NULL;
4354 ++cctx->ctx_lnum;
4355 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4356 // Comment lines result in NULL pointers, skip them.
4357 if (p != NULL)
4358 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004359 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004360}
4361
4362/*
4363 * Compile a nested :def command.
4364 */
4365 static char_u *
4366compile_nested_function(exarg_T *eap, cctx_T *cctx)
4367{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004368 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004369 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004370 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004371 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004372 lvar_T *lvar;
4373 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004374 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004375
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004376 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004377 {
4378 emsg(_(e_cannot_use_bang_with_nested_def));
4379 return NULL;
4380 }
4381
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004382 // Only g:Func() can use a namespace.
4383 if (name_start[1] == ':' && !is_global)
4384 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004385 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004386 return NULL;
4387 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004388 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4389 return NULL;
4390
Bram Moolenaar04b12692020-05-04 23:24:44 +02004391 eap->arg = name_end;
4392 eap->getline = exarg_getline;
4393 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004394 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004395 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004396 lambda_name = get_lambda_name();
4397 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004398
Bram Moolenaar822ba242020-05-24 23:00:18 +02004399 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004400 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004401 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004402 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004403 return NULL;
4404
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004405 if (is_global)
4406 {
4407 char_u *func_name = vim_strnsave(name_start + 2,
4408 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004409
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004410 if (func_name == NULL)
4411 r = FAIL;
4412 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004413 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004414 }
4415 else
4416 {
4417 // Define a local variable for the function reference.
4418 lvar = reserve_local(cctx, name_start, name_end - name_start,
4419 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004420 if (lvar == NULL)
4421 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004422 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004423 return NULL;
4424 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4425 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004426
Bram Moolenaar61a89812020-05-07 16:58:17 +02004427 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004428 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004429}
4430
4431/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432 * Return the length of an assignment operator, or zero if there isn't one.
4433 */
4434 int
4435assignment_len(char_u *p, int *heredoc)
4436{
4437 if (*p == '=')
4438 {
4439 if (p[1] == '<' && p[2] == '<')
4440 {
4441 *heredoc = TRUE;
4442 return 3;
4443 }
4444 return 1;
4445 }
4446 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4447 return 2;
4448 if (STRNCMP(p, "..=", 3) == 0)
4449 return 3;
4450 return 0;
4451}
4452
4453// words that cannot be used as a variable
4454static char *reserved[] = {
4455 "true",
4456 "false",
4457 NULL
4458};
4459
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004460typedef enum {
4461 dest_local,
4462 dest_option,
4463 dest_env,
4464 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004465 dest_buffer,
4466 dest_window,
4467 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004468 dest_vimvar,
4469 dest_script,
4470 dest_reg,
4471} assign_dest_T;
4472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004473/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004474 * Generate the load instruction for "name".
4475 */
4476 static void
4477generate_loadvar(
4478 cctx_T *cctx,
4479 assign_dest_T dest,
4480 char_u *name,
4481 lvar_T *lvar,
4482 type_T *type)
4483{
4484 switch (dest)
4485 {
4486 case dest_option:
4487 // TODO: check the option exists
4488 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4489 break;
4490 case dest_global:
4491 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4492 break;
4493 case dest_buffer:
4494 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4495 break;
4496 case dest_window:
4497 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4498 break;
4499 case dest_tab:
4500 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4501 break;
4502 case dest_script:
4503 compile_load_scriptvar(cctx,
4504 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4505 break;
4506 case dest_env:
4507 // Include $ in the name here
4508 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4509 break;
4510 case dest_reg:
4511 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4512 break;
4513 case dest_vimvar:
4514 generate_LOADV(cctx, name + 2, TRUE);
4515 break;
4516 case dest_local:
4517 if (lvar->lv_from_outer)
4518 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4519 NULL, type);
4520 else
4521 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4522 break;
4523 }
4524}
4525
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004526 void
4527vim9_declare_error(char_u *name)
4528{
4529 char *scope = "";
4530
4531 switch (*name)
4532 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004533 case 'g': scope = _("global"); break;
4534 case 'b': scope = _("buffer"); break;
4535 case 'w': scope = _("window"); break;
4536 case 't': scope = _("tab"); break;
4537 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004538 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004539 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004540 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004541 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004542 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004543 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004544 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004545 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004546 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004547}
4548
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004549/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004550 * Compile declaration and assignment:
4551 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004553 * Return NULL for an error.
4554 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555 */
4556 static char_u *
4557compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4558{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004559 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004560 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004561 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004562 char_u *ret = NULL;
4563 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004564 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004565 int scriptvar_sid = 0;
4566 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004567 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004568 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004569 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571 int oplen = 0;
4572 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004573 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004574 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004575 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004577 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004578
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004579 // Skip over the "var" or "[var, var]" to get to any "=".
4580 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4581 if (p == NULL)
4582 return *arg == '[' ? arg : NULL;
4583
4584 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004586 // TODO: should we allow this, and figure out type inference from list
4587 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004588 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589 return NULL;
4590 }
4591
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004592 sp = p;
4593 p = skipwhite(p);
4594 op = p;
4595 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004596
4597 if (var_count > 0 && oplen == 0)
4598 // can be something like "[1, 2]->func()"
4599 return arg;
4600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4602 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004603 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004604 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004605 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004606
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607 if (heredoc)
4608 {
4609 list_T *l;
4610 listitem_T *li;
4611
4612 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004613 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004615 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616
4617 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004618 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619 {
4620 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4621 li->li_tv.vval.v_string = NULL;
4622 }
4623 generate_NEWLIST(cctx, l->lv_len);
4624 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004625 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004626 list_free(l);
4627 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004628 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004630 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004631 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004632 // for "[var, var] = expr" evaluate the expression here, loop over the
4633 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004634
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004635 p = skipwhite(op + oplen);
4636 if (compile_expr0(&p, cctx) == FAIL)
4637 return NULL;
4638 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004640 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004641 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004642 type_T *stacktype;
4643
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004644 stacktype = stack->ga_len == 0 ? &t_void
4645 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004646 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004647 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004648 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004649 goto theend;
4650 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004651 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004652 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004653 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004654 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4655 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004656 }
4657 }
4658
4659 /*
4660 * Loop over variables in "[var, var] = expr".
4661 * For "var = expr" and "let var: type" this is done only once.
4662 */
4663 if (var_count > 0)
4664 var_start = skipwhite(arg + 1); // skip over the "["
4665 else
4666 var_start = arg;
4667 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4668 {
4669 char_u *var_end = skip_var_one(var_start, FALSE);
4670 size_t varlen;
4671 int new_local = FALSE;
4672 int opt_type;
4673 int opt_flags = 0;
4674 assign_dest_T dest = dest_local;
4675 int vimvaridx = -1;
4676 lvar_T *lvar = NULL;
4677 lvar_T arg_lvar;
4678 int has_type = FALSE;
4679 int has_index = FALSE;
4680 int instr_count = -1;
4681
Bram Moolenaar65821722020-08-02 18:58:54 +02004682 if (*var_start == '@')
4683 p = var_start + 2;
4684 else
4685 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02004686 // skip over the leading "&", "&l:", "&g:" and "$"
4687 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02004688 p = to_name_end(p, TRUE);
4689 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004690
4691 // "a: type" is declaring variable "a" with a type, not "a:".
4692 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4693 --var_end;
4694 if (is_decl && p == var_start + 2 && p[-1] == ':')
4695 --p;
4696
4697 varlen = p - var_start;
4698 vim_free(name);
4699 name = vim_strnsave(var_start, varlen);
4700 if (name == NULL)
4701 return NULL;
4702 if (!heredoc)
4703 type = &t_any;
4704
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004705 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004706 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004707 int declare_error = FALSE;
4708
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004709 if (*var_start == '&')
4710 {
4711 int cc;
4712 long numval;
4713
4714 dest = dest_option;
4715 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004716 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004717 emsg(_(e_const_option));
4718 goto theend;
4719 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004720 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004721 p = var_start;
4722 p = find_option_end(&p, &opt_flags);
4723 if (p == NULL)
4724 {
4725 // cannot happen?
4726 emsg(_(e_letunexp));
4727 goto theend;
4728 }
4729 cc = *p;
4730 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02004731 opt_type = get_option_value(skip_option_env_lead(var_start),
4732 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004733 *p = cc;
4734 if (opt_type == -3)
4735 {
4736 semsg(_(e_unknown_option), var_start);
4737 goto theend;
4738 }
4739 if (opt_type == -2 || opt_type == 0)
4740 type = &t_string;
4741 else
4742 type = &t_number; // both number and boolean option
4743 }
4744 else if (*var_start == '$')
4745 {
4746 dest = dest_env;
4747 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004748 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004749 }
4750 else if (*var_start == '@')
4751 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004752 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004753 {
4754 emsg_invreg(var_start[1]);
4755 goto theend;
4756 }
4757 dest = dest_reg;
4758 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004759 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004760 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004761 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004762 {
4763 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004764 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004765 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004766 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004767 {
4768 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004769 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004770 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004771 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004772 {
4773 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004774 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004775 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004776 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004777 {
4778 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004779 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004780 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004781 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004782 {
4783 typval_T *vtv;
4784 int di_flags;
4785
4786 vimvaridx = find_vim_var(name + 2, &di_flags);
4787 if (vimvaridx < 0)
4788 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004789 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004790 goto theend;
4791 }
4792 // We use the current value of "sandbox" here, is that OK?
4793 if (var_check_ro(di_flags, name, FALSE))
4794 goto theend;
4795 dest = dest_vimvar;
4796 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004797 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004798 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004799 }
4800 else
4801 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004802 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004803
4804 for (idx = 0; reserved[idx] != NULL; ++idx)
4805 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004806 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004807 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004808 goto theend;
4809 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004810
4811 lvar = lookup_local(var_start, varlen, cctx);
4812 if (lvar == NULL)
4813 {
4814 CLEAR_FIELD(arg_lvar);
4815 if (lookup_arg(var_start, varlen,
4816 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4817 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004818 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004819 if (is_decl)
4820 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004821 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004822 goto theend;
4823 }
4824 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004825 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004826 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004827 if (lvar != NULL)
4828 {
4829 if (is_decl)
4830 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004831 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004832 goto theend;
4833 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004834 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004835 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004836 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004837 int script_namespace = varlen > 1
4838 && STRNCMP(var_start, "s:", 2) == 0;
4839 int script_var = (script_namespace
Bram Moolenaar53900992020-08-22 19:02:02 +02004840 ? lookup_script(var_start + 2, varlen - 2, FALSE)
4841 : lookup_script(var_start, varlen, TRUE)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004842 imported_T *import =
4843 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004844
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004845 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004846 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004847 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4848
4849 if (is_decl)
4850 {
4851 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004852 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004853 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004854 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004855 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004856 name);
4857 goto theend;
4858 }
4859 else if (cctx->ctx_ufunc->uf_script_ctx_version
4860 == SCRIPT_VERSION_VIM9
4861 && script_namespace
4862 && !script_var && import == NULL)
4863 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004864 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004865 goto theend;
4866 }
4867
4868 dest = dest_script;
4869
4870 // existing script-local variables should have a type
4871 scriptvar_sid = current_sctx.sc_sid;
4872 if (import != NULL)
4873 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004874 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004875 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02004876 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4877 rawname, TRUE);
4878 if (scriptvar_idx > 0)
4879 {
4880 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4881 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004882 ((svar_T *)si->sn_var_vals.ga_data)
4883 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004884 type = sv->sv_type;
4885 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004886 }
4887 }
4888 else if (name[1] == ':' && name[2] != NUL)
4889 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004890 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004891 goto theend;
4892 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004893 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004894 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004895 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004896 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004897 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004898 else if (check_defined(var_start, varlen, cctx) == FAIL)
4899 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004900 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004901 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004902
4903 if (declare_error)
4904 {
4905 vim9_declare_error(name);
4906 goto theend;
4907 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004908 }
4909
4910 // handle "a:name" as a name, not index "name" on "a"
4911 if (varlen > 1 || var_start[varlen] != ':')
4912 p = var_end;
4913
4914 if (dest != dest_option)
4915 {
4916 if (is_decl && *p == ':')
4917 {
4918 // parse optional type: "let var: type = expr"
4919 if (!VIM_ISWHITE(p[1]))
4920 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004921 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004922 goto theend;
4923 }
4924 p = skipwhite(p + 1);
4925 type = parse_type(&p, cctx->ctx_type_list);
4926 has_type = TRUE;
4927 }
4928 else if (lvar != NULL)
4929 type = lvar->lv_type;
4930 }
4931
4932 if (oplen == 3 && !heredoc && dest != dest_global
4933 && type->tt_type != VAR_STRING
4934 && type->tt_type != VAR_ANY)
4935 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004936 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004937 goto theend;
4938 }
4939
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004940 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004941 {
4942 if (oplen > 1 && !heredoc)
4943 {
4944 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004945 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004946 goto theend;
4947 }
4948
4949 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004950 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
4951 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004952 goto theend;
4953 lvar = reserve_local(cctx, var_start, varlen,
4954 cmdidx == CMD_const, type);
4955 if (lvar == NULL)
4956 goto theend;
4957 new_local = TRUE;
4958 }
4959
4960 member_type = type;
4961 if (var_end > var_start + varlen)
4962 {
4963 // Something follows after the variable: "var[idx]".
4964 if (is_decl)
4965 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004966 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004967 goto theend;
4968 }
4969
4970 if (var_start[varlen] == '[')
4971 {
4972 has_index = TRUE;
4973 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004974 member_type = &t_any;
4975 else
4976 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004977 }
4978 else
4979 {
4980 semsg("Not supported yet: %s", var_start);
4981 goto theend;
4982 }
4983 }
4984 else if (lvar == &arg_lvar)
4985 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004986 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004987 goto theend;
4988 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02004989 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
4990 {
4991 semsg(_(e_cannot_assign_to_constant), name);
4992 goto theend;
4993 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004994
4995 if (!heredoc)
4996 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004997 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004998 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004999 if (oplen > 0 && var_count == 0)
5000 {
5001 // skip over the "=" and the expression
5002 p = skipwhite(op + oplen);
5003 compile_expr0(&p, cctx);
5004 }
5005 }
5006 else if (oplen > 0)
5007 {
5008 type_T *stacktype;
5009
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005010 // For "var = expr" evaluate the expression.
5011 if (var_count == 0)
5012 {
5013 int r;
5014
5015 // for "+=", "*=", "..=" etc. first load the current value
5016 if (*op != '=')
5017 {
5018 generate_loadvar(cctx, dest, name, lvar, type);
5019
5020 if (has_index)
5021 {
5022 // TODO: get member from list or dict
5023 emsg("Index with operation not supported yet");
5024 goto theend;
5025 }
5026 }
5027
5028 // Compile the expression. Temporarily hide the new local
5029 // variable here, it is not available to this expression.
5030 if (new_local)
5031 --cctx->ctx_locals.ga_len;
5032 instr_count = instr->ga_len;
5033 p = skipwhite(op + oplen);
5034 r = compile_expr0(&p, cctx);
5035 if (new_local)
5036 ++cctx->ctx_locals.ga_len;
5037 if (r == FAIL)
5038 goto theend;
5039 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005040 else if (semicolon && var_idx == var_count - 1)
5041 {
5042 // For "[var; var] = expr" get the rest of the list
5043 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5044 goto theend;
5045 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005046 else
5047 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005048 // For "[var, var] = expr" get the "var_idx" item from the
5049 // list.
5050 if (generate_GETITEM(cctx, var_idx) == FAIL)
5051 return FAIL;
5052 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005053
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005054 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005055 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005056 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005057 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005058 if ((stacktype->tt_type == VAR_FUNC
5059 || stacktype->tt_type == VAR_PARTIAL)
5060 && var_wrong_func_name(name, TRUE))
5061 goto theend;
5062
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005063 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005064 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005065 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005066 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005067 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005068 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005069 }
5070 else
5071 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005072 // An empty list or dict has a &t_void member,
5073 // for a variable that implies &t_any.
5074 if (stacktype == &t_list_empty)
5075 lvar->lv_type = &t_list_any;
5076 else if (stacktype == &t_dict_empty)
5077 lvar->lv_type = &t_dict_any;
5078 else
5079 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005080 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005081 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005082 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005083 {
5084 type_T *use_type = lvar->lv_type;
5085
Bram Moolenaar71abe482020-09-14 22:28:30 +02005086 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005087 if (has_index)
5088 {
5089 use_type = use_type->tt_member;
5090 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005091 // could be indexing "any"
5092 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005093 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005094 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005095 == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005096 goto theend;
5097 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005098 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005099 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005100 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005101 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005102 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005103 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005104 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005105 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005106 goto theend;
5107 }
5108 else if (!has_type || dest == dest_option)
5109 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005110 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005111 goto theend;
5112 }
5113 else
5114 {
5115 // variables are always initialized
5116 if (ga_grow(instr, 1) == FAIL)
5117 goto theend;
5118 switch (member_type->tt_type)
5119 {
5120 case VAR_BOOL:
5121 generate_PUSHBOOL(cctx, VVAL_FALSE);
5122 break;
5123 case VAR_FLOAT:
5124#ifdef FEAT_FLOAT
5125 generate_PUSHF(cctx, 0.0);
5126#endif
5127 break;
5128 case VAR_STRING:
5129 generate_PUSHS(cctx, NULL);
5130 break;
5131 case VAR_BLOB:
5132 generate_PUSHBLOB(cctx, NULL);
5133 break;
5134 case VAR_FUNC:
5135 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5136 break;
5137 case VAR_LIST:
5138 generate_NEWLIST(cctx, 0);
5139 break;
5140 case VAR_DICT:
5141 generate_NEWDICT(cctx, 0);
5142 break;
5143 case VAR_JOB:
5144 generate_PUSHJOB(cctx, NULL);
5145 break;
5146 case VAR_CHANNEL:
5147 generate_PUSHCHANNEL(cctx, NULL);
5148 break;
5149 case VAR_NUMBER:
5150 case VAR_UNKNOWN:
5151 case VAR_ANY:
5152 case VAR_PARTIAL:
5153 case VAR_VOID:
5154 case VAR_SPECIAL: // cannot happen
5155 generate_PUSHNR(cctx, 0);
5156 break;
5157 }
5158 }
5159 if (var_count == 0)
5160 end = p;
5161 }
5162
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005163 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005164 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005165 break;
5166
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005167 if (oplen > 0 && *op != '=')
5168 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005169 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005170 type_T *stacktype;
5171
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005172 if (*op == '.')
5173 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005174 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005175 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005176 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005177 if (
5178#ifdef FEAT_FLOAT
5179 // If variable is float operation with number is OK.
5180 !(expected == &t_float && stacktype == &t_number) &&
5181#endif
5182 need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005183 goto theend;
5184
5185 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005186 {
5187 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5188 goto theend;
5189 }
5190 else if (*op == '+')
5191 {
5192 if (generate_add_instr(cctx,
5193 operator_type(member_type, stacktype),
5194 member_type, stacktype) == FAIL)
5195 goto theend;
5196 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005197 else if (generate_two_op(cctx, op) == FAIL)
5198 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005199 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005200
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005201 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005202 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005203 int r;
5204
5205 // Compile the "idx" in "var[idx]".
5206 if (new_local)
5207 --cctx->ctx_locals.ga_len;
5208 p = skipwhite(var_start + varlen + 1);
5209 r = compile_expr0(&p, cctx);
5210 if (new_local)
5211 ++cctx->ctx_locals.ga_len;
5212 if (r == FAIL)
5213 goto theend;
5214 if (*skipwhite(p) != ']')
5215 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005216 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005217 emsg(_(e_missbrac));
5218 goto theend;
5219 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005220 if (type == &t_any)
5221 {
5222 type_T *idx_type = ((type_T **)stack->ga_data)[
5223 stack->ga_len - 1];
5224 // Index on variable of unknown type: guess the type from the
5225 // index type: number is dict, otherwise dict.
5226 // TODO: should do the assignment at runtime
5227 if (idx_type->tt_type == VAR_NUMBER)
5228 type = &t_list_any;
5229 else
5230 type = &t_dict_any;
5231 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005232 if (type->tt_type == VAR_DICT
5233 && may_generate_2STRING(-1, cctx) == FAIL)
5234 goto theend;
5235 if (type->tt_type == VAR_LIST
5236 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005237 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005238 {
5239 emsg(_(e_number_exp));
5240 goto theend;
5241 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005242
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005243 // Load the dict or list. On the stack we then have:
5244 // - value
5245 // - index
5246 // - variable
5247 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005248
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005249 if (type->tt_type == VAR_LIST)
5250 {
5251 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5252 return FAIL;
5253 }
5254 else if (type->tt_type == VAR_DICT)
5255 {
5256 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5257 return FAIL;
5258 }
5259 else
5260 {
5261 emsg(_(e_listreq));
5262 goto theend;
5263 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005264 }
5265 else
5266 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005267 if (is_decl && eap->forceit && cmdidx == CMD_const
5268 && (dest == dest_script || dest == dest_local))
5269 // ":const! var": lock the value, but not referenced variables
5270 generate_LOCKCONST(cctx);
5271
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005272 switch (dest)
5273 {
5274 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005275 generate_STOREOPT(cctx, skip_option_env_lead(name),
5276 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005277 break;
5278 case dest_global:
5279 // include g: with the name, easier to execute that way
5280 generate_STORE(cctx, ISN_STOREG, 0, name);
5281 break;
5282 case dest_buffer:
5283 // include b: with the name, easier to execute that way
5284 generate_STORE(cctx, ISN_STOREB, 0, name);
5285 break;
5286 case dest_window:
5287 // include w: with the name, easier to execute that way
5288 generate_STORE(cctx, ISN_STOREW, 0, name);
5289 break;
5290 case dest_tab:
5291 // include t: with the name, easier to execute that way
5292 generate_STORE(cctx, ISN_STORET, 0, name);
5293 break;
5294 case dest_env:
5295 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5296 break;
5297 case dest_reg:
5298 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5299 break;
5300 case dest_vimvar:
5301 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5302 break;
5303 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005304 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005305 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005306 {
5307 char_u *name_s = name;
5308
5309 // Include s: in the name for store_var()
5310 if (name[1] != ':')
5311 {
5312 int len = (int)STRLEN(name) + 3;
5313
5314 name_s = alloc(len);
5315 if (name_s == NULL)
5316 name_s = name;
5317 else
5318 vim_snprintf((char *)name_s, len,
5319 "s:%s", name);
5320 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005321 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5322 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005323 if (name_s != name)
5324 vim_free(name_s);
5325 }
5326 else
5327 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005328 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005329 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005330 break;
5331 case dest_local:
5332 if (lvar != NULL)
5333 {
5334 isn_T *isn = ((isn_T *)instr->ga_data)
5335 + instr->ga_len - 1;
5336
5337 // optimization: turn "var = 123" from ISN_PUSHNR +
5338 // ISN_STORE into ISN_STORENR
5339 if (!lvar->lv_from_outer
5340 && instr->ga_len == instr_count + 1
5341 && isn->isn_type == ISN_PUSHNR)
5342 {
5343 varnumber_T val = isn->isn_arg.number;
5344
5345 isn->isn_type = ISN_STORENR;
5346 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5347 isn->isn_arg.storenr.stnr_val = val;
5348 if (stack->ga_len > 0)
5349 --stack->ga_len;
5350 }
5351 else if (lvar->lv_from_outer)
5352 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005353 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005354 else
5355 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5356 }
5357 break;
5358 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005359 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005360
5361 if (var_idx + 1 < var_count)
5362 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005363 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005364
5365 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005366 if (var_count > 0 && !semicolon)
5367 {
5368 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5369 goto theend;
5370 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005371
Bram Moolenaarb2097502020-07-19 17:17:02 +02005372 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005373
5374theend:
5375 vim_free(name);
5376 return ret;
5377}
5378
5379/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005380 * Check if "name" can be "unlet".
5381 */
5382 int
5383check_vim9_unlet(char_u *name)
5384{
5385 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5386 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005387 // "unlet s:var" is allowed in legacy script.
5388 if (*name == 's' && !script_is_vim9())
5389 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005390 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005391 return FAIL;
5392 }
5393 return OK;
5394}
5395
5396/*
5397 * Callback passed to ex_unletlock().
5398 */
5399 static int
5400compile_unlet(
5401 lval_T *lvp,
5402 char_u *name_end,
5403 exarg_T *eap,
5404 int deep UNUSED,
5405 void *coookie)
5406{
5407 cctx_T *cctx = coookie;
5408
5409 if (lvp->ll_tv == NULL)
5410 {
5411 char_u *p = lvp->ll_name;
5412 int cc = *name_end;
5413 int ret = OK;
5414
5415 // Normal name. Only supports g:, w:, t: and b: namespaces.
5416 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005417 if (*p == '$')
5418 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5419 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005420 ret = FAIL;
5421 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005422 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005423
5424 *name_end = cc;
5425 return ret;
5426 }
5427
5428 // TODO: unlet {list}[idx]
5429 // TODO: unlet {dict}[key]
5430 emsg("Sorry, :unlet not fully implemented yet");
5431 return FAIL;
5432}
5433
5434/*
5435 * compile "unlet var", "lock var" and "unlock var"
5436 * "arg" points to "var".
5437 */
5438 static char_u *
5439compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5440{
5441 char_u *p = arg;
5442
5443 if (eap->cmdidx != CMD_unlet)
5444 {
5445 emsg("Sorry, :lock and unlock not implemented yet");
5446 return NULL;
5447 }
5448
5449 if (*p == '!')
5450 {
5451 p = skipwhite(p + 1);
5452 eap->forceit = TRUE;
5453 }
5454
5455 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5456 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5457}
5458
5459/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005460 * Compile an :import command.
5461 */
5462 static char_u *
5463compile_import(char_u *arg, cctx_T *cctx)
5464{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005465 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005466}
5467
5468/*
5469 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5470 */
5471 static int
5472compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5473{
5474 garray_T *instr = &cctx->ctx_instr;
5475 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5476
5477 if (endlabel == NULL)
5478 return FAIL;
5479 endlabel->el_next = *el;
5480 *el = endlabel;
5481 endlabel->el_end_label = instr->ga_len;
5482
5483 generate_JUMP(cctx, when, 0);
5484 return OK;
5485}
5486
5487 static void
5488compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5489{
5490 garray_T *instr = &cctx->ctx_instr;
5491
5492 while (*el != NULL)
5493 {
5494 endlabel_T *cur = (*el);
5495 isn_T *isn;
5496
5497 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5498 isn->isn_arg.jump.jump_where = instr->ga_len;
5499 *el = cur->el_next;
5500 vim_free(cur);
5501 }
5502}
5503
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005504 static void
5505compile_free_jump_to_end(endlabel_T **el)
5506{
5507 while (*el != NULL)
5508 {
5509 endlabel_T *cur = (*el);
5510
5511 *el = cur->el_next;
5512 vim_free(cur);
5513 }
5514}
5515
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005516/*
5517 * Create a new scope and set up the generic items.
5518 */
5519 static scope_T *
5520new_scope(cctx_T *cctx, scopetype_T type)
5521{
5522 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5523
5524 if (scope == NULL)
5525 return NULL;
5526 scope->se_outer = cctx->ctx_scope;
5527 cctx->ctx_scope = scope;
5528 scope->se_type = type;
5529 scope->se_local_count = cctx->ctx_locals.ga_len;
5530 return scope;
5531}
5532
5533/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005534 * Free the current scope and go back to the outer scope.
5535 */
5536 static void
5537drop_scope(cctx_T *cctx)
5538{
5539 scope_T *scope = cctx->ctx_scope;
5540
5541 if (scope == NULL)
5542 {
5543 iemsg("calling drop_scope() without a scope");
5544 return;
5545 }
5546 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005547 switch (scope->se_type)
5548 {
5549 case IF_SCOPE:
5550 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5551 case FOR_SCOPE:
5552 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5553 case WHILE_SCOPE:
5554 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5555 case TRY_SCOPE:
5556 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5557 case NO_SCOPE:
5558 case BLOCK_SCOPE:
5559 break;
5560 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005561 vim_free(scope);
5562}
5563
5564/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005565 * compile "if expr"
5566 *
5567 * "if expr" Produces instructions:
5568 * EVAL expr Push result of "expr"
5569 * JUMP_IF_FALSE end
5570 * ... body ...
5571 * end:
5572 *
5573 * "if expr | else" Produces instructions:
5574 * EVAL expr Push result of "expr"
5575 * JUMP_IF_FALSE else
5576 * ... body ...
5577 * JUMP_ALWAYS end
5578 * else:
5579 * ... body ...
5580 * end:
5581 *
5582 * "if expr1 | elseif expr2 | else" Produces instructions:
5583 * EVAL expr Push result of "expr"
5584 * JUMP_IF_FALSE elseif
5585 * ... body ...
5586 * JUMP_ALWAYS end
5587 * elseif:
5588 * EVAL expr Push result of "expr"
5589 * JUMP_IF_FALSE else
5590 * ... body ...
5591 * JUMP_ALWAYS end
5592 * else:
5593 * ... body ...
5594 * end:
5595 */
5596 static char_u *
5597compile_if(char_u *arg, cctx_T *cctx)
5598{
5599 char_u *p = arg;
5600 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005601 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005602 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005603 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005604 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005605
Bram Moolenaara5565e42020-05-09 15:44:01 +02005606 CLEAR_FIELD(ppconst);
5607 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005608 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005609 clear_ppconst(&ppconst);
5610 return NULL;
5611 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005612 if (cctx->ctx_skip == SKIP_YES)
5613 clear_ppconst(&ppconst);
5614 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005615 {
5616 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005617 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005618 clear_ppconst(&ppconst);
5619 }
5620 else
5621 {
5622 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005623 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005624 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005625 return NULL;
5626 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005627
5628 scope = new_scope(cctx, IF_SCOPE);
5629 if (scope == NULL)
5630 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005631 scope->se_skip_save = skip_save;
5632 // "is_had_return" will be reset if any block does not end in :return
5633 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005634
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005635 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005636 {
5637 // "where" is set when ":elseif", "else" or ":endif" is found
5638 scope->se_u.se_if.is_if_label = instr->ga_len;
5639 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5640 }
5641 else
5642 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005643
5644 return p;
5645}
5646
5647 static char_u *
5648compile_elseif(char_u *arg, cctx_T *cctx)
5649{
5650 char_u *p = arg;
5651 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005652 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005653 isn_T *isn;
5654 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005655 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02005656 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005657
5658 if (scope == NULL || scope->se_type != IF_SCOPE)
5659 {
5660 emsg(_(e_elseif_without_if));
5661 return NULL;
5662 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005663 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005664 if (!cctx->ctx_had_return)
5665 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005666
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005667 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005668 {
5669 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005670 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005671 return NULL;
5672 // previous "if" or "elseif" jumps here
5673 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5674 isn->isn_arg.jump.jump_where = instr->ga_len;
5675 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005676
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005677 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005678 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02005679 if (cctx->ctx_skip == SKIP_YES)
5680 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005681 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005682 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005683 clear_ppconst(&ppconst);
5684 return NULL;
5685 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02005686 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005687 if (scope->se_skip_save == SKIP_YES)
5688 clear_ppconst(&ppconst);
5689 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005690 {
5691 // The expression results in a constant.
5692 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005693 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005694 clear_ppconst(&ppconst);
5695 scope->se_u.se_if.is_if_label = -1;
5696 }
5697 else
5698 {
5699 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005700 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005701 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005702 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005703
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005704 // "where" is set when ":elseif", "else" or ":endif" is found
5705 scope->se_u.se_if.is_if_label = instr->ga_len;
5706 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5707 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005708
5709 return p;
5710}
5711
5712 static char_u *
5713compile_else(char_u *arg, cctx_T *cctx)
5714{
5715 char_u *p = arg;
5716 garray_T *instr = &cctx->ctx_instr;
5717 isn_T *isn;
5718 scope_T *scope = cctx->ctx_scope;
5719
5720 if (scope == NULL || scope->se_type != IF_SCOPE)
5721 {
5722 emsg(_(e_else_without_if));
5723 return NULL;
5724 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005725 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005726 if (!cctx->ctx_had_return)
5727 scope->se_u.se_if.is_had_return = FALSE;
5728 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005729
Bram Moolenaarefd88552020-06-18 20:50:10 +02005730 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005731 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005732 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005733 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005734 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005735 if (!cctx->ctx_had_return
5736 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5737 JUMP_ALWAYS, cctx) == FAIL)
5738 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005739 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005740
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005741 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005742 {
5743 if (scope->se_u.se_if.is_if_label >= 0)
5744 {
5745 // previous "if" or "elseif" jumps here
5746 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5747 isn->isn_arg.jump.jump_where = instr->ga_len;
5748 scope->se_u.se_if.is_if_label = -1;
5749 }
5750 }
5751
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005752 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005753 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5754 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005755
5756 return p;
5757}
5758
5759 static char_u *
5760compile_endif(char_u *arg, cctx_T *cctx)
5761{
5762 scope_T *scope = cctx->ctx_scope;
5763 ifscope_T *ifscope;
5764 garray_T *instr = &cctx->ctx_instr;
5765 isn_T *isn;
5766
5767 if (scope == NULL || scope->se_type != IF_SCOPE)
5768 {
5769 emsg(_(e_endif_without_if));
5770 return NULL;
5771 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005772 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005773 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005774 if (!cctx->ctx_had_return)
5775 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005776
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005777 if (scope->se_u.se_if.is_if_label >= 0)
5778 {
5779 // previous "if" or "elseif" jumps here
5780 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5781 isn->isn_arg.jump.jump_where = instr->ga_len;
5782 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005783 // Fill in the "end" label in jumps at the end of the blocks.
5784 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005785 cctx->ctx_skip = scope->se_skip_save;
5786
5787 // If all the blocks end in :return and there is an :else then the
5788 // had_return flag is set.
5789 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005790
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005791 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005792 return arg;
5793}
5794
5795/*
5796 * compile "for var in expr"
5797 *
5798 * Produces instructions:
5799 * PUSHNR -1
5800 * STORE loop-idx Set index to -1
5801 * EVAL expr Push result of "expr"
5802 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5803 * - if beyond end, jump to "end"
5804 * - otherwise get item from list and push it
5805 * STORE var Store item in "var"
5806 * ... body ...
5807 * JUMP top Jump back to repeat
5808 * end: DROP Drop the result of "expr"
5809 *
5810 */
5811 static char_u *
5812compile_for(char_u *arg, cctx_T *cctx)
5813{
5814 char_u *p;
5815 size_t varlen;
5816 garray_T *instr = &cctx->ctx_instr;
5817 garray_T *stack = &cctx->ctx_type_stack;
5818 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005819 lvar_T *loop_lvar; // loop iteration variable
5820 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005821 type_T *vartype;
5822
5823 // TODO: list of variables: "for [key, value] in dict"
5824 // parse "var"
5825 for (p = arg; eval_isnamec1(*p); ++p)
5826 ;
5827 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005828 var_lvar = lookup_local(arg, varlen, cctx);
5829 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005830 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005831 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005832 return NULL;
5833 }
5834
5835 // consume "in"
5836 p = skipwhite(p);
5837 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5838 {
5839 emsg(_(e_missing_in));
5840 return NULL;
5841 }
5842 p = skipwhite(p + 2);
5843
5844
5845 scope = new_scope(cctx, FOR_SCOPE);
5846 if (scope == NULL)
5847 return NULL;
5848
5849 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005850 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5851 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005852 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005853 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005854 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005855 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005856 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005857
5858 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005859 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5860 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005861 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005862 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005863 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005864 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005865 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005866
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005867 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005868
5869 // compile "expr", it remains on the stack until "endfor"
5870 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005871 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005872 {
5873 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005874 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005875 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005876
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005877 // Now that we know the type of "var", check that it is a list, now or at
5878 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005879 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005880 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005881 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005882 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005883 return NULL;
5884 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005885 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005886 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005887
5888 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005889 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005890
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005891 generate_FOR(cctx, loop_lvar->lv_idx);
5892 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005893
5894 return arg;
5895}
5896
5897/*
5898 * compile "endfor"
5899 */
5900 static char_u *
5901compile_endfor(char_u *arg, cctx_T *cctx)
5902{
5903 garray_T *instr = &cctx->ctx_instr;
5904 scope_T *scope = cctx->ctx_scope;
5905 forscope_T *forscope;
5906 isn_T *isn;
5907
5908 if (scope == NULL || scope->se_type != FOR_SCOPE)
5909 {
5910 emsg(_(e_for));
5911 return NULL;
5912 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005913 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005914 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005915 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005916
5917 // At end of ":for" scope jump back to the FOR instruction.
5918 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5919
5920 // Fill in the "end" label in the FOR statement so it can jump here
5921 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5922 isn->isn_arg.forloop.for_end = instr->ga_len;
5923
5924 // Fill in the "end" label any BREAK statements
5925 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5926
5927 // Below the ":for" scope drop the "expr" list from the stack.
5928 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5929 return NULL;
5930
5931 vim_free(scope);
5932
5933 return arg;
5934}
5935
5936/*
5937 * compile "while expr"
5938 *
5939 * Produces instructions:
5940 * top: EVAL expr Push result of "expr"
5941 * JUMP_IF_FALSE end jump if false
5942 * ... body ...
5943 * JUMP top Jump back to repeat
5944 * end:
5945 *
5946 */
5947 static char_u *
5948compile_while(char_u *arg, cctx_T *cctx)
5949{
5950 char_u *p = arg;
5951 garray_T *instr = &cctx->ctx_instr;
5952 scope_T *scope;
5953
5954 scope = new_scope(cctx, WHILE_SCOPE);
5955 if (scope == NULL)
5956 return NULL;
5957
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005958 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005959
5960 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005961 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005962 return NULL;
5963
5964 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005965 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005966 JUMP_IF_FALSE, cctx) == FAIL)
5967 return FAIL;
5968
5969 return p;
5970}
5971
5972/*
5973 * compile "endwhile"
5974 */
5975 static char_u *
5976compile_endwhile(char_u *arg, cctx_T *cctx)
5977{
5978 scope_T *scope = cctx->ctx_scope;
5979
5980 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5981 {
5982 emsg(_(e_while));
5983 return NULL;
5984 }
5985 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005986 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005987
5988 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005989 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005990
5991 // Fill in the "end" label in the WHILE statement so it can jump here.
5992 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005993 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005994
5995 vim_free(scope);
5996
5997 return arg;
5998}
5999
6000/*
6001 * compile "continue"
6002 */
6003 static char_u *
6004compile_continue(char_u *arg, cctx_T *cctx)
6005{
6006 scope_T *scope = cctx->ctx_scope;
6007
6008 for (;;)
6009 {
6010 if (scope == NULL)
6011 {
6012 emsg(_(e_continue));
6013 return NULL;
6014 }
6015 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6016 break;
6017 scope = scope->se_outer;
6018 }
6019
6020 // Jump back to the FOR or WHILE instruction.
6021 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006022 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6023 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006024 return arg;
6025}
6026
6027/*
6028 * compile "break"
6029 */
6030 static char_u *
6031compile_break(char_u *arg, cctx_T *cctx)
6032{
6033 scope_T *scope = cctx->ctx_scope;
6034 endlabel_T **el;
6035
6036 for (;;)
6037 {
6038 if (scope == NULL)
6039 {
6040 emsg(_(e_break));
6041 return NULL;
6042 }
6043 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6044 break;
6045 scope = scope->se_outer;
6046 }
6047
6048 // Jump to the end of the FOR or WHILE loop.
6049 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006050 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006051 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006052 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006053 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6054 return FAIL;
6055
6056 return arg;
6057}
6058
6059/*
6060 * compile "{" start of block
6061 */
6062 static char_u *
6063compile_block(char_u *arg, cctx_T *cctx)
6064{
6065 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6066 return NULL;
6067 return skipwhite(arg + 1);
6068}
6069
6070/*
6071 * compile end of block: drop one scope
6072 */
6073 static void
6074compile_endblock(cctx_T *cctx)
6075{
6076 scope_T *scope = cctx->ctx_scope;
6077
6078 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006079 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006080 vim_free(scope);
6081}
6082
6083/*
6084 * compile "try"
6085 * Creates a new scope for the try-endtry, pointing to the first catch and
6086 * finally.
6087 * Creates another scope for the "try" block itself.
6088 * TRY instruction sets up exception handling at runtime.
6089 *
6090 * "try"
6091 * TRY -> catch1, -> finally push trystack entry
6092 * ... try block
6093 * "throw {exception}"
6094 * EVAL {exception}
6095 * THROW create exception
6096 * ... try block
6097 * " catch {expr}"
6098 * JUMP -> finally
6099 * catch1: PUSH exeception
6100 * EVAL {expr}
6101 * MATCH
6102 * JUMP nomatch -> catch2
6103 * CATCH remove exception
6104 * ... catch block
6105 * " catch"
6106 * JUMP -> finally
6107 * catch2: CATCH remove exception
6108 * ... catch block
6109 * " finally"
6110 * finally:
6111 * ... finally block
6112 * " endtry"
6113 * ENDTRY pop trystack entry, may rethrow
6114 */
6115 static char_u *
6116compile_try(char_u *arg, cctx_T *cctx)
6117{
6118 garray_T *instr = &cctx->ctx_instr;
6119 scope_T *try_scope;
6120 scope_T *scope;
6121
6122 // scope that holds the jumps that go to catch/finally/endtry
6123 try_scope = new_scope(cctx, TRY_SCOPE);
6124 if (try_scope == NULL)
6125 return NULL;
6126
6127 // "catch" is set when the first ":catch" is found.
6128 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006129 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006130 if (generate_instr(cctx, ISN_TRY) == NULL)
6131 return NULL;
6132
6133 // scope for the try block itself
6134 scope = new_scope(cctx, BLOCK_SCOPE);
6135 if (scope == NULL)
6136 return NULL;
6137
6138 return arg;
6139}
6140
6141/*
6142 * compile "catch {expr}"
6143 */
6144 static char_u *
6145compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6146{
6147 scope_T *scope = cctx->ctx_scope;
6148 garray_T *instr = &cctx->ctx_instr;
6149 char_u *p;
6150 isn_T *isn;
6151
6152 // end block scope from :try or :catch
6153 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6154 compile_endblock(cctx);
6155 scope = cctx->ctx_scope;
6156
6157 // Error if not in a :try scope
6158 if (scope == NULL || scope->se_type != TRY_SCOPE)
6159 {
6160 emsg(_(e_catch));
6161 return NULL;
6162 }
6163
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006164 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006165 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006166 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006167 return NULL;
6168 }
6169
6170 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006171 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006172 JUMP_ALWAYS, cctx) == FAIL)
6173 return NULL;
6174
6175 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006176 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006177 if (isn->isn_arg.try.try_catch == 0)
6178 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006179 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006180 {
6181 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006182 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006183 isn->isn_arg.jump.jump_where = instr->ga_len;
6184 }
6185
6186 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006187 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006188 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006189 scope->se_u.se_try.ts_caught_all = TRUE;
6190 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006191 }
6192 else
6193 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006194 char_u *end;
6195 char_u *pat;
6196 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006197 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006198 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006199
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006200 // Push v:exception, push {expr} and MATCH
6201 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6202
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006203 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006204 if (*end != *p)
6205 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006206 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006207 vim_free(tofree);
6208 return FAIL;
6209 }
6210 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006211 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006212 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006213 len = (int)(end - tofree);
6214 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006215 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006216 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006217 if (pat == NULL)
6218 return FAIL;
6219 if (generate_PUSHS(cctx, pat) == FAIL)
6220 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6223 return NULL;
6224
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006225 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006226 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6227 return NULL;
6228 }
6229
6230 if (generate_instr(cctx, ISN_CATCH) == NULL)
6231 return NULL;
6232
6233 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6234 return NULL;
6235 return p;
6236}
6237
6238 static char_u *
6239compile_finally(char_u *arg, cctx_T *cctx)
6240{
6241 scope_T *scope = cctx->ctx_scope;
6242 garray_T *instr = &cctx->ctx_instr;
6243 isn_T *isn;
6244
6245 // end block scope from :try or :catch
6246 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6247 compile_endblock(cctx);
6248 scope = cctx->ctx_scope;
6249
6250 // Error if not in a :try scope
6251 if (scope == NULL || scope->se_type != TRY_SCOPE)
6252 {
6253 emsg(_(e_finally));
6254 return NULL;
6255 }
6256
6257 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006258 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006259 if (isn->isn_arg.try.try_finally != 0)
6260 {
6261 emsg(_(e_finally_dup));
6262 return NULL;
6263 }
6264
6265 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006266 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006267
Bram Moolenaar585fea72020-04-02 22:33:21 +02006268 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006269 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006270 {
6271 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006272 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006273 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006274 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006275 }
6276
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006277 // TODO: set index in ts_finally_label jumps
6278
6279 return arg;
6280}
6281
6282 static char_u *
6283compile_endtry(char_u *arg, cctx_T *cctx)
6284{
6285 scope_T *scope = cctx->ctx_scope;
6286 garray_T *instr = &cctx->ctx_instr;
6287 isn_T *isn;
6288
6289 // end block scope from :catch or :finally
6290 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6291 compile_endblock(cctx);
6292 scope = cctx->ctx_scope;
6293
6294 // Error if not in a :try scope
6295 if (scope == NULL || scope->se_type != TRY_SCOPE)
6296 {
6297 if (scope == NULL)
6298 emsg(_(e_no_endtry));
6299 else if (scope->se_type == WHILE_SCOPE)
6300 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006301 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006302 emsg(_(e_endfor));
6303 else
6304 emsg(_(e_endif));
6305 return NULL;
6306 }
6307
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006308 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006309 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6310 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006311 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006312 return NULL;
6313 }
6314
6315 // Fill in the "end" label in jumps at the end of the blocks, if not done
6316 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006317 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006318
6319 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006320 if (isn->isn_arg.try.try_catch == 0)
6321 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006322 if (isn->isn_arg.try.try_finally == 0)
6323 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006324
6325 if (scope->se_u.se_try.ts_catch_label != 0)
6326 {
6327 // Last catch without match jumps here
6328 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6329 isn->isn_arg.jump.jump_where = instr->ga_len;
6330 }
6331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006332 compile_endblock(cctx);
6333
6334 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6335 return NULL;
6336 return arg;
6337}
6338
6339/*
6340 * compile "throw {expr}"
6341 */
6342 static char_u *
6343compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6344{
6345 char_u *p = skipwhite(arg);
6346
Bram Moolenaara5565e42020-05-09 15:44:01 +02006347 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006348 return NULL;
6349 if (may_generate_2STRING(-1, cctx) == FAIL)
6350 return NULL;
6351 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6352 return NULL;
6353
6354 return p;
6355}
6356
6357/*
6358 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006359 * compile "echomsg expr"
6360 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006361 * compile "execute expr"
6362 */
6363 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006364compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006365{
6366 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006367 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006368 int count = 0;
6369
6370 for (;;)
6371 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006372 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006373 return NULL;
6374 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006375 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006376 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006377 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006378 break;
6379 }
6380
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006381 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6382 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6383 else if (cmdidx == CMD_execute)
6384 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6385 else if (cmdidx == CMD_echomsg)
6386 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6387 else
6388 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006389 return p;
6390}
6391
6392/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006393 * :put r
6394 * :put ={expr}
6395 */
6396 static char_u *
6397compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6398{
6399 char_u *line = arg;
6400 linenr_T lnum;
6401 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006402 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006403
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006404 eap->regname = *line;
6405
6406 if (eap->regname == '=')
6407 {
6408 char_u *p = line + 1;
6409
6410 if (compile_expr0(&p, cctx) == FAIL)
6411 return NULL;
6412 line = p;
6413 }
6414 else if (eap->regname != NUL)
6415 ++line;
6416
6417 // TODO: if the range is something like "$" need to evaluate at runtime
6418 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6419 return NULL;
6420 if (eap->addr_count == 0)
6421 lnum = -1;
6422 else
6423 lnum = eap->line2;
6424 if (above)
6425 --lnum;
6426
6427 generate_PUT(cctx, eap->regname, lnum);
6428 return line;
6429}
6430
6431/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006432 * A command that is not compiled, execute with legacy code.
6433 */
6434 static char_u *
6435compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6436{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006437 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006438 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006439 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006440
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006441 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006442 goto theend;
6443
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006444 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006445 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006446 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006447 int usefilter = FALSE;
6448
6449 has_expr = argt & (EX_XFILE | EX_EXPAND);
6450
6451 // If the command can be followed by a bar, find the bar and truncate
6452 // it, so that the following command can be compiled.
6453 // The '|' is overwritten with a NUL, it is put back below.
6454 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6455 && *eap->arg == '!')
6456 // :w !filter or :r !filter or :r! filter
6457 usefilter = TRUE;
6458 if ((argt & EX_TRLBAR) && !usefilter)
6459 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006460 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006461 separate_nextcmd(eap);
6462 if (eap->nextcmd != NULL)
6463 nextcmd = eap->nextcmd;
6464 }
6465 }
6466
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006467 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6468 {
6469 // expand filename in "syntax include [@group] filename"
6470 has_expr = TRUE;
6471 eap->arg = skipwhite(eap->arg + 7);
6472 if (*eap->arg == '@')
6473 eap->arg = skiptowhite(eap->arg);
6474 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006475
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006476 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006477 {
6478 int count = 0;
6479 char_u *start = skipwhite(line);
6480
6481 // :cmd xxx`=expr1`yyy`=expr2`zzz
6482 // PUSHS ":cmd xxx"
6483 // eval expr1
6484 // PUSHS "yyy"
6485 // eval expr2
6486 // PUSHS "zzz"
6487 // EXECCONCAT 5
6488 for (;;)
6489 {
6490 if (p > start)
6491 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006492 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006493 ++count;
6494 }
6495 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006496 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006497 return NULL;
6498 may_generate_2STRING(-1, cctx);
6499 ++count;
6500 p = skipwhite(p);
6501 if (*p != '`')
6502 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006503 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006504 return NULL;
6505 }
6506 start = p + 1;
6507
6508 p = (char_u *)strstr((char *)start, "`=");
6509 if (p == NULL)
6510 {
6511 if (*skipwhite(start) != NUL)
6512 {
6513 generate_PUSHS(cctx, vim_strsave(start));
6514 ++count;
6515 }
6516 break;
6517 }
6518 }
6519 generate_EXECCONCAT(cctx, count);
6520 }
6521 else
6522 generate_EXEC(cctx, line);
6523
6524theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006525 if (*nextcmd != NUL)
6526 {
6527 // the parser expects a pointer to the bar, put it back
6528 --nextcmd;
6529 *nextcmd = '|';
6530 }
6531
6532 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006533}
6534
6535/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006536 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006537 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006538 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006539 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006540add_def_function(ufunc_T *ufunc)
6541{
6542 dfunc_T *dfunc;
6543
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006544 if (def_functions.ga_len == 0)
6545 {
6546 // The first position is not used, so that a zero uf_dfunc_idx means it
6547 // wasn't set.
6548 if (ga_grow(&def_functions, 1) == FAIL)
6549 return FAIL;
6550 ++def_functions.ga_len;
6551 }
6552
Bram Moolenaar09689a02020-05-09 22:50:08 +02006553 // Add the function to "def_functions".
6554 if (ga_grow(&def_functions, 1) == FAIL)
6555 return FAIL;
6556 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6557 CLEAR_POINTER(dfunc);
6558 dfunc->df_idx = def_functions.ga_len;
6559 ufunc->uf_dfunc_idx = dfunc->df_idx;
6560 dfunc->df_ufunc = ufunc;
6561 ++def_functions.ga_len;
6562 return OK;
6563}
6564
6565/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006566 * After ex_function() has collected all the function lines: parse and compile
6567 * the lines into instructions.
6568 * Adds the function to "def_functions".
6569 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6570 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006571 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006572 * This can be used recursively through compile_lambda(), which may reallocate
6573 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006574 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006575 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006576 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006577compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006578{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006579 char_u *line = NULL;
6580 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006581 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006582 cctx_T cctx;
6583 garray_T *instr;
6584 int called_emsg_before = called_emsg;
6585 int ret = FAIL;
6586 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006587 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006588 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006589 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006590
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006591 // When using a function that was compiled before: Free old instructions.
6592 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006593 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006594 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006595 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6596 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006597 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006598 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006599 else
6600 {
6601 if (add_def_function(ufunc) == FAIL)
6602 return FAIL;
6603 new_def_function = TRUE;
6604 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006605
Bram Moolenaar985116a2020-07-12 17:31:09 +02006606 ufunc->uf_def_status = UF_COMPILING;
6607
Bram Moolenaara80faa82020-04-12 19:37:17 +02006608 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006609 cctx.ctx_ufunc = ufunc;
6610 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006611 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006612 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6613 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6614 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6615 cctx.ctx_type_list = &ufunc->uf_type_list;
6616 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6617 instr = &cctx.ctx_instr;
6618
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006619 // Set the context to the function, it may be compiled when called from
6620 // another script. Set the script version to the most modern one.
6621 // The line number will be set in next_line_from_context().
6622 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006623 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6624
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006625 // Make sure error messages are OK.
6626 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6627 if (do_estack_push)
6628 estack_push_ufunc(ufunc, 1);
6629
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006630 if (ufunc->uf_def_args.ga_len > 0)
6631 {
6632 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006633 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006634 int i;
6635 char_u *arg;
6636 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006637 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006638
6639 // Produce instructions for the default values of optional arguments.
6640 // Store the instruction index in uf_def_arg_idx[] so that we know
6641 // where to start when the function is called, depending on the number
6642 // of arguments.
6643 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6644 if (ufunc->uf_def_arg_idx == NULL)
6645 goto erret;
6646 for (i = 0; i < count; ++i)
6647 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006648 garray_T *stack = &cctx.ctx_type_stack;
6649 type_T *val_type;
6650 int arg_idx = first_def_arg + i;
6651
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006652 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6653 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006654 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006655 goto erret;
6656
6657 // If no type specified use the type of the default value.
6658 // Otherwise check that the default value type matches the
6659 // specified type.
6660 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6661 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006662 {
6663 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006664 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006665 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02006666 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
6667 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006668 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006669
6670 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006671 goto erret;
6672 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006673 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006674
6675 if (did_set_arg_type)
6676 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006677 }
6678
6679 /*
6680 * Loop over all the lines of the function and generate instructions.
6681 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006682 for (;;)
6683 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006684 exarg_T ea;
6685 cmdmod_T save_cmdmod;
6686 int starts_with_colon = FALSE;
6687 char_u *cmd;
6688 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006689
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006690 // Bail out on the first error to avoid a flood of errors and report
6691 // the right line number when inside try/catch.
6692 if (emsg_before != called_emsg)
6693 goto erret;
6694
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006695 if (line != NULL && *line == '|')
6696 // the line continues after a '|'
6697 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006698 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006699 && !(*line == '#' && (line == cctx.ctx_line_start
6700 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006701 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006702 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006703 goto erret;
6704 }
6705 else
6706 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006707 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006708 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006709 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006710 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006711 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006712 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006713
Bram Moolenaara80faa82020-04-12 19:37:17 +02006714 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006715 ea.cmdlinep = &line;
6716 ea.cmd = skipwhite(line);
6717
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006718 // Some things can be recognized by the first character.
6719 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006720 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006721 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006722 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006723 if (ea.cmd[1] != '{')
6724 {
6725 line = (char_u *)"";
6726 continue;
6727 }
6728 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006729
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006730 case '}':
6731 {
6732 // "}" ends a block scope
6733 scopetype_T stype = cctx.ctx_scope == NULL
6734 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006735
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006736 if (stype == BLOCK_SCOPE)
6737 {
6738 compile_endblock(&cctx);
6739 line = ea.cmd;
6740 }
6741 else
6742 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006743 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006744 goto erret;
6745 }
6746 if (line != NULL)
6747 line = skipwhite(ea.cmd + 1);
6748 continue;
6749 }
6750
6751 case '{':
6752 // "{" starts a block scope
6753 // "{'a': 1}->func() is something else
6754 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6755 {
6756 line = compile_block(ea.cmd, &cctx);
6757 continue;
6758 }
6759 break;
6760
6761 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006762 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006763 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006764 }
6765
6766 /*
6767 * COMMAND MODIFIERS
6768 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006769 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006770 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6771 {
6772 if (errormsg != NULL)
6773 goto erret;
6774 // empty line or comment
6775 line = (char_u *)"";
6776 continue;
6777 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006778 // TODO: use modifiers in the command
6779 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006780 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006781
6782 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006783 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006784 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006785 {
6786 if (*ea.cmd == '(')
6787 // not for "call()"
6788 ea.cmd = p;
6789 else
6790 ea.cmd = skipwhite(ea.cmd);
6791 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006792
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006793 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006794 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006795 char_u *pskip;
6796
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006797 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006798 // find what follows.
6799 // Skip over "var.member", "var[idx]" and the like.
6800 // Also "&opt = val", "$ENV = val" and "@r = val".
6801 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006802 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006803 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006804 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006805 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006806 char_u *var_end;
6807 int oplen;
6808 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006809
Bram Moolenaar65821722020-08-02 18:58:54 +02006810 if (ea.cmd[0] == '@')
6811 var_end = ea.cmd + 2;
6812 else
6813 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006814 FNE_CHECK_START | FNE_INCL_BR);
6815 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006816 if (oplen > 0)
6817 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006818 size_t len = p - ea.cmd;
6819
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006820 // Recognize an assignment if we recognize the variable
6821 // name:
6822 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006823 // "local = expr" where "local" is a local var.
6824 // "script = expr" where "script" is a script-local var.
6825 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006826 // "&opt = expr"
6827 // "$ENV = expr"
6828 // "@r = expr"
6829 if (*ea.cmd == '&'
6830 || *ea.cmd == '$'
6831 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006832 || ((len) > 2 && ea.cmd[1] == ':')
6833 || lookup_local(ea.cmd, len, &cctx) != NULL
6834 || lookup_arg(ea.cmd, len, NULL, NULL,
6835 NULL, &cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02006836 || lookup_script(ea.cmd, len, FALSE) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006837 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006838 {
6839 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006840 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006841 goto erret;
6842 continue;
6843 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006844 }
6845 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006846
6847 if (*ea.cmd == '[')
6848 {
6849 // [var, var] = expr
6850 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6851 if (line == NULL)
6852 goto erret;
6853 if (line != ea.cmd)
6854 continue;
6855 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006856 }
6857
6858 /*
6859 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006860 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006861 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006862 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006863 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006864 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02006865 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006866 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006867 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006868 if (!starts_with_colon)
6869 {
6870 emsg(_(e_colon_required_before_a_range));
6871 goto erret;
6872 }
6873 if (ends_excmd2(line, ea.cmd))
6874 {
6875 // A range without a command: jump to the line.
6876 // TODO: compile to a more efficient command, possibly
6877 // calling parse_cmd_address().
6878 ea.cmdidx = CMD_SIZE;
6879 line = compile_exec(line, &ea, &cctx);
6880 goto nextline;
6881 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006882 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006883 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006884 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006885 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006886 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006887
6888 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6889 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006890 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006891 {
6892 line += STRLEN(line);
6893 continue;
6894 }
6895
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006896 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006897 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006898 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006899 // CMD_let cannot happen, compile_assignment() above is used
6900 iemsg("Command from find_ex_command() not handled");
6901 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006902 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006903 }
6904
Bram Moolenaar3988f642020-08-27 22:43:03 +02006905 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006906 && ea.cmdidx != CMD_elseif
6907 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02006908 && ea.cmdidx != CMD_endif
6909 && ea.cmdidx != CMD_endfor
6910 && ea.cmdidx != CMD_endwhile
6911 && ea.cmdidx != CMD_catch
6912 && ea.cmdidx != CMD_finally
6913 && ea.cmdidx != CMD_endtry)
6914 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02006915 emsg(_(e_unreachable_code_after_return));
6916 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006917 }
6918
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006919 p = skipwhite(p);
6920 if (ea.cmdidx != CMD_SIZE
6921 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
6922 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02006923 if (ea.cmdidx >= 0)
6924 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006925 if ((ea.argt & EX_BANG) && *p == '!')
6926 {
6927 ea.forceit = TRUE;
6928 p = skipwhite(p + 1);
6929 }
6930 }
6931
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006932 switch (ea.cmdidx)
6933 {
6934 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006935 ea.arg = p;
6936 line = compile_nested_function(&ea, &cctx);
6937 break;
6938
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006939 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006940 // TODO: should we allow this, e.g. to declare a global
6941 // function?
6942 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006943 goto erret;
6944
6945 case CMD_return:
6946 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006947 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006948 break;
6949
6950 case CMD_let:
6951 case CMD_const:
6952 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006953 if (line == p)
6954 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006955 break;
6956
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006957 case CMD_unlet:
6958 case CMD_unlockvar:
6959 case CMD_lockvar:
6960 line = compile_unletlock(p, &ea, &cctx);
6961 break;
6962
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006963 case CMD_import:
6964 line = compile_import(p, &cctx);
6965 break;
6966
6967 case CMD_if:
6968 line = compile_if(p, &cctx);
6969 break;
6970 case CMD_elseif:
6971 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006972 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006973 break;
6974 case CMD_else:
6975 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006976 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006977 break;
6978 case CMD_endif:
6979 line = compile_endif(p, &cctx);
6980 break;
6981
6982 case CMD_while:
6983 line = compile_while(p, &cctx);
6984 break;
6985 case CMD_endwhile:
6986 line = compile_endwhile(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
6990 case CMD_for:
6991 line = compile_for(p, &cctx);
6992 break;
6993 case CMD_endfor:
6994 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006995 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006996 break;
6997 case CMD_continue:
6998 line = compile_continue(p, &cctx);
6999 break;
7000 case CMD_break:
7001 line = compile_break(p, &cctx);
7002 break;
7003
7004 case CMD_try:
7005 line = compile_try(p, &cctx);
7006 break;
7007 case CMD_catch:
7008 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007009 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007010 break;
7011 case CMD_finally:
7012 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007013 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007014 break;
7015 case CMD_endtry:
7016 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007017 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007018 break;
7019 case CMD_throw:
7020 line = compile_throw(p, &cctx);
7021 break;
7022
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007023 case CMD_eval:
7024 if (compile_expr0(&p, &cctx) == FAIL)
7025 goto erret;
7026
Bram Moolenaar3988f642020-08-27 22:43:03 +02007027 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007028 generate_instr_drop(&cctx, ISN_DROP, 1);
7029
7030 line = skipwhite(p);
7031 break;
7032
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007033 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007034 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007035 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007036 case CMD_echomsg:
7037 case CMD_echoerr:
7038 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007039 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007040
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007041 case CMD_put:
7042 ea.cmd = cmd;
7043 line = compile_put(p, &ea, &cctx);
7044 break;
7045
Bram Moolenaar3988f642020-08-27 22:43:03 +02007046 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007047
Bram Moolenaarae616492020-07-28 20:07:27 +02007048 case CMD_append:
7049 case CMD_change:
7050 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007051 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007052 case CMD_xit:
7053 not_in_vim9(&ea);
7054 goto erret;
7055
Bram Moolenaar002262f2020-07-08 17:47:57 +02007056 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007057 if (cctx.ctx_skip != SKIP_YES)
7058 {
7059 semsg(_(e_invalid_command_str), ea.cmd);
7060 goto erret;
7061 }
7062 // We don't check for a next command here.
7063 line = (char_u *)"";
7064 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007065
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007066 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007067 if (cctx.ctx_skip == SKIP_YES)
7068 {
7069 // We don't check for a next command here.
7070 line = (char_u *)"";
7071 }
7072 else
7073 {
7074 // Not recognized, execute with do_cmdline_cmd().
7075 ea.arg = p;
7076 line = compile_exec(line, &ea, &cctx);
7077 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007078 break;
7079 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007080nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007081 if (line == NULL)
7082 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007083 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007084
7085 if (cctx.ctx_type_stack.ga_len < 0)
7086 {
7087 iemsg("Type stack underflow");
7088 goto erret;
7089 }
7090 }
7091
7092 if (cctx.ctx_scope != NULL)
7093 {
7094 if (cctx.ctx_scope->se_type == IF_SCOPE)
7095 emsg(_(e_endif));
7096 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7097 emsg(_(e_endwhile));
7098 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7099 emsg(_(e_endfor));
7100 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007101 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007102 goto erret;
7103 }
7104
Bram Moolenaarefd88552020-06-18 20:50:10 +02007105 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007106 {
7107 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7108 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007109 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007110 goto erret;
7111 }
7112
7113 // Return zero if there is no return at the end.
7114 generate_PUSHNR(&cctx, 0);
7115 generate_instr(&cctx, ISN_RETURN);
7116 }
7117
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007118 {
7119 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7120 + ufunc->uf_dfunc_idx;
7121 dfunc->df_deleted = FALSE;
7122 dfunc->df_instr = instr->ga_data;
7123 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007124 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007125 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007126 if (cctx.ctx_outer_used)
7127 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007128 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007129 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007130
7131 ret = OK;
7132
7133erret:
7134 if (ret == FAIL)
7135 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007136 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007137 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7138 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007139
7140 for (idx = 0; idx < instr->ga_len; ++idx)
7141 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007142 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007143
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007144 // If using the last entry in the table and it was added above, we
7145 // might as well remove it.
7146 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007147 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007148 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007149 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007150 ufunc->uf_dfunc_idx = 0;
7151 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007152 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007153
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007154 while (cctx.ctx_scope != NULL)
7155 drop_scope(&cctx);
7156
Bram Moolenaar20431c92020-03-20 18:39:46 +01007157 // Don't execute this function body.
7158 ga_clear_strings(&ufunc->uf_lines);
7159
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007160 if (errormsg != NULL)
7161 emsg(errormsg);
7162 else if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007163 emsg(_(e_compile_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007164 }
7165
7166 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007167 if (do_estack_push)
7168 estack_pop();
7169
Bram Moolenaar20431c92020-03-20 18:39:46 +01007170 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007171 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007172 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007173 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007174}
7175
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007176 void
7177set_function_type(ufunc_T *ufunc)
7178{
7179 int varargs = ufunc->uf_va_name != NULL;
7180 int argcount = ufunc->uf_args.ga_len;
7181
7182 // Create a type for the function, with the return type and any
7183 // argument types.
7184 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7185 // The type is included in "tt_args".
7186 if (argcount > 0 || varargs)
7187 {
7188 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7189 argcount, &ufunc->uf_type_list);
7190 // Add argument types to the function type.
7191 if (func_type_add_arg_types(ufunc->uf_func_type,
7192 argcount + varargs,
7193 &ufunc->uf_type_list) == FAIL)
7194 return;
7195 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7196 ufunc->uf_func_type->tt_min_argcount =
7197 argcount - ufunc->uf_def_args.ga_len;
7198 if (ufunc->uf_arg_types == NULL)
7199 {
7200 int i;
7201
7202 // lambda does not have argument types.
7203 for (i = 0; i < argcount; ++i)
7204 ufunc->uf_func_type->tt_args[i] = &t_any;
7205 }
7206 else
7207 mch_memmove(ufunc->uf_func_type->tt_args,
7208 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7209 if (varargs)
7210 {
7211 ufunc->uf_func_type->tt_args[argcount] =
7212 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7213 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7214 }
7215 }
7216 else
7217 // No arguments, can use a predefined type.
7218 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7219 argcount, &ufunc->uf_type_list);
7220}
7221
7222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007223/*
7224 * Delete an instruction, free what it contains.
7225 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007226 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007227delete_instr(isn_T *isn)
7228{
7229 switch (isn->isn_type)
7230 {
7231 case ISN_EXEC:
7232 case ISN_LOADENV:
7233 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007234 case ISN_LOADB:
7235 case ISN_LOADW:
7236 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007237 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007238 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007239 case ISN_PUSHEXC:
7240 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007241 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007242 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007243 case ISN_STOREB:
7244 case ISN_STOREW:
7245 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007246 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007247 vim_free(isn->isn_arg.string);
7248 break;
7249
7250 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007251 case ISN_STORES:
7252 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007253 break;
7254
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007255 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007256 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007257 vim_free(isn->isn_arg.unlet.ul_name);
7258 break;
7259
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007260 case ISN_STOREOPT:
7261 vim_free(isn->isn_arg.storeopt.so_name);
7262 break;
7263
7264 case ISN_PUSHBLOB: // push blob isn_arg.blob
7265 blob_unref(isn->isn_arg.blob);
7266 break;
7267
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007268 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007269#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007270 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007271#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007272 break;
7273
7274 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007275#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007276 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007277#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007278 break;
7279
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007280 case ISN_UCALL:
7281 vim_free(isn->isn_arg.ufunc.cuf_name);
7282 break;
7283
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007284 case ISN_FUNCREF:
7285 {
7286 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7287 + isn->isn_arg.funcref.fr_func;
7288 func_ptr_unref(dfunc->df_ufunc);
7289 }
7290 break;
7291
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007292 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007293 {
7294 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7295 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7296
7297 if (ufunc != NULL)
7298 {
7299 // Clear uf_dfunc_idx so that the function is deleted.
7300 clear_def_function(ufunc);
7301 ufunc->uf_dfunc_idx = 0;
7302 func_ptr_unref(ufunc);
7303 }
7304
7305 vim_free(lambda);
7306 vim_free(isn->isn_arg.newfunc.nf_global);
7307 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007308 break;
7309
Bram Moolenaar5e654232020-09-16 15:22:00 +02007310 case ISN_CHECKTYPE:
7311 free_type(isn->isn_arg.type.ct_type);
7312 break;
7313
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007314 case ISN_2BOOL:
7315 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007316 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007317 case ISN_ADDBLOB:
7318 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007319 case ISN_ANYINDEX:
7320 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007321 case ISN_BCALL:
7322 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007323 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007324 case ISN_CHECKNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007325 case ISN_COMPAREANY:
7326 case ISN_COMPAREBLOB:
7327 case ISN_COMPAREBOOL:
7328 case ISN_COMPAREDICT:
7329 case ISN_COMPAREFLOAT:
7330 case ISN_COMPAREFUNC:
7331 case ISN_COMPARELIST:
7332 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007333 case ISN_COMPARESPECIAL:
7334 case ISN_COMPARESTRING:
7335 case ISN_CONCAT:
7336 case ISN_DCALL:
7337 case ISN_DROP:
7338 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007339 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007340 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007341 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007342 case ISN_EXECCONCAT:
7343 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007344 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007345 case ISN_GETITEM:
7346 case ISN_JUMP:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007347 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007348 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007349 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007350 case ISN_LOADBDICT:
7351 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007352 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007353 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007354 case ISN_LOADSCRIPT:
7355 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007356 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007357 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007358 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007359 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007360 case ISN_NEGATENR:
7361 case ISN_NEWDICT:
7362 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007363 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007364 case ISN_OPFLOAT:
7365 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007366 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007367 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007368 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007369 case ISN_PUSHF:
7370 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007371 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007372 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007373 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007374 case ISN_SHUFFLE:
7375 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007376 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007377 case ISN_STOREDICT:
7378 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007379 case ISN_STORENR:
7380 case ISN_STOREOUTER:
7381 case ISN_STOREREG:
7382 case ISN_STORESCRIPT:
7383 case ISN_STOREV:
7384 case ISN_STRINDEX:
7385 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007386 case ISN_THROW:
7387 case ISN_TRY:
7388 // nothing allocated
7389 break;
7390 }
7391}
7392
7393/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007394 * Free all instructions for "dfunc".
7395 */
7396 static void
7397delete_def_function_contents(dfunc_T *dfunc)
7398{
7399 int idx;
7400
7401 ga_clear(&dfunc->df_def_args_isn);
7402
7403 if (dfunc->df_instr != NULL)
7404 {
7405 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7406 delete_instr(dfunc->df_instr + idx);
7407 VIM_CLEAR(dfunc->df_instr);
7408 }
7409
7410 dfunc->df_deleted = TRUE;
7411}
7412
7413/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007414 * When a user function is deleted, clear the contents of any associated def
7415 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007416 */
7417 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007418clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007419{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007420 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007421 {
7422 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7423 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007424
Bram Moolenaar20431c92020-03-20 18:39:46 +01007425 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007426 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007427 }
7428}
7429
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007430/*
7431 * Used when a user function is about to be deleted: remove the pointer to it.
7432 * The entry in def_functions is then unused.
7433 */
7434 void
7435unlink_def_function(ufunc_T *ufunc)
7436{
7437 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7438
7439 dfunc->df_ufunc = NULL;
7440}
7441
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007442#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007443/*
7444 * Free all functions defined with ":def".
7445 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007446 void
7447free_def_functions(void)
7448{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007449 int idx;
7450
7451 for (idx = 0; idx < def_functions.ga_len; ++idx)
7452 {
7453 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7454
7455 delete_def_function_contents(dfunc);
7456 }
7457
7458 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007459}
7460#endif
7461
7462
7463#endif // FEAT_EVAL