blob: 18bf68b0c020d32096d99b62eb02cda6e39e3b0a [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
111 int lv_from_outer; // when TRUE using ctx_outer scope
112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200129 int ctx_has_closure; // set to one if a closures was created in
130 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200144
Bram Moolenaar02194d22020-10-24 23:08:38 +0200145 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100146};
147
Bram Moolenaar20431c92020-03-20 18:39:46 +0100148static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149
150/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100151 * Lookup variable "name" in the local scope and return it in "lvar".
152 * "lvar->lv_from_outer" is set accordingly.
153 * If "lvar" is NULL only check if the variable can be found.
154 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100155 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100156 static int
157lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158{
159 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100160 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100161
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100162 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100163 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200164
165 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100166 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
167 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100168 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
169 if (STRNCMP(name, lvp->lv_name, len) == 0
170 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200171 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100172 if (lvar != NULL)
173 {
174 *lvar = *lvp;
175 lvar->lv_from_outer = FALSE;
176 }
177 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200180
181 // Find local in outer function scope.
182 if (cctx->ctx_outer != NULL)
183 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100184 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200185 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100186 if (lvar != NULL)
187 {
188 cctx->ctx_outer_used = TRUE;
189 lvar->lv_from_outer = TRUE;
190 }
191 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200192 }
193 }
194
Bram Moolenaar709664c2020-12-12 14:33:41 +0100195 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196}
197
198/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200199 * Lookup an argument in the current function and an enclosing function.
200 * Returns the argument index in "idxp"
201 * Returns the argument type in "type"
202 * Sets "gen_load_outer" to TRUE if found in outer scope.
203 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100204 */
205 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200206arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200207 char_u *name,
208 size_t len,
209 int *idxp,
210 type_T **type,
211 int *gen_load_outer,
212 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213{
214 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200215 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100217 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200218 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
220 {
221 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
222
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200223 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
224 {
225 if (idxp != NULL)
226 {
227 // Arguments are located above the frame pointer. One further
228 // if there is a vararg argument
229 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
230 + STACK_FRAME_SIZE)
231 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
232
233 if (cctx->ctx_ufunc->uf_arg_types != NULL)
234 *type = cctx->ctx_ufunc->uf_arg_types[idx];
235 else
236 *type = &t_any;
237 }
238 return OK;
239 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100240 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100241
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200242 va_name = cctx->ctx_ufunc->uf_va_name;
243 if (va_name != NULL
244 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
245 {
246 if (idxp != NULL)
247 {
248 // varargs is always the last argument
249 *idxp = -STACK_FRAME_SIZE - 1;
250 *type = cctx->ctx_ufunc->uf_va_type;
251 }
252 return OK;
253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200255 if (cctx->ctx_outer != NULL)
256 {
257 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200258 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200259 == OK)
260 {
261 *gen_load_outer = TRUE;
262 return OK;
263 }
264 }
265
266 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100267}
268
269/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200270 * Lookup a script-local variable in the current script, possibly defined in a
271 * block that contains the function "cctx->ctx_ufunc".
272 * "cctx" is NULL at the script level.
273 * if "len" is <= 0 "name" must be NUL terminated.
274 * Return NULL when not found.
275 */
276 static sallvar_T *
277find_script_var(char_u *name, size_t len, cctx_T *cctx)
278{
279 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
280 hashitem_T *hi;
281 int cc;
282 sallvar_T *sav;
283 ufunc_T *ufunc;
284
285 // Find the list of all script variables with the right name.
286 if (len > 0)
287 {
288 cc = name[len];
289 name[len] = NUL;
290 }
291 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
292 if (len > 0)
293 name[len] = cc;
294 if (HASHITEM_EMPTY(hi))
295 return NULL;
296
297 sav = HI2SAV(hi);
298 if (sav->sav_block_id == 0 || cctx == NULL)
299 // variable defined in the script scope or not in a function.
300 return sav;
301
302 // Go over the variables with this name and find one that was visible
303 // from the function.
304 ufunc = cctx->ctx_ufunc;
305 while (sav != NULL)
306 {
307 int idx;
308
309 // Go over the blocks that this function was defined in. If the
310 // variable block ID matches it was visible to the function.
311 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
312 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
313 return sav;
314 sav = sav->sav_next;
315 }
316
317 return NULL;
318}
319
320/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100321 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200322 */
323 static int
324script_is_vim9()
325{
326 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
327}
328
329/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200330 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200331 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
332 * without "s:".
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200333 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100334 * Returns OK or FAIL.
335 */
336 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200337script_var_exists(char_u *name, size_t len, int vim9script, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100338{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200339 int is_vim9_script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200341 if (current_sctx.sc_sid <= 0)
342 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200343 is_vim9_script = script_is_vim9();
344 if (vim9script && !is_vim9_script)
Bram Moolenaar53900992020-08-22 19:02:02 +0200345 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200346 if (is_vim9_script)
347 {
348 // Check script variables that were visible where the function was
349 // defined.
350 if (find_script_var(name, len, cctx) != NULL)
351 return OK;
352 }
353 else
354 {
355 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
356 dictitem_T *di;
357 int cc;
358
359 // Check script variables that are currently visible
360 cc = name[len];
361 name[len] = NUL;
362 di = find_var_in_ht(ht, 0, name, TRUE);
363 name[len] = cc;
364 if (di != NULL)
365 return OK;
366 }
367
368 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100369}
370
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100371/*
372 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200373 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200374 * Does not check the global namespace.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100375 * Return FAIL and give an error if it defined.
376 */
377 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200378check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100379{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200380 int c = p[len];
381 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200382
383 p[len] = NUL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200384 if (script_var_exists(p, len, FALSE, cctx) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100385 || (cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100386 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200387 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200388 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200389 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100390 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200391 // A local or script-local function can shadow a global function.
392 if (ufunc == NULL || !func_is_global(ufunc)
393 || (p[0] == 'g' && p[1] == ':'))
394 {
395 p[len] = c;
396 semsg(_(e_name_already_defined_str), p);
397 return FAIL;
398 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100399 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200400 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100401 return OK;
402}
403
Bram Moolenaar65b95452020-07-19 14:03:09 +0200404
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100405/////////////////////////////////////////////////////////////////////
406// Following generate_ functions expect the caller to call ga_grow().
407
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200408#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
409#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100411/*
412 * Generate an instruction without arguments.
413 * Returns a pointer to the new instruction, NULL if failed.
414 */
415 static isn_T *
416generate_instr(cctx_T *cctx, isntype_T isn_type)
417{
418 garray_T *instr = &cctx->ctx_instr;
419 isn_T *isn;
420
Bram Moolenaar080457c2020-03-03 21:53:32 +0100421 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100422 if (ga_grow(instr, 1) == FAIL)
423 return NULL;
424 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
425 isn->isn_type = isn_type;
426 isn->isn_lnum = cctx->ctx_lnum + 1;
427 ++instr->ga_len;
428
429 return isn;
430}
431
432/*
433 * Generate an instruction without arguments.
434 * "drop" will be removed from the stack.
435 * Returns a pointer to the new instruction, NULL if failed.
436 */
437 static isn_T *
438generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
439{
440 garray_T *stack = &cctx->ctx_type_stack;
441
Bram Moolenaar080457c2020-03-03 21:53:32 +0100442 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100443 stack->ga_len -= drop;
444 return generate_instr(cctx, isn_type);
445}
446
447/*
448 * Generate instruction "isn_type" and put "type" on the type stack.
449 */
450 static isn_T *
451generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
452{
453 isn_T *isn;
454 garray_T *stack = &cctx->ctx_type_stack;
455
456 if ((isn = generate_instr(cctx, isn_type)) == NULL)
457 return NULL;
458
459 if (ga_grow(stack, 1) == FAIL)
460 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200461 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462 ++stack->ga_len;
463
464 return isn;
465}
466
467/*
468 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200469 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100470 */
471 static int
472may_generate_2STRING(int offset, cctx_T *cctx)
473{
474 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200475 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100476 garray_T *stack = &cctx->ctx_type_stack;
477 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
478
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200479 switch ((*type)->tt_type)
480 {
481 // nothing to be done
482 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100483
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200484 // conversion possible
485 case VAR_SPECIAL:
486 case VAR_BOOL:
487 case VAR_NUMBER:
488 case VAR_FLOAT:
489 break;
490
491 // conversion possible (with runtime check)
492 case VAR_ANY:
493 case VAR_UNKNOWN:
494 isntype = ISN_2STRING_ANY;
495 break;
496
497 // conversion not possible
498 case VAR_VOID:
499 case VAR_BLOB:
500 case VAR_FUNC:
501 case VAR_PARTIAL:
502 case VAR_LIST:
503 case VAR_DICT:
504 case VAR_JOB:
505 case VAR_CHANNEL:
506 to_string_error((*type)->tt_type);
507 return FAIL;
508 }
509
510 *type = &t_string;
511 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100512 return FAIL;
513 isn->isn_arg.number = offset;
514
515 return OK;
516}
517
518 static int
519check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
520{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200521 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100522 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200523 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524 {
525 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200526 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200528 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 return FAIL;
530 }
531 return OK;
532}
533
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200534 static int
535generate_add_instr(
536 cctx_T *cctx,
537 vartype_T vartype,
538 type_T *type1,
539 type_T *type2)
540{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100541 garray_T *stack = &cctx->ctx_type_stack;
542 isn_T *isn = generate_instr_drop(cctx,
543 vartype == VAR_NUMBER ? ISN_OPNR
544 : vartype == VAR_LIST ? ISN_ADDLIST
545 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200546#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100547 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200548#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100549 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200550
551 if (vartype != VAR_LIST && vartype != VAR_BLOB
552 && type1->tt_type != VAR_ANY
553 && type2->tt_type != VAR_ANY
554 && check_number_or_float(
555 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
556 return FAIL;
557
558 if (isn != NULL)
559 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100560
561 // When concatenating two lists with different member types the member type
562 // becomes "any".
563 if (vartype == VAR_LIST
564 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
565 && type1->tt_member != type2->tt_member)
566 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
567
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200568 return isn == NULL ? FAIL : OK;
569}
570
571/*
572 * Get the type to use for an instruction for an operation on "type1" and
573 * "type2". If they are matching use a type-specific instruction. Otherwise
574 * fall back to runtime type checking.
575 */
576 static vartype_T
577operator_type(type_T *type1, type_T *type2)
578{
579 if (type1->tt_type == type2->tt_type
580 && (type1->tt_type == VAR_NUMBER
581 || type1->tt_type == VAR_LIST
582#ifdef FEAT_FLOAT
583 || type1->tt_type == VAR_FLOAT
584#endif
585 || type1->tt_type == VAR_BLOB))
586 return type1->tt_type;
587 return VAR_ANY;
588}
589
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100590/*
591 * Generate an instruction with two arguments. The instruction depends on the
592 * type of the arguments.
593 */
594 static int
595generate_two_op(cctx_T *cctx, char_u *op)
596{
597 garray_T *stack = &cctx->ctx_type_stack;
598 type_T *type1;
599 type_T *type2;
600 vartype_T vartype;
601 isn_T *isn;
602
Bram Moolenaar080457c2020-03-03 21:53:32 +0100603 RETURN_OK_IF_SKIP(cctx);
604
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200605 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100606 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
607 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200608 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100609
610 switch (*op)
611 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200612 case '+':
613 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615 break;
616
617 case '-':
618 case '*':
619 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
620 op) == FAIL)
621 return FAIL;
622 if (vartype == VAR_NUMBER)
623 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
624#ifdef FEAT_FLOAT
625 else if (vartype == VAR_FLOAT)
626 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
627#endif
628 else
629 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
630 if (isn != NULL)
631 isn->isn_arg.op.op_type = *op == '*'
632 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
633 break;
634
Bram Moolenaar4c683752020-04-05 21:38:23 +0200635 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100636 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200637 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100638 && type2->tt_type != VAR_NUMBER))
639 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200640 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100641 return FAIL;
642 }
643 isn = generate_instr_drop(cctx,
644 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
645 if (isn != NULL)
646 isn->isn_arg.op.op_type = EXPR_REM;
647 break;
648 }
649
650 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200651 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100652 {
653 type_T *type = &t_any;
654
655#ifdef FEAT_FLOAT
656 // float+number and number+float results in float
657 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
658 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
659 type = &t_float;
660#endif
661 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
662 }
663
664 return OK;
665}
666
667/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200668 * Get the instruction to use for comparing "type1" with "type2"
669 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200671 static isntype_T
672get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673{
674 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675
Bram Moolenaar4c683752020-04-05 21:38:23 +0200676 if (type1 == VAR_UNKNOWN)
677 type1 = VAR_ANY;
678 if (type2 == VAR_UNKNOWN)
679 type2 = VAR_ANY;
680
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681 if (type1 == type2)
682 {
683 switch (type1)
684 {
685 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
686 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
687 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
688 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
689 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
690 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
691 case VAR_LIST: isntype = ISN_COMPARELIST; break;
692 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
693 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694 default: isntype = ISN_COMPAREANY; break;
695 }
696 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200697 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100698 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
699 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
700 isntype = ISN_COMPAREANY;
701
702 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
703 && (isntype == ISN_COMPAREBOOL
704 || isntype == ISN_COMPARESPECIAL
705 || isntype == ISN_COMPARENR
706 || isntype == ISN_COMPAREFLOAT))
707 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200708 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100709 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200710 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711 }
712 if (isntype == ISN_DROP
713 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
714 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
715 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
716 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
717 && exptype != EXPR_IS && exptype != EXPR_ISNOT
718 && (type1 == VAR_BLOB || type2 == VAR_BLOB
719 || type1 == VAR_LIST || type2 == VAR_LIST))))
720 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200721 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200723 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200725 return isntype;
726}
727
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200728 int
729check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
730{
731 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
732 return FAIL;
733 return OK;
734}
735
Bram Moolenaara5565e42020-05-09 15:44:01 +0200736/*
737 * Generate an ISN_COMPARE* instruction with a boolean result.
738 */
739 static int
740generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
741{
742 isntype_T isntype;
743 isn_T *isn;
744 garray_T *stack = &cctx->ctx_type_stack;
745 vartype_T type1;
746 vartype_T type2;
747
748 RETURN_OK_IF_SKIP(cctx);
749
750 // Get the known type of the two items on the stack. If they are matching
751 // use a type-specific instruction. Otherwise fall back to runtime type
752 // checking.
753 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
754 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
755 isntype = get_compare_isn(exptype, type1, type2);
756 if (isntype == ISN_DROP)
757 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758
759 if ((isn = generate_instr(cctx, isntype)) == NULL)
760 return FAIL;
761 isn->isn_arg.op.op_type = exptype;
762 isn->isn_arg.op.op_ic = ic;
763
764 // takes two arguments, puts one bool back
765 if (stack->ga_len >= 2)
766 {
767 --stack->ga_len;
768 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
769 }
770
771 return OK;
772}
773
774/*
775 * Generate an ISN_2BOOL instruction.
776 */
777 static int
778generate_2BOOL(cctx_T *cctx, int invert)
779{
780 isn_T *isn;
781 garray_T *stack = &cctx->ctx_type_stack;
782
Bram Moolenaar080457c2020-03-03 21:53:32 +0100783 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
785 return FAIL;
786 isn->isn_arg.number = invert;
787
788 // type becomes bool
789 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
790
791 return OK;
792}
793
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200794/*
795 * Generate an ISN_COND2BOOL instruction.
796 */
797 static int
798generate_COND2BOOL(cctx_T *cctx)
799{
800 isn_T *isn;
801 garray_T *stack = &cctx->ctx_type_stack;
802
803 RETURN_OK_IF_SKIP(cctx);
804 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
805 return FAIL;
806
807 // type becomes bool
808 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
809
810 return OK;
811}
812
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200814generate_TYPECHECK(
815 cctx_T *cctx,
816 type_T *expected,
817 int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100818{
819 isn_T *isn;
820 garray_T *stack = &cctx->ctx_type_stack;
821
Bram Moolenaar080457c2020-03-03 21:53:32 +0100822 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
824 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200825 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826 isn->isn_arg.type.ct_off = offset;
827
Bram Moolenaar5e654232020-09-16 15:22:00 +0200828 // type becomes expected
829 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100830
831 return OK;
832}
833
834/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200835 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
836 * used. Return FALSE if the types will never match.
837 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100838 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200839use_typecheck(type_T *actual, type_T *expected)
840{
841 if (actual->tt_type == VAR_ANY
842 || actual->tt_type == VAR_UNKNOWN
843 || (actual->tt_type == VAR_FUNC
844 && (expected->tt_type == VAR_FUNC
845 || expected->tt_type == VAR_PARTIAL)
846 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
847 return TRUE;
848 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
849 && actual->tt_type == expected->tt_type)
850 // This takes care of a nested list or dict.
851 return use_typecheck(actual->tt_member, expected->tt_member);
852 return FALSE;
853}
854
855/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200856 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200857 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200858 * - "actual" is a type that can be "expected" type: add a runtime check; or
859 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200860 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
861 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200862 */
863 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200864need_type(
865 type_T *actual,
866 type_T *expected,
867 int offset,
868 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200869 int silent,
870 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200871{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200872 if (expected == &t_bool && actual != &t_bool
873 && (actual->tt_flags & TTFLAG_BOOL_OK))
874 {
875 // Using "0", "1" or the result of an expression with "&&" or "||" as a
876 // boolean is OK but requires a conversion.
877 generate_2BOOL(cctx, FALSE);
878 return OK;
879 }
880
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200881 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200882 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200883
884 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200885 // If it's a constant a runtime check makes no sense.
886 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200887 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200888 generate_TYPECHECK(cctx, expected, offset);
889 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200890 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200891
892 if (!silent)
893 type_mismatch(expected, actual);
894 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200895}
896
897/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100898 * Check that the top of the type stack has a type that can be used as a
899 * condition. Give an error and return FAIL if not.
900 */
901 static int
902bool_on_stack(cctx_T *cctx)
903{
904 garray_T *stack = &cctx->ctx_type_stack;
905 type_T *type;
906
907 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
908 if (type == &t_bool)
909 return OK;
910
911 if (type == &t_any || type == &t_number)
912 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
913 // This requires a runtime type check.
914 return generate_COND2BOOL(cctx);
915
916 return need_type(type, &t_bool, -1, cctx, FALSE, FALSE);
917}
918
919/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100920 * Generate an ISN_PUSHNR instruction.
921 */
922 static int
923generate_PUSHNR(cctx_T *cctx, varnumber_T number)
924{
925 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200926 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927
Bram Moolenaar080457c2020-03-03 21:53:32 +0100928 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100929 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
930 return FAIL;
931 isn->isn_arg.number = number;
932
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200933 if (number == 0 || number == 1)
934 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200935 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200936
937 // A 0 or 1 number can also be used as a bool.
938 if (type != NULL)
939 {
940 type->tt_type = VAR_NUMBER;
941 type->tt_flags = TTFLAG_BOOL_OK;
942 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
943 }
944 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100945 return OK;
946}
947
948/*
949 * Generate an ISN_PUSHBOOL instruction.
950 */
951 static int
952generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
953{
954 isn_T *isn;
955
Bram Moolenaar080457c2020-03-03 21:53:32 +0100956 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100957 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
958 return FAIL;
959 isn->isn_arg.number = number;
960
961 return OK;
962}
963
964/*
965 * Generate an ISN_PUSHSPEC instruction.
966 */
967 static int
968generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
969{
970 isn_T *isn;
971
Bram Moolenaar080457c2020-03-03 21:53:32 +0100972 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100973 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
974 return FAIL;
975 isn->isn_arg.number = number;
976
977 return OK;
978}
979
980#ifdef FEAT_FLOAT
981/*
982 * Generate an ISN_PUSHF instruction.
983 */
984 static int
985generate_PUSHF(cctx_T *cctx, float_T fnumber)
986{
987 isn_T *isn;
988
Bram Moolenaar080457c2020-03-03 21:53:32 +0100989 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
991 return FAIL;
992 isn->isn_arg.fnumber = fnumber;
993
994 return OK;
995}
996#endif
997
998/*
999 * Generate an ISN_PUSHS instruction.
1000 * Consumes "str".
1001 */
1002 static int
1003generate_PUSHS(cctx_T *cctx, char_u *str)
1004{
1005 isn_T *isn;
1006
Bram Moolenaar080457c2020-03-03 21:53:32 +01001007 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001008 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1009 return FAIL;
1010 isn->isn_arg.string = str;
1011
1012 return OK;
1013}
1014
1015/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001016 * Generate an ISN_PUSHCHANNEL instruction.
1017 * Consumes "channel".
1018 */
1019 static int
1020generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1021{
1022 isn_T *isn;
1023
Bram Moolenaar080457c2020-03-03 21:53:32 +01001024 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001025 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1026 return FAIL;
1027 isn->isn_arg.channel = channel;
1028
1029 return OK;
1030}
1031
1032/*
1033 * Generate an ISN_PUSHJOB instruction.
1034 * Consumes "job".
1035 */
1036 static int
1037generate_PUSHJOB(cctx_T *cctx, job_T *job)
1038{
1039 isn_T *isn;
1040
Bram Moolenaar080457c2020-03-03 21:53:32 +01001041 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001042 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001043 return FAIL;
1044 isn->isn_arg.job = job;
1045
1046 return OK;
1047}
1048
1049/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050 * Generate an ISN_PUSHBLOB instruction.
1051 * Consumes "blob".
1052 */
1053 static int
1054generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1055{
1056 isn_T *isn;
1057
Bram Moolenaar080457c2020-03-03 21:53:32 +01001058 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1060 return FAIL;
1061 isn->isn_arg.blob = blob;
1062
1063 return OK;
1064}
1065
1066/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001067 * Generate an ISN_PUSHFUNC instruction with name "name".
1068 * Consumes "name".
1069 */
1070 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001071generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001072{
1073 isn_T *isn;
1074
Bram Moolenaar080457c2020-03-03 21:53:32 +01001075 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001076 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001077 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001078 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001079
1080 return OK;
1081}
1082
1083/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001084 * Generate an ISN_GETITEM instruction with "index".
1085 */
1086 static int
1087generate_GETITEM(cctx_T *cctx, int index)
1088{
1089 isn_T *isn;
1090 garray_T *stack = &cctx->ctx_type_stack;
1091 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1092 type_T *item_type = &t_any;
1093
1094 RETURN_OK_IF_SKIP(cctx);
1095
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001096 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001097 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001098 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001099 emsg(_(e_listreq));
1100 return FAIL;
1101 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001102 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001103 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1104 return FAIL;
1105 isn->isn_arg.number = index;
1106
1107 // add the item type to the type stack
1108 if (ga_grow(stack, 1) == FAIL)
1109 return FAIL;
1110 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1111 ++stack->ga_len;
1112 return OK;
1113}
1114
1115/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001116 * Generate an ISN_SLICE instruction with "count".
1117 */
1118 static int
1119generate_SLICE(cctx_T *cctx, int count)
1120{
1121 isn_T *isn;
1122
1123 RETURN_OK_IF_SKIP(cctx);
1124 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1125 return FAIL;
1126 isn->isn_arg.number = count;
1127 return OK;
1128}
1129
1130/*
1131 * Generate an ISN_CHECKLEN instruction with "min_len".
1132 */
1133 static int
1134generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1135{
1136 isn_T *isn;
1137
1138 RETURN_OK_IF_SKIP(cctx);
1139
1140 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1141 return FAIL;
1142 isn->isn_arg.checklen.cl_min_len = min_len;
1143 isn->isn_arg.checklen.cl_more_OK = more_OK;
1144
1145 return OK;
1146}
1147
1148/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149 * Generate an ISN_STORE instruction.
1150 */
1151 static int
1152generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1153{
1154 isn_T *isn;
1155
Bram Moolenaar080457c2020-03-03 21:53:32 +01001156 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001157 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1158 return FAIL;
1159 if (name != NULL)
1160 isn->isn_arg.string = vim_strsave(name);
1161 else
1162 isn->isn_arg.number = idx;
1163
1164 return OK;
1165}
1166
1167/*
1168 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1169 */
1170 static int
1171generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1172{
1173 isn_T *isn;
1174
Bram Moolenaar080457c2020-03-03 21:53:32 +01001175 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001176 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1177 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001178 isn->isn_arg.storenr.stnr_idx = idx;
1179 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001180
1181 return OK;
1182}
1183
1184/*
1185 * Generate an ISN_STOREOPT instruction
1186 */
1187 static int
1188generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1189{
1190 isn_T *isn;
1191
Bram Moolenaar080457c2020-03-03 21:53:32 +01001192 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001193 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194 return FAIL;
1195 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1196 isn->isn_arg.storeopt.so_flags = opt_flags;
1197
1198 return OK;
1199}
1200
1201/*
1202 * Generate an ISN_LOAD or similar instruction.
1203 */
1204 static int
1205generate_LOAD(
1206 cctx_T *cctx,
1207 isntype_T isn_type,
1208 int idx,
1209 char_u *name,
1210 type_T *type)
1211{
1212 isn_T *isn;
1213
Bram Moolenaar080457c2020-03-03 21:53:32 +01001214 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1216 return FAIL;
1217 if (name != NULL)
1218 isn->isn_arg.string = vim_strsave(name);
1219 else
1220 isn->isn_arg.number = idx;
1221
1222 return OK;
1223}
1224
1225/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001226 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001227 */
1228 static int
1229generate_LOADV(
1230 cctx_T *cctx,
1231 char_u *name,
1232 int error)
1233{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001234 int di_flags;
1235 int vidx = find_vim_var(name, &di_flags);
1236 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001237
Bram Moolenaar080457c2020-03-03 21:53:32 +01001238 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001239 if (vidx < 0)
1240 {
1241 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001242 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001243 return FAIL;
1244 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001245 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001246
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001247 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001248}
1249
1250/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001251 * Generate an ISN_UNLET instruction.
1252 */
1253 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001254generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001255{
1256 isn_T *isn;
1257
1258 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001259 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001260 return FAIL;
1261 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1262 isn->isn_arg.unlet.ul_forceit = forceit;
1263
1264 return OK;
1265}
1266
1267/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001268 * Generate an ISN_LOCKCONST instruction.
1269 */
1270 static int
1271generate_LOCKCONST(cctx_T *cctx)
1272{
1273 isn_T *isn;
1274
1275 RETURN_OK_IF_SKIP(cctx);
1276 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1277 return FAIL;
1278 return OK;
1279}
1280
1281/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001282 * Generate an ISN_LOADS instruction.
1283 */
1284 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001285generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001286 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001287 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001289 int sid,
1290 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291{
1292 isn_T *isn;
1293
Bram Moolenaar080457c2020-03-03 21:53:32 +01001294 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001295 if (isn_type == ISN_LOADS)
1296 isn = generate_instr_type(cctx, isn_type, type);
1297 else
1298 isn = generate_instr_drop(cctx, isn_type, 1);
1299 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001300 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001301 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1302 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303
1304 return OK;
1305}
1306
1307/*
1308 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1309 */
1310 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001311generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312 cctx_T *cctx,
1313 isntype_T isn_type,
1314 int sid,
1315 int idx,
1316 type_T *type)
1317{
1318 isn_T *isn;
1319
Bram Moolenaar080457c2020-03-03 21:53:32 +01001320 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 if (isn_type == ISN_LOADSCRIPT)
1322 isn = generate_instr_type(cctx, isn_type, type);
1323 else
1324 isn = generate_instr_drop(cctx, isn_type, 1);
1325 if (isn == NULL)
1326 return FAIL;
1327 isn->isn_arg.script.script_sid = sid;
1328 isn->isn_arg.script.script_idx = idx;
1329 return OK;
1330}
1331
1332/*
1333 * Generate an ISN_NEWLIST instruction.
1334 */
1335 static int
1336generate_NEWLIST(cctx_T *cctx, int count)
1337{
1338 isn_T *isn;
1339 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 type_T *type;
1341 type_T *member;
1342
Bram Moolenaar080457c2020-03-03 21:53:32 +01001343 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1345 return FAIL;
1346 isn->isn_arg.number = count;
1347
Bram Moolenaar127542b2020-08-09 17:22:04 +02001348 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001349 if (count == 0)
1350 member = &t_void;
1351 else
1352 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001353 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1354 cctx->ctx_type_list);
1355 type = get_list_type(member, cctx->ctx_type_list);
1356
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357 // drop the value types
1358 stack->ga_len -= count;
1359
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001360 // add the list type to the type stack
1361 if (ga_grow(stack, 1) == FAIL)
1362 return FAIL;
1363 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1364 ++stack->ga_len;
1365
1366 return OK;
1367}
1368
1369/*
1370 * Generate an ISN_NEWDICT instruction.
1371 */
1372 static int
1373generate_NEWDICT(cctx_T *cctx, int count)
1374{
1375 isn_T *isn;
1376 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377 type_T *type;
1378 type_T *member;
1379
Bram Moolenaar080457c2020-03-03 21:53:32 +01001380 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001381 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1382 return FAIL;
1383 isn->isn_arg.number = count;
1384
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001385 if (count == 0)
1386 member = &t_void;
1387 else
1388 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001389 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1390 cctx->ctx_type_list);
1391 type = get_dict_type(member, cctx->ctx_type_list);
1392
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393 // drop the key and value types
1394 stack->ga_len -= 2 * count;
1395
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001396 // add the dict type to the type stack
1397 if (ga_grow(stack, 1) == FAIL)
1398 return FAIL;
1399 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1400 ++stack->ga_len;
1401
1402 return OK;
1403}
1404
1405/*
1406 * Generate an ISN_FUNCREF instruction.
1407 */
1408 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001409generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001410{
1411 isn_T *isn;
1412 garray_T *stack = &cctx->ctx_type_stack;
1413
Bram Moolenaar080457c2020-03-03 21:53:32 +01001414 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001415 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1416 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001417 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001418 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001419
1420 if (ga_grow(stack, 1) == FAIL)
1421 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001422 ((type_T **)stack->ga_data)[stack->ga_len] =
1423 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424 ++stack->ga_len;
1425
1426 return OK;
1427}
1428
1429/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001430 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001431 * "lambda_name" and "func_name" must be in allocated memory and will be
1432 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001433 */
1434 static int
1435generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1436{
1437 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001438
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001439 if (cctx->ctx_skip == SKIP_YES)
1440 {
1441 vim_free(lambda_name);
1442 vim_free(func_name);
1443 return OK;
1444 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001445 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001446 {
1447 vim_free(lambda_name);
1448 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001449 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001450 }
1451 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001452 isn->isn_arg.newfunc.nf_global = func_name;
1453
1454 return OK;
1455}
1456
1457/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001458 * Generate an ISN_DEF instruction: list functions
1459 */
1460 static int
1461generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1462{
1463 isn_T *isn;
1464
1465 RETURN_OK_IF_SKIP(cctx);
1466 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1467 return FAIL;
1468 if (len > 0)
1469 {
1470 isn->isn_arg.string = vim_strnsave(name, len);
1471 if (isn->isn_arg.string == NULL)
1472 return FAIL;
1473 }
1474 return OK;
1475}
1476
1477/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001478 * Generate an ISN_JUMP instruction.
1479 */
1480 static int
1481generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1482{
1483 isn_T *isn;
1484 garray_T *stack = &cctx->ctx_type_stack;
1485
Bram Moolenaar080457c2020-03-03 21:53:32 +01001486 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001487 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1488 return FAIL;
1489 isn->isn_arg.jump.jump_when = when;
1490 isn->isn_arg.jump.jump_where = where;
1491
1492 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1493 --stack->ga_len;
1494
1495 return OK;
1496}
1497
1498 static int
1499generate_FOR(cctx_T *cctx, int loop_idx)
1500{
1501 isn_T *isn;
1502 garray_T *stack = &cctx->ctx_type_stack;
1503
Bram Moolenaar080457c2020-03-03 21:53:32 +01001504 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001505 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1506 return FAIL;
1507 isn->isn_arg.forloop.for_idx = loop_idx;
1508
1509 if (ga_grow(stack, 1) == FAIL)
1510 return FAIL;
1511 // type doesn't matter, will be stored next
1512 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1513 ++stack->ga_len;
1514
1515 return OK;
1516}
1517
1518/*
1519 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001520 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001521 * Return FAIL if the number of arguments is wrong.
1522 */
1523 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001524generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001525{
1526 isn_T *isn;
1527 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001528 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001529 type_T **argtypes = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001530
Bram Moolenaar080457c2020-03-03 21:53:32 +01001531 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001532 argoff = check_internal_func(func_idx, argcount);
1533 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001534 return FAIL;
1535
Bram Moolenaar389df252020-07-09 21:20:47 +02001536 if (method_call && argoff > 1)
1537 {
1538 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1539 return FAIL;
1540 isn->isn_arg.shuffle.shfl_item = argcount;
1541 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1542 }
1543
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001544 if (argcount > 0)
1545 {
1546 // Check the types of the arguments.
1547 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
1548 if (internal_func_check_arg_types(argtypes, func_idx, argcount) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001549 return FAIL;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001550 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001551
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1553 return FAIL;
1554 isn->isn_arg.bfunc.cbf_idx = func_idx;
1555 isn->isn_arg.bfunc.cbf_argcount = argcount;
1556
Bram Moolenaar94738d82020-10-21 14:25:07 +02001557 // Drop the argument types and push the return type.
1558 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 if (ga_grow(stack, 1) == FAIL)
1560 return FAIL;
1561 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001562 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001563 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001564
1565 return OK;
1566}
1567
1568/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001569 * Generate an ISN_LISTAPPEND instruction. Works like add().
1570 * Argument count is already checked.
1571 */
1572 static int
1573generate_LISTAPPEND(cctx_T *cctx)
1574{
1575 garray_T *stack = &cctx->ctx_type_stack;
1576 type_T *list_type;
1577 type_T *item_type;
1578 type_T *expected;
1579
1580 // Caller already checked that list_type is a list.
1581 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1582 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1583 expected = list_type->tt_member;
1584 if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL)
1585 return FAIL;
1586
1587 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1588 return FAIL;
1589
1590 --stack->ga_len; // drop the argument
1591 return OK;
1592}
1593
1594/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001595 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1596 * Argument count is already checked.
1597 */
1598 static int
1599generate_BLOBAPPEND(cctx_T *cctx)
1600{
1601 garray_T *stack = &cctx->ctx_type_stack;
1602 type_T *item_type;
1603
1604 // Caller already checked that blob_type is a blob.
1605 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1606 if (need_type(item_type, &t_number, -1, cctx, FALSE, FALSE) == FAIL)
1607 return FAIL;
1608
1609 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1610 return FAIL;
1611
1612 --stack->ga_len; // drop the argument
1613 return OK;
1614}
1615
1616/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001617 * Generate an ISN_DCALL or ISN_UCALL instruction.
1618 * Return FAIL if the number of arguments is wrong.
1619 */
1620 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001621generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622{
1623 isn_T *isn;
1624 garray_T *stack = &cctx->ctx_type_stack;
1625 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001626 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001627
Bram Moolenaar080457c2020-03-03 21:53:32 +01001628 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629 if (argcount > regular_args && !has_varargs(ufunc))
1630 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001631 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001632 return FAIL;
1633 }
1634 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1635 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001636 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001637 return FAIL;
1638 }
1639
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001640 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001641 {
1642 int i;
1643
1644 for (i = 0; i < argcount; ++i)
1645 {
1646 type_T *expected;
1647 type_T *actual;
1648
1649 if (i < regular_args)
1650 {
1651 if (ufunc->uf_arg_types == NULL)
1652 continue;
1653 expected = ufunc->uf_arg_types[i];
1654 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001655 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1656 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001657 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001658 else
1659 expected = ufunc->uf_va_type->tt_member;
1660 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001661 if (need_type(actual, expected, -argcount + i, cctx,
1662 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001663 {
1664 arg_type_mismatch(expected, actual, i + 1);
1665 return FAIL;
1666 }
1667 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001668 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001669 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1670 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001671 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001672 }
1673
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001674 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001675 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001676 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001677 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001678 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001679 {
1680 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1681 isn->isn_arg.dfunc.cdf_argcount = argcount;
1682 }
1683 else
1684 {
1685 // A user function may be deleted and redefined later, can't use the
1686 // ufunc pointer, need to look it up again at runtime.
1687 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1688 isn->isn_arg.ufunc.cuf_argcount = argcount;
1689 }
1690
1691 stack->ga_len -= argcount; // drop the arguments
1692 if (ga_grow(stack, 1) == FAIL)
1693 return FAIL;
1694 // add return value
1695 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1696 ++stack->ga_len;
1697
1698 return OK;
1699}
1700
1701/*
1702 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1703 */
1704 static int
1705generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1706{
1707 isn_T *isn;
1708 garray_T *stack = &cctx->ctx_type_stack;
1709
Bram Moolenaar080457c2020-03-03 21:53:32 +01001710 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1712 return FAIL;
1713 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1714 isn->isn_arg.ufunc.cuf_argcount = argcount;
1715
1716 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001717 if (ga_grow(stack, 1) == FAIL)
1718 return FAIL;
1719 // add return value
1720 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1721 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001722
1723 return OK;
1724}
1725
1726/*
1727 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001728 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001729 */
1730 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001731generate_PCALL(
1732 cctx_T *cctx,
1733 int argcount,
1734 char_u *name,
1735 type_T *type,
1736 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737{
1738 isn_T *isn;
1739 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001740 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741
Bram Moolenaar080457c2020-03-03 21:53:32 +01001742 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001743
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001744 if (type->tt_type == VAR_ANY)
1745 ret_type = &t_any;
1746 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001747 {
1748 if (type->tt_argcount != -1)
1749 {
1750 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1751
1752 if (argcount < type->tt_min_argcount - varargs)
1753 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001754 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001755 return FAIL;
1756 }
1757 if (!varargs && argcount > type->tt_argcount)
1758 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001759 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001760 return FAIL;
1761 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001762 if (type->tt_args != NULL)
1763 {
1764 int i;
1765
1766 for (i = 0; i < argcount; ++i)
1767 {
1768 int offset = -argcount + i - 1;
1769 type_T *actual = ((type_T **)stack->ga_data)[
1770 stack->ga_len + offset];
1771 type_T *expected;
1772
1773 if (varargs && i >= type->tt_min_argcount - 1)
1774 expected = type->tt_args[
1775 type->tt_min_argcount - 1]->tt_member;
1776 else
1777 expected = type->tt_args[i];
1778 if (need_type(actual, expected, offset,
1779 cctx, TRUE, FALSE) == FAIL)
1780 {
1781 arg_type_mismatch(expected, actual, i + 1);
1782 return FAIL;
1783 }
1784 }
1785 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001786 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001787 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001788 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001789 else
1790 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001791 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001792 return FAIL;
1793 }
1794
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1796 return FAIL;
1797 isn->isn_arg.pfunc.cpf_top = at_top;
1798 isn->isn_arg.pfunc.cpf_argcount = argcount;
1799
1800 stack->ga_len -= argcount; // drop the arguments
1801
1802 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001803 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001805 // If partial is above the arguments it must be cleared and replaced with
1806 // the return value.
1807 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1808 return FAIL;
1809
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810 return OK;
1811}
1812
1813/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001814 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001815 */
1816 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001817generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818{
1819 isn_T *isn;
1820 garray_T *stack = &cctx->ctx_type_stack;
1821 type_T *type;
1822
Bram Moolenaar080457c2020-03-03 21:53:32 +01001823 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001824 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001826 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001828 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001830 if (type->tt_type != VAR_DICT && type != &t_any)
1831 {
1832 emsg(_(e_dictreq));
1833 return FAIL;
1834 }
1835 // change dict type to dict member type
1836 if (type->tt_type == VAR_DICT)
1837 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001838
1839 return OK;
1840}
1841
1842/*
1843 * Generate an ISN_ECHO instruction.
1844 */
1845 static int
1846generate_ECHO(cctx_T *cctx, int with_white, int count)
1847{
1848 isn_T *isn;
1849
Bram Moolenaar080457c2020-03-03 21:53:32 +01001850 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001851 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1852 return FAIL;
1853 isn->isn_arg.echo.echo_with_white = with_white;
1854 isn->isn_arg.echo.echo_count = count;
1855
1856 return OK;
1857}
1858
Bram Moolenaarad39c092020-02-26 18:23:43 +01001859/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001860 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001861 */
1862 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001863generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001864{
1865 isn_T *isn;
1866
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001867 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001868 return FAIL;
1869 isn->isn_arg.number = count;
1870
1871 return OK;
1872}
1873
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001874/*
1875 * Generate an ISN_PUT instruction.
1876 */
1877 static int
1878generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1879{
1880 isn_T *isn;
1881
1882 RETURN_OK_IF_SKIP(cctx);
1883 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1884 return FAIL;
1885 isn->isn_arg.put.put_regname = regname;
1886 isn->isn_arg.put.put_lnum = lnum;
1887 return OK;
1888}
1889
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001890 static int
1891generate_EXEC(cctx_T *cctx, char_u *line)
1892{
1893 isn_T *isn;
1894
Bram Moolenaar080457c2020-03-03 21:53:32 +01001895 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001896 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1897 return FAIL;
1898 isn->isn_arg.string = vim_strsave(line);
1899 return OK;
1900}
1901
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001902 static int
1903generate_EXECCONCAT(cctx_T *cctx, int count)
1904{
1905 isn_T *isn;
1906
1907 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1908 return FAIL;
1909 isn->isn_arg.number = count;
1910 return OK;
1911}
1912
Bram Moolenaar08597872020-12-10 19:43:40 +01001913/*
1914 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1915 */
1916 static int
1917generate_RANGE(cctx_T *cctx, char_u *range)
1918{
1919 isn_T *isn;
1920 garray_T *stack = &cctx->ctx_type_stack;
1921
1922 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1923 return FAIL;
1924 isn->isn_arg.string = range;
1925
1926 if (ga_grow(stack, 1) == FAIL)
1927 return FAIL;
1928 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
1929 ++stack->ga_len;
1930 return OK;
1931}
1932
Bram Moolenaar792f7862020-11-23 08:31:18 +01001933 static int
1934generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
1935{
1936 isn_T *isn;
1937
1938 RETURN_OK_IF_SKIP(cctx);
1939 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
1940 return FAIL;
1941 isn->isn_arg.unpack.unp_count = var_count;
1942 isn->isn_arg.unpack.unp_semicolon = semicolon;
1943 return OK;
1944}
1945
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001946/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02001947 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001948 */
1949 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02001950generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001951{
1952 isn_T *isn;
1953
Bram Moolenaar02194d22020-10-24 23:08:38 +02001954 if (cmod->cmod_flags != 0
1955 || cmod->cmod_split != 0
1956 || cmod->cmod_verbose != 0
1957 || cmod->cmod_tab != 0
1958 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001959 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02001960 cctx->ctx_has_cmdmod = TRUE;
1961
1962 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001963 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001964 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1965 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1966 return FAIL;
1967 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001968 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02001969 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001970 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02001971
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001972 return OK;
1973}
1974
1975 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02001976generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001977{
Bram Moolenaarf665e972020-12-05 19:17:16 +01001978 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
1979 return FAIL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001980 return OK;
1981}
1982
1983/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001984 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001985 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001986 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001987 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02001988reserve_local(
1989 cctx_T *cctx,
1990 char_u *name,
1991 size_t len,
1992 int isConst,
1993 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001994{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001995 lvar_T *lvar;
1996
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001997 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001998 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001999 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002000 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002001 }
2002
2003 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002004 return NULL;
2005 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002006 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002008 // Every local variable uses the next entry on the stack. We could re-use
2009 // the last ones when leaving a scope, but then variables used in a closure
2010 // might get overwritten. To keep things simple do not re-use stack
2011 // entries. This is less efficient, but memory is cheap these days.
2012 lvar->lv_idx = cctx->ctx_locals_count++;
2013
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002014 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002015 lvar->lv_const = isConst;
2016 lvar->lv_type = type;
2017
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002018 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002019}
2020
2021/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002022 * Remove local variables above "new_top".
2023 */
2024 static void
2025unwind_locals(cctx_T *cctx, int new_top)
2026{
2027 if (cctx->ctx_locals.ga_len > new_top)
2028 {
2029 int idx;
2030 lvar_T *lvar;
2031
2032 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2033 {
2034 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2035 vim_free(lvar->lv_name);
2036 }
2037 }
2038 cctx->ctx_locals.ga_len = new_top;
2039}
2040
2041/*
2042 * Free all local variables.
2043 */
2044 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002045free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002046{
2047 unwind_locals(cctx, 0);
2048 ga_clear(&cctx->ctx_locals);
2049}
2050
2051/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002052 * Find "name" in script-local items of script "sid".
2053 * Returns the index in "sn_var_vals" if found.
2054 * If found but not in "sn_var_vals" returns -1.
2055 * If not found returns -2.
2056 */
2057 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002058get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002059{
2060 hashtab_T *ht;
2061 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002062 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002063 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064 int idx;
2065
Bram Moolenaare3d46852020-08-29 13:39:17 +02002066 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002067 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002068 if (sid == current_sctx.sc_sid)
2069 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002070 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002071
2072 if (sav == NULL)
2073 return -2;
2074 idx = sav->sav_var_vals_idx;
2075 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2076 if (check_writable && sv->sv_const)
2077 semsg(_(e_readonlyvar), name);
2078 return idx;
2079 }
2080
2081 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002082 ht = &SCRIPT_VARS(sid);
2083 di = find_var_in_ht(ht, 0, name, TRUE);
2084 if (di == NULL)
2085 return -2;
2086
2087 // Now find the svar_T index in sn_var_vals.
2088 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2089 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002090 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002091 if (sv->sv_tv == &di->di_tv)
2092 {
2093 if (check_writable && sv->sv_const)
2094 semsg(_(e_readonlyvar), name);
2095 return idx;
2096 }
2097 }
2098 return -1;
2099}
2100
2101/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002102 * Find "name" in imported items of the current script or in "cctx" if not
2103 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002104 */
2105 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002106find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002107{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002108 int idx;
2109
Bram Moolenaare3d46852020-08-29 13:39:17 +02002110 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002111 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112 if (cctx != NULL)
2113 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2114 {
2115 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2116 + idx;
2117
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002118 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2119 : STRLEN(import->imp_name) == len
2120 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121 return import;
2122 }
2123
Bram Moolenaarefa94442020-08-08 22:16:00 +02002124 return find_imported_in_script(name, len, current_sctx.sc_sid);
2125}
2126
2127 imported_T *
2128find_imported_in_script(char_u *name, size_t len, int sid)
2129{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002130 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002131 int idx;
2132
Bram Moolenaare3d46852020-08-29 13:39:17 +02002133 if (!SCRIPT_ID_VALID(sid))
2134 return NULL;
2135 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002136 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2137 {
2138 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2139
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002140 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2141 : STRLEN(import->imp_name) == len
2142 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002143 return import;
2144 }
2145 return NULL;
2146}
2147
2148/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002149 * Free all imported variables.
2150 */
2151 static void
2152free_imported(cctx_T *cctx)
2153{
2154 int idx;
2155
2156 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2157 {
2158 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2159
2160 vim_free(import->imp_name);
2161 }
2162 ga_clear(&cctx->ctx_imports);
2163}
2164
2165/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01002166 * Return TRUE if "p" points at a "#". Does not check for white space.
Bram Moolenaar23c55272020-06-21 16:58:13 +02002167 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002168 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002169vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002170{
Bram Moolenaare0de1712020-12-02 17:36:54 +01002171 return p[0] == '#';
Bram Moolenaar23c55272020-06-21 16:58:13 +02002172}
2173
2174/*
2175 * Return a pointer to the next line that isn't empty or only contains a
2176 * comment. Skips over white space.
2177 * Returns NULL if there is none.
2178 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002179 char_u *
2180peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002181{
2182 int lnum = cctx->ctx_lnum;
2183
2184 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2185 {
2186 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002187 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002188
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002189 // ignore NULLs inserted for continuation lines
2190 if (line != NULL)
2191 {
2192 p = skipwhite(line);
2193 if (*p != NUL && !vim9_comment_start(p))
2194 return p;
2195 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002196 }
2197 return NULL;
2198}
2199
2200/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002201 * Called when checking for a following operator at "arg". When the rest of
2202 * the line is empty or only a comment, peek the next line. If there is a next
2203 * line return a pointer to it and set "nextp".
2204 * Otherwise skip over white space.
2205 */
2206 static char_u *
2207may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2208{
2209 char_u *p = skipwhite(arg);
2210
2211 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002212 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002213 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002214 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002215 if (*nextp != NULL)
2216 return *nextp;
2217 }
2218 return p;
2219}
2220
2221/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002222 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002223 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002224 * Returns NULL when at the end.
2225 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002226 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002227next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002228{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002229 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002230
2231 do
2232 {
2233 ++cctx->ctx_lnum;
2234 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002235 {
2236 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002237 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002238 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002239 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002240 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002241 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002242 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002243 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002244 return line;
2245}
2246
2247/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002248 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002249 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002250 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2251 */
2252 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002253may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002254{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002255 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002256 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002257 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002258
2259 if (next == NULL)
2260 return FAIL;
2261 *arg = skipwhite(next);
2262 }
2263 return OK;
2264}
2265
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002266/*
2267 * Idem, and give an error when failed.
2268 */
2269 static int
2270may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2271{
2272 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2273 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002274 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002275 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002276 return FAIL;
2277 }
2278 return OK;
2279}
2280
2281
Bram Moolenaara5565e42020-05-09 15:44:01 +02002282// Structure passed between the compile_expr* functions to keep track of
2283// constants that have been parsed but for which no code was produced yet. If
2284// possible expressions on these constants are applied at compile time. If
2285// that is not possible, the code to push the constants needs to be generated
2286// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002287// Using 50 should be more than enough of 5 levels of ().
2288#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002289typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002290 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002291 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002292 int pp_is_const; // all generated code was constants, used for a
2293 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002294} ppconst_T;
2295
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002296static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002297static int compile_expr0(char_u **arg, cctx_T *cctx);
2298static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2299
Bram Moolenaara5565e42020-05-09 15:44:01 +02002300/*
2301 * Generate a PUSH instruction for "tv".
2302 * "tv" will be consumed or cleared.
2303 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2304 */
2305 static int
2306generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2307{
2308 if (tv != NULL)
2309 {
2310 switch (tv->v_type)
2311 {
2312 case VAR_UNKNOWN:
2313 break;
2314 case VAR_BOOL:
2315 generate_PUSHBOOL(cctx, tv->vval.v_number);
2316 break;
2317 case VAR_SPECIAL:
2318 generate_PUSHSPEC(cctx, tv->vval.v_number);
2319 break;
2320 case VAR_NUMBER:
2321 generate_PUSHNR(cctx, tv->vval.v_number);
2322 break;
2323#ifdef FEAT_FLOAT
2324 case VAR_FLOAT:
2325 generate_PUSHF(cctx, tv->vval.v_float);
2326 break;
2327#endif
2328 case VAR_BLOB:
2329 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2330 tv->vval.v_blob = NULL;
2331 break;
2332 case VAR_STRING:
2333 generate_PUSHS(cctx, tv->vval.v_string);
2334 tv->vval.v_string = NULL;
2335 break;
2336 default:
2337 iemsg("constant type not supported");
2338 clear_tv(tv);
2339 return FAIL;
2340 }
2341 tv->v_type = VAR_UNKNOWN;
2342 }
2343 return OK;
2344}
2345
2346/*
2347 * Generate code for any ppconst entries.
2348 */
2349 static int
2350generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2351{
2352 int i;
2353 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002354 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002355
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002356 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002357 for (i = 0; i < ppconst->pp_used; ++i)
2358 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2359 ret = FAIL;
2360 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002361 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002362 return ret;
2363}
2364
2365/*
2366 * Clear ppconst constants. Used when failing.
2367 */
2368 static void
2369clear_ppconst(ppconst_T *ppconst)
2370{
2371 int i;
2372
2373 for (i = 0; i < ppconst->pp_used; ++i)
2374 clear_tv(&ppconst->pp_tv[i]);
2375 ppconst->pp_used = 0;
2376}
2377
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002378/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002379 * Generate an instruction to load script-local variable "name", without the
2380 * leading "s:".
2381 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002382 */
2383 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002384compile_load_scriptvar(
2385 cctx_T *cctx,
2386 char_u *name, // variable NUL terminated
2387 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002388 char_u **end, // end of variable
2389 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002391 scriptitem_T *si;
2392 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393 imported_T *import;
2394
Bram Moolenaare3d46852020-08-29 13:39:17 +02002395 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2396 return FAIL;
2397 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002398 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002399 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002400 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002401 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002402 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2403 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002404 }
2405 if (idx >= 0)
2406 {
2407 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2408
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002409 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002410 current_sctx.sc_sid, idx, sv->sv_type);
2411 return OK;
2412 }
2413
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002414 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002415 if (import != NULL)
2416 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002417 if (import->imp_all)
2418 {
2419 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002420 char_u *exp_name;
2421 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002422 ufunc_T *ufunc;
2423 type_T *type;
2424
2425 // Used "import * as Name", need to lookup the member.
2426 if (*p != '.')
2427 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002428 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002429 return FAIL;
2430 }
2431 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002432 if (VIM_ISWHITE(*p))
2433 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002434 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002435 return FAIL;
2436 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002437
Bram Moolenaar1c991142020-07-04 13:15:31 +02002438 // isolate one name
2439 exp_name = p;
2440 while (eval_isnamec(*p))
2441 ++p;
2442 cc = *p;
2443 *p = NUL;
2444
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002445 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002446 *p = cc;
2447 p = skipwhite(p);
2448
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002449 // TODO: what if it is a function?
2450 if (idx < 0)
2451 return FAIL;
2452 *end = p;
2453
2454 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2455 import->imp_sid,
2456 idx,
2457 type);
2458 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002459 else if (import->imp_funcname != NULL)
2460 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002461 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002462 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2463 import->imp_sid,
2464 import->imp_var_vals_idx,
2465 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466 return OK;
2467 }
2468
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002469 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002470 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471 return FAIL;
2472}
2473
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002474 static int
2475generate_funcref(cctx_T *cctx, char_u *name)
2476{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002477 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002478
2479 if (ufunc == NULL)
2480 return FAIL;
2481
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002482 // Need to compile any default values to get the argument types.
2483 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2484 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2485 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002486 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002487}
2488
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489/*
2490 * Compile a variable name into a load instruction.
2491 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002492 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493 * When "error" is FALSE do not give an error when not found.
2494 */
2495 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002496compile_load(
2497 char_u **arg,
2498 char_u *end_arg,
2499 cctx_T *cctx,
2500 int is_expr,
2501 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002502{
2503 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002504 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002505 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002507 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002508
2509 if (*(*arg + 1) == ':')
2510 {
2511 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002512 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002513 {
2514 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002516 switch (**arg)
2517 {
2518 case 'g': isn_type = ISN_LOADGDICT; break;
2519 case 'w': isn_type = ISN_LOADWDICT; break;
2520 case 't': isn_type = ISN_LOADTDICT; break;
2521 case 'b': isn_type = ISN_LOADBDICT; break;
2522 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002523 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002524 goto theend;
2525 }
2526 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2527 goto theend;
2528 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002529 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530 else
2531 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002532 isntype_T isn_type = ISN_DROP;
2533
2534 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2535 if (name == NULL)
2536 return FAIL;
2537
2538 switch (**arg)
2539 {
2540 case 'v': res = generate_LOADV(cctx, name, error);
2541 break;
2542 case 's': res = compile_load_scriptvar(cctx, name,
2543 NULL, NULL, error);
2544 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002545 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2546 isn_type = ISN_LOADG;
2547 else
2548 {
2549 isn_type = ISN_LOADAUTO;
2550 vim_free(name);
2551 name = vim_strnsave(*arg, end - *arg);
2552 if (name == NULL)
2553 return FAIL;
2554 }
2555 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002556 case 'w': isn_type = ISN_LOADW; break;
2557 case 't': isn_type = ISN_LOADT; break;
2558 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002559 default: // cannot happen, just in case
2560 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002561 goto theend;
2562 }
2563 if (isn_type != ISN_DROP)
2564 {
2565 // Global, Buffer-local, Window-local and Tabpage-local
2566 // variables can be defined later, thus we don't check if it
2567 // exists, give error at runtime.
2568 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2569 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002570 }
2571 }
2572 else
2573 {
2574 size_t len = end - *arg;
2575 int idx;
2576 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002577 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578
2579 name = vim_strnsave(*arg, end - *arg);
2580 if (name == NULL)
2581 return FAIL;
2582
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002583 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002585 if (!gen_load_outer)
2586 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002587 }
2588 else
2589 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002590 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002591
Bram Moolenaar709664c2020-12-12 14:33:41 +01002592 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002594 type = lvar.lv_type;
2595 idx = lvar.lv_idx;
2596 if (lvar.lv_from_outer)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002597 gen_load_outer = TRUE;
2598 else
2599 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600 }
2601 else
2602 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002603 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002604 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002605 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002606 || find_imported(name, 0, cctx) != NULL)
2607 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002608
Bram Moolenaar0f769812020-09-12 18:32:34 +02002609 // When evaluating an expression and the name starts with an
2610 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002611 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002612 if (res == FAIL && is_expr
2613 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002614 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002615 }
2616 }
2617 if (gen_load)
2618 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002619 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002620 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002621 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002622 cctx->ctx_outer_used = TRUE;
2623 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624 }
2625
2626 *arg = end;
2627
2628theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002629 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002630 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002631 vim_free(name);
2632 return res;
2633}
2634
2635/*
2636 * Compile the argument expressions.
2637 * "arg" points to just after the "(" and is advanced to after the ")"
2638 */
2639 static int
2640compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2641{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002642 char_u *p = *arg;
2643 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002644 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645
Bram Moolenaare6085c52020-04-12 20:19:16 +02002646 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002647 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002648 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2649 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002650 if (*p == ')')
2651 {
2652 *arg = p + 1;
2653 return OK;
2654 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002655 if (must_end)
2656 {
2657 semsg(_(e_missing_comma_before_argument_str), p);
2658 return FAIL;
2659 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002660
Bram Moolenaara5565e42020-05-09 15:44:01 +02002661 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002662 return FAIL;
2663 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002664
2665 if (*p != ',' && *skipwhite(p) == ',')
2666 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002667 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002668 p = skipwhite(p);
2669 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002670 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002671 {
2672 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002673 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002674 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002675 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002676 else
2677 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002678 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002679 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002680 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002681failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002682 emsg(_(e_missing_close));
2683 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684}
2685
2686/*
2687 * Compile a function call: name(arg1, arg2)
2688 * "arg" points to "name", "arg + varlen" to the "(".
2689 * "argcount_init" is 1 for "value->method()"
2690 * Instructions:
2691 * EVAL arg1
2692 * EVAL arg2
2693 * BCALL / DCALL / UCALL
2694 */
2695 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002696compile_call(
2697 char_u **arg,
2698 size_t varlen,
2699 cctx_T *cctx,
2700 ppconst_T *ppconst,
2701 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702{
2703 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002704 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705 int argcount = argcount_init;
2706 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002707 char_u fname_buf[FLEN_FIXED + 1];
2708 char_u *tofree = NULL;
2709 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002710 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002711 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002712 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713
Bram Moolenaara5565e42020-05-09 15:44:01 +02002714 // we can evaluate "has('name')" at compile time
2715 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2716 {
2717 char_u *s = skipwhite(*arg + varlen + 1);
2718 typval_T argvars[2];
2719
2720 argvars[0].v_type = VAR_UNKNOWN;
2721 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002722 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002723 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002724 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002725 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002726 if (*s == ')' && argvars[0].v_type == VAR_STRING
2727 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002728 {
2729 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2730
2731 *arg = s + 1;
2732 argvars[1].v_type = VAR_UNKNOWN;
2733 tv->v_type = VAR_NUMBER;
2734 tv->vval.v_number = 0;
2735 f_has(argvars, tv);
2736 clear_tv(&argvars[0]);
2737 ++ppconst->pp_used;
2738 return OK;
2739 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002740 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002741 }
2742
2743 if (generate_ppconst(cctx, ppconst) == FAIL)
2744 return FAIL;
2745
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002746 if (varlen >= sizeof(namebuf))
2747 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002748 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002749 return FAIL;
2750 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002751 vim_strncpy(namebuf, *arg, varlen);
2752 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002753
2754 *arg = skipwhite(*arg + varlen + 1);
2755 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002756 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002757
Bram Moolenaar03290b82020-12-19 16:30:44 +01002758 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002759 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760 {
2761 int idx;
2762
2763 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002764 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002766 {
2767 if (STRCMP(name, "add") == 0 && argcount == 2)
2768 {
2769 garray_T *stack = &cctx->ctx_type_stack;
2770 type_T *type = ((type_T **)stack->ga_data)[
2771 stack->ga_len - 2];
2772
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002773 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002774 if (type->tt_type == VAR_LIST)
2775 {
2776 // inline "add(list, item)" so that the type can be checked
2777 res = generate_LISTAPPEND(cctx);
2778 idx = -1;
2779 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002780 else if (type->tt_type == VAR_BLOB)
2781 {
2782 // inline "add(blob, nr)" so that the type can be checked
2783 res = generate_BLOBAPPEND(cctx);
2784 idx = -1;
2785 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002786 }
2787
2788 if (idx >= 0)
2789 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2790 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002791 else
2792 semsg(_(e_unknownfunc), namebuf);
2793 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794 }
2795
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002796 // An argument or local variable can be a function reference, this
2797 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002798 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002799 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002800 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002801 // If we can find the function by name generate the right call.
2802 // Skip global functions here, a local funcref takes precedence.
2803 ufunc = find_func(name, FALSE, cctx);
2804 if (ufunc != NULL && !func_is_global(ufunc))
2805 {
2806 res = generate_CALL(cctx, ufunc, argcount);
2807 goto theend;
2808 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002809 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002810
2811 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002812 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002813 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002814 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002815 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002816 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002817 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002818 garray_T *stack = &cctx->ctx_type_stack;
2819 type_T *type;
2820
2821 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2822 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002823 goto theend;
2824 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002825
Bram Moolenaar0f769812020-09-12 18:32:34 +02002826 // If we can find a global function by name generate the right call.
2827 if (ufunc != NULL)
2828 {
2829 res = generate_CALL(cctx, ufunc, argcount);
2830 goto theend;
2831 }
2832
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002833 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002834 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002835 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002836 res = generate_UCALL(cctx, name, argcount);
2837 else
2838 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002839
2840theend:
2841 vim_free(tofree);
2842 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843}
2844
2845// like NAMESPACE_CHAR but with 'a' and 'l'.
2846#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2847
2848/*
2849 * Find the end of a variable or function name. Unlike find_name_end() this
2850 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002851 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 * Return a pointer to just after the name. Equal to "arg" if there is no
2853 * valid name.
2854 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01002855 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002856to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857{
2858 char_u *p;
2859
2860 // Quick check for valid starting character.
2861 if (!eval_isnamec1(*arg))
2862 return arg;
2863
2864 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2865 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2866 // and can be used in slice "[n:]".
2867 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002868 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002869 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2870 break;
2871 return p;
2872}
2873
2874/*
2875 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002876 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 */
2878 char_u *
2879to_name_const_end(char_u *arg)
2880{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002881 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 typval_T rettv;
2883
2884 if (p == arg && *arg == '[')
2885 {
2886
2887 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002888 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889 p = arg;
2890 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 return p;
2892}
2893
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894/*
2895 * parse a list: [expr, expr]
2896 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002897 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002898 */
2899 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002900compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901{
2902 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002903 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002905 int is_const;
2906 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002908 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002909 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002910 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002911 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002912 semsg(_(e_list_end), *arg);
2913 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002914 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002915 if (*p == ',')
2916 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002917 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002918 return FAIL;
2919 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002920 if (*p == ']')
2921 {
2922 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002923 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002924 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002925 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002926 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002927 if (!is_const)
2928 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002929 ++count;
2930 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002931 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002933 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2934 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002935 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002936 return FAIL;
2937 }
2938 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002939 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 p = skipwhite(p);
2941 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002942 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002944 ppconst->pp_is_const = is_all_const;
2945 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946}
2947
2948/*
2949 * parse a lambda: {arg, arg -> expr}
2950 * "*arg" points to the '{'.
2951 */
2952 static int
2953compile_lambda(char_u **arg, cctx_T *cctx)
2954{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955 typval_T rettv;
2956 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002957 evalarg_T evalarg;
2958
2959 CLEAR_FIELD(evalarg);
2960 evalarg.eval_flags = EVAL_EVALUATE;
2961 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962
2963 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002964 if (get_lambda_tv(arg, &rettv, TRUE, &evalarg) != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002965 {
2966 clear_evalarg(&evalarg, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 return FAIL;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002968 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01002969
Bram Moolenaar65c44152020-12-24 15:14:01 +01002970 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002972 ++ufunc->uf_refcount;
2973 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974
Bram Moolenaar65c44152020-12-24 15:14:01 +01002975 // Compile the function into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002976 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002978 clear_evalarg(&evalarg, NULL);
2979
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002980 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002981 {
2982 // The return type will now be known.
2983 set_function_type(ufunc);
2984
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002985 // The function reference count will be 1. When the ISN_FUNCREF
2986 // instruction is deleted the reference count is decremented and the
2987 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002988 return generate_FUNCREF(cctx, ufunc);
2989 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002990
2991 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 return FAIL;
2993}
2994
2995/*
2996 * Compile a lamda call: expr->{lambda}(args)
2997 * "arg" points to the "{".
2998 */
2999 static int
3000compile_lambda_call(char_u **arg, cctx_T *cctx)
3001{
3002 ufunc_T *ufunc;
3003 typval_T rettv;
3004 int argcount = 1;
3005 int ret = FAIL;
3006
3007 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003008 if (get_lambda_tv(arg, &rettv, TRUE, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 return FAIL;
3010
3011 if (**arg != '(')
3012 {
3013 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003014 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 else
3016 semsg(_(e_missing_paren), "lambda");
3017 clear_tv(&rettv);
3018 return FAIL;
3019 }
3020
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 ufunc = rettv.vval.v_partial->pt_func;
3022 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003023 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003024 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003025
Bram Moolenaara05e5242020-09-19 18:19:19 +02003026 // The function will have one line: "return {expr}". Compile it into
3027 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003028 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029
3030 // compile the arguments
3031 *arg = skipwhite(*arg + 1);
3032 if (compile_arguments(arg, cctx, &argcount) == OK)
3033 // call the compiled function
3034 ret = generate_CALL(cctx, ufunc, argcount);
3035
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003036 if (ret == FAIL)
3037 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 return ret;
3039}
3040
3041/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003042 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003044 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 */
3046 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003047compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048{
3049 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003050 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 int count = 0;
3052 dict_T *d = dict_alloc();
3053 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003054 char_u *whitep = *arg;
3055 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003056 int is_const;
3057 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058
3059 if (d == NULL)
3060 return FAIL;
3061 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003062 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003063 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003064 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065
Bram Moolenaar23c55272020-06-21 16:58:13 +02003066 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003067 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003068 *arg = NULL;
3069 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003070 }
3071
3072 if (**arg == '}')
3073 break;
3074
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003075 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003077 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003079 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003080 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003081 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3084 if (isn->isn_type == ISN_PUSHS)
3085 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003086 else
3087 {
3088 type_T *keytype = ((type_T **)stack->ga_data)
Bram Moolenaar2bede172020-11-19 18:53:18 +01003089 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003090 if (need_type(keytype, &t_string, -1, cctx,
Bram Moolenaar2bede172020-11-19 18:53:18 +01003091 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003092 return FAIL;
3093 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003094 *arg = skipwhite(*arg);
3095 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003096 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003097 emsg(_(e_missing_matching_bracket_after_dict_key));
3098 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003099 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003100 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003102 else
3103 {
3104 // {"name": value},
3105 // {'name': value},
3106 // {name: value} use "name" as a literal key
3107 key = get_literal_key(arg);
3108 if (key == NULL)
3109 return FAIL;
3110 if (generate_PUSHS(cctx, key) == FAIL)
3111 return FAIL;
3112 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113
3114 // Check for duplicate keys, if using string keys.
3115 if (key != NULL)
3116 {
3117 item = dict_find(d, key, -1);
3118 if (item != NULL)
3119 {
3120 semsg(_(e_duplicate_key), key);
3121 goto failret;
3122 }
3123 item = dictitem_alloc(key);
3124 if (item != NULL)
3125 {
3126 item->di_tv.v_type = VAR_UNKNOWN;
3127 item->di_tv.v_lock = 0;
3128 if (dict_add(d, item) == FAIL)
3129 dictitem_free(item);
3130 }
3131 }
3132
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 if (**arg != ':')
3134 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003135 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003136 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003137 else
3138 semsg(_(e_missing_dict_colon), *arg);
3139 return FAIL;
3140 }
3141 whitep = *arg + 1;
3142 if (!IS_WHITE_OR_NUL(*whitep))
3143 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003144 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 return FAIL;
3146 }
3147
3148 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003149 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003150 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003151 *arg = NULL;
3152 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003153 }
3154
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003155 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003157 if (!is_const)
3158 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 ++count;
3160
Bram Moolenaar2c330432020-04-13 14:41:35 +02003161 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003162 *arg = skipwhite(*arg);
3163 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003164 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003165 *arg = NULL;
3166 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003167 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168 if (**arg == '}')
3169 break;
3170 if (**arg != ',')
3171 {
3172 semsg(_(e_missing_dict_comma), *arg);
3173 goto failret;
3174 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003175 if (IS_WHITE_OR_NUL(*whitep))
3176 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003177 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003178 return FAIL;
3179 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003180 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003181 if (!IS_WHITE_OR_NUL(*whitep))
3182 {
3183 semsg(_(e_white_space_required_after_str), ",");
3184 return FAIL;
3185 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003186 *arg = skipwhite(*arg + 1);
3187 }
3188
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 *arg = *arg + 1;
3190
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003191 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003192 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003193 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003194 *arg += STRLEN(*arg);
3195
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003197 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198 return generate_NEWDICT(cctx, count);
3199
3200failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003201 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003202 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003203 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003204 *arg = (char_u *)"";
3205 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 dict_unref(d);
3207 return FAIL;
3208}
3209
3210/*
3211 * Compile "&option".
3212 */
3213 static int
3214compile_get_option(char_u **arg, cctx_T *cctx)
3215{
3216 typval_T rettv;
3217 char_u *start = *arg;
3218 int ret;
3219
3220 // parse the option and get the current value to get the type.
3221 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003222 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223 if (ret == OK)
3224 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003225 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 char_u *name = vim_strnsave(start, *arg - start);
3227 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3228
3229 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3230 vim_free(name);
3231 }
3232 clear_tv(&rettv);
3233
3234 return ret;
3235}
3236
3237/*
3238 * Compile "$VAR".
3239 */
3240 static int
3241compile_get_env(char_u **arg, cctx_T *cctx)
3242{
3243 char_u *start = *arg;
3244 int len;
3245 int ret;
3246 char_u *name;
3247
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 ++*arg;
3249 len = get_env_len(arg);
3250 if (len == 0)
3251 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003252 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003253 return FAIL;
3254 }
3255
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003256 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 name = vim_strnsave(start, len + 1);
3258 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3259 vim_free(name);
3260 return ret;
3261}
3262
3263/*
3264 * Compile "@r".
3265 */
3266 static int
3267compile_get_register(char_u **arg, cctx_T *cctx)
3268{
3269 int ret;
3270
3271 ++*arg;
3272 if (**arg == NUL)
3273 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003274 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 return FAIL;
3276 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003277 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 {
3279 emsg_invreg(**arg);
3280 return FAIL;
3281 }
3282 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3283 ++*arg;
3284 return ret;
3285}
3286
3287/*
3288 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003289 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290 */
3291 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003292apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003294 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295
3296 // this works from end to start
3297 while (p > start)
3298 {
3299 --p;
3300 if (*p == '-' || *p == '+')
3301 {
3302 // only '-' has an effect, for '+' we only check the type
3303#ifdef FEAT_FLOAT
3304 if (rettv->v_type == VAR_FLOAT)
3305 {
3306 if (*p == '-')
3307 rettv->vval.v_float = -rettv->vval.v_float;
3308 }
3309 else
3310#endif
3311 {
3312 varnumber_T val;
3313 int error = FALSE;
3314
3315 // tv_get_number_chk() accepts a string, but we don't want that
3316 // here
3317 if (check_not_string(rettv) == FAIL)
3318 return FAIL;
3319 val = tv_get_number_chk(rettv, &error);
3320 clear_tv(rettv);
3321 if (error)
3322 return FAIL;
3323 if (*p == '-')
3324 val = -val;
3325 rettv->v_type = VAR_NUMBER;
3326 rettv->vval.v_number = val;
3327 }
3328 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003329 else if (numeric_only)
3330 {
3331 ++p;
3332 break;
3333 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003334 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003335 {
3336 int v = tv2bool(rettv);
3337
3338 // '!' is permissive in the type.
3339 clear_tv(rettv);
3340 rettv->v_type = VAR_BOOL;
3341 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3342 }
3343 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003344 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003345 return OK;
3346}
3347
3348/*
3349 * Recognize v: variables that are constants and set "rettv".
3350 */
3351 static void
3352get_vim_constant(char_u **arg, typval_T *rettv)
3353{
3354 if (STRNCMP(*arg, "v:true", 6) == 0)
3355 {
3356 rettv->v_type = VAR_BOOL;
3357 rettv->vval.v_number = VVAL_TRUE;
3358 *arg += 6;
3359 }
3360 else if (STRNCMP(*arg, "v:false", 7) == 0)
3361 {
3362 rettv->v_type = VAR_BOOL;
3363 rettv->vval.v_number = VVAL_FALSE;
3364 *arg += 7;
3365 }
3366 else if (STRNCMP(*arg, "v:null", 6) == 0)
3367 {
3368 rettv->v_type = VAR_SPECIAL;
3369 rettv->vval.v_number = VVAL_NULL;
3370 *arg += 6;
3371 }
3372 else if (STRNCMP(*arg, "v:none", 6) == 0)
3373 {
3374 rettv->v_type = VAR_SPECIAL;
3375 rettv->vval.v_number = VVAL_NONE;
3376 *arg += 6;
3377 }
3378}
3379
Bram Moolenaar696ba232020-07-29 21:20:41 +02003380 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003381get_compare_type(char_u *p, int *len, int *type_is)
3382{
3383 exptype_T type = EXPR_UNKNOWN;
3384 int i;
3385
3386 switch (p[0])
3387 {
3388 case '=': if (p[1] == '=')
3389 type = EXPR_EQUAL;
3390 else if (p[1] == '~')
3391 type = EXPR_MATCH;
3392 break;
3393 case '!': if (p[1] == '=')
3394 type = EXPR_NEQUAL;
3395 else if (p[1] == '~')
3396 type = EXPR_NOMATCH;
3397 break;
3398 case '>': if (p[1] != '=')
3399 {
3400 type = EXPR_GREATER;
3401 *len = 1;
3402 }
3403 else
3404 type = EXPR_GEQUAL;
3405 break;
3406 case '<': if (p[1] != '=')
3407 {
3408 type = EXPR_SMALLER;
3409 *len = 1;
3410 }
3411 else
3412 type = EXPR_SEQUAL;
3413 break;
3414 case 'i': if (p[1] == 's')
3415 {
3416 // "is" and "isnot"; but not a prefix of a name
3417 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3418 *len = 5;
3419 i = p[*len];
3420 if (!isalnum(i) && i != '_')
3421 {
3422 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3423 *type_is = TRUE;
3424 }
3425 }
3426 break;
3427 }
3428 return type;
3429}
3430
3431/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003433 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434 */
3435 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003436compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003438 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439
3440 // this works from end to start
3441 while (p > start)
3442 {
3443 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003444 while (VIM_ISWHITE(*p))
3445 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 if (*p == '-' || *p == '+')
3447 {
3448 int negate = *p == '-';
3449 isn_T *isn;
3450
3451 // TODO: check type
3452 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3453 {
3454 --p;
3455 if (*p == '-')
3456 negate = !negate;
3457 }
3458 // only '-' has an effect, for '+' we only check the type
3459 if (negate)
3460 isn = generate_instr(cctx, ISN_NEGATENR);
3461 else
3462 isn = generate_instr(cctx, ISN_CHECKNR);
3463 if (isn == NULL)
3464 return FAIL;
3465 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003466 else if (numeric_only)
3467 {
3468 ++p;
3469 break;
3470 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003471 else
3472 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003473 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003475 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003476 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003477 if (p[-1] == '!')
3478 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003480 }
3481 if (generate_2BOOL(cctx, invert) == FAIL)
3482 return FAIL;
3483 }
3484 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003485 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003486 return OK;
3487}
3488
3489/*
3490 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003491 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003492 */
3493 static int
3494compile_subscript(
3495 char_u **arg,
3496 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003497 char_u *start_leader,
3498 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003499 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003501 char_u *name_start = *end_leader;
3502
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503 for (;;)
3504 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003505 char_u *p = skipwhite(*arg);
3506
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003507 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003508 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003509 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003510
3511 // If a following line starts with "->{" or "->X" advance to that
3512 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003513 // Also if a following line starts with ".x".
3514 if (next != NULL &&
3515 ((next[0] == '-' && next[1] == '>'
3516 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003517 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003518 {
3519 next = next_line_from_context(cctx, TRUE);
3520 if (next == NULL)
3521 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003522 *arg = next;
3523 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003524 }
3525 }
3526
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003527 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003528 // not a function call.
3529 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003530 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003531 garray_T *stack = &cctx->ctx_type_stack;
3532 type_T *type;
3533 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003535 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003536 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003537 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003538
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003540 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3541
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003542 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3544 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003545 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003546 return FAIL;
3547 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003548 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003550 char_u *pstart = p;
3551
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003552 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003553 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003554 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003555
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556 // something->method()
3557 // Apply the '!', '-' and '+' first:
3558 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003559 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003562 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003563 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003564 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565 if (**arg == '{')
3566 {
3567 // lambda call: list->{lambda}
Bram Moolenaar65c44152020-12-24 15:14:01 +01003568 // TODO: remove this
3569 if (compile_lambda_call(arg, cctx) == FAIL)
3570 return FAIL;
3571 }
3572 else if (**arg == '(')
3573 {
3574 // Funcref call: list->(Refs[2])()
3575 // or lambda: list->((arg) => expr)()
3576 // TODO: make this work
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 if (compile_lambda_call(arg, cctx) == FAIL)
3578 return FAIL;
3579 }
3580 else
3581 {
3582 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003583 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003584 if (!eval_isnamec1(*p))
3585 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003586 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003587 return FAIL;
3588 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003589 if (ASCII_ISALPHA(*p) && p[1] == ':')
3590 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003591 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003592 ;
3593 if (*p != '(')
3594 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003595 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596 return FAIL;
3597 }
3598 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003599 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600 return FAIL;
3601 }
3602 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003603 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003604 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003605 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003606 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003607 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003608 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003609 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003610
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003611 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003612 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003613 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003614 // TODO: blob index
3615 // TODO: more arguments
3616 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003617 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003618 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003619 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003620
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003621 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003622 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003623 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003624 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003625 if (**arg == ':')
3626 // missing first index is equal to zero
3627 generate_PUSHNR(cctx, 0);
3628 else
3629 {
3630 if (compile_expr0(arg, cctx) == FAIL)
3631 return FAIL;
3632 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3633 return FAIL;
3634 *arg = skipwhite(*arg);
3635 }
3636 if (**arg == ':')
3637 {
3638 *arg = skipwhite(*arg + 1);
3639 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3640 return FAIL;
3641 if (**arg == ']')
3642 // missing second index is equal to end of string
3643 generate_PUSHNR(cctx, -1);
3644 else
3645 {
3646 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003647 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003648 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3649 return FAIL;
3650 *arg = skipwhite(*arg);
3651 }
3652 is_slice = TRUE;
3653 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654
3655 if (**arg != ']')
3656 {
3657 emsg(_(e_missbrac));
3658 return FAIL;
3659 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003660 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003662 // We can index a list and a dict. If we don't know the type
3663 // we can use the index value type.
3664 // TODO: If we don't know use an instruction to figure it out at
3665 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003666 typep = ((type_T **)stack->ga_data) + stack->ga_len
3667 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003668 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003669 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3670 // If the index is a string, the variable must be a Dict.
3671 if (*typep == &t_any && valtype == &t_string)
3672 vtype = VAR_DICT;
3673 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003674 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003675 if (need_type(valtype, &t_number, -1, cctx,
3676 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003677 return FAIL;
3678 if (is_slice)
3679 {
3680 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003681 if (need_type(valtype, &t_number, -2, cctx,
3682 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003683 return FAIL;
3684 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003685 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003686
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003687 if (vtype == VAR_DICT)
3688 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003689 if (is_slice)
3690 {
3691 emsg(_(e_cannot_slice_dictionary));
3692 return FAIL;
3693 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003694 if ((*typep)->tt_type == VAR_DICT)
3695 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003696 else
3697 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003698 if (need_type(*typep, &t_dict_any, -2, cctx,
3699 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003700 return FAIL;
3701 *typep = &t_any;
3702 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003703 if (may_generate_2STRING(-1, cctx) == FAIL)
3704 return FAIL;
3705 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3706 return FAIL;
3707 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003708 else if (vtype == VAR_STRING)
3709 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003710 *typep = &t_string;
3711 if ((is_slice
3712 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3713 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003714 return FAIL;
3715 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003716 else if (vtype == VAR_BLOB)
3717 {
3718 emsg("Sorry, blob index and slice not implemented yet");
3719 return FAIL;
3720 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003721 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003722 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003723 if (is_slice)
3724 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003725 if (generate_instr_drop(cctx,
3726 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3727 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003728 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003729 }
Bram Moolenaared591872020-08-15 22:14:53 +02003730 else
3731 {
3732 if ((*typep)->tt_type == VAR_LIST)
3733 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003734 if (generate_instr_drop(cctx,
3735 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3736 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003737 return FAIL;
3738 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003739 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003740 else
3741 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003742 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003743 return FAIL;
3744 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003745 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003746 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003748 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003749 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003750 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003751 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003752
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003753 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003754 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003755 {
3756 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003757 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003758 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003759 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003760 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003761 while (eval_isnamec(*p))
3762 MB_PTR_ADV(p);
3763 if (p == *arg)
3764 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003765 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 return FAIL;
3767 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003768 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769 return FAIL;
3770 *arg = p;
3771 }
3772 else
3773 break;
3774 }
3775
3776 // TODO - see handle_subscript():
3777 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3778 // Don't do this when "Func" is already a partial that was bound
3779 // explicitly (pt_auto is FALSE).
3780
3781 return OK;
3782}
3783
3784/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003785 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3786 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003788 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003789 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003790 *
3791 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792 */
3793
3794/*
3795 * number number constant
3796 * 0zFFFFFFFF Blob constant
3797 * "string" string constant
3798 * 'string' literal string constant
3799 * &option-name option value
3800 * @r register contents
3801 * identifier variable value
3802 * function() function call
3803 * $VAR environment variable
3804 * (expression) nested expression
3805 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01003806 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807 *
3808 * Also handle:
3809 * ! in front logical NOT
3810 * - in front unary minus
3811 * + in front unary plus (ignored)
3812 * trailing (arg) funcref/partial call
3813 * trailing [] subscript in String or List
3814 * trailing .name entry in Dictionary
3815 * trailing ->name() method call
3816 */
3817 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003818compile_expr7(
3819 char_u **arg,
3820 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003821 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 char_u *start_leader, *end_leader;
3824 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003825 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003826 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003828 ppconst->pp_is_const = FALSE;
3829
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830 /*
3831 * Skip '!', '-' and '+' characters. They are handled later.
3832 */
3833 start_leader = *arg;
3834 while (**arg == '!' || **arg == '-' || **arg == '+')
3835 *arg = skipwhite(*arg + 1);
3836 end_leader = *arg;
3837
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003838 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839 switch (**arg)
3840 {
3841 /*
3842 * Number constant.
3843 */
3844 case '0': // also for blob starting with 0z
3845 case '1':
3846 case '2':
3847 case '3':
3848 case '4':
3849 case '5':
3850 case '6':
3851 case '7':
3852 case '8':
3853 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003854 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003855 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003856 // Apply "-" and "+" just before the number now, right to
3857 // left. Matters especially when "->" follows. Stops at
3858 // '!'.
3859 if (apply_leader(rettv, TRUE,
3860 start_leader, &end_leader) == FAIL)
3861 {
3862 clear_tv(rettv);
3863 return FAIL;
3864 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 break;
3866
3867 /*
3868 * String constant: "string".
3869 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003870 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871 return FAIL;
3872 break;
3873
3874 /*
3875 * Literal string constant: 'str''ing'.
3876 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003877 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878 return FAIL;
3879 break;
3880
3881 /*
3882 * Constant Vim variable.
3883 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003884 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 ret = NOTDONE;
3886 break;
3887
3888 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003889 * "true" constant
3890 */
3891 case 't': if (STRNCMP(*arg, "true", 4) == 0
3892 && !eval_isnamec((*arg)[4]))
3893 {
3894 *arg += 4;
3895 rettv->v_type = VAR_BOOL;
3896 rettv->vval.v_number = VVAL_TRUE;
3897 }
3898 else
3899 ret = NOTDONE;
3900 break;
3901
3902 /*
3903 * "false" constant
3904 */
3905 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3906 && !eval_isnamec((*arg)[5]))
3907 {
3908 *arg += 5;
3909 rettv->v_type = VAR_BOOL;
3910 rettv->vval.v_number = VVAL_FALSE;
3911 }
3912 else
3913 ret = NOTDONE;
3914 break;
3915
3916 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 * List: [expr, expr]
3918 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003919 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920 break;
3921
3922 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 * Lambda: {arg, arg -> expr}
3924 * Dictionary: {'key': val, 'key': val}
3925 */
3926 case '{': {
Bram Moolenaar27bf7af2020-12-23 20:27:31 +01003927 char_u *start = skipwhite(*arg + 1);
3928 char_u *after = start;
3929 garray_T ga_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003930
3931 // Find out what comes after the arguments.
Bram Moolenaar27bf7af2020-12-23 20:27:31 +01003932 ret = get_function_args(&after, '-', NULL,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003933 &ga_arg, TRUE, NULL, NULL,
3934 TRUE, NULL, NULL);
Bram Moolenaar27bf7af2020-12-23 20:27:31 +01003935 if (ret != FAIL && after[0] == '>'
3936 && ((after > start + 2
3937 && VIM_ISWHITE(after[-2]))
3938 || after == start + 1)
3939 && IS_WHITE_OR_NUL(after[1]))
Bram Moolenaar65c44152020-12-24 15:14:01 +01003940 // TODO: if we go with the "(arg) => expr" syntax
3941 // remove this
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942 ret = compile_lambda(arg, cctx);
3943 else
Bram Moolenaare0de1712020-12-02 17:36:54 +01003944 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 }
3946 break;
3947
3948 /*
3949 * Option value: &name
3950 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003951 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3952 return FAIL;
3953 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 break;
3955
3956 /*
3957 * Environment variable: $VAR.
3958 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003959 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3960 return FAIL;
3961 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962 break;
3963
3964 /*
3965 * Register contents: @r.
3966 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003967 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3968 return FAIL;
3969 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 break;
3971 /*
3972 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01003973 * lambda: (arg, arg) => expr
3974 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003975 */
Bram Moolenaar65c44152020-12-24 15:14:01 +01003976 case '(': {
3977 char_u *start = skipwhite(*arg + 1);
3978 char_u *after = start;
3979 garray_T ga_arg;
Bram Moolenaar1c747212020-05-09 18:28:34 +02003980
Bram Moolenaar65c44152020-12-24 15:14:01 +01003981 // Find out if "=>" comes after the ().
3982 ret = get_function_args(&after, ')', NULL,
3983 &ga_arg, TRUE, NULL, NULL,
3984 TRUE, NULL, NULL);
3985 if (ret == OK && VIM_ISWHITE(
3986 *after == ':' ? after[1] : *after))
3987 {
3988 if (*after == ':')
3989 // Skip over type in "(arg): type".
3990 after = skip_type(skipwhite(after + 1), TRUE);
3991
3992 after = skipwhite(after);
3993 if (after[0] == '=' && after[1] == '>'
3994 && IS_WHITE_OR_NUL(after[2]))
3995 {
3996 ret = compile_lambda(arg, cctx);
3997 break;
3998 }
3999 }
4000
4001 // (expression): recursive!
4002 *arg = skipwhite(*arg + 1);
4003 if (ppconst->pp_used <= PPSIZE - 10)
4004 {
4005 ret = compile_expr1(arg, cctx, ppconst);
4006 }
4007 else
4008 {
4009 // Not enough space in ppconst, flush constants.
4010 if (generate_ppconst(cctx, ppconst) == FAIL)
4011 return FAIL;
4012 ret = compile_expr0(arg, cctx);
4013 }
4014 *arg = skipwhite(*arg);
4015 if (**arg == ')')
4016 ++*arg;
4017 else if (ret == OK)
4018 {
4019 emsg(_(e_missing_close));
4020 ret = FAIL;
4021 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 }
4023 break;
4024
4025 default: ret = NOTDONE;
4026 break;
4027 }
4028 if (ret == FAIL)
4029 return FAIL;
4030
Bram Moolenaar1c747212020-05-09 18:28:34 +02004031 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004033 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004034 clear_tv(rettv);
4035 else
4036 // A constant expression can possibly be handled compile time,
4037 // return the value instead of generating code.
4038 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039 }
4040 else if (ret == NOTDONE)
4041 {
4042 char_u *p;
4043 int r;
4044
4045 if (!eval_isnamec1(**arg))
4046 {
Bram Moolenaare4984292020-12-13 14:19:25 +01004047 if (ends_excmd(*skipwhite(*arg)))
4048 semsg(_(e_empty_expression_str), *arg);
4049 else
4050 semsg(_(e_name_expected_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051 return FAIL;
4052 }
4053
4054 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004055 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004056 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004057 {
4058 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4059 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004061 {
4062 if (generate_ppconst(cctx, ppconst) == FAIL)
4063 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004064 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004065 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004066 if (r == FAIL)
4067 return FAIL;
4068 }
4069
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004070 // Handle following "[]", ".member", etc.
4071 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004072 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004073 ppconst) == FAIL)
4074 return FAIL;
4075 if (ppconst->pp_used > 0)
4076 {
4077 // apply the '!', '-' and '+' before the constant
4078 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004079 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004080 return FAIL;
4081 return OK;
4082 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004083 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004085 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004086}
4087
4088/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004089 * Give the "white on both sides" error, taking the operator from "p[len]".
4090 */
4091 void
4092error_white_both(char_u *op, int len)
4093{
4094 char_u buf[10];
4095
4096 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004097 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004098}
4099
4100/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004101 * <type>expr7: runtime type check / conversion
4102 */
4103 static int
4104compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4105{
4106 type_T *want_type = NULL;
4107
4108 // Recognize <type>
4109 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4110 {
4111 int called_emsg_before = called_emsg;
4112
4113 ++*arg;
4114 want_type = parse_type(arg, cctx->ctx_type_list);
4115 if (called_emsg != called_emsg_before)
4116 return FAIL;
4117
4118 if (**arg != '>')
4119 {
4120 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004121 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004122 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004123 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004124 return FAIL;
4125 }
4126 ++*arg;
4127 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
4128 return FAIL;
4129 }
4130
4131 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4132 return FAIL;
4133
4134 if (want_type != NULL)
4135 {
4136 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004137 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004138
Bram Moolenaard1103582020-08-14 22:44:25 +02004139 generate_ppconst(cctx, ppconst);
4140 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02004141 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004142 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004143 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004144 return FAIL;
4145 }
4146 }
4147
4148 return OK;
4149}
4150
4151/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152 * * number multiplication
4153 * / number division
4154 * % number modulo
4155 */
4156 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004157compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004158{
4159 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004160 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004161 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004162
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004163 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004164 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165 return FAIL;
4166
4167 /*
4168 * Repeat computing, until no "*", "/" or "%" is following.
4169 */
4170 for (;;)
4171 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004172 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004173 if (*op != '*' && *op != '/' && *op != '%')
4174 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004175 if (next != NULL)
4176 {
4177 *arg = next_line_from_context(cctx, TRUE);
4178 op = skipwhite(*arg);
4179 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004180
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004181 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004182 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004183 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004184 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 }
4186 *arg = skipwhite(op + 1);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004187 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004188 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004189
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004190 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004191 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004192 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004193
4194 if (ppconst->pp_used == ppconst_used + 2
4195 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4196 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004197 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004198 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4199 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004200 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004202 // both are numbers: compute the result
4203 switch (*op)
4204 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004205 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004206 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004207 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004208 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004209 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004210 break;
4211 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004212 tv1->vval.v_number = res;
4213 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004214 }
4215 else
4216 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004217 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004218 generate_two_op(cctx, op);
4219 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 }
4221
4222 return OK;
4223}
4224
4225/*
4226 * + number addition
4227 * - number subtraction
4228 * .. string concatenation
4229 */
4230 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004231compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232{
4233 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004234 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004235 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004236 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004237
4238 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004239 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240 return FAIL;
4241
4242 /*
4243 * Repeat computing, until no "+", "-" or ".." is following.
4244 */
4245 for (;;)
4246 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004247 op = may_peek_next_line(cctx, *arg, &next);
4248 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004249 break;
4250 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004251 if (next != NULL)
4252 {
4253 *arg = next_line_from_context(cctx, TRUE);
4254 op = skipwhite(*arg);
4255 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004256
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004257 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004258 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004259 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004260 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004261 }
4262
4263 *arg = skipwhite(op + oplen);
Bram Moolenaare0de1712020-12-02 17:36:54 +01004264 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004265 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004267 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004268 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269 return FAIL;
4270
Bram Moolenaara5565e42020-05-09 15:44:01 +02004271 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004272 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004273 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4274 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4275 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4276 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004278 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4279 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004280
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004281 // concat/subtract/add constant numbers
4282 if (*op == '+')
4283 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4284 else if (*op == '-')
4285 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4286 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004287 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004288 // concatenate constant strings
4289 char_u *s1 = tv1->vval.v_string;
4290 char_u *s2 = tv2->vval.v_string;
4291 size_t len1 = STRLEN(s1);
4292
4293 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4294 if (tv1->vval.v_string == NULL)
4295 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004296 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004297 return FAIL;
4298 }
4299 mch_memmove(tv1->vval.v_string, s1, len1);
4300 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004301 vim_free(s1);
4302 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004303 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004304 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 }
4306 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004307 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004308 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004309 if (*op == '.')
4310 {
4311 if (may_generate_2STRING(-2, cctx) == FAIL
4312 || may_generate_2STRING(-1, cctx) == FAIL)
4313 return FAIL;
4314 generate_instr_drop(cctx, ISN_CONCAT, 1);
4315 }
4316 else
4317 generate_two_op(cctx, op);
4318 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 }
4320
4321 return OK;
4322}
4323
4324/*
4325 * expr5a == expr5b
4326 * expr5a =~ expr5b
4327 * expr5a != expr5b
4328 * expr5a !~ expr5b
4329 * expr5a > expr5b
4330 * expr5a >= expr5b
4331 * expr5a < expr5b
4332 * expr5a <= expr5b
4333 * expr5a is expr5b
4334 * expr5a isnot expr5b
4335 *
4336 * Produces instructions:
4337 * EVAL expr5a Push result of "expr5a"
4338 * EVAL expr5b Push result of "expr5b"
4339 * COMPARE one of the compare instructions
4340 */
4341 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004342compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343{
4344 exptype_T type = EXPR_UNKNOWN;
4345 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004346 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004349 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350
4351 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004352 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004353 return FAIL;
4354
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004355 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004356 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357
4358 /*
4359 * If there is a comparative operator, use it.
4360 */
4361 if (type != EXPR_UNKNOWN)
4362 {
4363 int ic = FALSE; // Default: do not ignore case
4364
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004365 if (next != NULL)
4366 {
4367 *arg = next_line_from_context(cctx, TRUE);
4368 p = skipwhite(*arg);
4369 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370 if (type_is && (p[len] == '?' || p[len] == '#'))
4371 {
4372 semsg(_(e_invexpr2), *arg);
4373 return FAIL;
4374 }
4375 // extra question mark appended: ignore case
4376 if (p[len] == '?')
4377 {
4378 ic = TRUE;
4379 ++len;
4380 }
4381 // extra '#' appended: match case (ignored)
4382 else if (p[len] == '#')
4383 ++len;
4384 // nothing appended: match case
4385
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004386 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004388 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004389 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390 }
4391
4392 // get the second variable
4393 *arg = skipwhite(p + len);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004394 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004395 return FAIL;
4396
Bram Moolenaara5565e42020-05-09 15:44:01 +02004397 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 return FAIL;
4399
Bram Moolenaara5565e42020-05-09 15:44:01 +02004400 if (ppconst->pp_used == ppconst_used + 2)
4401 {
4402 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4403 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4404 int ret;
4405
4406 // Both sides are a constant, compute the result now.
4407 // First check for a valid combination of types, this is more
4408 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004409 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004410 ret = FAIL;
4411 else
4412 {
4413 ret = typval_compare(tv1, tv2, type, ic);
4414 tv1->v_type = VAR_BOOL;
4415 tv1->vval.v_number = tv1->vval.v_number
4416 ? VVAL_TRUE : VVAL_FALSE;
4417 clear_tv(tv2);
4418 --ppconst->pp_used;
4419 }
4420 return ret;
4421 }
4422
4423 generate_ppconst(cctx, ppconst);
4424 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 }
4426
4427 return OK;
4428}
4429
Bram Moolenaar7f141552020-05-09 17:35:53 +02004430static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4431
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432/*
4433 * Compile || or &&.
4434 */
4435 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004436compile_and_or(
4437 char_u **arg,
4438 cctx_T *cctx,
4439 char *op,
4440 ppconst_T *ppconst,
4441 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004443 char_u *next;
4444 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 int opchar = *op;
4446
4447 if (p[0] == opchar && p[1] == opchar)
4448 {
4449 garray_T *instr = &cctx->ctx_instr;
4450 garray_T end_ga;
4451
4452 /*
4453 * Repeat until there is no following "||" or "&&"
4454 */
4455 ga_init2(&end_ga, sizeof(int), 10);
4456 while (p[0] == opchar && p[1] == opchar)
4457 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004458 if (next != NULL)
4459 {
4460 *arg = next_line_from_context(cctx, TRUE);
4461 p = skipwhite(*arg);
4462 }
4463
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004464 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4465 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004466 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004467 return FAIL;
4468 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004469
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004470 // TODO: use ppconst if the value is a constant and check
4471 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004472 generate_ppconst(cctx, ppconst);
4473
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004474 // Every part must evaluate to a bool.
4475 if (bool_on_stack(cctx) == FAIL)
4476 {
4477 ga_clear(&end_ga);
4478 return FAIL;
4479 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004480
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481 if (ga_grow(&end_ga, 1) == FAIL)
4482 {
4483 ga_clear(&end_ga);
4484 return FAIL;
4485 }
4486 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4487 ++end_ga.ga_len;
4488 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004489 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490
4491 // eval the next expression
4492 *arg = skipwhite(p + 2);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004493 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004494 {
4495 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004496 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004497 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004498
Bram Moolenaara5565e42020-05-09 15:44:01 +02004499 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4500 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004501 {
4502 ga_clear(&end_ga);
4503 return FAIL;
4504 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004505
4506 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004507 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004508 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004509
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004510 // Every part must evaluate to a bool.
4511 if (bool_on_stack(cctx) == FAIL)
4512 {
4513 ga_clear(&end_ga);
4514 return FAIL;
4515 }
4516
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004517 // Fill in the end label in all jumps.
4518 while (end_ga.ga_len > 0)
4519 {
4520 isn_T *isn;
4521
4522 --end_ga.ga_len;
4523 isn = ((isn_T *)instr->ga_data)
4524 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4525 isn->isn_arg.jump.jump_where = instr->ga_len;
4526 }
4527 ga_clear(&end_ga);
4528 }
4529
4530 return OK;
4531}
4532
4533/*
4534 * expr4a && expr4a && expr4a logical AND
4535 *
4536 * Produces instructions:
4537 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004538 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004539 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004540 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004541 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542 * EVAL expr4c Push result of "expr4c"
4543 * end:
4544 */
4545 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004546compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004547{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004548 int ppconst_used = ppconst->pp_used;
4549
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004550 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004551 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552 return FAIL;
4553
4554 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004555 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004556}
4557
4558/*
4559 * expr3a || expr3b || expr3c logical OR
4560 *
4561 * Produces instructions:
4562 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004563 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004564 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004565 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004566 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004567 * EVAL expr3c Push result of "expr3c"
4568 * end:
4569 */
4570 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004571compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004572{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004573 int ppconst_used = ppconst->pp_used;
4574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004576 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004577 return FAIL;
4578
4579 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004580 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004581}
4582
4583/*
4584 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004586 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587 * JUMP_IF_FALSE alt jump if false
4588 * EVAL expr1a
4589 * JUMP_ALWAYS end
4590 * alt: EVAL expr1b
4591 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004592 *
4593 * Toplevel expression: expr2 ?? expr1
4594 * Produces instructions:
4595 * EVAL expr2 Push result of "expr2"
4596 * JUMP_AND_KEEP_IF_TRUE end jump if true
4597 * EVAL expr1
4598 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004599 */
4600 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004601compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602{
4603 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004604 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004605 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004606
Bram Moolenaar3988f642020-08-27 22:43:03 +02004607 // Ignore all kinds of errors when not producing code.
4608 if (cctx->ctx_skip == SKIP_YES)
4609 {
Bram Moolenaar683581e2020-10-22 21:22:58 +02004610 evalarg_T evalarg;
4611
4612 CLEAR_FIELD(evalarg);
4613 evalarg.eval_cctx = cctx;
4614 skip_expr(arg, &evalarg);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004615 return OK;
4616 }
4617
Bram Moolenaar61a89812020-05-07 16:58:17 +02004618 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004619 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004620 return FAIL;
4621
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004622 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004623 if (*p == '?')
4624 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004625 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004626 garray_T *instr = &cctx->ctx_instr;
4627 garray_T *stack = &cctx->ctx_type_stack;
4628 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004629 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004630 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004631 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004632 int has_const_expr = FALSE;
4633 int const_value = FALSE;
4634 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004636 if (next != NULL)
4637 {
4638 *arg = next_line_from_context(cctx, TRUE);
4639 p = skipwhite(*arg);
4640 }
4641
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004642 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004643 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004644 semsg(_(e_white_space_required_before_and_after_str),
4645 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004646 return FAIL;
4647 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004648
Bram Moolenaara5565e42020-05-09 15:44:01 +02004649 if (ppconst->pp_used == ppconst_used + 1)
4650 {
4651 // the condition is a constant, we know whether the ? or the :
4652 // expression is to be evaluated.
4653 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004654 if (op_falsy)
4655 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4656 else
4657 {
4658 int error = FALSE;
4659
4660 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4661 &error);
4662 if (error)
4663 return FAIL;
4664 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004665 cctx->ctx_skip = save_skip == SKIP_YES ||
4666 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4667
4668 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4669 // "left ?? right" and "left" is truthy: produce "left"
4670 generate_ppconst(cctx, ppconst);
4671 else
4672 {
4673 clear_tv(&ppconst->pp_tv[ppconst_used]);
4674 --ppconst->pp_used;
4675 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004676 }
4677 else
4678 {
4679 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004680 if (op_falsy)
4681 end_idx = instr->ga_len;
4682 generate_JUMP(cctx, op_falsy
4683 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4684 if (op_falsy)
4685 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004686 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004687
4688 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004689 *arg = skipwhite(p + 1 + op_falsy);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004690 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004691 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004692 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004693 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694
Bram Moolenaara5565e42020-05-09 15:44:01 +02004695 if (!has_const_expr)
4696 {
4697 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004699 if (!op_falsy)
4700 {
4701 // remember the type and drop it
4702 --stack->ga_len;
4703 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004704
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004705 end_idx = instr->ga_len;
4706 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004707
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004708 // jump here from JUMP_IF_FALSE
4709 isn = ((isn_T *)instr->ga_data) + alt_idx;
4710 isn->isn_arg.jump.jump_where = instr->ga_len;
4711 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004712 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004714 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004716 // Check for the ":".
4717 p = may_peek_next_line(cctx, *arg, &next);
4718 if (*p != ':')
4719 {
4720 emsg(_(e_missing_colon));
4721 return FAIL;
4722 }
4723 if (next != NULL)
4724 {
4725 *arg = next_line_from_context(cctx, TRUE);
4726 p = skipwhite(*arg);
4727 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004728
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004729 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4730 {
4731 semsg(_(e_white_space_required_before_and_after_str), ":");
4732 return FAIL;
4733 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004734
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004735 // evaluate the third expression
4736 if (has_const_expr)
4737 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004738 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004739 *arg = skipwhite(p + 1);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004740 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004741 return FAIL;
4742 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4743 return FAIL;
4744 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004745
Bram Moolenaara5565e42020-05-09 15:44:01 +02004746 if (!has_const_expr)
4747 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004748 type_T **typep;
4749
Bram Moolenaara5565e42020-05-09 15:44:01 +02004750 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004751
Bram Moolenaara5565e42020-05-09 15:44:01 +02004752 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004753 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4754 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004755
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004756 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004757 isn = ((isn_T *)instr->ga_data) + end_idx;
4758 isn->isn_arg.jump.jump_where = instr->ga_len;
4759 }
4760
4761 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762 }
4763 return OK;
4764}
4765
4766/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004767 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004768 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4769 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004770 */
4771 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004772compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004773{
4774 ppconst_T ppconst;
4775
4776 CLEAR_FIELD(ppconst);
4777 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4778 {
4779 clear_ppconst(&ppconst);
4780 return FAIL;
4781 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004782 if (is_const != NULL)
4783 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004784 if (generate_ppconst(cctx, &ppconst) == FAIL)
4785 return FAIL;
4786 return OK;
4787}
4788
4789/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004790 * Toplevel expression.
4791 */
4792 static int
4793compile_expr0(char_u **arg, cctx_T *cctx)
4794{
4795 return compile_expr0_ext(arg, cctx, NULL);
4796}
4797
4798/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004799 * compile "return [expr]"
4800 */
4801 static char_u *
4802compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4803{
4804 char_u *p = arg;
4805 garray_T *stack = &cctx->ctx_type_stack;
4806 type_T *stack_type;
4807
4808 if (*p != NUL && *p != '|' && *p != '\n')
4809 {
4810 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004811 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004812 return NULL;
4813
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004814 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004815 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004816 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4817 if (set_return_type)
4818 cctx->ctx_ufunc->uf_ret_type = stack_type;
4819 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004820 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004821 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4822 && stack_type->tt_type != VAR_VOID
4823 && stack_type->tt_type != VAR_UNKNOWN)
4824 {
4825 emsg(_(e_returning_value_in_function_without_return_type));
4826 return NULL;
4827 }
4828 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004829 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004830 return NULL;
4831 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02004832 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004833 }
4834 else
4835 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004836 // "set_return_type" cannot be TRUE, only used for a lambda which
4837 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004838 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4839 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004840 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004841 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004842 return NULL;
4843 }
4844
4845 // No argument, return zero.
4846 generate_PUSHNR(cctx, 0);
4847 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004848 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004849 return NULL;
4850
4851 // "return val | endif" is possible
4852 return skipwhite(p);
4853}
4854
4855/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004856 * Get a line from the compilation context, compatible with exarg_T getline().
4857 * Return a pointer to the line in allocated memory.
4858 * Return NULL for end-of-file or some error.
4859 */
4860 static char_u *
4861exarg_getline(
4862 int c UNUSED,
4863 void *cookie,
4864 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004865 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004866{
4867 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004868 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004869
Bram Moolenaar66250c92020-08-20 15:02:42 +02004870 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004871 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004872 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004873 return NULL;
4874 ++cctx->ctx_lnum;
4875 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4876 // Comment lines result in NULL pointers, skip them.
4877 if (p != NULL)
4878 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004879 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004880}
4881
4882/*
4883 * Compile a nested :def command.
4884 */
4885 static char_u *
4886compile_nested_function(exarg_T *eap, cctx_T *cctx)
4887{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004888 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004889 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004890 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004891 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004892 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004893 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004894
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004895 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004896 {
4897 emsg(_(e_cannot_use_bang_with_nested_def));
4898 return NULL;
4899 }
4900
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004901 if (*name_start == '/')
4902 {
4903 name_end = skip_regexp(name_start + 1, '/', TRUE);
4904 if (*name_end == '/')
4905 ++name_end;
4906 eap->nextcmd = check_nextcmd(name_end);
4907 }
4908 if (name_end == name_start || *skipwhite(name_end) != '(')
4909 {
4910 if (!ends_excmd2(name_start, name_end))
4911 {
4912 semsg(_(e_invalid_command_str), eap->cmd);
4913 return NULL;
4914 }
4915
4916 // "def" or "def Name": list functions
4917 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
4918 return NULL;
4919 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
4920 }
4921
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004922 // Only g:Func() can use a namespace.
4923 if (name_start[1] == ':' && !is_global)
4924 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004925 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004926 return NULL;
4927 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004928 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4929 return NULL;
4930
Bram Moolenaar04b12692020-05-04 23:24:44 +02004931 eap->arg = name_end;
4932 eap->getline = exarg_getline;
4933 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004934 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004935 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004936 lambda_name = vim_strsave(get_lambda_name());
4937 if (lambda_name == NULL)
4938 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004939 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004940
Bram Moolenaar822ba242020-05-24 23:00:18 +02004941 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004942 {
4943 r = eap->skip ? OK : FAIL;
4944 goto theend;
4945 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004946 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004947 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004948 {
4949 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004950 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004951 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004952
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004953 if (is_global)
4954 {
4955 char_u *func_name = vim_strnsave(name_start + 2,
4956 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004957
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004958 if (func_name == NULL)
4959 r = FAIL;
4960 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004961 {
Bram Moolenaareef21022020-08-01 22:16:43 +02004962 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004963 lambda_name = NULL;
4964 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004965 }
4966 else
4967 {
4968 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004969 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004970 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004971 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004972
Bram Moolenaareef21022020-08-01 22:16:43 +02004973 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004974 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004975 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004976 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004977 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004978
4979 // copy over the block scope IDs
4980 if (block_depth > 0)
4981 {
4982 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4983 if (ufunc->uf_block_ids != NULL)
4984 {
4985 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4986 sizeof(int) * block_depth);
4987 ufunc->uf_block_depth = block_depth;
4988 }
4989 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004990 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02004991 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004992 r = OK;
4993
4994theend:
4995 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004996 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004997}
4998
4999/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005000 * Return the length of an assignment operator, or zero if there isn't one.
5001 */
5002 int
5003assignment_len(char_u *p, int *heredoc)
5004{
5005 if (*p == '=')
5006 {
5007 if (p[1] == '<' && p[2] == '<')
5008 {
5009 *heredoc = TRUE;
5010 return 3;
5011 }
5012 return 1;
5013 }
5014 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5015 return 2;
5016 if (STRNCMP(p, "..=", 3) == 0)
5017 return 3;
5018 return 0;
5019}
5020
5021// words that cannot be used as a variable
5022static char *reserved[] = {
5023 "true",
5024 "false",
5025 NULL
5026};
5027
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005028typedef enum {
5029 dest_local,
5030 dest_option,
5031 dest_env,
5032 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005033 dest_buffer,
5034 dest_window,
5035 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005036 dest_vimvar,
5037 dest_script,
5038 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005039 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005040} assign_dest_T;
5041
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005042/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005043 * Generate the load instruction for "name".
5044 */
5045 static void
5046generate_loadvar(
5047 cctx_T *cctx,
5048 assign_dest_T dest,
5049 char_u *name,
5050 lvar_T *lvar,
5051 type_T *type)
5052{
5053 switch (dest)
5054 {
5055 case dest_option:
5056 // TODO: check the option exists
5057 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5058 break;
5059 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005060 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5061 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5062 else
5063 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005064 break;
5065 case dest_buffer:
5066 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5067 break;
5068 case dest_window:
5069 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5070 break;
5071 case dest_tab:
5072 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5073 break;
5074 case dest_script:
5075 compile_load_scriptvar(cctx,
5076 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5077 break;
5078 case dest_env:
5079 // Include $ in the name here
5080 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5081 break;
5082 case dest_reg:
5083 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5084 break;
5085 case dest_vimvar:
5086 generate_LOADV(cctx, name + 2, TRUE);
5087 break;
5088 case dest_local:
5089 if (lvar->lv_from_outer)
5090 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
5091 NULL, type);
5092 else
5093 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5094 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005095 case dest_expr:
5096 // list or dict value should already be on the stack.
5097 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005098 }
5099}
5100
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005101/*
5102 * Skip over "[expr]" or ".member".
5103 * Does not check for any errors.
5104 */
5105 static char_u *
5106skip_index(char_u *start)
5107{
5108 char_u *p = start;
5109
5110 if (*p == '[')
5111 {
5112 p = skipwhite(p + 1);
5113 (void)skip_expr(&p, NULL);
5114 p = skipwhite(p);
5115 if (*p == ']')
5116 return p + 1;
5117 return p;
5118 }
5119 // if (*p == '.')
5120 return to_name_end(p + 1, TRUE);
5121}
5122
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005123 void
5124vim9_declare_error(char_u *name)
5125{
5126 char *scope = "";
5127
5128 switch (*name)
5129 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005130 case 'g': scope = _("global"); break;
5131 case 'b': scope = _("buffer"); break;
5132 case 'w': scope = _("window"); break;
5133 case 't': scope = _("tab"); break;
5134 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005135 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005136 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005137 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005138 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005139 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005140 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005141 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005142 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005143 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005144}
5145
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005146/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005147 * For one assignment figure out the type of destination. Return it in "dest".
5148 * When not recognized "dest" is not set.
5149 * For an option "opt_flags" is set.
5150 * For a v:var "vimvaridx" is set.
5151 * "type" is set to the destination type if known, unchanted otherwise.
5152 * Return FAIL if an error message was given.
5153 */
5154 static int
5155get_var_dest(
5156 char_u *name,
5157 assign_dest_T *dest,
5158 int cmdidx,
5159 int *opt_flags,
5160 int *vimvaridx,
5161 type_T **type,
5162 cctx_T *cctx)
5163{
5164 char_u *p;
5165
5166 if (*name == '&')
5167 {
5168 int cc;
5169 long numval;
5170 int opt_type;
5171
5172 *dest = dest_option;
5173 if (cmdidx == CMD_final || cmdidx == CMD_const)
5174 {
5175 emsg(_(e_const_option));
5176 return FAIL;
5177 }
5178 p = name;
5179 p = find_option_end(&p, opt_flags);
5180 if (p == NULL)
5181 {
5182 // cannot happen?
5183 emsg(_(e_letunexp));
5184 return FAIL;
5185 }
5186 cc = *p;
5187 *p = NUL;
5188 opt_type = get_option_value(skip_option_env_lead(name),
5189 &numval, NULL, *opt_flags);
5190 *p = cc;
5191 if (opt_type == -3)
5192 {
5193 semsg(_(e_unknown_option), name);
5194 return FAIL;
5195 }
5196 if (opt_type == -2 || opt_type == 0)
5197 *type = &t_string;
5198 else
5199 *type = &t_number; // both number and boolean option
5200 }
5201 else if (*name == '$')
5202 {
5203 *dest = dest_env;
5204 *type = &t_string;
5205 }
5206 else if (*name == '@')
5207 {
5208 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5209 {
5210 emsg_invreg(name[1]);
5211 return FAIL;
5212 }
5213 *dest = dest_reg;
5214 *type = &t_string;
5215 }
5216 else if (STRNCMP(name, "g:", 2) == 0)
5217 {
5218 *dest = dest_global;
5219 }
5220 else if (STRNCMP(name, "b:", 2) == 0)
5221 {
5222 *dest = dest_buffer;
5223 }
5224 else if (STRNCMP(name, "w:", 2) == 0)
5225 {
5226 *dest = dest_window;
5227 }
5228 else if (STRNCMP(name, "t:", 2) == 0)
5229 {
5230 *dest = dest_tab;
5231 }
5232 else if (STRNCMP(name, "v:", 2) == 0)
5233 {
5234 typval_T *vtv;
5235 int di_flags;
5236
5237 *vimvaridx = find_vim_var(name + 2, &di_flags);
5238 if (*vimvaridx < 0)
5239 {
5240 semsg(_(e_variable_not_found_str), name);
5241 return FAIL;
5242 }
5243 // We use the current value of "sandbox" here, is that OK?
5244 if (var_check_ro(di_flags, name, FALSE))
5245 return FAIL;
5246 *dest = dest_vimvar;
5247 vtv = get_vim_var_tv(*vimvaridx);
5248 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5249 }
5250 return OK;
5251}
5252
5253/*
5254 * Generate a STORE instruction for "dest", not being "dest_local".
5255 * Return FAIL when out of memory.
5256 */
5257 static int
5258generate_store_var(
5259 cctx_T *cctx,
5260 assign_dest_T dest,
5261 int opt_flags,
5262 int vimvaridx,
5263 int scriptvar_idx,
5264 int scriptvar_sid,
5265 type_T *type,
5266 char_u *name)
5267{
5268 switch (dest)
5269 {
5270 case dest_option:
5271 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5272 opt_flags);
5273 case dest_global:
5274 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005275 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5276 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005277 case dest_buffer:
5278 // include b: with the name, easier to execute that way
5279 return generate_STORE(cctx, ISN_STOREB, 0, name);
5280 case dest_window:
5281 // include w: with the name, easier to execute that way
5282 return generate_STORE(cctx, ISN_STOREW, 0, name);
5283 case dest_tab:
5284 // include t: with the name, easier to execute that way
5285 return generate_STORE(cctx, ISN_STORET, 0, name);
5286 case dest_env:
5287 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5288 case dest_reg:
5289 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5290 case dest_vimvar:
5291 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5292 case dest_script:
5293 if (scriptvar_idx < 0)
5294 {
5295 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005296 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005297
Bram Moolenaar41d61962020-12-06 20:12:43 +01005298 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005299 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5300 scriptvar_sid, type);
5301 if (name_s != name)
5302 vim_free(name_s);
5303 return r;
5304 }
5305 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5306 scriptvar_sid, scriptvar_idx, type);
5307 case dest_local:
5308 case dest_expr:
5309 // cannot happen
5310 break;
5311 }
5312 return FAIL;
5313}
5314
5315/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005316 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005317 * "let name"
5318 * "var name = expr"
5319 * "final name = expr"
5320 * "const name = expr"
5321 * "name = expr"
5322 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005323 * Return NULL for an error.
5324 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005325 */
5326 static char_u *
5327compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5328{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005329 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005330 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005331 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005332 char_u *ret = NULL;
5333 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005334 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005335 int scriptvar_sid = 0;
5336 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005337 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005338 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005339 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005340 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005341 int oplen = 0;
5342 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005343 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005344 type_T *member_type = &t_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005345 type_T *rhs_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005346 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005347 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005348 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
5349 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005350
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005351 // Skip over the "var" or "[var, var]" to get to any "=".
5352 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5353 if (p == NULL)
5354 return *arg == '[' ? arg : NULL;
5355
5356 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005357 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005358 // TODO: should we allow this, and figure out type inference from list
5359 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005360 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005361 return NULL;
5362 }
5363
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005364 sp = p;
5365 p = skipwhite(p);
5366 op = p;
5367 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005368
5369 if (var_count > 0 && oplen == 0)
5370 // can be something like "[1, 2]->func()"
5371 return arg;
5372
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005373 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005374 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005375 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005376 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005377 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005378
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005379 if (heredoc)
5380 {
5381 list_T *l;
5382 listitem_T *li;
5383
5384 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005385 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005386 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005387 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005388 if (l == NULL)
5389 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005390
Bram Moolenaar078269b2020-09-21 20:35:55 +02005391 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005392 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005393 // Push each line and the create the list.
5394 FOR_ALL_LIST_ITEMS(l, li)
5395 {
5396 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5397 li->li_tv.vval.v_string = NULL;
5398 }
5399 generate_NEWLIST(cctx, l->lv_len);
5400 type = &t_list_string;
5401 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005402 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005403 list_free(l);
5404 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005405 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005406 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005407 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005408 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005409 char_u *wp;
5410
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005411 // for "[var, var] = expr" evaluate the expression here, loop over the
5412 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005413 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005414
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005415 wp = op + oplen;
5416 p = skipwhite(wp);
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01005417 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005418 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005419 if (compile_expr0(&p, cctx) == FAIL)
5420 return NULL;
5421 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005422
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005423 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005424 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005425 type_T *stacktype;
5426
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005427 stacktype = stack->ga_len == 0 ? &t_void
5428 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005429 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005430 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005431 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005432 goto theend;
5433 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005434 if (need_type(stacktype, &t_list_any, -1, cctx,
5435 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005436 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005437 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005438 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5439 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005440 if (stacktype->tt_member != NULL)
5441 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005442 }
5443 }
5444
5445 /*
5446 * Loop over variables in "[var, var] = expr".
5447 * For "var = expr" and "let var: type" this is done only once.
5448 */
5449 if (var_count > 0)
5450 var_start = skipwhite(arg + 1); // skip over the "["
5451 else
5452 var_start = arg;
5453 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5454 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005455 char_u *var_end;
5456 char_u *dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005457 size_t varlen;
5458 int new_local = FALSE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005459 assign_dest_T dest = dest_local;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005460 int opt_flags = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005461 int vimvaridx = -1;
Bram Moolenaar709664c2020-12-12 14:33:41 +01005462 lvar_T local_lvar;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005463 lvar_T *lvar = NULL;
5464 lvar_T arg_lvar;
5465 int has_type = FALSE;
5466 int has_index = FALSE;
5467 int instr_count = -1;
5468
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005469 // "dest_end" is the end of the destination, including "[expr]" or
5470 // ".name".
5471 // "var_end" is the end of the variable/option/etc. name.
5472 dest_end = skip_var_one(var_start, FALSE);
Bram Moolenaar65821722020-08-02 18:58:54 +02005473 if (*var_start == '@')
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005474 var_end = var_start + 2;
Bram Moolenaar65821722020-08-02 18:58:54 +02005475 else
5476 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02005477 // skip over the leading "&", "&l:", "&g:" and "$"
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005478 var_end = skip_option_env_lead(var_start);
5479 var_end = to_name_end(var_end, TRUE);
Bram Moolenaar65821722020-08-02 18:58:54 +02005480 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005481
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005482 // "a: type" is declaring variable "a" with a type, not dict "a:".
5483 if (is_decl && dest_end == var_start + 2 && dest_end[-1] == ':')
5484 --dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005485 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5486 --var_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005487
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005488 // compute the length of the destination without "[expr]" or ".name"
5489 varlen = var_end - var_start;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005490 vim_free(name);
5491 name = vim_strnsave(var_start, varlen);
5492 if (name == NULL)
5493 return NULL;
5494 if (!heredoc)
5495 type = &t_any;
5496
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005497 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005498 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005499 int declare_error = FALSE;
5500
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005501 if (get_var_dest(name, &dest, cmdidx, &opt_flags,
5502 &vimvaridx, &type, cctx) == FAIL)
5503 goto theend;
5504 if (dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005505 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005506 // Specific kind of variable recognized.
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005507 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005508 }
5509 else
5510 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005511 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005512
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005513 // No specific kind of variable recognized, just a name.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005514 for (idx = 0; reserved[idx] != NULL; ++idx)
5515 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005516 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005517 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005518 goto theend;
5519 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005520
Bram Moolenaar709664c2020-12-12 14:33:41 +01005521
5522 if (lookup_local(var_start, varlen, &local_lvar, cctx) == OK)
5523 lvar = &local_lvar;
5524 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005525 {
5526 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005527 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005528 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5529 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005530 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005531 if (is_decl)
5532 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005533 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005534 goto theend;
5535 }
5536 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005537 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005538 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005539 if (lvar != NULL)
5540 {
5541 if (is_decl)
5542 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005543 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005544 goto theend;
5545 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005546 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005547 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005548 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005549 int script_namespace = varlen > 1
5550 && STRNCMP(var_start, "s:", 2) == 0;
5551 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005552 ? script_var_exists(var_start + 2, varlen - 2,
5553 FALSE, cctx)
5554 : script_var_exists(var_start, varlen,
5555 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005556 imported_T *import =
5557 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005558
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005559 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005560 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005561 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5562
5563 if (is_decl)
5564 {
5565 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005566 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005567 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005568 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005569 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005570 name);
5571 goto theend;
5572 }
5573 else if (cctx->ctx_ufunc->uf_script_ctx_version
5574 == SCRIPT_VERSION_VIM9
5575 && script_namespace
5576 && !script_var && import == NULL)
5577 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005578 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005579 goto theend;
5580 }
5581
5582 dest = dest_script;
5583
5584 // existing script-local variables should have a type
5585 scriptvar_sid = current_sctx.sc_sid;
5586 if (import != NULL)
5587 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005588 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005589 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005590 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005591 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005592 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005593 {
5594 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5595 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005596 ((svar_T *)si->sn_var_vals.ga_data)
5597 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005598 type = sv->sv_type;
5599 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005600 }
5601 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005602 else if (check_defined(var_start, varlen, cctx) == FAIL)
5603 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005604 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005605 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005606
5607 if (declare_error)
5608 {
5609 vim9_declare_error(name);
5610 goto theend;
5611 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005612 }
5613
5614 // handle "a:name" as a name, not index "name" on "a"
5615 if (varlen > 1 || var_start[varlen] != ':')
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005616 var_end = dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005617
5618 if (dest != dest_option)
5619 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005620 if (is_decl && *var_end == ':')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005621 {
5622 // parse optional type: "let var: type = expr"
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005623 if (!VIM_ISWHITE(var_end[1]))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005624 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005625 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005626 goto theend;
5627 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005628 p = skipwhite(var_end + 1);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005629 type = parse_type(&p, cctx->ctx_type_list);
5630 has_type = TRUE;
5631 }
5632 else if (lvar != NULL)
5633 type = lvar->lv_type;
5634 }
5635
5636 if (oplen == 3 && !heredoc && dest != dest_global
5637 && type->tt_type != VAR_STRING
5638 && type->tt_type != VAR_ANY)
5639 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005640 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005641 goto theend;
5642 }
5643
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005644 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005645 {
5646 if (oplen > 1 && !heredoc)
5647 {
5648 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005649 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005650 goto theend;
5651 }
5652
5653 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005654 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5655 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005656 goto theend;
5657 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005658 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005659 if (lvar == NULL)
5660 goto theend;
5661 new_local = TRUE;
5662 }
5663
5664 member_type = type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005665 if (dest_end > var_start + varlen)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005666 {
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005667 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005668 // TODO: should we also handle "->func()" here?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005669 if (is_decl)
5670 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005671 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005672 goto theend;
5673 }
5674
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005675 if (var_start[varlen] == '[' || var_start[varlen] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005676 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005677 char_u *after = var_start + varlen;
5678
5679 // Only the last index is used below, if there are others
5680 // before it generate code for the expression. Thus for
5681 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5682 for (;;)
5683 {
5684 p = skip_index(after);
5685 if (*p != '[' && *p != '.')
5686 break;
5687 after = p;
5688 }
5689 if (after > var_start + varlen)
5690 {
5691 varlen = after - var_start;
5692 dest = dest_expr;
5693 // We don't know the type before evaluating the expression,
5694 // use "any" until then.
5695 type = &t_any;
5696 }
5697
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005698 has_index = TRUE;
5699 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005700 member_type = &t_any;
5701 else
5702 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005703 }
5704 else
5705 {
5706 semsg("Not supported yet: %s", var_start);
5707 goto theend;
5708 }
5709 }
5710 else if (lvar == &arg_lvar)
5711 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005712 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005713 goto theend;
5714 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005715 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5716 {
5717 semsg(_(e_cannot_assign_to_constant), name);
5718 goto theend;
5719 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005720
5721 if (!heredoc)
5722 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005723 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005724 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005725 if (oplen > 0 && var_count == 0)
5726 {
5727 // skip over the "=" and the expression
5728 p = skipwhite(op + oplen);
5729 compile_expr0(&p, cctx);
5730 }
5731 }
5732 else if (oplen > 0)
5733 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005734 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005735 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005736
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005737 // For "var = expr" evaluate the expression.
5738 if (var_count == 0)
5739 {
5740 int r;
5741
5742 // for "+=", "*=", "..=" etc. first load the current value
5743 if (*op != '=')
5744 {
5745 generate_loadvar(cctx, dest, name, lvar, type);
5746
5747 if (has_index)
5748 {
5749 // TODO: get member from list or dict
5750 emsg("Index with operation not supported yet");
5751 goto theend;
5752 }
5753 }
5754
5755 // Compile the expression. Temporarily hide the new local
5756 // variable here, it is not available to this expression.
5757 if (new_local)
5758 --cctx->ctx_locals.ga_len;
5759 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005760 wp = op + oplen;
5761 p = skipwhite(wp);
5762 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01005763 {
5764 if (new_local)
5765 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005766 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01005767 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005768 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005769 if (new_local)
5770 ++cctx->ctx_locals.ga_len;
5771 if (r == FAIL)
5772 goto theend;
5773 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005774 else if (semicolon && var_idx == var_count - 1)
5775 {
5776 // For "[var; var] = expr" get the rest of the list
5777 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5778 goto theend;
5779 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005780 else
5781 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005782 // For "[var, var] = expr" get the "var_idx" item from the
5783 // list.
5784 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01005785 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005786 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005787
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005788 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005789 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005790 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005791 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005792 if ((rhs_type->tt_type == VAR_FUNC
5793 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar0f769812020-09-12 18:32:34 +02005794 && var_wrong_func_name(name, TRUE))
5795 goto theend;
5796
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005797 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005798 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005799 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005800 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005801 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005802 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005803 }
5804 else
5805 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005806 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005807 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005808 if (rhs_type == &t_list_empty)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005809 lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005810 else if (rhs_type == &t_dict_empty)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005811 lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005812 else if (rhs_type == &t_unknown)
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005813 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005814 else
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005815 lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005816 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005817 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005818 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005819 {
5820 type_T *use_type = lvar->lv_type;
5821
Bram Moolenaar71abe482020-09-14 22:28:30 +02005822 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005823 if (has_index)
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005824 use_type = member_type;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005825 if (need_type(rhs_type, use_type, -1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005826 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005827 goto theend;
5828 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005829 }
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005830 else if (*p != '=' && need_type(rhs_type, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005831 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005832 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005833 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005834 else if (cmdidx == CMD_final)
5835 {
5836 emsg(_(e_final_requires_a_value));
5837 goto theend;
5838 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005839 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005840 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005841 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005842 goto theend;
5843 }
5844 else if (!has_type || dest == dest_option)
5845 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005846 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005847 goto theend;
5848 }
5849 else
5850 {
5851 // variables are always initialized
5852 if (ga_grow(instr, 1) == FAIL)
5853 goto theend;
5854 switch (member_type->tt_type)
5855 {
5856 case VAR_BOOL:
5857 generate_PUSHBOOL(cctx, VVAL_FALSE);
5858 break;
5859 case VAR_FLOAT:
5860#ifdef FEAT_FLOAT
5861 generate_PUSHF(cctx, 0.0);
5862#endif
5863 break;
5864 case VAR_STRING:
5865 generate_PUSHS(cctx, NULL);
5866 break;
5867 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005868 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005869 break;
5870 case VAR_FUNC:
5871 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5872 break;
5873 case VAR_LIST:
5874 generate_NEWLIST(cctx, 0);
5875 break;
5876 case VAR_DICT:
5877 generate_NEWDICT(cctx, 0);
5878 break;
5879 case VAR_JOB:
5880 generate_PUSHJOB(cctx, NULL);
5881 break;
5882 case VAR_CHANNEL:
5883 generate_PUSHCHANNEL(cctx, NULL);
5884 break;
5885 case VAR_NUMBER:
5886 case VAR_UNKNOWN:
5887 case VAR_ANY:
5888 case VAR_PARTIAL:
5889 case VAR_VOID:
5890 case VAR_SPECIAL: // cannot happen
5891 generate_PUSHNR(cctx, 0);
5892 break;
5893 }
5894 }
5895 if (var_count == 0)
5896 end = p;
5897 }
5898
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005899 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005900 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005901 break;
5902
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005903 if (oplen > 0 && *op != '=')
5904 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005905 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005906 type_T *stacktype;
5907
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005908 if (*op == '.')
5909 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005910 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005911 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005912 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005913 if (
5914#ifdef FEAT_FLOAT
5915 // If variable is float operation with number is OK.
5916 !(expected == &t_float && stacktype == &t_number) &&
5917#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005918 need_type(stacktype, expected, -1, cctx,
5919 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005920 goto theend;
5921
5922 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005923 {
5924 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5925 goto theend;
5926 }
5927 else if (*op == '+')
5928 {
5929 if (generate_add_instr(cctx,
5930 operator_type(member_type, stacktype),
5931 member_type, stacktype) == FAIL)
5932 goto theend;
5933 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005934 else if (generate_two_op(cctx, op) == FAIL)
5935 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005936 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005937
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005938 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005939 {
Bram Moolenaard24602f2020-12-20 15:20:56 +01005940 int r;
5941 vartype_T dest_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005942
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005943 // Compile the "idx" in "var[idx]" or "key" in "var.key".
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005944 p = var_start + varlen;
5945 if (*p == '[')
5946 {
5947 p = skipwhite(p + 1);
5948 r = compile_expr0(&p, cctx);
5949 if (r == OK && *skipwhite(p) != ']')
5950 {
5951 // this should not happen
5952 emsg(_(e_missbrac));
5953 r = FAIL;
5954 }
5955 }
5956 else // if (*p == '.')
5957 {
5958 char_u *key_end = to_name_end(p + 1, TRUE);
5959 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5960
5961 r = generate_PUSHS(cctx, key);
5962 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005963 if (r == FAIL)
5964 goto theend;
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005965
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005966 if (type == &t_any)
5967 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005968 // Index on variable of unknown type: check at runtime.
5969 dest_type = VAR_ANY;
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005970 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005971 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005972 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005973 dest_type = type->tt_type;
5974 if (dest_type == VAR_DICT
5975 && may_generate_2STRING(-1, cctx) == FAIL)
5976 goto theend;
5977 if (dest_type == VAR_LIST
5978 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
5979 != VAR_NUMBER)
5980 {
5981 emsg(_(e_number_exp));
5982 goto theend;
5983 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005984 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005985
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005986 // Load the dict or list. On the stack we then have:
5987 // - value
5988 // - index
5989 // - variable
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005990 if (dest == dest_expr)
5991 {
5992 int c = var_start[varlen];
5993
5994 // Evaluate "ll[expr]" of "ll[expr][idx]"
5995 p = var_start;
5996 var_start[varlen] = NUL;
5997 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
5998 {
5999 // this should not happen
6000 emsg(_(e_missbrac));
6001 goto theend;
6002 }
6003 var_start[varlen] = c;
6004
6005 type = stack->ga_len == 0 ? &t_void
6006 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6007 // now we can properly check the type
6008 if (type->tt_member != NULL
6009 && need_type(rhs_type, type->tt_member, -2, cctx,
6010 FALSE, FALSE) == FAIL)
6011 goto theend;
6012 }
6013 else
6014 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006015
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01006016 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6017 || dest_type == VAR_ANY)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006018 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01006019 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
6020
6021 if (isn == NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006022 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01006023 isn->isn_arg.vartype = dest_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006024 }
6025 else
6026 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006027 emsg(_(e_indexable_type_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006028 goto theend;
6029 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006030 }
6031 else
6032 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006033 if (is_decl && cmdidx == CMD_const
6034 && (dest == dest_script || dest == dest_local))
6035 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006036 generate_LOCKCONST(cctx);
6037
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006038 if (dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006039 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006040 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
6041 scriptvar_idx, scriptvar_sid, type, name) == FAIL)
6042 goto theend;
6043 }
6044 else if (lvar != NULL)
6045 {
6046 isn_T *isn = ((isn_T *)instr->ga_data)
6047 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006048
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006049 // optimization: turn "var = 123" from ISN_PUSHNR +
6050 // ISN_STORE into ISN_STORENR
6051 if (!lvar->lv_from_outer
6052 && instr->ga_len == instr_count + 1
6053 && isn->isn_type == ISN_PUSHNR)
6054 {
6055 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006056
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006057 isn->isn_type = ISN_STORENR;
6058 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
6059 isn->isn_arg.storenr.stnr_val = val;
6060 if (stack->ga_len > 0)
6061 --stack->ga_len;
6062 }
6063 else if (lvar->lv_from_outer)
6064 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, NULL);
6065 else
6066 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006067 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006068 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006069
6070 if (var_idx + 1 < var_count)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006071 var_start = skipwhite(dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006072 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006073
6074 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006075 if (var_count > 0 && !semicolon)
6076 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006077 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006078 goto theend;
6079 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006080
Bram Moolenaarb2097502020-07-19 17:17:02 +02006081 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006082
6083theend:
6084 vim_free(name);
6085 return ret;
6086}
6087
6088/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006089 * Check if "name" can be "unlet".
6090 */
6091 int
6092check_vim9_unlet(char_u *name)
6093{
6094 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6095 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006096 // "unlet s:var" is allowed in legacy script.
6097 if (*name == 's' && !script_is_vim9())
6098 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006099 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006100 return FAIL;
6101 }
6102 return OK;
6103}
6104
6105/*
6106 * Callback passed to ex_unletlock().
6107 */
6108 static int
6109compile_unlet(
6110 lval_T *lvp,
6111 char_u *name_end,
6112 exarg_T *eap,
6113 int deep UNUSED,
6114 void *coookie)
6115{
6116 cctx_T *cctx = coookie;
6117
6118 if (lvp->ll_tv == NULL)
6119 {
6120 char_u *p = lvp->ll_name;
6121 int cc = *name_end;
6122 int ret = OK;
6123
6124 // Normal name. Only supports g:, w:, t: and b: namespaces.
6125 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006126 if (*p == '$')
6127 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6128 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006129 ret = FAIL;
6130 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006131 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006132
6133 *name_end = cc;
6134 return ret;
6135 }
6136
6137 // TODO: unlet {list}[idx]
6138 // TODO: unlet {dict}[key]
6139 emsg("Sorry, :unlet not fully implemented yet");
6140 return FAIL;
6141}
6142
6143/*
6144 * compile "unlet var", "lock var" and "unlock var"
6145 * "arg" points to "var".
6146 */
6147 static char_u *
6148compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6149{
6150 char_u *p = arg;
6151
6152 if (eap->cmdidx != CMD_unlet)
6153 {
6154 emsg("Sorry, :lock and unlock not implemented yet");
6155 return NULL;
6156 }
6157
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006158 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
6159 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6160}
6161
6162/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006163 * Compile an :import command.
6164 */
6165 static char_u *
6166compile_import(char_u *arg, cctx_T *cctx)
6167{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006168 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006169}
6170
6171/*
6172 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6173 */
6174 static int
6175compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6176{
6177 garray_T *instr = &cctx->ctx_instr;
6178 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6179
6180 if (endlabel == NULL)
6181 return FAIL;
6182 endlabel->el_next = *el;
6183 *el = endlabel;
6184 endlabel->el_end_label = instr->ga_len;
6185
6186 generate_JUMP(cctx, when, 0);
6187 return OK;
6188}
6189
6190 static void
6191compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
6192{
6193 garray_T *instr = &cctx->ctx_instr;
6194
6195 while (*el != NULL)
6196 {
6197 endlabel_T *cur = (*el);
6198 isn_T *isn;
6199
6200 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
6201 isn->isn_arg.jump.jump_where = instr->ga_len;
6202 *el = cur->el_next;
6203 vim_free(cur);
6204 }
6205}
6206
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006207 static void
6208compile_free_jump_to_end(endlabel_T **el)
6209{
6210 while (*el != NULL)
6211 {
6212 endlabel_T *cur = (*el);
6213
6214 *el = cur->el_next;
6215 vim_free(cur);
6216 }
6217}
6218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006219/*
6220 * Create a new scope and set up the generic items.
6221 */
6222 static scope_T *
6223new_scope(cctx_T *cctx, scopetype_T type)
6224{
6225 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6226
6227 if (scope == NULL)
6228 return NULL;
6229 scope->se_outer = cctx->ctx_scope;
6230 cctx->ctx_scope = scope;
6231 scope->se_type = type;
6232 scope->se_local_count = cctx->ctx_locals.ga_len;
6233 return scope;
6234}
6235
6236/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006237 * Free the current scope and go back to the outer scope.
6238 */
6239 static void
6240drop_scope(cctx_T *cctx)
6241{
6242 scope_T *scope = cctx->ctx_scope;
6243
6244 if (scope == NULL)
6245 {
6246 iemsg("calling drop_scope() without a scope");
6247 return;
6248 }
6249 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006250 switch (scope->se_type)
6251 {
6252 case IF_SCOPE:
6253 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6254 case FOR_SCOPE:
6255 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6256 case WHILE_SCOPE:
6257 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6258 case TRY_SCOPE:
6259 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6260 case NO_SCOPE:
6261 case BLOCK_SCOPE:
6262 break;
6263 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006264 vim_free(scope);
6265}
6266
6267/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006268 * compile "if expr"
6269 *
6270 * "if expr" Produces instructions:
6271 * EVAL expr Push result of "expr"
6272 * JUMP_IF_FALSE end
6273 * ... body ...
6274 * end:
6275 *
6276 * "if expr | else" Produces instructions:
6277 * EVAL expr Push result of "expr"
6278 * JUMP_IF_FALSE else
6279 * ... body ...
6280 * JUMP_ALWAYS end
6281 * else:
6282 * ... body ...
6283 * end:
6284 *
6285 * "if expr1 | elseif expr2 | else" Produces instructions:
6286 * EVAL expr Push result of "expr"
6287 * JUMP_IF_FALSE elseif
6288 * ... body ...
6289 * JUMP_ALWAYS end
6290 * elseif:
6291 * EVAL expr Push result of "expr"
6292 * JUMP_IF_FALSE else
6293 * ... body ...
6294 * JUMP_ALWAYS end
6295 * else:
6296 * ... body ...
6297 * end:
6298 */
6299 static char_u *
6300compile_if(char_u *arg, cctx_T *cctx)
6301{
6302 char_u *p = arg;
6303 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006304 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006305 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006306 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006307 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006308
Bram Moolenaara5565e42020-05-09 15:44:01 +02006309 CLEAR_FIELD(ppconst);
6310 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006311 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006312 clear_ppconst(&ppconst);
6313 return NULL;
6314 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006315 if (cctx->ctx_skip == SKIP_YES)
6316 clear_ppconst(&ppconst);
6317 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006318 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006319 int error = FALSE;
6320 int v;
6321
Bram Moolenaara5565e42020-05-09 15:44:01 +02006322 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006323 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006324 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006325 if (error)
6326 return NULL;
6327 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006328 }
6329 else
6330 {
6331 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006332 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006333 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006334 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006335 if (bool_on_stack(cctx) == FAIL)
6336 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006337 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006338
6339 scope = new_scope(cctx, IF_SCOPE);
6340 if (scope == NULL)
6341 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006342 scope->se_skip_save = skip_save;
6343 // "is_had_return" will be reset if any block does not end in :return
6344 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006345
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006346 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006347 {
6348 // "where" is set when ":elseif", "else" or ":endif" is found
6349 scope->se_u.se_if.is_if_label = instr->ga_len;
6350 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6351 }
6352 else
6353 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006354
6355 return p;
6356}
6357
6358 static char_u *
6359compile_elseif(char_u *arg, cctx_T *cctx)
6360{
6361 char_u *p = arg;
6362 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006363 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006364 isn_T *isn;
6365 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006366 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006367 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006368
6369 if (scope == NULL || scope->se_type != IF_SCOPE)
6370 {
6371 emsg(_(e_elseif_without_if));
6372 return NULL;
6373 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006374 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006375 if (!cctx->ctx_had_return)
6376 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006377
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006378 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006379 {
6380 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006381 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006382 return NULL;
6383 // previous "if" or "elseif" jumps here
6384 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6385 isn->isn_arg.jump.jump_where = instr->ga_len;
6386 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006387
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006388 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006389 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006390 if (cctx->ctx_skip == SKIP_YES)
6391 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006392 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006393 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006394 clear_ppconst(&ppconst);
6395 return NULL;
6396 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006397 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006398 if (scope->se_skip_save == SKIP_YES)
6399 clear_ppconst(&ppconst);
6400 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006401 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006402 int error = FALSE;
6403 int v;
6404
Bram Moolenaar7f141552020-05-09 17:35:53 +02006405 // The expression results in a constant.
6406 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006407 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6408 if (error)
6409 return NULL;
6410 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006411 clear_ppconst(&ppconst);
6412 scope->se_u.se_if.is_if_label = -1;
6413 }
6414 else
6415 {
6416 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006417 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006418 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006419 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006420 if (bool_on_stack(cctx) == FAIL)
6421 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006422
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006423 // "where" is set when ":elseif", "else" or ":endif" is found
6424 scope->se_u.se_if.is_if_label = instr->ga_len;
6425 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6426 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006427
6428 return p;
6429}
6430
6431 static char_u *
6432compile_else(char_u *arg, cctx_T *cctx)
6433{
6434 char_u *p = arg;
6435 garray_T *instr = &cctx->ctx_instr;
6436 isn_T *isn;
6437 scope_T *scope = cctx->ctx_scope;
6438
6439 if (scope == NULL || scope->se_type != IF_SCOPE)
6440 {
6441 emsg(_(e_else_without_if));
6442 return NULL;
6443 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006444 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006445 if (!cctx->ctx_had_return)
6446 scope->se_u.se_if.is_had_return = FALSE;
6447 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006448
Bram Moolenaarefd88552020-06-18 20:50:10 +02006449 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006450 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006451 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006452 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006453 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006454 if (!cctx->ctx_had_return
6455 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6456 JUMP_ALWAYS, cctx) == FAIL)
6457 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006458 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006459
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006460 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006461 {
6462 if (scope->se_u.se_if.is_if_label >= 0)
6463 {
6464 // previous "if" or "elseif" jumps here
6465 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6466 isn->isn_arg.jump.jump_where = instr->ga_len;
6467 scope->se_u.se_if.is_if_label = -1;
6468 }
6469 }
6470
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006471 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006472 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6473 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006474
6475 return p;
6476}
6477
6478 static char_u *
6479compile_endif(char_u *arg, cctx_T *cctx)
6480{
6481 scope_T *scope = cctx->ctx_scope;
6482 ifscope_T *ifscope;
6483 garray_T *instr = &cctx->ctx_instr;
6484 isn_T *isn;
6485
6486 if (scope == NULL || scope->se_type != IF_SCOPE)
6487 {
6488 emsg(_(e_endif_without_if));
6489 return NULL;
6490 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006491 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006492 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006493 if (!cctx->ctx_had_return)
6494 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006495
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006496 if (scope->se_u.se_if.is_if_label >= 0)
6497 {
6498 // previous "if" or "elseif" jumps here
6499 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6500 isn->isn_arg.jump.jump_where = instr->ga_len;
6501 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006502 // Fill in the "end" label in jumps at the end of the blocks.
6503 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006504 cctx->ctx_skip = scope->se_skip_save;
6505
6506 // If all the blocks end in :return and there is an :else then the
6507 // had_return flag is set.
6508 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006509
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006510 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006511 return arg;
6512}
6513
6514/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01006515 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006516 *
6517 * Produces instructions:
6518 * PUSHNR -1
6519 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01006520 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006521 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6522 * - if beyond end, jump to "end"
6523 * - otherwise get item from list and push it
6524 * STORE var Store item in "var"
6525 * ... body ...
6526 * JUMP top Jump back to repeat
6527 * end: DROP Drop the result of "expr"
6528 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006529 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
6530 * UNPACK 2 Split item in 2
6531 * STORE var1 Store item in "var1"
6532 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006533 */
6534 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006535compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006536{
Bram Moolenaar792f7862020-11-23 08:31:18 +01006537 char_u *arg;
6538 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006539 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006540 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006541 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006542 int var_count = 0;
6543 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006544 size_t varlen;
6545 garray_T *instr = &cctx->ctx_instr;
6546 garray_T *stack = &cctx->ctx_type_stack;
6547 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006548 lvar_T *loop_lvar; // loop iteration variable
6549 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006550 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006551 type_T *item_type = &t_any;
6552 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006553
Bram Moolenaar792f7862020-11-23 08:31:18 +01006554 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
6555 if (var_count == 0)
6556 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006557
6558 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006559 wp = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006560 p = skipwhite(p);
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006561 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6562 return NULL;
6563 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006564 {
6565 emsg(_(e_missing_in));
6566 return NULL;
6567 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006568 wp = p + 2;
6569 p = skipwhite(wp);
6570 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6571 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006572
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006573 scope = new_scope(cctx, FOR_SCOPE);
6574 if (scope == NULL)
6575 return NULL;
6576
Bram Moolenaar792f7862020-11-23 08:31:18 +01006577 // Reserve a variable to store the loop iteration counter and initialize it
6578 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006579 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6580 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006581 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006582 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006583 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006584 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006585 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006586 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006587
6588 // compile "expr", it remains on the stack until "endfor"
6589 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006590 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006591 {
6592 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006593 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006594 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006595 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006596
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006597 // Now that we know the type of "var", check that it is a list, now or at
6598 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006599 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006600 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006601 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006602 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006603 return NULL;
6604 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006605
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006606 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006607 {
6608 if (var_count == 1)
6609 item_type = vartype->tt_member;
6610 else if (vartype->tt_member->tt_type == VAR_LIST
6611 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
6612 item_type = vartype->tt_member->tt_member;
6613 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006614
6615 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006616 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006617 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006618
Bram Moolenaar792f7862020-11-23 08:31:18 +01006619 arg = arg_start;
6620 if (var_count > 1)
6621 {
6622 generate_UNPACK(cctx, var_count, semicolon);
6623 arg = skipwhite(arg + 1); // skip white after '['
6624
6625 // the list item is replaced by a number of items
6626 if (ga_grow(stack, var_count - 1) == FAIL)
6627 {
6628 drop_scope(cctx);
6629 return NULL;
6630 }
6631 --stack->ga_len;
6632 for (idx = 0; idx < var_count; ++idx)
6633 {
6634 ((type_T **)stack->ga_data)[stack->ga_len] =
6635 (semicolon && idx == 0) ? vartype : item_type;
6636 ++stack->ga_len;
6637 }
6638 }
6639
6640 for (idx = 0; idx < var_count; ++idx)
6641 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006642 assign_dest_T dest = dest_local;
6643 int opt_flags = 0;
6644 int vimvaridx = -1;
6645 type_T *type = &t_any;
6646
6647 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006648 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006649 name = vim_strnsave(arg, varlen);
6650 if (name == NULL)
6651 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006652
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006653 // TODO: script var not supported?
6654 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
6655 &vimvaridx, &type, cctx) == FAIL)
6656 goto failed;
6657 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006658 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006659 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
6660 0, 0, type, name) == FAIL)
6661 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006662 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006663 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006664 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01006665 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006666 {
6667 semsg(_(e_variable_already_declared), arg);
6668 goto failed;
6669 }
6670
Bram Moolenaarea870692020-12-02 14:24:30 +01006671 if (STRNCMP(name, "s:", 2) == 0)
6672 {
6673 semsg(_(e_cannot_declare_script_variable_in_function), name);
6674 goto failed;
6675 }
6676
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006677 // Reserve a variable to store "var".
6678 // TODO: check for type
6679 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6680 if (var_lvar == NULL)
6681 // out of memory or used as an argument
6682 goto failed;
6683
6684 if (semicolon && idx == var_count - 1)
6685 var_lvar->lv_type = vartype;
6686 else
6687 var_lvar->lv_type = item_type;
6688 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
6689 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006690
6691 if (*p == ',' || *p == ';')
6692 ++p;
6693 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006694 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006695 }
6696
6697 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006698
6699failed:
6700 vim_free(name);
6701 drop_scope(cctx);
6702 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006703}
6704
6705/*
6706 * compile "endfor"
6707 */
6708 static char_u *
6709compile_endfor(char_u *arg, cctx_T *cctx)
6710{
6711 garray_T *instr = &cctx->ctx_instr;
6712 scope_T *scope = cctx->ctx_scope;
6713 forscope_T *forscope;
6714 isn_T *isn;
6715
6716 if (scope == NULL || scope->se_type != FOR_SCOPE)
6717 {
6718 emsg(_(e_for));
6719 return NULL;
6720 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006721 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006722 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006723 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006724
6725 // At end of ":for" scope jump back to the FOR instruction.
6726 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6727
6728 // Fill in the "end" label in the FOR statement so it can jump here
6729 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6730 isn->isn_arg.forloop.for_end = instr->ga_len;
6731
6732 // Fill in the "end" label any BREAK statements
6733 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6734
6735 // Below the ":for" scope drop the "expr" list from the stack.
6736 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6737 return NULL;
6738
6739 vim_free(scope);
6740
6741 return arg;
6742}
6743
6744/*
6745 * compile "while expr"
6746 *
6747 * Produces instructions:
6748 * top: EVAL expr Push result of "expr"
6749 * JUMP_IF_FALSE end jump if false
6750 * ... body ...
6751 * JUMP top Jump back to repeat
6752 * end:
6753 *
6754 */
6755 static char_u *
6756compile_while(char_u *arg, cctx_T *cctx)
6757{
6758 char_u *p = arg;
6759 garray_T *instr = &cctx->ctx_instr;
6760 scope_T *scope;
6761
6762 scope = new_scope(cctx, WHILE_SCOPE);
6763 if (scope == NULL)
6764 return NULL;
6765
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006766 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006767
6768 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006769 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006770 return NULL;
6771
Bram Moolenaar13106602020-10-04 16:06:05 +02006772 if (bool_on_stack(cctx) == FAIL)
6773 return FAIL;
6774
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006775 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006776 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006777 JUMP_IF_FALSE, cctx) == FAIL)
6778 return FAIL;
6779
6780 return p;
6781}
6782
6783/*
6784 * compile "endwhile"
6785 */
6786 static char_u *
6787compile_endwhile(char_u *arg, cctx_T *cctx)
6788{
6789 scope_T *scope = cctx->ctx_scope;
6790
6791 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6792 {
6793 emsg(_(e_while));
6794 return NULL;
6795 }
6796 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006797 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006798
6799 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006800 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006801
6802 // Fill in the "end" label in the WHILE statement so it can jump here.
6803 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006804 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006805
6806 vim_free(scope);
6807
6808 return arg;
6809}
6810
6811/*
6812 * compile "continue"
6813 */
6814 static char_u *
6815compile_continue(char_u *arg, cctx_T *cctx)
6816{
6817 scope_T *scope = cctx->ctx_scope;
6818
6819 for (;;)
6820 {
6821 if (scope == NULL)
6822 {
6823 emsg(_(e_continue));
6824 return NULL;
6825 }
6826 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6827 break;
6828 scope = scope->se_outer;
6829 }
6830
6831 // Jump back to the FOR or WHILE instruction.
6832 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006833 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6834 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006835 return arg;
6836}
6837
6838/*
6839 * compile "break"
6840 */
6841 static char_u *
6842compile_break(char_u *arg, cctx_T *cctx)
6843{
6844 scope_T *scope = cctx->ctx_scope;
6845 endlabel_T **el;
6846
6847 for (;;)
6848 {
6849 if (scope == NULL)
6850 {
6851 emsg(_(e_break));
6852 return NULL;
6853 }
6854 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6855 break;
6856 scope = scope->se_outer;
6857 }
6858
6859 // Jump to the end of the FOR or WHILE loop.
6860 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006861 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006862 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006863 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006864 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6865 return FAIL;
6866
6867 return arg;
6868}
6869
6870/*
6871 * compile "{" start of block
6872 */
6873 static char_u *
6874compile_block(char_u *arg, cctx_T *cctx)
6875{
6876 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6877 return NULL;
6878 return skipwhite(arg + 1);
6879}
6880
6881/*
6882 * compile end of block: drop one scope
6883 */
6884 static void
6885compile_endblock(cctx_T *cctx)
6886{
6887 scope_T *scope = cctx->ctx_scope;
6888
6889 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006890 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006891 vim_free(scope);
6892}
6893
6894/*
6895 * compile "try"
6896 * Creates a new scope for the try-endtry, pointing to the first catch and
6897 * finally.
6898 * Creates another scope for the "try" block itself.
6899 * TRY instruction sets up exception handling at runtime.
6900 *
6901 * "try"
6902 * TRY -> catch1, -> finally push trystack entry
6903 * ... try block
6904 * "throw {exception}"
6905 * EVAL {exception}
6906 * THROW create exception
6907 * ... try block
6908 * " catch {expr}"
6909 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006910 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006911 * EVAL {expr}
6912 * MATCH
6913 * JUMP nomatch -> catch2
6914 * CATCH remove exception
6915 * ... catch block
6916 * " catch"
6917 * JUMP -> finally
6918 * catch2: CATCH remove exception
6919 * ... catch block
6920 * " finally"
6921 * finally:
6922 * ... finally block
6923 * " endtry"
6924 * ENDTRY pop trystack entry, may rethrow
6925 */
6926 static char_u *
6927compile_try(char_u *arg, cctx_T *cctx)
6928{
6929 garray_T *instr = &cctx->ctx_instr;
6930 scope_T *try_scope;
6931 scope_T *scope;
6932
6933 // scope that holds the jumps that go to catch/finally/endtry
6934 try_scope = new_scope(cctx, TRY_SCOPE);
6935 if (try_scope == NULL)
6936 return NULL;
6937
6938 // "catch" is set when the first ":catch" is found.
6939 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006940 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006941 if (generate_instr(cctx, ISN_TRY) == NULL)
6942 return NULL;
6943
6944 // scope for the try block itself
6945 scope = new_scope(cctx, BLOCK_SCOPE);
6946 if (scope == NULL)
6947 return NULL;
6948
6949 return arg;
6950}
6951
6952/*
6953 * compile "catch {expr}"
6954 */
6955 static char_u *
6956compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6957{
6958 scope_T *scope = cctx->ctx_scope;
6959 garray_T *instr = &cctx->ctx_instr;
6960 char_u *p;
6961 isn_T *isn;
6962
6963 // end block scope from :try or :catch
6964 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6965 compile_endblock(cctx);
6966 scope = cctx->ctx_scope;
6967
6968 // Error if not in a :try scope
6969 if (scope == NULL || scope->se_type != TRY_SCOPE)
6970 {
6971 emsg(_(e_catch));
6972 return NULL;
6973 }
6974
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006975 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006976 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006977 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006978 return NULL;
6979 }
6980
6981 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006982 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006983 JUMP_ALWAYS, cctx) == FAIL)
6984 return NULL;
6985
6986 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006987 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006988 if (isn->isn_arg.try.try_catch == 0)
6989 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006990 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006991 {
6992 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006993 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006994 isn->isn_arg.jump.jump_where = instr->ga_len;
6995 }
6996
6997 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006998 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006999 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007000 scope->se_u.se_try.ts_caught_all = TRUE;
7001 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007002 }
7003 else
7004 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007005 char_u *end;
7006 char_u *pat;
7007 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007008 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007009 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007010
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007011 // Push v:exception, push {expr} and MATCH
7012 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7013
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007014 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007015 if (*end != *p)
7016 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007017 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007018 vim_free(tofree);
7019 return FAIL;
7020 }
7021 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007022 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007023 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007024 len = (int)(end - tofree);
7025 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007026 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007027 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007028 if (pat == NULL)
7029 return FAIL;
7030 if (generate_PUSHS(cctx, pat) == FAIL)
7031 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007032
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007033 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7034 return NULL;
7035
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007036 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007037 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7038 return NULL;
7039 }
7040
7041 if (generate_instr(cctx, ISN_CATCH) == NULL)
7042 return NULL;
7043
7044 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7045 return NULL;
7046 return p;
7047}
7048
7049 static char_u *
7050compile_finally(char_u *arg, cctx_T *cctx)
7051{
7052 scope_T *scope = cctx->ctx_scope;
7053 garray_T *instr = &cctx->ctx_instr;
7054 isn_T *isn;
7055
7056 // end block scope from :try or :catch
7057 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7058 compile_endblock(cctx);
7059 scope = cctx->ctx_scope;
7060
7061 // Error if not in a :try scope
7062 if (scope == NULL || scope->se_type != TRY_SCOPE)
7063 {
7064 emsg(_(e_finally));
7065 return NULL;
7066 }
7067
7068 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007069 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007070 if (isn->isn_arg.try.try_finally != 0)
7071 {
7072 emsg(_(e_finally_dup));
7073 return NULL;
7074 }
7075
7076 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007077 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007078
Bram Moolenaar585fea72020-04-02 22:33:21 +02007079 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007080 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007081 {
7082 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007083 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007084 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007085 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007086 }
7087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007088 // TODO: set index in ts_finally_label jumps
7089
7090 return arg;
7091}
7092
7093 static char_u *
7094compile_endtry(char_u *arg, cctx_T *cctx)
7095{
7096 scope_T *scope = cctx->ctx_scope;
7097 garray_T *instr = &cctx->ctx_instr;
7098 isn_T *isn;
7099
7100 // end block scope from :catch or :finally
7101 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7102 compile_endblock(cctx);
7103 scope = cctx->ctx_scope;
7104
7105 // Error if not in a :try scope
7106 if (scope == NULL || scope->se_type != TRY_SCOPE)
7107 {
7108 if (scope == NULL)
7109 emsg(_(e_no_endtry));
7110 else if (scope->se_type == WHILE_SCOPE)
7111 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007112 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007113 emsg(_(e_endfor));
7114 else
7115 emsg(_(e_endif));
7116 return NULL;
7117 }
7118
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007119 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007120 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
7121 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007122 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007123 return NULL;
7124 }
7125
7126 // Fill in the "end" label in jumps at the end of the blocks, if not done
7127 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007128 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007129
7130 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02007131 if (isn->isn_arg.try.try_catch == 0)
7132 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007133 if (isn->isn_arg.try.try_finally == 0)
7134 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007135
7136 if (scope->se_u.se_try.ts_catch_label != 0)
7137 {
7138 // Last catch without match jumps here
7139 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7140 isn->isn_arg.jump.jump_where = instr->ga_len;
7141 }
7142
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007143 compile_endblock(cctx);
7144
7145 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
7146 return NULL;
7147 return arg;
7148}
7149
7150/*
7151 * compile "throw {expr}"
7152 */
7153 static char_u *
7154compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7155{
7156 char_u *p = skipwhite(arg);
7157
Bram Moolenaara5565e42020-05-09 15:44:01 +02007158 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007159 return NULL;
7160 if (may_generate_2STRING(-1, cctx) == FAIL)
7161 return NULL;
7162 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7163 return NULL;
7164
7165 return p;
7166}
7167
7168/*
7169 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007170 * compile "echomsg expr"
7171 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007172 * compile "execute expr"
7173 */
7174 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007175compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007176{
7177 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007178 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007179 int count = 0;
7180
7181 for (;;)
7182 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007183 if (ends_excmd2(prev, p))
7184 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007185 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007186 return NULL;
7187 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007188 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007189 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007190 }
7191
Bram Moolenaare4984292020-12-13 14:19:25 +01007192 if (count > 0)
7193 {
7194 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7195 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7196 else if (cmdidx == CMD_execute)
7197 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7198 else if (cmdidx == CMD_echomsg)
7199 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7200 else
7201 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7202 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007203 return p;
7204}
7205
7206/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007207 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01007208 * instruction to compute it and return OK.
7209 * Otherwise return FAIL, the caller must deal with any range.
7210 */
7211 static int
7212compile_variable_range(exarg_T *eap, cctx_T *cctx)
7213{
7214 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7215 char_u *p = skipdigits(eap->cmd);
7216
7217 if (p == range_end)
7218 return FAIL;
7219 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7220}
7221
7222/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007223 * :put r
7224 * :put ={expr}
7225 */
7226 static char_u *
7227compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7228{
7229 char_u *line = arg;
7230 linenr_T lnum;
7231 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007232 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007233
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007234 eap->regname = *line;
7235
7236 if (eap->regname == '=')
7237 {
7238 char_u *p = line + 1;
7239
7240 if (compile_expr0(&p, cctx) == FAIL)
7241 return NULL;
7242 line = p;
7243 }
7244 else if (eap->regname != NUL)
7245 ++line;
7246
Bram Moolenaar08597872020-12-10 19:43:40 +01007247 if (compile_variable_range(eap, cctx) == OK)
7248 {
7249 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
7250 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007251 else
Bram Moolenaar08597872020-12-10 19:43:40 +01007252 {
7253 // Either no range or a number.
7254 // "errormsg" will not be set because the range is ADDR_LINES.
7255 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01007256 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01007257 return NULL;
7258 if (eap->addr_count == 0)
7259 lnum = -1;
7260 else
7261 lnum = eap->line2;
7262 if (above)
7263 --lnum;
7264 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007265
7266 generate_PUT(cctx, eap->regname, lnum);
7267 return line;
7268}
7269
7270/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007271 * A command that is not compiled, execute with legacy code.
7272 */
7273 static char_u *
7274compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7275{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007276 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007277 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007278 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007279
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007280 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007281 goto theend;
7282
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007283 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007284 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007285 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007286 int usefilter = FALSE;
7287
7288 has_expr = argt & (EX_XFILE | EX_EXPAND);
7289
7290 // If the command can be followed by a bar, find the bar and truncate
7291 // it, so that the following command can be compiled.
7292 // The '|' is overwritten with a NUL, it is put back below.
7293 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7294 && *eap->arg == '!')
7295 // :w !filter or :r !filter or :r! filter
7296 usefilter = TRUE;
7297 if ((argt & EX_TRLBAR) && !usefilter)
7298 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007299 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007300 separate_nextcmd(eap);
7301 if (eap->nextcmd != NULL)
7302 nextcmd = eap->nextcmd;
7303 }
7304 }
7305
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007306 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
7307 {
7308 // expand filename in "syntax include [@group] filename"
7309 has_expr = TRUE;
7310 eap->arg = skipwhite(eap->arg + 7);
7311 if (*eap->arg == '@')
7312 eap->arg = skiptowhite(eap->arg);
7313 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007314
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007315 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007316 {
7317 int count = 0;
7318 char_u *start = skipwhite(line);
7319
7320 // :cmd xxx`=expr1`yyy`=expr2`zzz
7321 // PUSHS ":cmd xxx"
7322 // eval expr1
7323 // PUSHS "yyy"
7324 // eval expr2
7325 // PUSHS "zzz"
7326 // EXECCONCAT 5
7327 for (;;)
7328 {
7329 if (p > start)
7330 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007331 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007332 ++count;
7333 }
7334 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007335 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007336 return NULL;
7337 may_generate_2STRING(-1, cctx);
7338 ++count;
7339 p = skipwhite(p);
7340 if (*p != '`')
7341 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007342 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007343 return NULL;
7344 }
7345 start = p + 1;
7346
7347 p = (char_u *)strstr((char *)start, "`=");
7348 if (p == NULL)
7349 {
7350 if (*skipwhite(start) != NUL)
7351 {
7352 generate_PUSHS(cctx, vim_strsave(start));
7353 ++count;
7354 }
7355 break;
7356 }
7357 }
7358 generate_EXECCONCAT(cctx, count);
7359 }
7360 else
7361 generate_EXEC(cctx, line);
7362
7363theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007364 if (*nextcmd != NUL)
7365 {
7366 // the parser expects a pointer to the bar, put it back
7367 --nextcmd;
7368 *nextcmd = '|';
7369 }
7370
7371 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007372}
7373
7374/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007375 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007376 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007377 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007378 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007379add_def_function(ufunc_T *ufunc)
7380{
7381 dfunc_T *dfunc;
7382
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007383 if (def_functions.ga_len == 0)
7384 {
7385 // The first position is not used, so that a zero uf_dfunc_idx means it
7386 // wasn't set.
7387 if (ga_grow(&def_functions, 1) == FAIL)
7388 return FAIL;
7389 ++def_functions.ga_len;
7390 }
7391
Bram Moolenaar09689a02020-05-09 22:50:08 +02007392 // Add the function to "def_functions".
7393 if (ga_grow(&def_functions, 1) == FAIL)
7394 return FAIL;
7395 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7396 CLEAR_POINTER(dfunc);
7397 dfunc->df_idx = def_functions.ga_len;
7398 ufunc->uf_dfunc_idx = dfunc->df_idx;
7399 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01007400 dfunc->df_name = vim_strsave(ufunc->uf_name);
7401 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007402 ++def_functions.ga_len;
7403 return OK;
7404}
7405
7406/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007407 * After ex_function() has collected all the function lines: parse and compile
7408 * the lines into instructions.
7409 * Adds the function to "def_functions".
7410 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
7411 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007412 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007413 * This can be used recursively through compile_lambda(), which may reallocate
7414 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007415 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007416 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007417 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007418compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007419{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007420 char_u *line = NULL;
7421 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007422 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007423 cctx_T cctx;
7424 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007425 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007426 int ret = FAIL;
7427 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007428 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007429 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007430 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007431
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007432 // When using a function that was compiled before: Free old instructions.
7433 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007434 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007435 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007436 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7437 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007438 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007439 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007440 else
7441 {
7442 if (add_def_function(ufunc) == FAIL)
7443 return FAIL;
7444 new_def_function = TRUE;
7445 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007446
Bram Moolenaar985116a2020-07-12 17:31:09 +02007447 ufunc->uf_def_status = UF_COMPILING;
7448
Bram Moolenaara80faa82020-04-12 19:37:17 +02007449 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007450 cctx.ctx_ufunc = ufunc;
7451 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007452 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007453 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7454 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7455 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7456 cctx.ctx_type_list = &ufunc->uf_type_list;
7457 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7458 instr = &cctx.ctx_instr;
7459
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007460 // Set the context to the function, it may be compiled when called from
7461 // another script. Set the script version to the most modern one.
7462 // The line number will be set in next_line_from_context().
7463 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007464 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7465
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007466 // Make sure error messages are OK.
7467 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7468 if (do_estack_push)
7469 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007470 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007471
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007472 if (ufunc->uf_def_args.ga_len > 0)
7473 {
7474 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007475 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007476 int i;
7477 char_u *arg;
7478 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007479 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007480
7481 // Produce instructions for the default values of optional arguments.
7482 // Store the instruction index in uf_def_arg_idx[] so that we know
7483 // where to start when the function is called, depending on the number
7484 // of arguments.
7485 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7486 if (ufunc->uf_def_arg_idx == NULL)
7487 goto erret;
7488 for (i = 0; i < count; ++i)
7489 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007490 garray_T *stack = &cctx.ctx_type_stack;
7491 type_T *val_type;
7492 int arg_idx = first_def_arg + i;
7493
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007494 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7495 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007496 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007497 goto erret;
7498
7499 // If no type specified use the type of the default value.
7500 // Otherwise check that the default value type matches the
7501 // specified type.
7502 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7503 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007504 {
7505 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007506 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007507 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007508 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7509 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007510 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007511
7512 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007513 goto erret;
7514 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007515 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007516
7517 if (did_set_arg_type)
7518 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007519 }
7520
7521 /*
7522 * Loop over all the lines of the function and generate instructions.
7523 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007524 for (;;)
7525 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007526 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007527 int starts_with_colon = FALSE;
7528 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007529 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007530
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007531 // Bail out on the first error to avoid a flood of errors and report
7532 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007533 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007534 goto erret;
7535
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007536 if (line != NULL && *line == '|')
7537 // the line continues after a '|'
7538 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007539 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007540 && !(*line == '#' && (line == cctx.ctx_line_start
7541 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007542 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007543 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007544 goto erret;
7545 }
7546 else
7547 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007548 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007549 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007550 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007551 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007552 }
7553
Bram Moolenaara80faa82020-04-12 19:37:17 +02007554 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007555 ea.cmdlinep = &line;
7556 ea.cmd = skipwhite(line);
7557
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007558 // Some things can be recognized by the first character.
7559 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007560 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007561 case '#':
Bram Moolenaare0de1712020-12-02 17:36:54 +01007562 // "#" starts a comment
7563 line = (char_u *)"";
7564 continue;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007565
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007566 case '}':
7567 {
7568 // "}" ends a block scope
7569 scopetype_T stype = cctx.ctx_scope == NULL
7570 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007571
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007572 if (stype == BLOCK_SCOPE)
7573 {
7574 compile_endblock(&cctx);
7575 line = ea.cmd;
7576 }
7577 else
7578 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007579 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007580 goto erret;
7581 }
7582 if (line != NULL)
7583 line = skipwhite(ea.cmd + 1);
7584 continue;
7585 }
7586
7587 case '{':
7588 // "{" starts a block scope
7589 // "{'a': 1}->func() is something else
7590 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7591 {
7592 line = compile_block(ea.cmd, &cctx);
7593 continue;
7594 }
7595 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007596 }
7597
7598 /*
7599 * COMMAND MODIFIERS
7600 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007601 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007602 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7603 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007604 {
7605 if (errormsg != NULL)
7606 goto erret;
7607 // empty line or comment
7608 line = (char_u *)"";
7609 continue;
7610 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007611 generate_cmdmods(&cctx, &local_cmdmod);
7612 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007613
Bram Moolenaare88c8e82020-11-01 17:03:37 +01007614 // Check if there was a colon after the last command modifier or before
7615 // the current position.
7616 for (p = ea.cmd; p >= line; --p)
7617 {
7618 if (*p == ':')
7619 starts_with_colon = TRUE;
7620 if (p < ea.cmd && !VIM_ISWHITE(*p))
7621 break;
7622 }
7623
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007624 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007625 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007626 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007627 {
7628 if (*ea.cmd == '(')
7629 // not for "call()"
7630 ea.cmd = p;
7631 else
7632 ea.cmd = skipwhite(ea.cmd);
7633 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007634
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007635 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007636 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007637 char_u *pskip;
7638
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007639 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007640 // find what follows.
7641 // Skip over "var.member", "var[idx]" and the like.
7642 // Also "&opt = val", "$ENV = val" and "@r = val".
7643 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007644 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007645 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007646 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007647 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007648 char_u *var_end;
7649 int oplen;
7650 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007651
Bram Moolenaar65821722020-08-02 18:58:54 +02007652 if (ea.cmd[0] == '@')
7653 var_end = ea.cmd + 2;
7654 else
7655 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007656 FNE_CHECK_START | FNE_INCL_BR);
7657 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007658 if (oplen > 0)
7659 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007660 size_t len = p - ea.cmd;
7661
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007662 // Recognize an assignment if we recognize the variable
7663 // name:
7664 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007665 // "local = expr" where "local" is a local var.
7666 // "script = expr" where "script" is a script-local var.
7667 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007668 // "&opt = expr"
7669 // "$ENV = expr"
7670 // "@r = expr"
7671 if (*ea.cmd == '&'
7672 || *ea.cmd == '$'
7673 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007674 || ((len) > 2 && ea.cmd[1] == ':')
Bram Moolenaar709664c2020-12-12 14:33:41 +01007675 || lookup_local(ea.cmd, len, NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007676 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007677 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007678 || script_var_exists(ea.cmd, len,
7679 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007680 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007681 {
7682 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007683 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007684 goto erret;
Bram Moolenaarf665e972020-12-05 19:17:16 +01007685 goto nextline;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007686 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007687 }
7688 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007689
7690 if (*ea.cmd == '[')
7691 {
7692 // [var, var] = expr
7693 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7694 if (line == NULL)
7695 goto erret;
7696 if (line != ea.cmd)
Bram Moolenaarf665e972020-12-05 19:17:16 +01007697 goto nextline;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007698 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007699 }
7700
7701 /*
7702 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007703 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007704 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007705 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007706 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007707 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007708 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007709 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007710 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007711 if (!starts_with_colon)
7712 {
7713 emsg(_(e_colon_required_before_a_range));
7714 goto erret;
7715 }
7716 if (ends_excmd2(line, ea.cmd))
7717 {
7718 // A range without a command: jump to the line.
7719 // TODO: compile to a more efficient command, possibly
7720 // calling parse_cmd_address().
7721 ea.cmdidx = CMD_SIZE;
7722 line = compile_exec(line, &ea, &cctx);
7723 goto nextline;
7724 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007725 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007726 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007727 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +01007728 : (int (*)(char_u *, size_t, void *, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007729 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007730
7731 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7732 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007733 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007734 {
7735 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01007736 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007737 }
7738
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007739 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007740 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007741 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01007742 // CMD_var cannot happen, compile_assignment() above would be
7743 // used. Most likely an assignment to a non-existing variable.
7744 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007745 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007746 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007747 }
7748
Bram Moolenaar3988f642020-08-27 22:43:03 +02007749 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007750 && ea.cmdidx != CMD_elseif
7751 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007752 && ea.cmdidx != CMD_endif
7753 && ea.cmdidx != CMD_endfor
7754 && ea.cmdidx != CMD_endwhile
7755 && ea.cmdidx != CMD_catch
7756 && ea.cmdidx != CMD_finally
7757 && ea.cmdidx != CMD_endtry)
7758 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007759 emsg(_(e_unreachable_code_after_return));
7760 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007761 }
7762
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007763 p = skipwhite(p);
7764 if (ea.cmdidx != CMD_SIZE
7765 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7766 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007767 if (ea.cmdidx >= 0)
7768 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007769 if ((ea.argt & EX_BANG) && *p == '!')
7770 {
7771 ea.forceit = TRUE;
7772 p = skipwhite(p + 1);
7773 }
7774 }
7775
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007776 switch (ea.cmdidx)
7777 {
7778 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007779 ea.arg = p;
7780 line = compile_nested_function(&ea, &cctx);
7781 break;
7782
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007783 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007784 // TODO: should we allow this, e.g. to declare a global
7785 // function?
7786 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007787 goto erret;
7788
7789 case CMD_return:
7790 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007791 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007792 break;
7793
7794 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02007795 emsg(_(e_cannot_use_let_in_vim9_script));
7796 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007797 case CMD_var:
7798 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007799 case CMD_const:
7800 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007801 if (line == p)
7802 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007803 break;
7804
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007805 case CMD_unlet:
7806 case CMD_unlockvar:
7807 case CMD_lockvar:
7808 line = compile_unletlock(p, &ea, &cctx);
7809 break;
7810
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007811 case CMD_import:
7812 line = compile_import(p, &cctx);
7813 break;
7814
7815 case CMD_if:
7816 line = compile_if(p, &cctx);
7817 break;
7818 case CMD_elseif:
7819 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007820 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007821 break;
7822 case CMD_else:
7823 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007824 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007825 break;
7826 case CMD_endif:
7827 line = compile_endif(p, &cctx);
7828 break;
7829
7830 case CMD_while:
7831 line = compile_while(p, &cctx);
7832 break;
7833 case CMD_endwhile:
7834 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007835 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007836 break;
7837
7838 case CMD_for:
7839 line = compile_for(p, &cctx);
7840 break;
7841 case CMD_endfor:
7842 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007843 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007844 break;
7845 case CMD_continue:
7846 line = compile_continue(p, &cctx);
7847 break;
7848 case CMD_break:
7849 line = compile_break(p, &cctx);
7850 break;
7851
7852 case CMD_try:
7853 line = compile_try(p, &cctx);
7854 break;
7855 case CMD_catch:
7856 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007857 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007858 break;
7859 case CMD_finally:
7860 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007861 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007862 break;
7863 case CMD_endtry:
7864 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007865 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007866 break;
7867 case CMD_throw:
7868 line = compile_throw(p, &cctx);
7869 break;
7870
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007871 case CMD_eval:
7872 if (compile_expr0(&p, &cctx) == FAIL)
7873 goto erret;
7874
Bram Moolenaar3988f642020-08-27 22:43:03 +02007875 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007876 generate_instr_drop(&cctx, ISN_DROP, 1);
7877
7878 line = skipwhite(p);
7879 break;
7880
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007881 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007882 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007883 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007884 case CMD_echomsg:
7885 case CMD_echoerr:
7886 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007887 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007888
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007889 case CMD_put:
7890 ea.cmd = cmd;
7891 line = compile_put(p, &ea, &cctx);
7892 break;
7893
Bram Moolenaar3988f642020-08-27 22:43:03 +02007894 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007895
Bram Moolenaarae616492020-07-28 20:07:27 +02007896 case CMD_append:
7897 case CMD_change:
7898 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007899 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007900 case CMD_xit:
7901 not_in_vim9(&ea);
7902 goto erret;
7903
Bram Moolenaar002262f2020-07-08 17:47:57 +02007904 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007905 if (cctx.ctx_skip != SKIP_YES)
7906 {
7907 semsg(_(e_invalid_command_str), ea.cmd);
7908 goto erret;
7909 }
7910 // We don't check for a next command here.
7911 line = (char_u *)"";
7912 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007913
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007914 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007915 if (cctx.ctx_skip == SKIP_YES)
7916 {
7917 // We don't check for a next command here.
7918 line = (char_u *)"";
7919 }
7920 else
7921 {
7922 // Not recognized, execute with do_cmdline_cmd().
7923 ea.arg = p;
7924 line = compile_exec(line, &ea, &cctx);
7925 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007926 break;
7927 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007928nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007929 if (line == NULL)
7930 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007931 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007932
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007933 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02007934 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007935
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007936 if (cctx.ctx_type_stack.ga_len < 0)
7937 {
7938 iemsg("Type stack underflow");
7939 goto erret;
7940 }
7941 }
7942
7943 if (cctx.ctx_scope != NULL)
7944 {
7945 if (cctx.ctx_scope->se_type == IF_SCOPE)
7946 emsg(_(e_endif));
7947 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7948 emsg(_(e_endwhile));
7949 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7950 emsg(_(e_endfor));
7951 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007952 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007953 goto erret;
7954 }
7955
Bram Moolenaarefd88552020-06-18 20:50:10 +02007956 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007957 {
7958 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7959 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007960 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007961 goto erret;
7962 }
7963
7964 // Return zero if there is no return at the end.
7965 generate_PUSHNR(&cctx, 0);
7966 generate_instr(&cctx, ISN_RETURN);
7967 }
7968
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007969 {
7970 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7971 + ufunc->uf_dfunc_idx;
7972 dfunc->df_deleted = FALSE;
7973 dfunc->df_instr = instr->ga_data;
7974 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007975 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007976 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007977 if (cctx.ctx_outer_used)
7978 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007979 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007980 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007981
7982 ret = OK;
7983
7984erret:
7985 if (ret == FAIL)
7986 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007987 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007988 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7989 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007990
7991 for (idx = 0; idx < instr->ga_len; ++idx)
7992 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007993 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01007994 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007995
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007996 // If using the last entry in the table and it was added above, we
7997 // might as well remove it.
7998 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007999 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008000 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008001 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008002 ufunc->uf_dfunc_idx = 0;
8003 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008004 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008005
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008006 while (cctx.ctx_scope != NULL)
8007 drop_scope(&cctx);
8008
Bram Moolenaar20431c92020-03-20 18:39:46 +01008009 // Don't execute this function body.
8010 ga_clear_strings(&ufunc->uf_lines);
8011
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008012 if (errormsg != NULL)
8013 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008014 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008015 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008016 }
8017
8018 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008019 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008020 if (do_estack_push)
8021 estack_pop();
8022
Bram Moolenaar20431c92020-03-20 18:39:46 +01008023 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008024 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008025 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008026 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008027}
8028
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008029 void
8030set_function_type(ufunc_T *ufunc)
8031{
8032 int varargs = ufunc->uf_va_name != NULL;
8033 int argcount = ufunc->uf_args.ga_len;
8034
8035 // Create a type for the function, with the return type and any
8036 // argument types.
8037 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8038 // The type is included in "tt_args".
8039 if (argcount > 0 || varargs)
8040 {
8041 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8042 argcount, &ufunc->uf_type_list);
8043 // Add argument types to the function type.
8044 if (func_type_add_arg_types(ufunc->uf_func_type,
8045 argcount + varargs,
8046 &ufunc->uf_type_list) == FAIL)
8047 return;
8048 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8049 ufunc->uf_func_type->tt_min_argcount =
8050 argcount - ufunc->uf_def_args.ga_len;
8051 if (ufunc->uf_arg_types == NULL)
8052 {
8053 int i;
8054
8055 // lambda does not have argument types.
8056 for (i = 0; i < argcount; ++i)
8057 ufunc->uf_func_type->tt_args[i] = &t_any;
8058 }
8059 else
8060 mch_memmove(ufunc->uf_func_type->tt_args,
8061 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8062 if (varargs)
8063 {
8064 ufunc->uf_func_type->tt_args[argcount] =
8065 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8066 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8067 }
8068 }
8069 else
8070 // No arguments, can use a predefined type.
8071 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8072 argcount, &ufunc->uf_type_list);
8073}
8074
8075
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008076/*
8077 * Delete an instruction, free what it contains.
8078 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008079 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008080delete_instr(isn_T *isn)
8081{
8082 switch (isn->isn_type)
8083 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008084 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008085 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008086 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008087 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008088 case ISN_LOADENV:
8089 case ISN_LOADG:
8090 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008091 case ISN_LOADT:
8092 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008093 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008094 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008095 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008096 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008097 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008098 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008099 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008100 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008101 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008102 case ISN_STOREW:
8103 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008104 vim_free(isn->isn_arg.string);
8105 break;
8106
8107 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008108 case ISN_STORES:
8109 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008110 break;
8111
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008112 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008113 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008114 vim_free(isn->isn_arg.unlet.ul_name);
8115 break;
8116
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008117 case ISN_STOREOPT:
8118 vim_free(isn->isn_arg.storeopt.so_name);
8119 break;
8120
8121 case ISN_PUSHBLOB: // push blob isn_arg.blob
8122 blob_unref(isn->isn_arg.blob);
8123 break;
8124
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008125 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008126#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008127 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008128#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008129 break;
8130
8131 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008132#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008133 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008134#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008135 break;
8136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008137 case ISN_UCALL:
8138 vim_free(isn->isn_arg.ufunc.cuf_name);
8139 break;
8140
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008141 case ISN_FUNCREF:
8142 {
8143 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8144 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01008145 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008146
Bram Moolenaar077a4232020-12-22 18:33:27 +01008147 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
8148 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02008149 }
8150 break;
8151
8152 case ISN_DCALL:
8153 {
8154 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8155 + isn->isn_arg.dfunc.cdf_idx;
8156
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008157 if (dfunc->df_ufunc != NULL
8158 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008159 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008160 }
8161 break;
8162
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008163 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008164 {
8165 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8166 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8167
8168 if (ufunc != NULL)
8169 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008170 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02008171 func_ptr_unref(ufunc);
8172 }
8173
8174 vim_free(lambda);
8175 vim_free(isn->isn_arg.newfunc.nf_global);
8176 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008177 break;
8178
Bram Moolenaar5e654232020-09-16 15:22:00 +02008179 case ISN_CHECKTYPE:
8180 free_type(isn->isn_arg.type.ct_type);
8181 break;
8182
Bram Moolenaar02194d22020-10-24 23:08:38 +02008183 case ISN_CMDMOD:
8184 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8185 ->cmod_filter_regmatch.regprog);
8186 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8187 break;
8188
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008189 case ISN_2BOOL:
8190 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008191 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008192 case ISN_ADDBLOB:
8193 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008194 case ISN_ANYINDEX:
8195 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008196 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008197 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008198 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008199 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008200 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008201 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008202 case ISN_COMPAREANY:
8203 case ISN_COMPAREBLOB:
8204 case ISN_COMPAREBOOL:
8205 case ISN_COMPAREDICT:
8206 case ISN_COMPAREFLOAT:
8207 case ISN_COMPAREFUNC:
8208 case ISN_COMPARELIST:
8209 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008210 case ISN_COMPARESPECIAL:
8211 case ISN_COMPARESTRING:
8212 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008213 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008214 case ISN_DROP:
8215 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008216 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008217 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008218 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008219 case ISN_EXECCONCAT:
8220 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008221 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008222 case ISN_GETITEM:
8223 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008224 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008225 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008226 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008227 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008228 case ISN_LOADBDICT:
8229 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008230 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008231 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008232 case ISN_LOADSCRIPT:
8233 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008234 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008235 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008236 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008237 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008238 case ISN_NEGATENR:
8239 case ISN_NEWDICT:
8240 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008241 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008242 case ISN_OPFLOAT:
8243 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008244 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008245 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008246 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008247 case ISN_PUSHF:
8248 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008249 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008250 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008251 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008252 case ISN_SHUFFLE:
8253 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008254 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01008255 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008256 case ISN_STORENR:
8257 case ISN_STOREOUTER:
8258 case ISN_STOREREG:
8259 case ISN_STORESCRIPT:
8260 case ISN_STOREV:
8261 case ISN_STRINDEX:
8262 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008263 case ISN_THROW:
8264 case ISN_TRY:
Bram Moolenaar792f7862020-11-23 08:31:18 +01008265 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008266 // nothing allocated
8267 break;
8268 }
8269}
8270
8271/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008272 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01008273 */
8274 static void
8275delete_def_function_contents(dfunc_T *dfunc)
8276{
8277 int idx;
8278
8279 ga_clear(&dfunc->df_def_args_isn);
8280
8281 if (dfunc->df_instr != NULL)
8282 {
8283 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
8284 delete_instr(dfunc->df_instr + idx);
8285 VIM_CLEAR(dfunc->df_instr);
8286 }
8287
8288 dfunc->df_deleted = TRUE;
8289}
8290
8291/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008292 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008293 * function, unless another user function still uses it.
8294 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008295 */
8296 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008297unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008298{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008299 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008300 {
8301 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8302 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008303
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008304 if (--dfunc->df_refcount <= 0)
8305 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008306 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008307 ufunc->uf_dfunc_idx = 0;
8308 if (dfunc->df_ufunc == ufunc)
8309 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008310 }
8311}
8312
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008313/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008314 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008315 */
8316 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008317link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008318{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008319 if (ufunc->uf_dfunc_idx > 0)
8320 {
8321 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8322 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008323
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008324 ++dfunc->df_refcount;
8325 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008326}
8327
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008328#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008329/*
8330 * Free all functions defined with ":def".
8331 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008332 void
8333free_def_functions(void)
8334{
Bram Moolenaar20431c92020-03-20 18:39:46 +01008335 int idx;
8336
8337 for (idx = 0; idx < def_functions.ga_len; ++idx)
8338 {
8339 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
8340
8341 delete_def_function_contents(dfunc);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008342 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008343 }
8344
8345 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008346}
8347#endif
8348
8349
8350#endif // FEAT_EVAL