blob: e4b7d44adbfbe1831ba301d1d68c9aa576e9bf2d [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
111 int lv_from_outer; // when TRUE using ctx_outer scope
112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200129 int ctx_closure_count; // number of closures created in the
130 // function
131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144};
145
Bram Moolenaar20431c92020-03-20 18:39:46 +0100146static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100147
148/*
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200149 * Lookup variable "name" in the local scope and return it.
150 * Return NULL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200152 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153lookup_local(char_u *name, size_t len, cctx_T *cctx)
154{
155 int idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200156 lvar_T *lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100158 if (len == 0)
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200159 return NULL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200160
161 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
163 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200164 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100165 if (STRNCMP(name, lvar->lv_name, len) == 0
166 && STRLEN(lvar->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167 {
168 lvar->lv_from_outer = FALSE;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200169 return lvar;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200170 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200172
173 // Find local in outer function scope.
174 if (cctx->ctx_outer != NULL)
175 {
176 lvar = lookup_local(name, len, cctx->ctx_outer);
177 if (lvar != NULL)
178 {
179 // TODO: are there situations we should not mark the outer scope as
180 // used?
181 cctx->ctx_outer_used = TRUE;
182 lvar->lv_from_outer = TRUE;
183 return lvar;
184 }
185 }
186
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200187 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100188}
189
190/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200191 * Lookup an argument in the current function and an enclosing function.
192 * Returns the argument index in "idxp"
193 * Returns the argument type in "type"
194 * Sets "gen_load_outer" to TRUE if found in outer scope.
195 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196 */
197 static int
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200198lookup_arg(
199 char_u *name,
200 size_t len,
201 int *idxp,
202 type_T **type,
203 int *gen_load_outer,
204 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100205{
206 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200207 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100209 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200210 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
212 {
213 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
214
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200215 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
216 {
217 if (idxp != NULL)
218 {
219 // Arguments are located above the frame pointer. One further
220 // if there is a vararg argument
221 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
222 + STACK_FRAME_SIZE)
223 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
224
225 if (cctx->ctx_ufunc->uf_arg_types != NULL)
226 *type = cctx->ctx_ufunc->uf_arg_types[idx];
227 else
228 *type = &t_any;
229 }
230 return OK;
231 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100232 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100233
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200234 va_name = cctx->ctx_ufunc->uf_va_name;
235 if (va_name != NULL
236 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
237 {
238 if (idxp != NULL)
239 {
240 // varargs is always the last argument
241 *idxp = -STACK_FRAME_SIZE - 1;
242 *type = cctx->ctx_ufunc->uf_va_type;
243 }
244 return OK;
245 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200247 if (cctx->ctx_outer != NULL)
248 {
249 // Lookup the name for an argument of the outer function.
250 if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
251 == OK)
252 {
253 *gen_load_outer = TRUE;
254 return OK;
255 }
256 }
257
258 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100259}
260
261/*
262 * Lookup a variable in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200263 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
264 * without "s:".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100265 * Returns OK or FAIL.
266 */
267 static int
Bram Moolenaar53900992020-08-22 19:02:02 +0200268lookup_script(char_u *name, size_t len, int vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100269{
270 int cc;
271 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
272 dictitem_T *di;
273
Bram Moolenaar53900992020-08-22 19:02:02 +0200274 if (vim9script && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
275 != SCRIPT_VERSION_VIM9)
276 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277 cc = name[len];
278 name[len] = NUL;
279 di = find_var_in_ht(ht, 0, name, TRUE);
280 name[len] = cc;
281 return di == NULL ? FAIL: OK;
282}
283
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100284/*
285 * Check if "p[len]" is already defined, either in script "import_sid" or in
286 * compilation context "cctx".
287 * Return FAIL and give an error if it defined.
288 */
289 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200290check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100291{
Bram Moolenaarad486a02020-08-01 23:22:18 +0200292 int c = p[len];
293
294 p[len] = NUL;
Bram Moolenaar53900992020-08-22 19:02:02 +0200295 if (lookup_script(p, len, FALSE) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100296 || (cctx != NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200297 && (lookup_local(p, len, cctx) != NULL
Bram Moolenaareef21022020-08-01 22:16:43 +0200298 || lookup_arg(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200299 || find_imported(p, len, cctx) != NULL
300 || find_func_even_dead(p, FALSE, cctx) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100301 {
Bram Moolenaarad486a02020-08-01 23:22:18 +0200302 p[len] = c;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200303 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100304 return FAIL;
305 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200306 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100307 return OK;
308}
309
Bram Moolenaar65b95452020-07-19 14:03:09 +0200310
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100311/////////////////////////////////////////////////////////////////////
312// Following generate_ functions expect the caller to call ga_grow().
313
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200314#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
315#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100316
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100317/*
318 * Generate an instruction without arguments.
319 * Returns a pointer to the new instruction, NULL if failed.
320 */
321 static isn_T *
322generate_instr(cctx_T *cctx, isntype_T isn_type)
323{
324 garray_T *instr = &cctx->ctx_instr;
325 isn_T *isn;
326
Bram Moolenaar080457c2020-03-03 21:53:32 +0100327 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100328 if (ga_grow(instr, 1) == FAIL)
329 return NULL;
330 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
331 isn->isn_type = isn_type;
332 isn->isn_lnum = cctx->ctx_lnum + 1;
333 ++instr->ga_len;
334
335 return isn;
336}
337
338/*
339 * Generate an instruction without arguments.
340 * "drop" will be removed from the stack.
341 * Returns a pointer to the new instruction, NULL if failed.
342 */
343 static isn_T *
344generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
345{
346 garray_T *stack = &cctx->ctx_type_stack;
347
Bram Moolenaar080457c2020-03-03 21:53:32 +0100348 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100349 stack->ga_len -= drop;
350 return generate_instr(cctx, isn_type);
351}
352
353/*
354 * Generate instruction "isn_type" and put "type" on the type stack.
355 */
356 static isn_T *
357generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
358{
359 isn_T *isn;
360 garray_T *stack = &cctx->ctx_type_stack;
361
362 if ((isn = generate_instr(cctx, isn_type)) == NULL)
363 return NULL;
364
365 if (ga_grow(stack, 1) == FAIL)
366 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200367 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100368 ++stack->ga_len;
369
370 return isn;
371}
372
373/*
374 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200375 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100376 */
377 static int
378may_generate_2STRING(int offset, cctx_T *cctx)
379{
380 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200381 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100382 garray_T *stack = &cctx->ctx_type_stack;
383 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
384
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200385 switch ((*type)->tt_type)
386 {
387 // nothing to be done
388 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100389
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200390 // conversion possible
391 case VAR_SPECIAL:
392 case VAR_BOOL:
393 case VAR_NUMBER:
394 case VAR_FLOAT:
395 break;
396
397 // conversion possible (with runtime check)
398 case VAR_ANY:
399 case VAR_UNKNOWN:
400 isntype = ISN_2STRING_ANY;
401 break;
402
403 // conversion not possible
404 case VAR_VOID:
405 case VAR_BLOB:
406 case VAR_FUNC:
407 case VAR_PARTIAL:
408 case VAR_LIST:
409 case VAR_DICT:
410 case VAR_JOB:
411 case VAR_CHANNEL:
412 to_string_error((*type)->tt_type);
413 return FAIL;
414 }
415
416 *type = &t_string;
417 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100418 return FAIL;
419 isn->isn_arg.number = offset;
420
421 return OK;
422}
423
424 static int
425check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
426{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200427 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100428 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200429 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100430 {
431 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200432 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100433 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200434 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100435 return FAIL;
436 }
437 return OK;
438}
439
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200440 static int
441generate_add_instr(
442 cctx_T *cctx,
443 vartype_T vartype,
444 type_T *type1,
445 type_T *type2)
446{
447 isn_T *isn = generate_instr_drop(cctx,
448 vartype == VAR_NUMBER ? ISN_OPNR
449 : vartype == VAR_LIST ? ISN_ADDLIST
450 : vartype == VAR_BLOB ? ISN_ADDBLOB
451#ifdef FEAT_FLOAT
452 : vartype == VAR_FLOAT ? ISN_OPFLOAT
453#endif
454 : ISN_OPANY, 1);
455
456 if (vartype != VAR_LIST && vartype != VAR_BLOB
457 && type1->tt_type != VAR_ANY
458 && type2->tt_type != VAR_ANY
459 && check_number_or_float(
460 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
461 return FAIL;
462
463 if (isn != NULL)
464 isn->isn_arg.op.op_type = EXPR_ADD;
465 return isn == NULL ? FAIL : OK;
466}
467
468/*
469 * Get the type to use for an instruction for an operation on "type1" and
470 * "type2". If they are matching use a type-specific instruction. Otherwise
471 * fall back to runtime type checking.
472 */
473 static vartype_T
474operator_type(type_T *type1, type_T *type2)
475{
476 if (type1->tt_type == type2->tt_type
477 && (type1->tt_type == VAR_NUMBER
478 || type1->tt_type == VAR_LIST
479#ifdef FEAT_FLOAT
480 || type1->tt_type == VAR_FLOAT
481#endif
482 || type1->tt_type == VAR_BLOB))
483 return type1->tt_type;
484 return VAR_ANY;
485}
486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100487/*
488 * Generate an instruction with two arguments. The instruction depends on the
489 * type of the arguments.
490 */
491 static int
492generate_two_op(cctx_T *cctx, char_u *op)
493{
494 garray_T *stack = &cctx->ctx_type_stack;
495 type_T *type1;
496 type_T *type2;
497 vartype_T vartype;
498 isn_T *isn;
499
Bram Moolenaar080457c2020-03-03 21:53:32 +0100500 RETURN_OK_IF_SKIP(cctx);
501
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200502 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100503 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
504 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200505 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100506
507 switch (*op)
508 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200509 case '+':
510 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100511 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100512 break;
513
514 case '-':
515 case '*':
516 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
517 op) == FAIL)
518 return FAIL;
519 if (vartype == VAR_NUMBER)
520 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
521#ifdef FEAT_FLOAT
522 else if (vartype == VAR_FLOAT)
523 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
524#endif
525 else
526 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
527 if (isn != NULL)
528 isn->isn_arg.op.op_type = *op == '*'
529 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
530 break;
531
Bram Moolenaar4c683752020-04-05 21:38:23 +0200532 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200534 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535 && type2->tt_type != VAR_NUMBER))
536 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200537 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100538 return FAIL;
539 }
540 isn = generate_instr_drop(cctx,
541 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
542 if (isn != NULL)
543 isn->isn_arg.op.op_type = EXPR_REM;
544 break;
545 }
546
547 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200548 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100549 {
550 type_T *type = &t_any;
551
552#ifdef FEAT_FLOAT
553 // float+number and number+float results in float
554 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
555 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
556 type = &t_float;
557#endif
558 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
559 }
560
561 return OK;
562}
563
564/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200565 * Get the instruction to use for comparing "type1" with "type2"
566 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100567 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200568 static isntype_T
569get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100570{
571 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100572
Bram Moolenaar4c683752020-04-05 21:38:23 +0200573 if (type1 == VAR_UNKNOWN)
574 type1 = VAR_ANY;
575 if (type2 == VAR_UNKNOWN)
576 type2 = VAR_ANY;
577
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100578 if (type1 == type2)
579 {
580 switch (type1)
581 {
582 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
583 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
584 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
585 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
586 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
587 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
588 case VAR_LIST: isntype = ISN_COMPARELIST; break;
589 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
590 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591 default: isntype = ISN_COMPAREANY; break;
592 }
593 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200594 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100595 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
596 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
597 isntype = ISN_COMPAREANY;
598
599 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
600 && (isntype == ISN_COMPAREBOOL
601 || isntype == ISN_COMPARESPECIAL
602 || isntype == ISN_COMPARENR
603 || isntype == ISN_COMPAREFLOAT))
604 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200605 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100606 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200607 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100608 }
609 if (isntype == ISN_DROP
610 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
611 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
612 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
613 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
614 && exptype != EXPR_IS && exptype != EXPR_ISNOT
615 && (type1 == VAR_BLOB || type2 == VAR_BLOB
616 || type1 == VAR_LIST || type2 == VAR_LIST))))
617 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200618 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100619 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200620 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200622 return isntype;
623}
624
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200625 int
626check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
627{
628 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
629 return FAIL;
630 return OK;
631}
632
Bram Moolenaara5565e42020-05-09 15:44:01 +0200633/*
634 * Generate an ISN_COMPARE* instruction with a boolean result.
635 */
636 static int
637generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
638{
639 isntype_T isntype;
640 isn_T *isn;
641 garray_T *stack = &cctx->ctx_type_stack;
642 vartype_T type1;
643 vartype_T type2;
644
645 RETURN_OK_IF_SKIP(cctx);
646
647 // Get the known type of the two items on the stack. If they are matching
648 // use a type-specific instruction. Otherwise fall back to runtime type
649 // checking.
650 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
651 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
652 isntype = get_compare_isn(exptype, type1, type2);
653 if (isntype == ISN_DROP)
654 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655
656 if ((isn = generate_instr(cctx, isntype)) == NULL)
657 return FAIL;
658 isn->isn_arg.op.op_type = exptype;
659 isn->isn_arg.op.op_ic = ic;
660
661 // takes two arguments, puts one bool back
662 if (stack->ga_len >= 2)
663 {
664 --stack->ga_len;
665 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
666 }
667
668 return OK;
669}
670
671/*
672 * Generate an ISN_2BOOL instruction.
673 */
674 static int
675generate_2BOOL(cctx_T *cctx, int invert)
676{
677 isn_T *isn;
678 garray_T *stack = &cctx->ctx_type_stack;
679
Bram Moolenaar080457c2020-03-03 21:53:32 +0100680 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
682 return FAIL;
683 isn->isn_arg.number = invert;
684
685 // type becomes bool
686 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
687
688 return OK;
689}
690
691 static int
692generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
693{
694 isn_T *isn;
695 garray_T *stack = &cctx->ctx_type_stack;
696
Bram Moolenaar080457c2020-03-03 21:53:32 +0100697 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100698 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
699 return FAIL;
Bram Moolenaar939b5db2020-04-28 22:49:08 +0200700 // TODO: whole type, e.g. for a function also arg and return types
701 isn->isn_arg.type.ct_type = vartype->tt_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100702 isn->isn_arg.type.ct_off = offset;
703
704 // type becomes vartype
Bram Moolenaar5adc55c2020-05-02 23:12:58 +0200705 ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706
707 return OK;
708}
709
710/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200711 * Check that
712 * - "actual" is "expected" type or
713 * - "actual" is a type that can be "expected" type: add a runtime check; or
714 * - return FAIL.
715 */
716 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200717need_type(
718 type_T *actual,
719 type_T *expected,
720 int offset,
721 cctx_T *cctx,
722 int silent)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200723{
724 if (check_type(expected, actual, FALSE) == OK)
725 return OK;
726 if (actual->tt_type != VAR_ANY
727 && actual->tt_type != VAR_UNKNOWN
728 && !(actual->tt_type == VAR_FUNC
729 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
730 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200731 if (!silent)
732 type_mismatch(expected, actual);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200733 return FAIL;
734 }
735 generate_TYPECHECK(cctx, expected, offset);
736 return OK;
737}
738
739/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100740 * Generate an ISN_PUSHNR instruction.
741 */
742 static int
743generate_PUSHNR(cctx_T *cctx, varnumber_T number)
744{
745 isn_T *isn;
746
Bram Moolenaar080457c2020-03-03 21:53:32 +0100747 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100748 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
749 return FAIL;
750 isn->isn_arg.number = number;
751
752 return OK;
753}
754
755/*
756 * Generate an ISN_PUSHBOOL instruction.
757 */
758 static int
759generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
760{
761 isn_T *isn;
762
Bram Moolenaar080457c2020-03-03 21:53:32 +0100763 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
765 return FAIL;
766 isn->isn_arg.number = number;
767
768 return OK;
769}
770
771/*
772 * Generate an ISN_PUSHSPEC instruction.
773 */
774 static int
775generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
776{
777 isn_T *isn;
778
Bram Moolenaar080457c2020-03-03 21:53:32 +0100779 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100780 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
781 return FAIL;
782 isn->isn_arg.number = number;
783
784 return OK;
785}
786
787#ifdef FEAT_FLOAT
788/*
789 * Generate an ISN_PUSHF instruction.
790 */
791 static int
792generate_PUSHF(cctx_T *cctx, float_T fnumber)
793{
794 isn_T *isn;
795
Bram Moolenaar080457c2020-03-03 21:53:32 +0100796 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100797 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
798 return FAIL;
799 isn->isn_arg.fnumber = fnumber;
800
801 return OK;
802}
803#endif
804
805/*
806 * Generate an ISN_PUSHS instruction.
807 * Consumes "str".
808 */
809 static int
810generate_PUSHS(cctx_T *cctx, char_u *str)
811{
812 isn_T *isn;
813
Bram Moolenaar080457c2020-03-03 21:53:32 +0100814 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100815 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
816 return FAIL;
817 isn->isn_arg.string = str;
818
819 return OK;
820}
821
822/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100823 * Generate an ISN_PUSHCHANNEL instruction.
824 * Consumes "channel".
825 */
826 static int
827generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
828{
829 isn_T *isn;
830
Bram Moolenaar080457c2020-03-03 21:53:32 +0100831 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100832 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
833 return FAIL;
834 isn->isn_arg.channel = channel;
835
836 return OK;
837}
838
839/*
840 * Generate an ISN_PUSHJOB instruction.
841 * Consumes "job".
842 */
843 static int
844generate_PUSHJOB(cctx_T *cctx, job_T *job)
845{
846 isn_T *isn;
847
Bram Moolenaar080457c2020-03-03 21:53:32 +0100848 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +0100849 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100850 return FAIL;
851 isn->isn_arg.job = job;
852
853 return OK;
854}
855
856/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100857 * Generate an ISN_PUSHBLOB instruction.
858 * Consumes "blob".
859 */
860 static int
861generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
862{
863 isn_T *isn;
864
Bram Moolenaar080457c2020-03-03 21:53:32 +0100865 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100866 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
867 return FAIL;
868 isn->isn_arg.blob = blob;
869
870 return OK;
871}
872
873/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100874 * Generate an ISN_PUSHFUNC instruction with name "name".
875 * Consumes "name".
876 */
877 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200878generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100879{
880 isn_T *isn;
881
Bram Moolenaar080457c2020-03-03 21:53:32 +0100882 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200883 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100884 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200885 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100886
887 return OK;
888}
889
890/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200891 * Generate an ISN_GETITEM instruction with "index".
892 */
893 static int
894generate_GETITEM(cctx_T *cctx, int index)
895{
896 isn_T *isn;
897 garray_T *stack = &cctx->ctx_type_stack;
898 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
899 type_T *item_type = &t_any;
900
901 RETURN_OK_IF_SKIP(cctx);
902
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200903 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200904 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200905 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200906 emsg(_(e_listreq));
907 return FAIL;
908 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +0200909 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200910 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
911 return FAIL;
912 isn->isn_arg.number = index;
913
914 // add the item type to the type stack
915 if (ga_grow(stack, 1) == FAIL)
916 return FAIL;
917 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
918 ++stack->ga_len;
919 return OK;
920}
921
922/*
Bram Moolenaar9af78762020-06-16 11:34:42 +0200923 * Generate an ISN_SLICE instruction with "count".
924 */
925 static int
926generate_SLICE(cctx_T *cctx, int count)
927{
928 isn_T *isn;
929
930 RETURN_OK_IF_SKIP(cctx);
931 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
932 return FAIL;
933 isn->isn_arg.number = count;
934 return OK;
935}
936
937/*
938 * Generate an ISN_CHECKLEN instruction with "min_len".
939 */
940 static int
941generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
942{
943 isn_T *isn;
944
945 RETURN_OK_IF_SKIP(cctx);
946
947 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
948 return FAIL;
949 isn->isn_arg.checklen.cl_min_len = min_len;
950 isn->isn_arg.checklen.cl_more_OK = more_OK;
951
952 return OK;
953}
954
955/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100956 * Generate an ISN_STORE instruction.
957 */
958 static int
959generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
960{
961 isn_T *isn;
962
Bram Moolenaar080457c2020-03-03 21:53:32 +0100963 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100964 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
965 return FAIL;
966 if (name != NULL)
967 isn->isn_arg.string = vim_strsave(name);
968 else
969 isn->isn_arg.number = idx;
970
971 return OK;
972}
973
974/*
975 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
976 */
977 static int
978generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
979{
980 isn_T *isn;
981
Bram Moolenaar080457c2020-03-03 21:53:32 +0100982 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100983 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
984 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +0100985 isn->isn_arg.storenr.stnr_idx = idx;
986 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100987
988 return OK;
989}
990
991/*
992 * Generate an ISN_STOREOPT instruction
993 */
994 static int
995generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
996{
997 isn_T *isn;
998
Bram Moolenaar080457c2020-03-03 21:53:32 +0100999 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001000 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001001 return FAIL;
1002 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1003 isn->isn_arg.storeopt.so_flags = opt_flags;
1004
1005 return OK;
1006}
1007
1008/*
1009 * Generate an ISN_LOAD or similar instruction.
1010 */
1011 static int
1012generate_LOAD(
1013 cctx_T *cctx,
1014 isntype_T isn_type,
1015 int idx,
1016 char_u *name,
1017 type_T *type)
1018{
1019 isn_T *isn;
1020
Bram Moolenaar080457c2020-03-03 21:53:32 +01001021 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001022 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1023 return FAIL;
1024 if (name != NULL)
1025 isn->isn_arg.string = vim_strsave(name);
1026 else
1027 isn->isn_arg.number = idx;
1028
1029 return OK;
1030}
1031
1032/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001033 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001034 */
1035 static int
1036generate_LOADV(
1037 cctx_T *cctx,
1038 char_u *name,
1039 int error)
1040{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001041 int di_flags;
1042 int vidx = find_vim_var(name, &di_flags);
1043 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001044
Bram Moolenaar080457c2020-03-03 21:53:32 +01001045 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001046 if (vidx < 0)
1047 {
1048 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001049 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001050 return FAIL;
1051 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001052 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001053
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001054 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001055}
1056
1057/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001058 * Generate an ISN_UNLET instruction.
1059 */
1060 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001061generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001062{
1063 isn_T *isn;
1064
1065 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001066 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001067 return FAIL;
1068 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1069 isn->isn_arg.unlet.ul_forceit = forceit;
1070
1071 return OK;
1072}
1073
1074/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075 * Generate an ISN_LOADS instruction.
1076 */
1077 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001078generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001079 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001080 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001082 int sid,
1083 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084{
1085 isn_T *isn;
1086
Bram Moolenaar080457c2020-03-03 21:53:32 +01001087 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001088 if (isn_type == ISN_LOADS)
1089 isn = generate_instr_type(cctx, isn_type, type);
1090 else
1091 isn = generate_instr_drop(cctx, isn_type, 1);
1092 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001093 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001094 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1095 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096
1097 return OK;
1098}
1099
1100/*
1101 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1102 */
1103 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001104generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001105 cctx_T *cctx,
1106 isntype_T isn_type,
1107 int sid,
1108 int idx,
1109 type_T *type)
1110{
1111 isn_T *isn;
1112
Bram Moolenaar080457c2020-03-03 21:53:32 +01001113 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001114 if (isn_type == ISN_LOADSCRIPT)
1115 isn = generate_instr_type(cctx, isn_type, type);
1116 else
1117 isn = generate_instr_drop(cctx, isn_type, 1);
1118 if (isn == NULL)
1119 return FAIL;
1120 isn->isn_arg.script.script_sid = sid;
1121 isn->isn_arg.script.script_idx = idx;
1122 return OK;
1123}
1124
1125/*
1126 * Generate an ISN_NEWLIST instruction.
1127 */
1128 static int
1129generate_NEWLIST(cctx_T *cctx, int count)
1130{
1131 isn_T *isn;
1132 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001133 type_T *type;
1134 type_T *member;
1135
Bram Moolenaar080457c2020-03-03 21:53:32 +01001136 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001137 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1138 return FAIL;
1139 isn->isn_arg.number = count;
1140
Bram Moolenaar127542b2020-08-09 17:22:04 +02001141 // get the member type from all the items on the stack.
1142 member = get_member_type_from_stack(
1143 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1144 cctx->ctx_type_list);
1145 type = get_list_type(member, cctx->ctx_type_list);
1146
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001147 // drop the value types
1148 stack->ga_len -= count;
1149
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001150 // add the list type to the type stack
1151 if (ga_grow(stack, 1) == FAIL)
1152 return FAIL;
1153 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1154 ++stack->ga_len;
1155
1156 return OK;
1157}
1158
1159/*
1160 * Generate an ISN_NEWDICT instruction.
1161 */
1162 static int
1163generate_NEWDICT(cctx_T *cctx, int count)
1164{
1165 isn_T *isn;
1166 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001167 type_T *type;
1168 type_T *member;
1169
Bram Moolenaar080457c2020-03-03 21:53:32 +01001170 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1172 return FAIL;
1173 isn->isn_arg.number = count;
1174
Bram Moolenaar127542b2020-08-09 17:22:04 +02001175 member = get_member_type_from_stack(
1176 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1177 cctx->ctx_type_list);
1178 type = get_dict_type(member, cctx->ctx_type_list);
1179
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001180 // drop the key and value types
1181 stack->ga_len -= 2 * count;
1182
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001183 // add the dict type to the type stack
1184 if (ga_grow(stack, 1) == FAIL)
1185 return FAIL;
1186 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1187 ++stack->ga_len;
1188
1189 return OK;
1190}
1191
1192/*
1193 * Generate an ISN_FUNCREF instruction.
1194 */
1195 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001196generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001197{
1198 isn_T *isn;
1199 garray_T *stack = &cctx->ctx_type_stack;
1200
Bram Moolenaar080457c2020-03-03 21:53:32 +01001201 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001202 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1203 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001204 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001205 isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206
1207 if (ga_grow(stack, 1) == FAIL)
1208 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001209 ((type_T **)stack->ga_data)[stack->ga_len] =
1210 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001211 ++stack->ga_len;
1212
1213 return OK;
1214}
1215
1216/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001217 * Generate an ISN_NEWFUNC instruction.
1218 */
1219 static int
1220generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1221{
1222 isn_T *isn;
1223 char_u *name;
1224
1225 RETURN_OK_IF_SKIP(cctx);
1226 name = vim_strsave(lambda_name);
1227 if (name == NULL)
1228 return FAIL;
1229 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1230 return FAIL;
1231 isn->isn_arg.newfunc.nf_lambda = name;
1232 isn->isn_arg.newfunc.nf_global = func_name;
1233
1234 return OK;
1235}
1236
1237/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001238 * Generate an ISN_JUMP instruction.
1239 */
1240 static int
1241generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1242{
1243 isn_T *isn;
1244 garray_T *stack = &cctx->ctx_type_stack;
1245
Bram Moolenaar080457c2020-03-03 21:53:32 +01001246 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001247 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1248 return FAIL;
1249 isn->isn_arg.jump.jump_when = when;
1250 isn->isn_arg.jump.jump_where = where;
1251
1252 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1253 --stack->ga_len;
1254
1255 return OK;
1256}
1257
1258 static int
1259generate_FOR(cctx_T *cctx, int loop_idx)
1260{
1261 isn_T *isn;
1262 garray_T *stack = &cctx->ctx_type_stack;
1263
Bram Moolenaar080457c2020-03-03 21:53:32 +01001264 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001265 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1266 return FAIL;
1267 isn->isn_arg.forloop.for_idx = loop_idx;
1268
1269 if (ga_grow(stack, 1) == FAIL)
1270 return FAIL;
1271 // type doesn't matter, will be stored next
1272 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1273 ++stack->ga_len;
1274
1275 return OK;
1276}
1277
1278/*
1279 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001280 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001281 * Return FAIL if the number of arguments is wrong.
1282 */
1283 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001284generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001285{
1286 isn_T *isn;
1287 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001288 int argoff;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001289 type_T *argtypes[MAX_FUNC_ARGS];
1290 int i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291
Bram Moolenaar080457c2020-03-03 21:53:32 +01001292 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001293 argoff = check_internal_func(func_idx, argcount);
1294 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001295 return FAIL;
1296
Bram Moolenaar389df252020-07-09 21:20:47 +02001297 if (method_call && argoff > 1)
1298 {
1299 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1300 return FAIL;
1301 isn->isn_arg.shuffle.shfl_item = argcount;
1302 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1303 }
1304
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1306 return FAIL;
1307 isn->isn_arg.bfunc.cbf_idx = func_idx;
1308 isn->isn_arg.bfunc.cbf_argcount = argcount;
1309
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001310 for (i = 0; i < argcount; ++i)
1311 argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1312
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001313 stack->ga_len -= argcount; // drop the arguments
1314 if (ga_grow(stack, 1) == FAIL)
1315 return FAIL;
1316 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001317 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318 ++stack->ga_len; // add return value
1319
1320 return OK;
1321}
1322
1323/*
1324 * Generate an ISN_DCALL or ISN_UCALL instruction.
1325 * Return FAIL if the number of arguments is wrong.
1326 */
1327 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001328generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001329{
1330 isn_T *isn;
1331 garray_T *stack = &cctx->ctx_type_stack;
1332 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001333 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001334
Bram Moolenaar080457c2020-03-03 21:53:32 +01001335 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336 if (argcount > regular_args && !has_varargs(ufunc))
1337 {
1338 semsg(_(e_toomanyarg), ufunc->uf_name);
1339 return FAIL;
1340 }
1341 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1342 {
1343 semsg(_(e_toofewarg), ufunc->uf_name);
1344 return FAIL;
1345 }
1346
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001347 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001348 {
1349 int i;
1350
1351 for (i = 0; i < argcount; ++i)
1352 {
1353 type_T *expected;
1354 type_T *actual;
1355
1356 if (i < regular_args)
1357 {
1358 if (ufunc->uf_arg_types == NULL)
1359 continue;
1360 expected = ufunc->uf_arg_types[i];
1361 }
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001362 else if (ufunc->uf_va_type == NULL)
1363 // possibly a lambda
1364 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001365 else
1366 expected = ufunc->uf_va_type->tt_member;
1367 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001368 if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001369 {
1370 arg_type_mismatch(expected, actual, i + 1);
1371 return FAIL;
1372 }
1373 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001374 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001375 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1376 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001377 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001378 }
1379
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001381 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001382 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001383 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001384 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385 {
1386 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1387 isn->isn_arg.dfunc.cdf_argcount = argcount;
1388 }
1389 else
1390 {
1391 // A user function may be deleted and redefined later, can't use the
1392 // ufunc pointer, need to look it up again at runtime.
1393 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1394 isn->isn_arg.ufunc.cuf_argcount = argcount;
1395 }
1396
1397 stack->ga_len -= argcount; // drop the arguments
1398 if (ga_grow(stack, 1) == FAIL)
1399 return FAIL;
1400 // add return value
1401 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1402 ++stack->ga_len;
1403
1404 return OK;
1405}
1406
1407/*
1408 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1409 */
1410 static int
1411generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1412{
1413 isn_T *isn;
1414 garray_T *stack = &cctx->ctx_type_stack;
1415
Bram Moolenaar080457c2020-03-03 21:53:32 +01001416 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001417 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1418 return FAIL;
1419 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1420 isn->isn_arg.ufunc.cuf_argcount = argcount;
1421
1422 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001423 if (ga_grow(stack, 1) == FAIL)
1424 return FAIL;
1425 // add return value
1426 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1427 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001428
1429 return OK;
1430}
1431
1432/*
1433 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001434 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001435 */
1436 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001437generate_PCALL(
1438 cctx_T *cctx,
1439 int argcount,
1440 char_u *name,
1441 type_T *type,
1442 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001443{
1444 isn_T *isn;
1445 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001446 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001447
Bram Moolenaar080457c2020-03-03 21:53:32 +01001448 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001449
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001450 if (type->tt_type == VAR_ANY)
1451 ret_type = &t_any;
1452 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001453 {
1454 if (type->tt_argcount != -1)
1455 {
1456 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1457
1458 if (argcount < type->tt_min_argcount - varargs)
1459 {
1460 semsg(_(e_toofewarg), "[reference]");
1461 return FAIL;
1462 }
1463 if (!varargs && argcount > type->tt_argcount)
1464 {
1465 semsg(_(e_toomanyarg), "[reference]");
1466 return FAIL;
1467 }
1468 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001469 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001470 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001471 else
1472 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001473 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001474 return FAIL;
1475 }
1476
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001477 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1478 return FAIL;
1479 isn->isn_arg.pfunc.cpf_top = at_top;
1480 isn->isn_arg.pfunc.cpf_argcount = argcount;
1481
1482 stack->ga_len -= argcount; // drop the arguments
1483
1484 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001485 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001486
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001487 // If partial is above the arguments it must be cleared and replaced with
1488 // the return value.
1489 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1490 return FAIL;
1491
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492 return OK;
1493}
1494
1495/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001496 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001497 */
1498 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001499generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001500{
1501 isn_T *isn;
1502 garray_T *stack = &cctx->ctx_type_stack;
1503 type_T *type;
1504
Bram Moolenaar080457c2020-03-03 21:53:32 +01001505 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001506 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001508 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001509
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001510 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001511 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001512 if (type->tt_type != VAR_DICT && type != &t_any)
1513 {
1514 emsg(_(e_dictreq));
1515 return FAIL;
1516 }
1517 // change dict type to dict member type
1518 if (type->tt_type == VAR_DICT)
1519 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001520
1521 return OK;
1522}
1523
1524/*
1525 * Generate an ISN_ECHO instruction.
1526 */
1527 static int
1528generate_ECHO(cctx_T *cctx, int with_white, int count)
1529{
1530 isn_T *isn;
1531
Bram Moolenaar080457c2020-03-03 21:53:32 +01001532 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001533 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1534 return FAIL;
1535 isn->isn_arg.echo.echo_with_white = with_white;
1536 isn->isn_arg.echo.echo_count = count;
1537
1538 return OK;
1539}
1540
Bram Moolenaarad39c092020-02-26 18:23:43 +01001541/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001542 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001543 */
1544 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001545generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001546{
1547 isn_T *isn;
1548
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001549 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001550 return FAIL;
1551 isn->isn_arg.number = count;
1552
1553 return OK;
1554}
1555
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556 static int
1557generate_EXEC(cctx_T *cctx, char_u *line)
1558{
1559 isn_T *isn;
1560
Bram Moolenaar080457c2020-03-03 21:53:32 +01001561 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001562 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1563 return FAIL;
1564 isn->isn_arg.string = vim_strsave(line);
1565 return OK;
1566}
1567
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001568 static int
1569generate_EXECCONCAT(cctx_T *cctx, int count)
1570{
1571 isn_T *isn;
1572
1573 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1574 return FAIL;
1575 isn->isn_arg.number = count;
1576 return OK;
1577}
1578
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001579/*
1580 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001581 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001582 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001583 static lvar_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001584reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
1585{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001586 lvar_T *lvar;
1587
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02001588 if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001589 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001590 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001591 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592 }
1593
1594 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001595 return NULL;
1596 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001597
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001598 // Every local variable uses the next entry on the stack. We could re-use
1599 // the last ones when leaving a scope, but then variables used in a closure
1600 // might get overwritten. To keep things simple do not re-use stack
1601 // entries. This is less efficient, but memory is cheap these days.
1602 lvar->lv_idx = cctx->ctx_locals_count++;
1603
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001604 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605 lvar->lv_const = isConst;
1606 lvar->lv_type = type;
1607
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001608 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609}
1610
1611/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001612 * Remove local variables above "new_top".
1613 */
1614 static void
1615unwind_locals(cctx_T *cctx, int new_top)
1616{
1617 if (cctx->ctx_locals.ga_len > new_top)
1618 {
1619 int idx;
1620 lvar_T *lvar;
1621
1622 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
1623 {
1624 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
1625 vim_free(lvar->lv_name);
1626 }
1627 }
1628 cctx->ctx_locals.ga_len = new_top;
1629}
1630
1631/*
1632 * Free all local variables.
1633 */
1634 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001635free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001636{
1637 unwind_locals(cctx, 0);
1638 ga_clear(&cctx->ctx_locals);
1639}
1640
1641/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001642 * Find "name" in script-local items of script "sid".
1643 * Returns the index in "sn_var_vals" if found.
1644 * If found but not in "sn_var_vals" returns -1.
1645 * If not found returns -2.
1646 */
1647 int
1648get_script_item_idx(int sid, char_u *name, int check_writable)
1649{
1650 hashtab_T *ht;
1651 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001652 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001653 int idx;
1654
1655 // First look the name up in the hashtable.
1656 if (sid <= 0 || sid > script_items.ga_len)
1657 return -1;
1658 ht = &SCRIPT_VARS(sid);
1659 di = find_var_in_ht(ht, 0, name, TRUE);
1660 if (di == NULL)
1661 return -2;
1662
1663 // Now find the svar_T index in sn_var_vals.
1664 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
1665 {
1666 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1667
1668 if (sv->sv_tv == &di->di_tv)
1669 {
1670 if (check_writable && sv->sv_const)
1671 semsg(_(e_readonlyvar), name);
1672 return idx;
1673 }
1674 }
1675 return -1;
1676}
1677
1678/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001679 * Find "name" in imported items of the current script or in "cctx" if not
1680 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681 */
1682 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001683find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001685 int idx;
1686
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02001687 if (current_sctx.sc_sid <= 0)
1688 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689 if (cctx != NULL)
1690 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1691 {
1692 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
1693 + idx;
1694
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001695 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1696 : STRLEN(import->imp_name) == len
1697 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001698 return import;
1699 }
1700
Bram Moolenaarefa94442020-08-08 22:16:00 +02001701 return find_imported_in_script(name, len, current_sctx.sc_sid);
1702}
1703
1704 imported_T *
1705find_imported_in_script(char_u *name, size_t len, int sid)
1706{
1707 scriptitem_T *si = SCRIPT_ITEM(sid);
1708 int idx;
1709
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001710 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
1711 {
1712 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
1713
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001714 if (len == 0 ? STRCMP(name, import->imp_name) == 0
1715 : STRLEN(import->imp_name) == len
1716 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001717 return import;
1718 }
1719 return NULL;
1720}
1721
1722/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01001723 * Free all imported variables.
1724 */
1725 static void
1726free_imported(cctx_T *cctx)
1727{
1728 int idx;
1729
1730 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
1731 {
1732 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
1733
1734 vim_free(import->imp_name);
1735 }
1736 ga_clear(&cctx->ctx_imports);
1737}
1738
1739/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02001740 * Return TRUE if "p" points at a "#" but not at "#{".
1741 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001742 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001743vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001744{
1745 return p[0] == '#' && p[1] != '{';
1746}
1747
1748/*
1749 * Return a pointer to the next line that isn't empty or only contains a
1750 * comment. Skips over white space.
1751 * Returns NULL if there is none.
1752 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001753 char_u *
1754peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02001755{
1756 int lnum = cctx->ctx_lnum;
1757
1758 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
1759 {
1760 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02001761 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001762
Bram Moolenaarba60cc42020-08-12 19:15:33 +02001763 // ignore NULLs inserted for continuation lines
1764 if (line != NULL)
1765 {
1766 p = skipwhite(line);
1767 if (*p != NUL && !vim9_comment_start(p))
1768 return p;
1769 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02001770 }
1771 return NULL;
1772}
1773
1774/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001775 * Called when checking for a following operator at "arg". When the rest of
1776 * the line is empty or only a comment, peek the next line. If there is a next
1777 * line return a pointer to it and set "nextp".
1778 * Otherwise skip over white space.
1779 */
1780 static char_u *
1781may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
1782{
1783 char_u *p = skipwhite(arg);
1784
1785 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001786 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001787 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001788 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02001789 if (*nextp != NULL)
1790 return *nextp;
1791 }
1792 return p;
1793}
1794
1795/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02001796 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02001797 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02001798 * Returns NULL when at the end.
1799 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001800 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02001801next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02001802{
Bram Moolenaar7a092242020-04-16 22:10:49 +02001803 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001804
1805 do
1806 {
1807 ++cctx->ctx_lnum;
1808 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02001809 {
1810 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02001811 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02001812 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02001813 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02001814 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001815 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02001816 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02001817 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02001818 return line;
1819}
1820
1821/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001822 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02001823 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001824 * Return FAIL if beyond the last line, "*arg" is unmodified then.
1825 */
1826 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02001827may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001828{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001829 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001830 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02001831 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001832
1833 if (next == NULL)
1834 return FAIL;
1835 *arg = skipwhite(next);
1836 }
1837 return OK;
1838}
1839
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001840/*
1841 * Idem, and give an error when failed.
1842 */
1843 static int
1844may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
1845{
1846 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1847 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001848 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02001849 return FAIL;
1850 }
1851 return OK;
1852}
1853
1854
Bram Moolenaara5565e42020-05-09 15:44:01 +02001855// Structure passed between the compile_expr* functions to keep track of
1856// constants that have been parsed but for which no code was produced yet. If
1857// possible expressions on these constants are applied at compile time. If
1858// that is not possible, the code to push the constants needs to be generated
1859// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02001860// Using 50 should be more than enough of 5 levels of ().
1861#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02001862typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02001863 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02001864 int pp_used; // active entries in pp_tv[]
1865} ppconst_T;
1866
Bram Moolenaar1c747212020-05-09 18:28:34 +02001867static int compile_expr0(char_u **arg, cctx_T *cctx);
1868static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1869
Bram Moolenaara5565e42020-05-09 15:44:01 +02001870/*
1871 * Generate a PUSH instruction for "tv".
1872 * "tv" will be consumed or cleared.
1873 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
1874 */
1875 static int
1876generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
1877{
1878 if (tv != NULL)
1879 {
1880 switch (tv->v_type)
1881 {
1882 case VAR_UNKNOWN:
1883 break;
1884 case VAR_BOOL:
1885 generate_PUSHBOOL(cctx, tv->vval.v_number);
1886 break;
1887 case VAR_SPECIAL:
1888 generate_PUSHSPEC(cctx, tv->vval.v_number);
1889 break;
1890 case VAR_NUMBER:
1891 generate_PUSHNR(cctx, tv->vval.v_number);
1892 break;
1893#ifdef FEAT_FLOAT
1894 case VAR_FLOAT:
1895 generate_PUSHF(cctx, tv->vval.v_float);
1896 break;
1897#endif
1898 case VAR_BLOB:
1899 generate_PUSHBLOB(cctx, tv->vval.v_blob);
1900 tv->vval.v_blob = NULL;
1901 break;
1902 case VAR_STRING:
1903 generate_PUSHS(cctx, tv->vval.v_string);
1904 tv->vval.v_string = NULL;
1905 break;
1906 default:
1907 iemsg("constant type not supported");
1908 clear_tv(tv);
1909 return FAIL;
1910 }
1911 tv->v_type = VAR_UNKNOWN;
1912 }
1913 return OK;
1914}
1915
1916/*
1917 * Generate code for any ppconst entries.
1918 */
1919 static int
1920generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
1921{
1922 int i;
1923 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001924 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001925
Bram Moolenaar9b68c822020-06-18 19:31:08 +02001926 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001927 for (i = 0; i < ppconst->pp_used; ++i)
1928 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
1929 ret = FAIL;
1930 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02001931 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02001932 return ret;
1933}
1934
1935/*
1936 * Clear ppconst constants. Used when failing.
1937 */
1938 static void
1939clear_ppconst(ppconst_T *ppconst)
1940{
1941 int i;
1942
1943 for (i = 0; i < ppconst->pp_used; ++i)
1944 clear_tv(&ppconst->pp_tv[i]);
1945 ppconst->pp_used = 0;
1946}
1947
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02001948/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001949 * Generate an instruction to load script-local variable "name", without the
1950 * leading "s:".
1951 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952 */
1953 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001954compile_load_scriptvar(
1955 cctx_T *cctx,
1956 char_u *name, // variable NUL terminated
1957 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01001958 char_u **end, // end of variable
1959 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001960{
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001961 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001962 int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
1963 imported_T *import;
1964
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001965 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001966 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01001967 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001968 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
1969 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001970 }
1971 if (idx >= 0)
1972 {
1973 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
1974
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001975 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 current_sctx.sc_sid, idx, sv->sv_type);
1977 return OK;
1978 }
1979
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01001980 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001981 if (import != NULL)
1982 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001983 if (import->imp_all)
1984 {
1985 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02001986 char_u *exp_name;
1987 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001988 ufunc_T *ufunc;
1989 type_T *type;
1990
1991 // Used "import * as Name", need to lookup the member.
1992 if (*p != '.')
1993 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001994 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01001995 return FAIL;
1996 }
1997 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01001998 if (VIM_ISWHITE(*p))
1999 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002000 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002001 return FAIL;
2002 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002003
Bram Moolenaar1c991142020-07-04 13:15:31 +02002004 // isolate one name
2005 exp_name = p;
2006 while (eval_isnamec(*p))
2007 ++p;
2008 cc = *p;
2009 *p = NUL;
2010
2011 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
2012 *p = cc;
2013 p = skipwhite(p);
2014
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002015 // TODO: what if it is a function?
2016 if (idx < 0)
2017 return FAIL;
2018 *end = p;
2019
2020 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2021 import->imp_sid,
2022 idx,
2023 type);
2024 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002025 else if (import->imp_funcname != NULL)
2026 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002027 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002028 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2029 import->imp_sid,
2030 import->imp_var_vals_idx,
2031 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002032 return OK;
2033 }
2034
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002035 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002036 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002037 return FAIL;
2038}
2039
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002040 static int
2041generate_funcref(cctx_T *cctx, char_u *name)
2042{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002043 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002044
2045 if (ufunc == NULL)
2046 return FAIL;
2047
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002048 // Need to compile any default values to get the argument types.
2049 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2050 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2051 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002052 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002053}
2054
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002055/*
2056 * Compile a variable name into a load instruction.
2057 * "end" points to just after the name.
2058 * When "error" is FALSE do not give an error when not found.
2059 */
2060 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002061compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002062{
2063 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002064 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002065 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002066 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002067 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002068
2069 if (*(*arg + 1) == ':')
2070 {
2071 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002072 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002073 {
2074 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002075
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002076 switch (**arg)
2077 {
2078 case 'g': isn_type = ISN_LOADGDICT; break;
2079 case 'w': isn_type = ISN_LOADWDICT; break;
2080 case 't': isn_type = ISN_LOADTDICT; break;
2081 case 'b': isn_type = ISN_LOADBDICT; break;
2082 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002083 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002084 goto theend;
2085 }
2086 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2087 goto theend;
2088 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002089 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002090 else
2091 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002092 isntype_T isn_type = ISN_DROP;
2093
2094 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2095 if (name == NULL)
2096 return FAIL;
2097
2098 switch (**arg)
2099 {
2100 case 'v': res = generate_LOADV(cctx, name, error);
2101 break;
2102 case 's': res = compile_load_scriptvar(cctx, name,
2103 NULL, NULL, error);
2104 break;
2105 case 'g': isn_type = ISN_LOADG; break;
2106 case 'w': isn_type = ISN_LOADW; break;
2107 case 't': isn_type = ISN_LOADT; break;
2108 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002109 default: semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002110 goto theend;
2111 }
2112 if (isn_type != ISN_DROP)
2113 {
2114 // Global, Buffer-local, Window-local and Tabpage-local
2115 // variables can be defined later, thus we don't check if it
2116 // exists, give error at runtime.
2117 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2118 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002119 }
2120 }
2121 else
2122 {
2123 size_t len = end - *arg;
2124 int idx;
2125 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002126 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002127
2128 name = vim_strnsave(*arg, end - *arg);
2129 if (name == NULL)
2130 return FAIL;
2131
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002132 if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002133 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002134 if (!gen_load_outer)
2135 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002136 }
2137 else
2138 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002139 lvar_T *lvar = lookup_local(*arg, len, cctx);
2140
2141 if (lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002142 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002143 type = lvar->lv_type;
2144 idx = lvar->lv_idx;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002145 if (lvar->lv_from_outer)
2146 gen_load_outer = TRUE;
2147 else
2148 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002149 }
2150 else
2151 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002152 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002153 // already exists in a Vim9 script or when it's imported.
2154 if (lookup_script(*arg, len, TRUE) == OK
2155 || find_imported(name, 0, cctx) != NULL)
2156 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002157
Bram Moolenaara5565e42020-05-09 15:44:01 +02002158 // When the name starts with an uppercase letter or "x:" it
2159 // can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002160 // TODO: this is just guessing
Bram Moolenaara5565e42020-05-09 15:44:01 +02002161 if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
2162 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002163 }
2164 }
2165 if (gen_load)
2166 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002167 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002168 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002169 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002170 cctx->ctx_outer_used = TRUE;
2171 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002172 }
2173
2174 *arg = end;
2175
2176theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002177 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002178 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002179 vim_free(name);
2180 return res;
2181}
2182
2183/*
2184 * Compile the argument expressions.
2185 * "arg" points to just after the "(" and is advanced to after the ")"
2186 */
2187 static int
2188compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2189{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002190 char_u *p = *arg;
2191 char_u *whitep = *arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002192
Bram Moolenaare6085c52020-04-12 20:19:16 +02002193 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002194 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002195 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2196 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002197 if (*p == ')')
2198 {
2199 *arg = p + 1;
2200 return OK;
2201 }
2202
Bram Moolenaara5565e42020-05-09 15:44:01 +02002203 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002204 return FAIL;
2205 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002206
2207 if (*p != ',' && *skipwhite(p) == ',')
2208 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002209 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002210 p = skipwhite(p);
2211 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002213 {
2214 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002215 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002216 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002217 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002218 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002219 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002220 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002221failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002222 emsg(_(e_missing_close));
2223 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002224}
2225
2226/*
2227 * Compile a function call: name(arg1, arg2)
2228 * "arg" points to "name", "arg + varlen" to the "(".
2229 * "argcount_init" is 1 for "value->method()"
2230 * Instructions:
2231 * EVAL arg1
2232 * EVAL arg2
2233 * BCALL / DCALL / UCALL
2234 */
2235 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002236compile_call(
2237 char_u **arg,
2238 size_t varlen,
2239 cctx_T *cctx,
2240 ppconst_T *ppconst,
2241 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002242{
2243 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002244 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245 int argcount = argcount_init;
2246 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002247 char_u fname_buf[FLEN_FIXED + 1];
2248 char_u *tofree = NULL;
2249 int error = FCERR_NONE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002250 ufunc_T *ufunc;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002251 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002252 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253
Bram Moolenaara5565e42020-05-09 15:44:01 +02002254 // we can evaluate "has('name')" at compile time
2255 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2256 {
2257 char_u *s = skipwhite(*arg + varlen + 1);
2258 typval_T argvars[2];
2259
2260 argvars[0].v_type = VAR_UNKNOWN;
2261 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002262 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002263 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002264 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002265 s = skipwhite(s);
2266 if (*s == ')' && argvars[0].v_type == VAR_STRING)
2267 {
2268 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2269
2270 *arg = s + 1;
2271 argvars[1].v_type = VAR_UNKNOWN;
2272 tv->v_type = VAR_NUMBER;
2273 tv->vval.v_number = 0;
2274 f_has(argvars, tv);
2275 clear_tv(&argvars[0]);
2276 ++ppconst->pp_used;
2277 return OK;
2278 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002279 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002280 }
2281
2282 if (generate_ppconst(cctx, ppconst) == FAIL)
2283 return FAIL;
2284
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285 if (varlen >= sizeof(namebuf))
2286 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002287 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002288 return FAIL;
2289 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002290 vim_strncpy(namebuf, *arg, varlen);
2291 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002292
2293 *arg = skipwhite(*arg + varlen + 1);
2294 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002295 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002296
Bram Moolenaara1773442020-08-12 15:21:22 +02002297 is_autoload = vim_strchr(name, '#') != NULL;
2298 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299 {
2300 int idx;
2301
2302 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002303 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002304 if (idx >= 0)
Bram Moolenaar389df252020-07-09 21:20:47 +02002305 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002306 else
2307 semsg(_(e_unknownfunc), namebuf);
2308 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002309 }
2310
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311 // If we can find the function by name generate the right call.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002312 ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 if (ufunc != NULL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002314 {
2315 res = generate_CALL(cctx, ufunc, argcount);
2316 goto theend;
2317 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002318
2319 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002320 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002321 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002323 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaara26b9702020-04-18 19:53:28 +02002324 && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002325 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002326 garray_T *stack = &cctx->ctx_type_stack;
2327 type_T *type;
2328
2329 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2330 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002331 goto theend;
2332 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002334 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002335 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002336 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002337 res = generate_UCALL(cctx, name, argcount);
2338 else
2339 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002340
2341theend:
2342 vim_free(tofree);
2343 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002344}
2345
2346// like NAMESPACE_CHAR but with 'a' and 'l'.
2347#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2348
2349/*
2350 * Find the end of a variable or function name. Unlike find_name_end() this
2351 * does not recognize magic braces.
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002352 * When "namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353 * Return a pointer to just after the name. Equal to "arg" if there is no
2354 * valid name.
2355 */
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002356 static char_u *
2357to_name_end(char_u *arg, int namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002358{
2359 char_u *p;
2360
2361 // Quick check for valid starting character.
2362 if (!eval_isnamec1(*arg))
2363 return arg;
2364
2365 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2366 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2367 // and can be used in slice "[n:]".
2368 if (*p == ':' && (p != arg + 1
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002369 || !namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002370 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2371 break;
2372 return p;
2373}
2374
2375/*
2376 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002377 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002378 */
2379 char_u *
2380to_name_const_end(char_u *arg)
2381{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002382 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002383 typval_T rettv;
2384
2385 if (p == arg && *arg == '[')
2386 {
2387
2388 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002389 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390 p = arg;
2391 }
2392 else if (p == arg && *arg == '#' && arg[1] == '{')
2393 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002394 // Can be "#{a: 1}->Func()".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395 ++p;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002396 if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397 p = arg;
2398 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002399
2400 return p;
2401}
2402
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403/*
2404 * parse a list: [expr, expr]
2405 * "*arg" points to the '['.
2406 */
2407 static int
2408compile_list(char_u **arg, cctx_T *cctx)
2409{
2410 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002411 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002412 int count = 0;
2413
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002414 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002415 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002416 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002417 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002418 semsg(_(e_list_end), *arg);
2419 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002420 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002421 if (*p == ',')
2422 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002423 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002424 return FAIL;
2425 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002426 if (*p == ']')
2427 {
2428 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002429 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002430 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02002431 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432 break;
2433 ++count;
2434 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002435 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002436 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002437 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2438 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002439 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002440 return FAIL;
2441 }
2442 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002443 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002444 p = skipwhite(p);
2445 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002446 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447
2448 generate_NEWLIST(cctx, count);
2449 return OK;
2450}
2451
2452/*
2453 * parse a lambda: {arg, arg -> expr}
2454 * "*arg" points to the '{'.
2455 */
2456 static int
2457compile_lambda(char_u **arg, cctx_T *cctx)
2458{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002459 typval_T rettv;
2460 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002461 evalarg_T evalarg;
2462
2463 CLEAR_FIELD(evalarg);
2464 evalarg.eval_flags = EVAL_EVALUATE;
2465 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466
2467 // Get the funcref in "rettv".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002468 if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002469 return FAIL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002470
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002472 ++ufunc->uf_refcount;
2473 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002474 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475
2476 // The function will have one line: "return {expr}".
2477 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002478 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002480 clear_evalarg(&evalarg, NULL);
2481
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002482 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002483 {
2484 // The return type will now be known.
2485 set_function_type(ufunc);
2486
2487 return generate_FUNCREF(cctx, ufunc);
2488 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002489
2490 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002491 return FAIL;
2492}
2493
2494/*
2495 * Compile a lamda call: expr->{lambda}(args)
2496 * "arg" points to the "{".
2497 */
2498 static int
2499compile_lambda_call(char_u **arg, cctx_T *cctx)
2500{
2501 ufunc_T *ufunc;
2502 typval_T rettv;
2503 int argcount = 1;
2504 int ret = FAIL;
2505
2506 // Get the funcref in "rettv".
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002507 if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002508 return FAIL;
2509
2510 if (**arg != '(')
2511 {
2512 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002513 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 else
2515 semsg(_(e_missing_paren), "lambda");
2516 clear_tv(&rettv);
2517 return FAIL;
2518 }
2519
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520 ufunc = rettv.vval.v_partial->pt_func;
2521 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002522 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002523 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002524
2525 // The function will have one line: "return {expr}".
2526 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002527 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528
2529 // compile the arguments
2530 *arg = skipwhite(*arg + 1);
2531 if (compile_arguments(arg, cctx, &argcount) == OK)
2532 // call the compiled function
2533 ret = generate_CALL(cctx, ufunc, argcount);
2534
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002535 if (ret == FAIL)
2536 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002537 return ret;
2538}
2539
2540/*
2541 * parse a dict: {'key': val} or #{key: val}
2542 * "*arg" points to the '{'.
2543 */
2544 static int
2545compile_dict(char_u **arg, cctx_T *cctx, int literal)
2546{
2547 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002548 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 int count = 0;
2550 dict_T *d = dict_alloc();
2551 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002552 char_u *whitep = *arg;
2553 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554
2555 if (d == NULL)
2556 return FAIL;
2557 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002558 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002559 {
2560 char_u *key = NULL;
2561
Bram Moolenaar23c55272020-06-21 16:58:13 +02002562 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002563 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002564 *arg = NULL;
2565 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002566 }
2567
2568 if (**arg == '}')
2569 break;
2570
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 if (literal)
2572 {
Bram Moolenaar2c330432020-04-13 14:41:35 +02002573 char_u *end = to_name_end(*arg, !literal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574
Bram Moolenaar2c330432020-04-13 14:41:35 +02002575 if (end == *arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002576 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002577 semsg(_(e_invalid_key_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578 return FAIL;
2579 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002580 key = vim_strnsave(*arg, end - *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581 if (generate_PUSHS(cctx, key) == FAIL)
2582 return FAIL;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002583 *arg = end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584 }
2585 else
2586 {
2587 isn_T *isn;
2588
Bram Moolenaara5565e42020-05-09 15:44:01 +02002589 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2592 if (isn->isn_type == ISN_PUSHS)
2593 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02002594 else
2595 {
2596 type_T *keytype = ((type_T **)stack->ga_data)
2597 [stack->ga_len - 1];
2598 if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
2599 return FAIL;
2600 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601 }
2602
2603 // Check for duplicate keys, if using string keys.
2604 if (key != NULL)
2605 {
2606 item = dict_find(d, key, -1);
2607 if (item != NULL)
2608 {
2609 semsg(_(e_duplicate_key), key);
2610 goto failret;
2611 }
2612 item = dictitem_alloc(key);
2613 if (item != NULL)
2614 {
2615 item->di_tv.v_type = VAR_UNKNOWN;
2616 item->di_tv.v_lock = 0;
2617 if (dict_add(d, item) == FAIL)
2618 dictitem_free(item);
2619 }
2620 }
2621
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002622 if (**arg != ':')
2623 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002624 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002625 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02002626 else
2627 semsg(_(e_missing_dict_colon), *arg);
2628 return FAIL;
2629 }
2630 whitep = *arg + 1;
2631 if (!IS_WHITE_OR_NUL(*whitep))
2632 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002633 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 return FAIL;
2635 }
2636
2637 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002638 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002639 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002640 *arg = NULL;
2641 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002642 }
2643
Bram Moolenaara5565e42020-05-09 15:44:01 +02002644 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645 return FAIL;
2646 ++count;
2647
Bram Moolenaar2c330432020-04-13 14:41:35 +02002648 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002649 *arg = skipwhite(*arg);
2650 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002651 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002652 *arg = NULL;
2653 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002654 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655 if (**arg == '}')
2656 break;
2657 if (**arg != ',')
2658 {
2659 semsg(_(e_missing_dict_comma), *arg);
2660 goto failret;
2661 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002662 if (IS_WHITE_OR_NUL(*whitep))
2663 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002664 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02002665 return FAIL;
2666 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002667 whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 *arg = skipwhite(*arg + 1);
2669 }
2670
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002671 *arg = *arg + 1;
2672
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002673 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002674 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002675 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002676 *arg += STRLEN(*arg);
2677
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002678 dict_unref(d);
2679 return generate_NEWDICT(cctx, count);
2680
2681failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002682 if (*arg == NULL)
2683 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 dict_unref(d);
2685 return FAIL;
2686}
2687
2688/*
2689 * Compile "&option".
2690 */
2691 static int
2692compile_get_option(char_u **arg, cctx_T *cctx)
2693{
2694 typval_T rettv;
2695 char_u *start = *arg;
2696 int ret;
2697
2698 // parse the option and get the current value to get the type.
2699 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002700 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002701 if (ret == OK)
2702 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002703 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704 char_u *name = vim_strnsave(start, *arg - start);
2705 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
2706
2707 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
2708 vim_free(name);
2709 }
2710 clear_tv(&rettv);
2711
2712 return ret;
2713}
2714
2715/*
2716 * Compile "$VAR".
2717 */
2718 static int
2719compile_get_env(char_u **arg, cctx_T *cctx)
2720{
2721 char_u *start = *arg;
2722 int len;
2723 int ret;
2724 char_u *name;
2725
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002726 ++*arg;
2727 len = get_env_len(arg);
2728 if (len == 0)
2729 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002730 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002731 return FAIL;
2732 }
2733
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002734 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002735 name = vim_strnsave(start, len + 1);
2736 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
2737 vim_free(name);
2738 return ret;
2739}
2740
2741/*
2742 * Compile "@r".
2743 */
2744 static int
2745compile_get_register(char_u **arg, cctx_T *cctx)
2746{
2747 int ret;
2748
2749 ++*arg;
2750 if (**arg == NUL)
2751 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002752 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002753 return FAIL;
2754 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02002755 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756 {
2757 emsg_invreg(**arg);
2758 return FAIL;
2759 }
2760 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
2761 ++*arg;
2762 return ret;
2763}
2764
2765/*
2766 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002767 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 */
2769 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002770apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002772 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773
2774 // this works from end to start
2775 while (p > start)
2776 {
2777 --p;
2778 if (*p == '-' || *p == '+')
2779 {
2780 // only '-' has an effect, for '+' we only check the type
2781#ifdef FEAT_FLOAT
2782 if (rettv->v_type == VAR_FLOAT)
2783 {
2784 if (*p == '-')
2785 rettv->vval.v_float = -rettv->vval.v_float;
2786 }
2787 else
2788#endif
2789 {
2790 varnumber_T val;
2791 int error = FALSE;
2792
2793 // tv_get_number_chk() accepts a string, but we don't want that
2794 // here
2795 if (check_not_string(rettv) == FAIL)
2796 return FAIL;
2797 val = tv_get_number_chk(rettv, &error);
2798 clear_tv(rettv);
2799 if (error)
2800 return FAIL;
2801 if (*p == '-')
2802 val = -val;
2803 rettv->v_type = VAR_NUMBER;
2804 rettv->vval.v_number = val;
2805 }
2806 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002807 else if (numeric_only)
2808 {
2809 ++p;
2810 break;
2811 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002812 else
2813 {
2814 int v = tv2bool(rettv);
2815
2816 // '!' is permissive in the type.
2817 clear_tv(rettv);
2818 rettv->v_type = VAR_BOOL;
2819 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
2820 }
2821 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002822 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002823 return OK;
2824}
2825
2826/*
2827 * Recognize v: variables that are constants and set "rettv".
2828 */
2829 static void
2830get_vim_constant(char_u **arg, typval_T *rettv)
2831{
2832 if (STRNCMP(*arg, "v:true", 6) == 0)
2833 {
2834 rettv->v_type = VAR_BOOL;
2835 rettv->vval.v_number = VVAL_TRUE;
2836 *arg += 6;
2837 }
2838 else if (STRNCMP(*arg, "v:false", 7) == 0)
2839 {
2840 rettv->v_type = VAR_BOOL;
2841 rettv->vval.v_number = VVAL_FALSE;
2842 *arg += 7;
2843 }
2844 else if (STRNCMP(*arg, "v:null", 6) == 0)
2845 {
2846 rettv->v_type = VAR_SPECIAL;
2847 rettv->vval.v_number = VVAL_NULL;
2848 *arg += 6;
2849 }
2850 else if (STRNCMP(*arg, "v:none", 6) == 0)
2851 {
2852 rettv->v_type = VAR_SPECIAL;
2853 rettv->vval.v_number = VVAL_NONE;
2854 *arg += 6;
2855 }
2856}
2857
Bram Moolenaar696ba232020-07-29 21:20:41 +02002858 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02002859get_compare_type(char_u *p, int *len, int *type_is)
2860{
2861 exptype_T type = EXPR_UNKNOWN;
2862 int i;
2863
2864 switch (p[0])
2865 {
2866 case '=': if (p[1] == '=')
2867 type = EXPR_EQUAL;
2868 else if (p[1] == '~')
2869 type = EXPR_MATCH;
2870 break;
2871 case '!': if (p[1] == '=')
2872 type = EXPR_NEQUAL;
2873 else if (p[1] == '~')
2874 type = EXPR_NOMATCH;
2875 break;
2876 case '>': if (p[1] != '=')
2877 {
2878 type = EXPR_GREATER;
2879 *len = 1;
2880 }
2881 else
2882 type = EXPR_GEQUAL;
2883 break;
2884 case '<': if (p[1] != '=')
2885 {
2886 type = EXPR_SMALLER;
2887 *len = 1;
2888 }
2889 else
2890 type = EXPR_SEQUAL;
2891 break;
2892 case 'i': if (p[1] == 's')
2893 {
2894 // "is" and "isnot"; but not a prefix of a name
2895 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2896 *len = 5;
2897 i = p[*len];
2898 if (!isalnum(i) && i != '_')
2899 {
2900 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
2901 *type_is = TRUE;
2902 }
2903 }
2904 break;
2905 }
2906 return type;
2907}
2908
2909/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002911 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 */
2913 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002914compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002916 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917
2918 // this works from end to start
2919 while (p > start)
2920 {
2921 --p;
2922 if (*p == '-' || *p == '+')
2923 {
2924 int negate = *p == '-';
2925 isn_T *isn;
2926
2927 // TODO: check type
2928 while (p > start && (p[-1] == '-' || p[-1] == '+'))
2929 {
2930 --p;
2931 if (*p == '-')
2932 negate = !negate;
2933 }
2934 // only '-' has an effect, for '+' we only check the type
2935 if (negate)
2936 isn = generate_instr(cctx, ISN_NEGATENR);
2937 else
2938 isn = generate_instr(cctx, ISN_CHECKNR);
2939 if (isn == NULL)
2940 return FAIL;
2941 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002942 else if (numeric_only)
2943 {
2944 ++p;
2945 break;
2946 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 else
2948 {
2949 int invert = TRUE;
2950
2951 while (p > start && p[-1] == '!')
2952 {
2953 --p;
2954 invert = !invert;
2955 }
2956 if (generate_2BOOL(cctx, invert) == FAIL)
2957 return FAIL;
2958 }
2959 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002960 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 return OK;
2962}
2963
2964/*
2965 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002966 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 */
2968 static int
2969compile_subscript(
2970 char_u **arg,
2971 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002972 char_u *start_leader,
2973 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02002974 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02002976 char_u *name_start = *end_leader;
2977
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978 for (;;)
2979 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002980 char_u *p = skipwhite(*arg);
2981
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002982 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002983 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002984 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002985
2986 // If a following line starts with "->{" or "->X" advance to that
2987 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002988 // Also if a following line starts with ".x".
2989 if (next != NULL &&
2990 ((next[0] == '-' && next[1] == '>'
2991 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02002992 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02002993 {
2994 next = next_line_from_context(cctx, TRUE);
2995 if (next == NULL)
2996 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002997 *arg = next;
2998 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02002999 }
3000 }
3001
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003002 // Do not skip over white space to find the "(", "exeucte 'x' ()" is
3003 // not a function call.
3004 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003006 garray_T *stack = &cctx->ctx_type_stack;
3007 type_T *type;
3008 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003010 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003011 return FAIL;
3012
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003014 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3015
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003016 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3018 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003019 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 return FAIL;
3021 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003022 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003024 char_u *pstart = p;
3025
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003026 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003027 return FAIL;
3028
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 // something->method()
3030 // Apply the '!', '-' and '+' first:
3031 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003032 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003035 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003036 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003037 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 if (**arg == '{')
3039 {
3040 // lambda call: list->{lambda}
3041 if (compile_lambda_call(arg, cctx) == FAIL)
3042 return FAIL;
3043 }
3044 else
3045 {
3046 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003047 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003048 if (!eval_isnamec1(*p))
3049 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003050 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003051 return FAIL;
3052 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003053 if (ASCII_ISALPHA(*p) && p[1] == ':')
3054 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003055 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 ;
3057 if (*p != '(')
3058 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003059 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060 return FAIL;
3061 }
3062 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003063 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 return FAIL;
3065 }
3066 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003067 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003069 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003070 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003071 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003072 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003073 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003074
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003075 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003076 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003077 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003078 // TODO: blob index
3079 // TODO: more arguments
3080 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003081 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003082 return FAIL;
3083
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003084 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003085 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003086 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003087 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003088 if (**arg == ':')
3089 // missing first index is equal to zero
3090 generate_PUSHNR(cctx, 0);
3091 else
3092 {
3093 if (compile_expr0(arg, cctx) == FAIL)
3094 return FAIL;
3095 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3096 return FAIL;
3097 *arg = skipwhite(*arg);
3098 }
3099 if (**arg == ':')
3100 {
3101 *arg = skipwhite(*arg + 1);
3102 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3103 return FAIL;
3104 if (**arg == ']')
3105 // missing second index is equal to end of string
3106 generate_PUSHNR(cctx, -1);
3107 else
3108 {
3109 if (compile_expr0(arg, cctx) == FAIL)
3110 return FAIL;
3111 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3112 return FAIL;
3113 *arg = skipwhite(*arg);
3114 }
3115 is_slice = TRUE;
3116 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117
3118 if (**arg != ']')
3119 {
3120 emsg(_(e_missbrac));
3121 return FAIL;
3122 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003123 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003124
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003125 // We can index a list and a dict. If we don't know the type
3126 // we can use the index value type.
3127 // TODO: If we don't know use an instruction to figure it out at
3128 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003129 typep = ((type_T **)stack->ga_data) + stack->ga_len
3130 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003131 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003132 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3133 // If the index is a string, the variable must be a Dict.
3134 if (*typep == &t_any && valtype == &t_string)
3135 vtype = VAR_DICT;
3136 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003137 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003138 if (need_type(valtype, &t_number, -1, cctx, FALSE) == FAIL)
3139 return FAIL;
3140 if (is_slice)
3141 {
3142 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
3143 if (need_type(valtype, &t_number, -2, cctx, FALSE) == FAIL)
3144 return FAIL;
3145 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003146 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003147
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003148 if (vtype == VAR_DICT)
3149 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003150 if (is_slice)
3151 {
3152 emsg(_(e_cannot_slice_dictionary));
3153 return FAIL;
3154 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003155 if ((*typep)->tt_type == VAR_DICT)
3156 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003157 else
3158 {
3159 if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL)
3160 return FAIL;
3161 *typep = &t_any;
3162 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003163 if (may_generate_2STRING(-1, cctx) == FAIL)
3164 return FAIL;
3165 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3166 return FAIL;
3167 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003168 else if (vtype == VAR_STRING)
3169 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003170 *typep = &t_string;
3171 if ((is_slice
3172 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3173 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003174 return FAIL;
3175 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003176 else if (vtype == VAR_BLOB)
3177 {
3178 emsg("Sorry, blob index and slice not implemented yet");
3179 return FAIL;
3180 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003181 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003182 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003183 if (is_slice)
3184 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003185 if (generate_instr_drop(cctx,
3186 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3187 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003188 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003189 }
Bram Moolenaared591872020-08-15 22:14:53 +02003190 else
3191 {
3192 if ((*typep)->tt_type == VAR_LIST)
3193 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003194 if (generate_instr_drop(cctx,
3195 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3196 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003197 return FAIL;
3198 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003199 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003200 else
3201 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003202 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003203 return FAIL;
3204 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003206 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003208 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003209 return FAIL;
3210
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003211 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003212 if (may_get_next_line(*arg, arg, cctx) == FAIL)
3213 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 // dictionary member: dict.name
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003215 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003216 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217 while (eval_isnamec(*p))
3218 MB_PTR_ADV(p);
3219 if (p == *arg)
3220 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003221 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 return FAIL;
3223 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003224 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 return FAIL;
3226 *arg = p;
3227 }
3228 else
3229 break;
3230 }
3231
3232 // TODO - see handle_subscript():
3233 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3234 // Don't do this when "Func" is already a partial that was bound
3235 // explicitly (pt_auto is FALSE).
3236
3237 return OK;
3238}
3239
3240/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003241 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3242 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243 *
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003244 * If the value is a constant "ppconst->pp_ret" will be set.
3245 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003246 *
3247 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 */
3249
3250/*
3251 * number number constant
3252 * 0zFFFFFFFF Blob constant
3253 * "string" string constant
3254 * 'string' literal string constant
3255 * &option-name option value
3256 * @r register contents
3257 * identifier variable value
3258 * function() function call
3259 * $VAR environment variable
3260 * (expression) nested expression
3261 * [expr, expr] List
3262 * {key: val, key: val} Dictionary
3263 * #{key: val, key: val} Dictionary with literal keys
3264 *
3265 * Also handle:
3266 * ! in front logical NOT
3267 * - in front unary minus
3268 * + in front unary plus (ignored)
3269 * trailing (arg) funcref/partial call
3270 * trailing [] subscript in String or List
3271 * trailing .name entry in Dictionary
3272 * trailing ->name() method call
3273 */
3274 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003275compile_expr7(
3276 char_u **arg,
3277 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003278 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280 char_u *start_leader, *end_leader;
3281 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003282 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003283 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284
3285 /*
3286 * Skip '!', '-' and '+' characters. They are handled later.
3287 */
3288 start_leader = *arg;
3289 while (**arg == '!' || **arg == '-' || **arg == '+')
3290 *arg = skipwhite(*arg + 1);
3291 end_leader = *arg;
3292
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003293 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 switch (**arg)
3295 {
3296 /*
3297 * Number constant.
3298 */
3299 case '0': // also for blob starting with 0z
3300 case '1':
3301 case '2':
3302 case '3':
3303 case '4':
3304 case '5':
3305 case '6':
3306 case '7':
3307 case '8':
3308 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003309 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003311 // Apply "-" and "+" just before the number now, right to
3312 // left. Matters especially when "->" follows. Stops at
3313 // '!'.
3314 if (apply_leader(rettv, TRUE,
3315 start_leader, &end_leader) == FAIL)
3316 {
3317 clear_tv(rettv);
3318 return FAIL;
3319 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320 break;
3321
3322 /*
3323 * String constant: "string".
3324 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003325 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003326 return FAIL;
3327 break;
3328
3329 /*
3330 * Literal string constant: 'str''ing'.
3331 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003332 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333 return FAIL;
3334 break;
3335
3336 /*
3337 * Constant Vim variable.
3338 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003339 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340 ret = NOTDONE;
3341 break;
3342
3343 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003344 * "true" constant
3345 */
3346 case 't': if (STRNCMP(*arg, "true", 4) == 0
3347 && !eval_isnamec((*arg)[4]))
3348 {
3349 *arg += 4;
3350 rettv->v_type = VAR_BOOL;
3351 rettv->vval.v_number = VVAL_TRUE;
3352 }
3353 else
3354 ret = NOTDONE;
3355 break;
3356
3357 /*
3358 * "false" constant
3359 */
3360 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3361 && !eval_isnamec((*arg)[5]))
3362 {
3363 *arg += 5;
3364 rettv->v_type = VAR_BOOL;
3365 rettv->vval.v_number = VVAL_FALSE;
3366 }
3367 else
3368 ret = NOTDONE;
3369 break;
3370
3371 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003372 * List: [expr, expr]
3373 */
3374 case '[': ret = compile_list(arg, cctx);
3375 break;
3376
3377 /*
3378 * Dictionary: #{key: val, key: val}
3379 */
3380 case '#': if ((*arg)[1] == '{')
3381 {
3382 ++*arg;
3383 ret = compile_dict(arg, cctx, TRUE);
3384 }
3385 else
3386 ret = NOTDONE;
3387 break;
3388
3389 /*
3390 * Lambda: {arg, arg -> expr}
3391 * Dictionary: {'key': val, 'key': val}
3392 */
3393 case '{': {
3394 char_u *start = skipwhite(*arg + 1);
3395
3396 // Find out what comes after the arguments.
3397 ret = get_function_args(&start, '-', NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003398 NULL, NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 if (ret != FAIL && *start == '>')
3400 ret = compile_lambda(arg, cctx);
3401 else
3402 ret = compile_dict(arg, cctx, FALSE);
3403 }
3404 break;
3405
3406 /*
3407 * Option value: &name
3408 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003409 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3410 return FAIL;
3411 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412 break;
3413
3414 /*
3415 * Environment variable: $VAR.
3416 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003417 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3418 return FAIL;
3419 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 break;
3421
3422 /*
3423 * Register contents: @r.
3424 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003425 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3426 return FAIL;
3427 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428 break;
3429 /*
3430 * nested expression: (expression).
3431 */
3432 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003433
3434 // recursive!
3435 if (ppconst->pp_used <= PPSIZE - 10)
3436 {
3437 ret = compile_expr1(arg, cctx, ppconst);
3438 }
3439 else
3440 {
3441 // Not enough space in ppconst, flush constants.
3442 if (generate_ppconst(cctx, ppconst) == FAIL)
3443 return FAIL;
3444 ret = compile_expr0(arg, cctx);
3445 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 *arg = skipwhite(*arg);
3447 if (**arg == ')')
3448 ++*arg;
3449 else if (ret == OK)
3450 {
3451 emsg(_(e_missing_close));
3452 ret = FAIL;
3453 }
3454 break;
3455
3456 default: ret = NOTDONE;
3457 break;
3458 }
3459 if (ret == FAIL)
3460 return FAIL;
3461
Bram Moolenaar1c747212020-05-09 18:28:34 +02003462 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003464 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003465 clear_tv(rettv);
3466 else
3467 // A constant expression can possibly be handled compile time,
3468 // return the value instead of generating code.
3469 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003470 }
3471 else if (ret == NOTDONE)
3472 {
3473 char_u *p;
3474 int r;
3475
3476 if (!eval_isnamec1(**arg))
3477 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003478 semsg(_(e_name_expected), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479 return FAIL;
3480 }
3481
3482 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003483 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02003485 {
3486 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
3487 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003488 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02003489 {
3490 if (generate_ppconst(cctx, ppconst) == FAIL)
3491 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003492 r = compile_load(arg, p, cctx, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003493 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494 if (r == FAIL)
3495 return FAIL;
3496 }
3497
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003498 // Handle following "[]", ".member", etc.
3499 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003500 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02003501 ppconst) == FAIL)
3502 return FAIL;
3503 if (ppconst->pp_used > 0)
3504 {
3505 // apply the '!', '-' and '+' before the constant
3506 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003507 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003508 return FAIL;
3509 return OK;
3510 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003511 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02003513 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514}
3515
3516/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003517 * Give the "white on both sides" error, taking the operator from "p[len]".
3518 */
3519 void
3520error_white_both(char_u *op, int len)
3521{
3522 char_u buf[10];
3523
3524 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003525 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003526}
3527
3528/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003529 * <type>expr7: runtime type check / conversion
3530 */
3531 static int
3532compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3533{
3534 type_T *want_type = NULL;
3535
3536 // Recognize <type>
3537 if (**arg == '<' && eval_isnamec1((*arg)[1]))
3538 {
3539 int called_emsg_before = called_emsg;
3540
3541 ++*arg;
3542 want_type = parse_type(arg, cctx->ctx_type_list);
3543 if (called_emsg != called_emsg_before)
3544 return FAIL;
3545
3546 if (**arg != '>')
3547 {
3548 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003549 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003550 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003551 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003552 return FAIL;
3553 }
3554 ++*arg;
3555 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
3556 return FAIL;
3557 }
3558
3559 if (compile_expr7(arg, cctx, ppconst) == FAIL)
3560 return FAIL;
3561
3562 if (want_type != NULL)
3563 {
3564 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02003565 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003566
Bram Moolenaard1103582020-08-14 22:44:25 +02003567 generate_ppconst(cctx, ppconst);
3568 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003569 if (check_type(want_type, actual, FALSE) == FAIL)
3570 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003571 if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
3572 return FAIL;
3573 }
3574 }
3575
3576 return OK;
3577}
3578
3579/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003580 * * number multiplication
3581 * / number division
3582 * % number modulo
3583 */
3584 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003585compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003586{
3587 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003588 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003589 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003591 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003592 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593 return FAIL;
3594
3595 /*
3596 * Repeat computing, until no "*", "/" or "%" is following.
3597 */
3598 for (;;)
3599 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003600 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601 if (*op != '*' && *op != '/' && *op != '%')
3602 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003603 if (next != NULL)
3604 {
3605 *arg = next_line_from_context(cctx, TRUE);
3606 op = skipwhite(*arg);
3607 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003608
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003609 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003611 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003612 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003613 }
3614 *arg = skipwhite(op + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003615 if (may_get_next_line(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003616 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003617
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003618 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02003619 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003621
3622 if (ppconst->pp_used == ppconst_used + 2
3623 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3624 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003625 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003626 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3627 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003628 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003630 // both are numbers: compute the result
3631 switch (*op)
3632 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003633 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003634 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003635 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003636 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003637 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003638 break;
3639 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003640 tv1->vval.v_number = res;
3641 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003642 }
3643 else
3644 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003645 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003646 generate_two_op(cctx, op);
3647 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648 }
3649
3650 return OK;
3651}
3652
3653/*
3654 * + number addition
3655 * - number subtraction
3656 * .. string concatenation
3657 */
3658 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003659compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660{
3661 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003662 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003664 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665
3666 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003667 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668 return FAIL;
3669
3670 /*
3671 * Repeat computing, until no "+", "-" or ".." is following.
3672 */
3673 for (;;)
3674 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003675 op = may_peek_next_line(cctx, *arg, &next);
3676 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677 break;
3678 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003679 if (next != NULL)
3680 {
3681 *arg = next_line_from_context(cctx, TRUE);
3682 op = skipwhite(*arg);
3683 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003685 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003687 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003688 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689 }
3690
3691 *arg = skipwhite(op + oplen);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003692 if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003693 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003695 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003696 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 return FAIL;
3698
Bram Moolenaara5565e42020-05-09 15:44:01 +02003699 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003700 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02003701 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
3702 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
3703 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
3704 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003706 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
3707 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003708
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003709 // concat/subtract/add constant numbers
3710 if (*op == '+')
3711 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
3712 else if (*op == '-')
3713 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
3714 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003715 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003716 // concatenate constant strings
3717 char_u *s1 = tv1->vval.v_string;
3718 char_u *s2 = tv2->vval.v_string;
3719 size_t len1 = STRLEN(s1);
3720
3721 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
3722 if (tv1->vval.v_string == NULL)
3723 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003724 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003725 return FAIL;
3726 }
3727 mch_memmove(tv1->vval.v_string, s1, len1);
3728 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003729 vim_free(s1);
3730 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003731 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003732 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 }
3734 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003735 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003736 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003737 if (*op == '.')
3738 {
3739 if (may_generate_2STRING(-2, cctx) == FAIL
3740 || may_generate_2STRING(-1, cctx) == FAIL)
3741 return FAIL;
3742 generate_instr_drop(cctx, ISN_CONCAT, 1);
3743 }
3744 else
3745 generate_two_op(cctx, op);
3746 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 }
3748
3749 return OK;
3750}
3751
3752/*
3753 * expr5a == expr5b
3754 * expr5a =~ expr5b
3755 * expr5a != expr5b
3756 * expr5a !~ expr5b
3757 * expr5a > expr5b
3758 * expr5a >= expr5b
3759 * expr5a < expr5b
3760 * expr5a <= expr5b
3761 * expr5a is expr5b
3762 * expr5a isnot expr5b
3763 *
3764 * Produces instructions:
3765 * EVAL expr5a Push result of "expr5a"
3766 * EVAL expr5b Push result of "expr5b"
3767 * COMPARE one of the compare instructions
3768 */
3769 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003770compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771{
3772 exptype_T type = EXPR_UNKNOWN;
3773 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003774 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003775 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02003777 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778
3779 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003780 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003781 return FAIL;
3782
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003783 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01003784 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003785
3786 /*
3787 * If there is a comparative operator, use it.
3788 */
3789 if (type != EXPR_UNKNOWN)
3790 {
3791 int ic = FALSE; // Default: do not ignore case
3792
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003793 if (next != NULL)
3794 {
3795 *arg = next_line_from_context(cctx, TRUE);
3796 p = skipwhite(*arg);
3797 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 if (type_is && (p[len] == '?' || p[len] == '#'))
3799 {
3800 semsg(_(e_invexpr2), *arg);
3801 return FAIL;
3802 }
3803 // extra question mark appended: ignore case
3804 if (p[len] == '?')
3805 {
3806 ic = TRUE;
3807 ++len;
3808 }
3809 // extra '#' appended: match case (ignored)
3810 else if (p[len] == '#')
3811 ++len;
3812 // nothing appended: match case
3813
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003814 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003816 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003817 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003818 }
3819
3820 // get the second variable
3821 *arg = skipwhite(p + len);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003822 if (may_get_next_line(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003823 return FAIL;
3824
Bram Moolenaara5565e42020-05-09 15:44:01 +02003825 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826 return FAIL;
3827
Bram Moolenaara5565e42020-05-09 15:44:01 +02003828 if (ppconst->pp_used == ppconst_used + 2)
3829 {
3830 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
3831 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3832 int ret;
3833
3834 // Both sides are a constant, compute the result now.
3835 // First check for a valid combination of types, this is more
3836 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003837 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003838 ret = FAIL;
3839 else
3840 {
3841 ret = typval_compare(tv1, tv2, type, ic);
3842 tv1->v_type = VAR_BOOL;
3843 tv1->vval.v_number = tv1->vval.v_number
3844 ? VVAL_TRUE : VVAL_FALSE;
3845 clear_tv(tv2);
3846 --ppconst->pp_used;
3847 }
3848 return ret;
3849 }
3850
3851 generate_ppconst(cctx, ppconst);
3852 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853 }
3854
3855 return OK;
3856}
3857
Bram Moolenaar7f141552020-05-09 17:35:53 +02003858static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3859
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860/*
3861 * Compile || or &&.
3862 */
3863 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003864compile_and_or(
3865 char_u **arg,
3866 cctx_T *cctx,
3867 char *op,
3868 ppconst_T *ppconst,
3869 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003871 char_u *next;
3872 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873 int opchar = *op;
3874
3875 if (p[0] == opchar && p[1] == opchar)
3876 {
3877 garray_T *instr = &cctx->ctx_instr;
3878 garray_T end_ga;
3879
3880 /*
3881 * Repeat until there is no following "||" or "&&"
3882 */
3883 ga_init2(&end_ga, sizeof(int), 10);
3884 while (p[0] == opchar && p[1] == opchar)
3885 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003886 if (next != NULL)
3887 {
3888 *arg = next_line_from_context(cctx, TRUE);
3889 p = skipwhite(*arg);
3890 }
3891
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003892 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3893 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003894 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003895 return FAIL;
3896 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897
Bram Moolenaara5565e42020-05-09 15:44:01 +02003898 // TODO: use ppconst if the value is a constant
3899 generate_ppconst(cctx, ppconst);
3900
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901 if (ga_grow(&end_ga, 1) == FAIL)
3902 {
3903 ga_clear(&end_ga);
3904 return FAIL;
3905 }
3906 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3907 ++end_ga.ga_len;
3908 generate_JUMP(cctx, opchar == '|'
3909 ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
3910
3911 // eval the next expression
3912 *arg = skipwhite(p + 2);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003913 if (may_get_next_line(p + 2, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02003914 return FAIL;
3915
Bram Moolenaara5565e42020-05-09 15:44:01 +02003916 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3917 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918 {
3919 ga_clear(&end_ga);
3920 return FAIL;
3921 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02003922
3923 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02003925 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926
3927 // Fill in the end label in all jumps.
3928 while (end_ga.ga_len > 0)
3929 {
3930 isn_T *isn;
3931
3932 --end_ga.ga_len;
3933 isn = ((isn_T *)instr->ga_data)
3934 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3935 isn->isn_arg.jump.jump_where = instr->ga_len;
3936 }
3937 ga_clear(&end_ga);
3938 }
3939
3940 return OK;
3941}
3942
3943/*
3944 * expr4a && expr4a && expr4a logical AND
3945 *
3946 * Produces instructions:
3947 * EVAL expr4a Push result of "expr4a"
3948 * JUMP_AND_KEEP_IF_FALSE end
3949 * EVAL expr4b Push result of "expr4b"
3950 * JUMP_AND_KEEP_IF_FALSE end
3951 * EVAL expr4c Push result of "expr4c"
3952 * end:
3953 */
3954 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003955compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003956{
Bram Moolenaara5565e42020-05-09 15:44:01 +02003957 int ppconst_used = ppconst->pp_used;
3958
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02003960 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003961 return FAIL;
3962
3963 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02003964 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965}
3966
3967/*
3968 * expr3a || expr3b || expr3c logical OR
3969 *
3970 * Produces instructions:
3971 * EVAL expr3a Push result of "expr3a"
3972 * JUMP_AND_KEEP_IF_TRUE end
3973 * EVAL expr3b Push result of "expr3b"
3974 * JUMP_AND_KEEP_IF_TRUE end
3975 * EVAL expr3c Push result of "expr3c"
3976 * end:
3977 */
3978 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003979compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980{
Bram Moolenaara5565e42020-05-09 15:44:01 +02003981 int ppconst_used = ppconst->pp_used;
3982
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003983 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02003984 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003985 return FAIL;
3986
3987 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02003988 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989}
3990
3991/*
3992 * Toplevel expression: expr2 ? expr1a : expr1b
3993 *
3994 * Produces instructions:
3995 * EVAL expr2 Push result of "expr"
3996 * JUMP_IF_FALSE alt jump if false
3997 * EVAL expr1a
3998 * JUMP_ALWAYS end
3999 * alt: EVAL expr1b
4000 * end:
4001 */
4002 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004003compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004{
4005 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004006 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004007 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008
Bram Moolenaar61a89812020-05-07 16:58:17 +02004009 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004010 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 return FAIL;
4012
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004013 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014 if (*p == '?')
4015 {
4016 garray_T *instr = &cctx->ctx_instr;
4017 garray_T *stack = &cctx->ctx_type_stack;
4018 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004019 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004021 type_T *type1 = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 type_T *type2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004023 int has_const_expr = FALSE;
4024 int const_value = FALSE;
4025 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004027 if (next != NULL)
4028 {
4029 *arg = next_line_from_context(cctx, TRUE);
4030 p = skipwhite(*arg);
4031 }
4032
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004033 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4034 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004035 semsg(_(e_white_space_required_before_and_after_str), "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004036 return FAIL;
4037 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004038
Bram Moolenaara5565e42020-05-09 15:44:01 +02004039 if (ppconst->pp_used == ppconst_used + 1)
4040 {
4041 // the condition is a constant, we know whether the ? or the :
4042 // expression is to be evaluated.
4043 has_const_expr = TRUE;
4044 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4045 clear_tv(&ppconst->pp_tv[ppconst_used]);
4046 --ppconst->pp_used;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004047 cctx->ctx_skip = save_skip == SKIP_YES || !const_value
4048 ? SKIP_YES : SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004049 }
4050 else
4051 {
4052 generate_ppconst(cctx, ppconst);
4053 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
4054 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055
4056 // evaluate the second expression; any type is accepted
4057 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004058 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004059 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004060 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004061 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062
Bram Moolenaara5565e42020-05-09 15:44:01 +02004063 if (!has_const_expr)
4064 {
4065 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004066
Bram Moolenaara5565e42020-05-09 15:44:01 +02004067 // remember the type and drop it
4068 --stack->ga_len;
4069 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070
Bram Moolenaara5565e42020-05-09 15:44:01 +02004071 end_idx = instr->ga_len;
4072 generate_JUMP(cctx, JUMP_ALWAYS, 0);
4073
4074 // jump here from JUMP_IF_FALSE
4075 isn = ((isn_T *)instr->ga_data) + alt_idx;
4076 isn->isn_arg.jump.jump_where = instr->ga_len;
4077 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004078
4079 // Check for the ":".
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004080 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 if (*p != ':')
4082 {
4083 emsg(_(e_missing_colon));
4084 return FAIL;
4085 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004086 if (next != NULL)
4087 {
4088 *arg = next_line_from_context(cctx, TRUE);
4089 p = skipwhite(*arg);
4090 }
4091
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004092 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4093 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004094 semsg(_(e_white_space_required_before_and_after_str), ":");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004095 return FAIL;
4096 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097
4098 // evaluate the third expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004099 if (has_const_expr)
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004100 cctx->ctx_skip = save_skip == SKIP_YES || const_value
4101 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004102 *arg = skipwhite(p + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02004103 if (may_get_next_line(p + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004104 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004105 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004106 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107
Bram Moolenaara5565e42020-05-09 15:44:01 +02004108 if (!has_const_expr)
4109 {
4110 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004111
Bram Moolenaara5565e42020-05-09 15:44:01 +02004112 // If the types differ, the result has a more generic type.
4113 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4114 common_type(type1, type2, &type2, cctx->ctx_type_list);
4115
4116 // jump here from JUMP_ALWAYS
4117 isn = ((isn_T *)instr->ga_data) + end_idx;
4118 isn->isn_arg.jump.jump_where = instr->ga_len;
4119 }
4120
4121 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122 }
4123 return OK;
4124}
4125
4126/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004127 * Toplevel expression.
4128 */
4129 static int
4130compile_expr0(char_u **arg, cctx_T *cctx)
4131{
4132 ppconst_T ppconst;
4133
4134 CLEAR_FIELD(ppconst);
4135 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4136 {
4137 clear_ppconst(&ppconst);
4138 return FAIL;
4139 }
4140 if (generate_ppconst(cctx, &ppconst) == FAIL)
4141 return FAIL;
4142 return OK;
4143}
4144
4145/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146 * compile "return [expr]"
4147 */
4148 static char_u *
4149compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4150{
4151 char_u *p = arg;
4152 garray_T *stack = &cctx->ctx_type_stack;
4153 type_T *stack_type;
4154
4155 if (*p != NUL && *p != '|' && *p != '\n')
4156 {
4157 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004158 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159 return NULL;
4160
4161 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4162 if (set_return_type)
4163 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004164 else
4165 {
4166 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4167 && stack_type->tt_type != VAR_VOID
4168 && stack_type->tt_type != VAR_UNKNOWN)
4169 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004170 emsg(_(e_returning_value_in_function_without_return_type));
Bram Moolenaar05a55512020-07-05 15:52:19 +02004171 return NULL;
4172 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004173 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4174 cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004175 return NULL;
Bram Moolenaar05a55512020-07-05 15:52:19 +02004176 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177 }
4178 else
4179 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004180 // "set_return_type" cannot be TRUE, only used for a lambda which
4181 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004182 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4183 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004185 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004186 return NULL;
4187 }
4188
4189 // No argument, return zero.
4190 generate_PUSHNR(cctx, 0);
4191 }
4192
4193 if (generate_instr(cctx, ISN_RETURN) == NULL)
4194 return NULL;
4195
4196 // "return val | endif" is possible
4197 return skipwhite(p);
4198}
4199
4200/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004201 * Get a line from the compilation context, compatible with exarg_T getline().
4202 * Return a pointer to the line in allocated memory.
4203 * Return NULL for end-of-file or some error.
4204 */
4205 static char_u *
4206exarg_getline(
4207 int c UNUSED,
4208 void *cookie,
4209 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004210 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004211{
4212 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004213 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004214
Bram Moolenaar66250c92020-08-20 15:02:42 +02004215 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004216 {
Bram Moolenaar66250c92020-08-20 15:02:42 +02004217 if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
4218 return NULL;
4219 ++cctx->ctx_lnum;
4220 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4221 // Comment lines result in NULL pointers, skip them.
4222 if (p != NULL)
4223 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004224 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004225}
4226
4227/*
4228 * Compile a nested :def command.
4229 */
4230 static char_u *
4231compile_nested_function(exarg_T *eap, cctx_T *cctx)
4232{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004233 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004234 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004235 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004236 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004237 lvar_T *lvar;
4238 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004239 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004240
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004241 // Only g:Func() can use a namespace.
4242 if (name_start[1] == ':' && !is_global)
4243 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004244 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004245 return NULL;
4246 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004247 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4248 return NULL;
4249
Bram Moolenaar04b12692020-05-04 23:24:44 +02004250 eap->arg = name_end;
4251 eap->getline = exarg_getline;
4252 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004253 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004254 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004255 lambda_name = get_lambda_name();
4256 ufunc = def_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004257
Bram Moolenaar822ba242020-05-24 23:00:18 +02004258 if (ufunc == NULL)
4259 return NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004260 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004261 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004262 return NULL;
4263
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004264 if (is_global)
4265 {
4266 char_u *func_name = vim_strnsave(name_start + 2,
4267 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004268
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004269 if (func_name == NULL)
4270 r = FAIL;
4271 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004272 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004273 }
4274 else
4275 {
4276 // Define a local variable for the function reference.
4277 lvar = reserve_local(cctx, name_start, name_end - name_start,
4278 TRUE, ufunc->uf_func_type);
Bram Moolenaareef21022020-08-01 22:16:43 +02004279 if (lvar == NULL)
4280 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004281 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004282 return NULL;
4283 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
4284 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004285
Bram Moolenaar61a89812020-05-07 16:58:17 +02004286 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004287 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004288}
4289
4290/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004291 * Return the length of an assignment operator, or zero if there isn't one.
4292 */
4293 int
4294assignment_len(char_u *p, int *heredoc)
4295{
4296 if (*p == '=')
4297 {
4298 if (p[1] == '<' && p[2] == '<')
4299 {
4300 *heredoc = TRUE;
4301 return 3;
4302 }
4303 return 1;
4304 }
4305 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4306 return 2;
4307 if (STRNCMP(p, "..=", 3) == 0)
4308 return 3;
4309 return 0;
4310}
4311
4312// words that cannot be used as a variable
4313static char *reserved[] = {
4314 "true",
4315 "false",
4316 NULL
4317};
4318
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004319typedef enum {
4320 dest_local,
4321 dest_option,
4322 dest_env,
4323 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004324 dest_buffer,
4325 dest_window,
4326 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004327 dest_vimvar,
4328 dest_script,
4329 dest_reg,
4330} assign_dest_T;
4331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004333 * Generate the load instruction for "name".
4334 */
4335 static void
4336generate_loadvar(
4337 cctx_T *cctx,
4338 assign_dest_T dest,
4339 char_u *name,
4340 lvar_T *lvar,
4341 type_T *type)
4342{
4343 switch (dest)
4344 {
4345 case dest_option:
4346 // TODO: check the option exists
4347 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4348 break;
4349 case dest_global:
4350 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
4351 break;
4352 case dest_buffer:
4353 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
4354 break;
4355 case dest_window:
4356 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
4357 break;
4358 case dest_tab:
4359 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
4360 break;
4361 case dest_script:
4362 compile_load_scriptvar(cctx,
4363 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
4364 break;
4365 case dest_env:
4366 // Include $ in the name here
4367 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
4368 break;
4369 case dest_reg:
4370 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
4371 break;
4372 case dest_vimvar:
4373 generate_LOADV(cctx, name + 2, TRUE);
4374 break;
4375 case dest_local:
4376 if (lvar->lv_from_outer)
4377 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
4378 NULL, type);
4379 else
4380 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
4381 break;
4382 }
4383}
4384
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004385 void
4386vim9_declare_error(char_u *name)
4387{
4388 char *scope = "";
4389
4390 switch (*name)
4391 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004392 case 'g': scope = _("global"); break;
4393 case 'b': scope = _("buffer"); break;
4394 case 'w': scope = _("window"); break;
4395 case 't': scope = _("tab"); break;
4396 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004397 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004398 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004399 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004400 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004401 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004402 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02004403 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004404 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004405 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02004406}
4407
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004408/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004409 * Compile declaration and assignment:
4410 * "let var", "let var = expr", "const var = expr" and "var = expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411 * "arg" points to "var".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004412 * Return NULL for an error.
4413 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 */
4415 static char_u *
4416compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
4417{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004418 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004420 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421 char_u *ret = NULL;
4422 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004423 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004424 int scriptvar_sid = 0;
4425 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004426 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004427 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004428 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004430 int oplen = 0;
4431 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02004432 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004433 type_T *member_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004434 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004435 char_u *sp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004436 int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004437
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004438 // Skip over the "var" or "[var, var]" to get to any "=".
4439 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
4440 if (p == NULL)
4441 return *arg == '[' ? arg : NULL;
4442
4443 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004444 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004445 // TODO: should we allow this, and figure out type inference from list
4446 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004447 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 return NULL;
4449 }
4450
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451 sp = p;
4452 p = skipwhite(p);
4453 op = p;
4454 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004455
4456 if (var_count > 0 && oplen == 0)
4457 // can be something like "[1, 2]->func()"
4458 return arg;
4459
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004460 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
4461 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004462 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004463 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02004464 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004465
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466 if (heredoc)
4467 {
4468 list_T *l;
4469 listitem_T *li;
4470
4471 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02004472 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004473 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004474 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004475
4476 // Push each line and the create the list.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004477 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478 {
4479 generate_PUSHS(cctx, li->li_tv.vval.v_string);
4480 li->li_tv.vval.v_string = NULL;
4481 }
4482 generate_NEWLIST(cctx, l->lv_len);
4483 type = &t_list_string;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004484 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004485 list_free(l);
4486 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004487 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004489 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004491 // for "[var, var] = expr" evaluate the expression here, loop over the
4492 // list of variables below.
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02004493
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004494 p = skipwhite(op + oplen);
4495 if (compile_expr0(&p, cctx) == FAIL)
4496 return NULL;
4497 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004499 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004500 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004501 type_T *stacktype;
4502
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004503 stacktype = stack->ga_len == 0 ? &t_void
4504 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004505 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004506 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004507 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004508 goto theend;
4509 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004510 if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004511 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02004512 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02004513 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
4514 semicolon);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004515 }
4516 }
4517
4518 /*
4519 * Loop over variables in "[var, var] = expr".
4520 * For "var = expr" and "let var: type" this is done only once.
4521 */
4522 if (var_count > 0)
4523 var_start = skipwhite(arg + 1); // skip over the "["
4524 else
4525 var_start = arg;
4526 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
4527 {
4528 char_u *var_end = skip_var_one(var_start, FALSE);
4529 size_t varlen;
4530 int new_local = FALSE;
4531 int opt_type;
4532 int opt_flags = 0;
4533 assign_dest_T dest = dest_local;
4534 int vimvaridx = -1;
4535 lvar_T *lvar = NULL;
4536 lvar_T arg_lvar;
4537 int has_type = FALSE;
4538 int has_index = FALSE;
4539 int instr_count = -1;
4540
Bram Moolenaar65821722020-08-02 18:58:54 +02004541 if (*var_start == '@')
4542 p = var_start + 2;
4543 else
4544 {
4545 p = (*var_start == '&' || *var_start == '$')
4546 ? var_start + 1 : var_start;
4547 p = to_name_end(p, TRUE);
4548 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004549
4550 // "a: type" is declaring variable "a" with a type, not "a:".
4551 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
4552 --var_end;
4553 if (is_decl && p == var_start + 2 && p[-1] == ':')
4554 --p;
4555
4556 varlen = p - var_start;
4557 vim_free(name);
4558 name = vim_strnsave(var_start, varlen);
4559 if (name == NULL)
4560 return NULL;
4561 if (!heredoc)
4562 type = &t_any;
4563
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004564 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004565 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004566 int declare_error = FALSE;
4567
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004568 if (*var_start == '&')
4569 {
4570 int cc;
4571 long numval;
4572
4573 dest = dest_option;
4574 if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004576 emsg(_(e_const_option));
4577 goto theend;
4578 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004579 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004580 p = var_start;
4581 p = find_option_end(&p, &opt_flags);
4582 if (p == NULL)
4583 {
4584 // cannot happen?
4585 emsg(_(e_letunexp));
4586 goto theend;
4587 }
4588 cc = *p;
4589 *p = NUL;
4590 opt_type = get_option_value(var_start + 1, &numval,
4591 NULL, opt_flags);
4592 *p = cc;
4593 if (opt_type == -3)
4594 {
4595 semsg(_(e_unknown_option), var_start);
4596 goto theend;
4597 }
4598 if (opt_type == -2 || opt_type == 0)
4599 type = &t_string;
4600 else
4601 type = &t_number; // both number and boolean option
4602 }
4603 else if (*var_start == '$')
4604 {
4605 dest = dest_env;
4606 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004607 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004608 }
4609 else if (*var_start == '@')
4610 {
Bram Moolenaar65821722020-08-02 18:58:54 +02004611 if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004612 {
4613 emsg_invreg(var_start[1]);
4614 goto theend;
4615 }
4616 dest = dest_reg;
4617 type = &t_string;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004618 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004619 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004620 else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004621 {
4622 dest = dest_global;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004623 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004624 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004625 else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004626 {
4627 dest = dest_buffer;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004628 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004629 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004630 else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004631 {
4632 dest = dest_window;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004633 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004634 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004635 else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004636 {
4637 dest = dest_tab;
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004638 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004639 }
Bram Moolenaar33afa242020-07-29 19:18:00 +02004640 else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004641 {
4642 typval_T *vtv;
4643 int di_flags;
4644
4645 vimvaridx = find_vim_var(name + 2, &di_flags);
4646 if (vimvaridx < 0)
4647 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004648 semsg(_(e_variable_not_found_str), var_start);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004649 goto theend;
4650 }
4651 // We use the current value of "sandbox" here, is that OK?
4652 if (var_check_ro(di_flags, name, FALSE))
4653 goto theend;
4654 dest = dest_vimvar;
4655 vtv = get_vim_var_tv(vimvaridx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02004656 type = typval2type_vimvar(vtv, cctx->ctx_type_list);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004657 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004658 }
4659 else
4660 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004661 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004662
4663 for (idx = 0; reserved[idx] != NULL; ++idx)
4664 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004665 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004666 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004667 goto theend;
4668 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004669
4670 lvar = lookup_local(var_start, varlen, cctx);
4671 if (lvar == NULL)
4672 {
4673 CLEAR_FIELD(arg_lvar);
4674 if (lookup_arg(var_start, varlen,
4675 &arg_lvar.lv_idx, &arg_lvar.lv_type,
4676 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004677 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004678 if (is_decl)
4679 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004680 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004681 goto theend;
4682 }
4683 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02004684 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004685 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004686 if (lvar != NULL)
4687 {
4688 if (is_decl)
4689 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004690 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004691 goto theend;
4692 }
4693 else if (lvar->lv_const)
4694 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004695 semsg(_(e_cannot_assign_to_constant), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004696 goto theend;
4697 }
4698 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004699 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004700 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004701 int script_namespace = varlen > 1
4702 && STRNCMP(var_start, "s:", 2) == 0;
4703 int script_var = (script_namespace
Bram Moolenaar53900992020-08-22 19:02:02 +02004704 ? lookup_script(var_start + 2, varlen - 2, FALSE)
4705 : lookup_script(var_start, varlen, TRUE)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004706 imported_T *import =
4707 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004708
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004709 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004710 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004711 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
4712
4713 if (is_decl)
4714 {
4715 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004716 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02004717 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004718 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004719 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004720 name);
4721 goto theend;
4722 }
4723 else if (cctx->ctx_ufunc->uf_script_ctx_version
4724 == SCRIPT_VERSION_VIM9
4725 && script_namespace
4726 && !script_var && import == NULL)
4727 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004728 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004729 goto theend;
4730 }
4731
4732 dest = dest_script;
4733
4734 // existing script-local variables should have a type
4735 scriptvar_sid = current_sctx.sc_sid;
4736 if (import != NULL)
4737 scriptvar_sid = import->imp_sid;
4738 scriptvar_idx = get_script_item_idx(scriptvar_sid,
4739 rawname, TRUE);
4740 if (scriptvar_idx >= 0)
4741 {
4742 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
4743 svar_T *sv =
4744 ((svar_T *)si->sn_var_vals.ga_data)
4745 + scriptvar_idx;
4746 type = sv->sv_type;
4747 }
4748 }
4749 else if (name[1] == ':' && name[2] != NUL)
4750 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004751 semsg(_(e_cannot_use_namespaced_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004752 goto theend;
4753 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004754 else if (!is_decl)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004755 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004756 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004757 goto theend;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004758 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02004759 else if (check_defined(var_start, varlen, cctx) == FAIL)
4760 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004761 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004762 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02004763
4764 if (declare_error)
4765 {
4766 vim9_declare_error(name);
4767 goto theend;
4768 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004769 }
4770
4771 // handle "a:name" as a name, not index "name" on "a"
4772 if (varlen > 1 || var_start[varlen] != ':')
4773 p = var_end;
4774
4775 if (dest != dest_option)
4776 {
4777 if (is_decl && *p == ':')
4778 {
4779 // parse optional type: "let var: type = expr"
4780 if (!VIM_ISWHITE(p[1]))
4781 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004782 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004783 goto theend;
4784 }
4785 p = skipwhite(p + 1);
4786 type = parse_type(&p, cctx->ctx_type_list);
4787 has_type = TRUE;
4788 }
4789 else if (lvar != NULL)
4790 type = lvar->lv_type;
4791 }
4792
4793 if (oplen == 3 && !heredoc && dest != dest_global
4794 && type->tt_type != VAR_STRING
4795 && type->tt_type != VAR_ANY)
4796 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004797 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004798 goto theend;
4799 }
4800
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004801 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004802 {
4803 if (oplen > 1 && !heredoc)
4804 {
4805 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004806 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004807 goto theend;
4808 }
4809
4810 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004811 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
4812 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004813 goto theend;
4814 lvar = reserve_local(cctx, var_start, varlen,
4815 cmdidx == CMD_const, type);
4816 if (lvar == NULL)
4817 goto theend;
4818 new_local = TRUE;
4819 }
4820
4821 member_type = type;
4822 if (var_end > var_start + varlen)
4823 {
4824 // Something follows after the variable: "var[idx]".
4825 if (is_decl)
4826 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004827 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004828 goto theend;
4829 }
4830
4831 if (var_start[varlen] == '[')
4832 {
4833 has_index = TRUE;
4834 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02004835 member_type = &t_any;
4836 else
4837 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004838 }
4839 else
4840 {
4841 semsg("Not supported yet: %s", var_start);
4842 goto theend;
4843 }
4844 }
4845 else if (lvar == &arg_lvar)
4846 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004847 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004848 goto theend;
4849 }
4850
4851 if (!heredoc)
4852 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004853 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004854 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004855 if (oplen > 0 && var_count == 0)
4856 {
4857 // skip over the "=" and the expression
4858 p = skipwhite(op + oplen);
4859 compile_expr0(&p, cctx);
4860 }
4861 }
4862 else if (oplen > 0)
4863 {
4864 type_T *stacktype;
4865
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004866 // For "var = expr" evaluate the expression.
4867 if (var_count == 0)
4868 {
4869 int r;
4870
4871 // for "+=", "*=", "..=" etc. first load the current value
4872 if (*op != '=')
4873 {
4874 generate_loadvar(cctx, dest, name, lvar, type);
4875
4876 if (has_index)
4877 {
4878 // TODO: get member from list or dict
4879 emsg("Index with operation not supported yet");
4880 goto theend;
4881 }
4882 }
4883
4884 // Compile the expression. Temporarily hide the new local
4885 // variable here, it is not available to this expression.
4886 if (new_local)
4887 --cctx->ctx_locals.ga_len;
4888 instr_count = instr->ga_len;
4889 p = skipwhite(op + oplen);
4890 r = compile_expr0(&p, cctx);
4891 if (new_local)
4892 ++cctx->ctx_locals.ga_len;
4893 if (r == FAIL)
4894 goto theend;
4895 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004896 else if (semicolon && var_idx == var_count - 1)
4897 {
4898 // For "[var; var] = expr" get the rest of the list
4899 if (generate_SLICE(cctx, var_count - 1) == FAIL)
4900 goto theend;
4901 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004902 else
4903 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004904 // For "[var, var] = expr" get the "var_idx" item from the
4905 // list.
4906 if (generate_GETITEM(cctx, var_idx) == FAIL)
4907 return FAIL;
4908 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004909
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004910 stacktype = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004911 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004912 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004913 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004914 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004915 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004916 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004917 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004918 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004919 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004920 }
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004921 else if ((stacktype->tt_type == VAR_FUNC
4922 || stacktype->tt_type == VAR_PARTIAL)
4923 && var_wrong_func_name(name, TRUE))
4924 {
4925 goto theend;
4926 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004927 else
4928 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004929 // An empty list or dict has a &t_void member,
4930 // for a variable that implies &t_any.
4931 if (stacktype == &t_list_empty)
4932 lvar->lv_type = &t_list_any;
4933 else if (stacktype == &t_dict_empty)
4934 lvar->lv_type = &t_dict_any;
4935 else
4936 lvar->lv_type = stacktype;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004937 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004938 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02004939 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004940 {
4941 type_T *use_type = lvar->lv_type;
4942
Bram Moolenaar93ad1472020-08-19 22:02:41 +02004943 // without operator type is here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004944 if (has_index)
4945 {
4946 use_type = use_type->tt_member;
4947 if (use_type == NULL)
4948 use_type = &t_void;
4949 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004950 if (need_type(stacktype, use_type, -1, cctx, FALSE)
Bram Moolenaar93ad1472020-08-19 22:02:41 +02004951 == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004952 goto theend;
4953 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004954 }
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004955 else if (*p != '=' && need_type(stacktype, member_type, -1,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004956 cctx, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02004957 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004958 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004959 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004960 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004961 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004962 goto theend;
4963 }
4964 else if (!has_type || dest == dest_option)
4965 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02004966 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004967 goto theend;
4968 }
4969 else
4970 {
4971 // variables are always initialized
4972 if (ga_grow(instr, 1) == FAIL)
4973 goto theend;
4974 switch (member_type->tt_type)
4975 {
4976 case VAR_BOOL:
4977 generate_PUSHBOOL(cctx, VVAL_FALSE);
4978 break;
4979 case VAR_FLOAT:
4980#ifdef FEAT_FLOAT
4981 generate_PUSHF(cctx, 0.0);
4982#endif
4983 break;
4984 case VAR_STRING:
4985 generate_PUSHS(cctx, NULL);
4986 break;
4987 case VAR_BLOB:
4988 generate_PUSHBLOB(cctx, NULL);
4989 break;
4990 case VAR_FUNC:
4991 generate_PUSHFUNC(cctx, NULL, &t_func_void);
4992 break;
4993 case VAR_LIST:
4994 generate_NEWLIST(cctx, 0);
4995 break;
4996 case VAR_DICT:
4997 generate_NEWDICT(cctx, 0);
4998 break;
4999 case VAR_JOB:
5000 generate_PUSHJOB(cctx, NULL);
5001 break;
5002 case VAR_CHANNEL:
5003 generate_PUSHCHANNEL(cctx, NULL);
5004 break;
5005 case VAR_NUMBER:
5006 case VAR_UNKNOWN:
5007 case VAR_ANY:
5008 case VAR_PARTIAL:
5009 case VAR_VOID:
5010 case VAR_SPECIAL: // cannot happen
5011 generate_PUSHNR(cctx, 0);
5012 break;
5013 }
5014 }
5015 if (var_count == 0)
5016 end = p;
5017 }
5018
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005019 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005020 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005021 break;
5022
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005023 if (oplen > 0 && *op != '=')
5024 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005025 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005026 type_T *stacktype;
5027
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005028 if (*op == '.')
5029 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005030 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005031 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005032 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005033 if (
5034#ifdef FEAT_FLOAT
5035 // If variable is float operation with number is OK.
5036 !(expected == &t_float && stacktype == &t_number) &&
5037#endif
5038 need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005039 goto theend;
5040
5041 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005042 {
5043 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5044 goto theend;
5045 }
5046 else if (*op == '+')
5047 {
5048 if (generate_add_instr(cctx,
5049 operator_type(member_type, stacktype),
5050 member_type, stacktype) == FAIL)
5051 goto theend;
5052 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005053 else if (generate_two_op(cctx, op) == FAIL)
5054 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005055 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005056
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005057 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005058 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005059 int r;
5060
5061 // Compile the "idx" in "var[idx]".
5062 if (new_local)
5063 --cctx->ctx_locals.ga_len;
5064 p = skipwhite(var_start + varlen + 1);
5065 r = compile_expr0(&p, cctx);
5066 if (new_local)
5067 ++cctx->ctx_locals.ga_len;
5068 if (r == FAIL)
5069 goto theend;
5070 if (*skipwhite(p) != ']')
5071 {
Bram Moolenaard1103582020-08-14 22:44:25 +02005072 // this should not happen
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005073 emsg(_(e_missbrac));
5074 goto theend;
5075 }
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005076 if (type == &t_any)
5077 {
5078 type_T *idx_type = ((type_T **)stack->ga_data)[
5079 stack->ga_len - 1];
5080 // Index on variable of unknown type: guess the type from the
5081 // index type: number is dict, otherwise dict.
5082 // TODO: should do the assignment at runtime
5083 if (idx_type->tt_type == VAR_NUMBER)
5084 type = &t_list_any;
5085 else
5086 type = &t_dict_any;
5087 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005088 if (type->tt_type == VAR_DICT
5089 && may_generate_2STRING(-1, cctx) == FAIL)
5090 goto theend;
5091 if (type->tt_type == VAR_LIST
5092 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005093 != VAR_NUMBER)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005094 {
5095 emsg(_(e_number_exp));
5096 goto theend;
5097 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005098
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005099 // Load the dict or list. On the stack we then have:
5100 // - value
5101 // - index
5102 // - variable
5103 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005104
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005105 if (type->tt_type == VAR_LIST)
5106 {
5107 if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
5108 return FAIL;
5109 }
5110 else if (type->tt_type == VAR_DICT)
5111 {
5112 if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
5113 return FAIL;
5114 }
5115 else
5116 {
5117 emsg(_(e_listreq));
5118 goto theend;
5119 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005120 }
5121 else
5122 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005123 switch (dest)
5124 {
5125 case dest_option:
5126 generate_STOREOPT(cctx, name + 1, opt_flags);
5127 break;
5128 case dest_global:
5129 // include g: with the name, easier to execute that way
5130 generate_STORE(cctx, ISN_STOREG, 0, name);
5131 break;
5132 case dest_buffer:
5133 // include b: with the name, easier to execute that way
5134 generate_STORE(cctx, ISN_STOREB, 0, name);
5135 break;
5136 case dest_window:
5137 // include w: with the name, easier to execute that way
5138 generate_STORE(cctx, ISN_STOREW, 0, name);
5139 break;
5140 case dest_tab:
5141 // include t: with the name, easier to execute that way
5142 generate_STORE(cctx, ISN_STORET, 0, name);
5143 break;
5144 case dest_env:
5145 generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5146 break;
5147 case dest_reg:
5148 generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5149 break;
5150 case dest_vimvar:
5151 generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5152 break;
5153 case dest_script:
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005154 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005155 if (scriptvar_idx < 0)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005156 {
5157 char_u *name_s = name;
5158
5159 // Include s: in the name for store_var()
5160 if (name[1] != ':')
5161 {
5162 int len = (int)STRLEN(name) + 3;
5163
5164 name_s = alloc(len);
5165 if (name_s == NULL)
5166 name_s = name;
5167 else
5168 vim_snprintf((char *)name_s, len,
5169 "s:%s", name);
5170 }
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005171 generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5172 scriptvar_sid, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005173 if (name_s != name)
5174 vim_free(name_s);
5175 }
5176 else
5177 generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005178 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005179 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005180 break;
5181 case dest_local:
5182 if (lvar != NULL)
5183 {
5184 isn_T *isn = ((isn_T *)instr->ga_data)
5185 + instr->ga_len - 1;
5186
5187 // optimization: turn "var = 123" from ISN_PUSHNR +
5188 // ISN_STORE into ISN_STORENR
5189 if (!lvar->lv_from_outer
5190 && instr->ga_len == instr_count + 1
5191 && isn->isn_type == ISN_PUSHNR)
5192 {
5193 varnumber_T val = isn->isn_arg.number;
5194
5195 isn->isn_type = ISN_STORENR;
5196 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5197 isn->isn_arg.storenr.stnr_val = val;
5198 if (stack->ga_len > 0)
5199 --stack->ga_len;
5200 }
5201 else if (lvar->lv_from_outer)
5202 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005203 NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005204 else
5205 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5206 }
5207 break;
5208 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005209 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005210
5211 if (var_idx + 1 < var_count)
5212 var_start = skipwhite(var_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005213 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005214
5215 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02005216 if (var_count > 0 && !semicolon)
5217 {
5218 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5219 goto theend;
5220 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005221
Bram Moolenaarb2097502020-07-19 17:17:02 +02005222 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005223
5224theend:
5225 vim_free(name);
5226 return ret;
5227}
5228
5229/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005230 * Check if "name" can be "unlet".
5231 */
5232 int
5233check_vim9_unlet(char_u *name)
5234{
5235 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
5236 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005237 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005238 return FAIL;
5239 }
5240 return OK;
5241}
5242
5243/*
5244 * Callback passed to ex_unletlock().
5245 */
5246 static int
5247compile_unlet(
5248 lval_T *lvp,
5249 char_u *name_end,
5250 exarg_T *eap,
5251 int deep UNUSED,
5252 void *coookie)
5253{
5254 cctx_T *cctx = coookie;
5255
5256 if (lvp->ll_tv == NULL)
5257 {
5258 char_u *p = lvp->ll_name;
5259 int cc = *name_end;
5260 int ret = OK;
5261
5262 // Normal name. Only supports g:, w:, t: and b: namespaces.
5263 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005264 if (*p == '$')
5265 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
5266 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005267 ret = FAIL;
5268 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02005269 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005270
5271 *name_end = cc;
5272 return ret;
5273 }
5274
5275 // TODO: unlet {list}[idx]
5276 // TODO: unlet {dict}[key]
5277 emsg("Sorry, :unlet not fully implemented yet");
5278 return FAIL;
5279}
5280
5281/*
5282 * compile "unlet var", "lock var" and "unlock var"
5283 * "arg" points to "var".
5284 */
5285 static char_u *
5286compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
5287{
5288 char_u *p = arg;
5289
5290 if (eap->cmdidx != CMD_unlet)
5291 {
5292 emsg("Sorry, :lock and unlock not implemented yet");
5293 return NULL;
5294 }
5295
5296 if (*p == '!')
5297 {
5298 p = skipwhite(p + 1);
5299 eap->forceit = TRUE;
5300 }
5301
5302 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
5303 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5304}
5305
5306/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005307 * Compile an :import command.
5308 */
5309 static char_u *
5310compile_import(char_u *arg, cctx_T *cctx)
5311{
Bram Moolenaar1c991142020-07-04 13:15:31 +02005312 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005313}
5314
5315/*
5316 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
5317 */
5318 static int
5319compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
5320{
5321 garray_T *instr = &cctx->ctx_instr;
5322 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
5323
5324 if (endlabel == NULL)
5325 return FAIL;
5326 endlabel->el_next = *el;
5327 *el = endlabel;
5328 endlabel->el_end_label = instr->ga_len;
5329
5330 generate_JUMP(cctx, when, 0);
5331 return OK;
5332}
5333
5334 static void
5335compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
5336{
5337 garray_T *instr = &cctx->ctx_instr;
5338
5339 while (*el != NULL)
5340 {
5341 endlabel_T *cur = (*el);
5342 isn_T *isn;
5343
5344 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
5345 isn->isn_arg.jump.jump_where = instr->ga_len;
5346 *el = cur->el_next;
5347 vim_free(cur);
5348 }
5349}
5350
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005351 static void
5352compile_free_jump_to_end(endlabel_T **el)
5353{
5354 while (*el != NULL)
5355 {
5356 endlabel_T *cur = (*el);
5357
5358 *el = cur->el_next;
5359 vim_free(cur);
5360 }
5361}
5362
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005363/*
5364 * Create a new scope and set up the generic items.
5365 */
5366 static scope_T *
5367new_scope(cctx_T *cctx, scopetype_T type)
5368{
5369 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
5370
5371 if (scope == NULL)
5372 return NULL;
5373 scope->se_outer = cctx->ctx_scope;
5374 cctx->ctx_scope = scope;
5375 scope->se_type = type;
5376 scope->se_local_count = cctx->ctx_locals.ga_len;
5377 return scope;
5378}
5379
5380/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005381 * Free the current scope and go back to the outer scope.
5382 */
5383 static void
5384drop_scope(cctx_T *cctx)
5385{
5386 scope_T *scope = cctx->ctx_scope;
5387
5388 if (scope == NULL)
5389 {
5390 iemsg("calling drop_scope() without a scope");
5391 return;
5392 }
5393 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02005394 switch (scope->se_type)
5395 {
5396 case IF_SCOPE:
5397 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
5398 case FOR_SCOPE:
5399 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
5400 case WHILE_SCOPE:
5401 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
5402 case TRY_SCOPE:
5403 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
5404 case NO_SCOPE:
5405 case BLOCK_SCOPE:
5406 break;
5407 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005408 vim_free(scope);
5409}
5410
5411/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005412 * compile "if expr"
5413 *
5414 * "if expr" Produces instructions:
5415 * EVAL expr Push result of "expr"
5416 * JUMP_IF_FALSE end
5417 * ... body ...
5418 * end:
5419 *
5420 * "if expr | else" Produces instructions:
5421 * EVAL expr Push result of "expr"
5422 * JUMP_IF_FALSE else
5423 * ... body ...
5424 * JUMP_ALWAYS end
5425 * else:
5426 * ... body ...
5427 * end:
5428 *
5429 * "if expr1 | elseif expr2 | else" Produces instructions:
5430 * EVAL expr Push result of "expr"
5431 * JUMP_IF_FALSE elseif
5432 * ... body ...
5433 * JUMP_ALWAYS end
5434 * elseif:
5435 * EVAL expr Push result of "expr"
5436 * JUMP_IF_FALSE else
5437 * ... body ...
5438 * JUMP_ALWAYS end
5439 * else:
5440 * ... body ...
5441 * end:
5442 */
5443 static char_u *
5444compile_if(char_u *arg, cctx_T *cctx)
5445{
5446 char_u *p = arg;
5447 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005448 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005449 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005450 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005451 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005452
Bram Moolenaara5565e42020-05-09 15:44:01 +02005453 CLEAR_FIELD(ppconst);
5454 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005455 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02005456 clear_ppconst(&ppconst);
5457 return NULL;
5458 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005459 if (cctx->ctx_skip == SKIP_YES)
5460 clear_ppconst(&ppconst);
5461 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005462 {
5463 // The expression results in a constant.
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005464 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005465 clear_ppconst(&ppconst);
5466 }
5467 else
5468 {
5469 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005470 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005471 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005472 return NULL;
5473 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005474
5475 scope = new_scope(cctx, IF_SCOPE);
5476 if (scope == NULL)
5477 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02005478 scope->se_skip_save = skip_save;
5479 // "is_had_return" will be reset if any block does not end in :return
5480 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005481
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005482 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005483 {
5484 // "where" is set when ":elseif", "else" or ":endif" is found
5485 scope->se_u.se_if.is_if_label = instr->ga_len;
5486 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5487 }
5488 else
5489 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005490
5491 return p;
5492}
5493
5494 static char_u *
5495compile_elseif(char_u *arg, cctx_T *cctx)
5496{
5497 char_u *p = arg;
5498 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005499 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005500 isn_T *isn;
5501 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005502 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005503
5504 if (scope == NULL || scope->se_type != IF_SCOPE)
5505 {
5506 emsg(_(e_elseif_without_if));
5507 return NULL;
5508 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005509 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005510 if (!cctx->ctx_had_return)
5511 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005512
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005513 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005514 {
5515 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005516 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005517 return NULL;
5518 // previous "if" or "elseif" jumps here
5519 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5520 isn->isn_arg.jump.jump_where = instr->ga_len;
5521 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005522
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005523 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02005524 CLEAR_FIELD(ppconst);
5525 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005526 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02005527 clear_ppconst(&ppconst);
5528 return NULL;
5529 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02005530 if (scope->se_skip_save == SKIP_YES)
5531 clear_ppconst(&ppconst);
5532 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02005533 {
5534 // The expression results in a constant.
5535 // TODO: how about nesting?
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005536 cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005537 clear_ppconst(&ppconst);
5538 scope->se_u.se_if.is_if_label = -1;
5539 }
5540 else
5541 {
5542 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005543 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02005544 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005545 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005546
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005547 // "where" is set when ":elseif", "else" or ":endif" is found
5548 scope->se_u.se_if.is_if_label = instr->ga_len;
5549 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
5550 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005551
5552 return p;
5553}
5554
5555 static char_u *
5556compile_else(char_u *arg, cctx_T *cctx)
5557{
5558 char_u *p = arg;
5559 garray_T *instr = &cctx->ctx_instr;
5560 isn_T *isn;
5561 scope_T *scope = cctx->ctx_scope;
5562
5563 if (scope == NULL || scope->se_type != IF_SCOPE)
5564 {
5565 emsg(_(e_else_without_if));
5566 return NULL;
5567 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01005568 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005569 if (!cctx->ctx_had_return)
5570 scope->se_u.se_if.is_had_return = FALSE;
5571 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005572
Bram Moolenaarefd88552020-06-18 20:50:10 +02005573 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005574 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005575 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005576 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005577 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02005578 if (!cctx->ctx_had_return
5579 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
5580 JUMP_ALWAYS, cctx) == FAIL)
5581 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005582 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005583
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005584 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005585 {
5586 if (scope->se_u.se_if.is_if_label >= 0)
5587 {
5588 // previous "if" or "elseif" jumps here
5589 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5590 isn->isn_arg.jump.jump_where = instr->ga_len;
5591 scope->se_u.se_if.is_if_label = -1;
5592 }
5593 }
5594
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02005595 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02005596 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
5597 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005598
5599 return p;
5600}
5601
5602 static char_u *
5603compile_endif(char_u *arg, cctx_T *cctx)
5604{
5605 scope_T *scope = cctx->ctx_scope;
5606 ifscope_T *ifscope;
5607 garray_T *instr = &cctx->ctx_instr;
5608 isn_T *isn;
5609
5610 if (scope == NULL || scope->se_type != IF_SCOPE)
5611 {
5612 emsg(_(e_endif_without_if));
5613 return NULL;
5614 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005615 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005616 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005617 if (!cctx->ctx_had_return)
5618 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005619
Bram Moolenaara259d8d2020-01-31 20:10:50 +01005620 if (scope->se_u.se_if.is_if_label >= 0)
5621 {
5622 // previous "if" or "elseif" jumps here
5623 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
5624 isn->isn_arg.jump.jump_where = instr->ga_len;
5625 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005626 // Fill in the "end" label in jumps at the end of the blocks.
5627 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02005628 cctx->ctx_skip = scope->se_skip_save;
5629
5630 // If all the blocks end in :return and there is an :else then the
5631 // had_return flag is set.
5632 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005633
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005634 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005635 return arg;
5636}
5637
5638/*
5639 * compile "for var in expr"
5640 *
5641 * Produces instructions:
5642 * PUSHNR -1
5643 * STORE loop-idx Set index to -1
5644 * EVAL expr Push result of "expr"
5645 * top: FOR loop-idx, end Increment index, use list on bottom of stack
5646 * - if beyond end, jump to "end"
5647 * - otherwise get item from list and push it
5648 * STORE var Store item in "var"
5649 * ... body ...
5650 * JUMP top Jump back to repeat
5651 * end: DROP Drop the result of "expr"
5652 *
5653 */
5654 static char_u *
5655compile_for(char_u *arg, cctx_T *cctx)
5656{
5657 char_u *p;
5658 size_t varlen;
5659 garray_T *instr = &cctx->ctx_instr;
5660 garray_T *stack = &cctx->ctx_type_stack;
5661 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005662 lvar_T *loop_lvar; // loop iteration variable
5663 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005664 type_T *vartype;
5665
5666 // TODO: list of variables: "for [key, value] in dict"
5667 // parse "var"
5668 for (p = arg; eval_isnamec1(*p); ++p)
5669 ;
5670 varlen = p - arg;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005671 var_lvar = lookup_local(arg, varlen, cctx);
5672 if (var_lvar != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005673 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005674 semsg(_(e_variable_already_declared), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005675 return NULL;
5676 }
5677
5678 // consume "in"
5679 p = skipwhite(p);
5680 if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
5681 {
5682 emsg(_(e_missing_in));
5683 return NULL;
5684 }
5685 p = skipwhite(p + 2);
5686
5687
5688 scope = new_scope(cctx, FOR_SCOPE);
5689 if (scope == NULL)
5690 return NULL;
5691
5692 // Reserve a variable to store the loop iteration counter.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005693 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
5694 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005695 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005696 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005697 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005698 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005699 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005700
5701 // Reserve a variable to store "var"
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005702 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
5703 if (var_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005704 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005705 // out of memory or used as an argument
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005706 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005707 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005708 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005709
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005710 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005711
5712 // compile "expr", it remains on the stack until "endfor"
5713 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005714 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005715 {
5716 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005717 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005718 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005719
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005720 // Now that we know the type of "var", check that it is a list, now or at
5721 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005722 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02005723 if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005724 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02005725 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005726 return NULL;
5727 }
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02005728 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005729 var_lvar->lv_type = vartype->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005730
5731 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005732 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005733
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005734 generate_FOR(cctx, loop_lvar->lv_idx);
5735 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005736
5737 return arg;
5738}
5739
5740/*
5741 * compile "endfor"
5742 */
5743 static char_u *
5744compile_endfor(char_u *arg, cctx_T *cctx)
5745{
5746 garray_T *instr = &cctx->ctx_instr;
5747 scope_T *scope = cctx->ctx_scope;
5748 forscope_T *forscope;
5749 isn_T *isn;
5750
5751 if (scope == NULL || scope->se_type != FOR_SCOPE)
5752 {
5753 emsg(_(e_for));
5754 return NULL;
5755 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005756 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005757 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005758 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005759
5760 // At end of ":for" scope jump back to the FOR instruction.
5761 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
5762
5763 // Fill in the "end" label in the FOR statement so it can jump here
5764 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
5765 isn->isn_arg.forloop.for_end = instr->ga_len;
5766
5767 // Fill in the "end" label any BREAK statements
5768 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
5769
5770 // Below the ":for" scope drop the "expr" list from the stack.
5771 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
5772 return NULL;
5773
5774 vim_free(scope);
5775
5776 return arg;
5777}
5778
5779/*
5780 * compile "while expr"
5781 *
5782 * Produces instructions:
5783 * top: EVAL expr Push result of "expr"
5784 * JUMP_IF_FALSE end jump if false
5785 * ... body ...
5786 * JUMP top Jump back to repeat
5787 * end:
5788 *
5789 */
5790 static char_u *
5791compile_while(char_u *arg, cctx_T *cctx)
5792{
5793 char_u *p = arg;
5794 garray_T *instr = &cctx->ctx_instr;
5795 scope_T *scope;
5796
5797 scope = new_scope(cctx, WHILE_SCOPE);
5798 if (scope == NULL)
5799 return NULL;
5800
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005801 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005802
5803 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02005804 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005805 return NULL;
5806
5807 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005808 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005809 JUMP_IF_FALSE, cctx) == FAIL)
5810 return FAIL;
5811
5812 return p;
5813}
5814
5815/*
5816 * compile "endwhile"
5817 */
5818 static char_u *
5819compile_endwhile(char_u *arg, cctx_T *cctx)
5820{
5821 scope_T *scope = cctx->ctx_scope;
5822
5823 if (scope == NULL || scope->se_type != WHILE_SCOPE)
5824 {
5825 emsg(_(e_while));
5826 return NULL;
5827 }
5828 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005829 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005830
5831 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005832 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005833
5834 // Fill in the "end" label in the WHILE statement so it can jump here.
5835 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005836 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005837
5838 vim_free(scope);
5839
5840 return arg;
5841}
5842
5843/*
5844 * compile "continue"
5845 */
5846 static char_u *
5847compile_continue(char_u *arg, cctx_T *cctx)
5848{
5849 scope_T *scope = cctx->ctx_scope;
5850
5851 for (;;)
5852 {
5853 if (scope == NULL)
5854 {
5855 emsg(_(e_continue));
5856 return NULL;
5857 }
5858 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5859 break;
5860 scope = scope->se_outer;
5861 }
5862
5863 // Jump back to the FOR or WHILE instruction.
5864 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005865 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
5866 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005867 return arg;
5868}
5869
5870/*
5871 * compile "break"
5872 */
5873 static char_u *
5874compile_break(char_u *arg, cctx_T *cctx)
5875{
5876 scope_T *scope = cctx->ctx_scope;
5877 endlabel_T **el;
5878
5879 for (;;)
5880 {
5881 if (scope == NULL)
5882 {
5883 emsg(_(e_break));
5884 return NULL;
5885 }
5886 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
5887 break;
5888 scope = scope->se_outer;
5889 }
5890
5891 // Jump to the end of the FOR or WHILE loop.
5892 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005893 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005894 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005895 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005896 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
5897 return FAIL;
5898
5899 return arg;
5900}
5901
5902/*
5903 * compile "{" start of block
5904 */
5905 static char_u *
5906compile_block(char_u *arg, cctx_T *cctx)
5907{
5908 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
5909 return NULL;
5910 return skipwhite(arg + 1);
5911}
5912
5913/*
5914 * compile end of block: drop one scope
5915 */
5916 static void
5917compile_endblock(cctx_T *cctx)
5918{
5919 scope_T *scope = cctx->ctx_scope;
5920
5921 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005922 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005923 vim_free(scope);
5924}
5925
5926/*
5927 * compile "try"
5928 * Creates a new scope for the try-endtry, pointing to the first catch and
5929 * finally.
5930 * Creates another scope for the "try" block itself.
5931 * TRY instruction sets up exception handling at runtime.
5932 *
5933 * "try"
5934 * TRY -> catch1, -> finally push trystack entry
5935 * ... try block
5936 * "throw {exception}"
5937 * EVAL {exception}
5938 * THROW create exception
5939 * ... try block
5940 * " catch {expr}"
5941 * JUMP -> finally
5942 * catch1: PUSH exeception
5943 * EVAL {expr}
5944 * MATCH
5945 * JUMP nomatch -> catch2
5946 * CATCH remove exception
5947 * ... catch block
5948 * " catch"
5949 * JUMP -> finally
5950 * catch2: CATCH remove exception
5951 * ... catch block
5952 * " finally"
5953 * finally:
5954 * ... finally block
5955 * " endtry"
5956 * ENDTRY pop trystack entry, may rethrow
5957 */
5958 static char_u *
5959compile_try(char_u *arg, cctx_T *cctx)
5960{
5961 garray_T *instr = &cctx->ctx_instr;
5962 scope_T *try_scope;
5963 scope_T *scope;
5964
5965 // scope that holds the jumps that go to catch/finally/endtry
5966 try_scope = new_scope(cctx, TRY_SCOPE);
5967 if (try_scope == NULL)
5968 return NULL;
5969
5970 // "catch" is set when the first ":catch" is found.
5971 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005972 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005973 if (generate_instr(cctx, ISN_TRY) == NULL)
5974 return NULL;
5975
5976 // scope for the try block itself
5977 scope = new_scope(cctx, BLOCK_SCOPE);
5978 if (scope == NULL)
5979 return NULL;
5980
5981 return arg;
5982}
5983
5984/*
5985 * compile "catch {expr}"
5986 */
5987 static char_u *
5988compile_catch(char_u *arg, cctx_T *cctx UNUSED)
5989{
5990 scope_T *scope = cctx->ctx_scope;
5991 garray_T *instr = &cctx->ctx_instr;
5992 char_u *p;
5993 isn_T *isn;
5994
5995 // end block scope from :try or :catch
5996 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
5997 compile_endblock(cctx);
5998 scope = cctx->ctx_scope;
5999
6000 // Error if not in a :try scope
6001 if (scope == NULL || scope->se_type != TRY_SCOPE)
6002 {
6003 emsg(_(e_catch));
6004 return NULL;
6005 }
6006
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006007 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006008 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006009 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006010 return NULL;
6011 }
6012
6013 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006014 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006015 JUMP_ALWAYS, cctx) == FAIL)
6016 return NULL;
6017
6018 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006019 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006020 if (isn->isn_arg.try.try_catch == 0)
6021 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006022 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006023 {
6024 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006025 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006026 isn->isn_arg.jump.jump_where = instr->ga_len;
6027 }
6028
6029 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006030 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006031 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006032 scope->se_u.se_try.ts_caught_all = TRUE;
6033 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006034 }
6035 else
6036 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006037 char_u *end;
6038 char_u *pat;
6039 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006040 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006041 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006042
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006043 // Push v:exception, push {expr} and MATCH
6044 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6045
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006046 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006047 if (*end != *p)
6048 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006049 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006050 vim_free(tofree);
6051 return FAIL;
6052 }
6053 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006054 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006055 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006056 len = (int)(end - tofree);
6057 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006058 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006059 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006060 if (pat == NULL)
6061 return FAIL;
6062 if (generate_PUSHS(cctx, pat) == FAIL)
6063 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006064
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006065 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6066 return NULL;
6067
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006068 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006069 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6070 return NULL;
6071 }
6072
6073 if (generate_instr(cctx, ISN_CATCH) == NULL)
6074 return NULL;
6075
6076 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6077 return NULL;
6078 return p;
6079}
6080
6081 static char_u *
6082compile_finally(char_u *arg, cctx_T *cctx)
6083{
6084 scope_T *scope = cctx->ctx_scope;
6085 garray_T *instr = &cctx->ctx_instr;
6086 isn_T *isn;
6087
6088 // end block scope from :try or :catch
6089 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6090 compile_endblock(cctx);
6091 scope = cctx->ctx_scope;
6092
6093 // Error if not in a :try scope
6094 if (scope == NULL || scope->se_type != TRY_SCOPE)
6095 {
6096 emsg(_(e_finally));
6097 return NULL;
6098 }
6099
6100 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006101 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006102 if (isn->isn_arg.try.try_finally != 0)
6103 {
6104 emsg(_(e_finally_dup));
6105 return NULL;
6106 }
6107
6108 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006109 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006110
Bram Moolenaar585fea72020-04-02 22:33:21 +02006111 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006112 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006113 {
6114 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006115 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006116 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006117 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006118 }
6119
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006120 // TODO: set index in ts_finally_label jumps
6121
6122 return arg;
6123}
6124
6125 static char_u *
6126compile_endtry(char_u *arg, cctx_T *cctx)
6127{
6128 scope_T *scope = cctx->ctx_scope;
6129 garray_T *instr = &cctx->ctx_instr;
6130 isn_T *isn;
6131
6132 // end block scope from :catch or :finally
6133 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6134 compile_endblock(cctx);
6135 scope = cctx->ctx_scope;
6136
6137 // Error if not in a :try scope
6138 if (scope == NULL || scope->se_type != TRY_SCOPE)
6139 {
6140 if (scope == NULL)
6141 emsg(_(e_no_endtry));
6142 else if (scope->se_type == WHILE_SCOPE)
6143 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01006144 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006145 emsg(_(e_endfor));
6146 else
6147 emsg(_(e_endif));
6148 return NULL;
6149 }
6150
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006151 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006152 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
6153 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006154 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006155 return NULL;
6156 }
6157
6158 // Fill in the "end" label in jumps at the end of the blocks, if not done
6159 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006160 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006161
6162 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02006163 if (isn->isn_arg.try.try_catch == 0)
6164 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006165 if (isn->isn_arg.try.try_finally == 0)
6166 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02006167
6168 if (scope->se_u.se_try.ts_catch_label != 0)
6169 {
6170 // Last catch without match jumps here
6171 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
6172 isn->isn_arg.jump.jump_where = instr->ga_len;
6173 }
6174
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006175 compile_endblock(cctx);
6176
6177 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
6178 return NULL;
6179 return arg;
6180}
6181
6182/*
6183 * compile "throw {expr}"
6184 */
6185 static char_u *
6186compile_throw(char_u *arg, cctx_T *cctx UNUSED)
6187{
6188 char_u *p = skipwhite(arg);
6189
Bram Moolenaara5565e42020-05-09 15:44:01 +02006190 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006191 return NULL;
6192 if (may_generate_2STRING(-1, cctx) == FAIL)
6193 return NULL;
6194 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
6195 return NULL;
6196
6197 return p;
6198}
6199
6200/*
6201 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006202 * compile "echomsg expr"
6203 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01006204 * compile "execute expr"
6205 */
6206 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006207compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006208{
6209 char_u *p = arg;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006210 char_u *prev;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006211 int count = 0;
6212
6213 for (;;)
6214 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006215 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01006216 return NULL;
6217 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006218 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01006219 p = skipwhite(p);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02006220 if (ends_excmd2(prev, p))
Bram Moolenaarad39c092020-02-26 18:23:43 +01006221 break;
6222 }
6223
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006224 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
6225 generate_ECHO(cctx, cmdidx == CMD_echo, count);
6226 else if (cmdidx == CMD_execute)
6227 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
6228 else if (cmdidx == CMD_echomsg)
6229 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
6230 else
6231 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006232 return p;
6233}
6234
6235/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006236 * A command that is not compiled, execute with legacy code.
6237 */
6238 static char_u *
6239compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
6240{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006241 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006242 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006243 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006244
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006245 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006246 goto theend;
6247
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02006248 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006249 {
6250 long argt = excmd_get_argt(eap->cmdidx);
6251 int usefilter = FALSE;
6252
6253 has_expr = argt & (EX_XFILE | EX_EXPAND);
6254
6255 // If the command can be followed by a bar, find the bar and truncate
6256 // it, so that the following command can be compiled.
6257 // The '|' is overwritten with a NUL, it is put back below.
6258 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
6259 && *eap->arg == '!')
6260 // :w !filter or :r !filter or :r! filter
6261 usefilter = TRUE;
6262 if ((argt & EX_TRLBAR) && !usefilter)
6263 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02006264 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006265 separate_nextcmd(eap);
6266 if (eap->nextcmd != NULL)
6267 nextcmd = eap->nextcmd;
6268 }
6269 }
6270
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006271 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
6272 {
6273 // expand filename in "syntax include [@group] filename"
6274 has_expr = TRUE;
6275 eap->arg = skipwhite(eap->arg + 7);
6276 if (*eap->arg == '@')
6277 eap->arg = skiptowhite(eap->arg);
6278 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006279
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02006280 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006281 {
6282 int count = 0;
6283 char_u *start = skipwhite(line);
6284
6285 // :cmd xxx`=expr1`yyy`=expr2`zzz
6286 // PUSHS ":cmd xxx"
6287 // eval expr1
6288 // PUSHS "yyy"
6289 // eval expr2
6290 // PUSHS "zzz"
6291 // EXECCONCAT 5
6292 for (;;)
6293 {
6294 if (p > start)
6295 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02006296 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006297 ++count;
6298 }
6299 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006300 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006301 return NULL;
6302 may_generate_2STRING(-1, cctx);
6303 ++count;
6304 p = skipwhite(p);
6305 if (*p != '`')
6306 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006307 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006308 return NULL;
6309 }
6310 start = p + 1;
6311
6312 p = (char_u *)strstr((char *)start, "`=");
6313 if (p == NULL)
6314 {
6315 if (*skipwhite(start) != NUL)
6316 {
6317 generate_PUSHS(cctx, vim_strsave(start));
6318 ++count;
6319 }
6320 break;
6321 }
6322 }
6323 generate_EXECCONCAT(cctx, count);
6324 }
6325 else
6326 generate_EXEC(cctx, line);
6327
6328theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02006329 if (*nextcmd != NUL)
6330 {
6331 // the parser expects a pointer to the bar, put it back
6332 --nextcmd;
6333 *nextcmd = '|';
6334 }
6335
6336 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006337}
6338
6339/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02006340 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006341 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02006342 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006343 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02006344add_def_function(ufunc_T *ufunc)
6345{
6346 dfunc_T *dfunc;
6347
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006348 if (def_functions.ga_len == 0)
6349 {
6350 // The first position is not used, so that a zero uf_dfunc_idx means it
6351 // wasn't set.
6352 if (ga_grow(&def_functions, 1) == FAIL)
6353 return FAIL;
6354 ++def_functions.ga_len;
6355 }
6356
Bram Moolenaar09689a02020-05-09 22:50:08 +02006357 // Add the function to "def_functions".
6358 if (ga_grow(&def_functions, 1) == FAIL)
6359 return FAIL;
6360 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
6361 CLEAR_POINTER(dfunc);
6362 dfunc->df_idx = def_functions.ga_len;
6363 ufunc->uf_dfunc_idx = dfunc->df_idx;
6364 dfunc->df_ufunc = ufunc;
6365 ++def_functions.ga_len;
6366 return OK;
6367}
6368
6369/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006370 * After ex_function() has collected all the function lines: parse and compile
6371 * the lines into instructions.
6372 * Adds the function to "def_functions".
6373 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
6374 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006375 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006376 * This can be used recursively through compile_lambda(), which may reallocate
6377 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02006378 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006379 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02006380 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006381compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006382{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006383 char_u *line = NULL;
6384 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006385 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006386 cctx_T cctx;
6387 garray_T *instr;
6388 int called_emsg_before = called_emsg;
6389 int ret = FAIL;
6390 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006391 int do_estack_push;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006392 int emsg_before = called_emsg;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006393 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006394
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006395 // When using a function that was compiled before: Free old instructions.
6396 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006397 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006398 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02006399 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6400 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02006401 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006402 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006403 else
6404 {
6405 if (add_def_function(ufunc) == FAIL)
6406 return FAIL;
6407 new_def_function = TRUE;
6408 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006409
Bram Moolenaar985116a2020-07-12 17:31:09 +02006410 ufunc->uf_def_status = UF_COMPILING;
6411
Bram Moolenaara80faa82020-04-12 19:37:17 +02006412 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006413 cctx.ctx_ufunc = ufunc;
6414 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006415 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006416 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
6417 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
6418 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
6419 cctx.ctx_type_list = &ufunc->uf_type_list;
6420 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
6421 instr = &cctx.ctx_instr;
6422
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006423 // Set the context to the function, it may be compiled when called from
6424 // another script. Set the script version to the most modern one.
6425 // The line number will be set in next_line_from_context().
6426 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006427 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6428
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006429 // Make sure error messages are OK.
6430 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
6431 if (do_estack_push)
6432 estack_push_ufunc(ufunc, 1);
6433
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006434 if (ufunc->uf_def_args.ga_len > 0)
6435 {
6436 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006437 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006438 int i;
6439 char_u *arg;
6440 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006441 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006442
6443 // Produce instructions for the default values of optional arguments.
6444 // Store the instruction index in uf_def_arg_idx[] so that we know
6445 // where to start when the function is called, depending on the number
6446 // of arguments.
6447 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
6448 if (ufunc->uf_def_arg_idx == NULL)
6449 goto erret;
6450 for (i = 0; i < count; ++i)
6451 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006452 garray_T *stack = &cctx.ctx_type_stack;
6453 type_T *val_type;
6454 int arg_idx = first_def_arg + i;
6455
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006456 ufunc->uf_def_arg_idx[i] = instr->ga_len;
6457 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02006458 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006459 goto erret;
6460
6461 // If no type specified use the type of the default value.
6462 // Otherwise check that the default value type matches the
6463 // specified type.
6464 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6465 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006466 {
6467 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006468 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006469 }
Bram Moolenaare30f64b2020-07-15 19:48:20 +02006470 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02006471 == FAIL)
6472 {
6473 arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
6474 arg_idx + 1);
6475 goto erret;
6476 }
6477
6478 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006479 goto erret;
6480 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006481 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02006482
6483 if (did_set_arg_type)
6484 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01006485 }
6486
6487 /*
6488 * Loop over all the lines of the function and generate instructions.
6489 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006490 for (;;)
6491 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006492 exarg_T ea;
6493 cmdmod_T save_cmdmod;
6494 int starts_with_colon = FALSE;
6495 char_u *cmd;
6496 int save_msg_scroll = msg_scroll;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006497
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02006498 // Bail out on the first error to avoid a flood of errors and report
6499 // the right line number when inside try/catch.
6500 if (emsg_before != called_emsg)
6501 goto erret;
6502
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006503 if (line != NULL && *line == '|')
6504 // the line continues after a '|'
6505 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02006506 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02006507 && !(*line == '#' && (line == cctx.ctx_line_start
6508 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006509 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02006510 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006511 goto erret;
6512 }
6513 else
6514 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02006515 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02006516 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006517 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006518 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006519 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01006520 emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006521
Bram Moolenaara80faa82020-04-12 19:37:17 +02006522 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006523 ea.cmdlinep = &line;
6524 ea.cmd = skipwhite(line);
6525
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006526 // Some things can be recognized by the first character.
6527 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006528 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006529 case '#':
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006530 // "#" starts a comment, but "#{" does not.
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006531 if (ea.cmd[1] != '{')
6532 {
6533 line = (char_u *)"";
6534 continue;
6535 }
6536 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006537
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006538 case '}':
6539 {
6540 // "}" ends a block scope
6541 scopetype_T stype = cctx.ctx_scope == NULL
6542 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006543
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006544 if (stype == BLOCK_SCOPE)
6545 {
6546 compile_endblock(&cctx);
6547 line = ea.cmd;
6548 }
6549 else
6550 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006551 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006552 goto erret;
6553 }
6554 if (line != NULL)
6555 line = skipwhite(ea.cmd + 1);
6556 continue;
6557 }
6558
6559 case '{':
6560 // "{" starts a block scope
6561 // "{'a': 1}->func() is something else
6562 if (ends_excmd(*skipwhite(ea.cmd + 1)))
6563 {
6564 line = compile_block(ea.cmd, &cctx);
6565 continue;
6566 }
6567 break;
6568
6569 case ':':
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006570 starts_with_colon = TRUE;
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02006571 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006572 }
6573
6574 /*
6575 * COMMAND MODIFIERS
6576 */
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006577 save_cmdmod = cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006578 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
6579 {
6580 if (errormsg != NULL)
6581 goto erret;
6582 // empty line or comment
6583 line = (char_u *)"";
6584 continue;
6585 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02006586 // TODO: use modifiers in the command
6587 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02006588 cmdmod = save_cmdmod;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006589
6590 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006591 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006592 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02006593 {
6594 if (*ea.cmd == '(')
6595 // not for "call()"
6596 ea.cmd = p;
6597 else
6598 ea.cmd = skipwhite(ea.cmd);
6599 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006600
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006601 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006602 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006603 char_u *pskip;
6604
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006605 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006606 // find what follows.
6607 // Skip over "var.member", "var[idx]" and the like.
6608 // Also "&opt = val", "$ENV = val" and "@r = val".
6609 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006610 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006611 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02006612 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006613 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006614 char_u *var_end;
6615 int oplen;
6616 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006617
Bram Moolenaar65821722020-08-02 18:58:54 +02006618 if (ea.cmd[0] == '@')
6619 var_end = ea.cmd + 2;
6620 else
6621 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006622 FNE_CHECK_START | FNE_INCL_BR);
6623 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006624 if (oplen > 0)
6625 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006626 size_t len = p - ea.cmd;
6627
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006628 // Recognize an assignment if we recognize the variable
6629 // name:
6630 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01006631 // "local = expr" where "local" is a local var.
6632 // "script = expr" where "script" is a script-local var.
6633 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006634 // "&opt = expr"
6635 // "$ENV = expr"
6636 // "@r = expr"
6637 if (*ea.cmd == '&'
6638 || *ea.cmd == '$'
6639 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006640 || ((len) > 2 && ea.cmd[1] == ':')
6641 || lookup_local(ea.cmd, len, &cctx) != NULL
6642 || lookup_arg(ea.cmd, len, NULL, NULL,
6643 NULL, &cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02006644 || lookup_script(ea.cmd, len, FALSE) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006645 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006646 {
6647 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006648 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006649 goto erret;
6650 continue;
6651 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006652 }
6653 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006654
6655 if (*ea.cmd == '[')
6656 {
6657 // [var, var] = expr
6658 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
6659 if (line == NULL)
6660 goto erret;
6661 if (line != ea.cmd)
6662 continue;
6663 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006664 }
6665
6666 /*
6667 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006668 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006669 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006670 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02006671 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006672 {
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006673 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006674 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006675 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006676 if (!starts_with_colon)
6677 {
6678 emsg(_(e_colon_required_before_a_range));
6679 goto erret;
6680 }
6681 if (ends_excmd2(line, ea.cmd))
6682 {
6683 // A range without a command: jump to the line.
6684 // TODO: compile to a more efficient command, possibly
6685 // calling parse_cmd_address().
6686 ea.cmdidx = CMD_SIZE;
6687 line = compile_exec(line, &ea, &cctx);
6688 goto nextline;
6689 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02006690 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006691 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006692 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006693 : (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01006694 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006695
6696 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
6697 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006698 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006699 {
6700 line += STRLEN(line);
6701 continue;
6702 }
6703
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006704 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006705 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006706 {
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006707 // CMD_let cannot happen, compile_assignment() above is used
6708 iemsg("Command from find_ex_command() not handled");
6709 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006710 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006711 }
6712
6713 p = skipwhite(p);
6714
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006715 if (cctx.ctx_skip == SKIP_YES
Bram Moolenaar7e380032020-06-19 22:35:44 +02006716 && ea.cmdidx != CMD_if
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006717 && ea.cmdidx != CMD_elseif
6718 && ea.cmdidx != CMD_else
6719 && ea.cmdidx != CMD_endif)
6720 {
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006721 line = (char_u *)"";
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006722 continue;
6723 }
6724
Bram Moolenaarefd88552020-06-18 20:50:10 +02006725 if (ea.cmdidx != CMD_elseif
6726 && ea.cmdidx != CMD_else
6727 && ea.cmdidx != CMD_endif
6728 && ea.cmdidx != CMD_endfor
6729 && ea.cmdidx != CMD_endwhile
6730 && ea.cmdidx != CMD_catch
6731 && ea.cmdidx != CMD_finally
6732 && ea.cmdidx != CMD_endtry)
6733 {
6734 if (cctx.ctx_had_return)
6735 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006736 emsg(_(e_unreachable_code_after_return));
Bram Moolenaarefd88552020-06-18 20:50:10 +02006737 goto erret;
6738 }
6739 }
6740
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006741 switch (ea.cmdidx)
6742 {
6743 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02006744 ea.arg = p;
6745 line = compile_nested_function(&ea, &cctx);
6746 break;
6747
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006748 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006749 // TODO: should we allow this, e.g. to declare a global
6750 // function?
6751 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006752 goto erret;
6753
6754 case CMD_return:
6755 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006756 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006757 break;
6758
6759 case CMD_let:
6760 case CMD_const:
6761 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006762 if (line == p)
6763 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006764 break;
6765
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006766 case CMD_unlet:
6767 case CMD_unlockvar:
6768 case CMD_lockvar:
6769 line = compile_unletlock(p, &ea, &cctx);
6770 break;
6771
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006772 case CMD_import:
6773 line = compile_import(p, &cctx);
6774 break;
6775
6776 case CMD_if:
6777 line = compile_if(p, &cctx);
6778 break;
6779 case CMD_elseif:
6780 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006781 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006782 break;
6783 case CMD_else:
6784 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006785 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006786 break;
6787 case CMD_endif:
6788 line = compile_endif(p, &cctx);
6789 break;
6790
6791 case CMD_while:
6792 line = compile_while(p, &cctx);
6793 break;
6794 case CMD_endwhile:
6795 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006796 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006797 break;
6798
6799 case CMD_for:
6800 line = compile_for(p, &cctx);
6801 break;
6802 case CMD_endfor:
6803 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006804 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006805 break;
6806 case CMD_continue:
6807 line = compile_continue(p, &cctx);
6808 break;
6809 case CMD_break:
6810 line = compile_break(p, &cctx);
6811 break;
6812
6813 case CMD_try:
6814 line = compile_try(p, &cctx);
6815 break;
6816 case CMD_catch:
6817 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006818 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006819 break;
6820 case CMD_finally:
6821 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006822 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006823 break;
6824 case CMD_endtry:
6825 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006826 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006827 break;
6828 case CMD_throw:
6829 line = compile_throw(p, &cctx);
6830 break;
6831
Bram Moolenaar007f9d62020-07-06 23:04:49 +02006832 case CMD_eval:
6833 if (compile_expr0(&p, &cctx) == FAIL)
6834 goto erret;
6835
6836 // drop the return value
6837 generate_instr_drop(&cctx, ISN_DROP, 1);
6838
6839 line = skipwhite(p);
6840 break;
6841
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006842 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006843 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01006844 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006845 case CMD_echomsg:
6846 case CMD_echoerr:
6847 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01006848 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006849
Bram Moolenaardf069ee2020-06-22 23:02:51 +02006850 // TODO: other commands with an expression argument
6851
Bram Moolenaarae616492020-07-28 20:07:27 +02006852 case CMD_append:
6853 case CMD_change:
6854 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02006855 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02006856 case CMD_xit:
6857 not_in_vim9(&ea);
6858 goto erret;
6859
Bram Moolenaar002262f2020-07-08 17:47:57 +02006860 case CMD_SIZE:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006861 semsg(_(e_invalid_command_str), ea.cmd);
Bram Moolenaar002262f2020-07-08 17:47:57 +02006862 goto erret;
6863
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006864 default:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006865 // Not recognized, execute with do_cmdline_cmd().
6866 ea.arg = p;
6867 line = compile_exec(line, &ea, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006868 break;
6869 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02006870nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006871 if (line == NULL)
6872 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02006873 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006874
6875 if (cctx.ctx_type_stack.ga_len < 0)
6876 {
6877 iemsg("Type stack underflow");
6878 goto erret;
6879 }
6880 }
6881
6882 if (cctx.ctx_scope != NULL)
6883 {
6884 if (cctx.ctx_scope->se_type == IF_SCOPE)
6885 emsg(_(e_endif));
6886 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
6887 emsg(_(e_endwhile));
6888 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
6889 emsg(_(e_endfor));
6890 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006891 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006892 goto erret;
6893 }
6894
Bram Moolenaarefd88552020-06-18 20:50:10 +02006895 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006896 {
6897 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
6898 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006899 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006900 goto erret;
6901 }
6902
6903 // Return zero if there is no return at the end.
6904 generate_PUSHNR(&cctx, 0);
6905 generate_instr(&cctx, ISN_RETURN);
6906 }
6907
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006908 {
6909 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6910 + ufunc->uf_dfunc_idx;
6911 dfunc->df_deleted = FALSE;
6912 dfunc->df_instr = instr->ga_data;
6913 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006914 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006915 dfunc->df_closure_count = cctx.ctx_closure_count;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02006916 if (cctx.ctx_outer_used)
6917 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006918 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006919 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006920
6921 ret = OK;
6922
6923erret:
6924 if (ret == FAIL)
6925 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006926 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02006927 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6928 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006929
6930 for (idx = 0; idx < instr->ga_len; ++idx)
6931 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006932 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006933
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006934 // If using the last entry in the table and it was added above, we
6935 // might as well remove it.
6936 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02006937 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006938 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01006939 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02006940 ufunc->uf_dfunc_idx = 0;
6941 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006942 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006943
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006944 while (cctx.ctx_scope != NULL)
6945 drop_scope(&cctx);
6946
Bram Moolenaar20431c92020-03-20 18:39:46 +01006947 // Don't execute this function body.
6948 ga_clear_strings(&ufunc->uf_lines);
6949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006950 if (errormsg != NULL)
6951 emsg(errormsg);
6952 else if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006953 emsg(_(e_compile_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006954 }
6955
6956 current_sctx = save_current_sctx;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02006957 if (do_estack_push)
6958 estack_pop();
6959
Bram Moolenaar20431c92020-03-20 18:39:46 +01006960 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006961 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006962 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02006963 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006964}
6965
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02006966 void
6967set_function_type(ufunc_T *ufunc)
6968{
6969 int varargs = ufunc->uf_va_name != NULL;
6970 int argcount = ufunc->uf_args.ga_len;
6971
6972 // Create a type for the function, with the return type and any
6973 // argument types.
6974 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
6975 // The type is included in "tt_args".
6976 if (argcount > 0 || varargs)
6977 {
6978 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
6979 argcount, &ufunc->uf_type_list);
6980 // Add argument types to the function type.
6981 if (func_type_add_arg_types(ufunc->uf_func_type,
6982 argcount + varargs,
6983 &ufunc->uf_type_list) == FAIL)
6984 return;
6985 ufunc->uf_func_type->tt_argcount = argcount + varargs;
6986 ufunc->uf_func_type->tt_min_argcount =
6987 argcount - ufunc->uf_def_args.ga_len;
6988 if (ufunc->uf_arg_types == NULL)
6989 {
6990 int i;
6991
6992 // lambda does not have argument types.
6993 for (i = 0; i < argcount; ++i)
6994 ufunc->uf_func_type->tt_args[i] = &t_any;
6995 }
6996 else
6997 mch_memmove(ufunc->uf_func_type->tt_args,
6998 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
6999 if (varargs)
7000 {
7001 ufunc->uf_func_type->tt_args[argcount] =
7002 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
7003 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
7004 }
7005 }
7006 else
7007 // No arguments, can use a predefined type.
7008 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
7009 argcount, &ufunc->uf_type_list);
7010}
7011
7012
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007013/*
7014 * Delete an instruction, free what it contains.
7015 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01007016 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007017delete_instr(isn_T *isn)
7018{
7019 switch (isn->isn_type)
7020 {
7021 case ISN_EXEC:
7022 case ISN_LOADENV:
7023 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007024 case ISN_LOADB:
7025 case ISN_LOADW:
7026 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007027 case ISN_LOADOPT:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007028 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007029 case ISN_PUSHEXC:
7030 case ISN_PUSHS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007031 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007032 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02007033 case ISN_STOREB:
7034 case ISN_STOREW:
7035 case ISN_STORET:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007036 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007037 vim_free(isn->isn_arg.string);
7038 break;
7039
7040 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01007041 case ISN_STORES:
7042 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007043 break;
7044
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007045 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02007046 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007047 vim_free(isn->isn_arg.unlet.ul_name);
7048 break;
7049
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007050 case ISN_STOREOPT:
7051 vim_free(isn->isn_arg.storeopt.so_name);
7052 break;
7053
7054 case ISN_PUSHBLOB: // push blob isn_arg.blob
7055 blob_unref(isn->isn_arg.blob);
7056 break;
7057
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007058 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007059#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007060 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007061#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007062 break;
7063
7064 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007065#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007066 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01007067#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01007068 break;
7069
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007070 case ISN_UCALL:
7071 vim_free(isn->isn_arg.ufunc.cuf_name);
7072 break;
7073
Bram Moolenaar221fcc72020-05-05 19:46:20 +02007074 case ISN_FUNCREF:
7075 {
7076 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7077 + isn->isn_arg.funcref.fr_func;
7078 func_ptr_unref(dfunc->df_ufunc);
7079 }
7080 break;
7081
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007082 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02007083 {
7084 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
7085 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
7086
7087 if (ufunc != NULL)
7088 {
7089 // Clear uf_dfunc_idx so that the function is deleted.
7090 clear_def_function(ufunc);
7091 ufunc->uf_dfunc_idx = 0;
7092 func_ptr_unref(ufunc);
7093 }
7094
7095 vim_free(lambda);
7096 vim_free(isn->isn_arg.newfunc.nf_global);
7097 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02007098 break;
7099
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007100 case ISN_2BOOL:
7101 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02007102 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007103 case ISN_ADDBLOB:
7104 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007105 case ISN_ANYINDEX:
7106 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007107 case ISN_BCALL:
7108 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007109 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007110 case ISN_CHECKNR:
7111 case ISN_CHECKTYPE:
7112 case ISN_COMPAREANY:
7113 case ISN_COMPAREBLOB:
7114 case ISN_COMPAREBOOL:
7115 case ISN_COMPAREDICT:
7116 case ISN_COMPAREFLOAT:
7117 case ISN_COMPAREFUNC:
7118 case ISN_COMPARELIST:
7119 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007120 case ISN_COMPARESPECIAL:
7121 case ISN_COMPARESTRING:
7122 case ISN_CONCAT:
7123 case ISN_DCALL:
7124 case ISN_DROP:
7125 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007126 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007127 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007128 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007129 case ISN_EXECCONCAT:
7130 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007131 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007132 case ISN_GETITEM:
7133 case ISN_JUMP:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02007134 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02007135 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007136 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007137 case ISN_LOADBDICT:
7138 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007139 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007140 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007141 case ISN_LOADSCRIPT:
7142 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007143 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02007144 case ISN_LOADWDICT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007145 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007146 case ISN_NEGATENR:
7147 case ISN_NEWDICT:
7148 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007149 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007150 case ISN_OPFLOAT:
7151 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007152 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007153 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007154 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007155 case ISN_PUSHF:
7156 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007157 case ISN_PUSHSPEC:
7158 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007159 case ISN_SHUFFLE:
7160 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007161 case ISN_STORE:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007162 case ISN_STOREDICT:
7163 case ISN_STORELIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02007164 case ISN_STORENR:
7165 case ISN_STOREOUTER:
7166 case ISN_STOREREG:
7167 case ISN_STORESCRIPT:
7168 case ISN_STOREV:
7169 case ISN_STRINDEX:
7170 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007171 case ISN_THROW:
7172 case ISN_TRY:
7173 // nothing allocated
7174 break;
7175 }
7176}
7177
7178/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01007179 * Free all instructions for "dfunc".
7180 */
7181 static void
7182delete_def_function_contents(dfunc_T *dfunc)
7183{
7184 int idx;
7185
7186 ga_clear(&dfunc->df_def_args_isn);
7187
7188 if (dfunc->df_instr != NULL)
7189 {
7190 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
7191 delete_instr(dfunc->df_instr + idx);
7192 VIM_CLEAR(dfunc->df_instr);
7193 }
7194
7195 dfunc->df_deleted = TRUE;
7196}
7197
7198/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007199 * When a user function is deleted, clear the contents of any associated def
7200 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007201 */
7202 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007203clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007204{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007205 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007206 {
7207 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7208 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007209
Bram Moolenaar20431c92020-03-20 18:39:46 +01007210 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007211 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007212 }
7213}
7214
7215#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01007216/*
7217 * Free all functions defined with ":def".
7218 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007219 void
7220free_def_functions(void)
7221{
Bram Moolenaar20431c92020-03-20 18:39:46 +01007222 int idx;
7223
7224 for (idx = 0; idx < def_functions.ga_len; ++idx)
7225 {
7226 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
7227
7228 delete_def_function_contents(dfunc);
7229 }
7230
7231 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007232}
7233#endif
7234
7235
7236#endif // FEAT_EVAL