blob: 1dbea9280d2681f424dfbb9bc95b001a36c804a4 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
111 int lv_from_outer; // when TRUE using ctx_outer scope
112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200129 int ctx_has_closure; // set to one if a closures was created in
130 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144};
145
Bram Moolenaar20431c92020-03-20 18:39:46 +0100146static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100147
148/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200149 * Lookup variable "name" in the local scope and return it.
150 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200152 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153lookup_local(char_u *name, size_t len, cctx_T *cctx)
154{
155 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200156 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100158 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200159 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200160
161 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
163 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200164 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100165 if (STRNCMP(name, lvar->lv_name, len) == 0
166 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167 {
168 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200169 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200170 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200172
173 // Find local in outer function scope.
174 if (cctx->ctx_outer != NULL)
175 {
176 lvar = lookup_local(name, len, cctx->ctx_outer);
177 if (lvar != NULL)
178 {
179 // TODO: are there situations we should not mark the outer scope as
180 // used?
181 cctx->ctx_outer_used = TRUE;
182 lvar->lv_from_outer = TRUE;
183 return lvar;
184 }
185 }
186
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200187 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100188}
189
190/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200191 * Lookup an argument in the current function and an enclosing function.
192 * Returns the argument index in "idxp"
193 * Returns the argument type in "type"
194 * Sets "gen_load_outer" to TRUE if found in outer scope.
195 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196 */
197 static int
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200198lookup_arg(
199 char_u *name,
200 size_t len,
201 int *idxp,
202 type_T **type,
203 int *gen_load_outer,
204 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100205{
206 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200207 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100209 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200210 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
212 {
213 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
214
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200215 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
216 {
217 if (idxp != NULL)
218 {
219 // Arguments are located above the frame pointer. One further
220 // if there is a vararg argument
221 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
222 + STACK_FRAME_SIZE)
223 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
224
225 if (cctx->ctx_ufunc->uf_arg_types != NULL)
226 *type = cctx->ctx_ufunc->uf_arg_types[idx];
227 else
228 *type = &t_any;
229 }
230 return OK;
231 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100232 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100233
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200234 va_name = cctx->ctx_ufunc->uf_va_name;
235 if (va_name != NULL
236 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
237 {
238 if (idxp != NULL)
239 {
240 // varargs is always the last argument
241 *idxp = -STACK_FRAME_SIZE - 1;
242 *type = cctx->ctx_ufunc->uf_va_type;
243 }
244 return OK;
245 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200247 if (cctx->ctx_outer != NULL)
248 {
249 // Lookup the name for an argument of the outer function.
250 if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
251 == OK)
252 {
253 *gen_load_outer = TRUE;
254 return OK;
255 }
256 }
257
258 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100259}
260
261/*
Bram Moolenaar84367732020-08-23 15:21:55 +0200262 * Returnd TRUE if the script context is Vim9 script.
263 */
264 static int
265script_is_vim9()
266{
267 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
268}
269
270/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 * Lookup a variable in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200272 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
273 * without "s:".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274 * Returns OK or FAIL.
275 */
276 static int
Bram Moolenaar53900992020-08-22 19:02:02 +0200277lookup_script(char_u *name, size_t len, int vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278{
279 int cc;
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200280 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100281 dictitem_T *di;
282
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200283 if (current_sctx.sc_sid <= 0)
284 return FAIL;
285 ht = &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar84367732020-08-23 15:21:55 +0200286 if (vim9script && !script_is_vim9())
Bram Moolenaar53900992020-08-22 19:02:02 +0200287 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100288 cc = name[len];
289 name[len] = NUL;
290 di = find_var_in_ht(ht, 0, name, TRUE);
291 name[len] = cc;
292 return di == NULL ? FAIL: OK;
293}
294
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100295/*
296 * Check if "p[len]" is already defined, either in script "import_sid" or in
297 * compilation context "cctx".
Bram Moolenaar0f769812020-09-12 18:32:34 +0200298 * Does not check the global namespace.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100299 * Return FAIL and give an error if it defined.
300 */
301 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200302check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100303{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200304 int c = p[len];
305 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200306
307 p[len] = NUL;
Bram Moolenaar53900992020-08-22 19:02:02 +0200308 if (lookup_script(p, len, FALSE) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100309 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200310 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaareef21022020-08-01 22:16:43 +0200311 || lookup_arg(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200312 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200313 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100314 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200315 // A local or script-local function can shadow a global function.
316 if (ufunc == NULL || !func_is_global(ufunc)
317 || (p[0] == 'g' && p[1] == ':'))
318 {
319 p[len] = c;
320 semsg(_(e_name_already_defined_str), p);
321 return FAIL;
322 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100323 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200324 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100325 return OK;
326}
327
Bram Moolenaar65b95452020-07-19 14:03:09 +0200328
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100329/////////////////////////////////////////////////////////////////////
330// Following generate_ functions expect the caller to call ga_grow().
331
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200332#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
333#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100334
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100335/*
336 * Generate an instruction without arguments.
337 * Returns a pointer to the new instruction, NULL if failed.
338 */
339 static isn_T *
340generate_instr(cctx_T *cctx, isntype_T isn_type)
341{
342 garray_T *instr = &cctx->ctx_instr;
343 isn_T *isn;
344
Bram Moolenaar080457c2020-03-03 21:53:32 +0100345 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100346 if (ga_grow(instr, 1) == FAIL)
347 return NULL;
348 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
349 isn->isn_type = isn_type;
350 isn->isn_lnum = cctx->ctx_lnum + 1;
351 ++instr->ga_len;
352
353 return isn;
354}
355
356/*
357 * Generate an instruction without arguments.
358 * "drop" will be removed from the stack.
359 * Returns a pointer to the new instruction, NULL if failed.
360 */
361 static isn_T *
362generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
363{
364 garray_T *stack = &cctx->ctx_type_stack;
365
Bram Moolenaar080457c2020-03-03 21:53:32 +0100366 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100367 stack->ga_len -= drop;
368 return generate_instr(cctx, isn_type);
369}
370
371/*
372 * Generate instruction "isn_type" and put "type" on the type stack.
373 */
374 static isn_T *
375generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
376{
377 isn_T *isn;
378 garray_T *stack = &cctx->ctx_type_stack;
379
380 if ((isn = generate_instr(cctx, isn_type)) == NULL)
381 return NULL;
382
383 if (ga_grow(stack, 1) == FAIL)
384 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200385 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100386 ++stack->ga_len;
387
388 return isn;
389}
390
391/*
392 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200393 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100394 */
395 static int
396may_generate_2STRING(int offset, cctx_T *cctx)
397{
398 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200399 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100400 garray_T *stack = &cctx->ctx_type_stack;
401 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
402
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200403 switch ((*type)->tt_type)
404 {
405 // nothing to be done
406 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100407
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200408 // conversion possible
409 case VAR_SPECIAL:
410 case VAR_BOOL:
411 case VAR_NUMBER:
412 case VAR_FLOAT:
413 break;
414
415 // conversion possible (with runtime check)
416 case VAR_ANY:
417 case VAR_UNKNOWN:
418 isntype = ISN_2STRING_ANY;
419 break;
420
421 // conversion not possible
422 case VAR_VOID:
423 case VAR_BLOB:
424 case VAR_FUNC:
425 case VAR_PARTIAL:
426 case VAR_LIST:
427 case VAR_DICT:
428 case VAR_JOB:
429 case VAR_CHANNEL:
430 to_string_error((*type)->tt_type);
431 return FAIL;
432 }
433
434 *type = &t_string;
435 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100436 return FAIL;
437 isn->isn_arg.number = offset;
438
439 return OK;
440}
441
442 static int
443check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
444{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200445 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100446 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200447 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100448 {
449 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200450 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100451 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200452 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100453 return FAIL;
454 }
455 return OK;
456}
457
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200458 static int
459generate_add_instr(
460 cctx_T *cctx,
461 vartype_T vartype,
462 type_T *type1,
463 type_T *type2)
464{
465 isn_T *isn = generate_instr_drop(cctx,
466 vartype == VAR_NUMBER ? ISN_OPNR
467 : vartype == VAR_LIST ? ISN_ADDLIST
468 : vartype == VAR_BLOB ? ISN_ADDBLOB
469#ifdef FEAT_FLOAT
470 : vartype == VAR_FLOAT ? ISN_OPFLOAT
471#endif
472 : ISN_OPANY, 1);
473
474 if (vartype != VAR_LIST && vartype != VAR_BLOB
475 && type1->tt_type != VAR_ANY
476 && type2->tt_type != VAR_ANY
477 && check_number_or_float(
478 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
479 return FAIL;
480
481 if (isn != NULL)
482 isn->isn_arg.op.op_type = EXPR_ADD;
483 return isn == NULL ? FAIL : OK;
484}
485
486/*
487 * Get the type to use for an instruction for an operation on "type1" and
488 * "type2". If they are matching use a type-specific instruction. Otherwise
489 * fall back to runtime type checking.
490 */
491 static vartype_T
492operator_type(type_T *type1, type_T *type2)
493{
494 if (type1->tt_type == type2->tt_type
495 && (type1->tt_type == VAR_NUMBER
496 || type1->tt_type == VAR_LIST
497#ifdef FEAT_FLOAT
498 || type1->tt_type == VAR_FLOAT
499#endif
500 || type1->tt_type == VAR_BLOB))
501 return type1->tt_type;
502 return VAR_ANY;
503}
504
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100505/*
506 * Generate an instruction with two arguments. The instruction depends on the
507 * type of the arguments.
508 */
509 static int
510generate_two_op(cctx_T *cctx, char_u *op)
511{
512 garray_T *stack = &cctx->ctx_type_stack;
513 type_T *type1;
514 type_T *type2;
515 vartype_T vartype;
516 isn_T *isn;
517
Bram Moolenaar080457c2020-03-03 21:53:32 +0100518 RETURN_OK_IF_SKIP(cctx);
519
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200520 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
522 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200523 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524
525 switch (*op)
526 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200527 case '+':
528 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530 break;
531
532 case '-':
533 case '*':
534 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
535 op) == FAIL)
536 return FAIL;
537 if (vartype == VAR_NUMBER)
538 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
539#ifdef FEAT_FLOAT
540 else if (vartype == VAR_FLOAT)
541 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
542#endif
543 else
544 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
545 if (isn != NULL)
546 isn->isn_arg.op.op_type = *op == '*'
547 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
548 break;
549
Bram Moolenaar4c683752020-04-05 21:38:23 +0200550 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200552 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100553 && type2->tt_type != VAR_NUMBER))
554 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200555 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100556 return FAIL;
557 }
558 isn = generate_instr_drop(cctx,
559 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
560 if (isn != NULL)
561 isn->isn_arg.op.op_type = EXPR_REM;
562 break;
563 }
564
565 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200566 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100567 {
568 type_T *type = &t_any;
569
570#ifdef FEAT_FLOAT
571 // float+number and number+float results in float
572 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
573 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
574 type = &t_float;
575#endif
576 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
577 }
578
579 return OK;
580}
581
582/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200583 * Get the instruction to use for comparing "type1" with "type2"
584 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100585 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200586 static isntype_T
587get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100588{
589 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100590
Bram Moolenaar4c683752020-04-05 21:38:23 +0200591 if (type1 == VAR_UNKNOWN)
592 type1 = VAR_ANY;
593 if (type2 == VAR_UNKNOWN)
594 type2 = VAR_ANY;
595
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100596 if (type1 == type2)
597 {
598 switch (type1)
599 {
600 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
601 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
602 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
603 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
604 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
605 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
606 case VAR_LIST: isntype = ISN_COMPARELIST; break;
607 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
608 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100609 default: isntype = ISN_COMPAREANY; break;
610 }
611 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200612 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100613 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
614 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
615 isntype = ISN_COMPAREANY;
616
617 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
618 && (isntype == ISN_COMPAREBOOL
619 || isntype == ISN_COMPARESPECIAL
620 || isntype == ISN_COMPARENR
621 || isntype == ISN_COMPAREFLOAT))
622 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200623 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200625 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100626 }
627 if (isntype == ISN_DROP
628 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
629 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
630 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
631 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
632 && exptype != EXPR_IS && exptype != EXPR_ISNOT
633 && (type1 == VAR_BLOB || type2 == VAR_BLOB
634 || type1 == VAR_LIST || type2 == VAR_LIST))))
635 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200636 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100637 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200638 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100639 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200640 return isntype;
641}
642
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200643 int
644check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
645{
646 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
647 return FAIL;
648 return OK;
649}
650
Bram Moolenaara5565e42020-05-09 15:44:01 +0200651/*
652 * Generate an ISN_COMPARE* instruction with a boolean result.
653 */
654 static int
655generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
656{
657 isntype_T isntype;
658 isn_T *isn;
659 garray_T *stack = &cctx->ctx_type_stack;
660 vartype_T type1;
661 vartype_T type2;
662
663 RETURN_OK_IF_SKIP(cctx);
664
665 // Get the known type of the two items on the stack. If they are matching
666 // use a type-specific instruction. Otherwise fall back to runtime type
667 // checking.
668 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
669 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
670 isntype = get_compare_isn(exptype, type1, type2);
671 if (isntype == ISN_DROP)
672 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673
674 if ((isn = generate_instr(cctx, isntype)) == NULL)
675 return FAIL;
676 isn->isn_arg.op.op_type = exptype;
677 isn->isn_arg.op.op_ic = ic;
678
679 // takes two arguments, puts one bool back
680 if (stack->ga_len >= 2)
681 {
682 --stack->ga_len;
683 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
684 }
685
686 return OK;
687}
688
689/*
690 * Generate an ISN_2BOOL instruction.
691 */
692 static int
693generate_2BOOL(cctx_T *cctx, int invert)
694{
695 isn_T *isn;
696 garray_T *stack = &cctx->ctx_type_stack;
697
Bram Moolenaar080457c2020-03-03 21:53:32 +0100698 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100699 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
700 return FAIL;
701 isn->isn_arg.number = invert;
702
703 // type becomes bool
704 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
705
706 return OK;
707}
708
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200709/*
710 * Generate an ISN_COND2BOOL instruction.
711 */
712 static int
713generate_COND2BOOL(cctx_T *cctx)
714{
715 isn_T *isn;
716 garray_T *stack = &cctx->ctx_type_stack;
717
718 RETURN_OK_IF_SKIP(cctx);
719 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
720 return FAIL;
721
722 // type becomes bool
723 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
724
725 return OK;
726}
727
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200729generate_TYPECHECK(
730 cctx_T *cctx,
731 type_T *expected,
732 int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100733{
734 isn_T *isn;
735 garray_T *stack = &cctx->ctx_type_stack;
736
Bram Moolenaar080457c2020-03-03 21:53:32 +0100737 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100738 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
739 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200740 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100741 isn->isn_arg.type.ct_off = offset;
742
Bram Moolenaar5e654232020-09-16 15:22:00 +0200743 // type becomes expected
744 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100745
746 return OK;
747}
748
749/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200750 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200751 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200752 * - "actual" is a type that can be "expected" type: add a runtime check; or
753 * - return FAIL.
754 */
755 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200756need_type(
757 type_T *actual,
758 type_T *expected,
759 int offset,
760 cctx_T *cctx,
761 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200762{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200763 if (expected == &t_bool && actual != &t_bool
764 && (actual->tt_flags & TTFLAG_BOOL_OK))
765 {
766 // Using "0", "1" or the result of an expression with "&&" or "||" as a
767 // boolean is OK but requires a conversion.
768 generate_2BOOL(cctx, FALSE);
769 return OK;
770 }
771
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200772 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200773 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200774
775 // If the actual type can be the expected type add a runtime check.
776 // TODO: if it's a constant a runtime check makes no sense.
777 if (actual->tt_type == VAR_ANY
778 || actual->tt_type == VAR_UNKNOWN
779 || (actual->tt_type == VAR_FUNC
780 && (expected->tt_type == VAR_FUNC
781 || expected->tt_type == VAR_PARTIAL)
782 && (actual->tt_member == &t_any || actual->tt_argcount < 0))
783 || (actual->tt_type == VAR_LIST
784 && expected->tt_type == VAR_LIST
785 && actual->tt_member == &t_any)
786 || (actual->tt_type == VAR_DICT
787 && expected->tt_type == VAR_DICT
788 && actual->tt_member == &t_any))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200789 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200790 generate_TYPECHECK(cctx, expected, offset);
791 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200792 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200793
794 if (!silent)
795 type_mismatch(expected, actual);
796 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200797}
798
799/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100800 * Generate an ISN_PUSHNR instruction.
801 */
802 static int
803generate_PUSHNR(cctx_T *cctx, varnumber_T number)
804{
805 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200806 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100807
Bram Moolenaar080457c2020-03-03 21:53:32 +0100808 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100809 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
810 return FAIL;
811 isn->isn_arg.number = number;
812
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200813 if (number == 0 || number == 1)
814 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200815 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200816
817 // A 0 or 1 number can also be used as a bool.
818 if (type != NULL)
819 {
820 type->tt_type = VAR_NUMBER;
821 type->tt_flags = TTFLAG_BOOL_OK;
822 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
823 }
824 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100825 return OK;
826}
827
828/*
829 * Generate an ISN_PUSHBOOL instruction.
830 */
831 static int
832generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
833{
834 isn_T *isn;
835
Bram Moolenaar080457c2020-03-03 21:53:32 +0100836 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
838 return FAIL;
839 isn->isn_arg.number = number;
840
841 return OK;
842}
843
844/*
845 * Generate an ISN_PUSHSPEC instruction.
846 */
847 static int
848generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
849{
850 isn_T *isn;
851
Bram Moolenaar080457c2020-03-03 21:53:32 +0100852 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
854 return FAIL;
855 isn->isn_arg.number = number;
856
857 return OK;
858}
859
860#ifdef FEAT_FLOAT
861/*
862 * Generate an ISN_PUSHF instruction.
863 */
864 static int
865generate_PUSHF(cctx_T *cctx, float_T fnumber)
866{
867 isn_T *isn;
868
Bram Moolenaar080457c2020-03-03 21:53:32 +0100869 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100870 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
871 return FAIL;
872 isn->isn_arg.fnumber = fnumber;
873
874 return OK;
875}
876#endif
877
878/*
879 * Generate an ISN_PUSHS instruction.
880 * Consumes "str".
881 */
882 static int
883generate_PUSHS(cctx_T *cctx, char_u *str)
884{
885 isn_T *isn;
886
Bram Moolenaar080457c2020-03-03 21:53:32 +0100887 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100888 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
889 return FAIL;
890 isn->isn_arg.string = str;
891
892 return OK;
893}
894
895/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100896 * Generate an ISN_PUSHCHANNEL instruction.
897 * Consumes "channel".
898 */
899 static int
900generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
901{
902 isn_T *isn;
903
Bram Moolenaar080457c2020-03-03 21:53:32 +0100904 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100905 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
906 return FAIL;
907 isn->isn_arg.channel = channel;
908
909 return OK;
910}
911
912/*
913 * Generate an ISN_PUSHJOB instruction.
914 * Consumes "job".
915 */
916 static int
917generate_PUSHJOB(cctx_T *cctx, job_T *job)
918{
919 isn_T *isn;
920
Bram Moolenaar080457c2020-03-03 21:53:32 +0100921 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100922 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100923 return FAIL;
924 isn->isn_arg.job = job;
925
926 return OK;
927}
928
929/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930 * Generate an ISN_PUSHBLOB instruction.
931 * Consumes "blob".
932 */
933 static int
934generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
935{
936 isn_T *isn;
937
Bram Moolenaar080457c2020-03-03 21:53:32 +0100938 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100939 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
940 return FAIL;
941 isn->isn_arg.blob = blob;
942
943 return OK;
944}
945
946/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100947 * Generate an ISN_PUSHFUNC instruction with name "name".
948 * Consumes "name".
949 */
950 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200951generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100952{
953 isn_T *isn;
954
Bram Moolenaar080457c2020-03-03 21:53:32 +0100955 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200956 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100957 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200958 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100959
960 return OK;
961}
962
963/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200964 * Generate an ISN_GETITEM instruction with "index".
965 */
966 static int
967generate_GETITEM(cctx_T *cctx, int index)
968{
969 isn_T *isn;
970 garray_T *stack = &cctx->ctx_type_stack;
971 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
972 type_T *item_type = &t_any;
973
974 RETURN_OK_IF_SKIP(cctx);
975
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200976 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200977 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200978 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200979 emsg(_(e_listreq));
980 return FAIL;
981 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200982 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200983 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
984 return FAIL;
985 isn->isn_arg.number = index;
986
987 // add the item type to the type stack
988 if (ga_grow(stack, 1) == FAIL)
989 return FAIL;
990 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
991 ++stack->ga_len;
992 return OK;
993}
994
995/*
Bram Moolenaar9af78762020-06-16 11:34:42 +0200996 * Generate an ISN_SLICE instruction with "count".
997 */
998 static int
999generate_SLICE(cctx_T *cctx, int count)
1000{
1001 isn_T *isn;
1002
1003 RETURN_OK_IF_SKIP(cctx);
1004 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1005 return FAIL;
1006 isn->isn_arg.number = count;
1007 return OK;
1008}
1009
1010/*
1011 * Generate an ISN_CHECKLEN instruction with "min_len".
1012 */
1013 static int
1014generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1015{
1016 isn_T *isn;
1017
1018 RETURN_OK_IF_SKIP(cctx);
1019
1020 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1021 return FAIL;
1022 isn->isn_arg.checklen.cl_min_len = min_len;
1023 isn->isn_arg.checklen.cl_more_OK = more_OK;
1024
1025 return OK;
1026}
1027
1028/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001029 * Generate an ISN_STORE instruction.
1030 */
1031 static int
1032generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1033{
1034 isn_T *isn;
1035
Bram Moolenaar080457c2020-03-03 21:53:32 +01001036 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001037 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1038 return FAIL;
1039 if (name != NULL)
1040 isn->isn_arg.string = vim_strsave(name);
1041 else
1042 isn->isn_arg.number = idx;
1043
1044 return OK;
1045}
1046
1047/*
1048 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1049 */
1050 static int
1051generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1052{
1053 isn_T *isn;
1054
Bram Moolenaar080457c2020-03-03 21:53:32 +01001055 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1057 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001058 isn->isn_arg.storenr.stnr_idx = idx;
1059 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001060
1061 return OK;
1062}
1063
1064/*
1065 * Generate an ISN_STOREOPT instruction
1066 */
1067 static int
1068generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1069{
1070 isn_T *isn;
1071
Bram Moolenaar080457c2020-03-03 21:53:32 +01001072 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001073 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001074 return FAIL;
1075 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1076 isn->isn_arg.storeopt.so_flags = opt_flags;
1077
1078 return OK;
1079}
1080
1081/*
1082 * Generate an ISN_LOAD or similar instruction.
1083 */
1084 static int
1085generate_LOAD(
1086 cctx_T *cctx,
1087 isntype_T isn_type,
1088 int idx,
1089 char_u *name,
1090 type_T *type)
1091{
1092 isn_T *isn;
1093
Bram Moolenaar080457c2020-03-03 21:53:32 +01001094 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001095 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1096 return FAIL;
1097 if (name != NULL)
1098 isn->isn_arg.string = vim_strsave(name);
1099 else
1100 isn->isn_arg.number = idx;
1101
1102 return OK;
1103}
1104
1105/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001106 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001107 */
1108 static int
1109generate_LOADV(
1110 cctx_T *cctx,
1111 char_u *name,
1112 int error)
1113{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001114 int di_flags;
1115 int vidx = find_vim_var(name, &di_flags);
1116 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001117
Bram Moolenaar080457c2020-03-03 21:53:32 +01001118 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001119 if (vidx < 0)
1120 {
1121 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001122 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001123 return FAIL;
1124 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001125 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001126
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001127 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001128}
1129
1130/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001131 * Generate an ISN_UNLET instruction.
1132 */
1133 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001134generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001135{
1136 isn_T *isn;
1137
1138 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001139 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001140 return FAIL;
1141 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1142 isn->isn_arg.unlet.ul_forceit = forceit;
1143
1144 return OK;
1145}
1146
1147/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001148 * Generate an ISN_LOCKCONST instruction.
1149 */
1150 static int
1151generate_LOCKCONST(cctx_T *cctx)
1152{
1153 isn_T *isn;
1154
1155 RETURN_OK_IF_SKIP(cctx);
1156 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1157 return FAIL;
1158 return OK;
1159}
1160
1161/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162 * Generate an ISN_LOADS instruction.
1163 */
1164 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001165generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001166 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001167 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001168 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001169 int sid,
1170 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171{
1172 isn_T *isn;
1173
Bram Moolenaar080457c2020-03-03 21:53:32 +01001174 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001175 if (isn_type == ISN_LOADS)
1176 isn = generate_instr_type(cctx, isn_type, type);
1177 else
1178 isn = generate_instr_drop(cctx, isn_type, 1);
1179 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001180 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001181 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1182 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001183
1184 return OK;
1185}
1186
1187/*
1188 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1189 */
1190 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001191generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001192 cctx_T *cctx,
1193 isntype_T isn_type,
1194 int sid,
1195 int idx,
1196 type_T *type)
1197{
1198 isn_T *isn;
1199
Bram Moolenaar080457c2020-03-03 21:53:32 +01001200 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001201 if (isn_type == ISN_LOADSCRIPT)
1202 isn = generate_instr_type(cctx, isn_type, type);
1203 else
1204 isn = generate_instr_drop(cctx, isn_type, 1);
1205 if (isn == NULL)
1206 return FAIL;
1207 isn->isn_arg.script.script_sid = sid;
1208 isn->isn_arg.script.script_idx = idx;
1209 return OK;
1210}
1211
1212/*
1213 * Generate an ISN_NEWLIST instruction.
1214 */
1215 static int
1216generate_NEWLIST(cctx_T *cctx, int count)
1217{
1218 isn_T *isn;
1219 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220 type_T *type;
1221 type_T *member;
1222
Bram Moolenaar080457c2020-03-03 21:53:32 +01001223 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001224 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1225 return FAIL;
1226 isn->isn_arg.number = count;
1227
Bram Moolenaar127542b2020-08-09 17:22:04 +02001228 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001229 if (count == 0)
1230 member = &t_void;
1231 else
1232 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001233 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1234 cctx->ctx_type_list);
1235 type = get_list_type(member, cctx->ctx_type_list);
1236
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001237 // drop the value types
1238 stack->ga_len -= count;
1239
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240 // add the list type to the type stack
1241 if (ga_grow(stack, 1) == FAIL)
1242 return FAIL;
1243 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1244 ++stack->ga_len;
1245
1246 return OK;
1247}
1248
1249/*
1250 * Generate an ISN_NEWDICT instruction.
1251 */
1252 static int
1253generate_NEWDICT(cctx_T *cctx, int count)
1254{
1255 isn_T *isn;
1256 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257 type_T *type;
1258 type_T *member;
1259
Bram Moolenaar080457c2020-03-03 21:53:32 +01001260 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1262 return FAIL;
1263 isn->isn_arg.number = count;
1264
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001265 if (count == 0)
1266 member = &t_void;
1267 else
1268 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001269 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1270 cctx->ctx_type_list);
1271 type = get_dict_type(member, cctx->ctx_type_list);
1272
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 // drop the key and value types
1274 stack->ga_len -= 2 * count;
1275
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276 // add the dict type to the type stack
1277 if (ga_grow(stack, 1) == FAIL)
1278 return FAIL;
1279 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1280 ++stack->ga_len;
1281
1282 return OK;
1283}
1284
1285/*
1286 * Generate an ISN_FUNCREF instruction.
1287 */
1288 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001289generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001290{
1291 isn_T *isn;
1292 garray_T *stack = &cctx->ctx_type_stack;
1293
Bram Moolenaar080457c2020-03-03 21:53:32 +01001294 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001295 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1296 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001297 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001298 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001299
1300 if (ga_grow(stack, 1) == FAIL)
1301 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001302 ((type_T **)stack->ga_data)[stack->ga_len] =
1303 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001304 ++stack->ga_len;
1305
1306 return OK;
1307}
1308
1309/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001310 * Generate an ISN_NEWFUNC instruction.
1311 */
1312 static int
1313generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1314{
1315 isn_T *isn;
1316 char_u *name;
1317
1318 RETURN_OK_IF_SKIP(cctx);
1319 name = vim_strsave(lambda_name);
1320 if (name == NULL)
1321 return FAIL;
1322 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1323 return FAIL;
1324 isn->isn_arg.newfunc.nf_lambda = name;
1325 isn->isn_arg.newfunc.nf_global = func_name;
1326
1327 return OK;
1328}
1329
1330/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331 * Generate an ISN_JUMP instruction.
1332 */
1333 static int
1334generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1335{
1336 isn_T *isn;
1337 garray_T *stack = &cctx->ctx_type_stack;
1338
Bram Moolenaar080457c2020-03-03 21:53:32 +01001339 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1341 return FAIL;
1342 isn->isn_arg.jump.jump_when = when;
1343 isn->isn_arg.jump.jump_where = where;
1344
1345 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1346 --stack->ga_len;
1347
1348 return OK;
1349}
1350
1351 static int
1352generate_FOR(cctx_T *cctx, int loop_idx)
1353{
1354 isn_T *isn;
1355 garray_T *stack = &cctx->ctx_type_stack;
1356
Bram Moolenaar080457c2020-03-03 21:53:32 +01001357 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001358 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1359 return FAIL;
1360 isn->isn_arg.forloop.for_idx = loop_idx;
1361
1362 if (ga_grow(stack, 1) == FAIL)
1363 return FAIL;
1364 // type doesn't matter, will be stored next
1365 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1366 ++stack->ga_len;
1367
1368 return OK;
1369}
1370
1371/*
1372 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001373 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001374 * Return FAIL if the number of arguments is wrong.
1375 */
1376 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001377generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378{
1379 isn_T *isn;
1380 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001381 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001382 type_T *argtypes[MAX_FUNC_ARGS];
1383 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001384
Bram Moolenaar080457c2020-03-03 21:53:32 +01001385 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001386 argoff = check_internal_func(func_idx, argcount);
1387 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001388 return FAIL;
1389
Bram Moolenaar389df252020-07-09 21:20:47 +02001390 if (method_call && argoff > 1)
1391 {
1392 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1393 return FAIL;
1394 isn->isn_arg.shuffle.shfl_item = argcount;
1395 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1396 }
1397
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001398 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1399 return FAIL;
1400 isn->isn_arg.bfunc.cbf_idx = func_idx;
1401 isn->isn_arg.bfunc.cbf_argcount = argcount;
1402
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001403 for (i = 0; i < argcount; ++i)
1404 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1405
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406 stack->ga_len -= argcount; // drop the arguments
1407 if (ga_grow(stack, 1) == FAIL)
1408 return FAIL;
1409 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001410 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411 ++stack->ga_len; // add return value
1412
1413 return OK;
1414}
1415
1416/*
1417 * Generate an ISN_DCALL or ISN_UCALL instruction.
1418 * Return FAIL if the number of arguments is wrong.
1419 */
1420 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001421generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001422{
1423 isn_T *isn;
1424 garray_T *stack = &cctx->ctx_type_stack;
1425 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001426 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001427
Bram Moolenaar080457c2020-03-03 21:53:32 +01001428 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429 if (argcount > regular_args && !has_varargs(ufunc))
1430 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001431 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432 return FAIL;
1433 }
1434 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1435 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001436 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001437 return FAIL;
1438 }
1439
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001440 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001441 {
1442 int i;
1443
1444 for (i = 0; i < argcount; ++i)
1445 {
1446 type_T *expected;
1447 type_T *actual;
1448
1449 if (i < regular_args)
1450 {
1451 if (ufunc->uf_arg_types == NULL)
1452 continue;
1453 expected = ufunc->uf_arg_types[i];
1454 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001455 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1456 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001457 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001458 else
1459 expected = ufunc->uf_va_type->tt_member;
1460 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001461 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001462 {
1463 arg_type_mismatch(expected, actual, i + 1);
1464 return FAIL;
1465 }
1466 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001467 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001468 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1469 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001470 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001471 }
1472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001474 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001475 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001476 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001477 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001478 {
1479 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1480 isn->isn_arg.dfunc.cdf_argcount = argcount;
1481 }
1482 else
1483 {
1484 // A user function may be deleted and redefined later, can't use the
1485 // ufunc pointer, need to look it up again at runtime.
1486 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1487 isn->isn_arg.ufunc.cuf_argcount = argcount;
1488 }
1489
1490 stack->ga_len -= argcount; // drop the arguments
1491 if (ga_grow(stack, 1) == FAIL)
1492 return FAIL;
1493 // add return value
1494 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1495 ++stack->ga_len;
1496
1497 return OK;
1498}
1499
1500/*
1501 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1502 */
1503 static int
1504generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1505{
1506 isn_T *isn;
1507 garray_T *stack = &cctx->ctx_type_stack;
1508
Bram Moolenaar080457c2020-03-03 21:53:32 +01001509 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1511 return FAIL;
1512 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1513 isn->isn_arg.ufunc.cuf_argcount = argcount;
1514
1515 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001516 if (ga_grow(stack, 1) == FAIL)
1517 return FAIL;
1518 // add return value
1519 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1520 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001521
1522 return OK;
1523}
1524
1525/*
1526 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001527 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528 */
1529 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001530generate_PCALL(
1531 cctx_T *cctx,
1532 int argcount,
1533 char_u *name,
1534 type_T *type,
1535 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536{
1537 isn_T *isn;
1538 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001539 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001540
Bram Moolenaar080457c2020-03-03 21:53:32 +01001541 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001542
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001543 if (type->tt_type == VAR_ANY)
1544 ret_type = &t_any;
1545 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001546 {
1547 if (type->tt_argcount != -1)
1548 {
1549 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1550
1551 if (argcount < type->tt_min_argcount - varargs)
1552 {
1553 semsg(_(e_toofewarg), "[reference]");
1554 return FAIL;
1555 }
1556 if (!varargs && argcount > type->tt_argcount)
1557 {
1558 semsg(_(e_toomanyarg), "[reference]");
1559 return FAIL;
1560 }
1561 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001562 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001563 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001564 else
1565 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001566 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001567 return FAIL;
1568 }
1569
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001570 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1571 return FAIL;
1572 isn->isn_arg.pfunc.cpf_top = at_top;
1573 isn->isn_arg.pfunc.cpf_argcount = argcount;
1574
1575 stack->ga_len -= argcount; // drop the arguments
1576
1577 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001578 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001579
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001580 // If partial is above the arguments it must be cleared and replaced with
1581 // the return value.
1582 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1583 return FAIL;
1584
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585 return OK;
1586}
1587
1588/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001589 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590 */
1591 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001592generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001593{
1594 isn_T *isn;
1595 garray_T *stack = &cctx->ctx_type_stack;
1596 type_T *type;
1597
Bram Moolenaar080457c2020-03-03 21:53:32 +01001598 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001599 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001601 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001602
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001603 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001605 if (type->tt_type != VAR_DICT && type != &t_any)
1606 {
1607 emsg(_(e_dictreq));
1608 return FAIL;
1609 }
1610 // change dict type to dict member type
1611 if (type->tt_type == VAR_DICT)
1612 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613
1614 return OK;
1615}
1616
1617/*
1618 * Generate an ISN_ECHO instruction.
1619 */
1620 static int
1621generate_ECHO(cctx_T *cctx, int with_white, int count)
1622{
1623 isn_T *isn;
1624
Bram Moolenaar080457c2020-03-03 21:53:32 +01001625 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001626 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1627 return FAIL;
1628 isn->isn_arg.echo.echo_with_white = with_white;
1629 isn->isn_arg.echo.echo_count = count;
1630
1631 return OK;
1632}
1633
Bram Moolenaarad39c092020-02-26 18:23:43 +01001634/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001635 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001636 */
1637 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001638generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001639{
1640 isn_T *isn;
1641
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001642 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001643 return FAIL;
1644 isn->isn_arg.number = count;
1645
1646 return OK;
1647}
1648
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001649/*
1650 * Generate an ISN_PUT instruction.
1651 */
1652 static int
1653generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1654{
1655 isn_T *isn;
1656
1657 RETURN_OK_IF_SKIP(cctx);
1658 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1659 return FAIL;
1660 isn->isn_arg.put.put_regname = regname;
1661 isn->isn_arg.put.put_lnum = lnum;
1662 return OK;
1663}
1664
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665 static int
1666generate_EXEC(cctx_T *cctx, char_u *line)
1667{
1668 isn_T *isn;
1669
Bram Moolenaar080457c2020-03-03 21:53:32 +01001670 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1672 return FAIL;
1673 isn->isn_arg.string = vim_strsave(line);
1674 return OK;
1675}
1676
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001677 static int
1678generate_EXECCONCAT(cctx_T *cctx, int count)
1679{
1680 isn_T *isn;
1681
1682 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1683 return FAIL;
1684 isn->isn_arg.number = count;
1685 return OK;
1686}
1687
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001688/*
1689 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001690 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001692 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02001693reserve_local(
1694 cctx_T *cctx,
1695 char_u *name,
1696 size_t len,
1697 int isConst,
1698 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700 lvar_T *lvar;
1701
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001702 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001703 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001704 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001705 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001706 }
1707
1708 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001709 return NULL;
1710 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02001711 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001712
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001713 // Every local variable uses the next entry on the stack. We could re-use
1714 // the last ones when leaving a scope, but then variables used in a closure
1715 // might get overwritten. To keep things simple do not re-use stack
1716 // entries. This is less efficient, but memory is cheap these days.
1717 lvar->lv_idx = cctx->ctx_locals_count++;
1718
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001719 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 lvar->lv_const = isConst;
1721 lvar->lv_type = type;
1722
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001723 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001724}
1725
1726/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001727 * Remove local variables above "new_top".
1728 */
1729 static void
1730unwind_locals(cctx_T *cctx, int new_top)
1731{
1732 if (cctx->ctx_locals.ga_len > new_top)
1733 {
1734 int idx;
1735 lvar_T *lvar;
1736
1737 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1738 {
1739 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1740 vim_free(lvar->lv_name);
1741 }
1742 }
1743 cctx->ctx_locals.ga_len = new_top;
1744}
1745
1746/*
1747 * Free all local variables.
1748 */
1749 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001750free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001751{
1752 unwind_locals(cctx, 0);
1753 ga_clear(&cctx->ctx_locals);
1754}
1755
1756/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001757 * Find "name" in script-local items of script "sid".
1758 * Returns the index in "sn_var_vals" if found.
1759 * If found but not in "sn_var_vals" returns -1.
1760 * If not found returns -2.
1761 */
1762 int
1763get_script_item_idx(int sid, char_u *name, int check_writable)
1764{
1765 hashtab_T *ht;
1766 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001767 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768 int idx;
1769
1770 // First look the name up in the hashtable.
Bram Moolenaare3d46852020-08-29 13:39:17 +02001771 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001772 return -1;
1773 ht = &SCRIPT_VARS(sid);
1774 di = find_var_in_ht(ht, 0, name, TRUE);
1775 if (di == NULL)
1776 return -2;
1777
1778 // Now find the svar_T index in sn_var_vals.
1779 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1780 {
1781 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1782
1783 if (sv->sv_tv == &di->di_tv)
1784 {
1785 if (check_writable && sv->sv_const)
1786 semsg(_(e_readonlyvar), name);
1787 return idx;
1788 }
1789 }
1790 return -1;
1791}
1792
1793/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001794 * Find "name" in imported items of the current script or in "cctx" if not
1795 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796 */
1797 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001798find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001799{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001800 int idx;
1801
Bram Moolenaare3d46852020-08-29 13:39:17 +02001802 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001803 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804 if (cctx != NULL)
1805 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1806 {
1807 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1808 + idx;
1809
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001810 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1811 : STRLEN(import->imp_name) == len
1812 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001813 return import;
1814 }
1815
Bram Moolenaarefa94442020-08-08 22:16:00 +02001816 return find_imported_in_script(name, len, current_sctx.sc_sid);
1817}
1818
1819 imported_T *
1820find_imported_in_script(char_u *name, size_t len, int sid)
1821{
Bram Moolenaare3d46852020-08-29 13:39:17 +02001822 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001823 int idx;
1824
Bram Moolenaare3d46852020-08-29 13:39:17 +02001825 if (!SCRIPT_ID_VALID(sid))
1826 return NULL;
1827 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1829 {
1830 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1831
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001832 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1833 : STRLEN(import->imp_name) == len
1834 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835 return import;
1836 }
1837 return NULL;
1838}
1839
1840/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001841 * Free all imported variables.
1842 */
1843 static void
1844free_imported(cctx_T *cctx)
1845{
1846 int idx;
1847
1848 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1849 {
1850 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1851
1852 vim_free(import->imp_name);
1853 }
1854 ga_clear(&cctx->ctx_imports);
1855}
1856
1857/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02001858 * Return TRUE if "p" points at a "#" but not at "#{".
1859 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001860 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001861vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001862{
1863 return p[0] == '#' && p[1] != '{';
1864}
1865
1866/*
1867 * Return a pointer to the next line that isn't empty or only contains a
1868 * comment. Skips over white space.
1869 * Returns NULL if there is none.
1870 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001871 char_u *
1872peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001873{
1874 int lnum = cctx->ctx_lnum;
1875
1876 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
1877 {
1878 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001879 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001880
Bram Moolenaarba60cc42020-08-12 19:15:33 +02001881 // ignore NULLs inserted for continuation lines
1882 if (line != NULL)
1883 {
1884 p = skipwhite(line);
1885 if (*p != NUL && !vim9_comment_start(p))
1886 return p;
1887 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02001888 }
1889 return NULL;
1890}
1891
1892/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001893 * Called when checking for a following operator at "arg". When the rest of
1894 * the line is empty or only a comment, peek the next line. If there is a next
1895 * line return a pointer to it and set "nextp".
1896 * Otherwise skip over white space.
1897 */
1898 static char_u *
1899may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
1900{
1901 char_u *p = skipwhite(arg);
1902
1903 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001904 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001905 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001906 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001907 if (*nextp != NULL)
1908 return *nextp;
1909 }
1910 return p;
1911}
1912
1913/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02001914 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02001915 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02001916 * Returns NULL when at the end.
1917 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001918 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02001919next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02001920{
Bram Moolenaar7a092242020-04-16 22:10:49 +02001921 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001922
1923 do
1924 {
1925 ++cctx->ctx_lnum;
1926 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02001927 {
1928 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001929 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02001930 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02001931 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02001932 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001933 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001934 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02001935 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02001936 return line;
1937}
1938
1939/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001940 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02001941 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001942 * Return FAIL if beyond the last line, "*arg" is unmodified then.
1943 */
1944 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02001945may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001946{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001947 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001948 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02001949 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001950
1951 if (next == NULL)
1952 return FAIL;
1953 *arg = skipwhite(next);
1954 }
1955 return OK;
1956}
1957
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001958/*
1959 * Idem, and give an error when failed.
1960 */
1961 static int
1962may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
1963{
1964 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1965 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001966 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001967 return FAIL;
1968 }
1969 return OK;
1970}
1971
1972
Bram Moolenaara5565e42020-05-09 15:44:01 +02001973// Structure passed between the compile_expr* functions to keep track of
1974// constants that have been parsed but for which no code was produced yet. If
1975// possible expressions on these constants are applied at compile time. If
1976// that is not possible, the code to push the constants needs to be generated
1977// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02001978// Using 50 should be more than enough of 5 levels of ().
1979#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02001980typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02001981 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02001982 int pp_used; // active entries in pp_tv[]
1983} ppconst_T;
1984
Bram Moolenaar1c747212020-05-09 18:28:34 +02001985static int compile_expr0(char_u **arg, cctx_T *cctx);
1986static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1987
Bram Moolenaara5565e42020-05-09 15:44:01 +02001988/*
1989 * Generate a PUSH instruction for "tv".
1990 * "tv" will be consumed or cleared.
1991 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
1992 */
1993 static int
1994generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
1995{
1996 if (tv != NULL)
1997 {
1998 switch (tv->v_type)
1999 {
2000 case VAR_UNKNOWN:
2001 break;
2002 case VAR_BOOL:
2003 generate_PUSHBOOL(cctx, tv->vval.v_number);
2004 break;
2005 case VAR_SPECIAL:
2006 generate_PUSHSPEC(cctx, tv->vval.v_number);
2007 break;
2008 case VAR_NUMBER:
2009 generate_PUSHNR(cctx, tv->vval.v_number);
2010 break;
2011#ifdef FEAT_FLOAT
2012 case VAR_FLOAT:
2013 generate_PUSHF(cctx, tv->vval.v_float);
2014 break;
2015#endif
2016 case VAR_BLOB:
2017 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2018 tv->vval.v_blob = NULL;
2019 break;
2020 case VAR_STRING:
2021 generate_PUSHS(cctx, tv->vval.v_string);
2022 tv->vval.v_string = NULL;
2023 break;
2024 default:
2025 iemsg("constant type not supported");
2026 clear_tv(tv);
2027 return FAIL;
2028 }
2029 tv->v_type = VAR_UNKNOWN;
2030 }
2031 return OK;
2032}
2033
2034/*
2035 * Generate code for any ppconst entries.
2036 */
2037 static int
2038generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2039{
2040 int i;
2041 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002042 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002043
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002044 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002045 for (i = 0; i < ppconst->pp_used; ++i)
2046 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2047 ret = FAIL;
2048 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002049 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002050 return ret;
2051}
2052
2053/*
2054 * Clear ppconst constants. Used when failing.
2055 */
2056 static void
2057clear_ppconst(ppconst_T *ppconst)
2058{
2059 int i;
2060
2061 for (i = 0; i < ppconst->pp_used; ++i)
2062 clear_tv(&ppconst->pp_tv[i]);
2063 ppconst->pp_used = 0;
2064}
2065
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002066/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002067 * Generate an instruction to load script-local variable "name", without the
2068 * leading "s:".
2069 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070 */
2071 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002072compile_load_scriptvar(
2073 cctx_T *cctx,
2074 char_u *name, // variable NUL terminated
2075 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002076 char_u **end, // end of variable
2077 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002078{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002079 scriptitem_T *si;
2080 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002081 imported_T *import;
2082
Bram Moolenaare3d46852020-08-29 13:39:17 +02002083 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2084 return FAIL;
2085 si = SCRIPT_ITEM(current_sctx.sc_sid);
2086 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002087 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002088 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002089 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002090 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2091 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002092 }
2093 if (idx >= 0)
2094 {
2095 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2096
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002097 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002098 current_sctx.sc_sid, idx, sv->sv_type);
2099 return OK;
2100 }
2101
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002102 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002103 if (import != NULL)
2104 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002105 if (import->imp_all)
2106 {
2107 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002108 char_u *exp_name;
2109 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002110 ufunc_T *ufunc;
2111 type_T *type;
2112
2113 // Used "import * as Name", need to lookup the member.
2114 if (*p != '.')
2115 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002116 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002117 return FAIL;
2118 }
2119 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002120 if (VIM_ISWHITE(*p))
2121 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002122 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002123 return FAIL;
2124 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002125
Bram Moolenaar1c991142020-07-04 13:15:31 +02002126 // isolate one name
2127 exp_name = p;
2128 while (eval_isnamec(*p))
2129 ++p;
2130 cc = *p;
2131 *p = NUL;
2132
2133 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2134 *p = cc;
2135 p = skipwhite(p);
2136
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002137 // TODO: what if it is a function?
2138 if (idx < 0)
2139 return FAIL;
2140 *end = p;
2141
2142 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2143 import->imp_sid,
2144 idx,
2145 type);
2146 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002147 else if (import->imp_funcname != NULL)
2148 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002149 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002150 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2151 import->imp_sid,
2152 import->imp_var_vals_idx,
2153 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002154 return OK;
2155 }
2156
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002157 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002158 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159 return FAIL;
2160}
2161
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002162 static int
2163generate_funcref(cctx_T *cctx, char_u *name)
2164{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002165 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002166
2167 if (ufunc == NULL)
2168 return FAIL;
2169
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002170 // Need to compile any default values to get the argument types.
2171 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2172 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2173 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002174 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002175}
2176
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002177/*
2178 * Compile a variable name into a load instruction.
2179 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002180 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002181 * When "error" is FALSE do not give an error when not found.
2182 */
2183 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002184compile_load(
2185 char_u **arg,
2186 char_u *end_arg,
2187 cctx_T *cctx,
2188 int is_expr,
2189 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002190{
2191 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002192 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002193 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002194 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002195 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002196
2197 if (*(*arg + 1) == ':')
2198 {
2199 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002200 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002201 {
2202 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002203
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002204 switch (**arg)
2205 {
2206 case 'g': isn_type = ISN_LOADGDICT; break;
2207 case 'w': isn_type = ISN_LOADWDICT; break;
2208 case 't': isn_type = ISN_LOADTDICT; break;
2209 case 'b': isn_type = ISN_LOADBDICT; break;
2210 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002211 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002212 goto theend;
2213 }
2214 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2215 goto theend;
2216 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002217 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002218 else
2219 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002220 isntype_T isn_type = ISN_DROP;
2221
2222 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2223 if (name == NULL)
2224 return FAIL;
2225
2226 switch (**arg)
2227 {
2228 case 'v': res = generate_LOADV(cctx, name, error);
2229 break;
2230 case 's': res = compile_load_scriptvar(cctx, name,
2231 NULL, NULL, error);
2232 break;
2233 case 'g': isn_type = ISN_LOADG; break;
2234 case 'w': isn_type = ISN_LOADW; break;
2235 case 't': isn_type = ISN_LOADT; break;
2236 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002237 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002238 goto theend;
2239 }
2240 if (isn_type != ISN_DROP)
2241 {
2242 // Global, Buffer-local, Window-local and Tabpage-local
2243 // variables can be defined later, thus we don't check if it
2244 // exists, give error at runtime.
2245 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2246 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002247 }
2248 }
2249 else
2250 {
2251 size_t len = end - *arg;
2252 int idx;
2253 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002254 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002255
2256 name = vim_strnsave(*arg, end - *arg);
2257 if (name == NULL)
2258 return FAIL;
2259
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002260 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002261 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002262 if (!gen_load_outer)
2263 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264 }
2265 else
2266 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002267 lvar_T *lvar = lookup_local(*arg, len, cctx);
2268
2269 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002270 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002271 type = lvar->lv_type;
2272 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002273 if (lvar->lv_from_outer)
2274 gen_load_outer = TRUE;
2275 else
2276 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002277 }
2278 else
2279 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002280 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002281 // already exists in a Vim9 script or when it's imported.
2282 if (lookup_script(*arg, len, TRUE) == OK
2283 || find_imported(name, 0, cctx) != NULL)
2284 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002285
Bram Moolenaar0f769812020-09-12 18:32:34 +02002286 // When evaluating an expression and the name starts with an
2287 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002288 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002289 if (res == FAIL && is_expr
2290 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002291 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002292 }
2293 }
2294 if (gen_load)
2295 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002296 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002297 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002298 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002299 cctx->ctx_outer_used = TRUE;
2300 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002301 }
2302
2303 *arg = end;
2304
2305theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002306 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002307 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002308 vim_free(name);
2309 return res;
2310}
2311
2312/*
2313 * Compile the argument expressions.
2314 * "arg" points to just after the "(" and is advanced to after the ")"
2315 */
2316 static int
2317compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2318{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002319 char_u *p = *arg;
2320 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002321 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322
Bram Moolenaare6085c52020-04-12 20:19:16 +02002323 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002324 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002325 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2326 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002327 if (*p == ')')
2328 {
2329 *arg = p + 1;
2330 return OK;
2331 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002332 if (must_end)
2333 {
2334 semsg(_(e_missing_comma_before_argument_str), p);
2335 return FAIL;
2336 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002337
Bram Moolenaara5565e42020-05-09 15:44:01 +02002338 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002339 return FAIL;
2340 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002341
2342 if (*p != ',' && *skipwhite(p) == ',')
2343 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002344 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002345 p = skipwhite(p);
2346 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002348 {
2349 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002350 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002351 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002352 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002353 else
2354 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002355 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002356 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002357 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002358failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002359 emsg(_(e_missing_close));
2360 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002361}
2362
2363/*
2364 * Compile a function call: name(arg1, arg2)
2365 * "arg" points to "name", "arg + varlen" to the "(".
2366 * "argcount_init" is 1 for "value->method()"
2367 * Instructions:
2368 * EVAL arg1
2369 * EVAL arg2
2370 * BCALL / DCALL / UCALL
2371 */
2372 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002373compile_call(
2374 char_u **arg,
2375 size_t varlen,
2376 cctx_T *cctx,
2377 ppconst_T *ppconst,
2378 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002379{
2380 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002381 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002382 int argcount = argcount_init;
2383 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002384 char_u fname_buf[FLEN_FIXED + 1];
2385 char_u *tofree = NULL;
2386 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002387 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002388 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002389 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390
Bram Moolenaara5565e42020-05-09 15:44:01 +02002391 // we can evaluate "has('name')" at compile time
2392 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2393 {
2394 char_u *s = skipwhite(*arg + varlen + 1);
2395 typval_T argvars[2];
2396
2397 argvars[0].v_type = VAR_UNKNOWN;
2398 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002399 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002400 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002401 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002402 s = skipwhite(s);
2403 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2404 {
2405 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2406
2407 *arg = s + 1;
2408 argvars[1].v_type = VAR_UNKNOWN;
2409 tv->v_type = VAR_NUMBER;
2410 tv->vval.v_number = 0;
2411 f_has(argvars, tv);
2412 clear_tv(&argvars[0]);
2413 ++ppconst->pp_used;
2414 return OK;
2415 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002416 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002417 }
2418
2419 if (generate_ppconst(cctx, ppconst) == FAIL)
2420 return FAIL;
2421
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002422 if (varlen >= sizeof(namebuf))
2423 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002424 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002425 return FAIL;
2426 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002427 vim_strncpy(namebuf, *arg, varlen);
2428 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002429
2430 *arg = skipwhite(*arg + varlen + 1);
2431 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002432 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002433
Bram Moolenaara1773442020-08-12 15:21:22 +02002434 is_autoload = vim_strchr(name, '#') != NULL;
2435 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002436 {
2437 int idx;
2438
2439 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002440 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002441 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002442 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002443 else
2444 semsg(_(e_unknownfunc), namebuf);
2445 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002446 }
2447
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448 // If we can find the function by name generate the right call.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002449 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002450 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar0f769812020-09-12 18:32:34 +02002451 if (ufunc != NULL && !func_is_global(ufunc))
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002452 {
2453 res = generate_CALL(cctx, ufunc, argcount);
2454 goto theend;
2455 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002456
2457 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002458 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002459 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002460 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002461 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002462 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002463 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002464 garray_T *stack = &cctx->ctx_type_stack;
2465 type_T *type;
2466
2467 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2468 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002469 goto theend;
2470 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471
Bram Moolenaar0f769812020-09-12 18:32:34 +02002472 // If we can find a global function by name generate the right call.
2473 if (ufunc != NULL)
2474 {
2475 res = generate_CALL(cctx, ufunc, argcount);
2476 goto theend;
2477 }
2478
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002479 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002480 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002481 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002482 res = generate_UCALL(cctx, name, argcount);
2483 else
2484 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002485
2486theend:
2487 vim_free(tofree);
2488 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489}
2490
2491// like NAMESPACE_CHAR but with 'a' and 'l'.
2492#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2493
2494/*
2495 * Find the end of a variable or function name. Unlike find_name_end() this
2496 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002497 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 * Return a pointer to just after the name. Equal to "arg" if there is no
2499 * valid name.
2500 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002501 static char_u *
2502to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503{
2504 char_u *p;
2505
2506 // Quick check for valid starting character.
2507 if (!eval_isnamec1(*arg))
2508 return arg;
2509
2510 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2511 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2512 // and can be used in slice "[n:]".
2513 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002514 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2516 break;
2517 return p;
2518}
2519
2520/*
2521 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002522 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 */
2524 char_u *
2525to_name_const_end(char_u *arg)
2526{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002527 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 typval_T rettv;
2529
2530 if (p == arg && *arg == '[')
2531 {
2532
2533 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002534 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002535 p = arg;
2536 }
2537 else if (p == arg && *arg == '#' && arg[1] == '{')
2538 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002539 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002541 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542 p = arg;
2543 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544
2545 return p;
2546}
2547
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002548/*
2549 * parse a list: [expr, expr]
2550 * "*arg" points to the '['.
2551 */
2552 static int
2553compile_list(char_u **arg, cctx_T *cctx)
2554{
2555 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002556 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002557 int count = 0;
2558
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002559 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002561 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002562 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002563 semsg(_(e_list_end), *arg);
2564 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002565 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002566 if (*p == ',')
2567 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002568 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002569 return FAIL;
2570 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002571 if (*p == ']')
2572 {
2573 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002574 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002575 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002576 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002577 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578 ++count;
2579 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002580 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002582 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2583 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002584 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002585 return FAIL;
2586 }
2587 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002588 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 p = skipwhite(p);
2590 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002591 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002592
2593 generate_NEWLIST(cctx, count);
2594 return OK;
2595}
2596
2597/*
2598 * parse a lambda: {arg, arg -> expr}
2599 * "*arg" points to the '{'.
2600 */
2601 static int
2602compile_lambda(char_u **arg, cctx_T *cctx)
2603{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604 typval_T rettv;
2605 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002606 evalarg_T evalarg;
2607
2608 CLEAR_FIELD(evalarg);
2609 evalarg.eval_flags = EVAL_EVALUATE;
2610 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611
2612 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002613 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002614 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002615
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002616 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002617 ++ufunc->uf_refcount;
2618 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002619 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002620
2621 // The function will have one line: "return {expr}".
2622 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002623 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002625 clear_evalarg(&evalarg, NULL);
2626
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002627 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002628 {
2629 // The return type will now be known.
2630 set_function_type(ufunc);
2631
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002632 // The function reference count will be 1. When the ISN_FUNCREF
2633 // instruction is deleted the reference count is decremented and the
2634 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002635 return generate_FUNCREF(cctx, ufunc);
2636 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002637
2638 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639 return FAIL;
2640}
2641
2642/*
2643 * Compile a lamda call: expr->{lambda}(args)
2644 * "arg" points to the "{".
2645 */
2646 static int
2647compile_lambda_call(char_u **arg, cctx_T *cctx)
2648{
2649 ufunc_T *ufunc;
2650 typval_T rettv;
2651 int argcount = 1;
2652 int ret = FAIL;
2653
2654 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002655 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002656 return FAIL;
2657
2658 if (**arg != '(')
2659 {
2660 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002661 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002662 else
2663 semsg(_(e_missing_paren), "lambda");
2664 clear_tv(&rettv);
2665 return FAIL;
2666 }
2667
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 ufunc = rettv.vval.v_partial->pt_func;
2669 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002670 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002671 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002672
Bram Moolenaara05e5242020-09-19 18:19:19 +02002673 // The function will have one line: "return {expr}". Compile it into
2674 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002675 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676
2677 // compile the arguments
2678 *arg = skipwhite(*arg + 1);
2679 if (compile_arguments(arg, cctx, &argcount) == OK)
2680 // call the compiled function
2681 ret = generate_CALL(cctx, ufunc, argcount);
2682
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002683 if (ret == FAIL)
2684 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002685 return ret;
2686}
2687
2688/*
2689 * parse a dict: {'key': val} or #{key: val}
2690 * "*arg" points to the '{'.
2691 */
2692 static int
2693compile_dict(char_u **arg, cctx_T *cctx, int literal)
2694{
2695 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002696 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697 int count = 0;
2698 dict_T *d = dict_alloc();
2699 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002700 char_u *whitep = *arg;
2701 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702
2703 if (d == NULL)
2704 return FAIL;
2705 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002706 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707 {
2708 char_u *key = NULL;
2709
Bram Moolenaar23c55272020-06-21 16:58:13 +02002710 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002711 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002712 *arg = NULL;
2713 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002714 }
2715
2716 if (**arg == '}')
2717 break;
2718
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719 if (literal)
2720 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002721 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002722
Bram Moolenaar2c330432020-04-13 14:41:35 +02002723 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002725 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002726 return FAIL;
2727 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002728 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002729 if (generate_PUSHS(cctx, key) == FAIL)
2730 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002731 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002732 }
2733 else
2734 {
2735 isn_T *isn;
2736
Bram Moolenaara5565e42020-05-09 15:44:01 +02002737 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002738 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002739 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2740 if (isn->isn_type == ISN_PUSHS)
2741 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002742 else
2743 {
2744 type_T *keytype = ((type_T **)stack->ga_data)
2745 [stack->ga_len - 1];
2746 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2747 return FAIL;
2748 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002749 }
2750
2751 // Check for duplicate keys, if using string keys.
2752 if (key != NULL)
2753 {
2754 item = dict_find(d, key, -1);
2755 if (item != NULL)
2756 {
2757 semsg(_(e_duplicate_key), key);
2758 goto failret;
2759 }
2760 item = dictitem_alloc(key);
2761 if (item != NULL)
2762 {
2763 item->di_tv.v_type = VAR_UNKNOWN;
2764 item->di_tv.v_lock = 0;
2765 if (dict_add(d, item) == FAIL)
2766 dictitem_free(item);
2767 }
2768 }
2769
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002770 if (**arg != ':')
2771 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002772 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002773 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002774 else
2775 semsg(_(e_missing_dict_colon), *arg);
2776 return FAIL;
2777 }
2778 whitep = *arg + 1;
2779 if (!IS_WHITE_OR_NUL(*whitep))
2780 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002781 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782 return FAIL;
2783 }
2784
2785 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002786 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002787 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002788 *arg = NULL;
2789 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002790 }
2791
Bram Moolenaara5565e42020-05-09 15:44:01 +02002792 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002793 return FAIL;
2794 ++count;
2795
Bram Moolenaar2c330432020-04-13 14:41:35 +02002796 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002797 *arg = skipwhite(*arg);
2798 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002799 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002800 *arg = NULL;
2801 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002802 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803 if (**arg == '}')
2804 break;
2805 if (**arg != ',')
2806 {
2807 semsg(_(e_missing_dict_comma), *arg);
2808 goto failret;
2809 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002810 if (IS_WHITE_OR_NUL(*whitep))
2811 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002812 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002813 return FAIL;
2814 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002815 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 *arg = skipwhite(*arg + 1);
2817 }
2818
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 *arg = *arg + 1;
2820
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002821 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002822 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002823 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002824 *arg += STRLEN(*arg);
2825
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826 dict_unref(d);
2827 return generate_NEWDICT(cctx, count);
2828
2829failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002830 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02002831 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002832 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02002833 *arg = (char_u *)"";
2834 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835 dict_unref(d);
2836 return FAIL;
2837}
2838
2839/*
2840 * Compile "&option".
2841 */
2842 static int
2843compile_get_option(char_u **arg, cctx_T *cctx)
2844{
2845 typval_T rettv;
2846 char_u *start = *arg;
2847 int ret;
2848
2849 // parse the option and get the current value to get the type.
2850 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002851 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 if (ret == OK)
2853 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002854 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855 char_u *name = vim_strnsave(start, *arg - start);
2856 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2857
2858 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2859 vim_free(name);
2860 }
2861 clear_tv(&rettv);
2862
2863 return ret;
2864}
2865
2866/*
2867 * Compile "$VAR".
2868 */
2869 static int
2870compile_get_env(char_u **arg, cctx_T *cctx)
2871{
2872 char_u *start = *arg;
2873 int len;
2874 int ret;
2875 char_u *name;
2876
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 ++*arg;
2878 len = get_env_len(arg);
2879 if (len == 0)
2880 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002881 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 return FAIL;
2883 }
2884
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002885 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886 name = vim_strnsave(start, len + 1);
2887 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2888 vim_free(name);
2889 return ret;
2890}
2891
2892/*
2893 * Compile "@r".
2894 */
2895 static int
2896compile_get_register(char_u **arg, cctx_T *cctx)
2897{
2898 int ret;
2899
2900 ++*arg;
2901 if (**arg == NUL)
2902 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002903 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904 return FAIL;
2905 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002906 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907 {
2908 emsg_invreg(**arg);
2909 return FAIL;
2910 }
2911 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2912 ++*arg;
2913 return ret;
2914}
2915
2916/*
2917 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002918 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 */
2920 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002921apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002923 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924
2925 // this works from end to start
2926 while (p > start)
2927 {
2928 --p;
2929 if (*p == '-' || *p == '+')
2930 {
2931 // only '-' has an effect, for '+' we only check the type
2932#ifdef FEAT_FLOAT
2933 if (rettv->v_type == VAR_FLOAT)
2934 {
2935 if (*p == '-')
2936 rettv->vval.v_float = -rettv->vval.v_float;
2937 }
2938 else
2939#endif
2940 {
2941 varnumber_T val;
2942 int error = FALSE;
2943
2944 // tv_get_number_chk() accepts a string, but we don't want that
2945 // here
2946 if (check_not_string(rettv) == FAIL)
2947 return FAIL;
2948 val = tv_get_number_chk(rettv, &error);
2949 clear_tv(rettv);
2950 if (error)
2951 return FAIL;
2952 if (*p == '-')
2953 val = -val;
2954 rettv->v_type = VAR_NUMBER;
2955 rettv->vval.v_number = val;
2956 }
2957 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002958 else if (numeric_only)
2959 {
2960 ++p;
2961 break;
2962 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 else
2964 {
2965 int v = tv2bool(rettv);
2966
2967 // '!' is permissive in the type.
2968 clear_tv(rettv);
2969 rettv->v_type = VAR_BOOL;
2970 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2971 }
2972 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002973 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974 return OK;
2975}
2976
2977/*
2978 * Recognize v: variables that are constants and set "rettv".
2979 */
2980 static void
2981get_vim_constant(char_u **arg, typval_T *rettv)
2982{
2983 if (STRNCMP(*arg, "v:true", 6) == 0)
2984 {
2985 rettv->v_type = VAR_BOOL;
2986 rettv->vval.v_number = VVAL_TRUE;
2987 *arg += 6;
2988 }
2989 else if (STRNCMP(*arg, "v:false", 7) == 0)
2990 {
2991 rettv->v_type = VAR_BOOL;
2992 rettv->vval.v_number = VVAL_FALSE;
2993 *arg += 7;
2994 }
2995 else if (STRNCMP(*arg, "v:null", 6) == 0)
2996 {
2997 rettv->v_type = VAR_SPECIAL;
2998 rettv->vval.v_number = VVAL_NULL;
2999 *arg += 6;
3000 }
3001 else if (STRNCMP(*arg, "v:none", 6) == 0)
3002 {
3003 rettv->v_type = VAR_SPECIAL;
3004 rettv->vval.v_number = VVAL_NONE;
3005 *arg += 6;
3006 }
3007}
3008
Bram Moolenaar696ba232020-07-29 21:20:41 +02003009 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003010get_compare_type(char_u *p, int *len, int *type_is)
3011{
3012 exptype_T type = EXPR_UNKNOWN;
3013 int i;
3014
3015 switch (p[0])
3016 {
3017 case '=': if (p[1] == '=')
3018 type = EXPR_EQUAL;
3019 else if (p[1] == '~')
3020 type = EXPR_MATCH;
3021 break;
3022 case '!': if (p[1] == '=')
3023 type = EXPR_NEQUAL;
3024 else if (p[1] == '~')
3025 type = EXPR_NOMATCH;
3026 break;
3027 case '>': if (p[1] != '=')
3028 {
3029 type = EXPR_GREATER;
3030 *len = 1;
3031 }
3032 else
3033 type = EXPR_GEQUAL;
3034 break;
3035 case '<': if (p[1] != '=')
3036 {
3037 type = EXPR_SMALLER;
3038 *len = 1;
3039 }
3040 else
3041 type = EXPR_SEQUAL;
3042 break;
3043 case 'i': if (p[1] == 's')
3044 {
3045 // "is" and "isnot"; but not a prefix of a name
3046 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3047 *len = 5;
3048 i = p[*len];
3049 if (!isalnum(i) && i != '_')
3050 {
3051 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3052 *type_is = TRUE;
3053 }
3054 }
3055 break;
3056 }
3057 return type;
3058}
3059
3060/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003062 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003063 */
3064 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003065compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003067 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068
3069 // this works from end to start
3070 while (p > start)
3071 {
3072 --p;
3073 if (*p == '-' || *p == '+')
3074 {
3075 int negate = *p == '-';
3076 isn_T *isn;
3077
3078 // TODO: check type
3079 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3080 {
3081 --p;
3082 if (*p == '-')
3083 negate = !negate;
3084 }
3085 // only '-' has an effect, for '+' we only check the type
3086 if (negate)
3087 isn = generate_instr(cctx, ISN_NEGATENR);
3088 else
3089 isn = generate_instr(cctx, ISN_CHECKNR);
3090 if (isn == NULL)
3091 return FAIL;
3092 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003093 else if (numeric_only)
3094 {
3095 ++p;
3096 break;
3097 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 else
3099 {
3100 int invert = TRUE;
3101
3102 while (p > start && p[-1] == '!')
3103 {
3104 --p;
3105 invert = !invert;
3106 }
3107 if (generate_2BOOL(cctx, invert) == FAIL)
3108 return FAIL;
3109 }
3110 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003111 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 return OK;
3113}
3114
3115/*
3116 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003117 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 */
3119 static int
3120compile_subscript(
3121 char_u **arg,
3122 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003123 char_u *start_leader,
3124 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003125 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003127 char_u *name_start = *end_leader;
3128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 for (;;)
3130 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003131 char_u *p = skipwhite(*arg);
3132
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003133 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003134 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003135 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003136
3137 // If a following line starts with "->{" or "->X" advance to that
3138 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003139 // Also if a following line starts with ".x".
3140 if (next != NULL &&
3141 ((next[0] == '-' && next[1] == '>'
3142 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003143 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003144 {
3145 next = next_line_from_context(cctx, TRUE);
3146 if (next == NULL)
3147 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003148 *arg = next;
3149 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003150 }
3151 }
3152
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003153 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3154 // not a function call.
3155 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003157 garray_T *stack = &cctx->ctx_type_stack;
3158 type_T *type;
3159 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003161 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003162 return FAIL;
3163
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003165 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3166
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003167 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3169 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003170 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 return FAIL;
3172 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003173 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003175 char_u *pstart = p;
3176
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003177 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003178 return FAIL;
3179
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 // something->method()
3181 // Apply the '!', '-' and '+' first:
3182 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003183 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003186 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003187 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003188 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 if (**arg == '{')
3190 {
3191 // lambda call: list->{lambda}
3192 if (compile_lambda_call(arg, cctx) == FAIL)
3193 return FAIL;
3194 }
3195 else
3196 {
3197 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003198 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003199 if (!eval_isnamec1(*p))
3200 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003201 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003202 return FAIL;
3203 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003204 if (ASCII_ISALPHA(*p) && p[1] == ':')
3205 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003206 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207 ;
3208 if (*p != '(')
3209 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003210 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 return FAIL;
3212 }
3213 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003214 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 return FAIL;
3216 }
3217 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003218 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003220 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003221 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003222 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003223 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003224 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003225
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003226 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003227 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003228 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003229 // TODO: blob index
3230 // TODO: more arguments
3231 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003232 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003233 return FAIL;
3234
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003235 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003236 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003237 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003238 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003239 if (**arg == ':')
3240 // missing first index is equal to zero
3241 generate_PUSHNR(cctx, 0);
3242 else
3243 {
3244 if (compile_expr0(arg, cctx) == FAIL)
3245 return FAIL;
3246 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3247 return FAIL;
3248 *arg = skipwhite(*arg);
3249 }
3250 if (**arg == ':')
3251 {
3252 *arg = skipwhite(*arg + 1);
3253 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3254 return FAIL;
3255 if (**arg == ']')
3256 // missing second index is equal to end of string
3257 generate_PUSHNR(cctx, -1);
3258 else
3259 {
3260 if (compile_expr0(arg, cctx) == FAIL)
3261 return FAIL;
3262 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3263 return FAIL;
3264 *arg = skipwhite(*arg);
3265 }
3266 is_slice = TRUE;
3267 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268
3269 if (**arg != ']')
3270 {
3271 emsg(_(e_missbrac));
3272 return FAIL;
3273 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003274 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003276 // We can index a list and a dict. If we don't know the type
3277 // we can use the index value type.
3278 // TODO: If we don't know use an instruction to figure it out at
3279 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003280 typep = ((type_T **)stack->ga_data) + stack->ga_len
3281 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003282 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003283 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3284 // If the index is a string, the variable must be a Dict.
3285 if (*typep == &t_any && valtype == &t_string)
3286 vtype = VAR_DICT;
3287 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003288 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003289 if (need_type(valtype, &t_number, -1, cctx, FALSE) == FAIL)
3290 return FAIL;
3291 if (is_slice)
3292 {
3293 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
3294 if (need_type(valtype, &t_number, -2, cctx, FALSE) == FAIL)
3295 return FAIL;
3296 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003297 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003298
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003299 if (vtype == VAR_DICT)
3300 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003301 if (is_slice)
3302 {
3303 emsg(_(e_cannot_slice_dictionary));
3304 return FAIL;
3305 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003306 if ((*typep)->tt_type == VAR_DICT)
3307 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003308 else
3309 {
3310 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3311 return FAIL;
3312 *typep = &t_any;
3313 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003314 if (may_generate_2STRING(-1, cctx) == FAIL)
3315 return FAIL;
3316 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3317 return FAIL;
3318 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003319 else if (vtype == VAR_STRING)
3320 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003321 *typep = &t_string;
3322 if ((is_slice
3323 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3324 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003325 return FAIL;
3326 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003327 else if (vtype == VAR_BLOB)
3328 {
3329 emsg("Sorry, blob index and slice not implemented yet");
3330 return FAIL;
3331 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003332 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003333 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003334 if (is_slice)
3335 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003336 if (generate_instr_drop(cctx,
3337 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3338 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003339 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003340 }
Bram Moolenaared591872020-08-15 22:14:53 +02003341 else
3342 {
3343 if ((*typep)->tt_type == VAR_LIST)
3344 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003345 if (generate_instr_drop(cctx,
3346 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3347 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003348 return FAIL;
3349 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003350 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003351 else
3352 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003353 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003354 return FAIL;
3355 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003357 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003358 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003359 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003360 return FAIL;
3361
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003362 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003363 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003364 {
3365 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003366 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003367 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003369 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003370 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371 while (eval_isnamec(*p))
3372 MB_PTR_ADV(p);
3373 if (p == *arg)
3374 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003375 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376 return FAIL;
3377 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003378 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379 return FAIL;
3380 *arg = p;
3381 }
3382 else
3383 break;
3384 }
3385
3386 // TODO - see handle_subscript():
3387 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3388 // Don't do this when "Func" is already a partial that was bound
3389 // explicitly (pt_auto is FALSE).
3390
3391 return OK;
3392}
3393
3394/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003395 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3396 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003398 * If the value is a constant "ppconst->pp_ret" will be set.
3399 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003400 *
3401 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 */
3403
3404/*
3405 * number number constant
3406 * 0zFFFFFFFF Blob constant
3407 * "string" string constant
3408 * 'string' literal string constant
3409 * &option-name option value
3410 * @r register contents
3411 * identifier variable value
3412 * function() function call
3413 * $VAR environment variable
3414 * (expression) nested expression
3415 * [expr, expr] List
3416 * {key: val, key: val} Dictionary
3417 * #{key: val, key: val} Dictionary with literal keys
3418 *
3419 * Also handle:
3420 * ! in front logical NOT
3421 * - in front unary minus
3422 * + in front unary plus (ignored)
3423 * trailing (arg) funcref/partial call
3424 * trailing [] subscript in String or List
3425 * trailing .name entry in Dictionary
3426 * trailing ->name() method call
3427 */
3428 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003429compile_expr7(
3430 char_u **arg,
3431 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003432 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003433{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434 char_u *start_leader, *end_leader;
3435 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003436 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003437 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438
3439 /*
3440 * Skip '!', '-' and '+' characters. They are handled later.
3441 */
3442 start_leader = *arg;
3443 while (**arg == '!' || **arg == '-' || **arg == '+')
3444 *arg = skipwhite(*arg + 1);
3445 end_leader = *arg;
3446
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003447 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448 switch (**arg)
3449 {
3450 /*
3451 * Number constant.
3452 */
3453 case '0': // also for blob starting with 0z
3454 case '1':
3455 case '2':
3456 case '3':
3457 case '4':
3458 case '5':
3459 case '6':
3460 case '7':
3461 case '8':
3462 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003463 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003464 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003465 // Apply "-" and "+" just before the number now, right to
3466 // left. Matters especially when "->" follows. Stops at
3467 // '!'.
3468 if (apply_leader(rettv, TRUE,
3469 start_leader, &end_leader) == FAIL)
3470 {
3471 clear_tv(rettv);
3472 return FAIL;
3473 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 break;
3475
3476 /*
3477 * String constant: "string".
3478 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003479 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003480 return FAIL;
3481 break;
3482
3483 /*
3484 * Literal string constant: 'str''ing'.
3485 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003486 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 return FAIL;
3488 break;
3489
3490 /*
3491 * Constant Vim variable.
3492 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003493 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494 ret = NOTDONE;
3495 break;
3496
3497 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003498 * "true" constant
3499 */
3500 case 't': if (STRNCMP(*arg, "true", 4) == 0
3501 && !eval_isnamec((*arg)[4]))
3502 {
3503 *arg += 4;
3504 rettv->v_type = VAR_BOOL;
3505 rettv->vval.v_number = VVAL_TRUE;
3506 }
3507 else
3508 ret = NOTDONE;
3509 break;
3510
3511 /*
3512 * "false" constant
3513 */
3514 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3515 && !eval_isnamec((*arg)[5]))
3516 {
3517 *arg += 5;
3518 rettv->v_type = VAR_BOOL;
3519 rettv->vval.v_number = VVAL_FALSE;
3520 }
3521 else
3522 ret = NOTDONE;
3523 break;
3524
3525 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003526 * List: [expr, expr]
3527 */
3528 case '[': ret = compile_list(arg, cctx);
3529 break;
3530
3531 /*
3532 * Dictionary: #{key: val, key: val}
3533 */
3534 case '#': if ((*arg)[1] == '{')
3535 {
3536 ++*arg;
3537 ret = compile_dict(arg, cctx, TRUE);
3538 }
3539 else
3540 ret = NOTDONE;
3541 break;
3542
3543 /*
3544 * Lambda: {arg, arg -> expr}
3545 * Dictionary: {'key': val, 'key': val}
3546 */
3547 case '{': {
3548 char_u *start = skipwhite(*arg + 1);
3549
3550 // Find out what comes after the arguments.
3551 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003552 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553 if (ret != FAIL && *start == '>')
3554 ret = compile_lambda(arg, cctx);
3555 else
3556 ret = compile_dict(arg, cctx, FALSE);
3557 }
3558 break;
3559
3560 /*
3561 * Option value: &name
3562 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003563 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3564 return FAIL;
3565 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 break;
3567
3568 /*
3569 * Environment variable: $VAR.
3570 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003571 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3572 return FAIL;
3573 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003574 break;
3575
3576 /*
3577 * Register contents: @r.
3578 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003579 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3580 return FAIL;
3581 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003582 break;
3583 /*
3584 * nested expression: (expression).
3585 */
3586 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003587
3588 // recursive!
3589 if (ppconst->pp_used <= PPSIZE - 10)
3590 {
3591 ret = compile_expr1(arg, cctx, ppconst);
3592 }
3593 else
3594 {
3595 // Not enough space in ppconst, flush constants.
3596 if (generate_ppconst(cctx, ppconst) == FAIL)
3597 return FAIL;
3598 ret = compile_expr0(arg, cctx);
3599 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600 *arg = skipwhite(*arg);
3601 if (**arg == ')')
3602 ++*arg;
3603 else if (ret == OK)
3604 {
3605 emsg(_(e_missing_close));
3606 ret = FAIL;
3607 }
3608 break;
3609
3610 default: ret = NOTDONE;
3611 break;
3612 }
3613 if (ret == FAIL)
3614 return FAIL;
3615
Bram Moolenaar1c747212020-05-09 18:28:34 +02003616 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003617 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003618 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003619 clear_tv(rettv);
3620 else
3621 // A constant expression can possibly be handled compile time,
3622 // return the value instead of generating code.
3623 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624 }
3625 else if (ret == NOTDONE)
3626 {
3627 char_u *p;
3628 int r;
3629
3630 if (!eval_isnamec1(**arg))
3631 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003632 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 return FAIL;
3634 }
3635
3636 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003637 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003639 {
3640 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3641 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003643 {
3644 if (generate_ppconst(cctx, ppconst) == FAIL)
3645 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02003646 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003647 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648 if (r == FAIL)
3649 return FAIL;
3650 }
3651
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003652 // Handle following "[]", ".member", etc.
3653 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003654 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003655 ppconst) == FAIL)
3656 return FAIL;
3657 if (ppconst->pp_used > 0)
3658 {
3659 // apply the '!', '-' and '+' before the constant
3660 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003661 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003662 return FAIL;
3663 return OK;
3664 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003665 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003667 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668}
3669
3670/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003671 * Give the "white on both sides" error, taking the operator from "p[len]".
3672 */
3673 void
3674error_white_both(char_u *op, int len)
3675{
3676 char_u buf[10];
3677
3678 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003679 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003680}
3681
3682/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003683 * <type>expr7: runtime type check / conversion
3684 */
3685 static int
3686compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3687{
3688 type_T *want_type = NULL;
3689
3690 // Recognize <type>
3691 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3692 {
3693 int called_emsg_before = called_emsg;
3694
3695 ++*arg;
3696 want_type = parse_type(arg, cctx->ctx_type_list);
3697 if (called_emsg != called_emsg_before)
3698 return FAIL;
3699
3700 if (**arg != '>')
3701 {
3702 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003703 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003704 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003705 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003706 return FAIL;
3707 }
3708 ++*arg;
3709 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3710 return FAIL;
3711 }
3712
3713 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3714 return FAIL;
3715
3716 if (want_type != NULL)
3717 {
3718 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003719 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003720
Bram Moolenaard1103582020-08-14 22:44:25 +02003721 generate_ppconst(cctx, ppconst);
3722 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02003723 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003724 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003725 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3726 return FAIL;
3727 }
3728 }
3729
3730 return OK;
3731}
3732
3733/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 * * number multiplication
3735 * / number division
3736 * % number modulo
3737 */
3738 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003739compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740{
3741 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003742 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003743 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003745 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003746 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 return FAIL;
3748
3749 /*
3750 * Repeat computing, until no "*", "/" or "%" is following.
3751 */
3752 for (;;)
3753 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003754 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003755 if (*op != '*' && *op != '/' && *op != '%')
3756 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003757 if (next != NULL)
3758 {
3759 *arg = next_line_from_context(cctx, TRUE);
3760 op = skipwhite(*arg);
3761 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003762
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003763 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003764 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003765 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003766 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767 }
3768 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003769 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003770 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003772 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003773 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003775
3776 if (ppconst->pp_used == ppconst_used + 2
3777 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3778 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003779 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003780 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3781 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003782 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003784 // both are numbers: compute the result
3785 switch (*op)
3786 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003787 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003788 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003789 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003790 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003791 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003792 break;
3793 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003794 tv1->vval.v_number = res;
3795 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003796 }
3797 else
3798 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003799 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003800 generate_two_op(cctx, op);
3801 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 }
3803
3804 return OK;
3805}
3806
3807/*
3808 * + number addition
3809 * - number subtraction
3810 * .. string concatenation
3811 */
3812 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003813compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814{
3815 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003816 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003818 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819
3820 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003821 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822 return FAIL;
3823
3824 /*
3825 * Repeat computing, until no "+", "-" or ".." is following.
3826 */
3827 for (;;)
3828 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003829 op = may_peek_next_line(cctx, *arg, &next);
3830 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 break;
3832 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003833 if (next != NULL)
3834 {
3835 *arg = next_line_from_context(cctx, TRUE);
3836 op = skipwhite(*arg);
3837 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003839 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003841 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003842 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003843 }
3844
3845 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003846 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003847 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003849 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003850 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 return FAIL;
3852
Bram Moolenaara5565e42020-05-09 15:44:01 +02003853 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003854 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003855 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3856 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3857 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3858 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003860 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3861 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003862
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003863 // concat/subtract/add constant numbers
3864 if (*op == '+')
3865 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3866 else if (*op == '-')
3867 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3868 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003869 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003870 // concatenate constant strings
3871 char_u *s1 = tv1->vval.v_string;
3872 char_u *s2 = tv2->vval.v_string;
3873 size_t len1 = STRLEN(s1);
3874
3875 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3876 if (tv1->vval.v_string == NULL)
3877 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003878 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003879 return FAIL;
3880 }
3881 mch_memmove(tv1->vval.v_string, s1, len1);
3882 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003883 vim_free(s1);
3884 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003885 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003886 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003887 }
3888 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003889 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003890 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003891 if (*op == '.')
3892 {
3893 if (may_generate_2STRING(-2, cctx) == FAIL
3894 || may_generate_2STRING(-1, cctx) == FAIL)
3895 return FAIL;
3896 generate_instr_drop(cctx, ISN_CONCAT, 1);
3897 }
3898 else
3899 generate_two_op(cctx, op);
3900 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901 }
3902
3903 return OK;
3904}
3905
3906/*
3907 * expr5a == expr5b
3908 * expr5a =~ expr5b
3909 * expr5a != expr5b
3910 * expr5a !~ expr5b
3911 * expr5a > expr5b
3912 * expr5a >= expr5b
3913 * expr5a < expr5b
3914 * expr5a <= expr5b
3915 * expr5a is expr5b
3916 * expr5a isnot expr5b
3917 *
3918 * Produces instructions:
3919 * EVAL expr5a Push result of "expr5a"
3920 * EVAL expr5b Push result of "expr5b"
3921 * COMPARE one of the compare instructions
3922 */
3923 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003924compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925{
3926 exptype_T type = EXPR_UNKNOWN;
3927 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003928 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003929 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003930 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003931 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932
3933 // get the first variable
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 Moolenaar67fbdfe2020-06-23 22:26:05 +02003937 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003938 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939
3940 /*
3941 * If there is a comparative operator, use it.
3942 */
3943 if (type != EXPR_UNKNOWN)
3944 {
3945 int ic = FALSE; // Default: do not ignore case
3946
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003947 if (next != NULL)
3948 {
3949 *arg = next_line_from_context(cctx, TRUE);
3950 p = skipwhite(*arg);
3951 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 if (type_is && (p[len] == '?' || p[len] == '#'))
3953 {
3954 semsg(_(e_invexpr2), *arg);
3955 return FAIL;
3956 }
3957 // extra question mark appended: ignore case
3958 if (p[len] == '?')
3959 {
3960 ic = TRUE;
3961 ++len;
3962 }
3963 // extra '#' appended: match case (ignored)
3964 else if (p[len] == '#')
3965 ++len;
3966 // nothing appended: match case
3967
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003968 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003970 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003971 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972 }
3973
3974 // get the second variable
3975 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003976 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003977 return FAIL;
3978
Bram Moolenaara5565e42020-05-09 15:44:01 +02003979 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980 return FAIL;
3981
Bram Moolenaara5565e42020-05-09 15:44:01 +02003982 if (ppconst->pp_used == ppconst_used + 2)
3983 {
3984 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3985 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3986 int ret;
3987
3988 // Both sides are a constant, compute the result now.
3989 // First check for a valid combination of types, this is more
3990 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003991 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003992 ret = FAIL;
3993 else
3994 {
3995 ret = typval_compare(tv1, tv2, type, ic);
3996 tv1->v_type = VAR_BOOL;
3997 tv1->vval.v_number = tv1->vval.v_number
3998 ? VVAL_TRUE : VVAL_FALSE;
3999 clear_tv(tv2);
4000 --ppconst->pp_used;
4001 }
4002 return ret;
4003 }
4004
4005 generate_ppconst(cctx, ppconst);
4006 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007 }
4008
4009 return OK;
4010}
4011
Bram Moolenaar7f141552020-05-09 17:35:53 +02004012static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4013
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014/*
4015 * Compile || or &&.
4016 */
4017 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004018compile_and_or(
4019 char_u **arg,
4020 cctx_T *cctx,
4021 char *op,
4022 ppconst_T *ppconst,
4023 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004025 char_u *next;
4026 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027 int opchar = *op;
4028
4029 if (p[0] == opchar && p[1] == opchar)
4030 {
4031 garray_T *instr = &cctx->ctx_instr;
4032 garray_T end_ga;
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004033 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004034 int all_bool_values = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004035
4036 /*
4037 * Repeat until there is no following "||" or "&&"
4038 */
4039 ga_init2(&end_ga, sizeof(int), 10);
4040 while (p[0] == opchar && p[1] == opchar)
4041 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004042 if (next != NULL)
4043 {
4044 *arg = next_line_from_context(cctx, TRUE);
4045 p = skipwhite(*arg);
4046 }
4047
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004048 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4049 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004050 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004051 return FAIL;
4052 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004054 // TODO: use ppconst if the value is a constant and check
4055 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004056 generate_ppconst(cctx, ppconst);
4057
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004058 if (((type_T **)stack->ga_data)[stack->ga_len - 1] != &t_bool)
4059 all_bool_values = FALSE;
4060
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061 if (ga_grow(&end_ga, 1) == FAIL)
4062 {
4063 ga_clear(&end_ga);
4064 return FAIL;
4065 }
4066 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4067 ++end_ga.ga_len;
4068 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004069 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070
4071 // eval the next expression
4072 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004073 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004074 return FAIL;
4075
Bram Moolenaara5565e42020-05-09 15:44:01 +02004076 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4077 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004078 {
4079 ga_clear(&end_ga);
4080 return FAIL;
4081 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004082
4083 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004085 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004086
4087 // Fill in the end label in all jumps.
4088 while (end_ga.ga_len > 0)
4089 {
4090 isn_T *isn;
4091
4092 --end_ga.ga_len;
4093 isn = ((isn_T *)instr->ga_data)
4094 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4095 isn->isn_arg.jump.jump_where = instr->ga_len;
4096 }
4097 ga_clear(&end_ga);
Bram Moolenaar4ed124c2020-09-09 20:03:46 +02004098
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004099 // The resulting type is converted to bool if needed.
4100 if (!all_bool_values)
4101 generate_COND2BOOL(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004102 }
4103
4104 return OK;
4105}
4106
4107/*
4108 * expr4a && expr4a && expr4a logical AND
4109 *
4110 * Produces instructions:
4111 * EVAL expr4a Push result of "expr4a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004112 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004114 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115 * EVAL expr4c Push result of "expr4c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004116 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004117 * end:
4118 */
4119 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004120compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004122 int ppconst_used = ppconst->pp_used;
4123
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004125 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126 return FAIL;
4127
4128 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004129 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130}
4131
4132/*
4133 * expr3a || expr3b || expr3c logical OR
4134 *
4135 * Produces instructions:
4136 * EVAL expr3a Push result of "expr3a"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004137 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004139 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 * EVAL expr3c Push result of "expr3c"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004141 * COND2BOOL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142 * end:
4143 */
4144 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004145compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004147 int ppconst_used = ppconst->pp_used;
4148
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004149 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004150 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004151 return FAIL;
4152
4153 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004154 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155}
4156
4157/*
4158 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004160 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161 * JUMP_IF_FALSE alt jump if false
4162 * EVAL expr1a
4163 * JUMP_ALWAYS end
4164 * alt: EVAL expr1b
4165 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004166 *
4167 * Toplevel expression: expr2 ?? expr1
4168 * Produces instructions:
4169 * EVAL expr2 Push result of "expr2"
4170 * JUMP_AND_KEEP_IF_TRUE end jump if true
4171 * EVAL expr1
4172 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004173 */
4174 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004175compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004176{
4177 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004178 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004179 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004180
Bram Moolenaar3988f642020-08-27 22:43:03 +02004181 // Ignore all kinds of errors when not producing code.
4182 if (cctx->ctx_skip == SKIP_YES)
4183 {
4184 skip_expr(arg);
4185 return OK;
4186 }
4187
Bram Moolenaar61a89812020-05-07 16:58:17 +02004188 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004189 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004190 return FAIL;
4191
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004192 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004193 if (*p == '?')
4194 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004195 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196 garray_T *instr = &cctx->ctx_instr;
4197 garray_T *stack = &cctx->ctx_type_stack;
4198 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004199 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004200 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004201 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004202 int has_const_expr = FALSE;
4203 int const_value = FALSE;
4204 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004206 if (next != NULL)
4207 {
4208 *arg = next_line_from_context(cctx, TRUE);
4209 p = skipwhite(*arg);
4210 }
4211
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004212 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004213 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004214 semsg(_(e_white_space_required_before_and_after_str),
4215 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004216 return FAIL;
4217 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218
Bram Moolenaara5565e42020-05-09 15:44:01 +02004219 if (ppconst->pp_used == ppconst_used + 1)
4220 {
4221 // the condition is a constant, we know whether the ? or the :
4222 // expression is to be evaluated.
4223 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004224 if (op_falsy)
4225 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4226 else
4227 {
4228 int error = FALSE;
4229
4230 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4231 &error);
4232 if (error)
4233 return FAIL;
4234 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004235 cctx->ctx_skip = save_skip == SKIP_YES ||
4236 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4237
4238 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4239 // "left ?? right" and "left" is truthy: produce "left"
4240 generate_ppconst(cctx, ppconst);
4241 else
4242 {
4243 clear_tv(&ppconst->pp_tv[ppconst_used]);
4244 --ppconst->pp_used;
4245 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004246 }
4247 else
4248 {
4249 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004250 if (op_falsy)
4251 end_idx = instr->ga_len;
4252 generate_JUMP(cctx, op_falsy
4253 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4254 if (op_falsy)
4255 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004256 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004257
4258 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004259 *arg = skipwhite(p + 1 + op_falsy);
4260 if (may_get_next_line(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004261 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004262 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004263 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264
Bram Moolenaara5565e42020-05-09 15:44:01 +02004265 if (!has_const_expr)
4266 {
4267 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004268
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004269 if (!op_falsy)
4270 {
4271 // remember the type and drop it
4272 --stack->ga_len;
4273 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004275 end_idx = instr->ga_len;
4276 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004277
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004278 // jump here from JUMP_IF_FALSE
4279 isn = ((isn_T *)instr->ga_data) + alt_idx;
4280 isn->isn_arg.jump.jump_where = instr->ga_len;
4281 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004282 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004284 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004285 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004286 // Check for the ":".
4287 p = may_peek_next_line(cctx, *arg, &next);
4288 if (*p != ':')
4289 {
4290 emsg(_(e_missing_colon));
4291 return FAIL;
4292 }
4293 if (next != NULL)
4294 {
4295 *arg = next_line_from_context(cctx, TRUE);
4296 p = skipwhite(*arg);
4297 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004298
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004299 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4300 {
4301 semsg(_(e_white_space_required_before_and_after_str), ":");
4302 return FAIL;
4303 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004305 // evaluate the third expression
4306 if (has_const_expr)
4307 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004308 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004309 *arg = skipwhite(p + 1);
4310 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
4311 return FAIL;
4312 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4313 return FAIL;
4314 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315
Bram Moolenaara5565e42020-05-09 15:44:01 +02004316 if (!has_const_expr)
4317 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004318 type_T **typep;
4319
Bram Moolenaara5565e42020-05-09 15:44:01 +02004320 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321
Bram Moolenaara5565e42020-05-09 15:44:01 +02004322 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004323 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4324 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004325
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004326 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004327 isn = ((isn_T *)instr->ga_data) + end_idx;
4328 isn->isn_arg.jump.jump_where = instr->ga_len;
4329 }
4330
4331 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 }
4333 return OK;
4334}
4335
4336/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004337 * Toplevel expression.
4338 */
4339 static int
4340compile_expr0(char_u **arg, cctx_T *cctx)
4341{
4342 ppconst_T ppconst;
4343
4344 CLEAR_FIELD(ppconst);
4345 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4346 {
4347 clear_ppconst(&ppconst);
4348 return FAIL;
4349 }
4350 if (generate_ppconst(cctx, &ppconst) == FAIL)
4351 return FAIL;
4352 return OK;
4353}
4354
4355/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004356 * compile "return [expr]"
4357 */
4358 static char_u *
4359compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4360{
4361 char_u *p = arg;
4362 garray_T *stack = &cctx->ctx_type_stack;
4363 type_T *stack_type;
4364
4365 if (*p != NUL && *p != '|' && *p != '\n')
4366 {
4367 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004368 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004369 return NULL;
4370
4371 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4372 if (set_return_type)
4373 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004374 else
4375 {
4376 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4377 && stack_type->tt_type != VAR_VOID
4378 && stack_type->tt_type != VAR_UNKNOWN)
4379 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004380 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004381 return NULL;
4382 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004383 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4384 cctx, FALSE) == FAIL)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004385 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004386 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387 }
4388 else
4389 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004390 // "set_return_type" cannot be TRUE, only used for a lambda which
4391 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004392 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4393 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004395 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396 return NULL;
4397 }
4398
4399 // No argument, return zero.
4400 generate_PUSHNR(cctx, 0);
4401 }
4402
4403 if (generate_instr(cctx, ISN_RETURN) == NULL)
4404 return NULL;
4405
4406 // "return val | endif" is possible
4407 return skipwhite(p);
4408}
4409
4410/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004411 * Get a line from the compilation context, compatible with exarg_T getline().
4412 * Return a pointer to the line in allocated memory.
4413 * Return NULL for end-of-file or some error.
4414 */
4415 static char_u *
4416exarg_getline(
4417 int c UNUSED,
4418 void *cookie,
4419 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004420 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004421{
4422 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004423 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004424
Bram Moolenaar66250c92020-08-20 15:02:42 +02004425 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004426 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004427 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004428 return NULL;
4429 ++cctx->ctx_lnum;
4430 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4431 // Comment lines result in NULL pointers, skip them.
4432 if (p != NULL)
4433 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004434 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004435}
4436
4437/*
4438 * Compile a nested :def command.
4439 */
4440 static char_u *
4441compile_nested_function(exarg_T *eap, cctx_T *cctx)
4442{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004443 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004444 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004445 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004446 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004447 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004448 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004449
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004450 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004451 {
4452 emsg(_(e_cannot_use_bang_with_nested_def));
4453 return NULL;
4454 }
4455
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004456 // Only g:Func() can use a namespace.
4457 if (name_start[1] == ':' && !is_global)
4458 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004459 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004460 return NULL;
4461 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004462 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4463 return NULL;
4464
Bram Moolenaar04b12692020-05-04 23:24:44 +02004465 eap->arg = name_end;
4466 eap->getline = exarg_getline;
4467 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004468 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004469 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004470 lambda_name = get_lambda_name();
4471 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004472
Bram Moolenaar822ba242020-05-24 23:00:18 +02004473 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004474 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004475 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004476 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004477 {
4478 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004479 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004480 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004481
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004482 if (is_global)
4483 {
4484 char_u *func_name = vim_strnsave(name_start + 2,
4485 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004486
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004487 if (func_name == NULL)
4488 r = FAIL;
4489 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004490 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004491 }
4492 else
4493 {
4494 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004495 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004496 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02004497
Bram Moolenaareef21022020-08-01 22:16:43 +02004498 if (lvar == NULL)
4499 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004500 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004501 return NULL;
4502 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4503 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004504
Bram Moolenaar61a89812020-05-07 16:58:17 +02004505 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004506 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004507}
4508
4509/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004510 * Return the length of an assignment operator, or zero if there isn't one.
4511 */
4512 int
4513assignment_len(char_u *p, int *heredoc)
4514{
4515 if (*p == '=')
4516 {
4517 if (p[1] == '<' && p[2] == '<')
4518 {
4519 *heredoc = TRUE;
4520 return 3;
4521 }
4522 return 1;
4523 }
4524 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4525 return 2;
4526 if (STRNCMP(p, "..=", 3) == 0)
4527 return 3;
4528 return 0;
4529}
4530
4531// words that cannot be used as a variable
4532static char *reserved[] = {
4533 "true",
4534 "false",
4535 NULL
4536};
4537
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004538typedef enum {
4539 dest_local,
4540 dest_option,
4541 dest_env,
4542 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004543 dest_buffer,
4544 dest_window,
4545 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004546 dest_vimvar,
4547 dest_script,
4548 dest_reg,
4549} assign_dest_T;
4550
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004551/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004552 * Generate the load instruction for "name".
4553 */
4554 static void
4555generate_loadvar(
4556 cctx_T *cctx,
4557 assign_dest_T dest,
4558 char_u *name,
4559 lvar_T *lvar,
4560 type_T *type)
4561{
4562 switch (dest)
4563 {
4564 case dest_option:
4565 // TODO: check the option exists
4566 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4567 break;
4568 case dest_global:
4569 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4570 break;
4571 case dest_buffer:
4572 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4573 break;
4574 case dest_window:
4575 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4576 break;
4577 case dest_tab:
4578 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4579 break;
4580 case dest_script:
4581 compile_load_scriptvar(cctx,
4582 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4583 break;
4584 case dest_env:
4585 // Include $ in the name here
4586 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4587 break;
4588 case dest_reg:
4589 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4590 break;
4591 case dest_vimvar:
4592 generate_LOADV(cctx, name + 2, TRUE);
4593 break;
4594 case dest_local:
4595 if (lvar->lv_from_outer)
4596 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4597 NULL, type);
4598 else
4599 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4600 break;
4601 }
4602}
4603
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004604 void
4605vim9_declare_error(char_u *name)
4606{
4607 char *scope = "";
4608
4609 switch (*name)
4610 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004611 case 'g': scope = _("global"); break;
4612 case 'b': scope = _("buffer"); break;
4613 case 'w': scope = _("window"); break;
4614 case 't': scope = _("tab"); break;
4615 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004616 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004617 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004618 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004619 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004620 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004621 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004622 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004623 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004624 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004625}
4626
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004627/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004628 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004629 * "let name"
4630 * "var name = expr"
4631 * "final name = expr"
4632 * "const name = expr"
4633 * "name = expr"
4634 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004635 * Return NULL for an error.
4636 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637 */
4638 static char_u *
4639compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4640{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004641 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004643 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 char_u *ret = NULL;
4645 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004646 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004647 int scriptvar_sid = 0;
4648 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004649 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004650 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004651 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653 int oplen = 0;
4654 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004655 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004656 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004657 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004658 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004659 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
4660 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004661
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004662 // Skip over the "var" or "[var, var]" to get to any "=".
4663 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4664 if (p == NULL)
4665 return *arg == '[' ? arg : NULL;
4666
4667 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004669 // TODO: should we allow this, and figure out type inference from list
4670 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004671 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004672 return NULL;
4673 }
4674
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004675 sp = p;
4676 p = skipwhite(p);
4677 op = p;
4678 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004679
4680 if (var_count > 0 && oplen == 0)
4681 // can be something like "[1, 2]->func()"
4682 return arg;
4683
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4685 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004686 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004687 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004688 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004689
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004690 if (heredoc)
4691 {
4692 list_T *l;
4693 listitem_T *li;
4694
4695 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004696 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004698 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02004699 if (l == NULL)
4700 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004701
Bram Moolenaar078269b2020-09-21 20:35:55 +02004702 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004703 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02004704 // Push each line and the create the list.
4705 FOR_ALL_LIST_ITEMS(l, li)
4706 {
4707 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4708 li->li_tv.vval.v_string = NULL;
4709 }
4710 generate_NEWLIST(cctx, l->lv_len);
4711 type = &t_list_string;
4712 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004714 list_free(l);
4715 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004716 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004717 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004718 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004719 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004720 // for "[var, var] = expr" evaluate the expression here, loop over the
4721 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004722
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004723 p = skipwhite(op + oplen);
4724 if (compile_expr0(&p, cctx) == FAIL)
4725 return NULL;
4726 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004728 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004729 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004730 type_T *stacktype;
4731
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004732 stacktype = stack->ga_len == 0 ? &t_void
4733 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004734 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004736 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004737 goto theend;
4738 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004739 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004740 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004741 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004742 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4743 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004744 }
4745 }
4746
4747 /*
4748 * Loop over variables in "[var, var] = expr".
4749 * For "var = expr" and "let var: type" this is done only once.
4750 */
4751 if (var_count > 0)
4752 var_start = skipwhite(arg + 1); // skip over the "["
4753 else
4754 var_start = arg;
4755 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4756 {
4757 char_u *var_end = skip_var_one(var_start, FALSE);
4758 size_t varlen;
4759 int new_local = FALSE;
4760 int opt_type;
4761 int opt_flags = 0;
4762 assign_dest_T dest = dest_local;
4763 int vimvaridx = -1;
4764 lvar_T *lvar = NULL;
4765 lvar_T arg_lvar;
4766 int has_type = FALSE;
4767 int has_index = FALSE;
4768 int instr_count = -1;
4769
Bram Moolenaar65821722020-08-02 18:58:54 +02004770 if (*var_start == '@')
4771 p = var_start + 2;
4772 else
4773 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02004774 // skip over the leading "&", "&l:", "&g:" and "$"
4775 p = skip_option_env_lead(var_start);
Bram Moolenaar65821722020-08-02 18:58:54 +02004776 p = to_name_end(p, TRUE);
4777 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004778
4779 // "a: type" is declaring variable "a" with a type, not "a:".
4780 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4781 --var_end;
4782 if (is_decl && p == var_start + 2 && p[-1] == ':')
4783 --p;
4784
4785 varlen = p - var_start;
4786 vim_free(name);
4787 name = vim_strnsave(var_start, varlen);
4788 if (name == NULL)
4789 return NULL;
4790 if (!heredoc)
4791 type = &t_any;
4792
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004793 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004794 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004795 int declare_error = FALSE;
4796
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004797 if (*var_start == '&')
4798 {
4799 int cc;
4800 long numval;
4801
4802 dest = dest_option;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004803 if (cmdidx == CMD_final || cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004804 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004805 emsg(_(e_const_option));
4806 goto theend;
4807 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004808 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004809 p = var_start;
4810 p = find_option_end(&p, &opt_flags);
4811 if (p == NULL)
4812 {
4813 // cannot happen?
4814 emsg(_(e_letunexp));
4815 goto theend;
4816 }
4817 cc = *p;
4818 *p = NUL;
Bram Moolenaar2e800952020-08-23 19:34:48 +02004819 opt_type = get_option_value(skip_option_env_lead(var_start),
4820 &numval, NULL, opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004821 *p = cc;
4822 if (opt_type == -3)
4823 {
4824 semsg(_(e_unknown_option), var_start);
4825 goto theend;
4826 }
4827 if (opt_type == -2 || opt_type == 0)
4828 type = &t_string;
4829 else
4830 type = &t_number; // both number and boolean option
4831 }
4832 else if (*var_start == '$')
4833 {
4834 dest = dest_env;
4835 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004836 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004837 }
4838 else if (*var_start == '@')
4839 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004840 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004841 {
4842 emsg_invreg(var_start[1]);
4843 goto theend;
4844 }
4845 dest = dest_reg;
4846 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004847 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004848 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004849 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004850 {
4851 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004852 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004853 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004854 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004855 {
4856 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004857 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004858 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004859 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004860 {
4861 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004862 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004863 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004864 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004865 {
4866 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004867 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004868 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004869 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004870 {
4871 typval_T *vtv;
4872 int di_flags;
4873
4874 vimvaridx = find_vim_var(name + 2, &di_flags);
4875 if (vimvaridx < 0)
4876 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004877 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004878 goto theend;
4879 }
4880 // We use the current value of "sandbox" here, is that OK?
4881 if (var_check_ro(di_flags, name, FALSE))
4882 goto theend;
4883 dest = dest_vimvar;
4884 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004885 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004886 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004887 }
4888 else
4889 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004890 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004891
4892 for (idx = 0; reserved[idx] != NULL; ++idx)
4893 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004894 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004895 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004896 goto theend;
4897 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004898
4899 lvar = lookup_local(var_start, varlen, cctx);
4900 if (lvar == NULL)
4901 {
4902 CLEAR_FIELD(arg_lvar);
4903 if (lookup_arg(var_start, varlen,
4904 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4905 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004906 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004907 if (is_decl)
4908 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004909 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004910 goto theend;
4911 }
4912 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004913 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004914 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004915 if (lvar != NULL)
4916 {
4917 if (is_decl)
4918 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004919 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004920 goto theend;
4921 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004922 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004923 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004924 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004925 int script_namespace = varlen > 1
4926 && STRNCMP(var_start, "s:", 2) == 0;
4927 int script_var = (script_namespace
Bram Moolenaar53900992020-08-22 19:02:02 +02004928 ? lookup_script(var_start + 2, varlen - 2, FALSE)
4929 : lookup_script(var_start, varlen, TRUE)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004930 imported_T *import =
4931 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004932
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004933 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004934 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004935 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4936
4937 if (is_decl)
4938 {
4939 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004940 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004941 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004942 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004943 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004944 name);
4945 goto theend;
4946 }
4947 else if (cctx->ctx_ufunc->uf_script_ctx_version
4948 == SCRIPT_VERSION_VIM9
4949 && script_namespace
4950 && !script_var && import == NULL)
4951 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004952 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004953 goto theend;
4954 }
4955
4956 dest = dest_script;
4957
4958 // existing script-local variables should have a type
4959 scriptvar_sid = current_sctx.sc_sid;
4960 if (import != NULL)
4961 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004962 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004963 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02004964 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4965 rawname, TRUE);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02004966 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02004967 {
4968 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4969 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004970 ((svar_T *)si->sn_var_vals.ga_data)
4971 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02004972 type = sv->sv_type;
4973 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004974 }
4975 }
4976 else if (name[1] == ':' && name[2] != NUL)
4977 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004978 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004979 goto theend;
4980 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004981 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004982 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004983 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004984 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004985 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004986 else if (check_defined(var_start, varlen, cctx) == FAIL)
4987 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004988 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004989 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004990
4991 if (declare_error)
4992 {
4993 vim9_declare_error(name);
4994 goto theend;
4995 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004996 }
4997
4998 // handle "a:name" as a name, not index "name" on "a"
4999 if (varlen > 1 || var_start[varlen] != ':')
5000 p = var_end;
5001
5002 if (dest != dest_option)
5003 {
5004 if (is_decl && *p == ':')
5005 {
5006 // parse optional type: "let var: type = expr"
5007 if (!VIM_ISWHITE(p[1]))
5008 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005009 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005010 goto theend;
5011 }
5012 p = skipwhite(p + 1);
5013 type = parse_type(&p, cctx->ctx_type_list);
5014 has_type = TRUE;
5015 }
5016 else if (lvar != NULL)
5017 type = lvar->lv_type;
5018 }
5019
5020 if (oplen == 3 && !heredoc && dest != dest_global
5021 && type->tt_type != VAR_STRING
5022 && type->tt_type != VAR_ANY)
5023 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005024 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005025 goto theend;
5026 }
5027
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005028 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005029 {
5030 if (oplen > 1 && !heredoc)
5031 {
5032 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005033 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005034 goto theend;
5035 }
5036
5037 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005038 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5039 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005040 goto theend;
5041 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005042 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005043 if (lvar == NULL)
5044 goto theend;
5045 new_local = TRUE;
5046 }
5047
5048 member_type = type;
5049 if (var_end > var_start + varlen)
5050 {
5051 // Something follows after the variable: "var[idx]".
5052 if (is_decl)
5053 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005054 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005055 goto theend;
5056 }
5057
5058 if (var_start[varlen] == '[')
5059 {
5060 has_index = TRUE;
5061 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005062 member_type = &t_any;
5063 else
5064 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005065 }
5066 else
5067 {
5068 semsg("Not supported yet: %s", var_start);
5069 goto theend;
5070 }
5071 }
5072 else if (lvar == &arg_lvar)
5073 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005074 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005075 goto theend;
5076 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005077 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5078 {
5079 semsg(_(e_cannot_assign_to_constant), name);
5080 goto theend;
5081 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005082
5083 if (!heredoc)
5084 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005085 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005086 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005087 if (oplen > 0 && var_count == 0)
5088 {
5089 // skip over the "=" and the expression
5090 p = skipwhite(op + oplen);
5091 compile_expr0(&p, cctx);
5092 }
5093 }
5094 else if (oplen > 0)
5095 {
5096 type_T *stacktype;
5097
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005098 // For "var = expr" evaluate the expression.
5099 if (var_count == 0)
5100 {
5101 int r;
5102
5103 // for "+=", "*=", "..=" etc. first load the current value
5104 if (*op != '=')
5105 {
5106 generate_loadvar(cctx, dest, name, lvar, type);
5107
5108 if (has_index)
5109 {
5110 // TODO: get member from list or dict
5111 emsg("Index with operation not supported yet");
5112 goto theend;
5113 }
5114 }
5115
5116 // Compile the expression. Temporarily hide the new local
5117 // variable here, it is not available to this expression.
5118 if (new_local)
5119 --cctx->ctx_locals.ga_len;
5120 instr_count = instr->ga_len;
5121 p = skipwhite(op + oplen);
5122 r = compile_expr0(&p, cctx);
5123 if (new_local)
5124 ++cctx->ctx_locals.ga_len;
5125 if (r == FAIL)
5126 goto theend;
5127 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005128 else if (semicolon && var_idx == var_count - 1)
5129 {
5130 // For "[var; var] = expr" get the rest of the list
5131 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5132 goto theend;
5133 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005134 else
5135 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005136 // For "[var, var] = expr" get the "var_idx" item from the
5137 // list.
5138 if (generate_GETITEM(cctx, var_idx) == FAIL)
5139 return FAIL;
5140 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005141
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005142 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005143 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005144 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005145 {
Bram Moolenaar0f769812020-09-12 18:32:34 +02005146 if ((stacktype->tt_type == VAR_FUNC
5147 || stacktype->tt_type == VAR_PARTIAL)
5148 && var_wrong_func_name(name, TRUE))
5149 goto theend;
5150
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005151 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005152 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005153 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005154 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005155 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005156 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005157 }
5158 else
5159 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005160 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005161 // for a variable that implies &t_any.
5162 if (stacktype == &t_list_empty)
5163 lvar->lv_type = &t_list_any;
5164 else if (stacktype == &t_dict_empty)
5165 lvar->lv_type = &t_dict_any;
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005166 else if (stacktype == &t_unknown)
5167 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005168 else
5169 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005170 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005171 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005172 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005173 {
5174 type_T *use_type = lvar->lv_type;
5175
Bram Moolenaar71abe482020-09-14 22:28:30 +02005176 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005177 if (has_index)
5178 {
5179 use_type = use_type->tt_member;
5180 if (use_type == NULL)
Bram Moolenaar71abe482020-09-14 22:28:30 +02005181 // could be indexing "any"
5182 use_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005183 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005184 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005185 == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005186 goto theend;
5187 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005188 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005189 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005190 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005191 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005192 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005193 else if (cmdidx == CMD_final)
5194 {
5195 emsg(_(e_final_requires_a_value));
5196 goto theend;
5197 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005198 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005199 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005200 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005201 goto theend;
5202 }
5203 else if (!has_type || dest == dest_option)
5204 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005205 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005206 goto theend;
5207 }
5208 else
5209 {
5210 // variables are always initialized
5211 if (ga_grow(instr, 1) == FAIL)
5212 goto theend;
5213 switch (member_type->tt_type)
5214 {
5215 case VAR_BOOL:
5216 generate_PUSHBOOL(cctx, VVAL_FALSE);
5217 break;
5218 case VAR_FLOAT:
5219#ifdef FEAT_FLOAT
5220 generate_PUSHF(cctx, 0.0);
5221#endif
5222 break;
5223 case VAR_STRING:
5224 generate_PUSHS(cctx, NULL);
5225 break;
5226 case VAR_BLOB:
5227 generate_PUSHBLOB(cctx, NULL);
5228 break;
5229 case VAR_FUNC:
5230 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5231 break;
5232 case VAR_LIST:
5233 generate_NEWLIST(cctx, 0);
5234 break;
5235 case VAR_DICT:
5236 generate_NEWDICT(cctx, 0);
5237 break;
5238 case VAR_JOB:
5239 generate_PUSHJOB(cctx, NULL);
5240 break;
5241 case VAR_CHANNEL:
5242 generate_PUSHCHANNEL(cctx, NULL);
5243 break;
5244 case VAR_NUMBER:
5245 case VAR_UNKNOWN:
5246 case VAR_ANY:
5247 case VAR_PARTIAL:
5248 case VAR_VOID:
5249 case VAR_SPECIAL: // cannot happen
5250 generate_PUSHNR(cctx, 0);
5251 break;
5252 }
5253 }
5254 if (var_count == 0)
5255 end = p;
5256 }
5257
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005258 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005259 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005260 break;
5261
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005262 if (oplen > 0 && *op != '=')
5263 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005264 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005265 type_T *stacktype;
5266
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005267 if (*op == '.')
5268 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005269 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005270 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005271 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005272 if (
5273#ifdef FEAT_FLOAT
5274 // If variable is float operation with number is OK.
5275 !(expected == &t_float && stacktype == &t_number) &&
5276#endif
5277 need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005278 goto theend;
5279
5280 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005281 {
5282 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5283 goto theend;
5284 }
5285 else if (*op == '+')
5286 {
5287 if (generate_add_instr(cctx,
5288 operator_type(member_type, stacktype),
5289 member_type, stacktype) == FAIL)
5290 goto theend;
5291 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005292 else if (generate_two_op(cctx, op) == FAIL)
5293 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005294 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005295
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005296 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005297 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005298 int r;
5299
5300 // Compile the "idx" in "var[idx]".
5301 if (new_local)
5302 --cctx->ctx_locals.ga_len;
5303 p = skipwhite(var_start + varlen + 1);
5304 r = compile_expr0(&p, cctx);
5305 if (new_local)
5306 ++cctx->ctx_locals.ga_len;
5307 if (r == FAIL)
5308 goto theend;
5309 if (*skipwhite(p) != ']')
5310 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005311 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005312 emsg(_(e_missbrac));
5313 goto theend;
5314 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005315 if (type == &t_any)
5316 {
5317 type_T *idx_type = ((type_T **)stack->ga_data)[
5318 stack->ga_len - 1];
5319 // Index on variable of unknown type: guess the type from the
5320 // index type: number is dict, otherwise dict.
5321 // TODO: should do the assignment at runtime
5322 if (idx_type->tt_type == VAR_NUMBER)
5323 type = &t_list_any;
5324 else
5325 type = &t_dict_any;
5326 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005327 if (type->tt_type == VAR_DICT
5328 && may_generate_2STRING(-1, cctx) == FAIL)
5329 goto theend;
5330 if (type->tt_type == VAR_LIST
5331 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005332 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005333 {
5334 emsg(_(e_number_exp));
5335 goto theend;
5336 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005337
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005338 // Load the dict or list. On the stack we then have:
5339 // - value
5340 // - index
5341 // - variable
5342 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005343
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005344 if (type->tt_type == VAR_LIST)
5345 {
5346 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5347 return FAIL;
5348 }
5349 else if (type->tt_type == VAR_DICT)
5350 {
5351 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5352 return FAIL;
5353 }
5354 else
5355 {
5356 emsg(_(e_listreq));
5357 goto theend;
5358 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005359 }
5360 else
5361 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005362 if (is_decl && cmdidx == CMD_const
5363 && (dest == dest_script || dest == dest_local))
5364 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005365 generate_LOCKCONST(cctx);
5366
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005367 switch (dest)
5368 {
5369 case dest_option:
Bram Moolenaar2e800952020-08-23 19:34:48 +02005370 generate_STOREOPT(cctx, skip_option_env_lead(name),
5371 opt_flags);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005372 break;
5373 case dest_global:
5374 // include g: with the name, easier to execute that way
5375 generate_STORE(cctx, ISN_STOREG, 0, name);
5376 break;
5377 case dest_buffer:
5378 // include b: with the name, easier to execute that way
5379 generate_STORE(cctx, ISN_STOREB, 0, name);
5380 break;
5381 case dest_window:
5382 // include w: with the name, easier to execute that way
5383 generate_STORE(cctx, ISN_STOREW, 0, name);
5384 break;
5385 case dest_tab:
5386 // include t: with the name, easier to execute that way
5387 generate_STORE(cctx, ISN_STORET, 0, name);
5388 break;
5389 case dest_env:
5390 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5391 break;
5392 case dest_reg:
5393 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5394 break;
5395 case dest_vimvar:
5396 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5397 break;
5398 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005399 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005400 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005401 {
5402 char_u *name_s = name;
5403
5404 // Include s: in the name for store_var()
5405 if (name[1] != ':')
5406 {
5407 int len = (int)STRLEN(name) + 3;
5408
5409 name_s = alloc(len);
5410 if (name_s == NULL)
5411 name_s = name;
5412 else
5413 vim_snprintf((char *)name_s, len,
5414 "s:%s", name);
5415 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005416 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5417 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005418 if (name_s != name)
5419 vim_free(name_s);
5420 }
5421 else
5422 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005423 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005424 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005425 break;
5426 case dest_local:
5427 if (lvar != NULL)
5428 {
5429 isn_T *isn = ((isn_T *)instr->ga_data)
5430 + instr->ga_len - 1;
5431
5432 // optimization: turn "var = 123" from ISN_PUSHNR +
5433 // ISN_STORE into ISN_STORENR
5434 if (!lvar->lv_from_outer
5435 && instr->ga_len == instr_count + 1
5436 && isn->isn_type == ISN_PUSHNR)
5437 {
5438 varnumber_T val = isn->isn_arg.number;
5439
5440 isn->isn_type = ISN_STORENR;
5441 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5442 isn->isn_arg.storenr.stnr_val = val;
5443 if (stack->ga_len > 0)
5444 --stack->ga_len;
5445 }
5446 else if (lvar->lv_from_outer)
5447 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005448 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005449 else
5450 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5451 }
5452 break;
5453 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005454 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005455
5456 if (var_idx + 1 < var_count)
5457 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005458 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005459
5460 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005461 if (var_count > 0 && !semicolon)
5462 {
5463 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5464 goto theend;
5465 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005466
Bram Moolenaarb2097502020-07-19 17:17:02 +02005467 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005468
5469theend:
5470 vim_free(name);
5471 return ret;
5472}
5473
5474/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005475 * Check if "name" can be "unlet".
5476 */
5477 int
5478check_vim9_unlet(char_u *name)
5479{
5480 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5481 {
Bram Moolenaar84367732020-08-23 15:21:55 +02005482 // "unlet s:var" is allowed in legacy script.
5483 if (*name == 's' && !script_is_vim9())
5484 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005485 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005486 return FAIL;
5487 }
5488 return OK;
5489}
5490
5491/*
5492 * Callback passed to ex_unletlock().
5493 */
5494 static int
5495compile_unlet(
5496 lval_T *lvp,
5497 char_u *name_end,
5498 exarg_T *eap,
5499 int deep UNUSED,
5500 void *coookie)
5501{
5502 cctx_T *cctx = coookie;
5503
5504 if (lvp->ll_tv == NULL)
5505 {
5506 char_u *p = lvp->ll_name;
5507 int cc = *name_end;
5508 int ret = OK;
5509
5510 // Normal name. Only supports g:, w:, t: and b: namespaces.
5511 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005512 if (*p == '$')
5513 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5514 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005515 ret = FAIL;
5516 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005517 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005518
5519 *name_end = cc;
5520 return ret;
5521 }
5522
5523 // TODO: unlet {list}[idx]
5524 // TODO: unlet {dict}[key]
5525 emsg("Sorry, :unlet not fully implemented yet");
5526 return FAIL;
5527}
5528
5529/*
5530 * compile "unlet var", "lock var" and "unlock var"
5531 * "arg" points to "var".
5532 */
5533 static char_u *
5534compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5535{
5536 char_u *p = arg;
5537
5538 if (eap->cmdidx != CMD_unlet)
5539 {
5540 emsg("Sorry, :lock and unlock not implemented yet");
5541 return NULL;
5542 }
5543
5544 if (*p == '!')
5545 {
5546 p = skipwhite(p + 1);
5547 eap->forceit = TRUE;
5548 }
5549
5550 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5551 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5552}
5553
5554/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005555 * Compile an :import command.
5556 */
5557 static char_u *
5558compile_import(char_u *arg, cctx_T *cctx)
5559{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005560 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005561}
5562
5563/*
5564 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5565 */
5566 static int
5567compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5568{
5569 garray_T *instr = &cctx->ctx_instr;
5570 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5571
5572 if (endlabel == NULL)
5573 return FAIL;
5574 endlabel->el_next = *el;
5575 *el = endlabel;
5576 endlabel->el_end_label = instr->ga_len;
5577
5578 generate_JUMP(cctx, when, 0);
5579 return OK;
5580}
5581
5582 static void
5583compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5584{
5585 garray_T *instr = &cctx->ctx_instr;
5586
5587 while (*el != NULL)
5588 {
5589 endlabel_T *cur = (*el);
5590 isn_T *isn;
5591
5592 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5593 isn->isn_arg.jump.jump_where = instr->ga_len;
5594 *el = cur->el_next;
5595 vim_free(cur);
5596 }
5597}
5598
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005599 static void
5600compile_free_jump_to_end(endlabel_T **el)
5601{
5602 while (*el != NULL)
5603 {
5604 endlabel_T *cur = (*el);
5605
5606 *el = cur->el_next;
5607 vim_free(cur);
5608 }
5609}
5610
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005611/*
5612 * Create a new scope and set up the generic items.
5613 */
5614 static scope_T *
5615new_scope(cctx_T *cctx, scopetype_T type)
5616{
5617 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5618
5619 if (scope == NULL)
5620 return NULL;
5621 scope->se_outer = cctx->ctx_scope;
5622 cctx->ctx_scope = scope;
5623 scope->se_type = type;
5624 scope->se_local_count = cctx->ctx_locals.ga_len;
5625 return scope;
5626}
5627
5628/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005629 * Free the current scope and go back to the outer scope.
5630 */
5631 static void
5632drop_scope(cctx_T *cctx)
5633{
5634 scope_T *scope = cctx->ctx_scope;
5635
5636 if (scope == NULL)
5637 {
5638 iemsg("calling drop_scope() without a scope");
5639 return;
5640 }
5641 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005642 switch (scope->se_type)
5643 {
5644 case IF_SCOPE:
5645 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5646 case FOR_SCOPE:
5647 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5648 case WHILE_SCOPE:
5649 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5650 case TRY_SCOPE:
5651 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5652 case NO_SCOPE:
5653 case BLOCK_SCOPE:
5654 break;
5655 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005656 vim_free(scope);
5657}
5658
5659/*
Bram Moolenaar13106602020-10-04 16:06:05 +02005660 * Check that the top of the type stack has a type that can be used as a
5661 * condition. Give an error and return FAIL if not.
5662 */
5663 static int
5664bool_on_stack(cctx_T *cctx)
5665{
5666 garray_T *stack = &cctx->ctx_type_stack;
5667 type_T *type;
5668
5669 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
5670 if (type != &t_bool && type != &t_number && type != &t_any
5671 && need_type(type, &t_bool, -1, cctx, FALSE) == FAIL)
5672 return FAIL;
5673 return OK;
5674}
5675
5676/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005677 * compile "if expr"
5678 *
5679 * "if expr" Produces instructions:
5680 * EVAL expr Push result of "expr"
5681 * JUMP_IF_FALSE end
5682 * ... body ...
5683 * end:
5684 *
5685 * "if expr | else" Produces instructions:
5686 * EVAL expr Push result of "expr"
5687 * JUMP_IF_FALSE else
5688 * ... body ...
5689 * JUMP_ALWAYS end
5690 * else:
5691 * ... body ...
5692 * end:
5693 *
5694 * "if expr1 | elseif expr2 | else" Produces instructions:
5695 * EVAL expr Push result of "expr"
5696 * JUMP_IF_FALSE elseif
5697 * ... body ...
5698 * JUMP_ALWAYS end
5699 * elseif:
5700 * EVAL expr Push result of "expr"
5701 * JUMP_IF_FALSE else
5702 * ... body ...
5703 * JUMP_ALWAYS end
5704 * else:
5705 * ... body ...
5706 * end:
5707 */
5708 static char_u *
5709compile_if(char_u *arg, cctx_T *cctx)
5710{
5711 char_u *p = arg;
5712 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005713 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005714 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005715 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005716 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005717
Bram Moolenaara5565e42020-05-09 15:44:01 +02005718 CLEAR_FIELD(ppconst);
5719 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005720 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005721 clear_ppconst(&ppconst);
5722 return NULL;
5723 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005724 if (cctx->ctx_skip == SKIP_YES)
5725 clear_ppconst(&ppconst);
5726 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005727 {
Bram Moolenaar13106602020-10-04 16:06:05 +02005728 int error = FALSE;
5729 int v;
5730
Bram Moolenaara5565e42020-05-09 15:44:01 +02005731 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02005732 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02005733 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02005734 if (error)
5735 return NULL;
5736 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005737 }
5738 else
5739 {
5740 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005741 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005742 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005743 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02005744 if (bool_on_stack(cctx) == FAIL)
5745 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005746 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005747
5748 scope = new_scope(cctx, IF_SCOPE);
5749 if (scope == NULL)
5750 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005751 scope->se_skip_save = skip_save;
5752 // "is_had_return" will be reset if any block does not end in :return
5753 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005754
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005755 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005756 {
5757 // "where" is set when ":elseif", "else" or ":endif" is found
5758 scope->se_u.se_if.is_if_label = instr->ga_len;
5759 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5760 }
5761 else
5762 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005763
5764 return p;
5765}
5766
5767 static char_u *
5768compile_elseif(char_u *arg, cctx_T *cctx)
5769{
5770 char_u *p = arg;
5771 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005772 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005773 isn_T *isn;
5774 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005775 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02005776 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005777
5778 if (scope == NULL || scope->se_type != IF_SCOPE)
5779 {
5780 emsg(_(e_elseif_without_if));
5781 return NULL;
5782 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005783 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005784 if (!cctx->ctx_had_return)
5785 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005786
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005787 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005788 {
5789 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005790 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005791 return NULL;
5792 // previous "if" or "elseif" jumps here
5793 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5794 isn->isn_arg.jump.jump_where = instr->ga_len;
5795 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005796
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005797 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005798 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02005799 if (cctx->ctx_skip == SKIP_YES)
5800 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005801 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005802 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005803 clear_ppconst(&ppconst);
5804 return NULL;
5805 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02005806 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005807 if (scope->se_skip_save == SKIP_YES)
5808 clear_ppconst(&ppconst);
5809 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005810 {
Bram Moolenaar13106602020-10-04 16:06:05 +02005811 int error = FALSE;
5812 int v;
5813
Bram Moolenaar7f141552020-05-09 17:35:53 +02005814 // The expression results in a constant.
5815 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02005816 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
5817 if (error)
5818 return NULL;
5819 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005820 clear_ppconst(&ppconst);
5821 scope->se_u.se_if.is_if_label = -1;
5822 }
5823 else
5824 {
5825 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005826 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005827 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005828 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02005829 if (bool_on_stack(cctx) == FAIL)
5830 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005831
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005832 // "where" is set when ":elseif", "else" or ":endif" is found
5833 scope->se_u.se_if.is_if_label = instr->ga_len;
5834 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5835 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005836
5837 return p;
5838}
5839
5840 static char_u *
5841compile_else(char_u *arg, cctx_T *cctx)
5842{
5843 char_u *p = arg;
5844 garray_T *instr = &cctx->ctx_instr;
5845 isn_T *isn;
5846 scope_T *scope = cctx->ctx_scope;
5847
5848 if (scope == NULL || scope->se_type != IF_SCOPE)
5849 {
5850 emsg(_(e_else_without_if));
5851 return NULL;
5852 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005853 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005854 if (!cctx->ctx_had_return)
5855 scope->se_u.se_if.is_had_return = FALSE;
5856 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005857
Bram Moolenaarefd88552020-06-18 20:50:10 +02005858 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005859 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005860 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005861 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005862 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005863 if (!cctx->ctx_had_return
5864 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5865 JUMP_ALWAYS, cctx) == FAIL)
5866 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005867 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005868
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005869 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005870 {
5871 if (scope->se_u.se_if.is_if_label >= 0)
5872 {
5873 // previous "if" or "elseif" jumps here
5874 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5875 isn->isn_arg.jump.jump_where = instr->ga_len;
5876 scope->se_u.se_if.is_if_label = -1;
5877 }
5878 }
5879
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005880 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005881 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5882 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005883
5884 return p;
5885}
5886
5887 static char_u *
5888compile_endif(char_u *arg, cctx_T *cctx)
5889{
5890 scope_T *scope = cctx->ctx_scope;
5891 ifscope_T *ifscope;
5892 garray_T *instr = &cctx->ctx_instr;
5893 isn_T *isn;
5894
5895 if (scope == NULL || scope->se_type != IF_SCOPE)
5896 {
5897 emsg(_(e_endif_without_if));
5898 return NULL;
5899 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005900 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005901 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005902 if (!cctx->ctx_had_return)
5903 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005904
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005905 if (scope->se_u.se_if.is_if_label >= 0)
5906 {
5907 // previous "if" or "elseif" jumps here
5908 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5909 isn->isn_arg.jump.jump_where = instr->ga_len;
5910 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005911 // Fill in the "end" label in jumps at the end of the blocks.
5912 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005913 cctx->ctx_skip = scope->se_skip_save;
5914
5915 // If all the blocks end in :return and there is an :else then the
5916 // had_return flag is set.
5917 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005918
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005919 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005920 return arg;
5921}
5922
5923/*
5924 * compile "for var in expr"
5925 *
5926 * Produces instructions:
5927 * PUSHNR -1
5928 * STORE loop-idx Set index to -1
5929 * EVAL expr Push result of "expr"
5930 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5931 * - if beyond end, jump to "end"
5932 * - otherwise get item from list and push it
5933 * STORE var Store item in "var"
5934 * ... body ...
5935 * JUMP top Jump back to repeat
5936 * end: DROP Drop the result of "expr"
5937 *
5938 */
5939 static char_u *
5940compile_for(char_u *arg, cctx_T *cctx)
5941{
5942 char_u *p;
5943 size_t varlen;
5944 garray_T *instr = &cctx->ctx_instr;
5945 garray_T *stack = &cctx->ctx_type_stack;
5946 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005947 lvar_T *loop_lvar; // loop iteration variable
5948 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005949 type_T *vartype;
5950
5951 // TODO: list of variables: "for [key, value] in dict"
5952 // parse "var"
5953 for (p = arg; eval_isnamec1(*p); ++p)
5954 ;
5955 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005956 var_lvar = lookup_local(arg, varlen, cctx);
5957 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005958 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005959 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005960 return NULL;
5961 }
5962
5963 // consume "in"
5964 p = skipwhite(p);
5965 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5966 {
5967 emsg(_(e_missing_in));
5968 return NULL;
5969 }
5970 p = skipwhite(p + 2);
5971
5972
5973 scope = new_scope(cctx, FOR_SCOPE);
5974 if (scope == NULL)
5975 return NULL;
5976
5977 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005978 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5979 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005980 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005981 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005982 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005983 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005984 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005985
5986 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005987 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5988 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005989 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005990 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005991 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005992 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005993 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005994
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005995 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005996
5997 // compile "expr", it remains on the stack until "endfor"
5998 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005999 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006000 {
6001 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006002 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006003 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006004
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006005 // Now that we know the type of "var", check that it is a list, now or at
6006 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006007 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02006008 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006009 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006010 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006011 return NULL;
6012 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006013 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006014 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006015
6016 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006017 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006018
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006019 generate_FOR(cctx, loop_lvar->lv_idx);
6020 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006021
6022 return arg;
6023}
6024
6025/*
6026 * compile "endfor"
6027 */
6028 static char_u *
6029compile_endfor(char_u *arg, cctx_T *cctx)
6030{
6031 garray_T *instr = &cctx->ctx_instr;
6032 scope_T *scope = cctx->ctx_scope;
6033 forscope_T *forscope;
6034 isn_T *isn;
6035
6036 if (scope == NULL || scope->se_type != FOR_SCOPE)
6037 {
6038 emsg(_(e_for));
6039 return NULL;
6040 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006041 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006042 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006043 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006044
6045 // At end of ":for" scope jump back to the FOR instruction.
6046 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6047
6048 // Fill in the "end" label in the FOR statement so it can jump here
6049 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6050 isn->isn_arg.forloop.for_end = instr->ga_len;
6051
6052 // Fill in the "end" label any BREAK statements
6053 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6054
6055 // Below the ":for" scope drop the "expr" list from the stack.
6056 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6057 return NULL;
6058
6059 vim_free(scope);
6060
6061 return arg;
6062}
6063
6064/*
6065 * compile "while expr"
6066 *
6067 * Produces instructions:
6068 * top: EVAL expr Push result of "expr"
6069 * JUMP_IF_FALSE end jump if false
6070 * ... body ...
6071 * JUMP top Jump back to repeat
6072 * end:
6073 *
6074 */
6075 static char_u *
6076compile_while(char_u *arg, cctx_T *cctx)
6077{
6078 char_u *p = arg;
6079 garray_T *instr = &cctx->ctx_instr;
6080 scope_T *scope;
6081
6082 scope = new_scope(cctx, WHILE_SCOPE);
6083 if (scope == NULL)
6084 return NULL;
6085
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006086 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006087
6088 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006089 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006090 return NULL;
6091
Bram Moolenaar13106602020-10-04 16:06:05 +02006092 if (bool_on_stack(cctx) == FAIL)
6093 return FAIL;
6094
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006095 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006096 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006097 JUMP_IF_FALSE, cctx) == FAIL)
6098 return FAIL;
6099
6100 return p;
6101}
6102
6103/*
6104 * compile "endwhile"
6105 */
6106 static char_u *
6107compile_endwhile(char_u *arg, cctx_T *cctx)
6108{
6109 scope_T *scope = cctx->ctx_scope;
6110
6111 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6112 {
6113 emsg(_(e_while));
6114 return NULL;
6115 }
6116 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006117 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006118
6119 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006120 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006121
6122 // Fill in the "end" label in the WHILE statement so it can jump here.
6123 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006124 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006125
6126 vim_free(scope);
6127
6128 return arg;
6129}
6130
6131/*
6132 * compile "continue"
6133 */
6134 static char_u *
6135compile_continue(char_u *arg, cctx_T *cctx)
6136{
6137 scope_T *scope = cctx->ctx_scope;
6138
6139 for (;;)
6140 {
6141 if (scope == NULL)
6142 {
6143 emsg(_(e_continue));
6144 return NULL;
6145 }
6146 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6147 break;
6148 scope = scope->se_outer;
6149 }
6150
6151 // Jump back to the FOR or WHILE instruction.
6152 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006153 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6154 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006155 return arg;
6156}
6157
6158/*
6159 * compile "break"
6160 */
6161 static char_u *
6162compile_break(char_u *arg, cctx_T *cctx)
6163{
6164 scope_T *scope = cctx->ctx_scope;
6165 endlabel_T **el;
6166
6167 for (;;)
6168 {
6169 if (scope == NULL)
6170 {
6171 emsg(_(e_break));
6172 return NULL;
6173 }
6174 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6175 break;
6176 scope = scope->se_outer;
6177 }
6178
6179 // Jump to the end of the FOR or WHILE loop.
6180 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006181 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006182 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006183 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006184 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6185 return FAIL;
6186
6187 return arg;
6188}
6189
6190/*
6191 * compile "{" start of block
6192 */
6193 static char_u *
6194compile_block(char_u *arg, cctx_T *cctx)
6195{
6196 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6197 return NULL;
6198 return skipwhite(arg + 1);
6199}
6200
6201/*
6202 * compile end of block: drop one scope
6203 */
6204 static void
6205compile_endblock(cctx_T *cctx)
6206{
6207 scope_T *scope = cctx->ctx_scope;
6208
6209 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006210 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006211 vim_free(scope);
6212}
6213
6214/*
6215 * compile "try"
6216 * Creates a new scope for the try-endtry, pointing to the first catch and
6217 * finally.
6218 * Creates another scope for the "try" block itself.
6219 * TRY instruction sets up exception handling at runtime.
6220 *
6221 * "try"
6222 * TRY -> catch1, -> finally push trystack entry
6223 * ... try block
6224 * "throw {exception}"
6225 * EVAL {exception}
6226 * THROW create exception
6227 * ... try block
6228 * " catch {expr}"
6229 * JUMP -> finally
6230 * catch1: PUSH exeception
6231 * EVAL {expr}
6232 * MATCH
6233 * JUMP nomatch -> catch2
6234 * CATCH remove exception
6235 * ... catch block
6236 * " catch"
6237 * JUMP -> finally
6238 * catch2: CATCH remove exception
6239 * ... catch block
6240 * " finally"
6241 * finally:
6242 * ... finally block
6243 * " endtry"
6244 * ENDTRY pop trystack entry, may rethrow
6245 */
6246 static char_u *
6247compile_try(char_u *arg, cctx_T *cctx)
6248{
6249 garray_T *instr = &cctx->ctx_instr;
6250 scope_T *try_scope;
6251 scope_T *scope;
6252
6253 // scope that holds the jumps that go to catch/finally/endtry
6254 try_scope = new_scope(cctx, TRY_SCOPE);
6255 if (try_scope == NULL)
6256 return NULL;
6257
6258 // "catch" is set when the first ":catch" is found.
6259 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006260 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006261 if (generate_instr(cctx, ISN_TRY) == NULL)
6262 return NULL;
6263
6264 // scope for the try block itself
6265 scope = new_scope(cctx, BLOCK_SCOPE);
6266 if (scope == NULL)
6267 return NULL;
6268
6269 return arg;
6270}
6271
6272/*
6273 * compile "catch {expr}"
6274 */
6275 static char_u *
6276compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6277{
6278 scope_T *scope = cctx->ctx_scope;
6279 garray_T *instr = &cctx->ctx_instr;
6280 char_u *p;
6281 isn_T *isn;
6282
6283 // end block scope from :try or :catch
6284 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6285 compile_endblock(cctx);
6286 scope = cctx->ctx_scope;
6287
6288 // Error if not in a :try scope
6289 if (scope == NULL || scope->se_type != TRY_SCOPE)
6290 {
6291 emsg(_(e_catch));
6292 return NULL;
6293 }
6294
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006295 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006296 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006297 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006298 return NULL;
6299 }
6300
6301 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006302 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006303 JUMP_ALWAYS, cctx) == FAIL)
6304 return NULL;
6305
6306 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006307 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006308 if (isn->isn_arg.try.try_catch == 0)
6309 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006310 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006311 {
6312 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006313 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006314 isn->isn_arg.jump.jump_where = instr->ga_len;
6315 }
6316
6317 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006318 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006319 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006320 scope->se_u.se_try.ts_caught_all = TRUE;
6321 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006322 }
6323 else
6324 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006325 char_u *end;
6326 char_u *pat;
6327 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006328 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006329 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006330
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006331 // Push v:exception, push {expr} and MATCH
6332 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6333
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006334 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006335 if (*end != *p)
6336 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006337 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006338 vim_free(tofree);
6339 return FAIL;
6340 }
6341 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006342 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006343 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006344 len = (int)(end - tofree);
6345 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006346 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006347 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006348 if (pat == NULL)
6349 return FAIL;
6350 if (generate_PUSHS(cctx, pat) == FAIL)
6351 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006352
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006353 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6354 return NULL;
6355
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006356 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006357 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6358 return NULL;
6359 }
6360
6361 if (generate_instr(cctx, ISN_CATCH) == NULL)
6362 return NULL;
6363
6364 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6365 return NULL;
6366 return p;
6367}
6368
6369 static char_u *
6370compile_finally(char_u *arg, cctx_T *cctx)
6371{
6372 scope_T *scope = cctx->ctx_scope;
6373 garray_T *instr = &cctx->ctx_instr;
6374 isn_T *isn;
6375
6376 // end block scope from :try or :catch
6377 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6378 compile_endblock(cctx);
6379 scope = cctx->ctx_scope;
6380
6381 // Error if not in a :try scope
6382 if (scope == NULL || scope->se_type != TRY_SCOPE)
6383 {
6384 emsg(_(e_finally));
6385 return NULL;
6386 }
6387
6388 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006389 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006390 if (isn->isn_arg.try.try_finally != 0)
6391 {
6392 emsg(_(e_finally_dup));
6393 return NULL;
6394 }
6395
6396 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006397 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006398
Bram Moolenaar585fea72020-04-02 22:33:21 +02006399 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006400 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006401 {
6402 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006403 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006404 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006405 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006406 }
6407
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006408 // TODO: set index in ts_finally_label jumps
6409
6410 return arg;
6411}
6412
6413 static char_u *
6414compile_endtry(char_u *arg, cctx_T *cctx)
6415{
6416 scope_T *scope = cctx->ctx_scope;
6417 garray_T *instr = &cctx->ctx_instr;
6418 isn_T *isn;
6419
6420 // end block scope from :catch or :finally
6421 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6422 compile_endblock(cctx);
6423 scope = cctx->ctx_scope;
6424
6425 // Error if not in a :try scope
6426 if (scope == NULL || scope->se_type != TRY_SCOPE)
6427 {
6428 if (scope == NULL)
6429 emsg(_(e_no_endtry));
6430 else if (scope->se_type == WHILE_SCOPE)
6431 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006432 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006433 emsg(_(e_endfor));
6434 else
6435 emsg(_(e_endif));
6436 return NULL;
6437 }
6438
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006439 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006440 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6441 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006442 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006443 return NULL;
6444 }
6445
6446 // Fill in the "end" label in jumps at the end of the blocks, if not done
6447 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006448 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006449
6450 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006451 if (isn->isn_arg.try.try_catch == 0)
6452 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006453 if (isn->isn_arg.try.try_finally == 0)
6454 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006455
6456 if (scope->se_u.se_try.ts_catch_label != 0)
6457 {
6458 // Last catch without match jumps here
6459 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6460 isn->isn_arg.jump.jump_where = instr->ga_len;
6461 }
6462
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006463 compile_endblock(cctx);
6464
6465 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6466 return NULL;
6467 return arg;
6468}
6469
6470/*
6471 * compile "throw {expr}"
6472 */
6473 static char_u *
6474compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6475{
6476 char_u *p = skipwhite(arg);
6477
Bram Moolenaara5565e42020-05-09 15:44:01 +02006478 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006479 return NULL;
6480 if (may_generate_2STRING(-1, cctx) == FAIL)
6481 return NULL;
6482 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6483 return NULL;
6484
6485 return p;
6486}
6487
6488/*
6489 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006490 * compile "echomsg expr"
6491 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006492 * compile "execute expr"
6493 */
6494 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006495compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006496{
6497 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006498 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006499 int count = 0;
6500
6501 for (;;)
6502 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006503 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006504 return NULL;
6505 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006506 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006507 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006508 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006509 break;
6510 }
6511
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006512 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6513 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6514 else if (cmdidx == CMD_execute)
6515 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6516 else if (cmdidx == CMD_echomsg)
6517 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6518 else
6519 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006520 return p;
6521}
6522
6523/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006524 * :put r
6525 * :put ={expr}
6526 */
6527 static char_u *
6528compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
6529{
6530 char_u *line = arg;
6531 linenr_T lnum;
6532 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006533 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006534
Bram Moolenaarc3516f72020-09-08 22:45:35 +02006535 eap->regname = *line;
6536
6537 if (eap->regname == '=')
6538 {
6539 char_u *p = line + 1;
6540
6541 if (compile_expr0(&p, cctx) == FAIL)
6542 return NULL;
6543 line = p;
6544 }
6545 else if (eap->regname != NUL)
6546 ++line;
6547
6548 // TODO: if the range is something like "$" need to evaluate at runtime
6549 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
6550 return NULL;
6551 if (eap->addr_count == 0)
6552 lnum = -1;
6553 else
6554 lnum = eap->line2;
6555 if (above)
6556 --lnum;
6557
6558 generate_PUT(cctx, eap->regname, lnum);
6559 return line;
6560}
6561
6562/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006563 * A command that is not compiled, execute with legacy code.
6564 */
6565 static char_u *
6566compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6567{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006568 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006569 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006570 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006571
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006572 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006573 goto theend;
6574
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006575 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006576 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006577 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006578 int usefilter = FALSE;
6579
6580 has_expr = argt & (EX_XFILE | EX_EXPAND);
6581
6582 // If the command can be followed by a bar, find the bar and truncate
6583 // it, so that the following command can be compiled.
6584 // The '|' is overwritten with a NUL, it is put back below.
6585 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6586 && *eap->arg == '!')
6587 // :w !filter or :r !filter or :r! filter
6588 usefilter = TRUE;
6589 if ((argt & EX_TRLBAR) && !usefilter)
6590 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006591 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006592 separate_nextcmd(eap);
6593 if (eap->nextcmd != NULL)
6594 nextcmd = eap->nextcmd;
6595 }
6596 }
6597
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006598 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6599 {
6600 // expand filename in "syntax include [@group] filename"
6601 has_expr = TRUE;
6602 eap->arg = skipwhite(eap->arg + 7);
6603 if (*eap->arg == '@')
6604 eap->arg = skiptowhite(eap->arg);
6605 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006606
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006607 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006608 {
6609 int count = 0;
6610 char_u *start = skipwhite(line);
6611
6612 // :cmd xxx`=expr1`yyy`=expr2`zzz
6613 // PUSHS ":cmd xxx"
6614 // eval expr1
6615 // PUSHS "yyy"
6616 // eval expr2
6617 // PUSHS "zzz"
6618 // EXECCONCAT 5
6619 for (;;)
6620 {
6621 if (p > start)
6622 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006623 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006624 ++count;
6625 }
6626 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006627 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006628 return NULL;
6629 may_generate_2STRING(-1, cctx);
6630 ++count;
6631 p = skipwhite(p);
6632 if (*p != '`')
6633 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006634 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006635 return NULL;
6636 }
6637 start = p + 1;
6638
6639 p = (char_u *)strstr((char *)start, "`=");
6640 if (p == NULL)
6641 {
6642 if (*skipwhite(start) != NUL)
6643 {
6644 generate_PUSHS(cctx, vim_strsave(start));
6645 ++count;
6646 }
6647 break;
6648 }
6649 }
6650 generate_EXECCONCAT(cctx, count);
6651 }
6652 else
6653 generate_EXEC(cctx, line);
6654
6655theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006656 if (*nextcmd != NUL)
6657 {
6658 // the parser expects a pointer to the bar, put it back
6659 --nextcmd;
6660 *nextcmd = '|';
6661 }
6662
6663 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006664}
6665
6666/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006667 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006668 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006669 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006670 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006671add_def_function(ufunc_T *ufunc)
6672{
6673 dfunc_T *dfunc;
6674
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006675 if (def_functions.ga_len == 0)
6676 {
6677 // The first position is not used, so that a zero uf_dfunc_idx means it
6678 // wasn't set.
6679 if (ga_grow(&def_functions, 1) == FAIL)
6680 return FAIL;
6681 ++def_functions.ga_len;
6682 }
6683
Bram Moolenaar09689a02020-05-09 22:50:08 +02006684 // Add the function to "def_functions".
6685 if (ga_grow(&def_functions, 1) == FAIL)
6686 return FAIL;
6687 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6688 CLEAR_POINTER(dfunc);
6689 dfunc->df_idx = def_functions.ga_len;
6690 ufunc->uf_dfunc_idx = dfunc->df_idx;
6691 dfunc->df_ufunc = ufunc;
6692 ++def_functions.ga_len;
6693 return OK;
6694}
6695
6696/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006697 * After ex_function() has collected all the function lines: parse and compile
6698 * the lines into instructions.
6699 * Adds the function to "def_functions".
6700 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6701 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006702 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006703 * This can be used recursively through compile_lambda(), which may reallocate
6704 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006705 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006706 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006707 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006708compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006709{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006710 char_u *line = NULL;
6711 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006712 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006713 cctx_T cctx;
6714 garray_T *instr;
6715 int called_emsg_before = called_emsg;
6716 int ret = FAIL;
6717 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02006718 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006719 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006720 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006721 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006722
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006723 // When using a function that was compiled before: Free old instructions.
6724 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006725 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006726 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006727 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6728 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006729 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006730 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006731 else
6732 {
6733 if (add_def_function(ufunc) == FAIL)
6734 return FAIL;
6735 new_def_function = TRUE;
6736 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006737
Bram Moolenaar985116a2020-07-12 17:31:09 +02006738 ufunc->uf_def_status = UF_COMPILING;
6739
Bram Moolenaara80faa82020-04-12 19:37:17 +02006740 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006741 cctx.ctx_ufunc = ufunc;
6742 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006743 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006744 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6745 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6746 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6747 cctx.ctx_type_list = &ufunc->uf_type_list;
6748 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6749 instr = &cctx.ctx_instr;
6750
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006751 // Set the context to the function, it may be compiled when called from
6752 // another script. Set the script version to the most modern one.
6753 // The line number will be set in next_line_from_context().
6754 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006755 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6756
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006757 // Make sure error messages are OK.
6758 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6759 if (do_estack_push)
6760 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02006761 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006762
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006763 if (ufunc->uf_def_args.ga_len > 0)
6764 {
6765 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006766 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006767 int i;
6768 char_u *arg;
6769 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006770 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006771
6772 // Produce instructions for the default values of optional arguments.
6773 // Store the instruction index in uf_def_arg_idx[] so that we know
6774 // where to start when the function is called, depending on the number
6775 // of arguments.
6776 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6777 if (ufunc->uf_def_arg_idx == NULL)
6778 goto erret;
6779 for (i = 0; i < count; ++i)
6780 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006781 garray_T *stack = &cctx.ctx_type_stack;
6782 type_T *val_type;
6783 int arg_idx = first_def_arg + i;
6784
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006785 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6786 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006787 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006788 goto erret;
6789
6790 // If no type specified use the type of the default value.
6791 // Otherwise check that the default value type matches the
6792 // specified type.
6793 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6794 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006795 {
6796 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006797 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006798 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02006799 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
6800 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006801 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006802
6803 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006804 goto erret;
6805 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006806 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006807
6808 if (did_set_arg_type)
6809 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006810 }
6811
6812 /*
6813 * Loop over all the lines of the function and generate instructions.
6814 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006815 for (;;)
6816 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006817 exarg_T ea;
6818 cmdmod_T save_cmdmod;
6819 int starts_with_colon = FALSE;
6820 char_u *cmd;
6821 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006822
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006823 // Bail out on the first error to avoid a flood of errors and report
6824 // the right line number when inside try/catch.
6825 if (emsg_before != called_emsg)
6826 goto erret;
6827
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006828 if (line != NULL && *line == '|')
6829 // the line continues after a '|'
6830 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006831 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006832 && !(*line == '#' && (line == cctx.ctx_line_start
6833 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006834 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006835 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006836 goto erret;
6837 }
6838 else
6839 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006840 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006841 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006842 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006843 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006844 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006845 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006846
Bram Moolenaara80faa82020-04-12 19:37:17 +02006847 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006848 ea.cmdlinep = &line;
6849 ea.cmd = skipwhite(line);
6850
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006851 // Some things can be recognized by the first character.
6852 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006853 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006854 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006855 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006856 if (ea.cmd[1] != '{')
6857 {
6858 line = (char_u *)"";
6859 continue;
6860 }
6861 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006862
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006863 case '}':
6864 {
6865 // "}" ends a block scope
6866 scopetype_T stype = cctx.ctx_scope == NULL
6867 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006868
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006869 if (stype == BLOCK_SCOPE)
6870 {
6871 compile_endblock(&cctx);
6872 line = ea.cmd;
6873 }
6874 else
6875 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006876 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006877 goto erret;
6878 }
6879 if (line != NULL)
6880 line = skipwhite(ea.cmd + 1);
6881 continue;
6882 }
6883
6884 case '{':
6885 // "{" starts a block scope
6886 // "{'a': 1}->func() is something else
6887 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6888 {
6889 line = compile_block(ea.cmd, &cctx);
6890 continue;
6891 }
6892 break;
6893
6894 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006895 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006896 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006897 }
6898
6899 /*
6900 * COMMAND MODIFIERS
6901 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006902 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006903 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6904 {
6905 if (errormsg != NULL)
6906 goto erret;
6907 // empty line or comment
6908 line = (char_u *)"";
6909 continue;
6910 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006911 // TODO: use modifiers in the command
6912 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006913 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006914
6915 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006916 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006917 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006918 {
6919 if (*ea.cmd == '(')
6920 // not for "call()"
6921 ea.cmd = p;
6922 else
6923 ea.cmd = skipwhite(ea.cmd);
6924 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006925
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006926 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006927 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006928 char_u *pskip;
6929
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006930 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006931 // find what follows.
6932 // Skip over "var.member", "var[idx]" and the like.
6933 // Also "&opt = val", "$ENV = val" and "@r = val".
6934 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006935 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006936 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006937 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006938 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006939 char_u *var_end;
6940 int oplen;
6941 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006942
Bram Moolenaar65821722020-08-02 18:58:54 +02006943 if (ea.cmd[0] == '@')
6944 var_end = ea.cmd + 2;
6945 else
6946 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006947 FNE_CHECK_START | FNE_INCL_BR);
6948 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006949 if (oplen > 0)
6950 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006951 size_t len = p - ea.cmd;
6952
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006953 // Recognize an assignment if we recognize the variable
6954 // name:
6955 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006956 // "local = expr" where "local" is a local var.
6957 // "script = expr" where "script" is a script-local var.
6958 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006959 // "&opt = expr"
6960 // "$ENV = expr"
6961 // "@r = expr"
6962 if (*ea.cmd == '&'
6963 || *ea.cmd == '$'
6964 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006965 || ((len) > 2 && ea.cmd[1] == ':')
6966 || lookup_local(ea.cmd, len, &cctx) != NULL
6967 || lookup_arg(ea.cmd, len, NULL, NULL,
6968 NULL, &cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02006969 || lookup_script(ea.cmd, len, FALSE) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006970 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006971 {
6972 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006973 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006974 goto erret;
6975 continue;
6976 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006977 }
6978 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006979
6980 if (*ea.cmd == '[')
6981 {
6982 // [var, var] = expr
6983 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6984 if (line == NULL)
6985 goto erret;
6986 if (line != ea.cmd)
6987 continue;
6988 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006989 }
6990
6991 /*
6992 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006993 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006994 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006995 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006996 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006997 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02006998 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006999 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007000 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007001 if (!starts_with_colon)
7002 {
7003 emsg(_(e_colon_required_before_a_range));
7004 goto erret;
7005 }
7006 if (ends_excmd2(line, ea.cmd))
7007 {
7008 // A range without a command: jump to the line.
7009 // TODO: compile to a more efficient command, possibly
7010 // calling parse_cmd_address().
7011 ea.cmdidx = CMD_SIZE;
7012 line = compile_exec(line, &ea, &cctx);
7013 goto nextline;
7014 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007015 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007016 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007017 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007018 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007019 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007020
7021 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7022 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007023 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007024 {
7025 line += STRLEN(line);
7026 continue;
7027 }
7028
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007029 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007030 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007031 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007032 // CMD_var cannot happen, compile_assignment() above is used
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007033 iemsg("Command from find_ex_command() not handled");
7034 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007035 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007036 }
7037
Bram Moolenaar3988f642020-08-27 22:43:03 +02007038 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007039 && ea.cmdidx != CMD_elseif
7040 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007041 && ea.cmdidx != CMD_endif
7042 && ea.cmdidx != CMD_endfor
7043 && ea.cmdidx != CMD_endwhile
7044 && ea.cmdidx != CMD_catch
7045 && ea.cmdidx != CMD_finally
7046 && ea.cmdidx != CMD_endtry)
7047 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007048 emsg(_(e_unreachable_code_after_return));
7049 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007050 }
7051
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007052 p = skipwhite(p);
7053 if (ea.cmdidx != CMD_SIZE
7054 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7055 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007056 if (ea.cmdidx >= 0)
7057 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007058 if ((ea.argt & EX_BANG) && *p == '!')
7059 {
7060 ea.forceit = TRUE;
7061 p = skipwhite(p + 1);
7062 }
7063 }
7064
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007065 switch (ea.cmdidx)
7066 {
7067 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007068 ea.arg = p;
7069 line = compile_nested_function(&ea, &cctx);
7070 break;
7071
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007072 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007073 // TODO: should we allow this, e.g. to declare a global
7074 // function?
7075 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007076 goto erret;
7077
7078 case CMD_return:
7079 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007080 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007081 break;
7082
7083 case CMD_let:
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02007084 if (get_vim_var_nr(VV_DISALLOW_LET))
7085 {
7086 emsg(_(e_cannot_use_let_in_vim9_script));
7087 break;
7088 }
7089 // FALLTHROUGH
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007090 case CMD_var:
7091 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007092 case CMD_const:
7093 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007094 if (line == p)
7095 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007096 break;
7097
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007098 case CMD_unlet:
7099 case CMD_unlockvar:
7100 case CMD_lockvar:
7101 line = compile_unletlock(p, &ea, &cctx);
7102 break;
7103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007104 case CMD_import:
7105 line = compile_import(p, &cctx);
7106 break;
7107
7108 case CMD_if:
7109 line = compile_if(p, &cctx);
7110 break;
7111 case CMD_elseif:
7112 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007113 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007114 break;
7115 case CMD_else:
7116 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007117 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007118 break;
7119 case CMD_endif:
7120 line = compile_endif(p, &cctx);
7121 break;
7122
7123 case CMD_while:
7124 line = compile_while(p, &cctx);
7125 break;
7126 case CMD_endwhile:
7127 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007128 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007129 break;
7130
7131 case CMD_for:
7132 line = compile_for(p, &cctx);
7133 break;
7134 case CMD_endfor:
7135 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007136 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007137 break;
7138 case CMD_continue:
7139 line = compile_continue(p, &cctx);
7140 break;
7141 case CMD_break:
7142 line = compile_break(p, &cctx);
7143 break;
7144
7145 case CMD_try:
7146 line = compile_try(p, &cctx);
7147 break;
7148 case CMD_catch:
7149 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007150 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007151 break;
7152 case CMD_finally:
7153 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007154 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007155 break;
7156 case CMD_endtry:
7157 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007158 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007159 break;
7160 case CMD_throw:
7161 line = compile_throw(p, &cctx);
7162 break;
7163
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007164 case CMD_eval:
7165 if (compile_expr0(&p, &cctx) == FAIL)
7166 goto erret;
7167
Bram Moolenaar3988f642020-08-27 22:43:03 +02007168 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007169 generate_instr_drop(&cctx, ISN_DROP, 1);
7170
7171 line = skipwhite(p);
7172 break;
7173
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007174 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007175 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007176 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007177 case CMD_echomsg:
7178 case CMD_echoerr:
7179 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007180 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007181
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007182 case CMD_put:
7183 ea.cmd = cmd;
7184 line = compile_put(p, &ea, &cctx);
7185 break;
7186
Bram Moolenaar3988f642020-08-27 22:43:03 +02007187 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007188
Bram Moolenaarae616492020-07-28 20:07:27 +02007189 case CMD_append:
7190 case CMD_change:
7191 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007192 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007193 case CMD_xit:
7194 not_in_vim9(&ea);
7195 goto erret;
7196
Bram Moolenaar002262f2020-07-08 17:47:57 +02007197 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007198 if (cctx.ctx_skip != SKIP_YES)
7199 {
7200 semsg(_(e_invalid_command_str), ea.cmd);
7201 goto erret;
7202 }
7203 // We don't check for a next command here.
7204 line = (char_u *)"";
7205 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007206
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007207 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007208 if (cctx.ctx_skip == SKIP_YES)
7209 {
7210 // We don't check for a next command here.
7211 line = (char_u *)"";
7212 }
7213 else
7214 {
7215 // Not recognized, execute with do_cmdline_cmd().
7216 ea.arg = p;
7217 line = compile_exec(line, &ea, &cctx);
7218 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007219 break;
7220 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007221nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007222 if (line == NULL)
7223 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007224 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007225
7226 if (cctx.ctx_type_stack.ga_len < 0)
7227 {
7228 iemsg("Type stack underflow");
7229 goto erret;
7230 }
7231 }
7232
7233 if (cctx.ctx_scope != NULL)
7234 {
7235 if (cctx.ctx_scope->se_type == IF_SCOPE)
7236 emsg(_(e_endif));
7237 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7238 emsg(_(e_endwhile));
7239 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7240 emsg(_(e_endfor));
7241 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007242 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007243 goto erret;
7244 }
7245
Bram Moolenaarefd88552020-06-18 20:50:10 +02007246 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007247 {
7248 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7249 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007250 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007251 goto erret;
7252 }
7253
7254 // Return zero if there is no return at the end.
7255 generate_PUSHNR(&cctx, 0);
7256 generate_instr(&cctx, ISN_RETURN);
7257 }
7258
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007259 {
7260 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7261 + ufunc->uf_dfunc_idx;
7262 dfunc->df_deleted = FALSE;
7263 dfunc->df_instr = instr->ga_data;
7264 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007265 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007266 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007267 if (cctx.ctx_outer_used)
7268 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007269 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007270 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007271
7272 ret = OK;
7273
7274erret:
7275 if (ret == FAIL)
7276 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007277 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007278 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7279 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007280
7281 for (idx = 0; idx < instr->ga_len; ++idx)
7282 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007283 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007284
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007285 // If using the last entry in the table and it was added above, we
7286 // might as well remove it.
7287 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007288 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007289 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007290 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007291 ufunc->uf_dfunc_idx = 0;
7292 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007293 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007294
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007295 while (cctx.ctx_scope != NULL)
7296 drop_scope(&cctx);
7297
Bram Moolenaar20431c92020-03-20 18:39:46 +01007298 // Don't execute this function body.
7299 ga_clear_strings(&ufunc->uf_lines);
7300
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007301 if (errormsg != NULL)
7302 emsg(errormsg);
7303 else if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007304 emsg(_(e_compile_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007305 }
7306
7307 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007308 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007309 if (do_estack_push)
7310 estack_pop();
7311
Bram Moolenaar20431c92020-03-20 18:39:46 +01007312 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007313 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007314 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007315 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007316}
7317
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007318 void
7319set_function_type(ufunc_T *ufunc)
7320{
7321 int varargs = ufunc->uf_va_name != NULL;
7322 int argcount = ufunc->uf_args.ga_len;
7323
7324 // Create a type for the function, with the return type and any
7325 // argument types.
7326 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7327 // The type is included in "tt_args".
7328 if (argcount > 0 || varargs)
7329 {
7330 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7331 argcount, &ufunc->uf_type_list);
7332 // Add argument types to the function type.
7333 if (func_type_add_arg_types(ufunc->uf_func_type,
7334 argcount + varargs,
7335 &ufunc->uf_type_list) == FAIL)
7336 return;
7337 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7338 ufunc->uf_func_type->tt_min_argcount =
7339 argcount - ufunc->uf_def_args.ga_len;
7340 if (ufunc->uf_arg_types == NULL)
7341 {
7342 int i;
7343
7344 // lambda does not have argument types.
7345 for (i = 0; i < argcount; ++i)
7346 ufunc->uf_func_type->tt_args[i] = &t_any;
7347 }
7348 else
7349 mch_memmove(ufunc->uf_func_type->tt_args,
7350 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7351 if (varargs)
7352 {
7353 ufunc->uf_func_type->tt_args[argcount] =
7354 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7355 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7356 }
7357 }
7358 else
7359 // No arguments, can use a predefined type.
7360 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7361 argcount, &ufunc->uf_type_list);
7362}
7363
7364
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007365/*
7366 * Delete an instruction, free what it contains.
7367 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007368 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007369delete_instr(isn_T *isn)
7370{
7371 switch (isn->isn_type)
7372 {
7373 case ISN_EXEC:
7374 case ISN_LOADENV:
7375 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007376 case ISN_LOADB:
7377 case ISN_LOADW:
7378 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007379 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007380 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007381 case ISN_PUSHEXC:
7382 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007383 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007384 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007385 case ISN_STOREB:
7386 case ISN_STOREW:
7387 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007388 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007389 vim_free(isn->isn_arg.string);
7390 break;
7391
7392 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007393 case ISN_STORES:
7394 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007395 break;
7396
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007397 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007398 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007399 vim_free(isn->isn_arg.unlet.ul_name);
7400 break;
7401
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007402 case ISN_STOREOPT:
7403 vim_free(isn->isn_arg.storeopt.so_name);
7404 break;
7405
7406 case ISN_PUSHBLOB: // push blob isn_arg.blob
7407 blob_unref(isn->isn_arg.blob);
7408 break;
7409
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007410 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007411#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007412 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007413#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007414 break;
7415
7416 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007417#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007418 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007419#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007420 break;
7421
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007422 case ISN_UCALL:
7423 vim_free(isn->isn_arg.ufunc.cuf_name);
7424 break;
7425
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007426 case ISN_FUNCREF:
7427 {
7428 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7429 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02007430
7431 if (func_name_refcount(dfunc->df_ufunc->uf_name))
7432 func_ptr_unref(dfunc->df_ufunc);
7433 }
7434 break;
7435
7436 case ISN_DCALL:
7437 {
7438 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7439 + isn->isn_arg.dfunc.cdf_idx;
7440
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007441 if (dfunc->df_ufunc != NULL
7442 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02007443 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007444 }
7445 break;
7446
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007447 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007448 {
7449 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7450 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7451
7452 if (ufunc != NULL)
7453 {
7454 // Clear uf_dfunc_idx so that the function is deleted.
7455 clear_def_function(ufunc);
7456 ufunc->uf_dfunc_idx = 0;
7457 func_ptr_unref(ufunc);
7458 }
7459
7460 vim_free(lambda);
7461 vim_free(isn->isn_arg.newfunc.nf_global);
7462 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007463 break;
7464
Bram Moolenaar5e654232020-09-16 15:22:00 +02007465 case ISN_CHECKTYPE:
7466 free_type(isn->isn_arg.type.ct_type);
7467 break;
7468
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007469 case ISN_2BOOL:
7470 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007471 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007472 case ISN_ADDBLOB:
7473 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007474 case ISN_ANYINDEX:
7475 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007476 case ISN_BCALL:
7477 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007478 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007479 case ISN_CHECKNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007480 case ISN_COMPAREANY:
7481 case ISN_COMPAREBLOB:
7482 case ISN_COMPAREBOOL:
7483 case ISN_COMPAREDICT:
7484 case ISN_COMPAREFLOAT:
7485 case ISN_COMPAREFUNC:
7486 case ISN_COMPARELIST:
7487 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007488 case ISN_COMPARESPECIAL:
7489 case ISN_COMPARESTRING:
7490 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02007491 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007492 case ISN_DROP:
7493 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007494 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007495 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007496 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007497 case ISN_EXECCONCAT:
7498 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007499 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007500 case ISN_GETITEM:
7501 case ISN_JUMP:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007502 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007503 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007504 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007505 case ISN_LOADBDICT:
7506 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007507 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007508 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007509 case ISN_LOADSCRIPT:
7510 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007511 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007512 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007513 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007514 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007515 case ISN_NEGATENR:
7516 case ISN_NEWDICT:
7517 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007518 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007519 case ISN_OPFLOAT:
7520 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007521 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007522 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007523 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007524 case ISN_PUSHF:
7525 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007526 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007527 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007528 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007529 case ISN_SHUFFLE:
7530 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007531 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007532 case ISN_STOREDICT:
7533 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007534 case ISN_STORENR:
7535 case ISN_STOREOUTER:
7536 case ISN_STOREREG:
7537 case ISN_STORESCRIPT:
7538 case ISN_STOREV:
7539 case ISN_STRINDEX:
7540 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007541 case ISN_THROW:
7542 case ISN_TRY:
7543 // nothing allocated
7544 break;
7545 }
7546}
7547
7548/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007549 * Free all instructions for "dfunc".
7550 */
7551 static void
7552delete_def_function_contents(dfunc_T *dfunc)
7553{
7554 int idx;
7555
7556 ga_clear(&dfunc->df_def_args_isn);
7557
7558 if (dfunc->df_instr != NULL)
7559 {
7560 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7561 delete_instr(dfunc->df_instr + idx);
7562 VIM_CLEAR(dfunc->df_instr);
7563 }
7564
7565 dfunc->df_deleted = TRUE;
7566}
7567
7568/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007569 * When a user function is deleted, clear the contents of any associated def
7570 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007571 */
7572 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007573clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007574{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007575 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007576 {
7577 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7578 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007579
Bram Moolenaar20431c92020-03-20 18:39:46 +01007580 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007581 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007582 }
7583}
7584
Bram Moolenaarfdeab652020-09-19 15:16:50 +02007585/*
7586 * Used when a user function is about to be deleted: remove the pointer to it.
7587 * The entry in def_functions is then unused.
7588 */
7589 void
7590unlink_def_function(ufunc_T *ufunc)
7591{
7592 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
7593
7594 dfunc->df_ufunc = NULL;
7595}
7596
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007597#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007598/*
7599 * Free all functions defined with ":def".
7600 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007601 void
7602free_def_functions(void)
7603{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007604 int idx;
7605
7606 for (idx = 0; idx < def_functions.ga_len; ++idx)
7607 {
7608 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7609
7610 delete_def_function_contents(dfunc);
7611 }
7612
7613 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007614}
7615#endif
7616
7617
7618#endif // FEAT_EVAL