blob: 6ed166d47927fc9b35d3a3961494baae35255a41 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
111 int lv_from_outer; // when TRUE using ctx_outer scope
112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200129 int ctx_has_closure; // set to one if a closures was created in
130 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144};
145
Bram Moolenaar20431c92020-03-20 18:39:46 +0100146static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100147
148/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200149 * Lookup variable "name" in the local scope and return it.
150 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200152 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153lookup_local(char_u *name, size_t len, cctx_T *cctx)
154{
155 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200156 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100158 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200159 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200160
161 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
163 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200164 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100165 if (STRNCMP(name, lvar->lv_name, len) == 0
166 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167 {
168 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200169 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200170 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200172
173 // Find local in outer function scope.
174 if (cctx->ctx_outer != NULL)
175 {
176 lvar = lookup_local(name, len, cctx->ctx_outer);
177 if (lvar != NULL)
178 {
179 // TODO: are there situations we should not mark the outer scope as
180 // used?
181 cctx->ctx_outer_used = TRUE;
182 lvar->lv_from_outer = TRUE;
183 return lvar;
184 }
185 }
186
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200187 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100188}
189
190/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200191 * Lookup an argument in the current function and an enclosing function.
192 * Returns the argument index in "idxp"
193 * Returns the argument type in "type"
194 * Sets "gen_load_outer" to TRUE if found in outer scope.
195 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196 */
197 static int
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200198lookup_arg(
199 char_u *name,
200 size_t len,
201 int *idxp,
202 type_T **type,
203 int *gen_load_outer,
204 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100205{
206 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200207 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100209 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200210 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
212 {
213 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
214
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200215 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
216 {
217 if (idxp != NULL)
218 {
219 // Arguments are located above the frame pointer. One further
220 // if there is a vararg argument
221 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
222 + STACK_FRAME_SIZE)
223 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
224
225 if (cctx->ctx_ufunc->uf_arg_types != NULL)
226 *type = cctx->ctx_ufunc->uf_arg_types[idx];
227 else
228 *type = &t_any;
229 }
230 return OK;
231 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100232 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100233
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200234 va_name = cctx->ctx_ufunc->uf_va_name;
235 if (va_name != NULL
236 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
237 {
238 if (idxp != NULL)
239 {
240 // varargs is always the last argument
241 *idxp = -STACK_FRAME_SIZE - 1;
242 *type = cctx->ctx_ufunc->uf_va_type;
243 }
244 return OK;
245 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200247 if (cctx->ctx_outer != NULL)
248 {
249 // Lookup the name for an argument of the outer function.
250 if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
251 == OK)
252 {
253 *gen_load_outer = TRUE;
254 return OK;
255 }
256 }
257
258 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100259}
260
261/*
Bram Moolenaar84367732020-08-23 15:21:55 +0200262 * Returnd TRUE if the script context is Vim9 script.
263 */
264 static int
265script_is_vim9()
266{
267 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
268}
269
270/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 * Lookup a variable in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200272 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
273 * without "s:".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274 * Returns OK or FAIL.
275 */
276 static int
Bram Moolenaar53900992020-08-22 19:02:02 +0200277lookup_script(char_u *name, size_t len, int vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278{
279 int cc;
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 Moolenaar148ce7a2020-09-23 21:57:23 +02001276 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277
1278 if (ga_grow(stack, 1) == FAIL)
1279 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001280 ((type_T **)stack->ga_data)[stack->ga_len] =
1281 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001282 ++stack->ga_len;
1283
1284 return OK;
1285}
1286
1287/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001288 * Generate an ISN_NEWFUNC instruction.
1289 */
1290 static int
1291generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1292{
1293 isn_T *isn;
1294 char_u *name;
1295
1296 RETURN_OK_IF_SKIP(cctx);
1297 name = vim_strsave(lambda_name);
1298 if (name == NULL)
1299 return FAIL;
1300 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1301 return FAIL;
1302 isn->isn_arg.newfunc.nf_lambda = name;
1303 isn->isn_arg.newfunc.nf_global = func_name;
1304
1305 return OK;
1306}
1307
1308/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309 * Generate an ISN_JUMP instruction.
1310 */
1311 static int
1312generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1313{
1314 isn_T *isn;
1315 garray_T *stack = &cctx->ctx_type_stack;
1316
Bram Moolenaar080457c2020-03-03 21:53:32 +01001317 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1319 return FAIL;
1320 isn->isn_arg.jump.jump_when = when;
1321 isn->isn_arg.jump.jump_where = where;
1322
1323 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1324 --stack->ga_len;
1325
1326 return OK;
1327}
1328
1329 static int
1330generate_FOR(cctx_T *cctx, int loop_idx)
1331{
1332 isn_T *isn;
1333 garray_T *stack = &cctx->ctx_type_stack;
1334
Bram Moolenaar080457c2020-03-03 21:53:32 +01001335 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1337 return FAIL;
1338 isn->isn_arg.forloop.for_idx = loop_idx;
1339
1340 if (ga_grow(stack, 1) == FAIL)
1341 return FAIL;
1342 // type doesn't matter, will be stored next
1343 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1344 ++stack->ga_len;
1345
1346 return OK;
1347}
1348
1349/*
1350 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001351 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 * Return FAIL if the number of arguments is wrong.
1353 */
1354 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001355generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356{
1357 isn_T *isn;
1358 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001359 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001360 type_T *argtypes[MAX_FUNC_ARGS];
1361 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362
Bram Moolenaar080457c2020-03-03 21:53:32 +01001363 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001364 argoff = check_internal_func(func_idx, argcount);
1365 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001366 return FAIL;
1367
Bram Moolenaar389df252020-07-09 21:20:47 +02001368 if (method_call && argoff > 1)
1369 {
1370 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1371 return FAIL;
1372 isn->isn_arg.shuffle.shfl_item = argcount;
1373 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1374 }
1375
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1377 return FAIL;
1378 isn->isn_arg.bfunc.cbf_idx = func_idx;
1379 isn->isn_arg.bfunc.cbf_argcount = argcount;
1380
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001381 for (i = 0; i < argcount; ++i)
1382 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1383
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001384 stack->ga_len -= argcount; // drop the arguments
1385 if (ga_grow(stack, 1) == FAIL)
1386 return FAIL;
1387 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001388 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001389 ++stack->ga_len; // add return value
1390
1391 return OK;
1392}
1393
1394/*
1395 * Generate an ISN_DCALL or ISN_UCALL instruction.
1396 * Return FAIL if the number of arguments is wrong.
1397 */
1398 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001399generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400{
1401 isn_T *isn;
1402 garray_T *stack = &cctx->ctx_type_stack;
1403 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001404 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001405
Bram Moolenaar080457c2020-03-03 21:53:32 +01001406 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407 if (argcount > regular_args && !has_varargs(ufunc))
1408 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001409 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001410 return FAIL;
1411 }
1412 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1413 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001414 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001415 return FAIL;
1416 }
1417
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001418 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001419 {
1420 int i;
1421
1422 for (i = 0; i < argcount; ++i)
1423 {
1424 type_T *expected;
1425 type_T *actual;
1426
1427 if (i < regular_args)
1428 {
1429 if (ufunc->uf_arg_types == NULL)
1430 continue;
1431 expected = ufunc->uf_arg_types[i];
1432 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001433 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1434 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001435 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001436 else
1437 expected = ufunc->uf_va_type->tt_member;
1438 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001439 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001440 {
1441 arg_type_mismatch(expected, actual, i + 1);
1442 return FAIL;
1443 }
1444 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001445 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001446 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1447 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001448 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001449 }
1450
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001451 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001452 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001453 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001455 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001456 {
1457 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1458 isn->isn_arg.dfunc.cdf_argcount = argcount;
1459 }
1460 else
1461 {
1462 // A user function may be deleted and redefined later, can't use the
1463 // ufunc pointer, need to look it up again at runtime.
1464 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1465 isn->isn_arg.ufunc.cuf_argcount = argcount;
1466 }
1467
1468 stack->ga_len -= argcount; // drop the arguments
1469 if (ga_grow(stack, 1) == FAIL)
1470 return FAIL;
1471 // add return value
1472 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1473 ++stack->ga_len;
1474
1475 return OK;
1476}
1477
1478/*
1479 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1480 */
1481 static int
1482generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1483{
1484 isn_T *isn;
1485 garray_T *stack = &cctx->ctx_type_stack;
1486
Bram Moolenaar080457c2020-03-03 21:53:32 +01001487 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1489 return FAIL;
1490 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1491 isn->isn_arg.ufunc.cuf_argcount = argcount;
1492
1493 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001494 if (ga_grow(stack, 1) == FAIL)
1495 return FAIL;
1496 // add return value
1497 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1498 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001499
1500 return OK;
1501}
1502
1503/*
1504 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001505 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001506 */
1507 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001508generate_PCALL(
1509 cctx_T *cctx,
1510 int argcount,
1511 char_u *name,
1512 type_T *type,
1513 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001514{
1515 isn_T *isn;
1516 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001517 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518
Bram Moolenaar080457c2020-03-03 21:53:32 +01001519 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001520
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001521 if (type->tt_type == VAR_ANY)
1522 ret_type = &t_any;
1523 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001524 {
1525 if (type->tt_argcount != -1)
1526 {
1527 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1528
1529 if (argcount < type->tt_min_argcount - varargs)
1530 {
1531 semsg(_(e_toofewarg), "[reference]");
1532 return FAIL;
1533 }
1534 if (!varargs && argcount > type->tt_argcount)
1535 {
1536 semsg(_(e_toomanyarg), "[reference]");
1537 return FAIL;
1538 }
1539 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001540 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001541 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001542 else
1543 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001544 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001545 return FAIL;
1546 }
1547
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001548 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1549 return FAIL;
1550 isn->isn_arg.pfunc.cpf_top = at_top;
1551 isn->isn_arg.pfunc.cpf_argcount = argcount;
1552
1553 stack->ga_len -= argcount; // drop the arguments
1554
1555 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001556 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001558 // If partial is above the arguments it must be cleared and replaced with
1559 // the return value.
1560 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1561 return FAIL;
1562
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 return OK;
1564}
1565
1566/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001567 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568 */
1569 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001570generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001571{
1572 isn_T *isn;
1573 garray_T *stack = &cctx->ctx_type_stack;
1574 type_T *type;
1575
Bram Moolenaar080457c2020-03-03 21:53:32 +01001576 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001577 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001578 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001579 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001580
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001581 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001582 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001583 if (type->tt_type != VAR_DICT && type != &t_any)
1584 {
1585 emsg(_(e_dictreq));
1586 return FAIL;
1587 }
1588 // change dict type to dict member type
1589 if (type->tt_type == VAR_DICT)
1590 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001591
1592 return OK;
1593}
1594
1595/*
1596 * Generate an ISN_ECHO instruction.
1597 */
1598 static int
1599generate_ECHO(cctx_T *cctx, int with_white, int count)
1600{
1601 isn_T *isn;
1602
Bram Moolenaar080457c2020-03-03 21:53:32 +01001603 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1605 return FAIL;
1606 isn->isn_arg.echo.echo_with_white = with_white;
1607 isn->isn_arg.echo.echo_count = count;
1608
1609 return OK;
1610}
1611
Bram Moolenaarad39c092020-02-26 18:23:43 +01001612/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001613 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001614 */
1615 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001616generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001617{
1618 isn_T *isn;
1619
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001620 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001621 return FAIL;
1622 isn->isn_arg.number = count;
1623
1624 return OK;
1625}
1626
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001627/*
1628 * Generate an ISN_PUT instruction.
1629 */
1630 static int
1631generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1632{
1633 isn_T *isn;
1634
1635 RETURN_OK_IF_SKIP(cctx);
1636 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1637 return FAIL;
1638 isn->isn_arg.put.put_regname = regname;
1639 isn->isn_arg.put.put_lnum = lnum;
1640 return OK;
1641}
1642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001643 static int
1644generate_EXEC(cctx_T *cctx, char_u *line)
1645{
1646 isn_T *isn;
1647
Bram Moolenaar080457c2020-03-03 21:53:32 +01001648 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001649 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1650 return FAIL;
1651 isn->isn_arg.string = vim_strsave(line);
1652 return OK;
1653}
1654
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001655 static int
1656generate_EXECCONCAT(cctx_T *cctx, int count)
1657{
1658 isn_T *isn;
1659
1660 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1661 return FAIL;
1662 isn->isn_arg.number = count;
1663 return OK;
1664}
1665
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001666/*
1667 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001668 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001669 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001670 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1672{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001673 lvar_T *lvar;
1674
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001675 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001676 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001677 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001678 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001679 }
1680
1681 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001682 return NULL;
1683 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001685 // Every local variable uses the next entry on the stack. We could re-use
1686 // the last ones when leaving a scope, but then variables used in a closure
1687 // might get overwritten. To keep things simple do not re-use stack
1688 // entries. This is less efficient, but memory is cheap these days.
1689 lvar->lv_idx = cctx->ctx_locals_count++;
1690
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001691 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001692 lvar->lv_const = isConst;
1693 lvar->lv_type = type;
1694
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001695 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001696}
1697
1698/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001699 * Remove local variables above "new_top".
1700 */
1701 static void
1702unwind_locals(cctx_T *cctx, int new_top)
1703{
1704 if (cctx->ctx_locals.ga_len > new_top)
1705 {
1706 int idx;
1707 lvar_T *lvar;
1708
1709 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1710 {
1711 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1712 vim_free(lvar->lv_name);
1713 }
1714 }
1715 cctx->ctx_locals.ga_len = new_top;
1716}
1717
1718/*
1719 * Free all local variables.
1720 */
1721 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001722free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001723{
1724 unwind_locals(cctx, 0);
1725 ga_clear(&cctx->ctx_locals);
1726}
1727
1728/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001729 * Find "name" in script-local items of script "sid".
1730 * Returns the index in "sn_var_vals" if found.
1731 * If found but not in "sn_var_vals" returns -1.
1732 * If not found returns -2.
1733 */
1734 int
1735get_script_item_idx(int sid, char_u *name, int check_writable)
1736{
1737 hashtab_T *ht;
1738 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001739 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001740 int idx;
1741
1742 // First look the name up in the hashtable.
Bram Moolenaare3d46852020-08-29 13:39:17 +02001743 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001744 return -1;
1745 ht = &SCRIPT_VARS(sid);
1746 di = find_var_in_ht(ht, 0, name, TRUE);
1747 if (di == NULL)
1748 return -2;
1749
1750 // Now find the svar_T index in sn_var_vals.
1751 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1752 {
1753 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1754
1755 if (sv->sv_tv == &di->di_tv)
1756 {
1757 if (check_writable && sv->sv_const)
1758 semsg(_(e_readonlyvar), name);
1759 return idx;
1760 }
1761 }
1762 return -1;
1763}
1764
1765/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001766 * Find "name" in imported items of the current script or in "cctx" if not
1767 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768 */
1769 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001770find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001772 int idx;
1773
Bram Moolenaare3d46852020-08-29 13:39:17 +02001774 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001775 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776 if (cctx != NULL)
1777 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1778 {
1779 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1780 + idx;
1781
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001782 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1783 : STRLEN(import->imp_name) == len
1784 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785 return import;
1786 }
1787
Bram Moolenaarefa94442020-08-08 22:16:00 +02001788 return find_imported_in_script(name, len, current_sctx.sc_sid);
1789}
1790
1791 imported_T *
1792find_imported_in_script(char_u *name, size_t len, int sid)
1793{
Bram Moolenaare3d46852020-08-29 13:39:17 +02001794 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001795 int idx;
1796
Bram Moolenaare3d46852020-08-29 13:39:17 +02001797 if (!SCRIPT_ID_VALID(sid))
1798 return NULL;
1799 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001800 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1801 {
1802 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1803
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001804 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1805 : STRLEN(import->imp_name) == len
1806 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807 return import;
1808 }
1809 return NULL;
1810}
1811
1812/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001813 * Free all imported variables.
1814 */
1815 static void
1816free_imported(cctx_T *cctx)
1817{
1818 int idx;
1819
1820 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1821 {
1822 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1823
1824 vim_free(import->imp_name);
1825 }
1826 ga_clear(&cctx->ctx_imports);
1827}
1828
1829/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02001830 * Return TRUE if "p" points at a "#" but not at "#{".
1831 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001832 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001833vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001834{
1835 return p[0] == '#' && p[1] != '{';
1836}
1837
1838/*
1839 * Return a pointer to the next line that isn't empty or only contains a
1840 * comment. Skips over white space.
1841 * Returns NULL if there is none.
1842 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001843 char_u *
1844peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001845{
1846 int lnum = cctx->ctx_lnum;
1847
1848 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
1849 {
1850 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001851 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001852
Bram Moolenaarba60cc42020-08-12 19:15:33 +02001853 // ignore NULLs inserted for continuation lines
1854 if (line != NULL)
1855 {
1856 p = skipwhite(line);
1857 if (*p != NUL && !vim9_comment_start(p))
1858 return p;
1859 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02001860 }
1861 return NULL;
1862}
1863
1864/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001865 * Called when checking for a following operator at "arg". When the rest of
1866 * the line is empty or only a comment, peek the next line. If there is a next
1867 * line return a pointer to it and set "nextp".
1868 * Otherwise skip over white space.
1869 */
1870 static char_u *
1871may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
1872{
1873 char_u *p = skipwhite(arg);
1874
1875 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001876 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001877 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001878 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001879 if (*nextp != NULL)
1880 return *nextp;
1881 }
1882 return p;
1883}
1884
1885/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02001886 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02001887 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02001888 * Returns NULL when at the end.
1889 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001890 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02001891next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02001892{
Bram Moolenaar7a092242020-04-16 22:10:49 +02001893 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001894
1895 do
1896 {
1897 ++cctx->ctx_lnum;
1898 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02001899 {
1900 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001901 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02001902 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02001903 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02001904 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001905 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001906 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02001907 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02001908 return line;
1909}
1910
1911/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001912 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02001913 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001914 * Return FAIL if beyond the last line, "*arg" is unmodified then.
1915 */
1916 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02001917may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001918{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001919 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001920 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02001921 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001922
1923 if (next == NULL)
1924 return FAIL;
1925 *arg = skipwhite(next);
1926 }
1927 return OK;
1928}
1929
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001930/*
1931 * Idem, and give an error when failed.
1932 */
1933 static int
1934may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
1935{
1936 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1937 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001938 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001939 return FAIL;
1940 }
1941 return OK;
1942}
1943
1944
Bram Moolenaara5565e42020-05-09 15:44:01 +02001945// Structure passed between the compile_expr* functions to keep track of
1946// constants that have been parsed but for which no code was produced yet. If
1947// possible expressions on these constants are applied at compile time. If
1948// that is not possible, the code to push the constants needs to be generated
1949// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02001950// Using 50 should be more than enough of 5 levels of ().
1951#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02001952typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02001953 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02001954 int pp_used; // active entries in pp_tv[]
1955} ppconst_T;
1956
Bram Moolenaar1c747212020-05-09 18:28:34 +02001957static int compile_expr0(char_u **arg, cctx_T *cctx);
1958static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1959
Bram Moolenaara5565e42020-05-09 15:44:01 +02001960/*
1961 * Generate a PUSH instruction for "tv".
1962 * "tv" will be consumed or cleared.
1963 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
1964 */
1965 static int
1966generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
1967{
1968 if (tv != NULL)
1969 {
1970 switch (tv->v_type)
1971 {
1972 case VAR_UNKNOWN:
1973 break;
1974 case VAR_BOOL:
1975 generate_PUSHBOOL(cctx, tv->vval.v_number);
1976 break;
1977 case VAR_SPECIAL:
1978 generate_PUSHSPEC(cctx, tv->vval.v_number);
1979 break;
1980 case VAR_NUMBER:
1981 generate_PUSHNR(cctx, tv->vval.v_number);
1982 break;
1983#ifdef FEAT_FLOAT
1984 case VAR_FLOAT:
1985 generate_PUSHF(cctx, tv->vval.v_float);
1986 break;
1987#endif
1988 case VAR_BLOB:
1989 generate_PUSHBLOB(cctx, tv->vval.v_blob);
1990 tv->vval.v_blob = NULL;
1991 break;
1992 case VAR_STRING:
1993 generate_PUSHS(cctx, tv->vval.v_string);
1994 tv->vval.v_string = NULL;
1995 break;
1996 default:
1997 iemsg("constant type not supported");
1998 clear_tv(tv);
1999 return FAIL;
2000 }
2001 tv->v_type = VAR_UNKNOWN;
2002 }
2003 return OK;
2004}
2005
2006/*
2007 * Generate code for any ppconst entries.
2008 */
2009 static int
2010generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2011{
2012 int i;
2013 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002014 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002015
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002016 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002017 for (i = 0; i < ppconst->pp_used; ++i)
2018 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2019 ret = FAIL;
2020 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002021 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002022 return ret;
2023}
2024
2025/*
2026 * Clear ppconst constants. Used when failing.
2027 */
2028 static void
2029clear_ppconst(ppconst_T *ppconst)
2030{
2031 int i;
2032
2033 for (i = 0; i < ppconst->pp_used; ++i)
2034 clear_tv(&ppconst->pp_tv[i]);
2035 ppconst->pp_used = 0;
2036}
2037
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002038/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002039 * Generate an instruction to load script-local variable "name", without the
2040 * leading "s:".
2041 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042 */
2043 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002044compile_load_scriptvar(
2045 cctx_T *cctx,
2046 char_u *name, // variable NUL terminated
2047 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002048 char_u **end, // end of variable
2049 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002050{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002051 scriptitem_T *si;
2052 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002053 imported_T *import;
2054
Bram Moolenaare3d46852020-08-29 13:39:17 +02002055 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2056 return FAIL;
2057 si = SCRIPT_ITEM(current_sctx.sc_sid);
2058 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002059 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002060 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002061 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002062 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2063 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064 }
2065 if (idx >= 0)
2066 {
2067 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2068
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002069 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070 current_sctx.sc_sid, idx, sv->sv_type);
2071 return OK;
2072 }
2073
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002074 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002075 if (import != NULL)
2076 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002077 if (import->imp_all)
2078 {
2079 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002080 char_u *exp_name;
2081 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002082 ufunc_T *ufunc;
2083 type_T *type;
2084
2085 // Used "import * as Name", need to lookup the member.
2086 if (*p != '.')
2087 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002088 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002089 return FAIL;
2090 }
2091 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002092 if (VIM_ISWHITE(*p))
2093 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002094 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002095 return FAIL;
2096 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002097
Bram Moolenaar1c991142020-07-04 13:15:31 +02002098 // isolate one name
2099 exp_name = p;
2100 while (eval_isnamec(*p))
2101 ++p;
2102 cc = *p;
2103 *p = NUL;
2104
2105 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2106 *p = cc;
2107 p = skipwhite(p);
2108
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002109 // TODO: what if it is a function?
2110 if (idx < 0)
2111 return FAIL;
2112 *end = p;
2113
2114 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2115 import->imp_sid,
2116 idx,
2117 type);
2118 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002119 else if (import->imp_funcname != NULL)
2120 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002121 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002122 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2123 import->imp_sid,
2124 import->imp_var_vals_idx,
2125 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002126 return OK;
2127 }
2128
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002129 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002130 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131 return FAIL;
2132}
2133
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002134 static int
2135generate_funcref(cctx_T *cctx, char_u *name)
2136{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002137 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002138
2139 if (ufunc == NULL)
2140 return FAIL;
2141
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002142 // Need to compile any default values to get the argument types.
2143 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2144 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2145 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002146 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002147}
2148
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002149/*
2150 * Compile a variable name into a load instruction.
2151 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002152 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002153 * When "error" is FALSE do not give an error when not found.
2154 */
2155 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002156compile_load(
2157 char_u **arg,
2158 char_u *end_arg,
2159 cctx_T *cctx,
2160 int is_expr,
2161 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162{
2163 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002164 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002165 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002166 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002167 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002168
2169 if (*(*arg + 1) == ':')
2170 {
2171 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002172 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002173 {
2174 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002176 switch (**arg)
2177 {
2178 case 'g': isn_type = ISN_LOADGDICT; break;
2179 case 'w': isn_type = ISN_LOADWDICT; break;
2180 case 't': isn_type = ISN_LOADTDICT; break;
2181 case 'b': isn_type = ISN_LOADBDICT; break;
2182 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002183 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002184 goto theend;
2185 }
2186 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2187 goto theend;
2188 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002189 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002190 else
2191 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002192 isntype_T isn_type = ISN_DROP;
2193
2194 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2195 if (name == NULL)
2196 return FAIL;
2197
2198 switch (**arg)
2199 {
2200 case 'v': res = generate_LOADV(cctx, name, error);
2201 break;
2202 case 's': res = compile_load_scriptvar(cctx, name,
2203 NULL, NULL, error);
2204 break;
2205 case 'g': isn_type = ISN_LOADG; break;
2206 case 'w': isn_type = ISN_LOADW; break;
2207 case 't': isn_type = ISN_LOADT; break;
2208 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002209 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002210 goto theend;
2211 }
2212 if (isn_type != ISN_DROP)
2213 {
2214 // Global, Buffer-local, Window-local and Tabpage-local
2215 // variables can be defined later, thus we don't check if it
2216 // exists, give error at runtime.
2217 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2218 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002219 }
2220 }
2221 else
2222 {
2223 size_t len = end - *arg;
2224 int idx;
2225 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002226 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002227
2228 name = vim_strnsave(*arg, end - *arg);
2229 if (name == NULL)
2230 return FAIL;
2231
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002232 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002233 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002234 if (!gen_load_outer)
2235 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002236 }
2237 else
2238 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002239 lvar_T *lvar = lookup_local(*arg, len, cctx);
2240
2241 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002242 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002243 type = lvar->lv_type;
2244 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002245 if (lvar->lv_from_outer)
2246 gen_load_outer = TRUE;
2247 else
2248 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002249 }
2250 else
2251 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002252 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002253 // already exists in a Vim9 script or when it's imported.
2254 if (lookup_script(*arg, len, TRUE) == OK
2255 || find_imported(name, 0, cctx) != NULL)
2256 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002257
Bram Moolenaar0f769812020-09-12 18:32:34 +02002258 // When evaluating an expression and the name starts with an
2259 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002260 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002261 if (res == FAIL && is_expr
2262 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002263 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264 }
2265 }
2266 if (gen_load)
2267 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002268 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002269 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002270 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002271 cctx->ctx_outer_used = TRUE;
2272 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002273 }
2274
2275 *arg = end;
2276
2277theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002278 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002279 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002280 vim_free(name);
2281 return res;
2282}
2283
2284/*
2285 * Compile the argument expressions.
2286 * "arg" points to just after the "(" and is advanced to after the ")"
2287 */
2288 static int
2289compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2290{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002291 char_u *p = *arg;
2292 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002293 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002294
Bram Moolenaare6085c52020-04-12 20:19:16 +02002295 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002296 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002297 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2298 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002299 if (*p == ')')
2300 {
2301 *arg = p + 1;
2302 return OK;
2303 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002304 if (must_end)
2305 {
2306 semsg(_(e_missing_comma_before_argument_str), p);
2307 return FAIL;
2308 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002309
Bram Moolenaara5565e42020-05-09 15:44:01 +02002310 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311 return FAIL;
2312 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002313
2314 if (*p != ',' && *skipwhite(p) == ',')
2315 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002316 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002317 p = skipwhite(p);
2318 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002319 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002320 {
2321 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002322 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002323 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002324 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002325 else
2326 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002327 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002328 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002329 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002330failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002331 emsg(_(e_missing_close));
2332 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333}
2334
2335/*
2336 * Compile a function call: name(arg1, arg2)
2337 * "arg" points to "name", "arg + varlen" to the "(".
2338 * "argcount_init" is 1 for "value->method()"
2339 * Instructions:
2340 * EVAL arg1
2341 * EVAL arg2
2342 * BCALL / DCALL / UCALL
2343 */
2344 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002345compile_call(
2346 char_u **arg,
2347 size_t varlen,
2348 cctx_T *cctx,
2349 ppconst_T *ppconst,
2350 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002351{
2352 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002353 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002354 int argcount = argcount_init;
2355 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002356 char_u fname_buf[FLEN_FIXED + 1];
2357 char_u *tofree = NULL;
2358 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002359 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002360 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002361 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002362
Bram Moolenaara5565e42020-05-09 15:44:01 +02002363 // we can evaluate "has('name')" at compile time
2364 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2365 {
2366 char_u *s = skipwhite(*arg + varlen + 1);
2367 typval_T argvars[2];
2368
2369 argvars[0].v_type = VAR_UNKNOWN;
2370 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002371 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002372 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002373 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002374 s = skipwhite(s);
2375 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2376 {
2377 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2378
2379 *arg = s + 1;
2380 argvars[1].v_type = VAR_UNKNOWN;
2381 tv->v_type = VAR_NUMBER;
2382 tv->vval.v_number = 0;
2383 f_has(argvars, tv);
2384 clear_tv(&argvars[0]);
2385 ++ppconst->pp_used;
2386 return OK;
2387 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002388 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002389 }
2390
2391 if (generate_ppconst(cctx, ppconst) == FAIL)
2392 return FAIL;
2393
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394 if (varlen >= sizeof(namebuf))
2395 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002396 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397 return FAIL;
2398 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002399 vim_strncpy(namebuf, *arg, varlen);
2400 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401
2402 *arg = skipwhite(*arg + varlen + 1);
2403 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002404 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405
Bram Moolenaara1773442020-08-12 15:21:22 +02002406 is_autoload = vim_strchr(name, '#') != NULL;
2407 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002408 {
2409 int idx;
2410
2411 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002412 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002414 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002415 else
2416 semsg(_(e_unknownfunc), namebuf);
2417 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002418 }
2419
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002420 // If we can find the function by name generate the right call.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002421 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002422 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar0f769812020-09-12 18:32:34 +02002423 if (ufunc != NULL && !func_is_global(ufunc))
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002424 {
2425 res = generate_CALL(cctx, ufunc, argcount);
2426 goto theend;
2427 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428
2429 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002430 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002431 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002433 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002434 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002435 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002436 garray_T *stack = &cctx->ctx_type_stack;
2437 type_T *type;
2438
2439 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2440 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002441 goto theend;
2442 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443
Bram Moolenaar0f769812020-09-12 18:32:34 +02002444 // If we can find a global function by name generate the right call.
2445 if (ufunc != NULL)
2446 {
2447 res = generate_CALL(cctx, ufunc, argcount);
2448 goto theend;
2449 }
2450
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002451 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002452 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002453 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002454 res = generate_UCALL(cctx, name, argcount);
2455 else
2456 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002457
2458theend:
2459 vim_free(tofree);
2460 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002461}
2462
2463// like NAMESPACE_CHAR but with 'a' and 'l'.
2464#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2465
2466/*
2467 * Find the end of a variable or function name. Unlike find_name_end() this
2468 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002469 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470 * Return a pointer to just after the name. Equal to "arg" if there is no
2471 * valid name.
2472 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002473 static char_u *
2474to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475{
2476 char_u *p;
2477
2478 // Quick check for valid starting character.
2479 if (!eval_isnamec1(*arg))
2480 return arg;
2481
2482 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2483 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2484 // and can be used in slice "[n:]".
2485 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002486 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2488 break;
2489 return p;
2490}
2491
2492/*
2493 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002494 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 */
2496 char_u *
2497to_name_const_end(char_u *arg)
2498{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002499 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002500 typval_T rettv;
2501
2502 if (p == arg && *arg == '[')
2503 {
2504
2505 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002506 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507 p = arg;
2508 }
2509 else if (p == arg && *arg == '#' && arg[1] == '{')
2510 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002511 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002513 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 p = arg;
2515 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516
2517 return p;
2518}
2519
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520/*
2521 * parse a list: [expr, expr]
2522 * "*arg" points to the '['.
2523 */
2524 static int
2525compile_list(char_u **arg, cctx_T *cctx)
2526{
2527 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002528 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 int count = 0;
2530
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002531 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002533 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002534 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002535 semsg(_(e_list_end), *arg);
2536 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002537 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002538 if (*p == ',')
2539 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002540 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002541 return FAIL;
2542 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002543 if (*p == ']')
2544 {
2545 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002546 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002547 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002548 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 break;
2550 ++count;
2551 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002552 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002554 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2555 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002556 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002557 return FAIL;
2558 }
2559 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002560 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561 p = skipwhite(p);
2562 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002563 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564
2565 generate_NEWLIST(cctx, count);
2566 return OK;
2567}
2568
2569/*
2570 * parse a lambda: {arg, arg -> expr}
2571 * "*arg" points to the '{'.
2572 */
2573 static int
2574compile_lambda(char_u **arg, cctx_T *cctx)
2575{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002576 typval_T rettv;
2577 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002578 evalarg_T evalarg;
2579
2580 CLEAR_FIELD(evalarg);
2581 evalarg.eval_flags = EVAL_EVALUATE;
2582 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002583
2584 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002585 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002587
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002589 ++ufunc->uf_refcount;
2590 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002591 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002592
2593 // The function will have one line: "return {expr}".
2594 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002595 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002597 clear_evalarg(&evalarg, NULL);
2598
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002599 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002600 {
2601 // The return type will now be known.
2602 set_function_type(ufunc);
2603
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002604 // The function reference count will be 1. When the ISN_FUNCREF
2605 // instruction is deleted the reference count is decremented and the
2606 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002607 return generate_FUNCREF(cctx, ufunc);
2608 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002609
2610 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611 return FAIL;
2612}
2613
2614/*
2615 * Compile a lamda call: expr->{lambda}(args)
2616 * "arg" points to the "{".
2617 */
2618 static int
2619compile_lambda_call(char_u **arg, cctx_T *cctx)
2620{
2621 ufunc_T *ufunc;
2622 typval_T rettv;
2623 int argcount = 1;
2624 int ret = FAIL;
2625
2626 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002627 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628 return FAIL;
2629
2630 if (**arg != '(')
2631 {
2632 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002633 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 else
2635 semsg(_(e_missing_paren), "lambda");
2636 clear_tv(&rettv);
2637 return FAIL;
2638 }
2639
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640 ufunc = rettv.vval.v_partial->pt_func;
2641 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002642 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002643 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002644
Bram Moolenaara05e5242020-09-19 18:19:19 +02002645 // The function will have one line: "return {expr}". Compile it into
2646 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002647 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648
2649 // compile the arguments
2650 *arg = skipwhite(*arg + 1);
2651 if (compile_arguments(arg, cctx, &argcount) == OK)
2652 // call the compiled function
2653 ret = generate_CALL(cctx, ufunc, argcount);
2654
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002655 if (ret == FAIL)
2656 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657 return ret;
2658}
2659
2660/*
2661 * parse a dict: {'key': val} or #{key: val}
2662 * "*arg" points to the '{'.
2663 */
2664 static int
2665compile_dict(char_u **arg, cctx_T *cctx, int literal)
2666{
2667 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002668 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002669 int count = 0;
2670 dict_T *d = dict_alloc();
2671 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002672 char_u *whitep = *arg;
2673 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002674
2675 if (d == NULL)
2676 return FAIL;
2677 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002678 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002679 {
2680 char_u *key = NULL;
2681
Bram Moolenaar23c55272020-06-21 16:58:13 +02002682 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002683 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002684 *arg = NULL;
2685 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002686 }
2687
2688 if (**arg == '}')
2689 break;
2690
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002691 if (literal)
2692 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002693 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002694
Bram Moolenaar2c330432020-04-13 14:41:35 +02002695 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002696 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002697 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002698 return FAIL;
2699 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002700 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002701 if (generate_PUSHS(cctx, key) == FAIL)
2702 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002703 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704 }
2705 else
2706 {
2707 isn_T *isn;
2708
Bram Moolenaara5565e42020-05-09 15:44:01 +02002709 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2712 if (isn->isn_type == ISN_PUSHS)
2713 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002714 else
2715 {
2716 type_T *keytype = ((type_T **)stack->ga_data)
2717 [stack->ga_len - 1];
2718 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2719 return FAIL;
2720 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002721 }
2722
2723 // Check for duplicate keys, if using string keys.
2724 if (key != NULL)
2725 {
2726 item = dict_find(d, key, -1);
2727 if (item != NULL)
2728 {
2729 semsg(_(e_duplicate_key), key);
2730 goto failret;
2731 }
2732 item = dictitem_alloc(key);
2733 if (item != NULL)
2734 {
2735 item->di_tv.v_type = VAR_UNKNOWN;
2736 item->di_tv.v_lock = 0;
2737 if (dict_add(d, item) == FAIL)
2738 dictitem_free(item);
2739 }
2740 }
2741
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742 if (**arg != ':')
2743 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002744 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002745 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002746 else
2747 semsg(_(e_missing_dict_colon), *arg);
2748 return FAIL;
2749 }
2750 whitep = *arg + 1;
2751 if (!IS_WHITE_OR_NUL(*whitep))
2752 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002753 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002754 return FAIL;
2755 }
2756
2757 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002758 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002759 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002760 *arg = NULL;
2761 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002762 }
2763
Bram Moolenaara5565e42020-05-09 15:44:01 +02002764 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765 return FAIL;
2766 ++count;
2767
Bram Moolenaar2c330432020-04-13 14:41:35 +02002768 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002769 *arg = skipwhite(*arg);
2770 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002771 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002772 *arg = NULL;
2773 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002774 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775 if (**arg == '}')
2776 break;
2777 if (**arg != ',')
2778 {
2779 semsg(_(e_missing_dict_comma), *arg);
2780 goto failret;
2781 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002782 if (IS_WHITE_OR_NUL(*whitep))
2783 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002784 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002785 return FAIL;
2786 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002787 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 *arg = skipwhite(*arg + 1);
2789 }
2790
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002791 *arg = *arg + 1;
2792
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002793 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002794 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002795 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002796 *arg += STRLEN(*arg);
2797
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 dict_unref(d);
2799 return generate_NEWDICT(cctx, count);
2800
2801failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002802 if (*arg == NULL)
2803 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 dict_unref(d);
2805 return FAIL;
2806}
2807
2808/*
2809 * Compile "&option".
2810 */
2811 static int
2812compile_get_option(char_u **arg, cctx_T *cctx)
2813{
2814 typval_T rettv;
2815 char_u *start = *arg;
2816 int ret;
2817
2818 // parse the option and get the current value to get the type.
2819 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002820 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821 if (ret == OK)
2822 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002823 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824 char_u *name = vim_strnsave(start, *arg - start);
2825 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2826
2827 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2828 vim_free(name);
2829 }
2830 clear_tv(&rettv);
2831
2832 return ret;
2833}
2834
2835/*
2836 * Compile "$VAR".
2837 */
2838 static int
2839compile_get_env(char_u **arg, cctx_T *cctx)
2840{
2841 char_u *start = *arg;
2842 int len;
2843 int ret;
2844 char_u *name;
2845
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846 ++*arg;
2847 len = get_env_len(arg);
2848 if (len == 0)
2849 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002850 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 return FAIL;
2852 }
2853
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002854 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855 name = vim_strnsave(start, len + 1);
2856 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2857 vim_free(name);
2858 return ret;
2859}
2860
2861/*
2862 * Compile "@r".
2863 */
2864 static int
2865compile_get_register(char_u **arg, cctx_T *cctx)
2866{
2867 int ret;
2868
2869 ++*arg;
2870 if (**arg == NUL)
2871 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002872 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873 return FAIL;
2874 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002875 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002876 {
2877 emsg_invreg(**arg);
2878 return FAIL;
2879 }
2880 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2881 ++*arg;
2882 return ret;
2883}
2884
2885/*
2886 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002887 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888 */
2889 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002890apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002892 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893
2894 // this works from end to start
2895 while (p > start)
2896 {
2897 --p;
2898 if (*p == '-' || *p == '+')
2899 {
2900 // only '-' has an effect, for '+' we only check the type
2901#ifdef FEAT_FLOAT
2902 if (rettv->v_type == VAR_FLOAT)
2903 {
2904 if (*p == '-')
2905 rettv->vval.v_float = -rettv->vval.v_float;
2906 }
2907 else
2908#endif
2909 {
2910 varnumber_T val;
2911 int error = FALSE;
2912
2913 // tv_get_number_chk() accepts a string, but we don't want that
2914 // here
2915 if (check_not_string(rettv) == FAIL)
2916 return FAIL;
2917 val = tv_get_number_chk(rettv, &error);
2918 clear_tv(rettv);
2919 if (error)
2920 return FAIL;
2921 if (*p == '-')
2922 val = -val;
2923 rettv->v_type = VAR_NUMBER;
2924 rettv->vval.v_number = val;
2925 }
2926 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002927 else if (numeric_only)
2928 {
2929 ++p;
2930 break;
2931 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 else
2933 {
2934 int v = tv2bool(rettv);
2935
2936 // '!' is permissive in the type.
2937 clear_tv(rettv);
2938 rettv->v_type = VAR_BOOL;
2939 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2940 }
2941 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002942 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943 return OK;
2944}
2945
2946/*
2947 * Recognize v: variables that are constants and set "rettv".
2948 */
2949 static void
2950get_vim_constant(char_u **arg, typval_T *rettv)
2951{
2952 if (STRNCMP(*arg, "v:true", 6) == 0)
2953 {
2954 rettv->v_type = VAR_BOOL;
2955 rettv->vval.v_number = VVAL_TRUE;
2956 *arg += 6;
2957 }
2958 else if (STRNCMP(*arg, "v:false", 7) == 0)
2959 {
2960 rettv->v_type = VAR_BOOL;
2961 rettv->vval.v_number = VVAL_FALSE;
2962 *arg += 7;
2963 }
2964 else if (STRNCMP(*arg, "v:null", 6) == 0)
2965 {
2966 rettv->v_type = VAR_SPECIAL;
2967 rettv->vval.v_number = VVAL_NULL;
2968 *arg += 6;
2969 }
2970 else if (STRNCMP(*arg, "v:none", 6) == 0)
2971 {
2972 rettv->v_type = VAR_SPECIAL;
2973 rettv->vval.v_number = VVAL_NONE;
2974 *arg += 6;
2975 }
2976}
2977
Bram Moolenaar696ba232020-07-29 21:20:41 +02002978 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02002979get_compare_type(char_u *p, int *len, int *type_is)
2980{
2981 exptype_T type = EXPR_UNKNOWN;
2982 int i;
2983
2984 switch (p[0])
2985 {
2986 case '=': if (p[1] == '=')
2987 type = EXPR_EQUAL;
2988 else if (p[1] == '~')
2989 type = EXPR_MATCH;
2990 break;
2991 case '!': if (p[1] == '=')
2992 type = EXPR_NEQUAL;
2993 else if (p[1] == '~')
2994 type = EXPR_NOMATCH;
2995 break;
2996 case '>': if (p[1] != '=')
2997 {
2998 type = EXPR_GREATER;
2999 *len = 1;
3000 }
3001 else
3002 type = EXPR_GEQUAL;
3003 break;
3004 case '<': if (p[1] != '=')
3005 {
3006 type = EXPR_SMALLER;
3007 *len = 1;
3008 }
3009 else
3010 type = EXPR_SEQUAL;
3011 break;
3012 case 'i': if (p[1] == 's')
3013 {
3014 // "is" and "isnot"; but not a prefix of a name
3015 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3016 *len = 5;
3017 i = p[*len];
3018 if (!isalnum(i) && i != '_')
3019 {
3020 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3021 *type_is = TRUE;
3022 }
3023 }
3024 break;
3025 }
3026 return type;
3027}
3028
3029/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003031 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 */
3033 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003034compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003036 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037
3038 // this works from end to start
3039 while (p > start)
3040 {
3041 --p;
3042 if (*p == '-' || *p == '+')
3043 {
3044 int negate = *p == '-';
3045 isn_T *isn;
3046
3047 // TODO: check type
3048 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3049 {
3050 --p;
3051 if (*p == '-')
3052 negate = !negate;
3053 }
3054 // only '-' has an effect, for '+' we only check the type
3055 if (negate)
3056 isn = generate_instr(cctx, ISN_NEGATENR);
3057 else
3058 isn = generate_instr(cctx, ISN_CHECKNR);
3059 if (isn == NULL)
3060 return FAIL;
3061 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003062 else if (numeric_only)
3063 {
3064 ++p;
3065 break;
3066 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 else
3068 {
3069 int invert = TRUE;
3070
3071 while (p > start && p[-1] == '!')
3072 {
3073 --p;
3074 invert = !invert;
3075 }
3076 if (generate_2BOOL(cctx, invert) == FAIL)
3077 return FAIL;
3078 }
3079 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003080 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081 return OK;
3082}
3083
3084/*
3085 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003086 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087 */
3088 static int
3089compile_subscript(
3090 char_u **arg,
3091 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003092 char_u *start_leader,
3093 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003094 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003095{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003096 char_u *name_start = *end_leader;
3097
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 for (;;)
3099 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003100 char_u *p = skipwhite(*arg);
3101
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003102 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003103 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003104 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003105
3106 // If a following line starts with "->{" or "->X" advance to that
3107 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003108 // Also if a following line starts with ".x".
3109 if (next != NULL &&
3110 ((next[0] == '-' && next[1] == '>'
3111 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003112 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003113 {
3114 next = next_line_from_context(cctx, TRUE);
3115 if (next == NULL)
3116 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003117 *arg = next;
3118 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003119 }
3120 }
3121
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003122 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3123 // not a function call.
3124 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003126 garray_T *stack = &cctx->ctx_type_stack;
3127 type_T *type;
3128 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003130 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003131 return FAIL;
3132
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003134 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3135
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003136 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3138 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003139 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140 return FAIL;
3141 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003142 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003144 char_u *pstart = p;
3145
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003146 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003147 return FAIL;
3148
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 // something->method()
3150 // Apply the '!', '-' and '+' first:
3151 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003152 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003153 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003155 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003156 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003157 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 if (**arg == '{')
3159 {
3160 // lambda call: list->{lambda}
3161 if (compile_lambda_call(arg, cctx) == FAIL)
3162 return FAIL;
3163 }
3164 else
3165 {
3166 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003167 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003168 if (!eval_isnamec1(*p))
3169 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003170 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003171 return FAIL;
3172 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003173 if (ASCII_ISALPHA(*p) && p[1] == ':')
3174 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003175 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 ;
3177 if (*p != '(')
3178 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003179 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 return FAIL;
3181 }
3182 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003183 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 return FAIL;
3185 }
3186 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003187 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003189 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003190 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003191 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003192 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003193 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003194
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003195 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003196 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003197 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003198 // TODO: blob index
3199 // TODO: more arguments
3200 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003201 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003202 return FAIL;
3203
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003204 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003205 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003206 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003207 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003208 if (**arg == ':')
3209 // missing first index is equal to zero
3210 generate_PUSHNR(cctx, 0);
3211 else
3212 {
3213 if (compile_expr0(arg, cctx) == FAIL)
3214 return FAIL;
3215 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3216 return FAIL;
3217 *arg = skipwhite(*arg);
3218 }
3219 if (**arg == ':')
3220 {
3221 *arg = skipwhite(*arg + 1);
3222 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3223 return FAIL;
3224 if (**arg == ']')
3225 // missing second index is equal to end of string
3226 generate_PUSHNR(cctx, -1);
3227 else
3228 {
3229 if (compile_expr0(arg, cctx) == FAIL)
3230 return FAIL;
3231 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3232 return FAIL;
3233 *arg = skipwhite(*arg);
3234 }
3235 is_slice = TRUE;
3236 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237
3238 if (**arg != ']')
3239 {
3240 emsg(_(e_missbrac));
3241 return FAIL;
3242 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003243 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003245 // We can index a list and a dict. If we don't know the type
3246 // we can use the index value type.
3247 // TODO: If we don't know use an instruction to figure it out at
3248 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003249 typep = ((type_T **)stack->ga_data) + stack->ga_len
3250 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003251 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003252 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3253 // If the index is a string, the variable must be a Dict.
3254 if (*typep == &t_any && valtype == &t_string)
3255 vtype = VAR_DICT;
3256 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003257 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003258 if (need_type(valtype, &t_number, -1, cctx, FALSE) == FAIL)
3259 return FAIL;
3260 if (is_slice)
3261 {
3262 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
3263 if (need_type(valtype, &t_number, -2, cctx, FALSE) == FAIL)
3264 return FAIL;
3265 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003266 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003267
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003268 if (vtype == VAR_DICT)
3269 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003270 if (is_slice)
3271 {
3272 emsg(_(e_cannot_slice_dictionary));
3273 return FAIL;
3274 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003275 if ((*typep)->tt_type == VAR_DICT)
3276 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003277 else
3278 {
3279 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3280 return FAIL;
3281 *typep = &t_any;
3282 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003283 if (may_generate_2STRING(-1, cctx) == FAIL)
3284 return FAIL;
3285 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3286 return FAIL;
3287 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003288 else if (vtype == VAR_STRING)
3289 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003290 *typep = &t_string;
3291 if ((is_slice
3292 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3293 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003294 return FAIL;
3295 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003296 else if (vtype == VAR_BLOB)
3297 {
3298 emsg("Sorry, blob index and slice not implemented yet");
3299 return FAIL;
3300 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003301 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003302 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003303 if (is_slice)
3304 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003305 if (generate_instr_drop(cctx,
3306 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3307 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003308 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003309 }
Bram Moolenaared591872020-08-15 22:14:53 +02003310 else
3311 {
3312 if ((*typep)->tt_type == VAR_LIST)
3313 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003314 if (generate_instr_drop(cctx,
3315 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3316 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003317 return FAIL;
3318 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003319 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003320 else
3321 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003322 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003323 return FAIL;
3324 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003325 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003326 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003328 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003329 return FAIL;
3330
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003331 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003332 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3333 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003334 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003335 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003336 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003337 while (eval_isnamec(*p))
3338 MB_PTR_ADV(p);
3339 if (p == *arg)
3340 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003341 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003342 return FAIL;
3343 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003344 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003345 return FAIL;
3346 *arg = p;
3347 }
3348 else
3349 break;
3350 }
3351
3352 // TODO - see handle_subscript():
3353 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3354 // Don't do this when "Func" is already a partial that was bound
3355 // explicitly (pt_auto is FALSE).
3356
3357 return OK;
3358}
3359
3360/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003361 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3362 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003364 * If the value is a constant "ppconst->pp_ret" will be set.
3365 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003366 *
3367 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 */
3369
3370/*
3371 * number number constant
3372 * 0zFFFFFFFF Blob constant
3373 * "string" string constant
3374 * 'string' literal string constant
3375 * &option-name option value
3376 * @r register contents
3377 * identifier variable value
3378 * function() function call
3379 * $VAR environment variable
3380 * (expression) nested expression
3381 * [expr, expr] List
3382 * {key: val, key: val} Dictionary
3383 * #{key: val, key: val} Dictionary with literal keys
3384 *
3385 * Also handle:
3386 * ! in front logical NOT
3387 * - in front unary minus
3388 * + in front unary plus (ignored)
3389 * trailing (arg) funcref/partial call
3390 * trailing [] subscript in String or List
3391 * trailing .name entry in Dictionary
3392 * trailing ->name() method call
3393 */
3394 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003395compile_expr7(
3396 char_u **arg,
3397 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003398 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003400 char_u *start_leader, *end_leader;
3401 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003402 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003403 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003404
3405 /*
3406 * Skip '!', '-' and '+' characters. They are handled later.
3407 */
3408 start_leader = *arg;
3409 while (**arg == '!' || **arg == '-' || **arg == '+')
3410 *arg = skipwhite(*arg + 1);
3411 end_leader = *arg;
3412
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003413 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414 switch (**arg)
3415 {
3416 /*
3417 * Number constant.
3418 */
3419 case '0': // also for blob starting with 0z
3420 case '1':
3421 case '2':
3422 case '3':
3423 case '4':
3424 case '5':
3425 case '6':
3426 case '7':
3427 case '8':
3428 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003429 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003431 // Apply "-" and "+" just before the number now, right to
3432 // left. Matters especially when "->" follows. Stops at
3433 // '!'.
3434 if (apply_leader(rettv, TRUE,
3435 start_leader, &end_leader) == FAIL)
3436 {
3437 clear_tv(rettv);
3438 return FAIL;
3439 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 break;
3441
3442 /*
3443 * String constant: "string".
3444 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003445 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 return FAIL;
3447 break;
3448
3449 /*
3450 * Literal string constant: 'str''ing'.
3451 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003452 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 return FAIL;
3454 break;
3455
3456 /*
3457 * Constant Vim variable.
3458 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003459 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460 ret = NOTDONE;
3461 break;
3462
3463 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003464 * "true" constant
3465 */
3466 case 't': if (STRNCMP(*arg, "true", 4) == 0
3467 && !eval_isnamec((*arg)[4]))
3468 {
3469 *arg += 4;
3470 rettv->v_type = VAR_BOOL;
3471 rettv->vval.v_number = VVAL_TRUE;
3472 }
3473 else
3474 ret = NOTDONE;
3475 break;
3476
3477 /*
3478 * "false" constant
3479 */
3480 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3481 && !eval_isnamec((*arg)[5]))
3482 {
3483 *arg += 5;
3484 rettv->v_type = VAR_BOOL;
3485 rettv->vval.v_number = VVAL_FALSE;
3486 }
3487 else
3488 ret = NOTDONE;
3489 break;
3490
3491 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003492 * List: [expr, expr]
3493 */
3494 case '[': ret = compile_list(arg, cctx);
3495 break;
3496
3497 /*
3498 * Dictionary: #{key: val, key: val}
3499 */
3500 case '#': if ((*arg)[1] == '{')
3501 {
3502 ++*arg;
3503 ret = compile_dict(arg, cctx, TRUE);
3504 }
3505 else
3506 ret = NOTDONE;
3507 break;
3508
3509 /*
3510 * Lambda: {arg, arg -> expr}
3511 * Dictionary: {'key': val, 'key': val}
3512 */
3513 case '{': {
3514 char_u *start = skipwhite(*arg + 1);
3515
3516 // Find out what comes after the arguments.
3517 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003518 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 if (ret != FAIL && *start == '>')
3520 ret = compile_lambda(arg, cctx);
3521 else
3522 ret = compile_dict(arg, cctx, FALSE);
3523 }
3524 break;
3525
3526 /*
3527 * Option value: &name
3528 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003529 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3530 return FAIL;
3531 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532 break;
3533
3534 /*
3535 * Environment variable: $VAR.
3536 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003537 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3538 return FAIL;
3539 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540 break;
3541
3542 /*
3543 * Register contents: @r.
3544 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003545 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3546 return FAIL;
3547 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548 break;
3549 /*
3550 * nested expression: (expression).
3551 */
3552 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003553
3554 // recursive!
3555 if (ppconst->pp_used <= PPSIZE - 10)
3556 {
3557 ret = compile_expr1(arg, cctx, ppconst);
3558 }
3559 else
3560 {
3561 // Not enough space in ppconst, flush constants.
3562 if (generate_ppconst(cctx, ppconst) == FAIL)
3563 return FAIL;
3564 ret = compile_expr0(arg, cctx);
3565 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 *arg = skipwhite(*arg);
3567 if (**arg == ')')
3568 ++*arg;
3569 else if (ret == OK)
3570 {
3571 emsg(_(e_missing_close));
3572 ret = FAIL;
3573 }
3574 break;
3575
3576 default: ret = NOTDONE;
3577 break;
3578 }
3579 if (ret == FAIL)
3580 return FAIL;
3581
Bram Moolenaar1c747212020-05-09 18:28:34 +02003582 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003584 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003585 clear_tv(rettv);
3586 else
3587 // A constant expression can possibly be handled compile time,
3588 // return the value instead of generating code.
3589 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590 }
3591 else if (ret == NOTDONE)
3592 {
3593 char_u *p;
3594 int r;
3595
3596 if (!eval_isnamec1(**arg))
3597 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003598 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003599 return FAIL;
3600 }
3601
3602 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003603 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003604 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003605 {
3606 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3607 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003609 {
3610 if (generate_ppconst(cctx, ppconst) == FAIL)
3611 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003612 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003613 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 if (r == FAIL)
3615 return FAIL;
3616 }
3617
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003618 // Handle following "[]", ".member", etc.
3619 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003620 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003621 ppconst) == FAIL)
3622 return FAIL;
3623 if (ppconst->pp_used > 0)
3624 {
3625 // apply the '!', '-' and '+' before the constant
3626 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003627 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003628 return FAIL;
3629 return OK;
3630 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003631 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003633 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634}
3635
3636/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003637 * Give the "white on both sides" error, taking the operator from "p[len]".
3638 */
3639 void
3640error_white_both(char_u *op, int len)
3641{
3642 char_u buf[10];
3643
3644 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003645 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003646}
3647
3648/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003649 * <type>expr7: runtime type check / conversion
3650 */
3651 static int
3652compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3653{
3654 type_T *want_type = NULL;
3655
3656 // Recognize <type>
3657 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3658 {
3659 int called_emsg_before = called_emsg;
3660
3661 ++*arg;
3662 want_type = parse_type(arg, cctx->ctx_type_list);
3663 if (called_emsg != called_emsg_before)
3664 return FAIL;
3665
3666 if (**arg != '>')
3667 {
3668 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003669 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003670 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003671 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003672 return FAIL;
3673 }
3674 ++*arg;
3675 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3676 return FAIL;
3677 }
3678
3679 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3680 return FAIL;
3681
3682 if (want_type != NULL)
3683 {
3684 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003685 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003686
Bram Moolenaard1103582020-08-14 22:44:25 +02003687 generate_ppconst(cctx, ppconst);
3688 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003689 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003690 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003691 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3692 return FAIL;
3693 }
3694 }
3695
3696 return OK;
3697}
3698
3699/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700 * * number multiplication
3701 * / number division
3702 * % number modulo
3703 */
3704 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003705compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706{
3707 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003708 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003709 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003711 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003712 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713 return FAIL;
3714
3715 /*
3716 * Repeat computing, until no "*", "/" or "%" is following.
3717 */
3718 for (;;)
3719 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003720 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 if (*op != '*' && *op != '/' && *op != '%')
3722 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003723 if (next != NULL)
3724 {
3725 *arg = next_line_from_context(cctx, TRUE);
3726 op = skipwhite(*arg);
3727 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003728
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003729 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003730 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003731 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003732 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 }
3734 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003735 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003736 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003737
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003738 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003739 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003741
3742 if (ppconst->pp_used == ppconst_used + 2
3743 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3744 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003745 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003746 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3747 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003748 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003750 // both are numbers: compute the result
3751 switch (*op)
3752 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003753 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003754 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003755 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003756 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003757 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003758 break;
3759 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003760 tv1->vval.v_number = res;
3761 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003762 }
3763 else
3764 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003765 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003766 generate_two_op(cctx, op);
3767 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 }
3769
3770 return OK;
3771}
3772
3773/*
3774 * + number addition
3775 * - number subtraction
3776 * .. string concatenation
3777 */
3778 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003779compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780{
3781 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003782 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003784 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003785
3786 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003787 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788 return FAIL;
3789
3790 /*
3791 * Repeat computing, until no "+", "-" or ".." is following.
3792 */
3793 for (;;)
3794 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003795 op = may_peek_next_line(cctx, *arg, &next);
3796 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797 break;
3798 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003799 if (next != NULL)
3800 {
3801 *arg = next_line_from_context(cctx, TRUE);
3802 op = skipwhite(*arg);
3803 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003804
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003805 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003807 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003808 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 }
3810
3811 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003812 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003813 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003815 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003816 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 return FAIL;
3818
Bram Moolenaara5565e42020-05-09 15:44:01 +02003819 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003820 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003821 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3822 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3823 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3824 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003826 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3827 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003828
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003829 // concat/subtract/add constant numbers
3830 if (*op == '+')
3831 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3832 else if (*op == '-')
3833 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3834 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003835 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003836 // concatenate constant strings
3837 char_u *s1 = tv1->vval.v_string;
3838 char_u *s2 = tv2->vval.v_string;
3839 size_t len1 = STRLEN(s1);
3840
3841 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3842 if (tv1->vval.v_string == NULL)
3843 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003844 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003845 return FAIL;
3846 }
3847 mch_memmove(tv1->vval.v_string, s1, len1);
3848 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003849 vim_free(s1);
3850 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003851 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003852 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853 }
3854 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003855 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003856 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003857 if (*op == '.')
3858 {
3859 if (may_generate_2STRING(-2, cctx) == FAIL
3860 || may_generate_2STRING(-1, cctx) == FAIL)
3861 return FAIL;
3862 generate_instr_drop(cctx, ISN_CONCAT, 1);
3863 }
3864 else
3865 generate_two_op(cctx, op);
3866 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 }
3868
3869 return OK;
3870}
3871
3872/*
3873 * expr5a == expr5b
3874 * expr5a =~ expr5b
3875 * expr5a != expr5b
3876 * expr5a !~ expr5b
3877 * expr5a > expr5b
3878 * expr5a >= expr5b
3879 * expr5a < expr5b
3880 * expr5a <= expr5b
3881 * expr5a is expr5b
3882 * expr5a isnot expr5b
3883 *
3884 * Produces instructions:
3885 * EVAL expr5a Push result of "expr5a"
3886 * EVAL expr5b Push result of "expr5b"
3887 * COMPARE one of the compare instructions
3888 */
3889 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003890compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003891{
3892 exptype_T type = EXPR_UNKNOWN;
3893 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003894 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003895 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003897 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898
3899 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003900 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901 return FAIL;
3902
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003903 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003904 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905
3906 /*
3907 * If there is a comparative operator, use it.
3908 */
3909 if (type != EXPR_UNKNOWN)
3910 {
3911 int ic = FALSE; // Default: do not ignore case
3912
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003913 if (next != NULL)
3914 {
3915 *arg = next_line_from_context(cctx, TRUE);
3916 p = skipwhite(*arg);
3917 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918 if (type_is && (p[len] == '?' || p[len] == '#'))
3919 {
3920 semsg(_(e_invexpr2), *arg);
3921 return FAIL;
3922 }
3923 // extra question mark appended: ignore case
3924 if (p[len] == '?')
3925 {
3926 ic = TRUE;
3927 ++len;
3928 }
3929 // extra '#' appended: match case (ignored)
3930 else if (p[len] == '#')
3931 ++len;
3932 // nothing appended: match case
3933
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003934 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003935 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003936 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003937 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 }
3939
3940 // get the second variable
3941 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003942 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003943 return FAIL;
3944
Bram Moolenaara5565e42020-05-09 15:44:01 +02003945 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 return FAIL;
3947
Bram Moolenaara5565e42020-05-09 15:44:01 +02003948 if (ppconst->pp_used == ppconst_used + 2)
3949 {
3950 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3951 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3952 int ret;
3953
3954 // Both sides are a constant, compute the result now.
3955 // First check for a valid combination of types, this is more
3956 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003957 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003958 ret = FAIL;
3959 else
3960 {
3961 ret = typval_compare(tv1, tv2, type, ic);
3962 tv1->v_type = VAR_BOOL;
3963 tv1->vval.v_number = tv1->vval.v_number
3964 ? VVAL_TRUE : VVAL_FALSE;
3965 clear_tv(tv2);
3966 --ppconst->pp_used;
3967 }
3968 return ret;
3969 }
3970
3971 generate_ppconst(cctx, ppconst);
3972 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 }
3974
3975 return OK;
3976}
3977
Bram Moolenaar7f141552020-05-09 17:35:53 +02003978static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3979
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980/*
3981 * Compile || or &&.
3982 */
3983 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003984compile_and_or(
3985 char_u **arg,
3986 cctx_T *cctx,
3987 char *op,
3988 ppconst_T *ppconst,
3989 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003991 char_u *next;
3992 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003993 int opchar = *op;
3994
3995 if (p[0] == opchar && p[1] == opchar)
3996 {
3997 garray_T *instr = &cctx->ctx_instr;
3998 garray_T end_ga;
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02003999 garray_T *stack = &cctx->ctx_type_stack;
4000 type_T **typep;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001
4002 /*
4003 * Repeat until there is no following "||" or "&&"
4004 */
4005 ga_init2(&end_ga, sizeof(int), 10);
4006 while (p[0] == opchar && p[1] == opchar)
4007 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004008 if (next != NULL)
4009 {
4010 *arg = next_line_from_context(cctx, TRUE);
4011 p = skipwhite(*arg);
4012 }
4013
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004014 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4015 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004016 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004017 return FAIL;
4018 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019
Bram Moolenaara5565e42020-05-09 15:44:01 +02004020 // TODO: use ppconst if the value is a constant
4021 generate_ppconst(cctx, ppconst);
4022
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023 if (ga_grow(&end_ga, 1) == FAIL)
4024 {
4025 ga_clear(&end_ga);
4026 return FAIL;
4027 }
4028 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4029 ++end_ga.ga_len;
4030 generate_JUMP(cctx, opchar == '|'
4031 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4032
4033 // eval the next expression
4034 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004035 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004036 return FAIL;
4037
Bram Moolenaara5565e42020-05-09 15:44:01 +02004038 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4039 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 {
4041 ga_clear(&end_ga);
4042 return FAIL;
4043 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004044
4045 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004047 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048
4049 // Fill in the end label in all jumps.
4050 while (end_ga.ga_len > 0)
4051 {
4052 isn_T *isn;
4053
4054 --end_ga.ga_len;
4055 isn = ((isn_T *)instr->ga_data)
4056 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4057 isn->isn_arg.jump.jump_where = instr->ga_len;
4058 }
4059 ga_clear(&end_ga);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004060
4061 // The resulting type can be used as a bool.
4062 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4063 if (*typep != &t_bool)
4064 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004065 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004066
4067 if (type != NULL)
4068 {
4069 *type = **typep;
4070 type->tt_flags |= TTFLAG_BOOL_OK;
4071 *typep = type;
4072 }
4073 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004074 }
4075
4076 return OK;
4077}
4078
4079/*
4080 * expr4a && expr4a && expr4a logical AND
4081 *
4082 * Produces instructions:
4083 * EVAL expr4a Push result of "expr4a"
4084 * JUMP_AND_KEEP_IF_FALSE end
4085 * EVAL expr4b Push result of "expr4b"
4086 * JUMP_AND_KEEP_IF_FALSE end
4087 * EVAL expr4c Push result of "expr4c"
4088 * end:
4089 */
4090 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004091compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004092{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004093 int ppconst_used = ppconst->pp_used;
4094
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004096 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 return FAIL;
4098
4099 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004100 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101}
4102
4103/*
4104 * expr3a || expr3b || expr3c logical OR
4105 *
4106 * Produces instructions:
4107 * EVAL expr3a Push result of "expr3a"
4108 * JUMP_AND_KEEP_IF_TRUE end
4109 * EVAL expr3b Push result of "expr3b"
4110 * JUMP_AND_KEEP_IF_TRUE end
4111 * EVAL expr3c Push result of "expr3c"
4112 * end:
4113 */
4114 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004115compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004116{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004117 int ppconst_used = ppconst->pp_used;
4118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004120 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121 return FAIL;
4122
4123 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004124 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004125}
4126
4127/*
4128 * Toplevel expression: expr2 ? expr1a : expr1b
4129 *
4130 * Produces instructions:
4131 * EVAL expr2 Push result of "expr"
4132 * JUMP_IF_FALSE alt jump if false
4133 * EVAL expr1a
4134 * JUMP_ALWAYS end
4135 * alt: EVAL expr1b
4136 * end:
4137 */
4138 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004139compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140{
4141 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004142 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004143 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144
Bram Moolenaar3988f642020-08-27 22:43:03 +02004145 // Ignore all kinds of errors when not producing code.
4146 if (cctx->ctx_skip == SKIP_YES)
4147 {
4148 skip_expr(arg);
4149 return OK;
4150 }
4151
Bram Moolenaar61a89812020-05-07 16:58:17 +02004152 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004153 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154 return FAIL;
4155
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004156 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 if (*p == '?')
4158 {
4159 garray_T *instr = &cctx->ctx_instr;
4160 garray_T *stack = &cctx->ctx_type_stack;
4161 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004162 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004164 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004166 int has_const_expr = FALSE;
4167 int const_value = FALSE;
4168 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004169
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004170 if (next != NULL)
4171 {
4172 *arg = next_line_from_context(cctx, TRUE);
4173 p = skipwhite(*arg);
4174 }
4175
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004176 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4177 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004178 semsg(_(e_white_space_required_before_and_after_str), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004179 return FAIL;
4180 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181
Bram Moolenaara5565e42020-05-09 15:44:01 +02004182 if (ppconst->pp_used == ppconst_used + 1)
4183 {
4184 // the condition is a constant, we know whether the ? or the :
4185 // expression is to be evaluated.
4186 has_const_expr = TRUE;
4187 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4188 clear_tv(&ppconst->pp_tv[ppconst_used]);
4189 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004190 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4191 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004192 }
4193 else
4194 {
4195 generate_ppconst(cctx, ppconst);
4196 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4197 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198
4199 // evaluate the second expression; any type is accepted
4200 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004201 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004202 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004203 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004204 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205
Bram Moolenaara5565e42020-05-09 15:44:01 +02004206 if (!has_const_expr)
4207 {
4208 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209
Bram Moolenaara5565e42020-05-09 15:44:01 +02004210 // remember the type and drop it
4211 --stack->ga_len;
4212 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213
Bram Moolenaara5565e42020-05-09 15:44:01 +02004214 end_idx = instr->ga_len;
4215 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4216
4217 // jump here from JUMP_IF_FALSE
4218 isn = ((isn_T *)instr->ga_data) + alt_idx;
4219 isn->isn_arg.jump.jump_where = instr->ga_len;
4220 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004221
4222 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004223 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224 if (*p != ':')
4225 {
4226 emsg(_(e_missing_colon));
4227 return FAIL;
4228 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004229 if (next != NULL)
4230 {
4231 *arg = next_line_from_context(cctx, TRUE);
4232 p = skipwhite(*arg);
4233 }
4234
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004235 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4236 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004237 semsg(_(e_white_space_required_before_and_after_str), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004238 return FAIL;
4239 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240
4241 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004242 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004243 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4244 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004246 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004247 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004248 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004249 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250
Bram Moolenaara5565e42020-05-09 15:44:01 +02004251 if (!has_const_expr)
4252 {
4253 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254
Bram Moolenaara5565e42020-05-09 15:44:01 +02004255 // If the types differ, the result has a more generic type.
4256 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4257 common_type(type1, type2, &type2, cctx->ctx_type_list);
4258
4259 // jump here from JUMP_ALWAYS
4260 isn = ((isn_T *)instr->ga_data) + end_idx;
4261 isn->isn_arg.jump.jump_where = instr->ga_len;
4262 }
4263
4264 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265 }
4266 return OK;
4267}
4268
4269/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004270 * Toplevel expression.
4271 */
4272 static int
4273compile_expr0(char_u **arg, cctx_T *cctx)
4274{
4275 ppconst_T ppconst;
4276
4277 CLEAR_FIELD(ppconst);
4278 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4279 {
4280 clear_ppconst(&ppconst);
4281 return FAIL;
4282 }
4283 if (generate_ppconst(cctx, &ppconst) == FAIL)
4284 return FAIL;
4285 return OK;
4286}
4287
4288/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004289 * compile "return [expr]"
4290 */
4291 static char_u *
4292compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4293{
4294 char_u *p = arg;
4295 garray_T *stack = &cctx->ctx_type_stack;
4296 type_T *stack_type;
4297
4298 if (*p != NUL && *p != '|' && *p != '\n')
4299 {
4300 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004301 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302 return NULL;
4303
4304 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4305 if (set_return_type)
4306 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004307 else
4308 {
4309 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4310 && stack_type->tt_type != VAR_VOID
4311 && stack_type->tt_type != VAR_UNKNOWN)
4312 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004313 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004314 return NULL;
4315 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004316 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4317 cctx, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004318 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004319 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 }
4321 else
4322 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004323 // "set_return_type" cannot be TRUE, only used for a lambda which
4324 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004325 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4326 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004328 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004329 return NULL;
4330 }
4331
4332 // No argument, return zero.
4333 generate_PUSHNR(cctx, 0);
4334 }
4335
4336 if (generate_instr(cctx, ISN_RETURN) == NULL)
4337 return NULL;
4338
4339 // "return val | endif" is possible
4340 return skipwhite(p);
4341}
4342
4343/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004344 * Get a line from the compilation context, compatible with exarg_T getline().
4345 * Return a pointer to the line in allocated memory.
4346 * Return NULL for end-of-file or some error.
4347 */
4348 static char_u *
4349exarg_getline(
4350 int c UNUSED,
4351 void *cookie,
4352 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004353 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004354{
4355 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004356 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004357
Bram Moolenaar66250c92020-08-20 15:02:42 +02004358 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004359 {
Bram Moolenaar66250c92020-08-20 15:02:42 +02004360 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4361 return NULL;
4362 ++cctx->ctx_lnum;
4363 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4364 // Comment lines result in NULL pointers, skip them.
4365 if (p != NULL)
4366 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004367 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004368}
4369
4370/*
4371 * Compile a nested :def command.
4372 */
4373 static char_u *
4374compile_nested_function(exarg_T *eap, cctx_T *cctx)
4375{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004376 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004377 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004378 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004379 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004380 lvar_T *lvar;
4381 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004382 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004383
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004384 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004385 {
4386 emsg(_(e_cannot_use_bang_with_nested_def));
4387 return NULL;
4388 }
4389
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004390 // Only g:Func() can use a namespace.
4391 if (name_start[1] == ':' && !is_global)
4392 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004393 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004394 return NULL;
4395 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004396 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4397 return NULL;
4398
Bram Moolenaar04b12692020-05-04 23:24:44 +02004399 eap->arg = name_end;
4400 eap->getline = exarg_getline;
4401 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004402 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004403 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004404 lambda_name = get_lambda_name();
4405 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004406
Bram Moolenaar822ba242020-05-24 23:00:18 +02004407 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004408 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004409 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004410 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004411 {
4412 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004413 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004414 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004415
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004416 if (is_global)
4417 {
4418 char_u *func_name = vim_strnsave(name_start + 2,
4419 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004420
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004421 if (func_name == NULL)
4422 r = FAIL;
4423 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004424 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004425 }
4426 else
4427 {
4428 // Define a local variable for the function reference.
4429 lvar = reserve_local(cctx, name_start, name_end - name_start,
4430 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004431 if (lvar == NULL)
4432 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004433 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004434 return NULL;
4435 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4436 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004437
Bram Moolenaar61a89812020-05-07 16:58:17 +02004438 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004439 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004440}
4441
4442/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443 * Return the length of an assignment operator, or zero if there isn't one.
4444 */
4445 int
4446assignment_len(char_u *p, int *heredoc)
4447{
4448 if (*p == '=')
4449 {
4450 if (p[1] == '<' && p[2] == '<')
4451 {
4452 *heredoc = TRUE;
4453 return 3;
4454 }
4455 return 1;
4456 }
4457 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4458 return 2;
4459 if (STRNCMP(p, "..=", 3) == 0)
4460 return 3;
4461 return 0;
4462}
4463
4464// words that cannot be used as a variable
4465static char *reserved[] = {
4466 "true",
4467 "false",
4468 NULL
4469};
4470
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004471typedef enum {
4472 dest_local,
4473 dest_option,
4474 dest_env,
4475 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004476 dest_buffer,
4477 dest_window,
4478 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004479 dest_vimvar,
4480 dest_script,
4481 dest_reg,
4482} assign_dest_T;
4483
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004485 * Generate the load instruction for "name".
4486 */
4487 static void
4488generate_loadvar(
4489 cctx_T *cctx,
4490 assign_dest_T dest,
4491 char_u *name,
4492 lvar_T *lvar,
4493 type_T *type)
4494{
4495 switch (dest)
4496 {
4497 case dest_option:
4498 // TODO: check the option exists
4499 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4500 break;
4501 case dest_global:
4502 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4503 break;
4504 case dest_buffer:
4505 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4506 break;
4507 case dest_window:
4508 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4509 break;
4510 case dest_tab:
4511 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4512 break;
4513 case dest_script:
4514 compile_load_scriptvar(cctx,
4515 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4516 break;
4517 case dest_env:
4518 // Include $ in the name here
4519 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4520 break;
4521 case dest_reg:
4522 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4523 break;
4524 case dest_vimvar:
4525 generate_LOADV(cctx, name + 2, TRUE);
4526 break;
4527 case dest_local:
4528 if (lvar->lv_from_outer)
4529 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4530 NULL, type);
4531 else
4532 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4533 break;
4534 }
4535}
4536
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004537 void
4538vim9_declare_error(char_u *name)
4539{
4540 char *scope = "";
4541
4542 switch (*name)
4543 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004544 case 'g': scope = _("global"); break;
4545 case 'b': scope = _("buffer"); break;
4546 case 'w': scope = _("window"); break;
4547 case 't': scope = _("tab"); break;
4548 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004549 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004550 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004551 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004552 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004553 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004554 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004555 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004556 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004557 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004558}
4559
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004560/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004561 * Compile declaration and assignment:
4562 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004563 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004564 * Return NULL for an error.
4565 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004566 */
4567 static char_u *
4568compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4569{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004570 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004572 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573 char_u *ret = NULL;
4574 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004575 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004576 int scriptvar_sid = 0;
4577 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004578 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004579 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004580 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004581 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004582 int oplen = 0;
4583 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004584 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004585 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004586 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004588 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004590 // Skip over the "var" or "[var, var]" to get to any "=".
4591 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4592 if (p == NULL)
4593 return *arg == '[' ? arg : NULL;
4594
4595 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004597 // TODO: should we allow this, and figure out type inference from list
4598 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004599 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600 return NULL;
4601 }
4602
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004603 sp = p;
4604 p = skipwhite(p);
4605 op = p;
4606 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004607
4608 if (var_count > 0 && oplen == 0)
4609 // can be something like "[1, 2]->func()"
4610 return arg;
4611
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004612 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4613 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004614 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004615 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004616 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004617
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618 if (heredoc)
4619 {
4620 list_T *l;
4621 listitem_T *li;
4622
4623 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004624 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004625 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004626 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627
Bram Moolenaar078269b2020-09-21 20:35:55 +02004628 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02004630 // Push each line and the create the list.
4631 FOR_ALL_LIST_ITEMS(l, li)
4632 {
4633 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4634 li->li_tv.vval.v_string = NULL;
4635 }
4636 generate_NEWLIST(cctx, l->lv_len);
4637 type = &t_list_string;
4638 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004640 list_free(l);
4641 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004642 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004644 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004645 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004646 // for "[var, var] = expr" evaluate the expression here, loop over the
4647 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004648
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004649 p = skipwhite(op + oplen);
4650 if (compile_expr0(&p, cctx) == FAIL)
4651 return NULL;
4652 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004654 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004655 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004656 type_T *stacktype;
4657
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004658 stacktype = stack->ga_len == 0 ? &t_void
4659 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004660 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004661 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004662 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004663 goto theend;
4664 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004665 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004666 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004667 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004668 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4669 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004670 }
4671 }
4672
4673 /*
4674 * Loop over variables in "[var, var] = expr".
4675 * For "var = expr" and "let var: type" this is done only once.
4676 */
4677 if (var_count > 0)
4678 var_start = skipwhite(arg + 1); // skip over the "["
4679 else
4680 var_start = arg;
4681 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4682 {
4683 char_u *var_end = skip_var_one(var_start, FALSE);
4684 size_t varlen;
4685 int new_local = FALSE;
4686 int opt_type;
4687 int opt_flags = 0;
4688 assign_dest_T dest = dest_local;
4689 int vimvaridx = -1;
4690 lvar_T *lvar = NULL;
4691 lvar_T arg_lvar;
4692 int has_type = FALSE;
4693 int has_index = FALSE;
4694 int instr_count = -1;
4695
Bram Moolenaar65821722020-08-02 18:58:54 +02004696 if (*var_start == '@')
4697 p = var_start + 2;
4698 else
4699 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02004700 // skip over the leading "&", "&l:", "&g:" and "$"
4701 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02004702 p = to_name_end(p, TRUE);
4703 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004704
4705 // "a: type" is declaring variable "a" with a type, not "a:".
4706 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4707 --var_end;
4708 if (is_decl && p == var_start + 2 && p[-1] == ':')
4709 --p;
4710
4711 varlen = p - var_start;
4712 vim_free(name);
4713 name = vim_strnsave(var_start, varlen);
4714 if (name == NULL)
4715 return NULL;
4716 if (!heredoc)
4717 type = &t_any;
4718
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004719 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004720 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004721 int declare_error = FALSE;
4722
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004723 if (*var_start == '&')
4724 {
4725 int cc;
4726 long numval;
4727
4728 dest = dest_option;
4729 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004730 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004731 emsg(_(e_const_option));
4732 goto theend;
4733 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004734 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004735 p = var_start;
4736 p = find_option_end(&p, &opt_flags);
4737 if (p == NULL)
4738 {
4739 // cannot happen?
4740 emsg(_(e_letunexp));
4741 goto theend;
4742 }
4743 cc = *p;
4744 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02004745 opt_type = get_option_value(skip_option_env_lead(var_start),
4746 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004747 *p = cc;
4748 if (opt_type == -3)
4749 {
4750 semsg(_(e_unknown_option), var_start);
4751 goto theend;
4752 }
4753 if (opt_type == -2 || opt_type == 0)
4754 type = &t_string;
4755 else
4756 type = &t_number; // both number and boolean option
4757 }
4758 else if (*var_start == '$')
4759 {
4760 dest = dest_env;
4761 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004762 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004763 }
4764 else if (*var_start == '@')
4765 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004766 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004767 {
4768 emsg_invreg(var_start[1]);
4769 goto theend;
4770 }
4771 dest = dest_reg;
4772 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004773 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004774 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004775 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004776 {
4777 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004778 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004779 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004780 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004781 {
4782 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004783 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004784 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004785 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004786 {
4787 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004788 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004789 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004790 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004791 {
4792 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004793 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004794 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004795 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004796 {
4797 typval_T *vtv;
4798 int di_flags;
4799
4800 vimvaridx = find_vim_var(name + 2, &di_flags);
4801 if (vimvaridx < 0)
4802 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004803 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004804 goto theend;
4805 }
4806 // We use the current value of "sandbox" here, is that OK?
4807 if (var_check_ro(di_flags, name, FALSE))
4808 goto theend;
4809 dest = dest_vimvar;
4810 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004811 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004812 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004813 }
4814 else
4815 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004816 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004817
4818 for (idx = 0; reserved[idx] != NULL; ++idx)
4819 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004820 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004821 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004822 goto theend;
4823 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004824
4825 lvar = lookup_local(var_start, varlen, cctx);
4826 if (lvar == NULL)
4827 {
4828 CLEAR_FIELD(arg_lvar);
4829 if (lookup_arg(var_start, varlen,
4830 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4831 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004832 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004833 if (is_decl)
4834 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004835 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004836 goto theend;
4837 }
4838 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004839 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004840 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004841 if (lvar != NULL)
4842 {
4843 if (is_decl)
4844 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004845 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004846 goto theend;
4847 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004848 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004849 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004850 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004851 int script_namespace = varlen > 1
4852 && STRNCMP(var_start, "s:", 2) == 0;
4853 int script_var = (script_namespace
Bram Moolenaar53900992020-08-22 19:02:02 +02004854 ? lookup_script(var_start + 2, varlen - 2, FALSE)
4855 : lookup_script(var_start, varlen, TRUE)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004856 imported_T *import =
4857 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004858
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004859 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004860 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004861 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4862
4863 if (is_decl)
4864 {
4865 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004866 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004867 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004868 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004869 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004870 name);
4871 goto theend;
4872 }
4873 else if (cctx->ctx_ufunc->uf_script_ctx_version
4874 == SCRIPT_VERSION_VIM9
4875 && script_namespace
4876 && !script_var && import == NULL)
4877 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004878 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004879 goto theend;
4880 }
4881
4882 dest = dest_script;
4883
4884 // existing script-local variables should have a type
4885 scriptvar_sid = current_sctx.sc_sid;
4886 if (import != NULL)
4887 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004888 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004889 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02004890 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4891 rawname, TRUE);
4892 if (scriptvar_idx > 0)
4893 {
4894 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4895 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004896 ((svar_T *)si->sn_var_vals.ga_data)
4897 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004898 type = sv->sv_type;
4899 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004900 }
4901 }
4902 else if (name[1] == ':' && name[2] != NUL)
4903 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004904 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004905 goto theend;
4906 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004907 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004908 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004909 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004910 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004911 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004912 else if (check_defined(var_start, varlen, cctx) == FAIL)
4913 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004914 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004915 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004916
4917 if (declare_error)
4918 {
4919 vim9_declare_error(name);
4920 goto theend;
4921 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004922 }
4923
4924 // handle "a:name" as a name, not index "name" on "a"
4925 if (varlen > 1 || var_start[varlen] != ':')
4926 p = var_end;
4927
4928 if (dest != dest_option)
4929 {
4930 if (is_decl && *p == ':')
4931 {
4932 // parse optional type: "let var: type = expr"
4933 if (!VIM_ISWHITE(p[1]))
4934 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004935 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004936 goto theend;
4937 }
4938 p = skipwhite(p + 1);
4939 type = parse_type(&p, cctx->ctx_type_list);
4940 has_type = TRUE;
4941 }
4942 else if (lvar != NULL)
4943 type = lvar->lv_type;
4944 }
4945
4946 if (oplen == 3 && !heredoc && dest != dest_global
4947 && type->tt_type != VAR_STRING
4948 && type->tt_type != VAR_ANY)
4949 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004950 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004951 goto theend;
4952 }
4953
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004954 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004955 {
4956 if (oplen > 1 && !heredoc)
4957 {
4958 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004959 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004960 goto theend;
4961 }
4962
4963 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004964 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
4965 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004966 goto theend;
4967 lvar = reserve_local(cctx, var_start, varlen,
4968 cmdidx == CMD_const, type);
4969 if (lvar == NULL)
4970 goto theend;
4971 new_local = TRUE;
4972 }
4973
4974 member_type = type;
4975 if (var_end > var_start + varlen)
4976 {
4977 // Something follows after the variable: "var[idx]".
4978 if (is_decl)
4979 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004980 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004981 goto theend;
4982 }
4983
4984 if (var_start[varlen] == '[')
4985 {
4986 has_index = TRUE;
4987 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004988 member_type = &t_any;
4989 else
4990 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004991 }
4992 else
4993 {
4994 semsg("Not supported yet: %s", var_start);
4995 goto theend;
4996 }
4997 }
4998 else if (lvar == &arg_lvar)
4999 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005000 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005001 goto theend;
5002 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005003 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5004 {
5005 semsg(_(e_cannot_assign_to_constant), name);
5006 goto theend;
5007 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005008
5009 if (!heredoc)
5010 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005011 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005012 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005013 if (oplen > 0 && var_count == 0)
5014 {
5015 // skip over the "=" and the expression
5016 p = skipwhite(op + oplen);
5017 compile_expr0(&p, cctx);
5018 }
5019 }
5020 else if (oplen > 0)
5021 {
5022 type_T *stacktype;
5023
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005024 // For "var = expr" evaluate the expression.
5025 if (var_count == 0)
5026 {
5027 int r;
5028
5029 // for "+=", "*=", "..=" etc. first load the current value
5030 if (*op != '=')
5031 {
5032 generate_loadvar(cctx, dest, name, lvar, type);
5033
5034 if (has_index)
5035 {
5036 // TODO: get member from list or dict
5037 emsg("Index with operation not supported yet");
5038 goto theend;
5039 }
5040 }
5041
5042 // Compile the expression. Temporarily hide the new local
5043 // variable here, it is not available to this expression.
5044 if (new_local)
5045 --cctx->ctx_locals.ga_len;
5046 instr_count = instr->ga_len;
5047 p = skipwhite(op + oplen);
5048 r = compile_expr0(&p, cctx);
5049 if (new_local)
5050 ++cctx->ctx_locals.ga_len;
5051 if (r == FAIL)
5052 goto theend;
5053 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005054 else if (semicolon && var_idx == var_count - 1)
5055 {
5056 // For "[var; var] = expr" get the rest of the list
5057 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5058 goto theend;
5059 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005060 else
5061 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005062 // For "[var, var] = expr" get the "var_idx" item from the
5063 // list.
5064 if (generate_GETITEM(cctx, var_idx) == FAIL)
5065 return FAIL;
5066 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005067
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005068 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005069 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005070 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005071 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005072 if ((stacktype->tt_type == VAR_FUNC
5073 || stacktype->tt_type == VAR_PARTIAL)
5074 && var_wrong_func_name(name, TRUE))
5075 goto theend;
5076
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005077 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005078 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005079 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005080 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005081 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005082 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005083 }
5084 else
5085 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005086 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005087 // for a variable that implies &t_any.
5088 if (stacktype == &t_list_empty)
5089 lvar->lv_type = &t_list_any;
5090 else if (stacktype == &t_dict_empty)
5091 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005092 else if (stacktype == &t_unknown)
5093 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005094 else
5095 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005096 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005097 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005098 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005099 {
5100 type_T *use_type = lvar->lv_type;
5101
Bram Moolenaar71abe482020-09-14 22:28:30 +02005102 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005103 if (has_index)
5104 {
5105 use_type = use_type->tt_member;
5106 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005107 // could be indexing "any"
5108 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005109 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005110 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005111 == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005112 goto theend;
5113 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005114 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005115 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005116 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005117 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005118 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005119 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005120 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005121 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005122 goto theend;
5123 }
5124 else if (!has_type || dest == dest_option)
5125 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005126 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005127 goto theend;
5128 }
5129 else
5130 {
5131 // variables are always initialized
5132 if (ga_grow(instr, 1) == FAIL)
5133 goto theend;
5134 switch (member_type->tt_type)
5135 {
5136 case VAR_BOOL:
5137 generate_PUSHBOOL(cctx, VVAL_FALSE);
5138 break;
5139 case VAR_FLOAT:
5140#ifdef FEAT_FLOAT
5141 generate_PUSHF(cctx, 0.0);
5142#endif
5143 break;
5144 case VAR_STRING:
5145 generate_PUSHS(cctx, NULL);
5146 break;
5147 case VAR_BLOB:
5148 generate_PUSHBLOB(cctx, NULL);
5149 break;
5150 case VAR_FUNC:
5151 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5152 break;
5153 case VAR_LIST:
5154 generate_NEWLIST(cctx, 0);
5155 break;
5156 case VAR_DICT:
5157 generate_NEWDICT(cctx, 0);
5158 break;
5159 case VAR_JOB:
5160 generate_PUSHJOB(cctx, NULL);
5161 break;
5162 case VAR_CHANNEL:
5163 generate_PUSHCHANNEL(cctx, NULL);
5164 break;
5165 case VAR_NUMBER:
5166 case VAR_UNKNOWN:
5167 case VAR_ANY:
5168 case VAR_PARTIAL:
5169 case VAR_VOID:
5170 case VAR_SPECIAL: // cannot happen
5171 generate_PUSHNR(cctx, 0);
5172 break;
5173 }
5174 }
5175 if (var_count == 0)
5176 end = p;
5177 }
5178
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005179 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005180 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005181 break;
5182
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005183 if (oplen > 0 && *op != '=')
5184 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005185 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005186 type_T *stacktype;
5187
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005188 if (*op == '.')
5189 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005190 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005191 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005192 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005193 if (
5194#ifdef FEAT_FLOAT
5195 // If variable is float operation with number is OK.
5196 !(expected == &t_float && stacktype == &t_number) &&
5197#endif
5198 need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005199 goto theend;
5200
5201 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005202 {
5203 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5204 goto theend;
5205 }
5206 else if (*op == '+')
5207 {
5208 if (generate_add_instr(cctx,
5209 operator_type(member_type, stacktype),
5210 member_type, stacktype) == FAIL)
5211 goto theend;
5212 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005213 else if (generate_two_op(cctx, op) == FAIL)
5214 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005215 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005216
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005217 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005218 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005219 int r;
5220
5221 // Compile the "idx" in "var[idx]".
5222 if (new_local)
5223 --cctx->ctx_locals.ga_len;
5224 p = skipwhite(var_start + varlen + 1);
5225 r = compile_expr0(&p, cctx);
5226 if (new_local)
5227 ++cctx->ctx_locals.ga_len;
5228 if (r == FAIL)
5229 goto theend;
5230 if (*skipwhite(p) != ']')
5231 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005232 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005233 emsg(_(e_missbrac));
5234 goto theend;
5235 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005236 if (type == &t_any)
5237 {
5238 type_T *idx_type = ((type_T **)stack->ga_data)[
5239 stack->ga_len - 1];
5240 // Index on variable of unknown type: guess the type from the
5241 // index type: number is dict, otherwise dict.
5242 // TODO: should do the assignment at runtime
5243 if (idx_type->tt_type == VAR_NUMBER)
5244 type = &t_list_any;
5245 else
5246 type = &t_dict_any;
5247 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005248 if (type->tt_type == VAR_DICT
5249 && may_generate_2STRING(-1, cctx) == FAIL)
5250 goto theend;
5251 if (type->tt_type == VAR_LIST
5252 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005253 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005254 {
5255 emsg(_(e_number_exp));
5256 goto theend;
5257 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005258
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005259 // Load the dict or list. On the stack we then have:
5260 // - value
5261 // - index
5262 // - variable
5263 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005264
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005265 if (type->tt_type == VAR_LIST)
5266 {
5267 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5268 return FAIL;
5269 }
5270 else if (type->tt_type == VAR_DICT)
5271 {
5272 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5273 return FAIL;
5274 }
5275 else
5276 {
5277 emsg(_(e_listreq));
5278 goto theend;
5279 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005280 }
5281 else
5282 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005283 if (is_decl && eap->forceit && cmdidx == CMD_const
5284 && (dest == dest_script || dest == dest_local))
5285 // ":const! var": lock the value, but not referenced variables
5286 generate_LOCKCONST(cctx);
5287
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005288 switch (dest)
5289 {
5290 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005291 generate_STOREOPT(cctx, skip_option_env_lead(name),
5292 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005293 break;
5294 case dest_global:
5295 // include g: with the name, easier to execute that way
5296 generate_STORE(cctx, ISN_STOREG, 0, name);
5297 break;
5298 case dest_buffer:
5299 // include b: with the name, easier to execute that way
5300 generate_STORE(cctx, ISN_STOREB, 0, name);
5301 break;
5302 case dest_window:
5303 // include w: with the name, easier to execute that way
5304 generate_STORE(cctx, ISN_STOREW, 0, name);
5305 break;
5306 case dest_tab:
5307 // include t: with the name, easier to execute that way
5308 generate_STORE(cctx, ISN_STORET, 0, name);
5309 break;
5310 case dest_env:
5311 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5312 break;
5313 case dest_reg:
5314 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5315 break;
5316 case dest_vimvar:
5317 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5318 break;
5319 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005320 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005321 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005322 {
5323 char_u *name_s = name;
5324
5325 // Include s: in the name for store_var()
5326 if (name[1] != ':')
5327 {
5328 int len = (int)STRLEN(name) + 3;
5329
5330 name_s = alloc(len);
5331 if (name_s == NULL)
5332 name_s = name;
5333 else
5334 vim_snprintf((char *)name_s, len,
5335 "s:%s", name);
5336 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005337 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5338 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005339 if (name_s != name)
5340 vim_free(name_s);
5341 }
5342 else
5343 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005344 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005345 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005346 break;
5347 case dest_local:
5348 if (lvar != NULL)
5349 {
5350 isn_T *isn = ((isn_T *)instr->ga_data)
5351 + instr->ga_len - 1;
5352
5353 // optimization: turn "var = 123" from ISN_PUSHNR +
5354 // ISN_STORE into ISN_STORENR
5355 if (!lvar->lv_from_outer
5356 && instr->ga_len == instr_count + 1
5357 && isn->isn_type == ISN_PUSHNR)
5358 {
5359 varnumber_T val = isn->isn_arg.number;
5360
5361 isn->isn_type = ISN_STORENR;
5362 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5363 isn->isn_arg.storenr.stnr_val = val;
5364 if (stack->ga_len > 0)
5365 --stack->ga_len;
5366 }
5367 else if (lvar->lv_from_outer)
5368 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005369 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005370 else
5371 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5372 }
5373 break;
5374 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005375 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005376
5377 if (var_idx + 1 < var_count)
5378 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005379 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005380
5381 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005382 if (var_count > 0 && !semicolon)
5383 {
5384 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5385 goto theend;
5386 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005387
Bram Moolenaarb2097502020-07-19 17:17:02 +02005388 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005389
5390theend:
5391 vim_free(name);
5392 return ret;
5393}
5394
5395/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005396 * Check if "name" can be "unlet".
5397 */
5398 int
5399check_vim9_unlet(char_u *name)
5400{
5401 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5402 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005403 // "unlet s:var" is allowed in legacy script.
5404 if (*name == 's' && !script_is_vim9())
5405 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005406 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005407 return FAIL;
5408 }
5409 return OK;
5410}
5411
5412/*
5413 * Callback passed to ex_unletlock().
5414 */
5415 static int
5416compile_unlet(
5417 lval_T *lvp,
5418 char_u *name_end,
5419 exarg_T *eap,
5420 int deep UNUSED,
5421 void *coookie)
5422{
5423 cctx_T *cctx = coookie;
5424
5425 if (lvp->ll_tv == NULL)
5426 {
5427 char_u *p = lvp->ll_name;
5428 int cc = *name_end;
5429 int ret = OK;
5430
5431 // Normal name. Only supports g:, w:, t: and b: namespaces.
5432 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005433 if (*p == '$')
5434 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5435 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005436 ret = FAIL;
5437 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005438 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005439
5440 *name_end = cc;
5441 return ret;
5442 }
5443
5444 // TODO: unlet {list}[idx]
5445 // TODO: unlet {dict}[key]
5446 emsg("Sorry, :unlet not fully implemented yet");
5447 return FAIL;
5448}
5449
5450/*
5451 * compile "unlet var", "lock var" and "unlock var"
5452 * "arg" points to "var".
5453 */
5454 static char_u *
5455compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5456{
5457 char_u *p = arg;
5458
5459 if (eap->cmdidx != CMD_unlet)
5460 {
5461 emsg("Sorry, :lock and unlock not implemented yet");
5462 return NULL;
5463 }
5464
5465 if (*p == '!')
5466 {
5467 p = skipwhite(p + 1);
5468 eap->forceit = TRUE;
5469 }
5470
5471 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5472 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5473}
5474
5475/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005476 * Compile an :import command.
5477 */
5478 static char_u *
5479compile_import(char_u *arg, cctx_T *cctx)
5480{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005481 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005482}
5483
5484/*
5485 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5486 */
5487 static int
5488compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5489{
5490 garray_T *instr = &cctx->ctx_instr;
5491 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5492
5493 if (endlabel == NULL)
5494 return FAIL;
5495 endlabel->el_next = *el;
5496 *el = endlabel;
5497 endlabel->el_end_label = instr->ga_len;
5498
5499 generate_JUMP(cctx, when, 0);
5500 return OK;
5501}
5502
5503 static void
5504compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5505{
5506 garray_T *instr = &cctx->ctx_instr;
5507
5508 while (*el != NULL)
5509 {
5510 endlabel_T *cur = (*el);
5511 isn_T *isn;
5512
5513 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5514 isn->isn_arg.jump.jump_where = instr->ga_len;
5515 *el = cur->el_next;
5516 vim_free(cur);
5517 }
5518}
5519
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005520 static void
5521compile_free_jump_to_end(endlabel_T **el)
5522{
5523 while (*el != NULL)
5524 {
5525 endlabel_T *cur = (*el);
5526
5527 *el = cur->el_next;
5528 vim_free(cur);
5529 }
5530}
5531
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005532/*
5533 * Create a new scope and set up the generic items.
5534 */
5535 static scope_T *
5536new_scope(cctx_T *cctx, scopetype_T type)
5537{
5538 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5539
5540 if (scope == NULL)
5541 return NULL;
5542 scope->se_outer = cctx->ctx_scope;
5543 cctx->ctx_scope = scope;
5544 scope->se_type = type;
5545 scope->se_local_count = cctx->ctx_locals.ga_len;
5546 return scope;
5547}
5548
5549/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005550 * Free the current scope and go back to the outer scope.
5551 */
5552 static void
5553drop_scope(cctx_T *cctx)
5554{
5555 scope_T *scope = cctx->ctx_scope;
5556
5557 if (scope == NULL)
5558 {
5559 iemsg("calling drop_scope() without a scope");
5560 return;
5561 }
5562 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005563 switch (scope->se_type)
5564 {
5565 case IF_SCOPE:
5566 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5567 case FOR_SCOPE:
5568 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5569 case WHILE_SCOPE:
5570 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5571 case TRY_SCOPE:
5572 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5573 case NO_SCOPE:
5574 case BLOCK_SCOPE:
5575 break;
5576 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005577 vim_free(scope);
5578}
5579
5580/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005581 * compile "if expr"
5582 *
5583 * "if expr" Produces instructions:
5584 * EVAL expr Push result of "expr"
5585 * JUMP_IF_FALSE end
5586 * ... body ...
5587 * end:
5588 *
5589 * "if expr | else" Produces instructions:
5590 * EVAL expr Push result of "expr"
5591 * JUMP_IF_FALSE else
5592 * ... body ...
5593 * JUMP_ALWAYS end
5594 * else:
5595 * ... body ...
5596 * end:
5597 *
5598 * "if expr1 | elseif expr2 | else" Produces instructions:
5599 * EVAL expr Push result of "expr"
5600 * JUMP_IF_FALSE elseif
5601 * ... body ...
5602 * JUMP_ALWAYS end
5603 * elseif:
5604 * EVAL expr Push result of "expr"
5605 * JUMP_IF_FALSE else
5606 * ... body ...
5607 * JUMP_ALWAYS end
5608 * else:
5609 * ... body ...
5610 * end:
5611 */
5612 static char_u *
5613compile_if(char_u *arg, cctx_T *cctx)
5614{
5615 char_u *p = arg;
5616 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005617 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005618 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005619 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005620 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005621
Bram Moolenaara5565e42020-05-09 15:44:01 +02005622 CLEAR_FIELD(ppconst);
5623 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005624 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005625 clear_ppconst(&ppconst);
5626 return NULL;
5627 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005628 if (cctx->ctx_skip == SKIP_YES)
5629 clear_ppconst(&ppconst);
5630 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005631 {
5632 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005633 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005634 clear_ppconst(&ppconst);
5635 }
5636 else
5637 {
5638 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005639 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005640 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005641 return NULL;
5642 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005643
5644 scope = new_scope(cctx, IF_SCOPE);
5645 if (scope == NULL)
5646 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005647 scope->se_skip_save = skip_save;
5648 // "is_had_return" will be reset if any block does not end in :return
5649 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005650
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005651 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005652 {
5653 // "where" is set when ":elseif", "else" or ":endif" is found
5654 scope->se_u.se_if.is_if_label = instr->ga_len;
5655 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5656 }
5657 else
5658 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005659
5660 return p;
5661}
5662
5663 static char_u *
5664compile_elseif(char_u *arg, cctx_T *cctx)
5665{
5666 char_u *p = arg;
5667 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005668 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005669 isn_T *isn;
5670 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005671 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02005672 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005673
5674 if (scope == NULL || scope->se_type != IF_SCOPE)
5675 {
5676 emsg(_(e_elseif_without_if));
5677 return NULL;
5678 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005679 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005680 if (!cctx->ctx_had_return)
5681 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005682
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005683 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005684 {
5685 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005686 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005687 return NULL;
5688 // previous "if" or "elseif" jumps here
5689 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5690 isn->isn_arg.jump.jump_where = instr->ga_len;
5691 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005692
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005693 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005694 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02005695 if (cctx->ctx_skip == SKIP_YES)
5696 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005697 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005698 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005699 clear_ppconst(&ppconst);
5700 return NULL;
5701 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02005702 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005703 if (scope->se_skip_save == SKIP_YES)
5704 clear_ppconst(&ppconst);
5705 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005706 {
5707 // The expression results in a constant.
5708 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005709 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005710 clear_ppconst(&ppconst);
5711 scope->se_u.se_if.is_if_label = -1;
5712 }
5713 else
5714 {
5715 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005716 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005717 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005718 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005719
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005720 // "where" is set when ":elseif", "else" or ":endif" is found
5721 scope->se_u.se_if.is_if_label = instr->ga_len;
5722 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5723 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005724
5725 return p;
5726}
5727
5728 static char_u *
5729compile_else(char_u *arg, cctx_T *cctx)
5730{
5731 char_u *p = arg;
5732 garray_T *instr = &cctx->ctx_instr;
5733 isn_T *isn;
5734 scope_T *scope = cctx->ctx_scope;
5735
5736 if (scope == NULL || scope->se_type != IF_SCOPE)
5737 {
5738 emsg(_(e_else_without_if));
5739 return NULL;
5740 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005741 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005742 if (!cctx->ctx_had_return)
5743 scope->se_u.se_if.is_had_return = FALSE;
5744 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005745
Bram Moolenaarefd88552020-06-18 20:50:10 +02005746 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005747 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005748 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005749 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005750 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005751 if (!cctx->ctx_had_return
5752 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5753 JUMP_ALWAYS, cctx) == FAIL)
5754 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005755 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005756
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005757 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005758 {
5759 if (scope->se_u.se_if.is_if_label >= 0)
5760 {
5761 // previous "if" or "elseif" jumps here
5762 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5763 isn->isn_arg.jump.jump_where = instr->ga_len;
5764 scope->se_u.se_if.is_if_label = -1;
5765 }
5766 }
5767
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005768 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005769 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5770 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005771
5772 return p;
5773}
5774
5775 static char_u *
5776compile_endif(char_u *arg, cctx_T *cctx)
5777{
5778 scope_T *scope = cctx->ctx_scope;
5779 ifscope_T *ifscope;
5780 garray_T *instr = &cctx->ctx_instr;
5781 isn_T *isn;
5782
5783 if (scope == NULL || scope->se_type != IF_SCOPE)
5784 {
5785 emsg(_(e_endif_without_if));
5786 return NULL;
5787 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005788 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005789 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005790 if (!cctx->ctx_had_return)
5791 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005792
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005793 if (scope->se_u.se_if.is_if_label >= 0)
5794 {
5795 // previous "if" or "elseif" jumps here
5796 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5797 isn->isn_arg.jump.jump_where = instr->ga_len;
5798 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005799 // Fill in the "end" label in jumps at the end of the blocks.
5800 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005801 cctx->ctx_skip = scope->se_skip_save;
5802
5803 // If all the blocks end in :return and there is an :else then the
5804 // had_return flag is set.
5805 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005806
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005807 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005808 return arg;
5809}
5810
5811/*
5812 * compile "for var in expr"
5813 *
5814 * Produces instructions:
5815 * PUSHNR -1
5816 * STORE loop-idx Set index to -1
5817 * EVAL expr Push result of "expr"
5818 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5819 * - if beyond end, jump to "end"
5820 * - otherwise get item from list and push it
5821 * STORE var Store item in "var"
5822 * ... body ...
5823 * JUMP top Jump back to repeat
5824 * end: DROP Drop the result of "expr"
5825 *
5826 */
5827 static char_u *
5828compile_for(char_u *arg, cctx_T *cctx)
5829{
5830 char_u *p;
5831 size_t varlen;
5832 garray_T *instr = &cctx->ctx_instr;
5833 garray_T *stack = &cctx->ctx_type_stack;
5834 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005835 lvar_T *loop_lvar; // loop iteration variable
5836 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005837 type_T *vartype;
5838
5839 // TODO: list of variables: "for [key, value] in dict"
5840 // parse "var"
5841 for (p = arg; eval_isnamec1(*p); ++p)
5842 ;
5843 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005844 var_lvar = lookup_local(arg, varlen, cctx);
5845 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005846 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005847 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005848 return NULL;
5849 }
5850
5851 // consume "in"
5852 p = skipwhite(p);
5853 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5854 {
5855 emsg(_(e_missing_in));
5856 return NULL;
5857 }
5858 p = skipwhite(p + 2);
5859
5860
5861 scope = new_scope(cctx, FOR_SCOPE);
5862 if (scope == NULL)
5863 return NULL;
5864
5865 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005866 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5867 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005868 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005869 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005870 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005871 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005872 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005873
5874 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005875 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5876 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005877 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005878 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005879 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005880 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005881 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005882
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005883 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005884
5885 // compile "expr", it remains on the stack until "endfor"
5886 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005887 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005888 {
5889 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005890 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005891 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005892
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005893 // Now that we know the type of "var", check that it is a list, now or at
5894 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005895 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005896 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005897 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005898 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005899 return NULL;
5900 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005901 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005902 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005903
5904 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005905 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005906
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005907 generate_FOR(cctx, loop_lvar->lv_idx);
5908 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005909
5910 return arg;
5911}
5912
5913/*
5914 * compile "endfor"
5915 */
5916 static char_u *
5917compile_endfor(char_u *arg, cctx_T *cctx)
5918{
5919 garray_T *instr = &cctx->ctx_instr;
5920 scope_T *scope = cctx->ctx_scope;
5921 forscope_T *forscope;
5922 isn_T *isn;
5923
5924 if (scope == NULL || scope->se_type != FOR_SCOPE)
5925 {
5926 emsg(_(e_for));
5927 return NULL;
5928 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005929 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005930 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005931 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005932
5933 // At end of ":for" scope jump back to the FOR instruction.
5934 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5935
5936 // Fill in the "end" label in the FOR statement so it can jump here
5937 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5938 isn->isn_arg.forloop.for_end = instr->ga_len;
5939
5940 // Fill in the "end" label any BREAK statements
5941 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5942
5943 // Below the ":for" scope drop the "expr" list from the stack.
5944 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5945 return NULL;
5946
5947 vim_free(scope);
5948
5949 return arg;
5950}
5951
5952/*
5953 * compile "while expr"
5954 *
5955 * Produces instructions:
5956 * top: EVAL expr Push result of "expr"
5957 * JUMP_IF_FALSE end jump if false
5958 * ... body ...
5959 * JUMP top Jump back to repeat
5960 * end:
5961 *
5962 */
5963 static char_u *
5964compile_while(char_u *arg, cctx_T *cctx)
5965{
5966 char_u *p = arg;
5967 garray_T *instr = &cctx->ctx_instr;
5968 scope_T *scope;
5969
5970 scope = new_scope(cctx, WHILE_SCOPE);
5971 if (scope == NULL)
5972 return NULL;
5973
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005974 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005975
5976 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005977 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005978 return NULL;
5979
5980 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005981 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005982 JUMP_IF_FALSE, cctx) == FAIL)
5983 return FAIL;
5984
5985 return p;
5986}
5987
5988/*
5989 * compile "endwhile"
5990 */
5991 static char_u *
5992compile_endwhile(char_u *arg, cctx_T *cctx)
5993{
5994 scope_T *scope = cctx->ctx_scope;
5995
5996 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5997 {
5998 emsg(_(e_while));
5999 return NULL;
6000 }
6001 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006002 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006003
6004 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006005 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006006
6007 // Fill in the "end" label in the WHILE statement so it can jump here.
6008 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006009 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006010
6011 vim_free(scope);
6012
6013 return arg;
6014}
6015
6016/*
6017 * compile "continue"
6018 */
6019 static char_u *
6020compile_continue(char_u *arg, cctx_T *cctx)
6021{
6022 scope_T *scope = cctx->ctx_scope;
6023
6024 for (;;)
6025 {
6026 if (scope == NULL)
6027 {
6028 emsg(_(e_continue));
6029 return NULL;
6030 }
6031 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6032 break;
6033 scope = scope->se_outer;
6034 }
6035
6036 // Jump back to the FOR or WHILE instruction.
6037 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006038 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6039 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006040 return arg;
6041}
6042
6043/*
6044 * compile "break"
6045 */
6046 static char_u *
6047compile_break(char_u *arg, cctx_T *cctx)
6048{
6049 scope_T *scope = cctx->ctx_scope;
6050 endlabel_T **el;
6051
6052 for (;;)
6053 {
6054 if (scope == NULL)
6055 {
6056 emsg(_(e_break));
6057 return NULL;
6058 }
6059 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6060 break;
6061 scope = scope->se_outer;
6062 }
6063
6064 // Jump to the end of the FOR or WHILE loop.
6065 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006066 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006067 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006068 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006069 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6070 return FAIL;
6071
6072 return arg;
6073}
6074
6075/*
6076 * compile "{" start of block
6077 */
6078 static char_u *
6079compile_block(char_u *arg, cctx_T *cctx)
6080{
6081 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6082 return NULL;
6083 return skipwhite(arg + 1);
6084}
6085
6086/*
6087 * compile end of block: drop one scope
6088 */
6089 static void
6090compile_endblock(cctx_T *cctx)
6091{
6092 scope_T *scope = cctx->ctx_scope;
6093
6094 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006095 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006096 vim_free(scope);
6097}
6098
6099/*
6100 * compile "try"
6101 * Creates a new scope for the try-endtry, pointing to the first catch and
6102 * finally.
6103 * Creates another scope for the "try" block itself.
6104 * TRY instruction sets up exception handling at runtime.
6105 *
6106 * "try"
6107 * TRY -> catch1, -> finally push trystack entry
6108 * ... try block
6109 * "throw {exception}"
6110 * EVAL {exception}
6111 * THROW create exception
6112 * ... try block
6113 * " catch {expr}"
6114 * JUMP -> finally
6115 * catch1: PUSH exeception
6116 * EVAL {expr}
6117 * MATCH
6118 * JUMP nomatch -> catch2
6119 * CATCH remove exception
6120 * ... catch block
6121 * " catch"
6122 * JUMP -> finally
6123 * catch2: CATCH remove exception
6124 * ... catch block
6125 * " finally"
6126 * finally:
6127 * ... finally block
6128 * " endtry"
6129 * ENDTRY pop trystack entry, may rethrow
6130 */
6131 static char_u *
6132compile_try(char_u *arg, cctx_T *cctx)
6133{
6134 garray_T *instr = &cctx->ctx_instr;
6135 scope_T *try_scope;
6136 scope_T *scope;
6137
6138 // scope that holds the jumps that go to catch/finally/endtry
6139 try_scope = new_scope(cctx, TRY_SCOPE);
6140 if (try_scope == NULL)
6141 return NULL;
6142
6143 // "catch" is set when the first ":catch" is found.
6144 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006145 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006146 if (generate_instr(cctx, ISN_TRY) == NULL)
6147 return NULL;
6148
6149 // scope for the try block itself
6150 scope = new_scope(cctx, BLOCK_SCOPE);
6151 if (scope == NULL)
6152 return NULL;
6153
6154 return arg;
6155}
6156
6157/*
6158 * compile "catch {expr}"
6159 */
6160 static char_u *
6161compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6162{
6163 scope_T *scope = cctx->ctx_scope;
6164 garray_T *instr = &cctx->ctx_instr;
6165 char_u *p;
6166 isn_T *isn;
6167
6168 // end block scope from :try or :catch
6169 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6170 compile_endblock(cctx);
6171 scope = cctx->ctx_scope;
6172
6173 // Error if not in a :try scope
6174 if (scope == NULL || scope->se_type != TRY_SCOPE)
6175 {
6176 emsg(_(e_catch));
6177 return NULL;
6178 }
6179
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006180 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006181 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006182 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006183 return NULL;
6184 }
6185
6186 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006187 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006188 JUMP_ALWAYS, cctx) == FAIL)
6189 return NULL;
6190
6191 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006192 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006193 if (isn->isn_arg.try.try_catch == 0)
6194 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006195 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006196 {
6197 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006198 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006199 isn->isn_arg.jump.jump_where = instr->ga_len;
6200 }
6201
6202 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006203 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006204 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006205 scope->se_u.se_try.ts_caught_all = TRUE;
6206 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006207 }
6208 else
6209 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006210 char_u *end;
6211 char_u *pat;
6212 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006213 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006214 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006215
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006216 // Push v:exception, push {expr} and MATCH
6217 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6218
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006219 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006220 if (*end != *p)
6221 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006222 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006223 vim_free(tofree);
6224 return FAIL;
6225 }
6226 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006227 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006228 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006229 len = (int)(end - tofree);
6230 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006231 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006232 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006233 if (pat == NULL)
6234 return FAIL;
6235 if (generate_PUSHS(cctx, pat) == FAIL)
6236 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006237
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006238 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6239 return NULL;
6240
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006241 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006242 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6243 return NULL;
6244 }
6245
6246 if (generate_instr(cctx, ISN_CATCH) == NULL)
6247 return NULL;
6248
6249 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6250 return NULL;
6251 return p;
6252}
6253
6254 static char_u *
6255compile_finally(char_u *arg, cctx_T *cctx)
6256{
6257 scope_T *scope = cctx->ctx_scope;
6258 garray_T *instr = &cctx->ctx_instr;
6259 isn_T *isn;
6260
6261 // end block scope from :try or :catch
6262 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6263 compile_endblock(cctx);
6264 scope = cctx->ctx_scope;
6265
6266 // Error if not in a :try scope
6267 if (scope == NULL || scope->se_type != TRY_SCOPE)
6268 {
6269 emsg(_(e_finally));
6270 return NULL;
6271 }
6272
6273 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006274 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006275 if (isn->isn_arg.try.try_finally != 0)
6276 {
6277 emsg(_(e_finally_dup));
6278 return NULL;
6279 }
6280
6281 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006282 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006283
Bram Moolenaar585fea72020-04-02 22:33:21 +02006284 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006285 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006286 {
6287 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006288 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006289 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006290 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006291 }
6292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006293 // TODO: set index in ts_finally_label jumps
6294
6295 return arg;
6296}
6297
6298 static char_u *
6299compile_endtry(char_u *arg, cctx_T *cctx)
6300{
6301 scope_T *scope = cctx->ctx_scope;
6302 garray_T *instr = &cctx->ctx_instr;
6303 isn_T *isn;
6304
6305 // end block scope from :catch or :finally
6306 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6307 compile_endblock(cctx);
6308 scope = cctx->ctx_scope;
6309
6310 // Error if not in a :try scope
6311 if (scope == NULL || scope->se_type != TRY_SCOPE)
6312 {
6313 if (scope == NULL)
6314 emsg(_(e_no_endtry));
6315 else if (scope->se_type == WHILE_SCOPE)
6316 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006317 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006318 emsg(_(e_endfor));
6319 else
6320 emsg(_(e_endif));
6321 return NULL;
6322 }
6323
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006324 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006325 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6326 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006327 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006328 return NULL;
6329 }
6330
6331 // Fill in the "end" label in jumps at the end of the blocks, if not done
6332 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006333 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006334
6335 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006336 if (isn->isn_arg.try.try_catch == 0)
6337 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006338 if (isn->isn_arg.try.try_finally == 0)
6339 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006340
6341 if (scope->se_u.se_try.ts_catch_label != 0)
6342 {
6343 // Last catch without match jumps here
6344 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6345 isn->isn_arg.jump.jump_where = instr->ga_len;
6346 }
6347
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006348 compile_endblock(cctx);
6349
6350 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6351 return NULL;
6352 return arg;
6353}
6354
6355/*
6356 * compile "throw {expr}"
6357 */
6358 static char_u *
6359compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6360{
6361 char_u *p = skipwhite(arg);
6362
Bram Moolenaara5565e42020-05-09 15:44:01 +02006363 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006364 return NULL;
6365 if (may_generate_2STRING(-1, cctx) == FAIL)
6366 return NULL;
6367 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6368 return NULL;
6369
6370 return p;
6371}
6372
6373/*
6374 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006375 * compile "echomsg expr"
6376 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006377 * compile "execute expr"
6378 */
6379 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006380compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006381{
6382 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006383 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006384 int count = 0;
6385
6386 for (;;)
6387 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006388 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006389 return NULL;
6390 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006391 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006392 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006393 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006394 break;
6395 }
6396
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006397 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6398 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6399 else if (cmdidx == CMD_execute)
6400 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6401 else if (cmdidx == CMD_echomsg)
6402 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6403 else
6404 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006405 return p;
6406}
6407
6408/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006409 * :put r
6410 * :put ={expr}
6411 */
6412 static char_u *
6413compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6414{
6415 char_u *line = arg;
6416 linenr_T lnum;
6417 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006418 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006419
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006420 eap->regname = *line;
6421
6422 if (eap->regname == '=')
6423 {
6424 char_u *p = line + 1;
6425
6426 if (compile_expr0(&p, cctx) == FAIL)
6427 return NULL;
6428 line = p;
6429 }
6430 else if (eap->regname != NUL)
6431 ++line;
6432
6433 // TODO: if the range is something like "$" need to evaluate at runtime
6434 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6435 return NULL;
6436 if (eap->addr_count == 0)
6437 lnum = -1;
6438 else
6439 lnum = eap->line2;
6440 if (above)
6441 --lnum;
6442
6443 generate_PUT(cctx, eap->regname, lnum);
6444 return line;
6445}
6446
6447/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006448 * A command that is not compiled, execute with legacy code.
6449 */
6450 static char_u *
6451compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6452{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006453 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006454 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006455 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006456
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006457 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006458 goto theend;
6459
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006460 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006461 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006462 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006463 int usefilter = FALSE;
6464
6465 has_expr = argt & (EX_XFILE | EX_EXPAND);
6466
6467 // If the command can be followed by a bar, find the bar and truncate
6468 // it, so that the following command can be compiled.
6469 // The '|' is overwritten with a NUL, it is put back below.
6470 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6471 && *eap->arg == '!')
6472 // :w !filter or :r !filter or :r! filter
6473 usefilter = TRUE;
6474 if ((argt & EX_TRLBAR) && !usefilter)
6475 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006476 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006477 separate_nextcmd(eap);
6478 if (eap->nextcmd != NULL)
6479 nextcmd = eap->nextcmd;
6480 }
6481 }
6482
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006483 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6484 {
6485 // expand filename in "syntax include [@group] filename"
6486 has_expr = TRUE;
6487 eap->arg = skipwhite(eap->arg + 7);
6488 if (*eap->arg == '@')
6489 eap->arg = skiptowhite(eap->arg);
6490 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006491
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006492 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006493 {
6494 int count = 0;
6495 char_u *start = skipwhite(line);
6496
6497 // :cmd xxx`=expr1`yyy`=expr2`zzz
6498 // PUSHS ":cmd xxx"
6499 // eval expr1
6500 // PUSHS "yyy"
6501 // eval expr2
6502 // PUSHS "zzz"
6503 // EXECCONCAT 5
6504 for (;;)
6505 {
6506 if (p > start)
6507 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006508 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006509 ++count;
6510 }
6511 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006512 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006513 return NULL;
6514 may_generate_2STRING(-1, cctx);
6515 ++count;
6516 p = skipwhite(p);
6517 if (*p != '`')
6518 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006519 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006520 return NULL;
6521 }
6522 start = p + 1;
6523
6524 p = (char_u *)strstr((char *)start, "`=");
6525 if (p == NULL)
6526 {
6527 if (*skipwhite(start) != NUL)
6528 {
6529 generate_PUSHS(cctx, vim_strsave(start));
6530 ++count;
6531 }
6532 break;
6533 }
6534 }
6535 generate_EXECCONCAT(cctx, count);
6536 }
6537 else
6538 generate_EXEC(cctx, line);
6539
6540theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006541 if (*nextcmd != NUL)
6542 {
6543 // the parser expects a pointer to the bar, put it back
6544 --nextcmd;
6545 *nextcmd = '|';
6546 }
6547
6548 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006549}
6550
6551/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006552 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006553 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006554 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006555 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006556add_def_function(ufunc_T *ufunc)
6557{
6558 dfunc_T *dfunc;
6559
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006560 if (def_functions.ga_len == 0)
6561 {
6562 // The first position is not used, so that a zero uf_dfunc_idx means it
6563 // wasn't set.
6564 if (ga_grow(&def_functions, 1) == FAIL)
6565 return FAIL;
6566 ++def_functions.ga_len;
6567 }
6568
Bram Moolenaar09689a02020-05-09 22:50:08 +02006569 // Add the function to "def_functions".
6570 if (ga_grow(&def_functions, 1) == FAIL)
6571 return FAIL;
6572 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6573 CLEAR_POINTER(dfunc);
6574 dfunc->df_idx = def_functions.ga_len;
6575 ufunc->uf_dfunc_idx = dfunc->df_idx;
6576 dfunc->df_ufunc = ufunc;
6577 ++def_functions.ga_len;
6578 return OK;
6579}
6580
6581/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006582 * After ex_function() has collected all the function lines: parse and compile
6583 * the lines into instructions.
6584 * Adds the function to "def_functions".
6585 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6586 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006587 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006588 * This can be used recursively through compile_lambda(), which may reallocate
6589 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006590 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006591 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006592 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006593compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006594{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006595 char_u *line = NULL;
6596 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006597 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006598 cctx_T cctx;
6599 garray_T *instr;
6600 int called_emsg_before = called_emsg;
6601 int ret = FAIL;
6602 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006603 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006604 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006605 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006606
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006607 // When using a function that was compiled before: Free old instructions.
6608 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006609 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006610 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006611 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6612 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006613 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006614 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006615 else
6616 {
6617 if (add_def_function(ufunc) == FAIL)
6618 return FAIL;
6619 new_def_function = TRUE;
6620 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006621
Bram Moolenaar985116a2020-07-12 17:31:09 +02006622 ufunc->uf_def_status = UF_COMPILING;
6623
Bram Moolenaara80faa82020-04-12 19:37:17 +02006624 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006625 cctx.ctx_ufunc = ufunc;
6626 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006627 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006628 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6629 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6630 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6631 cctx.ctx_type_list = &ufunc->uf_type_list;
6632 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6633 instr = &cctx.ctx_instr;
6634
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006635 // Set the context to the function, it may be compiled when called from
6636 // another script. Set the script version to the most modern one.
6637 // The line number will be set in next_line_from_context().
6638 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006639 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6640
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006641 // Make sure error messages are OK.
6642 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6643 if (do_estack_push)
6644 estack_push_ufunc(ufunc, 1);
6645
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006646 if (ufunc->uf_def_args.ga_len > 0)
6647 {
6648 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006649 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006650 int i;
6651 char_u *arg;
6652 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006653 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006654
6655 // Produce instructions for the default values of optional arguments.
6656 // Store the instruction index in uf_def_arg_idx[] so that we know
6657 // where to start when the function is called, depending on the number
6658 // of arguments.
6659 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6660 if (ufunc->uf_def_arg_idx == NULL)
6661 goto erret;
6662 for (i = 0; i < count; ++i)
6663 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006664 garray_T *stack = &cctx.ctx_type_stack;
6665 type_T *val_type;
6666 int arg_idx = first_def_arg + i;
6667
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006668 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6669 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006670 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006671 goto erret;
6672
6673 // If no type specified use the type of the default value.
6674 // Otherwise check that the default value type matches the
6675 // specified type.
6676 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6677 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006678 {
6679 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006680 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006681 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02006682 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
6683 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006684 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006685
6686 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006687 goto erret;
6688 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006689 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006690
6691 if (did_set_arg_type)
6692 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006693 }
6694
6695 /*
6696 * Loop over all the lines of the function and generate instructions.
6697 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006698 for (;;)
6699 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006700 exarg_T ea;
6701 cmdmod_T save_cmdmod;
6702 int starts_with_colon = FALSE;
6703 char_u *cmd;
6704 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006705
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006706 // Bail out on the first error to avoid a flood of errors and report
6707 // the right line number when inside try/catch.
6708 if (emsg_before != called_emsg)
6709 goto erret;
6710
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006711 if (line != NULL && *line == '|')
6712 // the line continues after a '|'
6713 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006714 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006715 && !(*line == '#' && (line == cctx.ctx_line_start
6716 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006717 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006718 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006719 goto erret;
6720 }
6721 else
6722 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006723 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006724 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006725 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006726 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006727 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006728 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006729
Bram Moolenaara80faa82020-04-12 19:37:17 +02006730 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006731 ea.cmdlinep = &line;
6732 ea.cmd = skipwhite(line);
6733
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006734 // Some things can be recognized by the first character.
6735 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006736 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006737 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006738 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006739 if (ea.cmd[1] != '{')
6740 {
6741 line = (char_u *)"";
6742 continue;
6743 }
6744 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006745
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006746 case '}':
6747 {
6748 // "}" ends a block scope
6749 scopetype_T stype = cctx.ctx_scope == NULL
6750 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006751
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006752 if (stype == BLOCK_SCOPE)
6753 {
6754 compile_endblock(&cctx);
6755 line = ea.cmd;
6756 }
6757 else
6758 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006759 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006760 goto erret;
6761 }
6762 if (line != NULL)
6763 line = skipwhite(ea.cmd + 1);
6764 continue;
6765 }
6766
6767 case '{':
6768 // "{" starts a block scope
6769 // "{'a': 1}->func() is something else
6770 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6771 {
6772 line = compile_block(ea.cmd, &cctx);
6773 continue;
6774 }
6775 break;
6776
6777 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006778 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006779 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006780 }
6781
6782 /*
6783 * COMMAND MODIFIERS
6784 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006785 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006786 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6787 {
6788 if (errormsg != NULL)
6789 goto erret;
6790 // empty line or comment
6791 line = (char_u *)"";
6792 continue;
6793 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006794 // TODO: use modifiers in the command
6795 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006796 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006797
6798 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006799 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006800 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006801 {
6802 if (*ea.cmd == '(')
6803 // not for "call()"
6804 ea.cmd = p;
6805 else
6806 ea.cmd = skipwhite(ea.cmd);
6807 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006808
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006809 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006810 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006811 char_u *pskip;
6812
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006813 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006814 // find what follows.
6815 // Skip over "var.member", "var[idx]" and the like.
6816 // Also "&opt = val", "$ENV = val" and "@r = val".
6817 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006818 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006819 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006820 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006821 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006822 char_u *var_end;
6823 int oplen;
6824 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006825
Bram Moolenaar65821722020-08-02 18:58:54 +02006826 if (ea.cmd[0] == '@')
6827 var_end = ea.cmd + 2;
6828 else
6829 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006830 FNE_CHECK_START | FNE_INCL_BR);
6831 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006832 if (oplen > 0)
6833 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006834 size_t len = p - ea.cmd;
6835
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006836 // Recognize an assignment if we recognize the variable
6837 // name:
6838 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006839 // "local = expr" where "local" is a local var.
6840 // "script = expr" where "script" is a script-local var.
6841 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006842 // "&opt = expr"
6843 // "$ENV = expr"
6844 // "@r = expr"
6845 if (*ea.cmd == '&'
6846 || *ea.cmd == '$'
6847 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006848 || ((len) > 2 && ea.cmd[1] == ':')
6849 || lookup_local(ea.cmd, len, &cctx) != NULL
6850 || lookup_arg(ea.cmd, len, NULL, NULL,
6851 NULL, &cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02006852 || lookup_script(ea.cmd, len, FALSE) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006853 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006854 {
6855 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006856 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006857 goto erret;
6858 continue;
6859 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006860 }
6861 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006862
6863 if (*ea.cmd == '[')
6864 {
6865 // [var, var] = expr
6866 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6867 if (line == NULL)
6868 goto erret;
6869 if (line != ea.cmd)
6870 continue;
6871 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006872 }
6873
6874 /*
6875 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006876 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006877 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006878 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006879 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006880 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02006881 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006882 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006883 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006884 if (!starts_with_colon)
6885 {
6886 emsg(_(e_colon_required_before_a_range));
6887 goto erret;
6888 }
6889 if (ends_excmd2(line, ea.cmd))
6890 {
6891 // A range without a command: jump to the line.
6892 // TODO: compile to a more efficient command, possibly
6893 // calling parse_cmd_address().
6894 ea.cmdidx = CMD_SIZE;
6895 line = compile_exec(line, &ea, &cctx);
6896 goto nextline;
6897 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006898 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006899 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006900 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006901 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006902 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006903
6904 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6905 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006906 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006907 {
6908 line += STRLEN(line);
6909 continue;
6910 }
6911
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006912 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006913 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006914 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006915 // CMD_let cannot happen, compile_assignment() above is used
6916 iemsg("Command from find_ex_command() not handled");
6917 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006918 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006919 }
6920
Bram Moolenaar3988f642020-08-27 22:43:03 +02006921 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006922 && ea.cmdidx != CMD_elseif
6923 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02006924 && ea.cmdidx != CMD_endif
6925 && ea.cmdidx != CMD_endfor
6926 && ea.cmdidx != CMD_endwhile
6927 && ea.cmdidx != CMD_catch
6928 && ea.cmdidx != CMD_finally
6929 && ea.cmdidx != CMD_endtry)
6930 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02006931 emsg(_(e_unreachable_code_after_return));
6932 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006933 }
6934
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006935 p = skipwhite(p);
6936 if (ea.cmdidx != CMD_SIZE
6937 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
6938 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02006939 if (ea.cmdidx >= 0)
6940 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006941 if ((ea.argt & EX_BANG) && *p == '!')
6942 {
6943 ea.forceit = TRUE;
6944 p = skipwhite(p + 1);
6945 }
6946 }
6947
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006948 switch (ea.cmdidx)
6949 {
6950 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006951 ea.arg = p;
6952 line = compile_nested_function(&ea, &cctx);
6953 break;
6954
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006955 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006956 // TODO: should we allow this, e.g. to declare a global
6957 // function?
6958 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006959 goto erret;
6960
6961 case CMD_return:
6962 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006963 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006964 break;
6965
6966 case CMD_let:
6967 case CMD_const:
6968 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006969 if (line == p)
6970 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006971 break;
6972
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006973 case CMD_unlet:
6974 case CMD_unlockvar:
6975 case CMD_lockvar:
6976 line = compile_unletlock(p, &ea, &cctx);
6977 break;
6978
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006979 case CMD_import:
6980 line = compile_import(p, &cctx);
6981 break;
6982
6983 case CMD_if:
6984 line = compile_if(p, &cctx);
6985 break;
6986 case CMD_elseif:
6987 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006988 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006989 break;
6990 case CMD_else:
6991 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006992 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006993 break;
6994 case CMD_endif:
6995 line = compile_endif(p, &cctx);
6996 break;
6997
6998 case CMD_while:
6999 line = compile_while(p, &cctx);
7000 break;
7001 case CMD_endwhile:
7002 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007003 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007004 break;
7005
7006 case CMD_for:
7007 line = compile_for(p, &cctx);
7008 break;
7009 case CMD_endfor:
7010 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007011 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007012 break;
7013 case CMD_continue:
7014 line = compile_continue(p, &cctx);
7015 break;
7016 case CMD_break:
7017 line = compile_break(p, &cctx);
7018 break;
7019
7020 case CMD_try:
7021 line = compile_try(p, &cctx);
7022 break;
7023 case CMD_catch:
7024 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007025 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007026 break;
7027 case CMD_finally:
7028 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007029 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007030 break;
7031 case CMD_endtry:
7032 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007033 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007034 break;
7035 case CMD_throw:
7036 line = compile_throw(p, &cctx);
7037 break;
7038
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007039 case CMD_eval:
7040 if (compile_expr0(&p, &cctx) == FAIL)
7041 goto erret;
7042
Bram Moolenaar3988f642020-08-27 22:43:03 +02007043 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007044 generate_instr_drop(&cctx, ISN_DROP, 1);
7045
7046 line = skipwhite(p);
7047 break;
7048
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007049 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007050 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007051 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007052 case CMD_echomsg:
7053 case CMD_echoerr:
7054 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007055 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007056
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007057 case CMD_put:
7058 ea.cmd = cmd;
7059 line = compile_put(p, &ea, &cctx);
7060 break;
7061
Bram Moolenaar3988f642020-08-27 22:43:03 +02007062 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007063
Bram Moolenaarae616492020-07-28 20:07:27 +02007064 case CMD_append:
7065 case CMD_change:
7066 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007067 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007068 case CMD_xit:
7069 not_in_vim9(&ea);
7070 goto erret;
7071
Bram Moolenaar002262f2020-07-08 17:47:57 +02007072 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007073 if (cctx.ctx_skip != SKIP_YES)
7074 {
7075 semsg(_(e_invalid_command_str), ea.cmd);
7076 goto erret;
7077 }
7078 // We don't check for a next command here.
7079 line = (char_u *)"";
7080 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007081
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007082 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007083 if (cctx.ctx_skip == SKIP_YES)
7084 {
7085 // We don't check for a next command here.
7086 line = (char_u *)"";
7087 }
7088 else
7089 {
7090 // Not recognized, execute with do_cmdline_cmd().
7091 ea.arg = p;
7092 line = compile_exec(line, &ea, &cctx);
7093 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007094 break;
7095 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007096nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007097 if (line == NULL)
7098 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007099 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007100
7101 if (cctx.ctx_type_stack.ga_len < 0)
7102 {
7103 iemsg("Type stack underflow");
7104 goto erret;
7105 }
7106 }
7107
7108 if (cctx.ctx_scope != NULL)
7109 {
7110 if (cctx.ctx_scope->se_type == IF_SCOPE)
7111 emsg(_(e_endif));
7112 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7113 emsg(_(e_endwhile));
7114 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7115 emsg(_(e_endfor));
7116 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007117 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007118 goto erret;
7119 }
7120
Bram Moolenaarefd88552020-06-18 20:50:10 +02007121 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007122 {
7123 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7124 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007125 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007126 goto erret;
7127 }
7128
7129 // Return zero if there is no return at the end.
7130 generate_PUSHNR(&cctx, 0);
7131 generate_instr(&cctx, ISN_RETURN);
7132 }
7133
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007134 {
7135 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7136 + ufunc->uf_dfunc_idx;
7137 dfunc->df_deleted = FALSE;
7138 dfunc->df_instr = instr->ga_data;
7139 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007140 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007141 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007142 if (cctx.ctx_outer_used)
7143 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007144 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007145 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007146
7147 ret = OK;
7148
7149erret:
7150 if (ret == FAIL)
7151 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007152 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007153 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7154 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007155
7156 for (idx = 0; idx < instr->ga_len; ++idx)
7157 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007158 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007159
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007160 // If using the last entry in the table and it was added above, we
7161 // might as well remove it.
7162 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007163 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007164 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007165 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007166 ufunc->uf_dfunc_idx = 0;
7167 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007168 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007169
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007170 while (cctx.ctx_scope != NULL)
7171 drop_scope(&cctx);
7172
Bram Moolenaar20431c92020-03-20 18:39:46 +01007173 // Don't execute this function body.
7174 ga_clear_strings(&ufunc->uf_lines);
7175
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007176 if (errormsg != NULL)
7177 emsg(errormsg);
7178 else if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007179 emsg(_(e_compile_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007180 }
7181
7182 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007183 if (do_estack_push)
7184 estack_pop();
7185
Bram Moolenaar20431c92020-03-20 18:39:46 +01007186 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007187 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007188 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007189 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007190}
7191
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007192 void
7193set_function_type(ufunc_T *ufunc)
7194{
7195 int varargs = ufunc->uf_va_name != NULL;
7196 int argcount = ufunc->uf_args.ga_len;
7197
7198 // Create a type for the function, with the return type and any
7199 // argument types.
7200 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7201 // The type is included in "tt_args".
7202 if (argcount > 0 || varargs)
7203 {
7204 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7205 argcount, &ufunc->uf_type_list);
7206 // Add argument types to the function type.
7207 if (func_type_add_arg_types(ufunc->uf_func_type,
7208 argcount + varargs,
7209 &ufunc->uf_type_list) == FAIL)
7210 return;
7211 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7212 ufunc->uf_func_type->tt_min_argcount =
7213 argcount - ufunc->uf_def_args.ga_len;
7214 if (ufunc->uf_arg_types == NULL)
7215 {
7216 int i;
7217
7218 // lambda does not have argument types.
7219 for (i = 0; i < argcount; ++i)
7220 ufunc->uf_func_type->tt_args[i] = &t_any;
7221 }
7222 else
7223 mch_memmove(ufunc->uf_func_type->tt_args,
7224 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7225 if (varargs)
7226 {
7227 ufunc->uf_func_type->tt_args[argcount] =
7228 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7229 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7230 }
7231 }
7232 else
7233 // No arguments, can use a predefined type.
7234 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7235 argcount, &ufunc->uf_type_list);
7236}
7237
7238
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007239/*
7240 * Delete an instruction, free what it contains.
7241 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007242 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007243delete_instr(isn_T *isn)
7244{
7245 switch (isn->isn_type)
7246 {
7247 case ISN_EXEC:
7248 case ISN_LOADENV:
7249 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007250 case ISN_LOADB:
7251 case ISN_LOADW:
7252 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007253 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007254 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007255 case ISN_PUSHEXC:
7256 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007257 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007258 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007259 case ISN_STOREB:
7260 case ISN_STOREW:
7261 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007262 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007263 vim_free(isn->isn_arg.string);
7264 break;
7265
7266 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007267 case ISN_STORES:
7268 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007269 break;
7270
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007271 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007272 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007273 vim_free(isn->isn_arg.unlet.ul_name);
7274 break;
7275
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007276 case ISN_STOREOPT:
7277 vim_free(isn->isn_arg.storeopt.so_name);
7278 break;
7279
7280 case ISN_PUSHBLOB: // push blob isn_arg.blob
7281 blob_unref(isn->isn_arg.blob);
7282 break;
7283
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007284 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007285#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007286 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007287#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007288 break;
7289
7290 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007291#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007292 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007293#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007294 break;
7295
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007296 case ISN_UCALL:
7297 vim_free(isn->isn_arg.ufunc.cuf_name);
7298 break;
7299
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007300 case ISN_FUNCREF:
7301 {
7302 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7303 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007304
7305 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7306 func_ptr_unref(dfunc->df_ufunc);
7307 }
7308 break;
7309
7310 case ISN_DCALL:
7311 {
7312 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7313 + isn->isn_arg.dfunc.cdf_idx;
7314
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007315 if (dfunc->df_ufunc != NULL
7316 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007317 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007318 }
7319 break;
7320
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007321 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007322 {
7323 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7324 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7325
7326 if (ufunc != NULL)
7327 {
7328 // Clear uf_dfunc_idx so that the function is deleted.
7329 clear_def_function(ufunc);
7330 ufunc->uf_dfunc_idx = 0;
7331 func_ptr_unref(ufunc);
7332 }
7333
7334 vim_free(lambda);
7335 vim_free(isn->isn_arg.newfunc.nf_global);
7336 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007337 break;
7338
Bram Moolenaar5e654232020-09-16 15:22:00 +02007339 case ISN_CHECKTYPE:
7340 free_type(isn->isn_arg.type.ct_type);
7341 break;
7342
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007343 case ISN_2BOOL:
7344 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007345 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007346 case ISN_ADDBLOB:
7347 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007348 case ISN_ANYINDEX:
7349 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007350 case ISN_BCALL:
7351 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007352 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007353 case ISN_CHECKNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007354 case ISN_COMPAREANY:
7355 case ISN_COMPAREBLOB:
7356 case ISN_COMPAREBOOL:
7357 case ISN_COMPAREDICT:
7358 case ISN_COMPAREFLOAT:
7359 case ISN_COMPAREFUNC:
7360 case ISN_COMPARELIST:
7361 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007362 case ISN_COMPARESPECIAL:
7363 case ISN_COMPARESTRING:
7364 case ISN_CONCAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007365 case ISN_DROP:
7366 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007367 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007368 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007369 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007370 case ISN_EXECCONCAT:
7371 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007372 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007373 case ISN_GETITEM:
7374 case ISN_JUMP:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007375 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007376 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007377 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007378 case ISN_LOADBDICT:
7379 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007380 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007381 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007382 case ISN_LOADSCRIPT:
7383 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007384 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007385 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007386 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007387 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007388 case ISN_NEGATENR:
7389 case ISN_NEWDICT:
7390 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007391 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007392 case ISN_OPFLOAT:
7393 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007394 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007395 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007396 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007397 case ISN_PUSHF:
7398 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007399 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007400 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007401 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007402 case ISN_SHUFFLE:
7403 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007404 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007405 case ISN_STOREDICT:
7406 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007407 case ISN_STORENR:
7408 case ISN_STOREOUTER:
7409 case ISN_STOREREG:
7410 case ISN_STORESCRIPT:
7411 case ISN_STOREV:
7412 case ISN_STRINDEX:
7413 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007414 case ISN_THROW:
7415 case ISN_TRY:
7416 // nothing allocated
7417 break;
7418 }
7419}
7420
7421/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007422 * Free all instructions for "dfunc".
7423 */
7424 static void
7425delete_def_function_contents(dfunc_T *dfunc)
7426{
7427 int idx;
7428
7429 ga_clear(&dfunc->df_def_args_isn);
7430
7431 if (dfunc->df_instr != NULL)
7432 {
7433 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7434 delete_instr(dfunc->df_instr + idx);
7435 VIM_CLEAR(dfunc->df_instr);
7436 }
7437
7438 dfunc->df_deleted = TRUE;
7439}
7440
7441/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007442 * When a user function is deleted, clear the contents of any associated def
7443 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007444 */
7445 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007446clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007447{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007448 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007449 {
7450 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7451 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007452
Bram Moolenaar20431c92020-03-20 18:39:46 +01007453 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007454 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007455 }
7456}
7457
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007458/*
7459 * Used when a user function is about to be deleted: remove the pointer to it.
7460 * The entry in def_functions is then unused.
7461 */
7462 void
7463unlink_def_function(ufunc_T *ufunc)
7464{
7465 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7466
7467 dfunc->df_ufunc = NULL;
7468}
7469
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007470#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007471/*
7472 * Free all functions defined with ":def".
7473 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007474 void
7475free_def_functions(void)
7476{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007477 int idx;
7478
7479 for (idx = 0; idx < def_functions.ga_len; ++idx)
7480 {
7481 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7482
7483 delete_def_function_contents(dfunc);
7484 }
7485
7486 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007487}
7488#endif
7489
7490
7491#endif // FEAT_EVAL