blob: a70ed5a44a5e3cd13d1d6aea969667a25bdd9ed6 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
111 int lv_from_outer; // when TRUE using ctx_outer scope
112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200129 int ctx_closure_count; // number of closures created in the
130 // function
131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144};
145
Bram Moolenaar20431c92020-03-20 18:39:46 +0100146static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100147
148/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200149 * Lookup variable "name" in the local scope and return it.
150 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200152 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153lookup_local(char_u *name, size_t len, cctx_T *cctx)
154{
155 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200156 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100158 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200159 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200160
161 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
163 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200164 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100165 if (STRNCMP(name, lvar->lv_name, len) == 0
166 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167 {
168 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200169 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200170 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200172
173 // Find local in outer function scope.
174 if (cctx->ctx_outer != NULL)
175 {
176 lvar = lookup_local(name, len, cctx->ctx_outer);
177 if (lvar != NULL)
178 {
179 // TODO: are there situations we should not mark the outer scope as
180 // used?
181 cctx->ctx_outer_used = TRUE;
182 lvar->lv_from_outer = TRUE;
183 return lvar;
184 }
185 }
186
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200187 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100188}
189
190/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200191 * Lookup an argument in the current function and an enclosing function.
192 * Returns the argument index in "idxp"
193 * Returns the argument type in "type"
194 * Sets "gen_load_outer" to TRUE if found in outer scope.
195 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196 */
197 static int
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200198lookup_arg(
199 char_u *name,
200 size_t len,
201 int *idxp,
202 type_T **type,
203 int *gen_load_outer,
204 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100205{
206 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200207 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100209 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200210 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
212 {
213 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
214
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200215 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
216 {
217 if (idxp != NULL)
218 {
219 // Arguments are located above the frame pointer. One further
220 // if there is a vararg argument
221 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
222 + STACK_FRAME_SIZE)
223 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
224
225 if (cctx->ctx_ufunc->uf_arg_types != NULL)
226 *type = cctx->ctx_ufunc->uf_arg_types[idx];
227 else
228 *type = &t_any;
229 }
230 return OK;
231 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100232 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100233
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200234 va_name = cctx->ctx_ufunc->uf_va_name;
235 if (va_name != NULL
236 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
237 {
238 if (idxp != NULL)
239 {
240 // varargs is always the last argument
241 *idxp = -STACK_FRAME_SIZE - 1;
242 *type = cctx->ctx_ufunc->uf_va_type;
243 }
244 return OK;
245 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200247 if (cctx->ctx_outer != NULL)
248 {
249 // Lookup the name for an argument of the outer function.
250 if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
251 == OK)
252 {
253 *gen_load_outer = TRUE;
254 return OK;
255 }
256 }
257
258 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100259}
260
261/*
Bram Moolenaar84367732020-08-23 15:21:55 +0200262 * Returnd TRUE if the script context is Vim9 script.
263 */
264 static int
265script_is_vim9()
266{
267 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
268}
269
270/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 * Lookup a variable in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200272 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
273 * without "s:".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274 * Returns OK or FAIL.
275 */
276 static int
Bram Moolenaar53900992020-08-22 19:02:02 +0200277lookup_script(char_u *name, size_t len, int vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278{
279 int cc;
280 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
281 dictitem_T *di;
282
Bram Moolenaar84367732020-08-23 15:21:55 +0200283 if (vim9script && !script_is_vim9())
Bram Moolenaar53900992020-08-22 19:02:02 +0200284 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100285 cc = name[len];
286 name[len] = NUL;
287 di = find_var_in_ht(ht, 0, name, TRUE);
288 name[len] = cc;
289 return di == NULL ? FAIL: OK;
290}
291
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100292/*
293 * Check if "p[len]" is already defined, either in script "import_sid" or in
294 * compilation context "cctx".
Bram Moolenaar0f769812020-09-12 18:32:34 +0200295 * Does not check the global namespace.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100296 * Return FAIL and give an error if it defined.
297 */
298 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200299check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100300{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200301 int c = p[len];
302 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200303
304 p[len] = NUL;
Bram Moolenaar53900992020-08-22 19:02:02 +0200305 if (lookup_script(p, len, FALSE) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100306 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200307 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaareef21022020-08-01 22:16:43 +0200308 || lookup_arg(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200309 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200310 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100311 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200312 // A local or script-local function can shadow a global function.
313 if (ufunc == NULL || !func_is_global(ufunc)
314 || (p[0] == 'g' && p[1] == ':'))
315 {
316 p[len] = c;
317 semsg(_(e_name_already_defined_str), p);
318 return FAIL;
319 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100320 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200321 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100322 return OK;
323}
324
Bram Moolenaar65b95452020-07-19 14:03:09 +0200325
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100326/////////////////////////////////////////////////////////////////////
327// Following generate_ functions expect the caller to call ga_grow().
328
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200329#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
330#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100332/*
333 * Generate an instruction without arguments.
334 * Returns a pointer to the new instruction, NULL if failed.
335 */
336 static isn_T *
337generate_instr(cctx_T *cctx, isntype_T isn_type)
338{
339 garray_T *instr = &cctx->ctx_instr;
340 isn_T *isn;
341
Bram Moolenaar080457c2020-03-03 21:53:32 +0100342 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100343 if (ga_grow(instr, 1) == FAIL)
344 return NULL;
345 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
346 isn->isn_type = isn_type;
347 isn->isn_lnum = cctx->ctx_lnum + 1;
348 ++instr->ga_len;
349
350 return isn;
351}
352
353/*
354 * Generate an instruction without arguments.
355 * "drop" will be removed from the stack.
356 * Returns a pointer to the new instruction, NULL if failed.
357 */
358 static isn_T *
359generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
360{
361 garray_T *stack = &cctx->ctx_type_stack;
362
Bram Moolenaar080457c2020-03-03 21:53:32 +0100363 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100364 stack->ga_len -= drop;
365 return generate_instr(cctx, isn_type);
366}
367
368/*
369 * Generate instruction "isn_type" and put "type" on the type stack.
370 */
371 static isn_T *
372generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
373{
374 isn_T *isn;
375 garray_T *stack = &cctx->ctx_type_stack;
376
377 if ((isn = generate_instr(cctx, isn_type)) == NULL)
378 return NULL;
379
380 if (ga_grow(stack, 1) == FAIL)
381 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200382 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100383 ++stack->ga_len;
384
385 return isn;
386}
387
388/*
389 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200390 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100391 */
392 static int
393may_generate_2STRING(int offset, cctx_T *cctx)
394{
395 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200396 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100397 garray_T *stack = &cctx->ctx_type_stack;
398 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
399
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200400 switch ((*type)->tt_type)
401 {
402 // nothing to be done
403 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100404
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200405 // conversion possible
406 case VAR_SPECIAL:
407 case VAR_BOOL:
408 case VAR_NUMBER:
409 case VAR_FLOAT:
410 break;
411
412 // conversion possible (with runtime check)
413 case VAR_ANY:
414 case VAR_UNKNOWN:
415 isntype = ISN_2STRING_ANY;
416 break;
417
418 // conversion not possible
419 case VAR_VOID:
420 case VAR_BLOB:
421 case VAR_FUNC:
422 case VAR_PARTIAL:
423 case VAR_LIST:
424 case VAR_DICT:
425 case VAR_JOB:
426 case VAR_CHANNEL:
427 to_string_error((*type)->tt_type);
428 return FAIL;
429 }
430
431 *type = &t_string;
432 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100433 return FAIL;
434 isn->isn_arg.number = offset;
435
436 return OK;
437}
438
439 static int
440check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
441{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200442 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100443 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200444 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100445 {
446 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200447 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100448 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200449 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100450 return FAIL;
451 }
452 return OK;
453}
454
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200455 static int
456generate_add_instr(
457 cctx_T *cctx,
458 vartype_T vartype,
459 type_T *type1,
460 type_T *type2)
461{
462 isn_T *isn = generate_instr_drop(cctx,
463 vartype == VAR_NUMBER ? ISN_OPNR
464 : vartype == VAR_LIST ? ISN_ADDLIST
465 : vartype == VAR_BLOB ? ISN_ADDBLOB
466#ifdef FEAT_FLOAT
467 : vartype == VAR_FLOAT ? ISN_OPFLOAT
468#endif
469 : ISN_OPANY, 1);
470
471 if (vartype != VAR_LIST && vartype != VAR_BLOB
472 && type1->tt_type != VAR_ANY
473 && type2->tt_type != VAR_ANY
474 && check_number_or_float(
475 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
476 return FAIL;
477
478 if (isn != NULL)
479 isn->isn_arg.op.op_type = EXPR_ADD;
480 return isn == NULL ? FAIL : OK;
481}
482
483/*
484 * Get the type to use for an instruction for an operation on "type1" and
485 * "type2". If they are matching use a type-specific instruction. Otherwise
486 * fall back to runtime type checking.
487 */
488 static vartype_T
489operator_type(type_T *type1, type_T *type2)
490{
491 if (type1->tt_type == type2->tt_type
492 && (type1->tt_type == VAR_NUMBER
493 || type1->tt_type == VAR_LIST
494#ifdef FEAT_FLOAT
495 || type1->tt_type == VAR_FLOAT
496#endif
497 || type1->tt_type == VAR_BLOB))
498 return type1->tt_type;
499 return VAR_ANY;
500}
501
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100502/*
503 * Generate an instruction with two arguments. The instruction depends on the
504 * type of the arguments.
505 */
506 static int
507generate_two_op(cctx_T *cctx, char_u *op)
508{
509 garray_T *stack = &cctx->ctx_type_stack;
510 type_T *type1;
511 type_T *type2;
512 vartype_T vartype;
513 isn_T *isn;
514
Bram Moolenaar080457c2020-03-03 21:53:32 +0100515 RETURN_OK_IF_SKIP(cctx);
516
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200517 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100518 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
519 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200520 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521
522 switch (*op)
523 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200524 case '+':
525 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100526 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527 break;
528
529 case '-':
530 case '*':
531 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
532 op) == FAIL)
533 return FAIL;
534 if (vartype == VAR_NUMBER)
535 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
536#ifdef FEAT_FLOAT
537 else if (vartype == VAR_FLOAT)
538 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
539#endif
540 else
541 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
542 if (isn != NULL)
543 isn->isn_arg.op.op_type = *op == '*'
544 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
545 break;
546
Bram Moolenaar4c683752020-04-05 21:38:23 +0200547 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100548 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200549 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100550 && type2->tt_type != VAR_NUMBER))
551 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200552 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100553 return FAIL;
554 }
555 isn = generate_instr_drop(cctx,
556 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
557 if (isn != NULL)
558 isn->isn_arg.op.op_type = EXPR_REM;
559 break;
560 }
561
562 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200563 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100564 {
565 type_T *type = &t_any;
566
567#ifdef FEAT_FLOAT
568 // float+number and number+float results in float
569 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
570 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
571 type = &t_float;
572#endif
573 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
574 }
575
576 return OK;
577}
578
579/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200580 * Get the instruction to use for comparing "type1" with "type2"
581 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100582 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200583 static isntype_T
584get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100585{
586 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100587
Bram Moolenaar4c683752020-04-05 21:38:23 +0200588 if (type1 == VAR_UNKNOWN)
589 type1 = VAR_ANY;
590 if (type2 == VAR_UNKNOWN)
591 type2 = VAR_ANY;
592
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100593 if (type1 == type2)
594 {
595 switch (type1)
596 {
597 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
598 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
599 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
600 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
601 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
602 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
603 case VAR_LIST: isntype = ISN_COMPARELIST; break;
604 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
605 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100606 default: isntype = ISN_COMPAREANY; break;
607 }
608 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200609 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100610 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
611 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
612 isntype = ISN_COMPAREANY;
613
614 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
615 && (isntype == ISN_COMPAREBOOL
616 || isntype == ISN_COMPARESPECIAL
617 || isntype == ISN_COMPARENR
618 || isntype == ISN_COMPAREFLOAT))
619 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200620 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200622 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100623 }
624 if (isntype == ISN_DROP
625 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
626 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
627 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
628 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
629 && exptype != EXPR_IS && exptype != EXPR_ISNOT
630 && (type1 == VAR_BLOB || type2 == VAR_BLOB
631 || type1 == VAR_LIST || type2 == VAR_LIST))))
632 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200633 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100634 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200635 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100636 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200637 return isntype;
638}
639
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200640 int
641check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
642{
643 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
644 return FAIL;
645 return OK;
646}
647
Bram Moolenaara5565e42020-05-09 15:44:01 +0200648/*
649 * Generate an ISN_COMPARE* instruction with a boolean result.
650 */
651 static int
652generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
653{
654 isntype_T isntype;
655 isn_T *isn;
656 garray_T *stack = &cctx->ctx_type_stack;
657 vartype_T type1;
658 vartype_T type2;
659
660 RETURN_OK_IF_SKIP(cctx);
661
662 // Get the known type of the two items on the stack. If they are matching
663 // use a type-specific instruction. Otherwise fall back to runtime type
664 // checking.
665 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
666 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
667 isntype = get_compare_isn(exptype, type1, type2);
668 if (isntype == ISN_DROP)
669 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670
671 if ((isn = generate_instr(cctx, isntype)) == NULL)
672 return FAIL;
673 isn->isn_arg.op.op_type = exptype;
674 isn->isn_arg.op.op_ic = ic;
675
676 // takes two arguments, puts one bool back
677 if (stack->ga_len >= 2)
678 {
679 --stack->ga_len;
680 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
681 }
682
683 return OK;
684}
685
686/*
687 * Generate an ISN_2BOOL instruction.
688 */
689 static int
690generate_2BOOL(cctx_T *cctx, int invert)
691{
692 isn_T *isn;
693 garray_T *stack = &cctx->ctx_type_stack;
694
Bram Moolenaar080457c2020-03-03 21:53:32 +0100695 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100696 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
697 return FAIL;
698 isn->isn_arg.number = invert;
699
700 // type becomes bool
701 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
702
703 return OK;
704}
705
706 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200707generate_TYPECHECK(
708 cctx_T *cctx,
709 type_T *expected,
710 int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711{
712 isn_T *isn;
713 garray_T *stack = &cctx->ctx_type_stack;
714
Bram Moolenaar080457c2020-03-03 21:53:32 +0100715 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100716 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
717 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200718 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100719 isn->isn_arg.type.ct_off = offset;
720
Bram Moolenaar5e654232020-09-16 15:22:00 +0200721 // type becomes expected
722 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100723
724 return OK;
725}
726
727/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200728 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200729 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200730 * - "actual" is a type that can be "expected" type: add a runtime check; or
731 * - return FAIL.
732 */
733 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200734need_type(
735 type_T *actual,
736 type_T *expected,
737 int offset,
738 cctx_T *cctx,
739 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200740{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200741 if (expected == &t_bool && actual != &t_bool
742 && (actual->tt_flags & TTFLAG_BOOL_OK))
743 {
744 // Using "0", "1" or the result of an expression with "&&" or "||" as a
745 // boolean is OK but requires a conversion.
746 generate_2BOOL(cctx, FALSE);
747 return OK;
748 }
749
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200750 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200751 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200752
753 // If the actual type can be the expected type add a runtime check.
754 // TODO: if it's a constant a runtime check makes no sense.
755 if (actual->tt_type == VAR_ANY
756 || actual->tt_type == VAR_UNKNOWN
757 || (actual->tt_type == VAR_FUNC
758 && (expected->tt_type == VAR_FUNC
759 || expected->tt_type == VAR_PARTIAL)
760 && (actual->tt_member == &t_any || actual->tt_argcount < 0))
761 || (actual->tt_type == VAR_LIST
762 && expected->tt_type == VAR_LIST
763 && actual->tt_member == &t_any)
764 || (actual->tt_type == VAR_DICT
765 && expected->tt_type == VAR_DICT
766 && actual->tt_member == &t_any))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200767 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200768 generate_TYPECHECK(cctx, expected, offset);
769 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200770 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200771
772 if (!silent)
773 type_mismatch(expected, actual);
774 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200775}
776
777/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100778 * Generate an ISN_PUSHNR instruction.
779 */
780 static int
781generate_PUSHNR(cctx_T *cctx, varnumber_T number)
782{
783 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200784 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100785
Bram Moolenaar080457c2020-03-03 21:53:32 +0100786 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100787 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
788 return FAIL;
789 isn->isn_arg.number = number;
790
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200791 if (number == 0 || number == 1)
792 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200793 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200794
795 // A 0 or 1 number can also be used as a bool.
796 if (type != NULL)
797 {
798 type->tt_type = VAR_NUMBER;
799 type->tt_flags = TTFLAG_BOOL_OK;
800 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
801 }
802 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100803 return OK;
804}
805
806/*
807 * Generate an ISN_PUSHBOOL instruction.
808 */
809 static int
810generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
811{
812 isn_T *isn;
813
Bram Moolenaar080457c2020-03-03 21:53:32 +0100814 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100815 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
816 return FAIL;
817 isn->isn_arg.number = number;
818
819 return OK;
820}
821
822/*
823 * Generate an ISN_PUSHSPEC instruction.
824 */
825 static int
826generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
827{
828 isn_T *isn;
829
Bram Moolenaar080457c2020-03-03 21:53:32 +0100830 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100831 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
832 return FAIL;
833 isn->isn_arg.number = number;
834
835 return OK;
836}
837
838#ifdef FEAT_FLOAT
839/*
840 * Generate an ISN_PUSHF instruction.
841 */
842 static int
843generate_PUSHF(cctx_T *cctx, float_T fnumber)
844{
845 isn_T *isn;
846
Bram Moolenaar080457c2020-03-03 21:53:32 +0100847 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100848 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
849 return FAIL;
850 isn->isn_arg.fnumber = fnumber;
851
852 return OK;
853}
854#endif
855
856/*
857 * Generate an ISN_PUSHS instruction.
858 * Consumes "str".
859 */
860 static int
861generate_PUSHS(cctx_T *cctx, char_u *str)
862{
863 isn_T *isn;
864
Bram Moolenaar080457c2020-03-03 21:53:32 +0100865 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100866 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
867 return FAIL;
868 isn->isn_arg.string = str;
869
870 return OK;
871}
872
873/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100874 * Generate an ISN_PUSHCHANNEL instruction.
875 * Consumes "channel".
876 */
877 static int
878generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
879{
880 isn_T *isn;
881
Bram Moolenaar080457c2020-03-03 21:53:32 +0100882 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100883 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
884 return FAIL;
885 isn->isn_arg.channel = channel;
886
887 return OK;
888}
889
890/*
891 * Generate an ISN_PUSHJOB instruction.
892 * Consumes "job".
893 */
894 static int
895generate_PUSHJOB(cctx_T *cctx, job_T *job)
896{
897 isn_T *isn;
898
Bram Moolenaar080457c2020-03-03 21:53:32 +0100899 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100900 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100901 return FAIL;
902 isn->isn_arg.job = job;
903
904 return OK;
905}
906
907/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100908 * Generate an ISN_PUSHBLOB instruction.
909 * Consumes "blob".
910 */
911 static int
912generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
913{
914 isn_T *isn;
915
Bram Moolenaar080457c2020-03-03 21:53:32 +0100916 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100917 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
918 return FAIL;
919 isn->isn_arg.blob = blob;
920
921 return OK;
922}
923
924/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100925 * Generate an ISN_PUSHFUNC instruction with name "name".
926 * Consumes "name".
927 */
928 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200929generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100930{
931 isn_T *isn;
932
Bram Moolenaar080457c2020-03-03 21:53:32 +0100933 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200934 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100935 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200936 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100937
938 return OK;
939}
940
941/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200942 * Generate an ISN_GETITEM instruction with "index".
943 */
944 static int
945generate_GETITEM(cctx_T *cctx, int index)
946{
947 isn_T *isn;
948 garray_T *stack = &cctx->ctx_type_stack;
949 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
950 type_T *item_type = &t_any;
951
952 RETURN_OK_IF_SKIP(cctx);
953
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200954 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200955 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200956 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200957 emsg(_(e_listreq));
958 return FAIL;
959 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200960 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200961 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
962 return FAIL;
963 isn->isn_arg.number = index;
964
965 // add the item type to the type stack
966 if (ga_grow(stack, 1) == FAIL)
967 return FAIL;
968 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
969 ++stack->ga_len;
970 return OK;
971}
972
973/*
Bram Moolenaar9af78762020-06-16 11:34:42 +0200974 * Generate an ISN_SLICE instruction with "count".
975 */
976 static int
977generate_SLICE(cctx_T *cctx, int count)
978{
979 isn_T *isn;
980
981 RETURN_OK_IF_SKIP(cctx);
982 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
983 return FAIL;
984 isn->isn_arg.number = count;
985 return OK;
986}
987
988/*
989 * Generate an ISN_CHECKLEN instruction with "min_len".
990 */
991 static int
992generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
993{
994 isn_T *isn;
995
996 RETURN_OK_IF_SKIP(cctx);
997
998 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
999 return FAIL;
1000 isn->isn_arg.checklen.cl_min_len = min_len;
1001 isn->isn_arg.checklen.cl_more_OK = more_OK;
1002
1003 return OK;
1004}
1005
1006/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001007 * Generate an ISN_STORE instruction.
1008 */
1009 static int
1010generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1011{
1012 isn_T *isn;
1013
Bram Moolenaar080457c2020-03-03 21:53:32 +01001014 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001015 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1016 return FAIL;
1017 if (name != NULL)
1018 isn->isn_arg.string = vim_strsave(name);
1019 else
1020 isn->isn_arg.number = idx;
1021
1022 return OK;
1023}
1024
1025/*
1026 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1027 */
1028 static int
1029generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1030{
1031 isn_T *isn;
1032
Bram Moolenaar080457c2020-03-03 21:53:32 +01001033 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001034 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1035 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001036 isn->isn_arg.storenr.stnr_idx = idx;
1037 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001038
1039 return OK;
1040}
1041
1042/*
1043 * Generate an ISN_STOREOPT instruction
1044 */
1045 static int
1046generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1047{
1048 isn_T *isn;
1049
Bram Moolenaar080457c2020-03-03 21:53:32 +01001050 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001051 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001052 return FAIL;
1053 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1054 isn->isn_arg.storeopt.so_flags = opt_flags;
1055
1056 return OK;
1057}
1058
1059/*
1060 * Generate an ISN_LOAD or similar instruction.
1061 */
1062 static int
1063generate_LOAD(
1064 cctx_T *cctx,
1065 isntype_T isn_type,
1066 int idx,
1067 char_u *name,
1068 type_T *type)
1069{
1070 isn_T *isn;
1071
Bram Moolenaar080457c2020-03-03 21:53:32 +01001072 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001073 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1074 return FAIL;
1075 if (name != NULL)
1076 isn->isn_arg.string = vim_strsave(name);
1077 else
1078 isn->isn_arg.number = idx;
1079
1080 return OK;
1081}
1082
1083/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001084 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001085 */
1086 static int
1087generate_LOADV(
1088 cctx_T *cctx,
1089 char_u *name,
1090 int error)
1091{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001092 int di_flags;
1093 int vidx = find_vim_var(name, &di_flags);
1094 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001095
Bram Moolenaar080457c2020-03-03 21:53:32 +01001096 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001097 if (vidx < 0)
1098 {
1099 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001100 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001101 return FAIL;
1102 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001103 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001104
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001105 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001106}
1107
1108/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001109 * Generate an ISN_UNLET instruction.
1110 */
1111 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001112generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001113{
1114 isn_T *isn;
1115
1116 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001117 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001118 return FAIL;
1119 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1120 isn->isn_arg.unlet.ul_forceit = forceit;
1121
1122 return OK;
1123}
1124
1125/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001126 * Generate an ISN_LOCKCONST instruction.
1127 */
1128 static int
1129generate_LOCKCONST(cctx_T *cctx)
1130{
1131 isn_T *isn;
1132
1133 RETURN_OK_IF_SKIP(cctx);
1134 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1135 return FAIL;
1136 return OK;
1137}
1138
1139/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001140 * Generate an ISN_LOADS instruction.
1141 */
1142 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001143generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001144 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001145 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001146 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001147 int sid,
1148 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149{
1150 isn_T *isn;
1151
Bram Moolenaar080457c2020-03-03 21:53:32 +01001152 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001153 if (isn_type == ISN_LOADS)
1154 isn = generate_instr_type(cctx, isn_type, type);
1155 else
1156 isn = generate_instr_drop(cctx, isn_type, 1);
1157 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001158 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001159 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1160 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001161
1162 return OK;
1163}
1164
1165/*
1166 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1167 */
1168 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001169generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001170 cctx_T *cctx,
1171 isntype_T isn_type,
1172 int sid,
1173 int idx,
1174 type_T *type)
1175{
1176 isn_T *isn;
1177
Bram Moolenaar080457c2020-03-03 21:53:32 +01001178 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179 if (isn_type == ISN_LOADSCRIPT)
1180 isn = generate_instr_type(cctx, isn_type, type);
1181 else
1182 isn = generate_instr_drop(cctx, isn_type, 1);
1183 if (isn == NULL)
1184 return FAIL;
1185 isn->isn_arg.script.script_sid = sid;
1186 isn->isn_arg.script.script_idx = idx;
1187 return OK;
1188}
1189
1190/*
1191 * Generate an ISN_NEWLIST instruction.
1192 */
1193 static int
1194generate_NEWLIST(cctx_T *cctx, int count)
1195{
1196 isn_T *isn;
1197 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001198 type_T *type;
1199 type_T *member;
1200
Bram Moolenaar080457c2020-03-03 21:53:32 +01001201 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001202 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1203 return FAIL;
1204 isn->isn_arg.number = count;
1205
Bram Moolenaar127542b2020-08-09 17:22:04 +02001206 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001207 if (count == 0)
1208 member = &t_void;
1209 else
1210 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001211 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1212 cctx->ctx_type_list);
1213 type = get_list_type(member, cctx->ctx_type_list);
1214
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215 // drop the value types
1216 stack->ga_len -= count;
1217
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001218 // add the list type to the type stack
1219 if (ga_grow(stack, 1) == FAIL)
1220 return FAIL;
1221 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1222 ++stack->ga_len;
1223
1224 return OK;
1225}
1226
1227/*
1228 * Generate an ISN_NEWDICT instruction.
1229 */
1230 static int
1231generate_NEWDICT(cctx_T *cctx, int count)
1232{
1233 isn_T *isn;
1234 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001235 type_T *type;
1236 type_T *member;
1237
Bram Moolenaar080457c2020-03-03 21:53:32 +01001238 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001239 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1240 return FAIL;
1241 isn->isn_arg.number = count;
1242
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001243 if (count == 0)
1244 member = &t_void;
1245 else
1246 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001247 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1248 cctx->ctx_type_list);
1249 type = get_dict_type(member, cctx->ctx_type_list);
1250
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001251 // drop the key and value types
1252 stack->ga_len -= 2 * count;
1253
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001254 // add the dict type to the type stack
1255 if (ga_grow(stack, 1) == FAIL)
1256 return FAIL;
1257 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1258 ++stack->ga_len;
1259
1260 return OK;
1261}
1262
1263/*
1264 * Generate an ISN_FUNCREF instruction.
1265 */
1266 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001267generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268{
1269 isn_T *isn;
1270 garray_T *stack = &cctx->ctx_type_stack;
1271
Bram Moolenaar080457c2020-03-03 21:53:32 +01001272 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1274 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001275 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001276 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277
1278 if (ga_grow(stack, 1) == FAIL)
1279 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001280 ((type_T **)stack->ga_data)[stack->ga_len] =
1281 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001282 ++stack->ga_len;
1283
1284 return OK;
1285}
1286
1287/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001288 * Generate an ISN_NEWFUNC instruction.
1289 */
1290 static int
1291generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1292{
1293 isn_T *isn;
1294 char_u *name;
1295
1296 RETURN_OK_IF_SKIP(cctx);
1297 name = vim_strsave(lambda_name);
1298 if (name == NULL)
1299 return FAIL;
1300 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1301 return FAIL;
1302 isn->isn_arg.newfunc.nf_lambda = name;
1303 isn->isn_arg.newfunc.nf_global = func_name;
1304
1305 return OK;
1306}
1307
1308/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309 * Generate an ISN_JUMP instruction.
1310 */
1311 static int
1312generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1313{
1314 isn_T *isn;
1315 garray_T *stack = &cctx->ctx_type_stack;
1316
Bram Moolenaar080457c2020-03-03 21:53:32 +01001317 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1319 return FAIL;
1320 isn->isn_arg.jump.jump_when = when;
1321 isn->isn_arg.jump.jump_where = where;
1322
1323 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1324 --stack->ga_len;
1325
1326 return OK;
1327}
1328
1329 static int
1330generate_FOR(cctx_T *cctx, int loop_idx)
1331{
1332 isn_T *isn;
1333 garray_T *stack = &cctx->ctx_type_stack;
1334
Bram Moolenaar080457c2020-03-03 21:53:32 +01001335 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1337 return FAIL;
1338 isn->isn_arg.forloop.for_idx = loop_idx;
1339
1340 if (ga_grow(stack, 1) == FAIL)
1341 return FAIL;
1342 // type doesn't matter, will be stored next
1343 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1344 ++stack->ga_len;
1345
1346 return OK;
1347}
1348
1349/*
1350 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001351 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 * Return FAIL if the number of arguments is wrong.
1353 */
1354 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001355generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356{
1357 isn_T *isn;
1358 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001359 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001360 type_T *argtypes[MAX_FUNC_ARGS];
1361 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362
Bram Moolenaar080457c2020-03-03 21:53:32 +01001363 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001364 argoff = check_internal_func(func_idx, argcount);
1365 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001366 return FAIL;
1367
Bram Moolenaar389df252020-07-09 21:20:47 +02001368 if (method_call && argoff > 1)
1369 {
1370 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1371 return FAIL;
1372 isn->isn_arg.shuffle.shfl_item = argcount;
1373 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1374 }
1375
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1377 return FAIL;
1378 isn->isn_arg.bfunc.cbf_idx = func_idx;
1379 isn->isn_arg.bfunc.cbf_argcount = argcount;
1380
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001381 for (i = 0; i < argcount; ++i)
1382 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1383
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001384 stack->ga_len -= argcount; // drop the arguments
1385 if (ga_grow(stack, 1) == FAIL)
1386 return FAIL;
1387 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001388 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001389 ++stack->ga_len; // add return value
1390
1391 return OK;
1392}
1393
1394/*
1395 * Generate an ISN_DCALL or ISN_UCALL instruction.
1396 * Return FAIL if the number of arguments is wrong.
1397 */
1398 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001399generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400{
1401 isn_T *isn;
1402 garray_T *stack = &cctx->ctx_type_stack;
1403 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001404 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001405
Bram Moolenaar080457c2020-03-03 21:53:32 +01001406 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407 if (argcount > regular_args && !has_varargs(ufunc))
1408 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001409 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001410 return FAIL;
1411 }
1412 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1413 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001414 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001415 return FAIL;
1416 }
1417
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001418 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001419 {
1420 int i;
1421
1422 for (i = 0; i < argcount; ++i)
1423 {
1424 type_T *expected;
1425 type_T *actual;
1426
1427 if (i < regular_args)
1428 {
1429 if (ufunc->uf_arg_types == NULL)
1430 continue;
1431 expected = ufunc->uf_arg_types[i];
1432 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001433 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1434 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001435 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001436 else
1437 expected = ufunc->uf_va_type->tt_member;
1438 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001439 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001440 {
1441 arg_type_mismatch(expected, actual, i + 1);
1442 return FAIL;
1443 }
1444 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001445 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001446 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1447 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001448 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001449 }
1450
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001451 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001452 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001453 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001455 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001456 {
1457 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1458 isn->isn_arg.dfunc.cdf_argcount = argcount;
1459 }
1460 else
1461 {
1462 // A user function may be deleted and redefined later, can't use the
1463 // ufunc pointer, need to look it up again at runtime.
1464 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1465 isn->isn_arg.ufunc.cuf_argcount = argcount;
1466 }
1467
1468 stack->ga_len -= argcount; // drop the arguments
1469 if (ga_grow(stack, 1) == FAIL)
1470 return FAIL;
1471 // add return value
1472 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1473 ++stack->ga_len;
1474
1475 return OK;
1476}
1477
1478/*
1479 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1480 */
1481 static int
1482generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1483{
1484 isn_T *isn;
1485 garray_T *stack = &cctx->ctx_type_stack;
1486
Bram Moolenaar080457c2020-03-03 21:53:32 +01001487 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1489 return FAIL;
1490 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1491 isn->isn_arg.ufunc.cuf_argcount = argcount;
1492
1493 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001494 if (ga_grow(stack, 1) == FAIL)
1495 return FAIL;
1496 // add return value
1497 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1498 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001499
1500 return OK;
1501}
1502
1503/*
1504 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001505 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001506 */
1507 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001508generate_PCALL(
1509 cctx_T *cctx,
1510 int argcount,
1511 char_u *name,
1512 type_T *type,
1513 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001514{
1515 isn_T *isn;
1516 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001517 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518
Bram Moolenaar080457c2020-03-03 21:53:32 +01001519 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001520
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001521 if (type->tt_type == VAR_ANY)
1522 ret_type = &t_any;
1523 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001524 {
1525 if (type->tt_argcount != -1)
1526 {
1527 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1528
1529 if (argcount < type->tt_min_argcount - varargs)
1530 {
1531 semsg(_(e_toofewarg), "[reference]");
1532 return FAIL;
1533 }
1534 if (!varargs && argcount > type->tt_argcount)
1535 {
1536 semsg(_(e_toomanyarg), "[reference]");
1537 return FAIL;
1538 }
1539 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001540 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001541 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001542 else
1543 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001544 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001545 return FAIL;
1546 }
1547
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001548 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1549 return FAIL;
1550 isn->isn_arg.pfunc.cpf_top = at_top;
1551 isn->isn_arg.pfunc.cpf_argcount = argcount;
1552
1553 stack->ga_len -= argcount; // drop the arguments
1554
1555 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001556 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001558 // If partial is above the arguments it must be cleared and replaced with
1559 // the return value.
1560 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1561 return FAIL;
1562
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 return OK;
1564}
1565
1566/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001567 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568 */
1569 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001570generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001571{
1572 isn_T *isn;
1573 garray_T *stack = &cctx->ctx_type_stack;
1574 type_T *type;
1575
Bram Moolenaar080457c2020-03-03 21:53:32 +01001576 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001577 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001578 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001579 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001580
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001581 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001582 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001583 if (type->tt_type != VAR_DICT && type != &t_any)
1584 {
1585 emsg(_(e_dictreq));
1586 return FAIL;
1587 }
1588 // change dict type to dict member type
1589 if (type->tt_type == VAR_DICT)
1590 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001591
1592 return OK;
1593}
1594
1595/*
1596 * Generate an ISN_ECHO instruction.
1597 */
1598 static int
1599generate_ECHO(cctx_T *cctx, int with_white, int count)
1600{
1601 isn_T *isn;
1602
Bram Moolenaar080457c2020-03-03 21:53:32 +01001603 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1605 return FAIL;
1606 isn->isn_arg.echo.echo_with_white = with_white;
1607 isn->isn_arg.echo.echo_count = count;
1608
1609 return OK;
1610}
1611
Bram Moolenaarad39c092020-02-26 18:23:43 +01001612/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001613 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001614 */
1615 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001616generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001617{
1618 isn_T *isn;
1619
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001620 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001621 return FAIL;
1622 isn->isn_arg.number = count;
1623
1624 return OK;
1625}
1626
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001627/*
1628 * Generate an ISN_PUT instruction.
1629 */
1630 static int
1631generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1632{
1633 isn_T *isn;
1634
1635 RETURN_OK_IF_SKIP(cctx);
1636 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1637 return FAIL;
1638 isn->isn_arg.put.put_regname = regname;
1639 isn->isn_arg.put.put_lnum = lnum;
1640 return OK;
1641}
1642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001643 static int
1644generate_EXEC(cctx_T *cctx, char_u *line)
1645{
1646 isn_T *isn;
1647
Bram Moolenaar080457c2020-03-03 21:53:32 +01001648 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001649 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1650 return FAIL;
1651 isn->isn_arg.string = vim_strsave(line);
1652 return OK;
1653}
1654
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001655 static int
1656generate_EXECCONCAT(cctx_T *cctx, int count)
1657{
1658 isn_T *isn;
1659
1660 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1661 return FAIL;
1662 isn->isn_arg.number = count;
1663 return OK;
1664}
1665
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001666/*
1667 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001668 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001669 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001670 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1672{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001673 lvar_T *lvar;
1674
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001675 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001676 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001677 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001678 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001679 }
1680
1681 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001682 return NULL;
1683 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001685 // Every local variable uses the next entry on the stack. We could re-use
1686 // the last ones when leaving a scope, but then variables used in a closure
1687 // might get overwritten. To keep things simple do not re-use stack
1688 // entries. This is less efficient, but memory is cheap these days.
1689 lvar->lv_idx = cctx->ctx_locals_count++;
1690
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001691 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001692 lvar->lv_const = isConst;
1693 lvar->lv_type = type;
1694
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001695 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001696}
1697
1698/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001699 * Remove local variables above "new_top".
1700 */
1701 static void
1702unwind_locals(cctx_T *cctx, int new_top)
1703{
1704 if (cctx->ctx_locals.ga_len > new_top)
1705 {
1706 int idx;
1707 lvar_T *lvar;
1708
1709 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1710 {
1711 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1712 vim_free(lvar->lv_name);
1713 }
1714 }
1715 cctx->ctx_locals.ga_len = new_top;
1716}
1717
1718/*
1719 * Free all local variables.
1720 */
1721 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001722free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001723{
1724 unwind_locals(cctx, 0);
1725 ga_clear(&cctx->ctx_locals);
1726}
1727
1728/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001729 * Find "name" in script-local items of script "sid".
1730 * Returns the index in "sn_var_vals" if found.
1731 * If found but not in "sn_var_vals" returns -1.
1732 * If not found returns -2.
1733 */
1734 int
1735get_script_item_idx(int sid, char_u *name, int check_writable)
1736{
1737 hashtab_T *ht;
1738 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001739 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001740 int idx;
1741
1742 // First look the name up in the hashtable.
Bram Moolenaare3d46852020-08-29 13:39:17 +02001743 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001744 return -1;
1745 ht = &SCRIPT_VARS(sid);
1746 di = find_var_in_ht(ht, 0, name, TRUE);
1747 if (di == NULL)
1748 return -2;
1749
1750 // Now find the svar_T index in sn_var_vals.
1751 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1752 {
1753 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1754
1755 if (sv->sv_tv == &di->di_tv)
1756 {
1757 if (check_writable && sv->sv_const)
1758 semsg(_(e_readonlyvar), name);
1759 return idx;
1760 }
1761 }
1762 return -1;
1763}
1764
1765/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001766 * Find "name" in imported items of the current script or in "cctx" if not
1767 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768 */
1769 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001770find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001772 int idx;
1773
Bram Moolenaare3d46852020-08-29 13:39:17 +02001774 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001775 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776 if (cctx != NULL)
1777 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1778 {
1779 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1780 + idx;
1781
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001782 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1783 : STRLEN(import->imp_name) == len
1784 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785 return import;
1786 }
1787
Bram Moolenaarefa94442020-08-08 22:16:00 +02001788 return find_imported_in_script(name, len, current_sctx.sc_sid);
1789}
1790
1791 imported_T *
1792find_imported_in_script(char_u *name, size_t len, int sid)
1793{
Bram Moolenaare3d46852020-08-29 13:39:17 +02001794 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001795 int idx;
1796
Bram Moolenaare3d46852020-08-29 13:39:17 +02001797 if (!SCRIPT_ID_VALID(sid))
1798 return NULL;
1799 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001800 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1801 {
1802 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1803
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001804 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1805 : STRLEN(import->imp_name) == len
1806 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807 return import;
1808 }
1809 return NULL;
1810}
1811
1812/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001813 * Free all imported variables.
1814 */
1815 static void
1816free_imported(cctx_T *cctx)
1817{
1818 int idx;
1819
1820 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1821 {
1822 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1823
1824 vim_free(import->imp_name);
1825 }
1826 ga_clear(&cctx->ctx_imports);
1827}
1828
1829/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02001830 * Return TRUE if "p" points at a "#" but not at "#{".
1831 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001832 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001833vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001834{
1835 return p[0] == '#' && p[1] != '{';
1836}
1837
1838/*
1839 * Return a pointer to the next line that isn't empty or only contains a
1840 * comment. Skips over white space.
1841 * Returns NULL if there is none.
1842 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001843 char_u *
1844peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001845{
1846 int lnum = cctx->ctx_lnum;
1847
1848 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
1849 {
1850 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001851 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001852
Bram Moolenaarba60cc42020-08-12 19:15:33 +02001853 // ignore NULLs inserted for continuation lines
1854 if (line != NULL)
1855 {
1856 p = skipwhite(line);
1857 if (*p != NUL && !vim9_comment_start(p))
1858 return p;
1859 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02001860 }
1861 return NULL;
1862}
1863
1864/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001865 * Called when checking for a following operator at "arg". When the rest of
1866 * the line is empty or only a comment, peek the next line. If there is a next
1867 * line return a pointer to it and set "nextp".
1868 * Otherwise skip over white space.
1869 */
1870 static char_u *
1871may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
1872{
1873 char_u *p = skipwhite(arg);
1874
1875 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001876 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001877 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001878 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001879 if (*nextp != NULL)
1880 return *nextp;
1881 }
1882 return p;
1883}
1884
1885/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02001886 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02001887 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02001888 * Returns NULL when at the end.
1889 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001890 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02001891next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02001892{
Bram Moolenaar7a092242020-04-16 22:10:49 +02001893 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001894
1895 do
1896 {
1897 ++cctx->ctx_lnum;
1898 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02001899 {
1900 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001901 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02001902 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02001903 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02001904 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001905 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001906 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02001907 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02001908 return line;
1909}
1910
1911/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001912 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02001913 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001914 * Return FAIL if beyond the last line, "*arg" is unmodified then.
1915 */
1916 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02001917may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001918{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001919 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001920 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02001921 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001922
1923 if (next == NULL)
1924 return FAIL;
1925 *arg = skipwhite(next);
1926 }
1927 return OK;
1928}
1929
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001930/*
1931 * Idem, and give an error when failed.
1932 */
1933 static int
1934may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
1935{
1936 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1937 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001938 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001939 return FAIL;
1940 }
1941 return OK;
1942}
1943
1944
Bram Moolenaara5565e42020-05-09 15:44:01 +02001945// Structure passed between the compile_expr* functions to keep track of
1946// constants that have been parsed but for which no code was produced yet. If
1947// possible expressions on these constants are applied at compile time. If
1948// that is not possible, the code to push the constants needs to be generated
1949// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02001950// Using 50 should be more than enough of 5 levels of ().
1951#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02001952typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02001953 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02001954 int pp_used; // active entries in pp_tv[]
1955} ppconst_T;
1956
Bram Moolenaar1c747212020-05-09 18:28:34 +02001957static int compile_expr0(char_u **arg, cctx_T *cctx);
1958static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1959
Bram Moolenaara5565e42020-05-09 15:44:01 +02001960/*
1961 * Generate a PUSH instruction for "tv".
1962 * "tv" will be consumed or cleared.
1963 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
1964 */
1965 static int
1966generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
1967{
1968 if (tv != NULL)
1969 {
1970 switch (tv->v_type)
1971 {
1972 case VAR_UNKNOWN:
1973 break;
1974 case VAR_BOOL:
1975 generate_PUSHBOOL(cctx, tv->vval.v_number);
1976 break;
1977 case VAR_SPECIAL:
1978 generate_PUSHSPEC(cctx, tv->vval.v_number);
1979 break;
1980 case VAR_NUMBER:
1981 generate_PUSHNR(cctx, tv->vval.v_number);
1982 break;
1983#ifdef FEAT_FLOAT
1984 case VAR_FLOAT:
1985 generate_PUSHF(cctx, tv->vval.v_float);
1986 break;
1987#endif
1988 case VAR_BLOB:
1989 generate_PUSHBLOB(cctx, tv->vval.v_blob);
1990 tv->vval.v_blob = NULL;
1991 break;
1992 case VAR_STRING:
1993 generate_PUSHS(cctx, tv->vval.v_string);
1994 tv->vval.v_string = NULL;
1995 break;
1996 default:
1997 iemsg("constant type not supported");
1998 clear_tv(tv);
1999 return FAIL;
2000 }
2001 tv->v_type = VAR_UNKNOWN;
2002 }
2003 return OK;
2004}
2005
2006/*
2007 * Generate code for any ppconst entries.
2008 */
2009 static int
2010generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2011{
2012 int i;
2013 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002014 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002015
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002016 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002017 for (i = 0; i < ppconst->pp_used; ++i)
2018 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2019 ret = FAIL;
2020 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002021 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002022 return ret;
2023}
2024
2025/*
2026 * Clear ppconst constants. Used when failing.
2027 */
2028 static void
2029clear_ppconst(ppconst_T *ppconst)
2030{
2031 int i;
2032
2033 for (i = 0; i < ppconst->pp_used; ++i)
2034 clear_tv(&ppconst->pp_tv[i]);
2035 ppconst->pp_used = 0;
2036}
2037
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002038/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002039 * Generate an instruction to load script-local variable "name", without the
2040 * leading "s:".
2041 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042 */
2043 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002044compile_load_scriptvar(
2045 cctx_T *cctx,
2046 char_u *name, // variable NUL terminated
2047 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002048 char_u **end, // end of variable
2049 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002050{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002051 scriptitem_T *si;
2052 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002053 imported_T *import;
2054
Bram Moolenaare3d46852020-08-29 13:39:17 +02002055 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2056 return FAIL;
2057 si = SCRIPT_ITEM(current_sctx.sc_sid);
2058 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002059 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002060 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002061 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002062 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2063 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064 }
2065 if (idx >= 0)
2066 {
2067 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2068
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002069 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070 current_sctx.sc_sid, idx, sv->sv_type);
2071 return OK;
2072 }
2073
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002074 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002075 if (import != NULL)
2076 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002077 if (import->imp_all)
2078 {
2079 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002080 char_u *exp_name;
2081 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002082 ufunc_T *ufunc;
2083 type_T *type;
2084
2085 // Used "import * as Name", need to lookup the member.
2086 if (*p != '.')
2087 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002088 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002089 return FAIL;
2090 }
2091 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002092 if (VIM_ISWHITE(*p))
2093 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002094 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002095 return FAIL;
2096 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002097
Bram Moolenaar1c991142020-07-04 13:15:31 +02002098 // isolate one name
2099 exp_name = p;
2100 while (eval_isnamec(*p))
2101 ++p;
2102 cc = *p;
2103 *p = NUL;
2104
2105 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2106 *p = cc;
2107 p = skipwhite(p);
2108
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002109 // TODO: what if it is a function?
2110 if (idx < 0)
2111 return FAIL;
2112 *end = p;
2113
2114 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2115 import->imp_sid,
2116 idx,
2117 type);
2118 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002119 else if (import->imp_funcname != NULL)
2120 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002121 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002122 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2123 import->imp_sid,
2124 import->imp_var_vals_idx,
2125 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002126 return OK;
2127 }
2128
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002129 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002130 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131 return FAIL;
2132}
2133
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002134 static int
2135generate_funcref(cctx_T *cctx, char_u *name)
2136{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002137 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002138
2139 if (ufunc == NULL)
2140 return FAIL;
2141
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002142 // Need to compile any default values to get the argument types.
2143 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2144 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2145 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002146 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002147}
2148
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002149/*
2150 * Compile a variable name into a load instruction.
2151 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002152 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002153 * When "error" is FALSE do not give an error when not found.
2154 */
2155 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002156compile_load(
2157 char_u **arg,
2158 char_u *end_arg,
2159 cctx_T *cctx,
2160 int is_expr,
2161 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162{
2163 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002164 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002165 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002166 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002167 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002168
2169 if (*(*arg + 1) == ':')
2170 {
2171 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002172 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002173 {
2174 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002176 switch (**arg)
2177 {
2178 case 'g': isn_type = ISN_LOADGDICT; break;
2179 case 'w': isn_type = ISN_LOADWDICT; break;
2180 case 't': isn_type = ISN_LOADTDICT; break;
2181 case 'b': isn_type = ISN_LOADBDICT; break;
2182 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002183 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002184 goto theend;
2185 }
2186 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2187 goto theend;
2188 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002189 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002190 else
2191 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002192 isntype_T isn_type = ISN_DROP;
2193
2194 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2195 if (name == NULL)
2196 return FAIL;
2197
2198 switch (**arg)
2199 {
2200 case 'v': res = generate_LOADV(cctx, name, error);
2201 break;
2202 case 's': res = compile_load_scriptvar(cctx, name,
2203 NULL, NULL, error);
2204 break;
2205 case 'g': isn_type = ISN_LOADG; break;
2206 case 'w': isn_type = ISN_LOADW; break;
2207 case 't': isn_type = ISN_LOADT; break;
2208 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002209 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002210 goto theend;
2211 }
2212 if (isn_type != ISN_DROP)
2213 {
2214 // Global, Buffer-local, Window-local and Tabpage-local
2215 // variables can be defined later, thus we don't check if it
2216 // exists, give error at runtime.
2217 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2218 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002219 }
2220 }
2221 else
2222 {
2223 size_t len = end - *arg;
2224 int idx;
2225 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002226 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002227
2228 name = vim_strnsave(*arg, end - *arg);
2229 if (name == NULL)
2230 return FAIL;
2231
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002232 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002233 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002234 if (!gen_load_outer)
2235 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002236 }
2237 else
2238 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002239 lvar_T *lvar = lookup_local(*arg, len, cctx);
2240
2241 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002242 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002243 type = lvar->lv_type;
2244 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002245 if (lvar->lv_from_outer)
2246 gen_load_outer = TRUE;
2247 else
2248 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002249 }
2250 else
2251 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002252 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002253 // already exists in a Vim9 script or when it's imported.
2254 if (lookup_script(*arg, len, TRUE) == OK
2255 || find_imported(name, 0, cctx) != NULL)
2256 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002257
Bram Moolenaar0f769812020-09-12 18:32:34 +02002258 // When evaluating an expression and the name starts with an
2259 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002260 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002261 if (res == FAIL && is_expr
2262 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002263 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264 }
2265 }
2266 if (gen_load)
2267 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002268 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002269 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002270 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002271 cctx->ctx_outer_used = TRUE;
2272 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002273 }
2274
2275 *arg = end;
2276
2277theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002278 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002279 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002280 vim_free(name);
2281 return res;
2282}
2283
2284/*
2285 * Compile the argument expressions.
2286 * "arg" points to just after the "(" and is advanced to after the ")"
2287 */
2288 static int
2289compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2290{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002291 char_u *p = *arg;
2292 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002293
Bram Moolenaare6085c52020-04-12 20:19:16 +02002294 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002295 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002296 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2297 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002298 if (*p == ')')
2299 {
2300 *arg = p + 1;
2301 return OK;
2302 }
2303
Bram Moolenaara5565e42020-05-09 15:44:01 +02002304 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305 return FAIL;
2306 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002307
2308 if (*p != ',' && *skipwhite(p) == ',')
2309 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002310 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002311 p = skipwhite(p);
2312 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002314 {
2315 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002316 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002317 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002318 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002319 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002320 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002321 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002322failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002323 emsg(_(e_missing_close));
2324 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002325}
2326
2327/*
2328 * Compile a function call: name(arg1, arg2)
2329 * "arg" points to "name", "arg + varlen" to the "(".
2330 * "argcount_init" is 1 for "value->method()"
2331 * Instructions:
2332 * EVAL arg1
2333 * EVAL arg2
2334 * BCALL / DCALL / UCALL
2335 */
2336 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002337compile_call(
2338 char_u **arg,
2339 size_t varlen,
2340 cctx_T *cctx,
2341 ppconst_T *ppconst,
2342 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002343{
2344 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002345 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002346 int argcount = argcount_init;
2347 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002348 char_u fname_buf[FLEN_FIXED + 1];
2349 char_u *tofree = NULL;
2350 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002351 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002352 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002353 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002354
Bram Moolenaara5565e42020-05-09 15:44:01 +02002355 // we can evaluate "has('name')" at compile time
2356 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2357 {
2358 char_u *s = skipwhite(*arg + varlen + 1);
2359 typval_T argvars[2];
2360
2361 argvars[0].v_type = VAR_UNKNOWN;
2362 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002363 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002364 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002365 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002366 s = skipwhite(s);
2367 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2368 {
2369 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2370
2371 *arg = s + 1;
2372 argvars[1].v_type = VAR_UNKNOWN;
2373 tv->v_type = VAR_NUMBER;
2374 tv->vval.v_number = 0;
2375 f_has(argvars, tv);
2376 clear_tv(&argvars[0]);
2377 ++ppconst->pp_used;
2378 return OK;
2379 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002380 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002381 }
2382
2383 if (generate_ppconst(cctx, ppconst) == FAIL)
2384 return FAIL;
2385
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002386 if (varlen >= sizeof(namebuf))
2387 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002388 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002389 return FAIL;
2390 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002391 vim_strncpy(namebuf, *arg, varlen);
2392 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393
2394 *arg = skipwhite(*arg + varlen + 1);
2395 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002396 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397
Bram Moolenaara1773442020-08-12 15:21:22 +02002398 is_autoload = vim_strchr(name, '#') != NULL;
2399 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002400 {
2401 int idx;
2402
2403 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002404 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002406 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002407 else
2408 semsg(_(e_unknownfunc), namebuf);
2409 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002410 }
2411
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002412 // If we can find the function by name generate the right call.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002413 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002414 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar0f769812020-09-12 18:32:34 +02002415 if (ufunc != NULL && !func_is_global(ufunc))
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002416 {
2417 res = generate_CALL(cctx, ufunc, argcount);
2418 goto theend;
2419 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002420
2421 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002422 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002423 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002424 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002425 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002426 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002427 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002428 garray_T *stack = &cctx->ctx_type_stack;
2429 type_T *type;
2430
2431 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2432 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002433 goto theend;
2434 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435
Bram Moolenaar0f769812020-09-12 18:32:34 +02002436 // If we can find a global function by name generate the right call.
2437 if (ufunc != NULL)
2438 {
2439 res = generate_CALL(cctx, ufunc, argcount);
2440 goto theend;
2441 }
2442
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002443 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002444 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002445 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002446 res = generate_UCALL(cctx, name, argcount);
2447 else
2448 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002449
2450theend:
2451 vim_free(tofree);
2452 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002453}
2454
2455// like NAMESPACE_CHAR but with 'a' and 'l'.
2456#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2457
2458/*
2459 * Find the end of a variable or function name. Unlike find_name_end() this
2460 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002461 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002462 * Return a pointer to just after the name. Equal to "arg" if there is no
2463 * valid name.
2464 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002465 static char_u *
2466to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467{
2468 char_u *p;
2469
2470 // Quick check for valid starting character.
2471 if (!eval_isnamec1(*arg))
2472 return arg;
2473
2474 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2475 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2476 // and can be used in slice "[n:]".
2477 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002478 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2480 break;
2481 return p;
2482}
2483
2484/*
2485 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002486 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487 */
2488 char_u *
2489to_name_const_end(char_u *arg)
2490{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002491 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 typval_T rettv;
2493
2494 if (p == arg && *arg == '[')
2495 {
2496
2497 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002498 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002499 p = arg;
2500 }
2501 else if (p == arg && *arg == '#' && arg[1] == '{')
2502 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002503 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002504 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002505 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506 p = arg;
2507 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002508
2509 return p;
2510}
2511
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512/*
2513 * parse a list: [expr, expr]
2514 * "*arg" points to the '['.
2515 */
2516 static int
2517compile_list(char_u **arg, cctx_T *cctx)
2518{
2519 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002520 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521 int count = 0;
2522
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002523 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002524 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002525 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002526 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002527 semsg(_(e_list_end), *arg);
2528 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002529 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002530 if (*p == ',')
2531 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002532 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002533 return FAIL;
2534 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002535 if (*p == ']')
2536 {
2537 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002538 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002539 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002540 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002541 break;
2542 ++count;
2543 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002544 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002546 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2547 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002548 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002549 return FAIL;
2550 }
2551 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002552 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553 p = skipwhite(p);
2554 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002555 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002556
2557 generate_NEWLIST(cctx, count);
2558 return OK;
2559}
2560
2561/*
2562 * parse a lambda: {arg, arg -> expr}
2563 * "*arg" points to the '{'.
2564 */
2565 static int
2566compile_lambda(char_u **arg, cctx_T *cctx)
2567{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568 typval_T rettv;
2569 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002570 evalarg_T evalarg;
2571
2572 CLEAR_FIELD(evalarg);
2573 evalarg.eval_flags = EVAL_EVALUATE;
2574 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575
2576 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002577 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002579
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002580 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002581 ++ufunc->uf_refcount;
2582 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002583 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584
2585 // The function will have one line: "return {expr}".
2586 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002587 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002589 clear_evalarg(&evalarg, NULL);
2590
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002591 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002592 {
2593 // The return type will now be known.
2594 set_function_type(ufunc);
2595
2596 return generate_FUNCREF(cctx, ufunc);
2597 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002598
2599 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600 return FAIL;
2601}
2602
2603/*
2604 * Compile a lamda call: expr->{lambda}(args)
2605 * "arg" points to the "{".
2606 */
2607 static int
2608compile_lambda_call(char_u **arg, cctx_T *cctx)
2609{
2610 ufunc_T *ufunc;
2611 typval_T rettv;
2612 int argcount = 1;
2613 int ret = FAIL;
2614
2615 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002616 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002617 return FAIL;
2618
2619 if (**arg != '(')
2620 {
2621 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002622 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002623 else
2624 semsg(_(e_missing_paren), "lambda");
2625 clear_tv(&rettv);
2626 return FAIL;
2627 }
2628
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002629 ufunc = rettv.vval.v_partial->pt_func;
2630 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002631 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002632 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002633
2634 // The function will have one line: "return {expr}".
2635 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002636 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637
2638 // compile the arguments
2639 *arg = skipwhite(*arg + 1);
2640 if (compile_arguments(arg, cctx, &argcount) == OK)
2641 // call the compiled function
2642 ret = generate_CALL(cctx, ufunc, argcount);
2643
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002644 if (ret == FAIL)
2645 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646 return ret;
2647}
2648
2649/*
2650 * parse a dict: {'key': val} or #{key: val}
2651 * "*arg" points to the '{'.
2652 */
2653 static int
2654compile_dict(char_u **arg, cctx_T *cctx, int literal)
2655{
2656 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002657 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658 int count = 0;
2659 dict_T *d = dict_alloc();
2660 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002661 char_u *whitep = *arg;
2662 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002663
2664 if (d == NULL)
2665 return FAIL;
2666 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002667 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 {
2669 char_u *key = NULL;
2670
Bram Moolenaar23c55272020-06-21 16:58:13 +02002671 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002672 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002673 *arg = NULL;
2674 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002675 }
2676
2677 if (**arg == '}')
2678 break;
2679
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002680 if (literal)
2681 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002682 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002683
Bram Moolenaar2c330432020-04-13 14:41:35 +02002684 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002685 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002686 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002687 return FAIL;
2688 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002689 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002690 if (generate_PUSHS(cctx, key) == FAIL)
2691 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002692 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002693 }
2694 else
2695 {
2696 isn_T *isn;
2697
Bram Moolenaara5565e42020-05-09 15:44:01 +02002698 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002700 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2701 if (isn->isn_type == ISN_PUSHS)
2702 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002703 else
2704 {
2705 type_T *keytype = ((type_T **)stack->ga_data)
2706 [stack->ga_len - 1];
2707 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2708 return FAIL;
2709 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710 }
2711
2712 // Check for duplicate keys, if using string keys.
2713 if (key != NULL)
2714 {
2715 item = dict_find(d, key, -1);
2716 if (item != NULL)
2717 {
2718 semsg(_(e_duplicate_key), key);
2719 goto failret;
2720 }
2721 item = dictitem_alloc(key);
2722 if (item != NULL)
2723 {
2724 item->di_tv.v_type = VAR_UNKNOWN;
2725 item->di_tv.v_lock = 0;
2726 if (dict_add(d, item) == FAIL)
2727 dictitem_free(item);
2728 }
2729 }
2730
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002731 if (**arg != ':')
2732 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002733 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002734 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002735 else
2736 semsg(_(e_missing_dict_colon), *arg);
2737 return FAIL;
2738 }
2739 whitep = *arg + 1;
2740 if (!IS_WHITE_OR_NUL(*whitep))
2741 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002742 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002743 return FAIL;
2744 }
2745
2746 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002747 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002748 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002749 *arg = NULL;
2750 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002751 }
2752
Bram Moolenaara5565e42020-05-09 15:44:01 +02002753 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002754 return FAIL;
2755 ++count;
2756
Bram Moolenaar2c330432020-04-13 14:41:35 +02002757 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002758 *arg = skipwhite(*arg);
2759 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002760 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002761 *arg = NULL;
2762 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002763 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764 if (**arg == '}')
2765 break;
2766 if (**arg != ',')
2767 {
2768 semsg(_(e_missing_dict_comma), *arg);
2769 goto failret;
2770 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002771 if (IS_WHITE_OR_NUL(*whitep))
2772 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002773 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002774 return FAIL;
2775 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002776 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777 *arg = skipwhite(*arg + 1);
2778 }
2779
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002780 *arg = *arg + 1;
2781
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002782 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002783 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002784 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002785 *arg += STRLEN(*arg);
2786
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 dict_unref(d);
2788 return generate_NEWDICT(cctx, count);
2789
2790failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002791 if (*arg == NULL)
2792 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002793 dict_unref(d);
2794 return FAIL;
2795}
2796
2797/*
2798 * Compile "&option".
2799 */
2800 static int
2801compile_get_option(char_u **arg, cctx_T *cctx)
2802{
2803 typval_T rettv;
2804 char_u *start = *arg;
2805 int ret;
2806
2807 // parse the option and get the current value to get the type.
2808 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002809 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002810 if (ret == OK)
2811 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002812 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 char_u *name = vim_strnsave(start, *arg - start);
2814 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2815
2816 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2817 vim_free(name);
2818 }
2819 clear_tv(&rettv);
2820
2821 return ret;
2822}
2823
2824/*
2825 * Compile "$VAR".
2826 */
2827 static int
2828compile_get_env(char_u **arg, cctx_T *cctx)
2829{
2830 char_u *start = *arg;
2831 int len;
2832 int ret;
2833 char_u *name;
2834
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835 ++*arg;
2836 len = get_env_len(arg);
2837 if (len == 0)
2838 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002839 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002840 return FAIL;
2841 }
2842
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002843 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002844 name = vim_strnsave(start, len + 1);
2845 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2846 vim_free(name);
2847 return ret;
2848}
2849
2850/*
2851 * Compile "@r".
2852 */
2853 static int
2854compile_get_register(char_u **arg, cctx_T *cctx)
2855{
2856 int ret;
2857
2858 ++*arg;
2859 if (**arg == NUL)
2860 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002861 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 return FAIL;
2863 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002864 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002865 {
2866 emsg_invreg(**arg);
2867 return FAIL;
2868 }
2869 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2870 ++*arg;
2871 return ret;
2872}
2873
2874/*
2875 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002876 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 */
2878 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002879apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002880{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002881 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882
2883 // this works from end to start
2884 while (p > start)
2885 {
2886 --p;
2887 if (*p == '-' || *p == '+')
2888 {
2889 // only '-' has an effect, for '+' we only check the type
2890#ifdef FEAT_FLOAT
2891 if (rettv->v_type == VAR_FLOAT)
2892 {
2893 if (*p == '-')
2894 rettv->vval.v_float = -rettv->vval.v_float;
2895 }
2896 else
2897#endif
2898 {
2899 varnumber_T val;
2900 int error = FALSE;
2901
2902 // tv_get_number_chk() accepts a string, but we don't want that
2903 // here
2904 if (check_not_string(rettv) == FAIL)
2905 return FAIL;
2906 val = tv_get_number_chk(rettv, &error);
2907 clear_tv(rettv);
2908 if (error)
2909 return FAIL;
2910 if (*p == '-')
2911 val = -val;
2912 rettv->v_type = VAR_NUMBER;
2913 rettv->vval.v_number = val;
2914 }
2915 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002916 else if (numeric_only)
2917 {
2918 ++p;
2919 break;
2920 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 else
2922 {
2923 int v = tv2bool(rettv);
2924
2925 // '!' is permissive in the type.
2926 clear_tv(rettv);
2927 rettv->v_type = VAR_BOOL;
2928 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2929 }
2930 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002931 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 return OK;
2933}
2934
2935/*
2936 * Recognize v: variables that are constants and set "rettv".
2937 */
2938 static void
2939get_vim_constant(char_u **arg, typval_T *rettv)
2940{
2941 if (STRNCMP(*arg, "v:true", 6) == 0)
2942 {
2943 rettv->v_type = VAR_BOOL;
2944 rettv->vval.v_number = VVAL_TRUE;
2945 *arg += 6;
2946 }
2947 else if (STRNCMP(*arg, "v:false", 7) == 0)
2948 {
2949 rettv->v_type = VAR_BOOL;
2950 rettv->vval.v_number = VVAL_FALSE;
2951 *arg += 7;
2952 }
2953 else if (STRNCMP(*arg, "v:null", 6) == 0)
2954 {
2955 rettv->v_type = VAR_SPECIAL;
2956 rettv->vval.v_number = VVAL_NULL;
2957 *arg += 6;
2958 }
2959 else if (STRNCMP(*arg, "v:none", 6) == 0)
2960 {
2961 rettv->v_type = VAR_SPECIAL;
2962 rettv->vval.v_number = VVAL_NONE;
2963 *arg += 6;
2964 }
2965}
2966
Bram Moolenaar696ba232020-07-29 21:20:41 +02002967 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02002968get_compare_type(char_u *p, int *len, int *type_is)
2969{
2970 exptype_T type = EXPR_UNKNOWN;
2971 int i;
2972
2973 switch (p[0])
2974 {
2975 case '=': if (p[1] == '=')
2976 type = EXPR_EQUAL;
2977 else if (p[1] == '~')
2978 type = EXPR_MATCH;
2979 break;
2980 case '!': if (p[1] == '=')
2981 type = EXPR_NEQUAL;
2982 else if (p[1] == '~')
2983 type = EXPR_NOMATCH;
2984 break;
2985 case '>': if (p[1] != '=')
2986 {
2987 type = EXPR_GREATER;
2988 *len = 1;
2989 }
2990 else
2991 type = EXPR_GEQUAL;
2992 break;
2993 case '<': if (p[1] != '=')
2994 {
2995 type = EXPR_SMALLER;
2996 *len = 1;
2997 }
2998 else
2999 type = EXPR_SEQUAL;
3000 break;
3001 case 'i': if (p[1] == 's')
3002 {
3003 // "is" and "isnot"; but not a prefix of a name
3004 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3005 *len = 5;
3006 i = p[*len];
3007 if (!isalnum(i) && i != '_')
3008 {
3009 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3010 *type_is = TRUE;
3011 }
3012 }
3013 break;
3014 }
3015 return type;
3016}
3017
3018/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003020 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 */
3022 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003023compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003025 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026
3027 // this works from end to start
3028 while (p > start)
3029 {
3030 --p;
3031 if (*p == '-' || *p == '+')
3032 {
3033 int negate = *p == '-';
3034 isn_T *isn;
3035
3036 // TODO: check type
3037 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3038 {
3039 --p;
3040 if (*p == '-')
3041 negate = !negate;
3042 }
3043 // only '-' has an effect, for '+' we only check the type
3044 if (negate)
3045 isn = generate_instr(cctx, ISN_NEGATENR);
3046 else
3047 isn = generate_instr(cctx, ISN_CHECKNR);
3048 if (isn == NULL)
3049 return FAIL;
3050 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003051 else if (numeric_only)
3052 {
3053 ++p;
3054 break;
3055 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 else
3057 {
3058 int invert = TRUE;
3059
3060 while (p > start && p[-1] == '!')
3061 {
3062 --p;
3063 invert = !invert;
3064 }
3065 if (generate_2BOOL(cctx, invert) == FAIL)
3066 return FAIL;
3067 }
3068 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003069 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 return OK;
3071}
3072
3073/*
3074 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003075 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 */
3077 static int
3078compile_subscript(
3079 char_u **arg,
3080 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003081 char_u *start_leader,
3082 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003083 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003085 char_u *name_start = *end_leader;
3086
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087 for (;;)
3088 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003089 char_u *p = skipwhite(*arg);
3090
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003091 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003092 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003093 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003094
3095 // If a following line starts with "->{" or "->X" advance to that
3096 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003097 // Also if a following line starts with ".x".
3098 if (next != NULL &&
3099 ((next[0] == '-' && next[1] == '>'
3100 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003101 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003102 {
3103 next = next_line_from_context(cctx, TRUE);
3104 if (next == NULL)
3105 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003106 *arg = next;
3107 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003108 }
3109 }
3110
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003111 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3112 // not a function call.
3113 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003115 garray_T *stack = &cctx->ctx_type_stack;
3116 type_T *type;
3117 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003119 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003120 return FAIL;
3121
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003122 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003123 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3124
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003125 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3127 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003128 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 return FAIL;
3130 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003131 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003133 char_u *pstart = p;
3134
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003135 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003136 return FAIL;
3137
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138 // something->method()
3139 // Apply the '!', '-' and '+' first:
3140 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003141 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003144 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003145 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003146 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 if (**arg == '{')
3148 {
3149 // lambda call: list->{lambda}
3150 if (compile_lambda_call(arg, cctx) == FAIL)
3151 return FAIL;
3152 }
3153 else
3154 {
3155 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003156 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003157 if (!eval_isnamec1(*p))
3158 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003159 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003160 return FAIL;
3161 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003162 if (ASCII_ISALPHA(*p) && p[1] == ':')
3163 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003164 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 ;
3166 if (*p != '(')
3167 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003168 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 return FAIL;
3170 }
3171 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003172 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173 return FAIL;
3174 }
3175 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003176 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003178 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003179 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003180 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003181 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003182 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003183
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003184 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003185 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003186 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003187 // TODO: blob index
3188 // TODO: more arguments
3189 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003190 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003191 return FAIL;
3192
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003193 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003194 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003195 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003196 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003197 if (**arg == ':')
3198 // missing first index is equal to zero
3199 generate_PUSHNR(cctx, 0);
3200 else
3201 {
3202 if (compile_expr0(arg, cctx) == FAIL)
3203 return FAIL;
3204 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3205 return FAIL;
3206 *arg = skipwhite(*arg);
3207 }
3208 if (**arg == ':')
3209 {
3210 *arg = skipwhite(*arg + 1);
3211 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3212 return FAIL;
3213 if (**arg == ']')
3214 // missing second index is equal to end of string
3215 generate_PUSHNR(cctx, -1);
3216 else
3217 {
3218 if (compile_expr0(arg, cctx) == FAIL)
3219 return FAIL;
3220 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3221 return FAIL;
3222 *arg = skipwhite(*arg);
3223 }
3224 is_slice = TRUE;
3225 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226
3227 if (**arg != ']')
3228 {
3229 emsg(_(e_missbrac));
3230 return FAIL;
3231 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003232 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003234 // We can index a list and a dict. If we don't know the type
3235 // we can use the index value type.
3236 // TODO: If we don't know use an instruction to figure it out at
3237 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003238 typep = ((type_T **)stack->ga_data) + stack->ga_len
3239 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003240 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003241 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3242 // If the index is a string, the variable must be a Dict.
3243 if (*typep == &t_any && valtype == &t_string)
3244 vtype = VAR_DICT;
3245 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003246 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003247 if (need_type(valtype, &t_number, -1, cctx, FALSE) == FAIL)
3248 return FAIL;
3249 if (is_slice)
3250 {
3251 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
3252 if (need_type(valtype, &t_number, -2, cctx, FALSE) == FAIL)
3253 return FAIL;
3254 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003255 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003256
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003257 if (vtype == VAR_DICT)
3258 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003259 if (is_slice)
3260 {
3261 emsg(_(e_cannot_slice_dictionary));
3262 return FAIL;
3263 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003264 if ((*typep)->tt_type == VAR_DICT)
3265 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003266 else
3267 {
3268 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3269 return FAIL;
3270 *typep = &t_any;
3271 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003272 if (may_generate_2STRING(-1, cctx) == FAIL)
3273 return FAIL;
3274 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3275 return FAIL;
3276 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003277 else if (vtype == VAR_STRING)
3278 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003279 *typep = &t_string;
3280 if ((is_slice
3281 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3282 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003283 return FAIL;
3284 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003285 else if (vtype == VAR_BLOB)
3286 {
3287 emsg("Sorry, blob index and slice not implemented yet");
3288 return FAIL;
3289 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003290 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003291 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003292 if (is_slice)
3293 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003294 if (generate_instr_drop(cctx,
3295 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3296 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003297 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003298 }
Bram Moolenaared591872020-08-15 22:14:53 +02003299 else
3300 {
3301 if ((*typep)->tt_type == VAR_LIST)
3302 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003303 if (generate_instr_drop(cctx,
3304 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3305 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003306 return FAIL;
3307 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003308 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003309 else
3310 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003311 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003312 return FAIL;
3313 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003315 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003317 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003318 return FAIL;
3319
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003320 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003321 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3322 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003323 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003324 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003325 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003326 while (eval_isnamec(*p))
3327 MB_PTR_ADV(p);
3328 if (p == *arg)
3329 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003330 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331 return FAIL;
3332 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003333 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003334 return FAIL;
3335 *arg = p;
3336 }
3337 else
3338 break;
3339 }
3340
3341 // TODO - see handle_subscript():
3342 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3343 // Don't do this when "Func" is already a partial that was bound
3344 // explicitly (pt_auto is FALSE).
3345
3346 return OK;
3347}
3348
3349/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003350 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3351 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003352 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003353 * If the value is a constant "ppconst->pp_ret" will be set.
3354 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003355 *
3356 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357 */
3358
3359/*
3360 * number number constant
3361 * 0zFFFFFFFF Blob constant
3362 * "string" string constant
3363 * 'string' literal string constant
3364 * &option-name option value
3365 * @r register contents
3366 * identifier variable value
3367 * function() function call
3368 * $VAR environment variable
3369 * (expression) nested expression
3370 * [expr, expr] List
3371 * {key: val, key: val} Dictionary
3372 * #{key: val, key: val} Dictionary with literal keys
3373 *
3374 * Also handle:
3375 * ! in front logical NOT
3376 * - in front unary minus
3377 * + in front unary plus (ignored)
3378 * trailing (arg) funcref/partial call
3379 * trailing [] subscript in String or List
3380 * trailing .name entry in Dictionary
3381 * trailing ->name() method call
3382 */
3383 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003384compile_expr7(
3385 char_u **arg,
3386 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003387 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389 char_u *start_leader, *end_leader;
3390 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003391 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003392 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393
3394 /*
3395 * Skip '!', '-' and '+' characters. They are handled later.
3396 */
3397 start_leader = *arg;
3398 while (**arg == '!' || **arg == '-' || **arg == '+')
3399 *arg = skipwhite(*arg + 1);
3400 end_leader = *arg;
3401
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003402 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403 switch (**arg)
3404 {
3405 /*
3406 * Number constant.
3407 */
3408 case '0': // also for blob starting with 0z
3409 case '1':
3410 case '2':
3411 case '3':
3412 case '4':
3413 case '5':
3414 case '6':
3415 case '7':
3416 case '8':
3417 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003418 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003420 // Apply "-" and "+" just before the number now, right to
3421 // left. Matters especially when "->" follows. Stops at
3422 // '!'.
3423 if (apply_leader(rettv, TRUE,
3424 start_leader, &end_leader) == FAIL)
3425 {
3426 clear_tv(rettv);
3427 return FAIL;
3428 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429 break;
3430
3431 /*
3432 * String constant: "string".
3433 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003434 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 return FAIL;
3436 break;
3437
3438 /*
3439 * Literal string constant: 'str''ing'.
3440 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003441 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 return FAIL;
3443 break;
3444
3445 /*
3446 * Constant Vim variable.
3447 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003448 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 ret = NOTDONE;
3450 break;
3451
3452 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003453 * "true" constant
3454 */
3455 case 't': if (STRNCMP(*arg, "true", 4) == 0
3456 && !eval_isnamec((*arg)[4]))
3457 {
3458 *arg += 4;
3459 rettv->v_type = VAR_BOOL;
3460 rettv->vval.v_number = VVAL_TRUE;
3461 }
3462 else
3463 ret = NOTDONE;
3464 break;
3465
3466 /*
3467 * "false" constant
3468 */
3469 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3470 && !eval_isnamec((*arg)[5]))
3471 {
3472 *arg += 5;
3473 rettv->v_type = VAR_BOOL;
3474 rettv->vval.v_number = VVAL_FALSE;
3475 }
3476 else
3477 ret = NOTDONE;
3478 break;
3479
3480 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481 * List: [expr, expr]
3482 */
3483 case '[': ret = compile_list(arg, cctx);
3484 break;
3485
3486 /*
3487 * Dictionary: #{key: val, key: val}
3488 */
3489 case '#': if ((*arg)[1] == '{')
3490 {
3491 ++*arg;
3492 ret = compile_dict(arg, cctx, TRUE);
3493 }
3494 else
3495 ret = NOTDONE;
3496 break;
3497
3498 /*
3499 * Lambda: {arg, arg -> expr}
3500 * Dictionary: {'key': val, 'key': val}
3501 */
3502 case '{': {
3503 char_u *start = skipwhite(*arg + 1);
3504
3505 // Find out what comes after the arguments.
3506 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003507 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003508 if (ret != FAIL && *start == '>')
3509 ret = compile_lambda(arg, cctx);
3510 else
3511 ret = compile_dict(arg, cctx, FALSE);
3512 }
3513 break;
3514
3515 /*
3516 * Option value: &name
3517 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003518 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3519 return FAIL;
3520 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003521 break;
3522
3523 /*
3524 * Environment variable: $VAR.
3525 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003526 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3527 return FAIL;
3528 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003529 break;
3530
3531 /*
3532 * Register contents: @r.
3533 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003534 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3535 return FAIL;
3536 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003537 break;
3538 /*
3539 * nested expression: (expression).
3540 */
3541 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003542
3543 // recursive!
3544 if (ppconst->pp_used <= PPSIZE - 10)
3545 {
3546 ret = compile_expr1(arg, cctx, ppconst);
3547 }
3548 else
3549 {
3550 // Not enough space in ppconst, flush constants.
3551 if (generate_ppconst(cctx, ppconst) == FAIL)
3552 return FAIL;
3553 ret = compile_expr0(arg, cctx);
3554 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555 *arg = skipwhite(*arg);
3556 if (**arg == ')')
3557 ++*arg;
3558 else if (ret == OK)
3559 {
3560 emsg(_(e_missing_close));
3561 ret = FAIL;
3562 }
3563 break;
3564
3565 default: ret = NOTDONE;
3566 break;
3567 }
3568 if (ret == FAIL)
3569 return FAIL;
3570
Bram Moolenaar1c747212020-05-09 18:28:34 +02003571 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003573 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003574 clear_tv(rettv);
3575 else
3576 // A constant expression can possibly be handled compile time,
3577 // return the value instead of generating code.
3578 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003579 }
3580 else if (ret == NOTDONE)
3581 {
3582 char_u *p;
3583 int r;
3584
3585 if (!eval_isnamec1(**arg))
3586 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003587 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003588 return FAIL;
3589 }
3590
3591 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003592 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003594 {
3595 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3596 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003598 {
3599 if (generate_ppconst(cctx, ppconst) == FAIL)
3600 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003601 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003602 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003603 if (r == FAIL)
3604 return FAIL;
3605 }
3606
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003607 // Handle following "[]", ".member", etc.
3608 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003609 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003610 ppconst) == FAIL)
3611 return FAIL;
3612 if (ppconst->pp_used > 0)
3613 {
3614 // apply the '!', '-' and '+' before the constant
3615 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003616 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003617 return FAIL;
3618 return OK;
3619 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003620 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003622 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623}
3624
3625/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003626 * Give the "white on both sides" error, taking the operator from "p[len]".
3627 */
3628 void
3629error_white_both(char_u *op, int len)
3630{
3631 char_u buf[10];
3632
3633 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003634 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003635}
3636
3637/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003638 * <type>expr7: runtime type check / conversion
3639 */
3640 static int
3641compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3642{
3643 type_T *want_type = NULL;
3644
3645 // Recognize <type>
3646 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3647 {
3648 int called_emsg_before = called_emsg;
3649
3650 ++*arg;
3651 want_type = parse_type(arg, cctx->ctx_type_list);
3652 if (called_emsg != called_emsg_before)
3653 return FAIL;
3654
3655 if (**arg != '>')
3656 {
3657 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003658 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003659 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003660 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003661 return FAIL;
3662 }
3663 ++*arg;
3664 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3665 return FAIL;
3666 }
3667
3668 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3669 return FAIL;
3670
3671 if (want_type != NULL)
3672 {
3673 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003674 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003675
Bram Moolenaard1103582020-08-14 22:44:25 +02003676 generate_ppconst(cctx, ppconst);
3677 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003678 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003679 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003680 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3681 return FAIL;
3682 }
3683 }
3684
3685 return OK;
3686}
3687
3688/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689 * * number multiplication
3690 * / number division
3691 * % number modulo
3692 */
3693 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003694compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695{
3696 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003697 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003698 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003700 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003701 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 return FAIL;
3703
3704 /*
3705 * Repeat computing, until no "*", "/" or "%" is following.
3706 */
3707 for (;;)
3708 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003709 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 if (*op != '*' && *op != '/' && *op != '%')
3711 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003712 if (next != NULL)
3713 {
3714 *arg = next_line_from_context(cctx, TRUE);
3715 op = skipwhite(*arg);
3716 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003717
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003718 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003720 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003721 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003722 }
3723 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003724 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003725 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003727 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003728 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003730
3731 if (ppconst->pp_used == ppconst_used + 2
3732 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3733 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003734 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003735 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3736 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003737 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003739 // both are numbers: compute the result
3740 switch (*op)
3741 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003742 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003743 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003744 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003745 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003746 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003747 break;
3748 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003749 tv1->vval.v_number = res;
3750 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003751 }
3752 else
3753 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003754 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003755 generate_two_op(cctx, op);
3756 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003757 }
3758
3759 return OK;
3760}
3761
3762/*
3763 * + number addition
3764 * - number subtraction
3765 * .. string concatenation
3766 */
3767 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003768compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769{
3770 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003771 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003773 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774
3775 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003776 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 return FAIL;
3778
3779 /*
3780 * Repeat computing, until no "+", "-" or ".." is following.
3781 */
3782 for (;;)
3783 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003784 op = may_peek_next_line(cctx, *arg, &next);
3785 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786 break;
3787 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003788 if (next != NULL)
3789 {
3790 *arg = next_line_from_context(cctx, TRUE);
3791 op = skipwhite(*arg);
3792 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003793
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003794 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003796 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003797 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 }
3799
3800 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003801 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003802 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003804 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003805 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 return FAIL;
3807
Bram Moolenaara5565e42020-05-09 15:44:01 +02003808 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003809 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003810 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3811 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3812 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3813 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003815 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3816 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003817
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003818 // concat/subtract/add constant numbers
3819 if (*op == '+')
3820 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3821 else if (*op == '-')
3822 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3823 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003824 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003825 // concatenate constant strings
3826 char_u *s1 = tv1->vval.v_string;
3827 char_u *s2 = tv2->vval.v_string;
3828 size_t len1 = STRLEN(s1);
3829
3830 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3831 if (tv1->vval.v_string == NULL)
3832 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003833 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003834 return FAIL;
3835 }
3836 mch_memmove(tv1->vval.v_string, s1, len1);
3837 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003838 vim_free(s1);
3839 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003840 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003841 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842 }
3843 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003844 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003845 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003846 if (*op == '.')
3847 {
3848 if (may_generate_2STRING(-2, cctx) == FAIL
3849 || may_generate_2STRING(-1, cctx) == FAIL)
3850 return FAIL;
3851 generate_instr_drop(cctx, ISN_CONCAT, 1);
3852 }
3853 else
3854 generate_two_op(cctx, op);
3855 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 }
3857
3858 return OK;
3859}
3860
3861/*
3862 * expr5a == expr5b
3863 * expr5a =~ expr5b
3864 * expr5a != expr5b
3865 * expr5a !~ expr5b
3866 * expr5a > expr5b
3867 * expr5a >= expr5b
3868 * expr5a < expr5b
3869 * expr5a <= expr5b
3870 * expr5a is expr5b
3871 * expr5a isnot expr5b
3872 *
3873 * Produces instructions:
3874 * EVAL expr5a Push result of "expr5a"
3875 * EVAL expr5b Push result of "expr5b"
3876 * COMPARE one of the compare instructions
3877 */
3878 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003879compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880{
3881 exptype_T type = EXPR_UNKNOWN;
3882 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003883 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003884 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003886 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003887
3888 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003889 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003890 return FAIL;
3891
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003892 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003893 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894
3895 /*
3896 * If there is a comparative operator, use it.
3897 */
3898 if (type != EXPR_UNKNOWN)
3899 {
3900 int ic = FALSE; // Default: do not ignore case
3901
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003902 if (next != NULL)
3903 {
3904 *arg = next_line_from_context(cctx, TRUE);
3905 p = skipwhite(*arg);
3906 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907 if (type_is && (p[len] == '?' || p[len] == '#'))
3908 {
3909 semsg(_(e_invexpr2), *arg);
3910 return FAIL;
3911 }
3912 // extra question mark appended: ignore case
3913 if (p[len] == '?')
3914 {
3915 ic = TRUE;
3916 ++len;
3917 }
3918 // extra '#' appended: match case (ignored)
3919 else if (p[len] == '#')
3920 ++len;
3921 // nothing appended: match case
3922
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003923 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003925 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003926 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927 }
3928
3929 // get the second variable
3930 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003931 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003932 return FAIL;
3933
Bram Moolenaara5565e42020-05-09 15:44:01 +02003934 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003935 return FAIL;
3936
Bram Moolenaara5565e42020-05-09 15:44:01 +02003937 if (ppconst->pp_used == ppconst_used + 2)
3938 {
3939 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3940 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3941 int ret;
3942
3943 // Both sides are a constant, compute the result now.
3944 // First check for a valid combination of types, this is more
3945 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003946 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003947 ret = FAIL;
3948 else
3949 {
3950 ret = typval_compare(tv1, tv2, type, ic);
3951 tv1->v_type = VAR_BOOL;
3952 tv1->vval.v_number = tv1->vval.v_number
3953 ? VVAL_TRUE : VVAL_FALSE;
3954 clear_tv(tv2);
3955 --ppconst->pp_used;
3956 }
3957 return ret;
3958 }
3959
3960 generate_ppconst(cctx, ppconst);
3961 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962 }
3963
3964 return OK;
3965}
3966
Bram Moolenaar7f141552020-05-09 17:35:53 +02003967static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3968
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969/*
3970 * Compile || or &&.
3971 */
3972 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003973compile_and_or(
3974 char_u **arg,
3975 cctx_T *cctx,
3976 char *op,
3977 ppconst_T *ppconst,
3978 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003980 char_u *next;
3981 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982 int opchar = *op;
3983
3984 if (p[0] == opchar && p[1] == opchar)
3985 {
3986 garray_T *instr = &cctx->ctx_instr;
3987 garray_T end_ga;
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02003988 garray_T *stack = &cctx->ctx_type_stack;
3989 type_T **typep;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990
3991 /*
3992 * Repeat until there is no following "||" or "&&"
3993 */
3994 ga_init2(&end_ga, sizeof(int), 10);
3995 while (p[0] == opchar && p[1] == opchar)
3996 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003997 if (next != NULL)
3998 {
3999 *arg = next_line_from_context(cctx, TRUE);
4000 p = skipwhite(*arg);
4001 }
4002
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004003 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4004 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004005 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004006 return FAIL;
4007 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008
Bram Moolenaara5565e42020-05-09 15:44:01 +02004009 // TODO: use ppconst if the value is a constant
4010 generate_ppconst(cctx, ppconst);
4011
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012 if (ga_grow(&end_ga, 1) == FAIL)
4013 {
4014 ga_clear(&end_ga);
4015 return FAIL;
4016 }
4017 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4018 ++end_ga.ga_len;
4019 generate_JUMP(cctx, opchar == '|'
4020 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
4021
4022 // eval the next expression
4023 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004024 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004025 return FAIL;
4026
Bram Moolenaara5565e42020-05-09 15:44:01 +02004027 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4028 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029 {
4030 ga_clear(&end_ga);
4031 return FAIL;
4032 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004033
4034 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004035 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004036 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004037
4038 // Fill in the end label in all jumps.
4039 while (end_ga.ga_len > 0)
4040 {
4041 isn_T *isn;
4042
4043 --end_ga.ga_len;
4044 isn = ((isn_T *)instr->ga_data)
4045 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4046 isn->isn_arg.jump.jump_where = instr->ga_len;
4047 }
4048 ga_clear(&end_ga);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004049
4050 // The resulting type can be used as a bool.
4051 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4052 if (*typep != &t_bool)
4053 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004054 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004055
4056 if (type != NULL)
4057 {
4058 *type = **typep;
4059 type->tt_flags |= TTFLAG_BOOL_OK;
4060 *typep = type;
4061 }
4062 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004063 }
4064
4065 return OK;
4066}
4067
4068/*
4069 * expr4a && expr4a && expr4a logical AND
4070 *
4071 * Produces instructions:
4072 * EVAL expr4a Push result of "expr4a"
4073 * JUMP_AND_KEEP_IF_FALSE end
4074 * EVAL expr4b Push result of "expr4b"
4075 * JUMP_AND_KEEP_IF_FALSE end
4076 * EVAL expr4c Push result of "expr4c"
4077 * end:
4078 */
4079 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004080compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004082 int ppconst_used = ppconst->pp_used;
4083
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004085 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004086 return FAIL;
4087
4088 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004089 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090}
4091
4092/*
4093 * expr3a || expr3b || expr3c logical OR
4094 *
4095 * Produces instructions:
4096 * EVAL expr3a Push result of "expr3a"
4097 * JUMP_AND_KEEP_IF_TRUE end
4098 * EVAL expr3b Push result of "expr3b"
4099 * JUMP_AND_KEEP_IF_TRUE end
4100 * EVAL expr3c Push result of "expr3c"
4101 * end:
4102 */
4103 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004104compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004105{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004106 int ppconst_used = ppconst->pp_used;
4107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004109 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004110 return FAIL;
4111
4112 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004113 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004114}
4115
4116/*
4117 * Toplevel expression: expr2 ? expr1a : expr1b
4118 *
4119 * Produces instructions:
4120 * EVAL expr2 Push result of "expr"
4121 * JUMP_IF_FALSE alt jump if false
4122 * EVAL expr1a
4123 * JUMP_ALWAYS end
4124 * alt: EVAL expr1b
4125 * end:
4126 */
4127 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004128compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129{
4130 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004131 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004132 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133
Bram Moolenaar3988f642020-08-27 22:43:03 +02004134 // Ignore all kinds of errors when not producing code.
4135 if (cctx->ctx_skip == SKIP_YES)
4136 {
4137 skip_expr(arg);
4138 return OK;
4139 }
4140
Bram Moolenaar61a89812020-05-07 16:58:17 +02004141 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004142 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004143 return FAIL;
4144
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004145 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146 if (*p == '?')
4147 {
4148 garray_T *instr = &cctx->ctx_instr;
4149 garray_T *stack = &cctx->ctx_type_stack;
4150 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004151 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004153 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004155 int has_const_expr = FALSE;
4156 int const_value = FALSE;
4157 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004158
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004159 if (next != NULL)
4160 {
4161 *arg = next_line_from_context(cctx, TRUE);
4162 p = skipwhite(*arg);
4163 }
4164
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004165 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4166 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004167 semsg(_(e_white_space_required_before_and_after_str), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004168 return FAIL;
4169 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170
Bram Moolenaara5565e42020-05-09 15:44:01 +02004171 if (ppconst->pp_used == ppconst_used + 1)
4172 {
4173 // the condition is a constant, we know whether the ? or the :
4174 // expression is to be evaluated.
4175 has_const_expr = TRUE;
4176 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4177 clear_tv(&ppconst->pp_tv[ppconst_used]);
4178 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004179 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4180 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004181 }
4182 else
4183 {
4184 generate_ppconst(cctx, ppconst);
4185 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4186 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004187
4188 // evaluate the second expression; any type is accepted
4189 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004190 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004191 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004192 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004193 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194
Bram Moolenaara5565e42020-05-09 15:44:01 +02004195 if (!has_const_expr)
4196 {
4197 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198
Bram Moolenaara5565e42020-05-09 15:44:01 +02004199 // remember the type and drop it
4200 --stack->ga_len;
4201 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202
Bram Moolenaara5565e42020-05-09 15:44:01 +02004203 end_idx = instr->ga_len;
4204 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4205
4206 // jump here from JUMP_IF_FALSE
4207 isn = ((isn_T *)instr->ga_data) + alt_idx;
4208 isn->isn_arg.jump.jump_where = instr->ga_len;
4209 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210
4211 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004212 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213 if (*p != ':')
4214 {
4215 emsg(_(e_missing_colon));
4216 return FAIL;
4217 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004218 if (next != NULL)
4219 {
4220 *arg = next_line_from_context(cctx, TRUE);
4221 p = skipwhite(*arg);
4222 }
4223
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004224 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4225 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004226 semsg(_(e_white_space_required_before_and_after_str), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004227 return FAIL;
4228 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229
4230 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004231 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004232 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4233 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004235 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004236 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004237 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004238 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004239
Bram Moolenaara5565e42020-05-09 15:44:01 +02004240 if (!has_const_expr)
4241 {
4242 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004243
Bram Moolenaara5565e42020-05-09 15:44:01 +02004244 // If the types differ, the result has a more generic type.
4245 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4246 common_type(type1, type2, &type2, cctx->ctx_type_list);
4247
4248 // jump here from JUMP_ALWAYS
4249 isn = ((isn_T *)instr->ga_data) + end_idx;
4250 isn->isn_arg.jump.jump_where = instr->ga_len;
4251 }
4252
4253 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254 }
4255 return OK;
4256}
4257
4258/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004259 * Toplevel expression.
4260 */
4261 static int
4262compile_expr0(char_u **arg, cctx_T *cctx)
4263{
4264 ppconst_T ppconst;
4265
4266 CLEAR_FIELD(ppconst);
4267 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4268 {
4269 clear_ppconst(&ppconst);
4270 return FAIL;
4271 }
4272 if (generate_ppconst(cctx, &ppconst) == FAIL)
4273 return FAIL;
4274 return OK;
4275}
4276
4277/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 * compile "return [expr]"
4279 */
4280 static char_u *
4281compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4282{
4283 char_u *p = arg;
4284 garray_T *stack = &cctx->ctx_type_stack;
4285 type_T *stack_type;
4286
4287 if (*p != NUL && *p != '|' && *p != '\n')
4288 {
4289 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004290 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004291 return NULL;
4292
4293 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4294 if (set_return_type)
4295 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004296 else
4297 {
4298 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4299 && stack_type->tt_type != VAR_VOID
4300 && stack_type->tt_type != VAR_UNKNOWN)
4301 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004302 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004303 return NULL;
4304 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004305 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4306 cctx, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004307 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004308 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004309 }
4310 else
4311 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004312 // "set_return_type" cannot be TRUE, only used for a lambda which
4313 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004314 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4315 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004317 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 return NULL;
4319 }
4320
4321 // No argument, return zero.
4322 generate_PUSHNR(cctx, 0);
4323 }
4324
4325 if (generate_instr(cctx, ISN_RETURN) == NULL)
4326 return NULL;
4327
4328 // "return val | endif" is possible
4329 return skipwhite(p);
4330}
4331
4332/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004333 * Get a line from the compilation context, compatible with exarg_T getline().
4334 * Return a pointer to the line in allocated memory.
4335 * Return NULL for end-of-file or some error.
4336 */
4337 static char_u *
4338exarg_getline(
4339 int c UNUSED,
4340 void *cookie,
4341 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004342 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004343{
4344 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004345 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004346
Bram Moolenaar66250c92020-08-20 15:02:42 +02004347 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004348 {
Bram Moolenaar66250c92020-08-20 15:02:42 +02004349 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4350 return NULL;
4351 ++cctx->ctx_lnum;
4352 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4353 // Comment lines result in NULL pointers, skip them.
4354 if (p != NULL)
4355 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004356 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004357}
4358
4359/*
4360 * Compile a nested :def command.
4361 */
4362 static char_u *
4363compile_nested_function(exarg_T *eap, cctx_T *cctx)
4364{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004365 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004366 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004367 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004368 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004369 lvar_T *lvar;
4370 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004371 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004372
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004373 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004374 {
4375 emsg(_(e_cannot_use_bang_with_nested_def));
4376 return NULL;
4377 }
4378
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004379 // Only g:Func() can use a namespace.
4380 if (name_start[1] == ':' && !is_global)
4381 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004382 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004383 return NULL;
4384 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004385 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4386 return NULL;
4387
Bram Moolenaar04b12692020-05-04 23:24:44 +02004388 eap->arg = name_end;
4389 eap->getline = exarg_getline;
4390 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004391 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004392 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004393 lambda_name = get_lambda_name();
4394 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004395
Bram Moolenaar822ba242020-05-24 23:00:18 +02004396 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004397 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004398 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004399 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004400 return NULL;
4401
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004402 if (is_global)
4403 {
4404 char_u *func_name = vim_strnsave(name_start + 2,
4405 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004406
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004407 if (func_name == NULL)
4408 r = FAIL;
4409 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004410 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004411 }
4412 else
4413 {
4414 // Define a local variable for the function reference.
4415 lvar = reserve_local(cctx, name_start, name_end - name_start,
4416 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004417 if (lvar == NULL)
4418 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004419 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004420 return NULL;
4421 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4422 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004423
Bram Moolenaar61a89812020-05-07 16:58:17 +02004424 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004425 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004426}
4427
4428/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429 * Return the length of an assignment operator, or zero if there isn't one.
4430 */
4431 int
4432assignment_len(char_u *p, int *heredoc)
4433{
4434 if (*p == '=')
4435 {
4436 if (p[1] == '<' && p[2] == '<')
4437 {
4438 *heredoc = TRUE;
4439 return 3;
4440 }
4441 return 1;
4442 }
4443 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4444 return 2;
4445 if (STRNCMP(p, "..=", 3) == 0)
4446 return 3;
4447 return 0;
4448}
4449
4450// words that cannot be used as a variable
4451static char *reserved[] = {
4452 "true",
4453 "false",
4454 NULL
4455};
4456
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004457typedef enum {
4458 dest_local,
4459 dest_option,
4460 dest_env,
4461 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004462 dest_buffer,
4463 dest_window,
4464 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004465 dest_vimvar,
4466 dest_script,
4467 dest_reg,
4468} assign_dest_T;
4469
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004471 * Generate the load instruction for "name".
4472 */
4473 static void
4474generate_loadvar(
4475 cctx_T *cctx,
4476 assign_dest_T dest,
4477 char_u *name,
4478 lvar_T *lvar,
4479 type_T *type)
4480{
4481 switch (dest)
4482 {
4483 case dest_option:
4484 // TODO: check the option exists
4485 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4486 break;
4487 case dest_global:
4488 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4489 break;
4490 case dest_buffer:
4491 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4492 break;
4493 case dest_window:
4494 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4495 break;
4496 case dest_tab:
4497 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4498 break;
4499 case dest_script:
4500 compile_load_scriptvar(cctx,
4501 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4502 break;
4503 case dest_env:
4504 // Include $ in the name here
4505 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4506 break;
4507 case dest_reg:
4508 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4509 break;
4510 case dest_vimvar:
4511 generate_LOADV(cctx, name + 2, TRUE);
4512 break;
4513 case dest_local:
4514 if (lvar->lv_from_outer)
4515 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4516 NULL, type);
4517 else
4518 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4519 break;
4520 }
4521}
4522
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004523 void
4524vim9_declare_error(char_u *name)
4525{
4526 char *scope = "";
4527
4528 switch (*name)
4529 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004530 case 'g': scope = _("global"); break;
4531 case 'b': scope = _("buffer"); break;
4532 case 'w': scope = _("window"); break;
4533 case 't': scope = _("tab"); break;
4534 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004535 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004536 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004537 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004538 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004539 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004540 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004541 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004542 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004543 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004544}
4545
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004546/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004547 * Compile declaration and assignment:
4548 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004550 * Return NULL for an error.
4551 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552 */
4553 static char_u *
4554compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4555{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004556 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004557 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004558 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004559 char_u *ret = NULL;
4560 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004561 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004562 int scriptvar_sid = 0;
4563 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004565 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004566 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004567 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004568 int oplen = 0;
4569 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004570 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004571 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004572 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004574 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004576 // Skip over the "var" or "[var, var]" to get to any "=".
4577 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4578 if (p == NULL)
4579 return *arg == '[' ? arg : NULL;
4580
4581 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004582 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004583 // TODO: should we allow this, and figure out type inference from list
4584 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004585 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004586 return NULL;
4587 }
4588
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589 sp = p;
4590 p = skipwhite(p);
4591 op = p;
4592 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004593
4594 if (var_count > 0 && oplen == 0)
4595 // can be something like "[1, 2]->func()"
4596 return arg;
4597
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4599 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004600 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004601 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004602 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004603
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604 if (heredoc)
4605 {
4606 list_T *l;
4607 listitem_T *li;
4608
4609 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004610 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004611 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004612 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613
4614 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004615 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616 {
4617 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4618 li->li_tv.vval.v_string = NULL;
4619 }
4620 generate_NEWLIST(cctx, l->lv_len);
4621 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004622 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004623 list_free(l);
4624 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004625 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004626 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004627 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004629 // for "[var, var] = expr" evaluate the expression here, loop over the
4630 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004631
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004632 p = skipwhite(op + oplen);
4633 if (compile_expr0(&p, cctx) == FAIL)
4634 return NULL;
4635 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004637 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004639 type_T *stacktype;
4640
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004641 stacktype = stack->ga_len == 0 ? &t_void
4642 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004643 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004645 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004646 goto theend;
4647 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004648 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004649 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004650 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004651 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4652 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004653 }
4654 }
4655
4656 /*
4657 * Loop over variables in "[var, var] = expr".
4658 * For "var = expr" and "let var: type" this is done only once.
4659 */
4660 if (var_count > 0)
4661 var_start = skipwhite(arg + 1); // skip over the "["
4662 else
4663 var_start = arg;
4664 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4665 {
4666 char_u *var_end = skip_var_one(var_start, FALSE);
4667 size_t varlen;
4668 int new_local = FALSE;
4669 int opt_type;
4670 int opt_flags = 0;
4671 assign_dest_T dest = dest_local;
4672 int vimvaridx = -1;
4673 lvar_T *lvar = NULL;
4674 lvar_T arg_lvar;
4675 int has_type = FALSE;
4676 int has_index = FALSE;
4677 int instr_count = -1;
4678
Bram Moolenaar65821722020-08-02 18:58:54 +02004679 if (*var_start == '@')
4680 p = var_start + 2;
4681 else
4682 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02004683 // skip over the leading "&", "&l:", "&g:" and "$"
4684 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02004685 p = to_name_end(p, TRUE);
4686 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004687
4688 // "a: type" is declaring variable "a" with a type, not "a:".
4689 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4690 --var_end;
4691 if (is_decl && p == var_start + 2 && p[-1] == ':')
4692 --p;
4693
4694 varlen = p - var_start;
4695 vim_free(name);
4696 name = vim_strnsave(var_start, varlen);
4697 if (name == NULL)
4698 return NULL;
4699 if (!heredoc)
4700 type = &t_any;
4701
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004702 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004703 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004704 int declare_error = FALSE;
4705
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004706 if (*var_start == '&')
4707 {
4708 int cc;
4709 long numval;
4710
4711 dest = dest_option;
4712 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004714 emsg(_(e_const_option));
4715 goto theend;
4716 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004717 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004718 p = var_start;
4719 p = find_option_end(&p, &opt_flags);
4720 if (p == NULL)
4721 {
4722 // cannot happen?
4723 emsg(_(e_letunexp));
4724 goto theend;
4725 }
4726 cc = *p;
4727 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02004728 opt_type = get_option_value(skip_option_env_lead(var_start),
4729 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004730 *p = cc;
4731 if (opt_type == -3)
4732 {
4733 semsg(_(e_unknown_option), var_start);
4734 goto theend;
4735 }
4736 if (opt_type == -2 || opt_type == 0)
4737 type = &t_string;
4738 else
4739 type = &t_number; // both number and boolean option
4740 }
4741 else if (*var_start == '$')
4742 {
4743 dest = dest_env;
4744 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004745 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004746 }
4747 else if (*var_start == '@')
4748 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004749 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004750 {
4751 emsg_invreg(var_start[1]);
4752 goto theend;
4753 }
4754 dest = dest_reg;
4755 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004756 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004757 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004758 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004759 {
4760 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004761 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004762 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004763 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004764 {
4765 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004766 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004767 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004768 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004769 {
4770 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004771 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004772 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004773 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004774 {
4775 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004776 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004777 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004778 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004779 {
4780 typval_T *vtv;
4781 int di_flags;
4782
4783 vimvaridx = find_vim_var(name + 2, &di_flags);
4784 if (vimvaridx < 0)
4785 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004786 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004787 goto theend;
4788 }
4789 // We use the current value of "sandbox" here, is that OK?
4790 if (var_check_ro(di_flags, name, FALSE))
4791 goto theend;
4792 dest = dest_vimvar;
4793 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004794 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004795 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004796 }
4797 else
4798 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004799 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004800
4801 for (idx = 0; reserved[idx] != NULL; ++idx)
4802 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004803 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004804 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004805 goto theend;
4806 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004807
4808 lvar = lookup_local(var_start, varlen, cctx);
4809 if (lvar == NULL)
4810 {
4811 CLEAR_FIELD(arg_lvar);
4812 if (lookup_arg(var_start, varlen,
4813 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4814 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004815 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004816 if (is_decl)
4817 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004818 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004819 goto theend;
4820 }
4821 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004822 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004823 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004824 if (lvar != NULL)
4825 {
4826 if (is_decl)
4827 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004828 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004829 goto theend;
4830 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004831 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004832 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004833 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004834 int script_namespace = varlen > 1
4835 && STRNCMP(var_start, "s:", 2) == 0;
4836 int script_var = (script_namespace
Bram Moolenaar53900992020-08-22 19:02:02 +02004837 ? lookup_script(var_start + 2, varlen - 2, FALSE)
4838 : lookup_script(var_start, varlen, TRUE)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004839 imported_T *import =
4840 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004841
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004842 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004843 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004844 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4845
4846 if (is_decl)
4847 {
4848 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004849 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004850 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004851 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004852 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004853 name);
4854 goto theend;
4855 }
4856 else if (cctx->ctx_ufunc->uf_script_ctx_version
4857 == SCRIPT_VERSION_VIM9
4858 && script_namespace
4859 && !script_var && import == NULL)
4860 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004861 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004862 goto theend;
4863 }
4864
4865 dest = dest_script;
4866
4867 // existing script-local variables should have a type
4868 scriptvar_sid = current_sctx.sc_sid;
4869 if (import != NULL)
4870 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004871 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004872 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02004873 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4874 rawname, TRUE);
4875 if (scriptvar_idx > 0)
4876 {
4877 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4878 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004879 ((svar_T *)si->sn_var_vals.ga_data)
4880 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004881 type = sv->sv_type;
4882 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004883 }
4884 }
4885 else if (name[1] == ':' && name[2] != NUL)
4886 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004887 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004888 goto theend;
4889 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004890 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004891 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004892 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004893 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004894 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004895 else if (check_defined(var_start, varlen, cctx) == FAIL)
4896 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004897 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004898 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004899
4900 if (declare_error)
4901 {
4902 vim9_declare_error(name);
4903 goto theend;
4904 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004905 }
4906
4907 // handle "a:name" as a name, not index "name" on "a"
4908 if (varlen > 1 || var_start[varlen] != ':')
4909 p = var_end;
4910
4911 if (dest != dest_option)
4912 {
4913 if (is_decl && *p == ':')
4914 {
4915 // parse optional type: "let var: type = expr"
4916 if (!VIM_ISWHITE(p[1]))
4917 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004918 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004919 goto theend;
4920 }
4921 p = skipwhite(p + 1);
4922 type = parse_type(&p, cctx->ctx_type_list);
4923 has_type = TRUE;
4924 }
4925 else if (lvar != NULL)
4926 type = lvar->lv_type;
4927 }
4928
4929 if (oplen == 3 && !heredoc && dest != dest_global
4930 && type->tt_type != VAR_STRING
4931 && type->tt_type != VAR_ANY)
4932 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004933 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004934 goto theend;
4935 }
4936
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004937 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004938 {
4939 if (oplen > 1 && !heredoc)
4940 {
4941 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004942 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004943 goto theend;
4944 }
4945
4946 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004947 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
4948 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004949 goto theend;
4950 lvar = reserve_local(cctx, var_start, varlen,
4951 cmdidx == CMD_const, type);
4952 if (lvar == NULL)
4953 goto theend;
4954 new_local = TRUE;
4955 }
4956
4957 member_type = type;
4958 if (var_end > var_start + varlen)
4959 {
4960 // Something follows after the variable: "var[idx]".
4961 if (is_decl)
4962 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004963 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004964 goto theend;
4965 }
4966
4967 if (var_start[varlen] == '[')
4968 {
4969 has_index = TRUE;
4970 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004971 member_type = &t_any;
4972 else
4973 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004974 }
4975 else
4976 {
4977 semsg("Not supported yet: %s", var_start);
4978 goto theend;
4979 }
4980 }
4981 else if (lvar == &arg_lvar)
4982 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004983 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004984 goto theend;
4985 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02004986 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
4987 {
4988 semsg(_(e_cannot_assign_to_constant), name);
4989 goto theend;
4990 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004991
4992 if (!heredoc)
4993 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004994 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004995 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004996 if (oplen > 0 && var_count == 0)
4997 {
4998 // skip over the "=" and the expression
4999 p = skipwhite(op + oplen);
5000 compile_expr0(&p, cctx);
5001 }
5002 }
5003 else if (oplen > 0)
5004 {
5005 type_T *stacktype;
5006
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005007 // For "var = expr" evaluate the expression.
5008 if (var_count == 0)
5009 {
5010 int r;
5011
5012 // for "+=", "*=", "..=" etc. first load the current value
5013 if (*op != '=')
5014 {
5015 generate_loadvar(cctx, dest, name, lvar, type);
5016
5017 if (has_index)
5018 {
5019 // TODO: get member from list or dict
5020 emsg("Index with operation not supported yet");
5021 goto theend;
5022 }
5023 }
5024
5025 // Compile the expression. Temporarily hide the new local
5026 // variable here, it is not available to this expression.
5027 if (new_local)
5028 --cctx->ctx_locals.ga_len;
5029 instr_count = instr->ga_len;
5030 p = skipwhite(op + oplen);
5031 r = compile_expr0(&p, cctx);
5032 if (new_local)
5033 ++cctx->ctx_locals.ga_len;
5034 if (r == FAIL)
5035 goto theend;
5036 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005037 else if (semicolon && var_idx == var_count - 1)
5038 {
5039 // For "[var; var] = expr" get the rest of the list
5040 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5041 goto theend;
5042 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005043 else
5044 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005045 // For "[var, var] = expr" get the "var_idx" item from the
5046 // list.
5047 if (generate_GETITEM(cctx, var_idx) == FAIL)
5048 return FAIL;
5049 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005050
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005051 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005052 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005053 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005054 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005055 if ((stacktype->tt_type == VAR_FUNC
5056 || stacktype->tt_type == VAR_PARTIAL)
5057 && var_wrong_func_name(name, TRUE))
5058 goto theend;
5059
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005060 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005061 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005062 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005063 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005064 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005065 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005066 }
5067 else
5068 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005069 // An empty list or dict has a &t_void member,
5070 // for a variable that implies &t_any.
5071 if (stacktype == &t_list_empty)
5072 lvar->lv_type = &t_list_any;
5073 else if (stacktype == &t_dict_empty)
5074 lvar->lv_type = &t_dict_any;
5075 else
5076 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005077 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005078 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005079 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005080 {
5081 type_T *use_type = lvar->lv_type;
5082
Bram Moolenaar71abe482020-09-14 22:28:30 +02005083 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005084 if (has_index)
5085 {
5086 use_type = use_type->tt_member;
5087 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005088 // could be indexing "any"
5089 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005090 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005091 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005092 == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005093 goto theend;
5094 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005095 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005096 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005097 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005098 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005099 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005100 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005101 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005102 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005103 goto theend;
5104 }
5105 else if (!has_type || dest == dest_option)
5106 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005107 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005108 goto theend;
5109 }
5110 else
5111 {
5112 // variables are always initialized
5113 if (ga_grow(instr, 1) == FAIL)
5114 goto theend;
5115 switch (member_type->tt_type)
5116 {
5117 case VAR_BOOL:
5118 generate_PUSHBOOL(cctx, VVAL_FALSE);
5119 break;
5120 case VAR_FLOAT:
5121#ifdef FEAT_FLOAT
5122 generate_PUSHF(cctx, 0.0);
5123#endif
5124 break;
5125 case VAR_STRING:
5126 generate_PUSHS(cctx, NULL);
5127 break;
5128 case VAR_BLOB:
5129 generate_PUSHBLOB(cctx, NULL);
5130 break;
5131 case VAR_FUNC:
5132 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5133 break;
5134 case VAR_LIST:
5135 generate_NEWLIST(cctx, 0);
5136 break;
5137 case VAR_DICT:
5138 generate_NEWDICT(cctx, 0);
5139 break;
5140 case VAR_JOB:
5141 generate_PUSHJOB(cctx, NULL);
5142 break;
5143 case VAR_CHANNEL:
5144 generate_PUSHCHANNEL(cctx, NULL);
5145 break;
5146 case VAR_NUMBER:
5147 case VAR_UNKNOWN:
5148 case VAR_ANY:
5149 case VAR_PARTIAL:
5150 case VAR_VOID:
5151 case VAR_SPECIAL: // cannot happen
5152 generate_PUSHNR(cctx, 0);
5153 break;
5154 }
5155 }
5156 if (var_count == 0)
5157 end = p;
5158 }
5159
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005160 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005161 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005162 break;
5163
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005164 if (oplen > 0 && *op != '=')
5165 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005166 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005167 type_T *stacktype;
5168
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005169 if (*op == '.')
5170 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005171 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005172 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005173 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005174 if (
5175#ifdef FEAT_FLOAT
5176 // If variable is float operation with number is OK.
5177 !(expected == &t_float && stacktype == &t_number) &&
5178#endif
5179 need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005180 goto theend;
5181
5182 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005183 {
5184 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5185 goto theend;
5186 }
5187 else if (*op == '+')
5188 {
5189 if (generate_add_instr(cctx,
5190 operator_type(member_type, stacktype),
5191 member_type, stacktype) == FAIL)
5192 goto theend;
5193 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005194 else if (generate_two_op(cctx, op) == FAIL)
5195 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005196 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005197
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005198 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005199 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005200 int r;
5201
5202 // Compile the "idx" in "var[idx]".
5203 if (new_local)
5204 --cctx->ctx_locals.ga_len;
5205 p = skipwhite(var_start + varlen + 1);
5206 r = compile_expr0(&p, cctx);
5207 if (new_local)
5208 ++cctx->ctx_locals.ga_len;
5209 if (r == FAIL)
5210 goto theend;
5211 if (*skipwhite(p) != ']')
5212 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005213 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005214 emsg(_(e_missbrac));
5215 goto theend;
5216 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005217 if (type == &t_any)
5218 {
5219 type_T *idx_type = ((type_T **)stack->ga_data)[
5220 stack->ga_len - 1];
5221 // Index on variable of unknown type: guess the type from the
5222 // index type: number is dict, otherwise dict.
5223 // TODO: should do the assignment at runtime
5224 if (idx_type->tt_type == VAR_NUMBER)
5225 type = &t_list_any;
5226 else
5227 type = &t_dict_any;
5228 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005229 if (type->tt_type == VAR_DICT
5230 && may_generate_2STRING(-1, cctx) == FAIL)
5231 goto theend;
5232 if (type->tt_type == VAR_LIST
5233 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005234 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005235 {
5236 emsg(_(e_number_exp));
5237 goto theend;
5238 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005239
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005240 // Load the dict or list. On the stack we then have:
5241 // - value
5242 // - index
5243 // - variable
5244 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005245
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005246 if (type->tt_type == VAR_LIST)
5247 {
5248 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5249 return FAIL;
5250 }
5251 else if (type->tt_type == VAR_DICT)
5252 {
5253 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5254 return FAIL;
5255 }
5256 else
5257 {
5258 emsg(_(e_listreq));
5259 goto theend;
5260 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005261 }
5262 else
5263 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005264 if (is_decl && eap->forceit && cmdidx == CMD_const
5265 && (dest == dest_script || dest == dest_local))
5266 // ":const! var": lock the value, but not referenced variables
5267 generate_LOCKCONST(cctx);
5268
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005269 switch (dest)
5270 {
5271 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005272 generate_STOREOPT(cctx, skip_option_env_lead(name),
5273 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005274 break;
5275 case dest_global:
5276 // include g: with the name, easier to execute that way
5277 generate_STORE(cctx, ISN_STOREG, 0, name);
5278 break;
5279 case dest_buffer:
5280 // include b: with the name, easier to execute that way
5281 generate_STORE(cctx, ISN_STOREB, 0, name);
5282 break;
5283 case dest_window:
5284 // include w: with the name, easier to execute that way
5285 generate_STORE(cctx, ISN_STOREW, 0, name);
5286 break;
5287 case dest_tab:
5288 // include t: with the name, easier to execute that way
5289 generate_STORE(cctx, ISN_STORET, 0, name);
5290 break;
5291 case dest_env:
5292 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5293 break;
5294 case dest_reg:
5295 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5296 break;
5297 case dest_vimvar:
5298 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5299 break;
5300 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005301 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005302 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005303 {
5304 char_u *name_s = name;
5305
5306 // Include s: in the name for store_var()
5307 if (name[1] != ':')
5308 {
5309 int len = (int)STRLEN(name) + 3;
5310
5311 name_s = alloc(len);
5312 if (name_s == NULL)
5313 name_s = name;
5314 else
5315 vim_snprintf((char *)name_s, len,
5316 "s:%s", name);
5317 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005318 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5319 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005320 if (name_s != name)
5321 vim_free(name_s);
5322 }
5323 else
5324 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005325 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005326 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005327 break;
5328 case dest_local:
5329 if (lvar != NULL)
5330 {
5331 isn_T *isn = ((isn_T *)instr->ga_data)
5332 + instr->ga_len - 1;
5333
5334 // optimization: turn "var = 123" from ISN_PUSHNR +
5335 // ISN_STORE into ISN_STORENR
5336 if (!lvar->lv_from_outer
5337 && instr->ga_len == instr_count + 1
5338 && isn->isn_type == ISN_PUSHNR)
5339 {
5340 varnumber_T val = isn->isn_arg.number;
5341
5342 isn->isn_type = ISN_STORENR;
5343 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5344 isn->isn_arg.storenr.stnr_val = val;
5345 if (stack->ga_len > 0)
5346 --stack->ga_len;
5347 }
5348 else if (lvar->lv_from_outer)
5349 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005350 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005351 else
5352 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5353 }
5354 break;
5355 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005356 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005357
5358 if (var_idx + 1 < var_count)
5359 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005360 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005361
5362 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005363 if (var_count > 0 && !semicolon)
5364 {
5365 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5366 goto theend;
5367 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005368
Bram Moolenaarb2097502020-07-19 17:17:02 +02005369 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005370
5371theend:
5372 vim_free(name);
5373 return ret;
5374}
5375
5376/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005377 * Check if "name" can be "unlet".
5378 */
5379 int
5380check_vim9_unlet(char_u *name)
5381{
5382 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5383 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005384 // "unlet s:var" is allowed in legacy script.
5385 if (*name == 's' && !script_is_vim9())
5386 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005387 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005388 return FAIL;
5389 }
5390 return OK;
5391}
5392
5393/*
5394 * Callback passed to ex_unletlock().
5395 */
5396 static int
5397compile_unlet(
5398 lval_T *lvp,
5399 char_u *name_end,
5400 exarg_T *eap,
5401 int deep UNUSED,
5402 void *coookie)
5403{
5404 cctx_T *cctx = coookie;
5405
5406 if (lvp->ll_tv == NULL)
5407 {
5408 char_u *p = lvp->ll_name;
5409 int cc = *name_end;
5410 int ret = OK;
5411
5412 // Normal name. Only supports g:, w:, t: and b: namespaces.
5413 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005414 if (*p == '$')
5415 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5416 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005417 ret = FAIL;
5418 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005419 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005420
5421 *name_end = cc;
5422 return ret;
5423 }
5424
5425 // TODO: unlet {list}[idx]
5426 // TODO: unlet {dict}[key]
5427 emsg("Sorry, :unlet not fully implemented yet");
5428 return FAIL;
5429}
5430
5431/*
5432 * compile "unlet var", "lock var" and "unlock var"
5433 * "arg" points to "var".
5434 */
5435 static char_u *
5436compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5437{
5438 char_u *p = arg;
5439
5440 if (eap->cmdidx != CMD_unlet)
5441 {
5442 emsg("Sorry, :lock and unlock not implemented yet");
5443 return NULL;
5444 }
5445
5446 if (*p == '!')
5447 {
5448 p = skipwhite(p + 1);
5449 eap->forceit = TRUE;
5450 }
5451
5452 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5453 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5454}
5455
5456/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005457 * Compile an :import command.
5458 */
5459 static char_u *
5460compile_import(char_u *arg, cctx_T *cctx)
5461{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005462 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005463}
5464
5465/*
5466 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5467 */
5468 static int
5469compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5470{
5471 garray_T *instr = &cctx->ctx_instr;
5472 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5473
5474 if (endlabel == NULL)
5475 return FAIL;
5476 endlabel->el_next = *el;
5477 *el = endlabel;
5478 endlabel->el_end_label = instr->ga_len;
5479
5480 generate_JUMP(cctx, when, 0);
5481 return OK;
5482}
5483
5484 static void
5485compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5486{
5487 garray_T *instr = &cctx->ctx_instr;
5488
5489 while (*el != NULL)
5490 {
5491 endlabel_T *cur = (*el);
5492 isn_T *isn;
5493
5494 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5495 isn->isn_arg.jump.jump_where = instr->ga_len;
5496 *el = cur->el_next;
5497 vim_free(cur);
5498 }
5499}
5500
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005501 static void
5502compile_free_jump_to_end(endlabel_T **el)
5503{
5504 while (*el != NULL)
5505 {
5506 endlabel_T *cur = (*el);
5507
5508 *el = cur->el_next;
5509 vim_free(cur);
5510 }
5511}
5512
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005513/*
5514 * Create a new scope and set up the generic items.
5515 */
5516 static scope_T *
5517new_scope(cctx_T *cctx, scopetype_T type)
5518{
5519 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5520
5521 if (scope == NULL)
5522 return NULL;
5523 scope->se_outer = cctx->ctx_scope;
5524 cctx->ctx_scope = scope;
5525 scope->se_type = type;
5526 scope->se_local_count = cctx->ctx_locals.ga_len;
5527 return scope;
5528}
5529
5530/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005531 * Free the current scope and go back to the outer scope.
5532 */
5533 static void
5534drop_scope(cctx_T *cctx)
5535{
5536 scope_T *scope = cctx->ctx_scope;
5537
5538 if (scope == NULL)
5539 {
5540 iemsg("calling drop_scope() without a scope");
5541 return;
5542 }
5543 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005544 switch (scope->se_type)
5545 {
5546 case IF_SCOPE:
5547 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5548 case FOR_SCOPE:
5549 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5550 case WHILE_SCOPE:
5551 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5552 case TRY_SCOPE:
5553 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5554 case NO_SCOPE:
5555 case BLOCK_SCOPE:
5556 break;
5557 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005558 vim_free(scope);
5559}
5560
5561/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005562 * compile "if expr"
5563 *
5564 * "if expr" Produces instructions:
5565 * EVAL expr Push result of "expr"
5566 * JUMP_IF_FALSE end
5567 * ... body ...
5568 * end:
5569 *
5570 * "if expr | else" Produces instructions:
5571 * EVAL expr Push result of "expr"
5572 * JUMP_IF_FALSE else
5573 * ... body ...
5574 * JUMP_ALWAYS end
5575 * else:
5576 * ... body ...
5577 * end:
5578 *
5579 * "if expr1 | elseif expr2 | else" Produces instructions:
5580 * EVAL expr Push result of "expr"
5581 * JUMP_IF_FALSE elseif
5582 * ... body ...
5583 * JUMP_ALWAYS end
5584 * elseif:
5585 * EVAL expr Push result of "expr"
5586 * JUMP_IF_FALSE else
5587 * ... body ...
5588 * JUMP_ALWAYS end
5589 * else:
5590 * ... body ...
5591 * end:
5592 */
5593 static char_u *
5594compile_if(char_u *arg, cctx_T *cctx)
5595{
5596 char_u *p = arg;
5597 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005598 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005599 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005600 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005601 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005602
Bram Moolenaara5565e42020-05-09 15:44:01 +02005603 CLEAR_FIELD(ppconst);
5604 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005605 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005606 clear_ppconst(&ppconst);
5607 return NULL;
5608 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005609 if (cctx->ctx_skip == SKIP_YES)
5610 clear_ppconst(&ppconst);
5611 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005612 {
5613 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005614 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005615 clear_ppconst(&ppconst);
5616 }
5617 else
5618 {
5619 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005620 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005621 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005622 return NULL;
5623 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005624
5625 scope = new_scope(cctx, IF_SCOPE);
5626 if (scope == NULL)
5627 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005628 scope->se_skip_save = skip_save;
5629 // "is_had_return" will be reset if any block does not end in :return
5630 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005631
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005632 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005633 {
5634 // "where" is set when ":elseif", "else" or ":endif" is found
5635 scope->se_u.se_if.is_if_label = instr->ga_len;
5636 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5637 }
5638 else
5639 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005640
5641 return p;
5642}
5643
5644 static char_u *
5645compile_elseif(char_u *arg, cctx_T *cctx)
5646{
5647 char_u *p = arg;
5648 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005649 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005650 isn_T *isn;
5651 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005652 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02005653 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005654
5655 if (scope == NULL || scope->se_type != IF_SCOPE)
5656 {
5657 emsg(_(e_elseif_without_if));
5658 return NULL;
5659 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005660 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005661 if (!cctx->ctx_had_return)
5662 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005663
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005664 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005665 {
5666 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005667 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005668 return NULL;
5669 // previous "if" or "elseif" jumps here
5670 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5671 isn->isn_arg.jump.jump_where = instr->ga_len;
5672 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005673
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005674 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005675 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02005676 if (cctx->ctx_skip == SKIP_YES)
5677 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005678 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005679 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005680 clear_ppconst(&ppconst);
5681 return NULL;
5682 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02005683 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005684 if (scope->se_skip_save == SKIP_YES)
5685 clear_ppconst(&ppconst);
5686 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005687 {
5688 // The expression results in a constant.
5689 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005690 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005691 clear_ppconst(&ppconst);
5692 scope->se_u.se_if.is_if_label = -1;
5693 }
5694 else
5695 {
5696 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005697 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005698 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005699 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005700
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005701 // "where" is set when ":elseif", "else" or ":endif" is found
5702 scope->se_u.se_if.is_if_label = instr->ga_len;
5703 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5704 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005705
5706 return p;
5707}
5708
5709 static char_u *
5710compile_else(char_u *arg, cctx_T *cctx)
5711{
5712 char_u *p = arg;
5713 garray_T *instr = &cctx->ctx_instr;
5714 isn_T *isn;
5715 scope_T *scope = cctx->ctx_scope;
5716
5717 if (scope == NULL || scope->se_type != IF_SCOPE)
5718 {
5719 emsg(_(e_else_without_if));
5720 return NULL;
5721 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005722 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005723 if (!cctx->ctx_had_return)
5724 scope->se_u.se_if.is_had_return = FALSE;
5725 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005726
Bram Moolenaarefd88552020-06-18 20:50:10 +02005727 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005728 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005729 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005730 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005731 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005732 if (!cctx->ctx_had_return
5733 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5734 JUMP_ALWAYS, cctx) == FAIL)
5735 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005736 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005737
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005738 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005739 {
5740 if (scope->se_u.se_if.is_if_label >= 0)
5741 {
5742 // previous "if" or "elseif" jumps here
5743 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5744 isn->isn_arg.jump.jump_where = instr->ga_len;
5745 scope->se_u.se_if.is_if_label = -1;
5746 }
5747 }
5748
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005749 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005750 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5751 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005752
5753 return p;
5754}
5755
5756 static char_u *
5757compile_endif(char_u *arg, cctx_T *cctx)
5758{
5759 scope_T *scope = cctx->ctx_scope;
5760 ifscope_T *ifscope;
5761 garray_T *instr = &cctx->ctx_instr;
5762 isn_T *isn;
5763
5764 if (scope == NULL || scope->se_type != IF_SCOPE)
5765 {
5766 emsg(_(e_endif_without_if));
5767 return NULL;
5768 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005769 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005770 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005771 if (!cctx->ctx_had_return)
5772 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005773
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005774 if (scope->se_u.se_if.is_if_label >= 0)
5775 {
5776 // previous "if" or "elseif" jumps here
5777 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5778 isn->isn_arg.jump.jump_where = instr->ga_len;
5779 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005780 // Fill in the "end" label in jumps at the end of the blocks.
5781 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005782 cctx->ctx_skip = scope->se_skip_save;
5783
5784 // If all the blocks end in :return and there is an :else then the
5785 // had_return flag is set.
5786 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005787
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005788 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005789 return arg;
5790}
5791
5792/*
5793 * compile "for var in expr"
5794 *
5795 * Produces instructions:
5796 * PUSHNR -1
5797 * STORE loop-idx Set index to -1
5798 * EVAL expr Push result of "expr"
5799 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5800 * - if beyond end, jump to "end"
5801 * - otherwise get item from list and push it
5802 * STORE var Store item in "var"
5803 * ... body ...
5804 * JUMP top Jump back to repeat
5805 * end: DROP Drop the result of "expr"
5806 *
5807 */
5808 static char_u *
5809compile_for(char_u *arg, cctx_T *cctx)
5810{
5811 char_u *p;
5812 size_t varlen;
5813 garray_T *instr = &cctx->ctx_instr;
5814 garray_T *stack = &cctx->ctx_type_stack;
5815 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005816 lvar_T *loop_lvar; // loop iteration variable
5817 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005818 type_T *vartype;
5819
5820 // TODO: list of variables: "for [key, value] in dict"
5821 // parse "var"
5822 for (p = arg; eval_isnamec1(*p); ++p)
5823 ;
5824 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005825 var_lvar = lookup_local(arg, varlen, cctx);
5826 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005827 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005828 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005829 return NULL;
5830 }
5831
5832 // consume "in"
5833 p = skipwhite(p);
5834 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5835 {
5836 emsg(_(e_missing_in));
5837 return NULL;
5838 }
5839 p = skipwhite(p + 2);
5840
5841
5842 scope = new_scope(cctx, FOR_SCOPE);
5843 if (scope == NULL)
5844 return NULL;
5845
5846 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005847 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5848 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005849 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005850 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005851 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005852 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005853 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005854
5855 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005856 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5857 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005858 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005859 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005860 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005861 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005862 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005863
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005864 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005865
5866 // compile "expr", it remains on the stack until "endfor"
5867 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005868 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005869 {
5870 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
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005874 // Now that we know the type of "var", check that it is a list, now or at
5875 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005876 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005877 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005878 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005879 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005880 return NULL;
5881 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005882 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005883 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005884
5885 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005886 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005887
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005888 generate_FOR(cctx, loop_lvar->lv_idx);
5889 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005890
5891 return arg;
5892}
5893
5894/*
5895 * compile "endfor"
5896 */
5897 static char_u *
5898compile_endfor(char_u *arg, cctx_T *cctx)
5899{
5900 garray_T *instr = &cctx->ctx_instr;
5901 scope_T *scope = cctx->ctx_scope;
5902 forscope_T *forscope;
5903 isn_T *isn;
5904
5905 if (scope == NULL || scope->se_type != FOR_SCOPE)
5906 {
5907 emsg(_(e_for));
5908 return NULL;
5909 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005910 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005911 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005912 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005913
5914 // At end of ":for" scope jump back to the FOR instruction.
5915 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5916
5917 // Fill in the "end" label in the FOR statement so it can jump here
5918 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5919 isn->isn_arg.forloop.for_end = instr->ga_len;
5920
5921 // Fill in the "end" label any BREAK statements
5922 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5923
5924 // Below the ":for" scope drop the "expr" list from the stack.
5925 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5926 return NULL;
5927
5928 vim_free(scope);
5929
5930 return arg;
5931}
5932
5933/*
5934 * compile "while expr"
5935 *
5936 * Produces instructions:
5937 * top: EVAL expr Push result of "expr"
5938 * JUMP_IF_FALSE end jump if false
5939 * ... body ...
5940 * JUMP top Jump back to repeat
5941 * end:
5942 *
5943 */
5944 static char_u *
5945compile_while(char_u *arg, cctx_T *cctx)
5946{
5947 char_u *p = arg;
5948 garray_T *instr = &cctx->ctx_instr;
5949 scope_T *scope;
5950
5951 scope = new_scope(cctx, WHILE_SCOPE);
5952 if (scope == NULL)
5953 return NULL;
5954
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005955 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005956
5957 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005958 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005959 return NULL;
5960
5961 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005962 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005963 JUMP_IF_FALSE, cctx) == FAIL)
5964 return FAIL;
5965
5966 return p;
5967}
5968
5969/*
5970 * compile "endwhile"
5971 */
5972 static char_u *
5973compile_endwhile(char_u *arg, cctx_T *cctx)
5974{
5975 scope_T *scope = cctx->ctx_scope;
5976
5977 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5978 {
5979 emsg(_(e_while));
5980 return NULL;
5981 }
5982 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005983 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005984
5985 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005986 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005987
5988 // Fill in the "end" label in the WHILE statement so it can jump here.
5989 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005990 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005991
5992 vim_free(scope);
5993
5994 return arg;
5995}
5996
5997/*
5998 * compile "continue"
5999 */
6000 static char_u *
6001compile_continue(char_u *arg, cctx_T *cctx)
6002{
6003 scope_T *scope = cctx->ctx_scope;
6004
6005 for (;;)
6006 {
6007 if (scope == NULL)
6008 {
6009 emsg(_(e_continue));
6010 return NULL;
6011 }
6012 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6013 break;
6014 scope = scope->se_outer;
6015 }
6016
6017 // Jump back to the FOR or WHILE instruction.
6018 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006019 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6020 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006021 return arg;
6022}
6023
6024/*
6025 * compile "break"
6026 */
6027 static char_u *
6028compile_break(char_u *arg, cctx_T *cctx)
6029{
6030 scope_T *scope = cctx->ctx_scope;
6031 endlabel_T **el;
6032
6033 for (;;)
6034 {
6035 if (scope == NULL)
6036 {
6037 emsg(_(e_break));
6038 return NULL;
6039 }
6040 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6041 break;
6042 scope = scope->se_outer;
6043 }
6044
6045 // Jump to the end of the FOR or WHILE loop.
6046 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006047 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006048 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006049 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006050 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6051 return FAIL;
6052
6053 return arg;
6054}
6055
6056/*
6057 * compile "{" start of block
6058 */
6059 static char_u *
6060compile_block(char_u *arg, cctx_T *cctx)
6061{
6062 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6063 return NULL;
6064 return skipwhite(arg + 1);
6065}
6066
6067/*
6068 * compile end of block: drop one scope
6069 */
6070 static void
6071compile_endblock(cctx_T *cctx)
6072{
6073 scope_T *scope = cctx->ctx_scope;
6074
6075 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006076 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006077 vim_free(scope);
6078}
6079
6080/*
6081 * compile "try"
6082 * Creates a new scope for the try-endtry, pointing to the first catch and
6083 * finally.
6084 * Creates another scope for the "try" block itself.
6085 * TRY instruction sets up exception handling at runtime.
6086 *
6087 * "try"
6088 * TRY -> catch1, -> finally push trystack entry
6089 * ... try block
6090 * "throw {exception}"
6091 * EVAL {exception}
6092 * THROW create exception
6093 * ... try block
6094 * " catch {expr}"
6095 * JUMP -> finally
6096 * catch1: PUSH exeception
6097 * EVAL {expr}
6098 * MATCH
6099 * JUMP nomatch -> catch2
6100 * CATCH remove exception
6101 * ... catch block
6102 * " catch"
6103 * JUMP -> finally
6104 * catch2: CATCH remove exception
6105 * ... catch block
6106 * " finally"
6107 * finally:
6108 * ... finally block
6109 * " endtry"
6110 * ENDTRY pop trystack entry, may rethrow
6111 */
6112 static char_u *
6113compile_try(char_u *arg, cctx_T *cctx)
6114{
6115 garray_T *instr = &cctx->ctx_instr;
6116 scope_T *try_scope;
6117 scope_T *scope;
6118
6119 // scope that holds the jumps that go to catch/finally/endtry
6120 try_scope = new_scope(cctx, TRY_SCOPE);
6121 if (try_scope == NULL)
6122 return NULL;
6123
6124 // "catch" is set when the first ":catch" is found.
6125 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006126 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006127 if (generate_instr(cctx, ISN_TRY) == NULL)
6128 return NULL;
6129
6130 // scope for the try block itself
6131 scope = new_scope(cctx, BLOCK_SCOPE);
6132 if (scope == NULL)
6133 return NULL;
6134
6135 return arg;
6136}
6137
6138/*
6139 * compile "catch {expr}"
6140 */
6141 static char_u *
6142compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6143{
6144 scope_T *scope = cctx->ctx_scope;
6145 garray_T *instr = &cctx->ctx_instr;
6146 char_u *p;
6147 isn_T *isn;
6148
6149 // end block scope from :try or :catch
6150 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6151 compile_endblock(cctx);
6152 scope = cctx->ctx_scope;
6153
6154 // Error if not in a :try scope
6155 if (scope == NULL || scope->se_type != TRY_SCOPE)
6156 {
6157 emsg(_(e_catch));
6158 return NULL;
6159 }
6160
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006161 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006162 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006163 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006164 return NULL;
6165 }
6166
6167 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006168 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006169 JUMP_ALWAYS, cctx) == FAIL)
6170 return NULL;
6171
6172 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006173 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006174 if (isn->isn_arg.try.try_catch == 0)
6175 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006176 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006177 {
6178 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006179 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006180 isn->isn_arg.jump.jump_where = instr->ga_len;
6181 }
6182
6183 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006184 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006185 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006186 scope->se_u.se_try.ts_caught_all = TRUE;
6187 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006188 }
6189 else
6190 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006191 char_u *end;
6192 char_u *pat;
6193 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006194 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006195 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006196
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006197 // Push v:exception, push {expr} and MATCH
6198 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6199
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006200 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006201 if (*end != *p)
6202 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006203 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006204 vim_free(tofree);
6205 return FAIL;
6206 }
6207 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006208 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006209 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006210 len = (int)(end - tofree);
6211 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006212 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006213 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006214 if (pat == NULL)
6215 return FAIL;
6216 if (generate_PUSHS(cctx, pat) == FAIL)
6217 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006219 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6220 return NULL;
6221
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006222 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006223 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6224 return NULL;
6225 }
6226
6227 if (generate_instr(cctx, ISN_CATCH) == NULL)
6228 return NULL;
6229
6230 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6231 return NULL;
6232 return p;
6233}
6234
6235 static char_u *
6236compile_finally(char_u *arg, cctx_T *cctx)
6237{
6238 scope_T *scope = cctx->ctx_scope;
6239 garray_T *instr = &cctx->ctx_instr;
6240 isn_T *isn;
6241
6242 // end block scope from :try or :catch
6243 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6244 compile_endblock(cctx);
6245 scope = cctx->ctx_scope;
6246
6247 // Error if not in a :try scope
6248 if (scope == NULL || scope->se_type != TRY_SCOPE)
6249 {
6250 emsg(_(e_finally));
6251 return NULL;
6252 }
6253
6254 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006255 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006256 if (isn->isn_arg.try.try_finally != 0)
6257 {
6258 emsg(_(e_finally_dup));
6259 return NULL;
6260 }
6261
6262 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006263 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006264
Bram Moolenaar585fea72020-04-02 22:33:21 +02006265 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006266 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006267 {
6268 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006269 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006270 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006271 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006272 }
6273
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006274 // TODO: set index in ts_finally_label jumps
6275
6276 return arg;
6277}
6278
6279 static char_u *
6280compile_endtry(char_u *arg, cctx_T *cctx)
6281{
6282 scope_T *scope = cctx->ctx_scope;
6283 garray_T *instr = &cctx->ctx_instr;
6284 isn_T *isn;
6285
6286 // end block scope from :catch or :finally
6287 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6288 compile_endblock(cctx);
6289 scope = cctx->ctx_scope;
6290
6291 // Error if not in a :try scope
6292 if (scope == NULL || scope->se_type != TRY_SCOPE)
6293 {
6294 if (scope == NULL)
6295 emsg(_(e_no_endtry));
6296 else if (scope->se_type == WHILE_SCOPE)
6297 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006298 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006299 emsg(_(e_endfor));
6300 else
6301 emsg(_(e_endif));
6302 return NULL;
6303 }
6304
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006305 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006306 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6307 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006308 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006309 return NULL;
6310 }
6311
6312 // Fill in the "end" label in jumps at the end of the blocks, if not done
6313 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006314 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006315
6316 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006317 if (isn->isn_arg.try.try_catch == 0)
6318 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006319 if (isn->isn_arg.try.try_finally == 0)
6320 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006321
6322 if (scope->se_u.se_try.ts_catch_label != 0)
6323 {
6324 // Last catch without match jumps here
6325 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6326 isn->isn_arg.jump.jump_where = instr->ga_len;
6327 }
6328
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006329 compile_endblock(cctx);
6330
6331 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6332 return NULL;
6333 return arg;
6334}
6335
6336/*
6337 * compile "throw {expr}"
6338 */
6339 static char_u *
6340compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6341{
6342 char_u *p = skipwhite(arg);
6343
Bram Moolenaara5565e42020-05-09 15:44:01 +02006344 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006345 return NULL;
6346 if (may_generate_2STRING(-1, cctx) == FAIL)
6347 return NULL;
6348 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6349 return NULL;
6350
6351 return p;
6352}
6353
6354/*
6355 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006356 * compile "echomsg expr"
6357 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006358 * compile "execute expr"
6359 */
6360 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006361compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006362{
6363 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006364 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006365 int count = 0;
6366
6367 for (;;)
6368 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006369 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006370 return NULL;
6371 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006372 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006373 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006374 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006375 break;
6376 }
6377
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006378 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6379 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6380 else if (cmdidx == CMD_execute)
6381 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6382 else if (cmdidx == CMD_echomsg)
6383 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6384 else
6385 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006386 return p;
6387}
6388
6389/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006390 * :put r
6391 * :put ={expr}
6392 */
6393 static char_u *
6394compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6395{
6396 char_u *line = arg;
6397 linenr_T lnum;
6398 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006399 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006400
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006401 eap->regname = *line;
6402
6403 if (eap->regname == '=')
6404 {
6405 char_u *p = line + 1;
6406
6407 if (compile_expr0(&p, cctx) == FAIL)
6408 return NULL;
6409 line = p;
6410 }
6411 else if (eap->regname != NUL)
6412 ++line;
6413
6414 // TODO: if the range is something like "$" need to evaluate at runtime
6415 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6416 return NULL;
6417 if (eap->addr_count == 0)
6418 lnum = -1;
6419 else
6420 lnum = eap->line2;
6421 if (above)
6422 --lnum;
6423
6424 generate_PUT(cctx, eap->regname, lnum);
6425 return line;
6426}
6427
6428/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006429 * A command that is not compiled, execute with legacy code.
6430 */
6431 static char_u *
6432compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6433{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006434 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006435 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006436 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006437
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006438 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006439 goto theend;
6440
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006441 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006442 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006443 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006444 int usefilter = FALSE;
6445
6446 has_expr = argt & (EX_XFILE | EX_EXPAND);
6447
6448 // If the command can be followed by a bar, find the bar and truncate
6449 // it, so that the following command can be compiled.
6450 // The '|' is overwritten with a NUL, it is put back below.
6451 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6452 && *eap->arg == '!')
6453 // :w !filter or :r !filter or :r! filter
6454 usefilter = TRUE;
6455 if ((argt & EX_TRLBAR) && !usefilter)
6456 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006457 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006458 separate_nextcmd(eap);
6459 if (eap->nextcmd != NULL)
6460 nextcmd = eap->nextcmd;
6461 }
6462 }
6463
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006464 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6465 {
6466 // expand filename in "syntax include [@group] filename"
6467 has_expr = TRUE;
6468 eap->arg = skipwhite(eap->arg + 7);
6469 if (*eap->arg == '@')
6470 eap->arg = skiptowhite(eap->arg);
6471 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006472
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006473 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006474 {
6475 int count = 0;
6476 char_u *start = skipwhite(line);
6477
6478 // :cmd xxx`=expr1`yyy`=expr2`zzz
6479 // PUSHS ":cmd xxx"
6480 // eval expr1
6481 // PUSHS "yyy"
6482 // eval expr2
6483 // PUSHS "zzz"
6484 // EXECCONCAT 5
6485 for (;;)
6486 {
6487 if (p > start)
6488 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006489 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006490 ++count;
6491 }
6492 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006493 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006494 return NULL;
6495 may_generate_2STRING(-1, cctx);
6496 ++count;
6497 p = skipwhite(p);
6498 if (*p != '`')
6499 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006500 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006501 return NULL;
6502 }
6503 start = p + 1;
6504
6505 p = (char_u *)strstr((char *)start, "`=");
6506 if (p == NULL)
6507 {
6508 if (*skipwhite(start) != NUL)
6509 {
6510 generate_PUSHS(cctx, vim_strsave(start));
6511 ++count;
6512 }
6513 break;
6514 }
6515 }
6516 generate_EXECCONCAT(cctx, count);
6517 }
6518 else
6519 generate_EXEC(cctx, line);
6520
6521theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006522 if (*nextcmd != NUL)
6523 {
6524 // the parser expects a pointer to the bar, put it back
6525 --nextcmd;
6526 *nextcmd = '|';
6527 }
6528
6529 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006530}
6531
6532/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006533 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006534 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006535 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006536 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006537add_def_function(ufunc_T *ufunc)
6538{
6539 dfunc_T *dfunc;
6540
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006541 if (def_functions.ga_len == 0)
6542 {
6543 // The first position is not used, so that a zero uf_dfunc_idx means it
6544 // wasn't set.
6545 if (ga_grow(&def_functions, 1) == FAIL)
6546 return FAIL;
6547 ++def_functions.ga_len;
6548 }
6549
Bram Moolenaar09689a02020-05-09 22:50:08 +02006550 // Add the function to "def_functions".
6551 if (ga_grow(&def_functions, 1) == FAIL)
6552 return FAIL;
6553 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6554 CLEAR_POINTER(dfunc);
6555 dfunc->df_idx = def_functions.ga_len;
6556 ufunc->uf_dfunc_idx = dfunc->df_idx;
6557 dfunc->df_ufunc = ufunc;
6558 ++def_functions.ga_len;
6559 return OK;
6560}
6561
6562/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006563 * After ex_function() has collected all the function lines: parse and compile
6564 * the lines into instructions.
6565 * Adds the function to "def_functions".
6566 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6567 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006568 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006569 * This can be used recursively through compile_lambda(), which may reallocate
6570 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006571 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006572 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006573 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006574compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006575{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006576 char_u *line = NULL;
6577 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006578 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006579 cctx_T cctx;
6580 garray_T *instr;
6581 int called_emsg_before = called_emsg;
6582 int ret = FAIL;
6583 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006584 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006585 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006586 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006587
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006588 // When using a function that was compiled before: Free old instructions.
6589 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006590 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006591 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006592 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6593 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006594 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006595 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006596 else
6597 {
6598 if (add_def_function(ufunc) == FAIL)
6599 return FAIL;
6600 new_def_function = TRUE;
6601 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006602
Bram Moolenaar985116a2020-07-12 17:31:09 +02006603 ufunc->uf_def_status = UF_COMPILING;
6604
Bram Moolenaara80faa82020-04-12 19:37:17 +02006605 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006606 cctx.ctx_ufunc = ufunc;
6607 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006608 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006609 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6610 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6611 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6612 cctx.ctx_type_list = &ufunc->uf_type_list;
6613 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6614 instr = &cctx.ctx_instr;
6615
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006616 // Set the context to the function, it may be compiled when called from
6617 // another script. Set the script version to the most modern one.
6618 // The line number will be set in next_line_from_context().
6619 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006620 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6621
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006622 // Make sure error messages are OK.
6623 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6624 if (do_estack_push)
6625 estack_push_ufunc(ufunc, 1);
6626
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006627 if (ufunc->uf_def_args.ga_len > 0)
6628 {
6629 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006630 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006631 int i;
6632 char_u *arg;
6633 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006634 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006635
6636 // Produce instructions for the default values of optional arguments.
6637 // Store the instruction index in uf_def_arg_idx[] so that we know
6638 // where to start when the function is called, depending on the number
6639 // of arguments.
6640 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6641 if (ufunc->uf_def_arg_idx == NULL)
6642 goto erret;
6643 for (i = 0; i < count; ++i)
6644 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006645 garray_T *stack = &cctx.ctx_type_stack;
6646 type_T *val_type;
6647 int arg_idx = first_def_arg + i;
6648
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006649 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6650 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006651 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006652 goto erret;
6653
6654 // If no type specified use the type of the default value.
6655 // Otherwise check that the default value type matches the
6656 // specified type.
6657 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6658 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006659 {
6660 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006661 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006662 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02006663 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
6664 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006665 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006666
6667 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006668 goto erret;
6669 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006670 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006671
6672 if (did_set_arg_type)
6673 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006674 }
6675
6676 /*
6677 * Loop over all the lines of the function and generate instructions.
6678 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006679 for (;;)
6680 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006681 exarg_T ea;
6682 cmdmod_T save_cmdmod;
6683 int starts_with_colon = FALSE;
6684 char_u *cmd;
6685 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006686
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006687 // Bail out on the first error to avoid a flood of errors and report
6688 // the right line number when inside try/catch.
6689 if (emsg_before != called_emsg)
6690 goto erret;
6691
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006692 if (line != NULL && *line == '|')
6693 // the line continues after a '|'
6694 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006695 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006696 && !(*line == '#' && (line == cctx.ctx_line_start
6697 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006698 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006699 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006700 goto erret;
6701 }
6702 else
6703 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006704 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006705 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006706 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006707 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006708 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006709 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006710
Bram Moolenaara80faa82020-04-12 19:37:17 +02006711 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006712 ea.cmdlinep = &line;
6713 ea.cmd = skipwhite(line);
6714
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006715 // Some things can be recognized by the first character.
6716 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006717 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006718 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006719 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006720 if (ea.cmd[1] != '{')
6721 {
6722 line = (char_u *)"";
6723 continue;
6724 }
6725 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006726
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006727 case '}':
6728 {
6729 // "}" ends a block scope
6730 scopetype_T stype = cctx.ctx_scope == NULL
6731 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006732
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006733 if (stype == BLOCK_SCOPE)
6734 {
6735 compile_endblock(&cctx);
6736 line = ea.cmd;
6737 }
6738 else
6739 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006740 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006741 goto erret;
6742 }
6743 if (line != NULL)
6744 line = skipwhite(ea.cmd + 1);
6745 continue;
6746 }
6747
6748 case '{':
6749 // "{" starts a block scope
6750 // "{'a': 1}->func() is something else
6751 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6752 {
6753 line = compile_block(ea.cmd, &cctx);
6754 continue;
6755 }
6756 break;
6757
6758 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006759 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006760 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006761 }
6762
6763 /*
6764 * COMMAND MODIFIERS
6765 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006766 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006767 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6768 {
6769 if (errormsg != NULL)
6770 goto erret;
6771 // empty line or comment
6772 line = (char_u *)"";
6773 continue;
6774 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006775 // TODO: use modifiers in the command
6776 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006777 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006778
6779 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006780 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006781 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006782 {
6783 if (*ea.cmd == '(')
6784 // not for "call()"
6785 ea.cmd = p;
6786 else
6787 ea.cmd = skipwhite(ea.cmd);
6788 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006789
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006790 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006791 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006792 char_u *pskip;
6793
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006794 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006795 // find what follows.
6796 // Skip over "var.member", "var[idx]" and the like.
6797 // Also "&opt = val", "$ENV = val" and "@r = val".
6798 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006799 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006800 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006801 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006802 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006803 char_u *var_end;
6804 int oplen;
6805 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006806
Bram Moolenaar65821722020-08-02 18:58:54 +02006807 if (ea.cmd[0] == '@')
6808 var_end = ea.cmd + 2;
6809 else
6810 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006811 FNE_CHECK_START | FNE_INCL_BR);
6812 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006813 if (oplen > 0)
6814 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006815 size_t len = p - ea.cmd;
6816
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006817 // Recognize an assignment if we recognize the variable
6818 // name:
6819 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006820 // "local = expr" where "local" is a local var.
6821 // "script = expr" where "script" is a script-local var.
6822 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006823 // "&opt = expr"
6824 // "$ENV = expr"
6825 // "@r = expr"
6826 if (*ea.cmd == '&'
6827 || *ea.cmd == '$'
6828 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006829 || ((len) > 2 && ea.cmd[1] == ':')
6830 || lookup_local(ea.cmd, len, &cctx) != NULL
6831 || lookup_arg(ea.cmd, len, NULL, NULL,
6832 NULL, &cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02006833 || lookup_script(ea.cmd, len, FALSE) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006834 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006835 {
6836 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006837 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006838 goto erret;
6839 continue;
6840 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006841 }
6842 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006843
6844 if (*ea.cmd == '[')
6845 {
6846 // [var, var] = expr
6847 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6848 if (line == NULL)
6849 goto erret;
6850 if (line != ea.cmd)
6851 continue;
6852 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006853 }
6854
6855 /*
6856 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006857 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006858 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006859 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006860 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006861 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02006862 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006863 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006864 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006865 if (!starts_with_colon)
6866 {
6867 emsg(_(e_colon_required_before_a_range));
6868 goto erret;
6869 }
6870 if (ends_excmd2(line, ea.cmd))
6871 {
6872 // A range without a command: jump to the line.
6873 // TODO: compile to a more efficient command, possibly
6874 // calling parse_cmd_address().
6875 ea.cmdidx = CMD_SIZE;
6876 line = compile_exec(line, &ea, &cctx);
6877 goto nextline;
6878 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006879 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006880 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006881 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006882 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006883 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006884
6885 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6886 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006887 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006888 {
6889 line += STRLEN(line);
6890 continue;
6891 }
6892
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006893 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006894 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006895 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006896 // CMD_let cannot happen, compile_assignment() above is used
6897 iemsg("Command from find_ex_command() not handled");
6898 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006899 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006900 }
6901
Bram Moolenaar3988f642020-08-27 22:43:03 +02006902 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006903 && ea.cmdidx != CMD_elseif
6904 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02006905 && ea.cmdidx != CMD_endif
6906 && ea.cmdidx != CMD_endfor
6907 && ea.cmdidx != CMD_endwhile
6908 && ea.cmdidx != CMD_catch
6909 && ea.cmdidx != CMD_finally
6910 && ea.cmdidx != CMD_endtry)
6911 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02006912 emsg(_(e_unreachable_code_after_return));
6913 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006914 }
6915
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006916 p = skipwhite(p);
6917 if (ea.cmdidx != CMD_SIZE
6918 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
6919 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02006920 if (ea.cmdidx >= 0)
6921 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006922 if ((ea.argt & EX_BANG) && *p == '!')
6923 {
6924 ea.forceit = TRUE;
6925 p = skipwhite(p + 1);
6926 }
6927 }
6928
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006929 switch (ea.cmdidx)
6930 {
6931 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006932 ea.arg = p;
6933 line = compile_nested_function(&ea, &cctx);
6934 break;
6935
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006936 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006937 // TODO: should we allow this, e.g. to declare a global
6938 // function?
6939 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006940 goto erret;
6941
6942 case CMD_return:
6943 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006944 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006945 break;
6946
6947 case CMD_let:
6948 case CMD_const:
6949 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006950 if (line == p)
6951 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006952 break;
6953
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006954 case CMD_unlet:
6955 case CMD_unlockvar:
6956 case CMD_lockvar:
6957 line = compile_unletlock(p, &ea, &cctx);
6958 break;
6959
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006960 case CMD_import:
6961 line = compile_import(p, &cctx);
6962 break;
6963
6964 case CMD_if:
6965 line = compile_if(p, &cctx);
6966 break;
6967 case CMD_elseif:
6968 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006969 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006970 break;
6971 case CMD_else:
6972 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006973 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006974 break;
6975 case CMD_endif:
6976 line = compile_endif(p, &cctx);
6977 break;
6978
6979 case CMD_while:
6980 line = compile_while(p, &cctx);
6981 break;
6982 case CMD_endwhile:
6983 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006984 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006985 break;
6986
6987 case CMD_for:
6988 line = compile_for(p, &cctx);
6989 break;
6990 case CMD_endfor:
6991 line = compile_endfor(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_continue:
6995 line = compile_continue(p, &cctx);
6996 break;
6997 case CMD_break:
6998 line = compile_break(p, &cctx);
6999 break;
7000
7001 case CMD_try:
7002 line = compile_try(p, &cctx);
7003 break;
7004 case CMD_catch:
7005 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007006 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007007 break;
7008 case CMD_finally:
7009 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007010 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007011 break;
7012 case CMD_endtry:
7013 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007014 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007015 break;
7016 case CMD_throw:
7017 line = compile_throw(p, &cctx);
7018 break;
7019
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007020 case CMD_eval:
7021 if (compile_expr0(&p, &cctx) == FAIL)
7022 goto erret;
7023
Bram Moolenaar3988f642020-08-27 22:43:03 +02007024 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007025 generate_instr_drop(&cctx, ISN_DROP, 1);
7026
7027 line = skipwhite(p);
7028 break;
7029
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007030 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007031 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007032 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007033 case CMD_echomsg:
7034 case CMD_echoerr:
7035 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007036 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007037
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007038 case CMD_put:
7039 ea.cmd = cmd;
7040 line = compile_put(p, &ea, &cctx);
7041 break;
7042
Bram Moolenaar3988f642020-08-27 22:43:03 +02007043 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007044
Bram Moolenaarae616492020-07-28 20:07:27 +02007045 case CMD_append:
7046 case CMD_change:
7047 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007048 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007049 case CMD_xit:
7050 not_in_vim9(&ea);
7051 goto erret;
7052
Bram Moolenaar002262f2020-07-08 17:47:57 +02007053 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007054 if (cctx.ctx_skip != SKIP_YES)
7055 {
7056 semsg(_(e_invalid_command_str), ea.cmd);
7057 goto erret;
7058 }
7059 // We don't check for a next command here.
7060 line = (char_u *)"";
7061 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007062
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007063 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007064 if (cctx.ctx_skip == SKIP_YES)
7065 {
7066 // We don't check for a next command here.
7067 line = (char_u *)"";
7068 }
7069 else
7070 {
7071 // Not recognized, execute with do_cmdline_cmd().
7072 ea.arg = p;
7073 line = compile_exec(line, &ea, &cctx);
7074 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007075 break;
7076 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007077nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007078 if (line == NULL)
7079 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007080 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007081
7082 if (cctx.ctx_type_stack.ga_len < 0)
7083 {
7084 iemsg("Type stack underflow");
7085 goto erret;
7086 }
7087 }
7088
7089 if (cctx.ctx_scope != NULL)
7090 {
7091 if (cctx.ctx_scope->se_type == IF_SCOPE)
7092 emsg(_(e_endif));
7093 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7094 emsg(_(e_endwhile));
7095 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7096 emsg(_(e_endfor));
7097 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007098 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007099 goto erret;
7100 }
7101
Bram Moolenaarefd88552020-06-18 20:50:10 +02007102 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007103 {
7104 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7105 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007106 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007107 goto erret;
7108 }
7109
7110 // Return zero if there is no return at the end.
7111 generate_PUSHNR(&cctx, 0);
7112 generate_instr(&cctx, ISN_RETURN);
7113 }
7114
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007115 {
7116 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7117 + ufunc->uf_dfunc_idx;
7118 dfunc->df_deleted = FALSE;
7119 dfunc->df_instr = instr->ga_data;
7120 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007121 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02007122 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007123 if (cctx.ctx_outer_used)
7124 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007125 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007126 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007127
7128 ret = OK;
7129
7130erret:
7131 if (ret == FAIL)
7132 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007133 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007134 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7135 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007136
7137 for (idx = 0; idx < instr->ga_len; ++idx)
7138 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007139 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007140
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007141 // If using the last entry in the table and it was added above, we
7142 // might as well remove it.
7143 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007144 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007145 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007146 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007147 ufunc->uf_dfunc_idx = 0;
7148 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007149 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007150
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007151 while (cctx.ctx_scope != NULL)
7152 drop_scope(&cctx);
7153
Bram Moolenaar20431c92020-03-20 18:39:46 +01007154 // Don't execute this function body.
7155 ga_clear_strings(&ufunc->uf_lines);
7156
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007157 if (errormsg != NULL)
7158 emsg(errormsg);
7159 else if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007160 emsg(_(e_compile_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007161 }
7162
7163 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007164 if (do_estack_push)
7165 estack_pop();
7166
Bram Moolenaar20431c92020-03-20 18:39:46 +01007167 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007168 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007169 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007170 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007171}
7172
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007173 void
7174set_function_type(ufunc_T *ufunc)
7175{
7176 int varargs = ufunc->uf_va_name != NULL;
7177 int argcount = ufunc->uf_args.ga_len;
7178
7179 // Create a type for the function, with the return type and any
7180 // argument types.
7181 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7182 // The type is included in "tt_args".
7183 if (argcount > 0 || varargs)
7184 {
7185 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7186 argcount, &ufunc->uf_type_list);
7187 // Add argument types to the function type.
7188 if (func_type_add_arg_types(ufunc->uf_func_type,
7189 argcount + varargs,
7190 &ufunc->uf_type_list) == FAIL)
7191 return;
7192 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7193 ufunc->uf_func_type->tt_min_argcount =
7194 argcount - ufunc->uf_def_args.ga_len;
7195 if (ufunc->uf_arg_types == NULL)
7196 {
7197 int i;
7198
7199 // lambda does not have argument types.
7200 for (i = 0; i < argcount; ++i)
7201 ufunc->uf_func_type->tt_args[i] = &t_any;
7202 }
7203 else
7204 mch_memmove(ufunc->uf_func_type->tt_args,
7205 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7206 if (varargs)
7207 {
7208 ufunc->uf_func_type->tt_args[argcount] =
7209 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7210 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7211 }
7212 }
7213 else
7214 // No arguments, can use a predefined type.
7215 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7216 argcount, &ufunc->uf_type_list);
7217}
7218
7219
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007220/*
7221 * Delete an instruction, free what it contains.
7222 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007223 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007224delete_instr(isn_T *isn)
7225{
7226 switch (isn->isn_type)
7227 {
7228 case ISN_EXEC:
7229 case ISN_LOADENV:
7230 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007231 case ISN_LOADB:
7232 case ISN_LOADW:
7233 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007234 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007235 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007236 case ISN_PUSHEXC:
7237 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007238 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007239 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007240 case ISN_STOREB:
7241 case ISN_STOREW:
7242 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007243 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007244 vim_free(isn->isn_arg.string);
7245 break;
7246
7247 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007248 case ISN_STORES:
7249 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007250 break;
7251
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007252 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007253 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007254 vim_free(isn->isn_arg.unlet.ul_name);
7255 break;
7256
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007257 case ISN_STOREOPT:
7258 vim_free(isn->isn_arg.storeopt.so_name);
7259 break;
7260
7261 case ISN_PUSHBLOB: // push blob isn_arg.blob
7262 blob_unref(isn->isn_arg.blob);
7263 break;
7264
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007265 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007266#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007267 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007268#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007269 break;
7270
7271 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007272#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007273 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007274#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007275 break;
7276
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007277 case ISN_UCALL:
7278 vim_free(isn->isn_arg.ufunc.cuf_name);
7279 break;
7280
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007281 case ISN_FUNCREF:
7282 {
7283 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7284 + isn->isn_arg.funcref.fr_func;
7285 func_ptr_unref(dfunc->df_ufunc);
7286 }
7287 break;
7288
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007289 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007290 {
7291 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7292 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7293
7294 if (ufunc != NULL)
7295 {
7296 // Clear uf_dfunc_idx so that the function is deleted.
7297 clear_def_function(ufunc);
7298 ufunc->uf_dfunc_idx = 0;
7299 func_ptr_unref(ufunc);
7300 }
7301
7302 vim_free(lambda);
7303 vim_free(isn->isn_arg.newfunc.nf_global);
7304 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007305 break;
7306
Bram Moolenaar5e654232020-09-16 15:22:00 +02007307 case ISN_CHECKTYPE:
7308 free_type(isn->isn_arg.type.ct_type);
7309 break;
7310
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007311 case ISN_2BOOL:
7312 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007313 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007314 case ISN_ADDBLOB:
7315 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007316 case ISN_ANYINDEX:
7317 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007318 case ISN_BCALL:
7319 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007320 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007321 case ISN_CHECKNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007322 case ISN_COMPAREANY:
7323 case ISN_COMPAREBLOB:
7324 case ISN_COMPAREBOOL:
7325 case ISN_COMPAREDICT:
7326 case ISN_COMPAREFLOAT:
7327 case ISN_COMPAREFUNC:
7328 case ISN_COMPARELIST:
7329 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007330 case ISN_COMPARESPECIAL:
7331 case ISN_COMPARESTRING:
7332 case ISN_CONCAT:
7333 case ISN_DCALL:
7334 case ISN_DROP:
7335 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007336 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007337 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007338 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007339 case ISN_EXECCONCAT:
7340 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007341 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007342 case ISN_GETITEM:
7343 case ISN_JUMP:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007344 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007345 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007346 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007347 case ISN_LOADBDICT:
7348 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007349 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007350 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007351 case ISN_LOADSCRIPT:
7352 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007353 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007354 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007355 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007356 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007357 case ISN_NEGATENR:
7358 case ISN_NEWDICT:
7359 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007360 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007361 case ISN_OPFLOAT:
7362 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007363 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007364 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007365 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007366 case ISN_PUSHF:
7367 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007368 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007369 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007370 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007371 case ISN_SHUFFLE:
7372 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007373 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007374 case ISN_STOREDICT:
7375 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007376 case ISN_STORENR:
7377 case ISN_STOREOUTER:
7378 case ISN_STOREREG:
7379 case ISN_STORESCRIPT:
7380 case ISN_STOREV:
7381 case ISN_STRINDEX:
7382 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007383 case ISN_THROW:
7384 case ISN_TRY:
7385 // nothing allocated
7386 break;
7387 }
7388}
7389
7390/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007391 * Free all instructions for "dfunc".
7392 */
7393 static void
7394delete_def_function_contents(dfunc_T *dfunc)
7395{
7396 int idx;
7397
7398 ga_clear(&dfunc->df_def_args_isn);
7399
7400 if (dfunc->df_instr != NULL)
7401 {
7402 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7403 delete_instr(dfunc->df_instr + idx);
7404 VIM_CLEAR(dfunc->df_instr);
7405 }
7406
7407 dfunc->df_deleted = TRUE;
7408}
7409
7410/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007411 * When a user function is deleted, clear the contents of any associated def
7412 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007413 */
7414 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007415clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007416{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007417 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007418 {
7419 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7420 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007421
Bram Moolenaar20431c92020-03-20 18:39:46 +01007422 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007423 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007424 }
7425}
7426
7427#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007428/*
7429 * Free all functions defined with ":def".
7430 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007431 void
7432free_def_functions(void)
7433{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007434 int idx;
7435
7436 for (idx = 0; idx < def_functions.ga_len; ++idx)
7437 {
7438 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7439
7440 delete_def_function_contents(dfunc);
7441 }
7442
7443 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007444}
7445#endif
7446
7447
7448#endif // FEAT_EVAL