blob: 41db5e669147bdb07ea8569d68b9be840090af48 [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;
Bram Moolenaarced68a02021-01-24 17:53:47 +010047 int is_seen_skip_not; // a block was unconditionally executed
Bram Moolenaarefd88552020-06-18 20:50:10 +020048 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010049 int is_if_label; // instruction idx at IF or ELSEIF
50 endlabel_T *is_end_label; // instructions to set end label
51} ifscope_T;
52
53/*
54 * info specific for the scope of :while
55 */
56typedef struct {
57 int ws_top_label; // instruction idx at WHILE
58 endlabel_T *ws_end_label; // instructions to set end
59} whilescope_T;
60
61/*
62 * info specific for the scope of :for
63 */
64typedef struct {
65 int fs_top_label; // instruction idx at FOR
66 endlabel_T *fs_end_label; // break instructions
67} forscope_T;
68
69/*
70 * info specific for the scope of :try
71 */
72typedef struct {
73 int ts_try_label; // instruction idx at TRY
74 endlabel_T *ts_end_label; // jump to :finally or :endtry
75 int ts_catch_label; // instruction idx of last CATCH
76 int ts_caught_all; // "catch" without argument encountered
77} tryscope_T;
78
79typedef enum {
80 NO_SCOPE,
81 IF_SCOPE,
82 WHILE_SCOPE,
83 FOR_SCOPE,
84 TRY_SCOPE,
85 BLOCK_SCOPE
86} scopetype_T;
87
88/*
89 * Info for one scope, pointed to by "ctx_scope".
90 */
91typedef struct scope_S scope_T;
92struct scope_S {
93 scope_T *se_outer; // scope containing this one
94 scopetype_T se_type;
95 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020096 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010097 union {
98 ifscope_T se_if;
99 whilescope_T se_while;
100 forscope_T se_for;
101 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100102 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100103};
104
105/*
106 * Entry for "ctx_locals". Used for arguments and local variables.
107 */
108typedef struct {
109 char_u *lv_name;
110 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200111 int lv_idx; // index of the variable on the stack
Bram Moolenaarab360522021-01-10 14:02:28 +0100112 int lv_from_outer; // nesting level, using ctx_outer scope
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200113 int lv_const; // when TRUE cannot be assigned to
114 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115} lvar_T;
116
117/*
118 * Context for compiling lines of Vim script.
119 * Stores info about the local variables and condition stack.
120 */
121struct cctx_S {
122 ufunc_T *ctx_ufunc; // current function
123 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200124 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100125 garray_T ctx_instr; // generated instructions
126
Bram Moolenaarb2049902021-01-24 12:53:53 +0100127 int ctx_profiling; // when TRUE generate ISN_PROF_START
128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100129 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200130 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100131
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200132 int ctx_has_closure; // set to one if a closures was created in
133 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200134
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 garray_T ctx_imports; // imported items
136
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200137 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100138 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200139 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100140
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141 cctx_T *ctx_outer; // outer scope for lambda or nested
142 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200143 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200144
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100145 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200146 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200147
Bram Moolenaar02194d22020-10-24 23:08:38 +0200148 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149};
150
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100151static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100152
153/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100154 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100155 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100156 * If "lvar" is NULL only check if the variable can be found.
157 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100159 static int
160lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100161{
162 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100163 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100165 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100166 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167
168 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
170 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100171 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
172 if (STRNCMP(name, lvp->lv_name, len) == 0
173 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200174 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100175 if (lvar != NULL)
176 {
177 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100178 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100179 }
180 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200181 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200183
184 // Find local in outer function scope.
185 if (cctx->ctx_outer != NULL)
186 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100187 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200188 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100189 if (lvar != NULL)
190 {
191 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100192 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100193 }
194 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200195 }
196 }
197
Bram Moolenaar709664c2020-12-12 14:33:41 +0100198 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100199}
200
201/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200202 * Lookup an argument in the current function and an enclosing function.
203 * Returns the argument index in "idxp"
204 * Returns the argument type in "type"
205 * Sets "gen_load_outer" to TRUE if found in outer scope.
206 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207 */
208 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200209arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200210 char_u *name,
211 size_t len,
212 int *idxp,
213 type_T **type,
214 int *gen_load_outer,
215 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216{
217 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200218 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100220 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200221 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100222 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
223 {
224 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
225
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200226 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
227 {
228 if (idxp != NULL)
229 {
230 // Arguments are located above the frame pointer. One further
231 // if there is a vararg argument
232 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
233 + STACK_FRAME_SIZE)
234 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
235
236 if (cctx->ctx_ufunc->uf_arg_types != NULL)
237 *type = cctx->ctx_ufunc->uf_arg_types[idx];
238 else
239 *type = &t_any;
240 }
241 return OK;
242 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100243 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200245 va_name = cctx->ctx_ufunc->uf_va_name;
246 if (va_name != NULL
247 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
248 {
249 if (idxp != NULL)
250 {
251 // varargs is always the last argument
252 *idxp = -STACK_FRAME_SIZE - 1;
253 *type = cctx->ctx_ufunc->uf_va_type;
254 }
255 return OK;
256 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200258 if (cctx->ctx_outer != NULL)
259 {
260 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200261 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200262 == OK)
263 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100264 if (gen_load_outer != NULL)
265 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200266 return OK;
267 }
268 }
269
270 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271}
272
273/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200274 * Lookup a script-local variable in the current script, possibly defined in a
275 * block that contains the function "cctx->ctx_ufunc".
276 * "cctx" is NULL at the script level.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100277 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200278 * Return NULL when not found.
279 */
280 static sallvar_T *
281find_script_var(char_u *name, size_t len, cctx_T *cctx)
282{
283 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
284 hashitem_T *hi;
285 int cc;
286 sallvar_T *sav;
287 ufunc_T *ufunc;
288
289 // Find the list of all script variables with the right name.
290 if (len > 0)
291 {
292 cc = name[len];
293 name[len] = NUL;
294 }
295 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
296 if (len > 0)
297 name[len] = cc;
298 if (HASHITEM_EMPTY(hi))
299 return NULL;
300
301 sav = HI2SAV(hi);
302 if (sav->sav_block_id == 0 || cctx == NULL)
303 // variable defined in the script scope or not in a function.
304 return sav;
305
306 // Go over the variables with this name and find one that was visible
307 // from the function.
308 ufunc = cctx->ctx_ufunc;
309 while (sav != NULL)
310 {
311 int idx;
312
313 // Go over the blocks that this function was defined in. If the
314 // variable block ID matches it was visible to the function.
315 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
316 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
317 return sav;
318 sav = sav->sav_next;
319 }
320
321 return NULL;
322}
323
324/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100325 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200326 */
327 static int
328script_is_vim9()
329{
330 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
331}
332
333/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200334 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200335 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
336 * without "s:".
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200337 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100338 * Returns OK or FAIL.
339 */
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100340 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200341script_var_exists(char_u *name, size_t len, int vim9script, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100342{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200343 int is_vim9_script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100344
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200345 if (current_sctx.sc_sid <= 0)
346 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200347 is_vim9_script = script_is_vim9();
348 if (vim9script && !is_vim9_script)
Bram Moolenaar53900992020-08-22 19:02:02 +0200349 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200350 if (is_vim9_script)
351 {
352 // Check script variables that were visible where the function was
353 // defined.
354 if (find_script_var(name, len, cctx) != NULL)
355 return OK;
356 }
357 else
358 {
359 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
360 dictitem_T *di;
361 int cc;
362
363 // Check script variables that are currently visible
364 cc = name[len];
365 name[len] = NUL;
366 di = find_var_in_ht(ht, 0, name, TRUE);
367 name[len] = cc;
368 if (di != NULL)
369 return OK;
370 }
371
372 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100373}
374
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100375/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100376 * Return TRUE if "name" is a local variable, argument, script variable or
377 * imported.
378 */
379 static int
380variable_exists(char_u *name, size_t len, cctx_T *cctx)
381{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100382 return (cctx != NULL
383 && (lookup_local(name, len, NULL, cctx) == OK
384 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaare0890d62021-02-17 14:52:14 +0100385 || script_var_exists(name, len, FALSE, cctx) == OK
386 || find_imported(name, len, cctx) != NULL;
387}
388
389/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100390 * Return TRUE if "name" is a local variable, argument, script variable,
391 * imported or function.
392 */
393 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100394item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100395{
396 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100397 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100398
399 if (variable_exists(name, len, cctx))
400 return TRUE;
401
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100402 // This is similar to what is in lookup_scriptitem():
403 // Find a function, so that a following "->" works.
404 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
405 // a function call.
406 p = skipwhite(name + len);
407
408 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
409 {
410 // Do not check for an internal function, since it might also be a
411 // valid command, such as ":split" versuse "split()".
412 // Skip "g:" before a function name.
413 is_global = (name[0] == 'g' && name[1] == ':');
414 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
415 }
416 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100417}
418
419/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100420 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200421 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200422 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100423 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100424 * Return FAIL and give an error if it defined.
425 */
426 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100427check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100428{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200429 int c = p[len];
430 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200431
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100432 if (script_var_exists(p, len, FALSE, cctx) == OK)
433 {
434 if (is_arg)
435 semsg(_(e_argument_already_declared_in_script_str), p);
436 else
437 semsg(_(e_variable_already_declared_in_script_str), p);
438 return FAIL;
439 }
440
Bram Moolenaarad486a02020-08-01 23:22:18 +0200441 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100442 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100443 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200444 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200445 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200446 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100447 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200448 // A local or script-local function can shadow a global function.
449 if (ufunc == NULL || !func_is_global(ufunc)
450 || (p[0] == 'g' && p[1] == ':'))
451 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100452 if (is_arg)
453 semsg(_(e_argument_name_shadows_existing_variable_str), p);
454 else
455 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200456 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200457 return FAIL;
458 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100459 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200460 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100461 return OK;
462}
463
Bram Moolenaar65b95452020-07-19 14:03:09 +0200464
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100465/////////////////////////////////////////////////////////////////////
466// Following generate_ functions expect the caller to call ga_grow().
467
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200468#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
469#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100470
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100471/*
472 * Generate an instruction without arguments.
473 * Returns a pointer to the new instruction, NULL if failed.
474 */
475 static isn_T *
476generate_instr(cctx_T *cctx, isntype_T isn_type)
477{
478 garray_T *instr = &cctx->ctx_instr;
479 isn_T *isn;
480
Bram Moolenaar080457c2020-03-03 21:53:32 +0100481 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100482 if (ga_grow(instr, 1) == FAIL)
483 return NULL;
484 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
485 isn->isn_type = isn_type;
486 isn->isn_lnum = cctx->ctx_lnum + 1;
487 ++instr->ga_len;
488
489 return isn;
490}
491
492/*
493 * Generate an instruction without arguments.
494 * "drop" will be removed from the stack.
495 * Returns a pointer to the new instruction, NULL if failed.
496 */
497 static isn_T *
498generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
499{
500 garray_T *stack = &cctx->ctx_type_stack;
501
Bram Moolenaar080457c2020-03-03 21:53:32 +0100502 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100503 stack->ga_len -= drop;
504 return generate_instr(cctx, isn_type);
505}
506
507/*
508 * Generate instruction "isn_type" and put "type" on the type stack.
509 */
510 static isn_T *
511generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
512{
513 isn_T *isn;
514 garray_T *stack = &cctx->ctx_type_stack;
515
516 if ((isn = generate_instr(cctx, isn_type)) == NULL)
517 return NULL;
518
519 if (ga_grow(stack, 1) == FAIL)
520 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200521 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100522 ++stack->ga_len;
523
524 return isn;
525}
526
527/*
528 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200529 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530 */
531 static int
532may_generate_2STRING(int offset, cctx_T *cctx)
533{
534 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200535 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100536 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100537 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100538
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100539 RETURN_OK_IF_SKIP(cctx);
540 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200541 switch ((*type)->tt_type)
542 {
543 // nothing to be done
544 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100545
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200546 // conversion possible
547 case VAR_SPECIAL:
548 case VAR_BOOL:
549 case VAR_NUMBER:
550 case VAR_FLOAT:
551 break;
552
553 // conversion possible (with runtime check)
554 case VAR_ANY:
555 case VAR_UNKNOWN:
556 isntype = ISN_2STRING_ANY;
557 break;
558
559 // conversion not possible
560 case VAR_VOID:
561 case VAR_BLOB:
562 case VAR_FUNC:
563 case VAR_PARTIAL:
564 case VAR_LIST:
565 case VAR_DICT:
566 case VAR_JOB:
567 case VAR_CHANNEL:
568 to_string_error((*type)->tt_type);
569 return FAIL;
570 }
571
572 *type = &t_string;
573 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574 return FAIL;
575 isn->isn_arg.number = offset;
576
577 return OK;
578}
579
580 static int
581check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
582{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200583 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100584 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200585 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586 {
587 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200588 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200590 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591 return FAIL;
592 }
593 return OK;
594}
595
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200596 static int
597generate_add_instr(
598 cctx_T *cctx,
599 vartype_T vartype,
600 type_T *type1,
601 type_T *type2)
602{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100603 garray_T *stack = &cctx->ctx_type_stack;
604 isn_T *isn = generate_instr_drop(cctx,
605 vartype == VAR_NUMBER ? ISN_OPNR
606 : vartype == VAR_LIST ? ISN_ADDLIST
607 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200608#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100609 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200610#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100611 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200612
613 if (vartype != VAR_LIST && vartype != VAR_BLOB
614 && type1->tt_type != VAR_ANY
615 && type2->tt_type != VAR_ANY
616 && check_number_or_float(
617 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
618 return FAIL;
619
620 if (isn != NULL)
621 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100622
623 // When concatenating two lists with different member types the member type
624 // becomes "any".
625 if (vartype == VAR_LIST
626 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
627 && type1->tt_member != type2->tt_member)
628 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
629
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200630 return isn == NULL ? FAIL : OK;
631}
632
633/*
634 * Get the type to use for an instruction for an operation on "type1" and
635 * "type2". If they are matching use a type-specific instruction. Otherwise
636 * fall back to runtime type checking.
637 */
638 static vartype_T
639operator_type(type_T *type1, type_T *type2)
640{
641 if (type1->tt_type == type2->tt_type
642 && (type1->tt_type == VAR_NUMBER
643 || type1->tt_type == VAR_LIST
644#ifdef FEAT_FLOAT
645 || type1->tt_type == VAR_FLOAT
646#endif
647 || type1->tt_type == VAR_BLOB))
648 return type1->tt_type;
649 return VAR_ANY;
650}
651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100652/*
653 * Generate an instruction with two arguments. The instruction depends on the
654 * type of the arguments.
655 */
656 static int
657generate_two_op(cctx_T *cctx, char_u *op)
658{
659 garray_T *stack = &cctx->ctx_type_stack;
660 type_T *type1;
661 type_T *type2;
662 vartype_T vartype;
663 isn_T *isn;
664
Bram Moolenaar080457c2020-03-03 21:53:32 +0100665 RETURN_OK_IF_SKIP(cctx);
666
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200667 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100668 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
669 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200670 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100671
672 switch (*op)
673 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200674 case '+':
675 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100676 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100677 break;
678
679 case '-':
680 case '*':
681 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
682 op) == FAIL)
683 return FAIL;
684 if (vartype == VAR_NUMBER)
685 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
686#ifdef FEAT_FLOAT
687 else if (vartype == VAR_FLOAT)
688 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
689#endif
690 else
691 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
692 if (isn != NULL)
693 isn->isn_arg.op.op_type = *op == '*'
694 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
695 break;
696
Bram Moolenaar4c683752020-04-05 21:38:23 +0200697 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100698 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200699 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100700 && type2->tt_type != VAR_NUMBER))
701 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200702 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100703 return FAIL;
704 }
705 isn = generate_instr_drop(cctx,
706 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
707 if (isn != NULL)
708 isn->isn_arg.op.op_type = EXPR_REM;
709 break;
710 }
711
712 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200713 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100714 {
715 type_T *type = &t_any;
716
717#ifdef FEAT_FLOAT
718 // float+number and number+float results in float
719 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
720 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
721 type = &t_float;
722#endif
723 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
724 }
725
726 return OK;
727}
728
729/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200730 * Get the instruction to use for comparing "type1" with "type2"
731 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100732 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200733 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100734get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100735{
736 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100737
Bram Moolenaar4c683752020-04-05 21:38:23 +0200738 if (type1 == VAR_UNKNOWN)
739 type1 = VAR_ANY;
740 if (type2 == VAR_UNKNOWN)
741 type2 = VAR_ANY;
742
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743 if (type1 == type2)
744 {
745 switch (type1)
746 {
747 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
748 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
749 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
750 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
751 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
752 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
753 case VAR_LIST: isntype = ISN_COMPARELIST; break;
754 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
755 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756 default: isntype = ISN_COMPAREANY; break;
757 }
758 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200759 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100760 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100761 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762 isntype = ISN_COMPAREANY;
763
Bram Moolenaar657137c2021-01-09 15:45:23 +0100764 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100765 && (isntype == ISN_COMPAREBOOL
766 || isntype == ISN_COMPARESPECIAL
767 || isntype == ISN_COMPARENR
768 || isntype == ISN_COMPAREFLOAT))
769 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200770 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100771 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200772 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100773 }
774 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100775 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
777 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100778 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
779 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100780 && (type1 == VAR_BLOB || type2 == VAR_BLOB
781 || type1 == VAR_LIST || type2 == VAR_LIST))))
782 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200783 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200785 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100786 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200787 return isntype;
788}
789
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200790 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100791check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200792{
793 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
794 return FAIL;
795 return OK;
796}
797
Bram Moolenaara5565e42020-05-09 15:44:01 +0200798/*
799 * Generate an ISN_COMPARE* instruction with a boolean result.
800 */
801 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100802generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200803{
804 isntype_T isntype;
805 isn_T *isn;
806 garray_T *stack = &cctx->ctx_type_stack;
807 vartype_T type1;
808 vartype_T type2;
809
810 RETURN_OK_IF_SKIP(cctx);
811
812 // Get the known type of the two items on the stack. If they are matching
813 // use a type-specific instruction. Otherwise fall back to runtime type
814 // checking.
815 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
816 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100817 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200818 if (isntype == ISN_DROP)
819 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100820
821 if ((isn = generate_instr(cctx, isntype)) == NULL)
822 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100823 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100824 isn->isn_arg.op.op_ic = ic;
825
826 // takes two arguments, puts one bool back
827 if (stack->ga_len >= 2)
828 {
829 --stack->ga_len;
830 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
831 }
832
833 return OK;
834}
835
836/*
837 * Generate an ISN_2BOOL instruction.
838 */
839 static int
840generate_2BOOL(cctx_T *cctx, int invert)
841{
842 isn_T *isn;
843 garray_T *stack = &cctx->ctx_type_stack;
844
Bram Moolenaar080457c2020-03-03 21:53:32 +0100845 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100846 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
847 return FAIL;
848 isn->isn_arg.number = invert;
849
850 // type becomes bool
851 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
852
853 return OK;
854}
855
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200856/*
857 * Generate an ISN_COND2BOOL instruction.
858 */
859 static int
860generate_COND2BOOL(cctx_T *cctx)
861{
862 isn_T *isn;
863 garray_T *stack = &cctx->ctx_type_stack;
864
865 RETURN_OK_IF_SKIP(cctx);
866 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
867 return FAIL;
868
869 // type becomes bool
870 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
871
872 return OK;
873}
874
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100875 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200876generate_TYPECHECK(
877 cctx_T *cctx,
878 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100879 int offset,
880 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100881{
882 isn_T *isn;
883 garray_T *stack = &cctx->ctx_type_stack;
884
Bram Moolenaar080457c2020-03-03 21:53:32 +0100885 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100886 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
887 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200888 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100889 isn->isn_arg.type.ct_off = (int8_T)offset;
890 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891
Bram Moolenaar5e654232020-09-16 15:22:00 +0200892 // type becomes expected
893 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100894
895 return OK;
896}
897
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100898 static int
899generate_SETTYPE(
900 cctx_T *cctx,
901 type_T *expected)
902{
903 isn_T *isn;
904
905 RETURN_OK_IF_SKIP(cctx);
906 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
907 return FAIL;
908 isn->isn_arg.type.ct_type = alloc_type(expected);
909 return OK;
910}
911
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100912/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200913 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
914 * used. Return FALSE if the types will never match.
915 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100916 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200917use_typecheck(type_T *actual, type_T *expected)
918{
919 if (actual->tt_type == VAR_ANY
920 || actual->tt_type == VAR_UNKNOWN
921 || (actual->tt_type == VAR_FUNC
922 && (expected->tt_type == VAR_FUNC
923 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100924 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
925 && ((actual->tt_member == &t_void)
926 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200927 return TRUE;
928 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
929 && actual->tt_type == expected->tt_type)
930 // This takes care of a nested list or dict.
931 return use_typecheck(actual->tt_member, expected->tt_member);
932 return FALSE;
933}
934
935/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200936 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200937 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200938 * - "actual" is a type that can be "expected" type: add a runtime check; or
939 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200940 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
941 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200942 */
Bram Moolenaar351ead02021-01-16 16:07:01 +0100943 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200944need_type(
945 type_T *actual,
946 type_T *expected,
947 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100948 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200949 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200950 int silent,
951 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200952{
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100953 where_T where;
954
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200955 if (expected == &t_bool && actual != &t_bool
956 && (actual->tt_flags & TTFLAG_BOOL_OK))
957 {
958 // Using "0", "1" or the result of an expression with "&&" or "||" as a
959 // boolean is OK but requires a conversion.
960 generate_2BOOL(cctx, FALSE);
961 return OK;
962 }
963
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100964 where.wt_index = arg_idx;
965 where.wt_variable = FALSE;
966 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200967 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200968
969 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200970 // If it's a constant a runtime check makes no sense.
971 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200972 {
Bram Moolenaare32e5162021-01-21 20:21:29 +0100973 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200974 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200975 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200976
977 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100978 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200979 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200980}
981
982/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100983 * Check that the top of the type stack has a type that can be used as a
984 * condition. Give an error and return FAIL if not.
985 */
986 static int
987bool_on_stack(cctx_T *cctx)
988{
989 garray_T *stack = &cctx->ctx_type_stack;
990 type_T *type;
991
992 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
993 if (type == &t_bool)
994 return OK;
995
996 if (type == &t_any || type == &t_number)
997 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
998 // This requires a runtime type check.
999 return generate_COND2BOOL(cctx);
1000
Bram Moolenaar351ead02021-01-16 16:07:01 +01001001 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001002}
1003
1004/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001005 * Generate an ISN_PUSHNR instruction.
1006 */
1007 static int
1008generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1009{
1010 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001011 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001012
Bram Moolenaar080457c2020-03-03 21:53:32 +01001013 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001014 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1015 return FAIL;
1016 isn->isn_arg.number = number;
1017
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001018 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001019 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001020 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001021 return OK;
1022}
1023
1024/*
1025 * Generate an ISN_PUSHBOOL instruction.
1026 */
1027 static int
1028generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1029{
1030 isn_T *isn;
1031
Bram Moolenaar080457c2020-03-03 21:53:32 +01001032 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1034 return FAIL;
1035 isn->isn_arg.number = number;
1036
1037 return OK;
1038}
1039
1040/*
1041 * Generate an ISN_PUSHSPEC instruction.
1042 */
1043 static int
1044generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1045{
1046 isn_T *isn;
1047
Bram Moolenaar080457c2020-03-03 21:53:32 +01001048 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001049 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1050 return FAIL;
1051 isn->isn_arg.number = number;
1052
1053 return OK;
1054}
1055
1056#ifdef FEAT_FLOAT
1057/*
1058 * Generate an ISN_PUSHF instruction.
1059 */
1060 static int
1061generate_PUSHF(cctx_T *cctx, float_T fnumber)
1062{
1063 isn_T *isn;
1064
Bram Moolenaar080457c2020-03-03 21:53:32 +01001065 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1067 return FAIL;
1068 isn->isn_arg.fnumber = fnumber;
1069
1070 return OK;
1071}
1072#endif
1073
1074/*
1075 * Generate an ISN_PUSHS instruction.
1076 * Consumes "str".
1077 */
1078 static int
1079generate_PUSHS(cctx_T *cctx, char_u *str)
1080{
1081 isn_T *isn;
1082
Bram Moolenaar508b5612021-01-02 18:17:26 +01001083 if (cctx->ctx_skip == SKIP_YES)
1084 {
1085 vim_free(str);
1086 return OK;
1087 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1089 return FAIL;
1090 isn->isn_arg.string = str;
1091
1092 return OK;
1093}
1094
1095/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001096 * Generate an ISN_PUSHCHANNEL instruction.
1097 * Consumes "channel".
1098 */
1099 static int
1100generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1101{
1102 isn_T *isn;
1103
Bram Moolenaar080457c2020-03-03 21:53:32 +01001104 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001105 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1106 return FAIL;
1107 isn->isn_arg.channel = channel;
1108
1109 return OK;
1110}
1111
1112/*
1113 * Generate an ISN_PUSHJOB instruction.
1114 * Consumes "job".
1115 */
1116 static int
1117generate_PUSHJOB(cctx_T *cctx, job_T *job)
1118{
1119 isn_T *isn;
1120
Bram Moolenaar080457c2020-03-03 21:53:32 +01001121 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001122 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001123 return FAIL;
1124 isn->isn_arg.job = job;
1125
1126 return OK;
1127}
1128
1129/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001130 * Generate an ISN_PUSHBLOB instruction.
1131 * Consumes "blob".
1132 */
1133 static int
1134generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1135{
1136 isn_T *isn;
1137
Bram Moolenaar080457c2020-03-03 21:53:32 +01001138 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001139 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1140 return FAIL;
1141 isn->isn_arg.blob = blob;
1142
1143 return OK;
1144}
1145
1146/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001147 * Generate an ISN_PUSHFUNC instruction with name "name".
1148 * Consumes "name".
1149 */
1150 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001151generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001152{
1153 isn_T *isn;
1154
Bram Moolenaar080457c2020-03-03 21:53:32 +01001155 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001156 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001157 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001158 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001159
1160 return OK;
1161}
1162
1163/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001164 * Generate an ISN_GETITEM instruction with "index".
1165 */
1166 static int
1167generate_GETITEM(cctx_T *cctx, int index)
1168{
1169 isn_T *isn;
1170 garray_T *stack = &cctx->ctx_type_stack;
1171 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1172 type_T *item_type = &t_any;
1173
1174 RETURN_OK_IF_SKIP(cctx);
1175
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001176 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001177 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001178 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001179 emsg(_(e_listreq));
1180 return FAIL;
1181 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001182 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001183 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1184 return FAIL;
1185 isn->isn_arg.number = index;
1186
1187 // add the item type to the type stack
1188 if (ga_grow(stack, 1) == FAIL)
1189 return FAIL;
1190 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1191 ++stack->ga_len;
1192 return OK;
1193}
1194
1195/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001196 * Generate an ISN_SLICE instruction with "count".
1197 */
1198 static int
1199generate_SLICE(cctx_T *cctx, int count)
1200{
1201 isn_T *isn;
1202
1203 RETURN_OK_IF_SKIP(cctx);
1204 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1205 return FAIL;
1206 isn->isn_arg.number = count;
1207 return OK;
1208}
1209
1210/*
1211 * Generate an ISN_CHECKLEN instruction with "min_len".
1212 */
1213 static int
1214generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1215{
1216 isn_T *isn;
1217
1218 RETURN_OK_IF_SKIP(cctx);
1219
1220 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1221 return FAIL;
1222 isn->isn_arg.checklen.cl_min_len = min_len;
1223 isn->isn_arg.checklen.cl_more_OK = more_OK;
1224
1225 return OK;
1226}
1227
1228/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001229 * Generate an ISN_STORE instruction.
1230 */
1231 static int
1232generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1233{
1234 isn_T *isn;
1235
Bram Moolenaar080457c2020-03-03 21:53:32 +01001236 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001237 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1238 return FAIL;
1239 if (name != NULL)
1240 isn->isn_arg.string = vim_strsave(name);
1241 else
1242 isn->isn_arg.number = idx;
1243
1244 return OK;
1245}
1246
1247/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001248 * Generate an ISN_STOREOUTER instruction.
1249 */
1250 static int
1251generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1252{
1253 isn_T *isn;
1254
1255 RETURN_OK_IF_SKIP(cctx);
1256 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1257 return FAIL;
1258 isn->isn_arg.outer.outer_idx = idx;
1259 isn->isn_arg.outer.outer_depth = level;
1260
1261 return OK;
1262}
1263
1264/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001265 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1266 */
1267 static int
1268generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1269{
1270 isn_T *isn;
1271
Bram Moolenaar080457c2020-03-03 21:53:32 +01001272 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1274 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001275 isn->isn_arg.storenr.stnr_idx = idx;
1276 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277
1278 return OK;
1279}
1280
1281/*
1282 * Generate an ISN_STOREOPT instruction
1283 */
1284 static int
1285generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1286{
1287 isn_T *isn;
1288
Bram Moolenaar080457c2020-03-03 21:53:32 +01001289 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001290 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291 return FAIL;
1292 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1293 isn->isn_arg.storeopt.so_flags = opt_flags;
1294
1295 return OK;
1296}
1297
1298/*
1299 * Generate an ISN_LOAD or similar instruction.
1300 */
1301 static int
1302generate_LOAD(
1303 cctx_T *cctx,
1304 isntype_T isn_type,
1305 int idx,
1306 char_u *name,
1307 type_T *type)
1308{
1309 isn_T *isn;
1310
Bram Moolenaar080457c2020-03-03 21:53:32 +01001311 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1313 return FAIL;
1314 if (name != NULL)
1315 isn->isn_arg.string = vim_strsave(name);
1316 else
1317 isn->isn_arg.number = idx;
1318
1319 return OK;
1320}
1321
1322/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001323 * Generate an ISN_LOADOUTER instruction
1324 */
1325 static int
1326generate_LOADOUTER(
1327 cctx_T *cctx,
1328 int idx,
1329 int nesting,
1330 type_T *type)
1331{
1332 isn_T *isn;
1333
1334 RETURN_OK_IF_SKIP(cctx);
1335 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1336 return FAIL;
1337 isn->isn_arg.outer.outer_idx = idx;
1338 isn->isn_arg.outer.outer_depth = nesting;
1339
1340 return OK;
1341}
1342
1343/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001344 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001345 */
1346 static int
1347generate_LOADV(
1348 cctx_T *cctx,
1349 char_u *name,
1350 int error)
1351{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001352 int di_flags;
1353 int vidx = find_vim_var(name, &di_flags);
1354 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001355
Bram Moolenaar080457c2020-03-03 21:53:32 +01001356 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001357 if (vidx < 0)
1358 {
1359 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001360 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001361 return FAIL;
1362 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001363 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001364
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001365 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001366}
1367
1368/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001369 * Generate an ISN_UNLET instruction.
1370 */
1371 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001372generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001373{
1374 isn_T *isn;
1375
1376 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001377 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001378 return FAIL;
1379 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1380 isn->isn_arg.unlet.ul_forceit = forceit;
1381
1382 return OK;
1383}
1384
1385/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001386 * Generate an ISN_LOCKCONST instruction.
1387 */
1388 static int
1389generate_LOCKCONST(cctx_T *cctx)
1390{
1391 isn_T *isn;
1392
1393 RETURN_OK_IF_SKIP(cctx);
1394 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1395 return FAIL;
1396 return OK;
1397}
1398
1399/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400 * Generate an ISN_LOADS instruction.
1401 */
1402 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001403generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001405 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001407 int sid,
1408 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409{
1410 isn_T *isn;
1411
Bram Moolenaar080457c2020-03-03 21:53:32 +01001412 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001413 if (isn_type == ISN_LOADS)
1414 isn = generate_instr_type(cctx, isn_type, type);
1415 else
1416 isn = generate_instr_drop(cctx, isn_type, 1);
1417 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001418 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001419 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1420 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421
1422 return OK;
1423}
1424
1425/*
1426 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1427 */
1428 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001429generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001430 cctx_T *cctx,
1431 isntype_T isn_type,
1432 int sid,
1433 int idx,
1434 type_T *type)
1435{
1436 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001437 scriptref_T *sref;
1438 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001439
Bram Moolenaar080457c2020-03-03 21:53:32 +01001440 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441 if (isn_type == ISN_LOADSCRIPT)
1442 isn = generate_instr_type(cctx, isn_type, type);
1443 else
1444 isn = generate_instr_drop(cctx, isn_type, 1);
1445 if (isn == NULL)
1446 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001447
1448 // This requires three arguments, which doesn't fit in an instruction, thus
1449 // we need to allocate a struct for this.
1450 sref = ALLOC_ONE(scriptref_T);
1451 if (sref == NULL)
1452 return FAIL;
1453 isn->isn_arg.script.scriptref = sref;
1454 sref->sref_sid = sid;
1455 sref->sref_idx = idx;
1456 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001457 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001458 return OK;
1459}
1460
1461/*
1462 * Generate an ISN_NEWLIST instruction.
1463 */
1464 static int
1465generate_NEWLIST(cctx_T *cctx, int count)
1466{
1467 isn_T *isn;
1468 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001469 type_T *type;
1470 type_T *member;
1471
Bram Moolenaar080457c2020-03-03 21:53:32 +01001472 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1474 return FAIL;
1475 isn->isn_arg.number = count;
1476
Bram Moolenaar127542b2020-08-09 17:22:04 +02001477 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001478 if (count == 0)
1479 member = &t_void;
1480 else
1481 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001482 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1483 cctx->ctx_type_list);
1484 type = get_list_type(member, cctx->ctx_type_list);
1485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001486 // drop the value types
1487 stack->ga_len -= count;
1488
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001489 // add the list type to the type stack
1490 if (ga_grow(stack, 1) == FAIL)
1491 return FAIL;
1492 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1493 ++stack->ga_len;
1494
1495 return OK;
1496}
1497
1498/*
1499 * Generate an ISN_NEWDICT instruction.
1500 */
1501 static int
1502generate_NEWDICT(cctx_T *cctx, int count)
1503{
1504 isn_T *isn;
1505 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001506 type_T *type;
1507 type_T *member;
1508
Bram Moolenaar080457c2020-03-03 21:53:32 +01001509 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1511 return FAIL;
1512 isn->isn_arg.number = count;
1513
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001514 if (count == 0)
1515 member = &t_void;
1516 else
1517 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001518 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1519 cctx->ctx_type_list);
1520 type = get_dict_type(member, cctx->ctx_type_list);
1521
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522 // drop the key and value types
1523 stack->ga_len -= 2 * count;
1524
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001525 // add the dict type to the type stack
1526 if (ga_grow(stack, 1) == FAIL)
1527 return FAIL;
1528 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1529 ++stack->ga_len;
1530
1531 return OK;
1532}
1533
1534/*
1535 * Generate an ISN_FUNCREF instruction.
1536 */
1537 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001538generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001539{
1540 isn_T *isn;
1541 garray_T *stack = &cctx->ctx_type_stack;
1542
Bram Moolenaar080457c2020-03-03 21:53:32 +01001543 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1545 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001546 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001547 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001548
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001549 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001550 // the nested context, including this one.
1551 if (ufunc->uf_flags & FC_CLOSURE)
1552 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1553
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001554 if (ga_grow(stack, 1) == FAIL)
1555 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001556 ((type_T **)stack->ga_data)[stack->ga_len] =
1557 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 ++stack->ga_len;
1559
1560 return OK;
1561}
1562
1563/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001564 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001565 * "lambda_name" and "func_name" must be in allocated memory and will be
1566 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001567 */
1568 static int
1569generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1570{
1571 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001572
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001573 if (cctx->ctx_skip == SKIP_YES)
1574 {
1575 vim_free(lambda_name);
1576 vim_free(func_name);
1577 return OK;
1578 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001579 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001580 {
1581 vim_free(lambda_name);
1582 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001583 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001584 }
1585 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001586 isn->isn_arg.newfunc.nf_global = func_name;
1587
1588 return OK;
1589}
1590
1591/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001592 * Generate an ISN_DEF instruction: list functions
1593 */
1594 static int
1595generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1596{
1597 isn_T *isn;
1598
1599 RETURN_OK_IF_SKIP(cctx);
1600 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1601 return FAIL;
1602 if (len > 0)
1603 {
1604 isn->isn_arg.string = vim_strnsave(name, len);
1605 if (isn->isn_arg.string == NULL)
1606 return FAIL;
1607 }
1608 return OK;
1609}
1610
1611/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001612 * Generate an ISN_JUMP instruction.
1613 */
1614 static int
1615generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1616{
1617 isn_T *isn;
1618 garray_T *stack = &cctx->ctx_type_stack;
1619
Bram Moolenaar080457c2020-03-03 21:53:32 +01001620 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1622 return FAIL;
1623 isn->isn_arg.jump.jump_when = when;
1624 isn->isn_arg.jump.jump_where = where;
1625
1626 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1627 --stack->ga_len;
1628
1629 return OK;
1630}
1631
1632 static int
1633generate_FOR(cctx_T *cctx, int loop_idx)
1634{
1635 isn_T *isn;
1636 garray_T *stack = &cctx->ctx_type_stack;
1637
Bram Moolenaar080457c2020-03-03 21:53:32 +01001638 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001639 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1640 return FAIL;
1641 isn->isn_arg.forloop.for_idx = loop_idx;
1642
1643 if (ga_grow(stack, 1) == FAIL)
1644 return FAIL;
1645 // type doesn't matter, will be stored next
1646 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1647 ++stack->ga_len;
1648
1649 return OK;
1650}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001651/*
1652 * Generate an ISN_TRYCONT instruction.
1653 */
1654 static int
1655generate_TRYCONT(cctx_T *cctx, int levels, int where)
1656{
1657 isn_T *isn;
1658
1659 RETURN_OK_IF_SKIP(cctx);
1660 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1661 return FAIL;
1662 isn->isn_arg.trycont.tct_levels = levels;
1663 isn->isn_arg.trycont.tct_where = where;
1664
1665 return OK;
1666}
1667
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001668
1669/*
1670 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001671 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001672 * Return FAIL if the number of arguments is wrong.
1673 */
1674 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001675generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001676{
1677 isn_T *isn;
1678 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001679 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001680 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001681 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001682
Bram Moolenaar080457c2020-03-03 21:53:32 +01001683 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001684 argoff = check_internal_func(func_idx, argcount);
1685 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001686 return FAIL;
1687
Bram Moolenaar389df252020-07-09 21:20:47 +02001688 if (method_call && argoff > 1)
1689 {
1690 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1691 return FAIL;
1692 isn->isn_arg.shuffle.shfl_item = argcount;
1693 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1694 }
1695
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001696 if (argcount > 0)
1697 {
1698 // Check the types of the arguments.
1699 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001700 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1701 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001702 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001703 if (internal_func_is_map(func_idx))
1704 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001705 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001706
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1708 return FAIL;
1709 isn->isn_arg.bfunc.cbf_idx = func_idx;
1710 isn->isn_arg.bfunc.cbf_argcount = argcount;
1711
Bram Moolenaar94738d82020-10-21 14:25:07 +02001712 // Drop the argument types and push the return type.
1713 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001714 if (ga_grow(stack, 1) == FAIL)
1715 return FAIL;
1716 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001717 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001718 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001720 if (maptype != NULL && maptype->tt_member != NULL
1721 && maptype->tt_member != &t_any)
1722 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001723 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001724
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001725 return OK;
1726}
1727
1728/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001729 * Generate an ISN_LISTAPPEND instruction. Works like add().
1730 * Argument count is already checked.
1731 */
1732 static int
1733generate_LISTAPPEND(cctx_T *cctx)
1734{
1735 garray_T *stack = &cctx->ctx_type_stack;
1736 type_T *list_type;
1737 type_T *item_type;
1738 type_T *expected;
1739
1740 // Caller already checked that list_type is a list.
1741 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1742 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1743 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001744 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001745 return FAIL;
1746
1747 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1748 return FAIL;
1749
1750 --stack->ga_len; // drop the argument
1751 return OK;
1752}
1753
1754/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001755 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1756 * Argument count is already checked.
1757 */
1758 static int
1759generate_BLOBAPPEND(cctx_T *cctx)
1760{
1761 garray_T *stack = &cctx->ctx_type_stack;
1762 type_T *item_type;
1763
1764 // Caller already checked that blob_type is a blob.
1765 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001766 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001767 return FAIL;
1768
1769 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1770 return FAIL;
1771
1772 --stack->ga_len; // drop the argument
1773 return OK;
1774}
1775
1776/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001777 * Return TRUE if "ufunc" should be compiled, taking into account whether
1778 * "profile" indicates profiling is to be done.
1779 */
1780 int
Bram Moolenaarf002a412021-01-24 13:34:18 +01001781func_needs_compiling(ufunc_T *ufunc, int profile UNUSED)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001782{
1783 switch (ufunc->uf_def_status)
1784 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001785 case UF_NOT_COMPILED: break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001786 case UF_TO_BE_COMPILED: return TRUE;
1787 case UF_COMPILED:
1788 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001789#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001790 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1791 + ufunc->uf_dfunc_idx;
1792
1793 return profile ? dfunc->df_instr_prof == NULL
1794 : dfunc->df_instr == NULL;
Bram Moolenaarf002a412021-01-24 13:34:18 +01001795#else
1796 break;
1797#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01001798 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001799 case UF_COMPILING: break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001800 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001801 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001802}
1803
1804/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805 * Generate an ISN_DCALL or ISN_UCALL instruction.
1806 * Return FAIL if the number of arguments is wrong.
1807 */
1808 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001809generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810{
1811 isn_T *isn;
1812 garray_T *stack = &cctx->ctx_type_stack;
1813 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001814 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001815
Bram Moolenaar080457c2020-03-03 21:53:32 +01001816 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001817 if (argcount > regular_args && !has_varargs(ufunc))
1818 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001819 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820 return FAIL;
1821 }
1822 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1823 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001824 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825 return FAIL;
1826 }
1827
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001828 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001829 {
1830 int i;
1831
1832 for (i = 0; i < argcount; ++i)
1833 {
1834 type_T *expected;
1835 type_T *actual;
1836
1837 if (i < regular_args)
1838 {
1839 if (ufunc->uf_arg_types == NULL)
1840 continue;
1841 expected = ufunc->uf_arg_types[i];
1842 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001843 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1844 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001845 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001846 else
1847 expected = ufunc->uf_va_type->tt_member;
1848 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001849 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001850 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001851 {
1852 arg_type_mismatch(expected, actual, i + 1);
1853 return FAIL;
1854 }
1855 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001856 if (func_needs_compiling(ufunc, PROFILING(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001857 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001858 PROFILING(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001859 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001860 }
1861
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001862 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001863 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001864 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001865 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001866 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001867 {
1868 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1869 isn->isn_arg.dfunc.cdf_argcount = argcount;
1870 }
1871 else
1872 {
1873 // A user function may be deleted and redefined later, can't use the
1874 // ufunc pointer, need to look it up again at runtime.
1875 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1876 isn->isn_arg.ufunc.cuf_argcount = argcount;
1877 }
1878
1879 stack->ga_len -= argcount; // drop the arguments
1880 if (ga_grow(stack, 1) == FAIL)
1881 return FAIL;
1882 // add return value
1883 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1884 ++stack->ga_len;
1885
1886 return OK;
1887}
1888
1889/*
1890 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1891 */
1892 static int
1893generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1894{
1895 isn_T *isn;
1896 garray_T *stack = &cctx->ctx_type_stack;
1897
Bram Moolenaar080457c2020-03-03 21:53:32 +01001898 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001899 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1900 return FAIL;
1901 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1902 isn->isn_arg.ufunc.cuf_argcount = argcount;
1903
1904 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001905 if (ga_grow(stack, 1) == FAIL)
1906 return FAIL;
1907 // add return value
1908 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1909 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001910
1911 return OK;
1912}
1913
1914/*
1915 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001916 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917 */
1918 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001919generate_PCALL(
1920 cctx_T *cctx,
1921 int argcount,
1922 char_u *name,
1923 type_T *type,
1924 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925{
1926 isn_T *isn;
1927 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001928 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929
Bram Moolenaar080457c2020-03-03 21:53:32 +01001930 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001931
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001932 if (type->tt_type == VAR_ANY)
1933 ret_type = &t_any;
1934 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001935 {
1936 if (type->tt_argcount != -1)
1937 {
1938 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1939
1940 if (argcount < type->tt_min_argcount - varargs)
1941 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001942 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001943 return FAIL;
1944 }
1945 if (!varargs && argcount > type->tt_argcount)
1946 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001947 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001948 return FAIL;
1949 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001950 if (type->tt_args != NULL)
1951 {
1952 int i;
1953
1954 for (i = 0; i < argcount; ++i)
1955 {
1956 int offset = -argcount + i - 1;
1957 type_T *actual = ((type_T **)stack->ga_data)[
1958 stack->ga_len + offset];
1959 type_T *expected;
1960
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001961 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001962 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001963 type->tt_argcount - 1]->tt_member;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001964 else
1965 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001966 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001967 cctx, TRUE, FALSE) == FAIL)
1968 {
1969 arg_type_mismatch(expected, actual, i + 1);
1970 return FAIL;
1971 }
1972 }
1973 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001974 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001975 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001976 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001977 else
1978 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001979 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001980 return FAIL;
1981 }
1982
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1984 return FAIL;
1985 isn->isn_arg.pfunc.cpf_top = at_top;
1986 isn->isn_arg.pfunc.cpf_argcount = argcount;
1987
1988 stack->ga_len -= argcount; // drop the arguments
1989
1990 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001991 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001992
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001993 // If partial is above the arguments it must be cleared and replaced with
1994 // the return value.
1995 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1996 return FAIL;
1997
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001998 return OK;
1999}
2000
2001/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002002 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 */
2004 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002005generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006{
2007 isn_T *isn;
2008 garray_T *stack = &cctx->ctx_type_stack;
2009 type_T *type;
2010
Bram Moolenaar080457c2020-03-03 21:53:32 +01002011 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002012 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002014 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002015
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002016 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002018 if (type->tt_type != VAR_DICT && type != &t_any)
2019 {
2020 emsg(_(e_dictreq));
2021 return FAIL;
2022 }
2023 // change dict type to dict member type
2024 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002025 {
2026 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2027 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2028 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029
2030 return OK;
2031}
2032
2033/*
2034 * Generate an ISN_ECHO instruction.
2035 */
2036 static int
2037generate_ECHO(cctx_T *cctx, int with_white, int count)
2038{
2039 isn_T *isn;
2040
Bram Moolenaar080457c2020-03-03 21:53:32 +01002041 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2043 return FAIL;
2044 isn->isn_arg.echo.echo_with_white = with_white;
2045 isn->isn_arg.echo.echo_count = count;
2046
2047 return OK;
2048}
2049
Bram Moolenaarad39c092020-02-26 18:23:43 +01002050/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002051 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002052 */
2053 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002054generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002055{
2056 isn_T *isn;
2057
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002058 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002059 return FAIL;
2060 isn->isn_arg.number = count;
2061
2062 return OK;
2063}
2064
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002065/*
2066 * Generate an ISN_PUT instruction.
2067 */
2068 static int
2069generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2070{
2071 isn_T *isn;
2072
2073 RETURN_OK_IF_SKIP(cctx);
2074 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2075 return FAIL;
2076 isn->isn_arg.put.put_regname = regname;
2077 isn->isn_arg.put.put_lnum = lnum;
2078 return OK;
2079}
2080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002081 static int
2082generate_EXEC(cctx_T *cctx, char_u *line)
2083{
2084 isn_T *isn;
2085
Bram Moolenaar080457c2020-03-03 21:53:32 +01002086 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002087 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2088 return FAIL;
2089 isn->isn_arg.string = vim_strsave(line);
2090 return OK;
2091}
2092
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002093 static int
2094generate_EXECCONCAT(cctx_T *cctx, int count)
2095{
2096 isn_T *isn;
2097
2098 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2099 return FAIL;
2100 isn->isn_arg.number = count;
2101 return OK;
2102}
2103
Bram Moolenaar08597872020-12-10 19:43:40 +01002104/*
2105 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2106 */
2107 static int
2108generate_RANGE(cctx_T *cctx, char_u *range)
2109{
2110 isn_T *isn;
2111 garray_T *stack = &cctx->ctx_type_stack;
2112
2113 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2114 return FAIL;
2115 isn->isn_arg.string = range;
2116
2117 if (ga_grow(stack, 1) == FAIL)
2118 return FAIL;
2119 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2120 ++stack->ga_len;
2121 return OK;
2122}
2123
Bram Moolenaar792f7862020-11-23 08:31:18 +01002124 static int
2125generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2126{
2127 isn_T *isn;
2128
2129 RETURN_OK_IF_SKIP(cctx);
2130 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2131 return FAIL;
2132 isn->isn_arg.unpack.unp_count = var_count;
2133 isn->isn_arg.unpack.unp_semicolon = semicolon;
2134 return OK;
2135}
2136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002137/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002138 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002139 */
2140 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002141generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002142{
2143 isn_T *isn;
2144
Bram Moolenaar02194d22020-10-24 23:08:38 +02002145 if (cmod->cmod_flags != 0
2146 || cmod->cmod_split != 0
2147 || cmod->cmod_verbose != 0
2148 || cmod->cmod_tab != 0
2149 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002150 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002151 cctx->ctx_has_cmdmod = TRUE;
2152
2153 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002154 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002155 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2156 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2157 return FAIL;
2158 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002159 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002160 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002161 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002162
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002163 return OK;
2164}
2165
2166 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002167generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002168{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002169 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2170 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002171 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002172 return OK;
2173}
2174
Bram Moolenaara91a7132021-03-25 21:12:15 +01002175/*
2176 * If an ISN_CMDMOD was just generated drop it.
2177 */
2178 static void
2179drop_cmdmod(cctx_T *cctx)
2180{
2181 garray_T *instr = &cctx->ctx_instr;
2182
2183 // Drop any CMDMOD instruction
2184 if (cctx->ctx_has_cmdmod
2185 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2186 == ISN_CMDMOD)
2187 {
2188 --instr->ga_len;
2189 cctx->ctx_has_cmdmod = FALSE;
2190 }
2191}
2192
2193/*
2194 * Get the index of the current instruction.
2195 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2196 */
2197 static int
2198current_instr_idx(cctx_T *cctx)
2199{
2200 garray_T *instr = &cctx->ctx_instr;
2201 int idx = instr->ga_len;
2202
2203 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
2204 .isn_type == ISN_CMDMOD)
2205 --idx;
2206#ifdef FEAT_PROFILE
2207 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[idx - 1]
2208 .isn_type == ISN_PROF_START)
2209 --idx;
2210#endif
2211 return idx;
2212}
2213
Bram Moolenaarf002a412021-01-24 13:34:18 +01002214#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002215 static void
2216may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2217{
2218 if (cctx->ctx_profiling && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002219 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002220}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002221#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002222
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002223/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002224 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002225 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002226 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002227 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002228reserve_local(
2229 cctx_T *cctx,
2230 char_u *name,
2231 size_t len,
2232 int isConst,
2233 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002234{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002235 lvar_T *lvar;
2236
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002237 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002238 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002239 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002240 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002241 }
2242
2243 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002244 return NULL;
2245 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002246 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002247
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002248 // Every local variable uses the next entry on the stack. We could re-use
2249 // the last ones when leaving a scope, but then variables used in a closure
2250 // might get overwritten. To keep things simple do not re-use stack
2251 // entries. This is less efficient, but memory is cheap these days.
2252 lvar->lv_idx = cctx->ctx_locals_count++;
2253
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002254 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002255 lvar->lv_const = isConst;
2256 lvar->lv_type = type;
2257
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002258 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002259}
2260
2261/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002262 * Remove local variables above "new_top".
2263 */
2264 static void
2265unwind_locals(cctx_T *cctx, int new_top)
2266{
2267 if (cctx->ctx_locals.ga_len > new_top)
2268 {
2269 int idx;
2270 lvar_T *lvar;
2271
2272 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2273 {
2274 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2275 vim_free(lvar->lv_name);
2276 }
2277 }
2278 cctx->ctx_locals.ga_len = new_top;
2279}
2280
2281/*
2282 * Free all local variables.
2283 */
2284 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002285free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002286{
2287 unwind_locals(cctx, 0);
2288 ga_clear(&cctx->ctx_locals);
2289}
2290
2291/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002292 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2293 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2294 * error if the variable was defined with :const.
2295 */
2296 static int
2297check_item_writable(svar_T *sv, int check_writable, char_u *name)
2298{
2299 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2300 || (check_writable == ASSIGN_FINAL
2301 && sv->sv_const == ASSIGN_CONST))
2302 {
2303 semsg(_(e_readonlyvar), name);
2304 return FAIL;
2305 }
2306 return OK;
2307}
2308
2309/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002310 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002311 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002312 * Returns the index in "sn_var_vals" if found.
2313 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002314 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002315 */
2316 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002317get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002318{
2319 hashtab_T *ht;
2320 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002321 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002322 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002323 int idx;
2324
Bram Moolenaare3d46852020-08-29 13:39:17 +02002325 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002326 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002327 if (sid == current_sctx.sc_sid)
2328 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002329 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002330
2331 if (sav == NULL)
2332 return -2;
2333 idx = sav->sav_var_vals_idx;
2334 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002335 if (check_item_writable(sv, check_writable, name) == FAIL)
2336 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002337 return idx;
2338 }
2339
2340 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002341 ht = &SCRIPT_VARS(sid);
2342 di = find_var_in_ht(ht, 0, name, TRUE);
2343 if (di == NULL)
2344 return -2;
2345
2346 // Now find the svar_T index in sn_var_vals.
2347 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2348 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002349 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002350 if (sv->sv_tv == &di->di_tv)
2351 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002352 if (check_item_writable(sv, check_writable, name) == FAIL)
2353 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002354 return idx;
2355 }
2356 }
2357 return -1;
2358}
2359
2360/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002361 * Find "name" in imported items of the current script or in "cctx" if not
2362 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002363 */
2364 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002365find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002366{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002367 int idx;
2368
Bram Moolenaare3d46852020-08-29 13:39:17 +02002369 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002370 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002371 if (cctx != NULL)
2372 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2373 {
2374 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2375 + idx;
2376
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002377 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2378 : STRLEN(import->imp_name) == len
2379 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002380 return import;
2381 }
2382
Bram Moolenaarefa94442020-08-08 22:16:00 +02002383 return find_imported_in_script(name, len, current_sctx.sc_sid);
2384}
2385
2386 imported_T *
2387find_imported_in_script(char_u *name, size_t len, int sid)
2388{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002389 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002390 int idx;
2391
Bram Moolenaare3d46852020-08-29 13:39:17 +02002392 if (!SCRIPT_ID_VALID(sid))
2393 return NULL;
2394 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2396 {
2397 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2398
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002399 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2400 : STRLEN(import->imp_name) == len
2401 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402 return import;
2403 }
2404 return NULL;
2405}
2406
2407/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002408 * Free all imported variables.
2409 */
2410 static void
2411free_imported(cctx_T *cctx)
2412{
2413 int idx;
2414
2415 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2416 {
2417 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2418
2419 vim_free(import->imp_name);
2420 }
2421 ga_clear(&cctx->ctx_imports);
2422}
2423
2424/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002425 * Return a pointer to the next line that isn't empty or only contains a
2426 * comment. Skips over white space.
2427 * Returns NULL if there is none.
2428 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002429 char_u *
2430peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002431{
2432 int lnum = cctx->ctx_lnum;
2433
2434 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2435 {
2436 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002437 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002438
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002439 // ignore NULLs inserted for continuation lines
2440 if (line != NULL)
2441 {
2442 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002443 if (vim9_bad_comment(p))
2444 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002445 if (*p != NUL && !vim9_comment_start(p))
2446 return p;
2447 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002448 }
2449 return NULL;
2450}
2451
2452/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002453 * Called when checking for a following operator at "arg". When the rest of
2454 * the line is empty or only a comment, peek the next line. If there is a next
2455 * line return a pointer to it and set "nextp".
2456 * Otherwise skip over white space.
2457 */
2458 static char_u *
2459may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2460{
2461 char_u *p = skipwhite(arg);
2462
2463 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002464 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002465 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002466 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002467 if (*nextp != NULL)
2468 return *nextp;
2469 }
2470 return p;
2471}
2472
2473/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002474 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002475 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002476 * Returns NULL when at the end.
2477 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002478 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002479next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002480{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002481 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002482
2483 do
2484 {
2485 ++cctx->ctx_lnum;
2486 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002487 {
2488 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002489 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002490 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002491 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002492 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002493 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002494 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002495 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002496 return line;
2497}
2498
2499/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002500 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002501 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002502 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002503 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2504 */
2505 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002506may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002507{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002508 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002509 if (vim9_bad_comment(*arg))
2510 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002511 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002512 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002513 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002514
2515 if (next == NULL)
2516 return FAIL;
2517 *arg = skipwhite(next);
2518 }
2519 return OK;
2520}
2521
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002522/*
2523 * Idem, and give an error when failed.
2524 */
2525 static int
2526may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2527{
2528 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2529 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002530 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002531 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002532 return FAIL;
2533 }
2534 return OK;
2535}
2536
2537
Bram Moolenaara5565e42020-05-09 15:44:01 +02002538// Structure passed between the compile_expr* functions to keep track of
2539// constants that have been parsed but for which no code was produced yet. If
2540// possible expressions on these constants are applied at compile time. If
2541// that is not possible, the code to push the constants needs to be generated
2542// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002543// Using 50 should be more than enough of 5 levels of ().
2544#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002545typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002546 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002547 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002548 int pp_is_const; // all generated code was constants, used for a
2549 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002550} ppconst_T;
2551
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002552static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002553static int compile_expr0(char_u **arg, cctx_T *cctx);
2554static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2555
Bram Moolenaara5565e42020-05-09 15:44:01 +02002556/*
2557 * Generate a PUSH instruction for "tv".
2558 * "tv" will be consumed or cleared.
2559 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2560 */
2561 static int
2562generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2563{
2564 if (tv != NULL)
2565 {
2566 switch (tv->v_type)
2567 {
2568 case VAR_UNKNOWN:
2569 break;
2570 case VAR_BOOL:
2571 generate_PUSHBOOL(cctx, tv->vval.v_number);
2572 break;
2573 case VAR_SPECIAL:
2574 generate_PUSHSPEC(cctx, tv->vval.v_number);
2575 break;
2576 case VAR_NUMBER:
2577 generate_PUSHNR(cctx, tv->vval.v_number);
2578 break;
2579#ifdef FEAT_FLOAT
2580 case VAR_FLOAT:
2581 generate_PUSHF(cctx, tv->vval.v_float);
2582 break;
2583#endif
2584 case VAR_BLOB:
2585 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2586 tv->vval.v_blob = NULL;
2587 break;
2588 case VAR_STRING:
2589 generate_PUSHS(cctx, tv->vval.v_string);
2590 tv->vval.v_string = NULL;
2591 break;
2592 default:
2593 iemsg("constant type not supported");
2594 clear_tv(tv);
2595 return FAIL;
2596 }
2597 tv->v_type = VAR_UNKNOWN;
2598 }
2599 return OK;
2600}
2601
2602/*
2603 * Generate code for any ppconst entries.
2604 */
2605 static int
2606generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2607{
2608 int i;
2609 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002610 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002611
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002612 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002613 for (i = 0; i < ppconst->pp_used; ++i)
2614 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2615 ret = FAIL;
2616 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002617 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002618 return ret;
2619}
2620
2621/*
2622 * Clear ppconst constants. Used when failing.
2623 */
2624 static void
2625clear_ppconst(ppconst_T *ppconst)
2626{
2627 int i;
2628
2629 for (i = 0; i < ppconst->pp_used; ++i)
2630 clear_tv(&ppconst->pp_tv[i]);
2631 ppconst->pp_used = 0;
2632}
2633
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002634/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002635 * Generate an instruction to load script-local variable "name", without the
2636 * leading "s:".
2637 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002638 */
2639 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002640compile_load_scriptvar(
2641 cctx_T *cctx,
2642 char_u *name, // variable NUL terminated
2643 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002644 char_u **end, // end of variable
2645 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002647 scriptitem_T *si;
2648 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649 imported_T *import;
2650
Bram Moolenaare3d46852020-08-29 13:39:17 +02002651 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2652 return FAIL;
2653 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002654 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002655 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002656 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002657 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002658 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2659 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660 }
2661 if (idx >= 0)
2662 {
2663 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2664
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002665 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666 current_sctx.sc_sid, idx, sv->sv_type);
2667 return OK;
2668 }
2669
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002670 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002671 if (import != NULL)
2672 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002673 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002674 {
2675 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002676 char_u *exp_name;
2677 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002678 ufunc_T *ufunc;
2679 type_T *type;
2680
2681 // Used "import * as Name", need to lookup the member.
2682 if (*p != '.')
2683 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002684 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002685 return FAIL;
2686 }
2687 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002688 if (VIM_ISWHITE(*p))
2689 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002690 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002691 return FAIL;
2692 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002693
Bram Moolenaar1c991142020-07-04 13:15:31 +02002694 // isolate one name
2695 exp_name = p;
2696 while (eval_isnamec(*p))
2697 ++p;
2698 cc = *p;
2699 *p = NUL;
2700
Bram Moolenaaredba7072021-03-13 21:14:18 +01002701 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2702 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002703 *p = cc;
2704 p = skipwhite(p);
2705
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002706 // TODO: what if it is a function?
2707 if (idx < 0)
2708 return FAIL;
2709 *end = p;
2710
2711 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2712 import->imp_sid,
2713 idx,
2714 type);
2715 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002716 else if (import->imp_funcname != NULL)
2717 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002718 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002719 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2720 import->imp_sid,
2721 import->imp_var_vals_idx,
2722 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002723 return OK;
2724 }
2725
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002726 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002727 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 return FAIL;
2729}
2730
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002731 static int
2732generate_funcref(cctx_T *cctx, char_u *name)
2733{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002734 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002735
2736 if (ufunc == NULL)
2737 return FAIL;
2738
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002739 // Need to compile any default values to get the argument types.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01002740 if (func_needs_compiling(ufunc, PROFILING(ufunc))
2741 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002742 == FAIL)
2743 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002744 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002745}
2746
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002747/*
2748 * Compile a variable name into a load instruction.
2749 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002750 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 * When "error" is FALSE do not give an error when not found.
2752 */
2753 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002754compile_load(
2755 char_u **arg,
2756 char_u *end_arg,
2757 cctx_T *cctx,
2758 int is_expr,
2759 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760{
2761 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002762 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002763 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002765 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766
2767 if (*(*arg + 1) == ':')
2768 {
2769 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002770 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002771 {
2772 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002774 switch (**arg)
2775 {
2776 case 'g': isn_type = ISN_LOADGDICT; break;
2777 case 'w': isn_type = ISN_LOADWDICT; break;
2778 case 't': isn_type = ISN_LOADTDICT; break;
2779 case 'b': isn_type = ISN_LOADBDICT; break;
2780 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002781 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002782 goto theend;
2783 }
2784 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2785 goto theend;
2786 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002787 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 else
2789 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002790 isntype_T isn_type = ISN_DROP;
2791
2792 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2793 if (name == NULL)
2794 return FAIL;
2795
2796 switch (**arg)
2797 {
2798 case 'v': res = generate_LOADV(cctx, name, error);
2799 break;
2800 case 's': res = compile_load_scriptvar(cctx, name,
2801 NULL, NULL, error);
2802 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002803 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2804 isn_type = ISN_LOADG;
2805 else
2806 {
2807 isn_type = ISN_LOADAUTO;
2808 vim_free(name);
2809 name = vim_strnsave(*arg, end - *arg);
2810 if (name == NULL)
2811 return FAIL;
2812 }
2813 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002814 case 'w': isn_type = ISN_LOADW; break;
2815 case 't': isn_type = ISN_LOADT; break;
2816 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002817 default: // cannot happen, just in case
2818 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002819 goto theend;
2820 }
2821 if (isn_type != ISN_DROP)
2822 {
2823 // Global, Buffer-local, Window-local and Tabpage-local
2824 // variables can be defined later, thus we don't check if it
2825 // exists, give error at runtime.
2826 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2827 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 }
2829 }
2830 else
2831 {
2832 size_t len = end - *arg;
2833 int idx;
2834 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002835 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002836
2837 name = vim_strnsave(*arg, end - *arg);
2838 if (name == NULL)
2839 return FAIL;
2840
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002841 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002843 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002844 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845 }
2846 else
2847 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002848 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002849
Bram Moolenaar709664c2020-12-12 14:33:41 +01002850 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002852 type = lvar.lv_type;
2853 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01002854 if (lvar.lv_from_outer != 0)
2855 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002856 else
2857 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 }
2859 else
2860 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002861 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002862 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002863 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002864 || find_imported(name, 0, cctx) != NULL)
2865 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002866
Bram Moolenaar0f769812020-09-12 18:32:34 +02002867 // When evaluating an expression and the name starts with an
2868 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002869 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002870 if (res == FAIL && is_expr
2871 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002872 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873 }
2874 }
2875 if (gen_load)
2876 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01002877 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002878 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002879 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002880 cctx->ctx_outer_used = TRUE;
2881 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 }
2883
2884 *arg = end;
2885
2886theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002887 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002888 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889 vim_free(name);
2890 return res;
2891}
2892
2893/*
2894 * Compile the argument expressions.
2895 * "arg" points to just after the "(" and is advanced to after the ")"
2896 */
2897 static int
2898compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2899{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002900 char_u *p = *arg;
2901 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002902 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002903
Bram Moolenaare6085c52020-04-12 20:19:16 +02002904 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002905 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002906 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2907 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002908 if (*p == ')')
2909 {
2910 *arg = p + 1;
2911 return OK;
2912 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002913 if (must_end)
2914 {
2915 semsg(_(e_missing_comma_before_argument_str), p);
2916 return FAIL;
2917 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002918
Bram Moolenaara5565e42020-05-09 15:44:01 +02002919 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 return FAIL;
2921 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002922
2923 if (*p != ',' && *skipwhite(p) == ',')
2924 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01002925 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002926 p = skipwhite(p);
2927 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002929 {
2930 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002931 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01002932 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002933 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002934 else
2935 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002936 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002937 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002939failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002940 emsg(_(e_missing_close));
2941 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942}
2943
2944/*
2945 * Compile a function call: name(arg1, arg2)
2946 * "arg" points to "name", "arg + varlen" to the "(".
2947 * "argcount_init" is 1 for "value->method()"
2948 * Instructions:
2949 * EVAL arg1
2950 * EVAL arg2
2951 * BCALL / DCALL / UCALL
2952 */
2953 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002954compile_call(
2955 char_u **arg,
2956 size_t varlen,
2957 cctx_T *cctx,
2958 ppconst_T *ppconst,
2959 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960{
2961 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002962 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 int argcount = argcount_init;
2964 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002965 char_u fname_buf[FLEN_FIXED + 1];
2966 char_u *tofree = NULL;
2967 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002968 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002969 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002970 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971
Bram Moolenaara5565e42020-05-09 15:44:01 +02002972 // we can evaluate "has('name')" at compile time
2973 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2974 {
2975 char_u *s = skipwhite(*arg + varlen + 1);
2976 typval_T argvars[2];
2977
2978 argvars[0].v_type = VAR_UNKNOWN;
2979 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002980 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002981 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002982 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002983 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002984 if (*s == ')' && argvars[0].v_type == VAR_STRING
2985 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002986 {
2987 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2988
2989 *arg = s + 1;
2990 argvars[1].v_type = VAR_UNKNOWN;
2991 tv->v_type = VAR_NUMBER;
2992 tv->vval.v_number = 0;
2993 f_has(argvars, tv);
2994 clear_tv(&argvars[0]);
2995 ++ppconst->pp_used;
2996 return OK;
2997 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002998 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002999 }
3000
3001 if (generate_ppconst(cctx, ppconst) == FAIL)
3002 return FAIL;
3003
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 if (varlen >= sizeof(namebuf))
3005 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003006 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 return FAIL;
3008 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003009 vim_strncpy(namebuf, *arg, varlen);
3010 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011
3012 *arg = skipwhite(*arg + varlen + 1);
3013 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003014 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015
Bram Moolenaar03290b82020-12-19 16:30:44 +01003016 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003017 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018 {
3019 int idx;
3020
3021 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003022 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003024 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003025 if (STRCMP(name, "flatten") == 0)
3026 {
3027 emsg(_(e_cannot_use_flatten_in_vim9_script));
3028 goto theend;
3029 }
3030
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003031 if (STRCMP(name, "add") == 0 && argcount == 2)
3032 {
3033 garray_T *stack = &cctx->ctx_type_stack;
3034 type_T *type = ((type_T **)stack->ga_data)[
3035 stack->ga_len - 2];
3036
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003037 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003038 if (type->tt_type == VAR_LIST)
3039 {
3040 // inline "add(list, item)" so that the type can be checked
3041 res = generate_LISTAPPEND(cctx);
3042 idx = -1;
3043 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003044 else if (type->tt_type == VAR_BLOB)
3045 {
3046 // inline "add(blob, nr)" so that the type can be checked
3047 res = generate_BLOBAPPEND(cctx);
3048 idx = -1;
3049 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003050 }
3051
3052 if (idx >= 0)
3053 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3054 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003055 else
3056 semsg(_(e_unknownfunc), namebuf);
3057 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 }
3059
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003060 // An argument or local variable can be a function reference, this
3061 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003062 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003063 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003064 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003065 // If we can find the function by name generate the right call.
3066 // Skip global functions here, a local funcref takes precedence.
3067 ufunc = find_func(name, FALSE, cctx);
3068 if (ufunc != NULL && !func_is_global(ufunc))
3069 {
3070 res = generate_CALL(cctx, ufunc, argcount);
3071 goto theend;
3072 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003073 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074
3075 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003076 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003077 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003079 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003080 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003081 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003082 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003083 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003084
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003085 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003086 goto theend;
3087 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088
Bram Moolenaar0f769812020-09-12 18:32:34 +02003089 // If we can find a global function by name generate the right call.
3090 if (ufunc != NULL)
3091 {
3092 res = generate_CALL(cctx, ufunc, argcount);
3093 goto theend;
3094 }
3095
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003096 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003097 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003098 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003099 res = generate_UCALL(cctx, name, argcount);
3100 else
3101 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003102
3103theend:
3104 vim_free(tofree);
3105 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106}
3107
3108// like NAMESPACE_CHAR but with 'a' and 'l'.
3109#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3110
3111/*
3112 * Find the end of a variable or function name. Unlike find_name_end() this
3113 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003114 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 * Return a pointer to just after the name. Equal to "arg" if there is no
3116 * valid name.
3117 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003118 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003119to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120{
3121 char_u *p;
3122
3123 // Quick check for valid starting character.
3124 if (!eval_isnamec1(*arg))
3125 return arg;
3126
3127 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3128 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3129 // and can be used in slice "[n:]".
3130 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003131 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3133 break;
3134 return p;
3135}
3136
3137/*
3138 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003139 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140 */
3141 char_u *
3142to_name_const_end(char_u *arg)
3143{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003144 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 typval_T rettv;
3146
3147 if (p == arg && *arg == '[')
3148 {
3149
3150 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003151 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 p = arg;
3153 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154 return p;
3155}
3156
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157/*
3158 * parse a list: [expr, expr]
3159 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003160 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 */
3162 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003163compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164{
3165 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003166 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003168 int is_const;
3169 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003171 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003173 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003174 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003175 semsg(_(e_list_end), *arg);
3176 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003177 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003178 if (*p == ',')
3179 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003180 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003181 return FAIL;
3182 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003183 if (*p == ']')
3184 {
3185 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003186 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003187 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003188 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003189 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003190 if (!is_const)
3191 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 ++count;
3193 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003194 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003195 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003196 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3197 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003198 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003199 return FAIL;
3200 }
3201 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003202 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 p = skipwhite(p);
3204 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003205 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003207 ppconst->pp_is_const = is_all_const;
3208 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209}
3210
3211/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003212 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003213 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003214 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 */
3216 static int
3217compile_lambda(char_u **arg, cctx_T *cctx)
3218{
Bram Moolenaare462f522020-12-27 14:43:30 +01003219 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 typval_T rettv;
3221 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003222 evalarg_T evalarg;
3223
3224 CLEAR_FIELD(evalarg);
3225 evalarg.eval_flags = EVAL_EVALUATE;
3226 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003227
3228 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003229 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3230 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003231 {
3232 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003233 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003234 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003235
Bram Moolenaar65c44152020-12-24 15:14:01 +01003236 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003238 ++ufunc->uf_refcount;
3239 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240
Bram Moolenaar65c44152020-12-24 15:14:01 +01003241 // Compile the function into instructions.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01003242 compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003244 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3245 // points into it. Point to the original line to avoid a dangling pointer.
3246 if (evalarg.eval_tofree_cmdline != NULL)
3247 {
3248 size_t off = *arg - evalarg.eval_tofree_cmdline;
3249
3250 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3251 + off;
3252 }
3253
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003254 clear_evalarg(&evalarg, NULL);
3255
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003256 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003257 {
3258 // The return type will now be known.
3259 set_function_type(ufunc);
3260
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003261 // The function reference count will be 1. When the ISN_FUNCREF
3262 // instruction is deleted the reference count is decremented and the
3263 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003264 return generate_FUNCREF(cctx, ufunc);
3265 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003266
3267 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268 return FAIL;
3269}
3270
3271/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003272 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003274 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 */
3276 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003277compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278{
3279 garray_T *instr = &cctx->ctx_instr;
3280 int count = 0;
3281 dict_T *d = dict_alloc();
3282 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003283 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003284 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003285 int is_const;
3286 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003287
3288 if (d == NULL)
3289 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003290 if (generate_ppconst(cctx, ppconst) == FAIL)
3291 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003292 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003294 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295
Bram Moolenaar23c55272020-06-21 16:58:13 +02003296 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003297 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003298 *arg = NULL;
3299 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003300 }
3301
3302 if (**arg == '}')
3303 break;
3304
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003305 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003307 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003309 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003310 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003311 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003313 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003314 if (isn->isn_type == ISN_PUSHNR)
3315 {
3316 char buf[NUMBUFLEN];
3317
3318 // Convert to string at compile time.
3319 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3320 isn->isn_type = ISN_PUSHS;
3321 isn->isn_arg.string = vim_strsave((char_u *)buf);
3322 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003323 if (isn->isn_type == ISN_PUSHS)
3324 key = isn->isn_arg.string;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003325 else if (may_generate_2STRING(-1, cctx) == FAIL)
3326 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003327 *arg = skipwhite(*arg);
3328 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003329 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003330 emsg(_(e_missing_matching_bracket_after_dict_key));
3331 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003332 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003333 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003334 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003335 else
3336 {
3337 // {"name": value},
3338 // {'name': value},
3339 // {name: value} use "name" as a literal key
3340 key = get_literal_key(arg);
3341 if (key == NULL)
3342 return FAIL;
3343 if (generate_PUSHS(cctx, key) == FAIL)
3344 return FAIL;
3345 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346
3347 // Check for duplicate keys, if using string keys.
3348 if (key != NULL)
3349 {
3350 item = dict_find(d, key, -1);
3351 if (item != NULL)
3352 {
3353 semsg(_(e_duplicate_key), key);
3354 goto failret;
3355 }
3356 item = dictitem_alloc(key);
3357 if (item != NULL)
3358 {
3359 item->di_tv.v_type = VAR_UNKNOWN;
3360 item->di_tv.v_lock = 0;
3361 if (dict_add(d, item) == FAIL)
3362 dictitem_free(item);
3363 }
3364 }
3365
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003366 if (**arg != ':')
3367 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003368 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003369 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003370 else
3371 semsg(_(e_missing_dict_colon), *arg);
3372 return FAIL;
3373 }
3374 whitep = *arg + 1;
3375 if (!IS_WHITE_OR_NUL(*whitep))
3376 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003377 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003378 return FAIL;
3379 }
3380
Bram Moolenaar23c55272020-06-21 16:58:13 +02003381 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003382 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003383 *arg = NULL;
3384 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003385 }
3386
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003387 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003389 if (!is_const)
3390 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 ++count;
3392
Bram Moolenaar2c330432020-04-13 14:41:35 +02003393 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003394 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003395 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003396 *arg = NULL;
3397 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003398 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 if (**arg == '}')
3400 break;
3401 if (**arg != ',')
3402 {
3403 semsg(_(e_missing_dict_comma), *arg);
3404 goto failret;
3405 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003406 if (IS_WHITE_OR_NUL(*whitep))
3407 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003408 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003409 return FAIL;
3410 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003411 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003412 if (!IS_WHITE_OR_NUL(*whitep))
3413 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003414 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003415 return FAIL;
3416 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003417 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418 }
3419
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 *arg = *arg + 1;
3421
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003422 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003423 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003424 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003425 *arg += STRLEN(*arg);
3426
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003428 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429 return generate_NEWDICT(cctx, count);
3430
3431failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003432 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003433 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003434 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003435 *arg = (char_u *)"";
3436 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 dict_unref(d);
3438 return FAIL;
3439}
3440
3441/*
3442 * Compile "&option".
3443 */
3444 static int
3445compile_get_option(char_u **arg, cctx_T *cctx)
3446{
3447 typval_T rettv;
3448 char_u *start = *arg;
3449 int ret;
3450
3451 // parse the option and get the current value to get the type.
3452 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003453 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454 if (ret == OK)
3455 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003456 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003457 char_u *name = vim_strnsave(start, *arg - start);
3458 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3459 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460
3461 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3462 vim_free(name);
3463 }
3464 clear_tv(&rettv);
3465
3466 return ret;
3467}
3468
3469/*
3470 * Compile "$VAR".
3471 */
3472 static int
3473compile_get_env(char_u **arg, cctx_T *cctx)
3474{
3475 char_u *start = *arg;
3476 int len;
3477 int ret;
3478 char_u *name;
3479
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003480 ++*arg;
3481 len = get_env_len(arg);
3482 if (len == 0)
3483 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003484 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 return FAIL;
3486 }
3487
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003488 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489 name = vim_strnsave(start, len + 1);
3490 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3491 vim_free(name);
3492 return ret;
3493}
3494
3495/*
3496 * Compile "@r".
3497 */
3498 static int
3499compile_get_register(char_u **arg, cctx_T *cctx)
3500{
3501 int ret;
3502
3503 ++*arg;
3504 if (**arg == NUL)
3505 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003506 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507 return FAIL;
3508 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003509 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510 {
3511 emsg_invreg(**arg);
3512 return FAIL;
3513 }
3514 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3515 ++*arg;
3516 return ret;
3517}
3518
3519/*
3520 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003521 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003522 */
3523 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003524apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003525{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003526 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003527
3528 // this works from end to start
3529 while (p > start)
3530 {
3531 --p;
3532 if (*p == '-' || *p == '+')
3533 {
3534 // only '-' has an effect, for '+' we only check the type
3535#ifdef FEAT_FLOAT
3536 if (rettv->v_type == VAR_FLOAT)
3537 {
3538 if (*p == '-')
3539 rettv->vval.v_float = -rettv->vval.v_float;
3540 }
3541 else
3542#endif
3543 {
3544 varnumber_T val;
3545 int error = FALSE;
3546
3547 // tv_get_number_chk() accepts a string, but we don't want that
3548 // here
3549 if (check_not_string(rettv) == FAIL)
3550 return FAIL;
3551 val = tv_get_number_chk(rettv, &error);
3552 clear_tv(rettv);
3553 if (error)
3554 return FAIL;
3555 if (*p == '-')
3556 val = -val;
3557 rettv->v_type = VAR_NUMBER;
3558 rettv->vval.v_number = val;
3559 }
3560 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003561 else if (numeric_only)
3562 {
3563 ++p;
3564 break;
3565 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003566 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003567 {
3568 int v = tv2bool(rettv);
3569
3570 // '!' is permissive in the type.
3571 clear_tv(rettv);
3572 rettv->v_type = VAR_BOOL;
3573 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3574 }
3575 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003576 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 return OK;
3578}
3579
3580/*
3581 * Recognize v: variables that are constants and set "rettv".
3582 */
3583 static void
3584get_vim_constant(char_u **arg, typval_T *rettv)
3585{
3586 if (STRNCMP(*arg, "v:true", 6) == 0)
3587 {
3588 rettv->v_type = VAR_BOOL;
3589 rettv->vval.v_number = VVAL_TRUE;
3590 *arg += 6;
3591 }
3592 else if (STRNCMP(*arg, "v:false", 7) == 0)
3593 {
3594 rettv->v_type = VAR_BOOL;
3595 rettv->vval.v_number = VVAL_FALSE;
3596 *arg += 7;
3597 }
3598 else if (STRNCMP(*arg, "v:null", 6) == 0)
3599 {
3600 rettv->v_type = VAR_SPECIAL;
3601 rettv->vval.v_number = VVAL_NULL;
3602 *arg += 6;
3603 }
3604 else if (STRNCMP(*arg, "v:none", 6) == 0)
3605 {
3606 rettv->v_type = VAR_SPECIAL;
3607 rettv->vval.v_number = VVAL_NONE;
3608 *arg += 6;
3609 }
3610}
3611
Bram Moolenaar657137c2021-01-09 15:45:23 +01003612 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003613get_compare_type(char_u *p, int *len, int *type_is)
3614{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003615 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003616 int i;
3617
3618 switch (p[0])
3619 {
3620 case '=': if (p[1] == '=')
3621 type = EXPR_EQUAL;
3622 else if (p[1] == '~')
3623 type = EXPR_MATCH;
3624 break;
3625 case '!': if (p[1] == '=')
3626 type = EXPR_NEQUAL;
3627 else if (p[1] == '~')
3628 type = EXPR_NOMATCH;
3629 break;
3630 case '>': if (p[1] != '=')
3631 {
3632 type = EXPR_GREATER;
3633 *len = 1;
3634 }
3635 else
3636 type = EXPR_GEQUAL;
3637 break;
3638 case '<': if (p[1] != '=')
3639 {
3640 type = EXPR_SMALLER;
3641 *len = 1;
3642 }
3643 else
3644 type = EXPR_SEQUAL;
3645 break;
3646 case 'i': if (p[1] == 's')
3647 {
3648 // "is" and "isnot"; but not a prefix of a name
3649 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3650 *len = 5;
3651 i = p[*len];
3652 if (!isalnum(i) && i != '_')
3653 {
3654 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3655 *type_is = TRUE;
3656 }
3657 }
3658 break;
3659 }
3660 return type;
3661}
3662
3663/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003664 * Skip over an expression, ignoring most errors.
3665 */
3666 static void
3667skip_expr_cctx(char_u **arg, cctx_T *cctx)
3668{
3669 evalarg_T evalarg;
3670
3671 CLEAR_FIELD(evalarg);
3672 evalarg.eval_cctx = cctx;
3673 skip_expr(arg, &evalarg);
3674}
3675
3676/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003678 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 */
3680 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003681compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003683 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684
3685 // this works from end to start
3686 while (p > start)
3687 {
3688 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003689 while (VIM_ISWHITE(*p))
3690 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 if (*p == '-' || *p == '+')
3692 {
3693 int negate = *p == '-';
3694 isn_T *isn;
3695
3696 // TODO: check type
3697 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3698 {
3699 --p;
3700 if (*p == '-')
3701 negate = !negate;
3702 }
3703 // only '-' has an effect, for '+' we only check the type
3704 if (negate)
3705 isn = generate_instr(cctx, ISN_NEGATENR);
3706 else
3707 isn = generate_instr(cctx, ISN_CHECKNR);
3708 if (isn == NULL)
3709 return FAIL;
3710 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003711 else if (numeric_only)
3712 {
3713 ++p;
3714 break;
3715 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003716 else
3717 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003718 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003720 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003722 if (p[-1] == '!')
3723 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 }
3726 if (generate_2BOOL(cctx, invert) == FAIL)
3727 return FAIL;
3728 }
3729 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003730 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731 return OK;
3732}
3733
3734/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003735 * Compile "(expression)": recursive!
3736 * Return FAIL/OK.
3737 */
3738 static int
3739compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3740{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003741 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003742 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003743
Bram Moolenaar24156692021-01-14 20:35:49 +01003744 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3745 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003746 if (ppconst->pp_used <= PPSIZE - 10)
3747 {
3748 ret = compile_expr1(arg, cctx, ppconst);
3749 }
3750 else
3751 {
3752 // Not enough space in ppconst, flush constants.
3753 if (generate_ppconst(cctx, ppconst) == FAIL)
3754 return FAIL;
3755 ret = compile_expr0(arg, cctx);
3756 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003757 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3758 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003759 if (**arg == ')')
3760 ++*arg;
3761 else if (ret == OK)
3762 {
3763 emsg(_(e_missing_close));
3764 ret = FAIL;
3765 }
3766 return ret;
3767}
3768
3769/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003771 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 */
3773 static int
3774compile_subscript(
3775 char_u **arg,
3776 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003777 char_u *start_leader,
3778 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003779 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003781 char_u *name_start = *end_leader;
3782
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783 for (;;)
3784 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003785 char_u *p = skipwhite(*arg);
3786
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003787 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003788 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003789 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003790
3791 // If a following line starts with "->{" or "->X" advance to that
3792 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003793 // Also if a following line starts with ".x".
3794 if (next != NULL &&
3795 ((next[0] == '-' && next[1] == '>'
3796 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003797 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003798 {
3799 next = next_line_from_context(cctx, TRUE);
3800 if (next == NULL)
3801 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003802 *arg = next;
3803 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003804 }
3805 }
3806
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003807 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003808 // not a function call.
3809 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003811 garray_T *stack = &cctx->ctx_type_stack;
3812 type_T *type;
3813 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003815 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003816 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003817 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003818
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003820 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3821
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003822 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3824 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003825 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826 return FAIL;
3827 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003828 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003830 char_u *pstart = p;
3831
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003832 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003833 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003834 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003835
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836 // something->method()
3837 // Apply the '!', '-' and '+' first:
3838 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003839 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003842 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003843 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003844 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003845 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003846 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003847 int argcount = 1;
3848 char_u *expr;
3849 garray_T *stack;
3850 type_T *type;
3851
3852 // Funcref call: list->(Refs[2])(arg)
3853 // or lambda: list->((arg) => expr)(arg)
3854 // Fist compile the arguments.
3855 expr = *arg;
3856 *arg = skipwhite(*arg + 1);
3857 skip_expr_cctx(arg, cctx);
3858 *arg = skipwhite(*arg);
3859 if (**arg != ')')
3860 {
3861 semsg(_(e_missing_paren), *arg);
3862 return FAIL;
3863 }
3864 ++*arg;
3865 if (**arg != '(')
3866 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003867 if (*skipwhite(*arg) == '(')
3868 emsg(_(e_nowhitespace));
3869 else
3870 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01003871 return FAIL;
3872 }
3873
3874 *arg = skipwhite(*arg + 1);
3875 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3876 return FAIL;
3877
3878 // Compile the function expression.
3879 if (compile_parenthesis(&expr, cctx, ppconst) == FAIL)
3880 return FAIL;
3881
3882 stack = &cctx->ctx_type_stack;
3883 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3884 if (generate_PCALL(cctx, argcount,
3885 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 return FAIL;
3887 }
3888 else
3889 {
3890 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003891 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003892 if (!eval_isnamec1(*p))
3893 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003894 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003895 return FAIL;
3896 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003897 if (ASCII_ISALPHA(*p) && p[1] == ':')
3898 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003899 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900 ;
3901 if (*p != '(')
3902 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003903 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904 return FAIL;
3905 }
3906 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003907 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908 return FAIL;
3909 }
3910 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003911 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003913 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003914 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003915 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003916 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003917 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003918
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003919 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003920 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003921 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003922 // TODO: blob index
3923 // TODO: more arguments
3924 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003925 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003926 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003927 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003928
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003929 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003930 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003931 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003932 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003933 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003934 // missing first index is equal to zero
3935 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003936 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003937 else
3938 {
3939 if (compile_expr0(arg, cctx) == FAIL)
3940 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003941 if (**arg == ':')
3942 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003943 semsg(_(e_white_space_required_before_and_after_str_at_str),
3944 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003945 return FAIL;
3946 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003947 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003948 return FAIL;
3949 *arg = skipwhite(*arg);
3950 }
3951 if (**arg == ':')
3952 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003953 is_slice = TRUE;
3954 ++*arg;
3955 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
3956 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003957 semsg(_(e_white_space_required_before_and_after_str_at_str),
3958 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003959 return FAIL;
3960 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003961 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003962 return FAIL;
3963 if (**arg == ']')
3964 // missing second index is equal to end of string
3965 generate_PUSHNR(cctx, -1);
3966 else
3967 {
3968 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003969 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003970 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003971 return FAIL;
3972 *arg = skipwhite(*arg);
3973 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003974 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003975
3976 if (**arg != ']')
3977 {
3978 emsg(_(e_missbrac));
3979 return FAIL;
3980 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003981 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003983 // We can index a list and a dict. If we don't know the type
3984 // we can use the index value type.
3985 // TODO: If we don't know use an instruction to figure it out at
3986 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003987 typep = ((type_T **)stack->ga_data) + stack->ga_len
3988 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003989 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003990 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3991 // If the index is a string, the variable must be a Dict.
3992 if (*typep == &t_any && valtype == &t_string)
3993 vtype = VAR_DICT;
3994 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003995 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003996 if (need_type(valtype, &t_number, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003997 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003998 return FAIL;
3999 if (is_slice)
4000 {
4001 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar351ead02021-01-16 16:07:01 +01004002 if (need_type(valtype, &t_number, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004003 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02004004 return FAIL;
4005 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004006 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02004007
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004008 if (vtype == VAR_DICT)
4009 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004010 if (is_slice)
4011 {
4012 emsg(_(e_cannot_slice_dictionary));
4013 return FAIL;
4014 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004015 if ((*typep)->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01004016 {
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004017 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01004018 if (*typep == &t_unknown)
4019 // empty dict was used
4020 *typep = &t_any;
4021 }
Bram Moolenaar7892b952020-07-20 22:09:34 +02004022 else
4023 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004024 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004025 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02004026 return FAIL;
4027 *typep = &t_any;
4028 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004029 if (may_generate_2STRING(-1, cctx) == FAIL)
4030 return FAIL;
4031 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
4032 return FAIL;
4033 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004034 else if (vtype == VAR_STRING)
4035 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004036 *typep = &t_string;
4037 if ((is_slice
4038 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
4039 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004040 return FAIL;
4041 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004042 else if (vtype == VAR_BLOB)
4043 {
4044 emsg("Sorry, blob index and slice not implemented yet");
4045 return FAIL;
4046 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004047 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01004048 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004049 if (is_slice)
4050 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004051 if (generate_instr_drop(cctx,
4052 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
4053 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02004054 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004055 }
Bram Moolenaared591872020-08-15 22:14:53 +02004056 else
4057 {
4058 if ((*typep)->tt_type == VAR_LIST)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01004059 {
Bram Moolenaared591872020-08-15 22:14:53 +02004060 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01004061 if (*typep == &t_unknown)
4062 // empty list was used
4063 *typep = &t_any;
4064 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004065 if (generate_instr_drop(cctx,
4066 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
4067 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02004068 return FAIL;
4069 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004070 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004071 else
4072 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02004073 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01004074 return FAIL;
4075 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004076 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004077 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004078 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004079 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004080 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004081 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004082 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004083
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004084 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004085 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004086 {
4087 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004088 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004089 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004090 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004091 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004092 while (eval_isnamec(*p))
4093 MB_PTR_ADV(p);
4094 if (p == *arg)
4095 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004096 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 return FAIL;
4098 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004099 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004100 return FAIL;
4101 *arg = p;
4102 }
4103 else
4104 break;
4105 }
4106
4107 // TODO - see handle_subscript():
4108 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4109 // Don't do this when "Func" is already a partial that was bound
4110 // explicitly (pt_auto is FALSE).
4111
4112 return OK;
4113}
4114
4115/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004116 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4117 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004119 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004120 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004121 *
4122 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123 */
4124
4125/*
4126 * number number constant
4127 * 0zFFFFFFFF Blob constant
4128 * "string" string constant
4129 * 'string' literal string constant
4130 * &option-name option value
4131 * @r register contents
4132 * identifier variable value
4133 * function() function call
4134 * $VAR environment variable
4135 * (expression) nested expression
4136 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004137 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138 *
4139 * Also handle:
4140 * ! in front logical NOT
4141 * - in front unary minus
4142 * + in front unary plus (ignored)
4143 * trailing (arg) funcref/partial call
4144 * trailing [] subscript in String or List
4145 * trailing .name entry in Dictionary
4146 * trailing ->name() method call
4147 */
4148 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004149compile_expr7(
4150 char_u **arg,
4151 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004152 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004153{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154 char_u *start_leader, *end_leader;
4155 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004156 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004157 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004158
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004159 ppconst->pp_is_const = FALSE;
4160
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161 /*
4162 * Skip '!', '-' and '+' characters. They are handled later.
4163 */
4164 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004165 if (eval_leader(arg, TRUE) == FAIL)
4166 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004167 end_leader = *arg;
4168
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004169 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170 switch (**arg)
4171 {
4172 /*
4173 * Number constant.
4174 */
4175 case '0': // also for blob starting with 0z
4176 case '1':
4177 case '2':
4178 case '3':
4179 case '4':
4180 case '5':
4181 case '6':
4182 case '7':
4183 case '8':
4184 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004185 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004186 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004187 // Apply "-" and "+" just before the number now, right to
4188 // left. Matters especially when "->" follows. Stops at
4189 // '!'.
4190 if (apply_leader(rettv, TRUE,
4191 start_leader, &end_leader) == FAIL)
4192 {
4193 clear_tv(rettv);
4194 return FAIL;
4195 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196 break;
4197
4198 /*
4199 * String constant: "string".
4200 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004201 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202 return FAIL;
4203 break;
4204
4205 /*
4206 * Literal string constant: 'str''ing'.
4207 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004208 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209 return FAIL;
4210 break;
4211
4212 /*
4213 * Constant Vim variable.
4214 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004215 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216 ret = NOTDONE;
4217 break;
4218
4219 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004220 * "true" constant
4221 */
4222 case 't': if (STRNCMP(*arg, "true", 4) == 0
4223 && !eval_isnamec((*arg)[4]))
4224 {
4225 *arg += 4;
4226 rettv->v_type = VAR_BOOL;
4227 rettv->vval.v_number = VVAL_TRUE;
4228 }
4229 else
4230 ret = NOTDONE;
4231 break;
4232
4233 /*
4234 * "false" constant
4235 */
4236 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4237 && !eval_isnamec((*arg)[5]))
4238 {
4239 *arg += 5;
4240 rettv->v_type = VAR_BOOL;
4241 rettv->vval.v_number = VVAL_FALSE;
4242 }
4243 else
4244 ret = NOTDONE;
4245 break;
4246
4247 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004248 * "null" constant
4249 */
4250 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004251 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004252 {
4253 *arg += 4;
4254 rettv->v_type = VAR_SPECIAL;
4255 rettv->vval.v_number = VVAL_NULL;
4256 }
4257 else
4258 ret = NOTDONE;
4259 break;
4260
4261 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004262 * List: [expr, expr]
4263 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004264 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4265 return FAIL;
4266 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004267 break;
4268
4269 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004270 * Dictionary: {'key': val, 'key': val}
4271 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004272 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4273 return FAIL;
4274 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004275 break;
4276
4277 /*
4278 * Option value: &name
4279 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004280 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4281 return FAIL;
4282 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283 break;
4284
4285 /*
4286 * Environment variable: $VAR.
4287 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004288 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4289 return FAIL;
4290 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004291 break;
4292
4293 /*
4294 * Register contents: @r.
4295 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004296 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4297 return FAIL;
4298 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 break;
4300 /*
4301 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004302 * lambda: (arg, arg) => expr
4303 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004305 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4306 ret = compile_lambda(arg, cctx);
4307 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004308 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004309 break;
4310
4311 default: ret = NOTDONE;
4312 break;
4313 }
4314 if (ret == FAIL)
4315 return FAIL;
4316
Bram Moolenaar1c747212020-05-09 18:28:34 +02004317 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004319 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004320 clear_tv(rettv);
4321 else
4322 // A constant expression can possibly be handled compile time,
4323 // return the value instead of generating code.
4324 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325 }
4326 else if (ret == NOTDONE)
4327 {
4328 char_u *p;
4329 int r;
4330
4331 if (!eval_isnamec1(**arg))
4332 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004333 if (!vim9_bad_comment(*arg))
4334 {
4335 if (ends_excmd(*skipwhite(*arg)))
4336 semsg(_(e_empty_expression_str), *arg);
4337 else
4338 semsg(_(e_name_expected_str), *arg);
4339 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 return FAIL;
4341 }
4342
4343 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004344 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004346 {
4347 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4348 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004350 {
4351 if (generate_ppconst(cctx, ppconst) == FAIL)
4352 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004353 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004354 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355 if (r == FAIL)
4356 return FAIL;
4357 }
4358
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004359 // Handle following "[]", ".member", etc.
4360 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004361 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004362 ppconst) == FAIL)
4363 return FAIL;
4364 if (ppconst->pp_used > 0)
4365 {
4366 // apply the '!', '-' and '+' before the constant
4367 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004368 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004369 return FAIL;
4370 return OK;
4371 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004372 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004374 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375}
4376
4377/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004378 * Give the "white on both sides" error, taking the operator from "p[len]".
4379 */
4380 void
4381error_white_both(char_u *op, int len)
4382{
4383 char_u buf[10];
4384
4385 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004386 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004387}
4388
4389/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004390 * <type>expr7: runtime type check / conversion
4391 */
4392 static int
4393compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4394{
4395 type_T *want_type = NULL;
4396
4397 // Recognize <type>
4398 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4399 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004400 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004401 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4402 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004403 return FAIL;
4404
4405 if (**arg != '>')
4406 {
4407 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004408 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004409 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004410 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004411 return FAIL;
4412 }
4413 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004414 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004415 return FAIL;
4416 }
4417
4418 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4419 return FAIL;
4420
4421 if (want_type != NULL)
4422 {
4423 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004424 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004425 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004426
Bram Moolenaard1103582020-08-14 22:44:25 +02004427 generate_ppconst(cctx, ppconst);
4428 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004429 where.wt_index = 0;
4430 where.wt_variable = FALSE;
4431 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004432 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004433 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004434 return FAIL;
4435 }
4436 }
4437
4438 return OK;
4439}
4440
4441/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 * * number multiplication
4443 * / number division
4444 * % number modulo
4445 */
4446 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004447compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448{
4449 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004450 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004451 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004452
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004453 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004454 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004455 return FAIL;
4456
4457 /*
4458 * Repeat computing, until no "*", "/" or "%" is following.
4459 */
4460 for (;;)
4461 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004462 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004463 if (*op != '*' && *op != '/' && *op != '%')
4464 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004465 if (next != NULL)
4466 {
4467 *arg = next_line_from_context(cctx, TRUE);
4468 op = skipwhite(*arg);
4469 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004470
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004471 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004473 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004474 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004475 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004476 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004477 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004479 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004480 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004482
4483 if (ppconst->pp_used == ppconst_used + 2
4484 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4485 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004486 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004487 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4488 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4489 varnumber_T res = 0;
4490 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004492 // both are numbers: compute the result
4493 switch (*op)
4494 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004495 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004496 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004497 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004498 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004499 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004500 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004501 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004502 break;
4503 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004504 if (failed)
4505 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004506 tv1->vval.v_number = res;
4507 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004508 }
4509 else
4510 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004511 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004512 generate_two_op(cctx, op);
4513 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004514 }
4515
4516 return OK;
4517}
4518
4519/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004520 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004521 * - number subtraction
4522 * .. string concatenation
4523 */
4524 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004525compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004526{
4527 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004528 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004529 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004530 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004531
4532 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004533 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004534 return FAIL;
4535
4536 /*
4537 * Repeat computing, until no "+", "-" or ".." is following.
4538 */
4539 for (;;)
4540 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004541 op = may_peek_next_line(cctx, *arg, &next);
4542 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004543 break;
4544 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004545 if (next != NULL)
4546 {
4547 *arg = next_line_from_context(cctx, TRUE);
4548 op = skipwhite(*arg);
4549 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004550
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004551 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004553 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004554 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555 }
4556
Bram Moolenaare0de1712020-12-02 17:36:54 +01004557 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004558 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004559
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004560 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004561 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004562 return FAIL;
4563
Bram Moolenaara5565e42020-05-09 15:44:01 +02004564 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004565 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004566 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4567 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4568 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4569 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004571 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4572 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004573
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004574 // concat/subtract/add constant numbers
4575 if (*op == '+')
4576 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4577 else if (*op == '-')
4578 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4579 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004580 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004581 // concatenate constant strings
4582 char_u *s1 = tv1->vval.v_string;
4583 char_u *s2 = tv2->vval.v_string;
4584 size_t len1 = STRLEN(s1);
4585
4586 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4587 if (tv1->vval.v_string == NULL)
4588 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004589 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004590 return FAIL;
4591 }
4592 mch_memmove(tv1->vval.v_string, s1, len1);
4593 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004594 vim_free(s1);
4595 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004596 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004597 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 }
4599 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004600 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004601 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004602 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004603 if (*op == '.')
4604 {
4605 if (may_generate_2STRING(-2, cctx) == FAIL
4606 || may_generate_2STRING(-1, cctx) == FAIL)
4607 return FAIL;
4608 generate_instr_drop(cctx, ISN_CONCAT, 1);
4609 }
4610 else
4611 generate_two_op(cctx, op);
4612 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613 }
4614
4615 return OK;
4616}
4617
4618/*
4619 * expr5a == expr5b
4620 * expr5a =~ expr5b
4621 * expr5a != expr5b
4622 * expr5a !~ expr5b
4623 * expr5a > expr5b
4624 * expr5a >= expr5b
4625 * expr5a < expr5b
4626 * expr5a <= expr5b
4627 * expr5a is expr5b
4628 * expr5a isnot expr5b
4629 *
4630 * Produces instructions:
4631 * EVAL expr5a Push result of "expr5a"
4632 * EVAL expr5b Push result of "expr5b"
4633 * COMPARE one of the compare instructions
4634 */
4635 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004636compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004638 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004640 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004641 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004643 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644
4645 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004646 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004647 return FAIL;
4648
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004649 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004650 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004651
4652 /*
4653 * If there is a comparative operator, use it.
4654 */
4655 if (type != EXPR_UNKNOWN)
4656 {
4657 int ic = FALSE; // Default: do not ignore case
4658
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004659 if (next != NULL)
4660 {
4661 *arg = next_line_from_context(cctx, TRUE);
4662 p = skipwhite(*arg);
4663 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004664 if (type_is && (p[len] == '?' || p[len] == '#'))
4665 {
4666 semsg(_(e_invexpr2), *arg);
4667 return FAIL;
4668 }
4669 // extra question mark appended: ignore case
4670 if (p[len] == '?')
4671 {
4672 ic = TRUE;
4673 ++len;
4674 }
4675 // extra '#' appended: match case (ignored)
4676 else if (p[len] == '#')
4677 ++len;
4678 // nothing appended: match case
4679
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004680 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004681 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004682 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004683 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684 }
4685
4686 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004687 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004688 return FAIL;
4689
Bram Moolenaara5565e42020-05-09 15:44:01 +02004690 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004691 return FAIL;
4692
Bram Moolenaara5565e42020-05-09 15:44:01 +02004693 if (ppconst->pp_used == ppconst_used + 2)
4694 {
4695 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4696 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4697 int ret;
4698
4699 // Both sides are a constant, compute the result now.
4700 // First check for a valid combination of types, this is more
4701 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004702 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004703 ret = FAIL;
4704 else
4705 {
4706 ret = typval_compare(tv1, tv2, type, ic);
4707 tv1->v_type = VAR_BOOL;
4708 tv1->vval.v_number = tv1->vval.v_number
4709 ? VVAL_TRUE : VVAL_FALSE;
4710 clear_tv(tv2);
4711 --ppconst->pp_used;
4712 }
4713 return ret;
4714 }
4715
4716 generate_ppconst(cctx, ppconst);
4717 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004718 }
4719
4720 return OK;
4721}
4722
Bram Moolenaar7f141552020-05-09 17:35:53 +02004723static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4724
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004725/*
4726 * Compile || or &&.
4727 */
4728 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004729compile_and_or(
4730 char_u **arg,
4731 cctx_T *cctx,
4732 char *op,
4733 ppconst_T *ppconst,
4734 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004736 char_u *next;
4737 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738 int opchar = *op;
4739
4740 if (p[0] == opchar && p[1] == opchar)
4741 {
4742 garray_T *instr = &cctx->ctx_instr;
4743 garray_T end_ga;
4744
4745 /*
4746 * Repeat until there is no following "||" or "&&"
4747 */
4748 ga_init2(&end_ga, sizeof(int), 10);
4749 while (p[0] == opchar && p[1] == opchar)
4750 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004751 if (next != NULL)
4752 {
4753 *arg = next_line_from_context(cctx, TRUE);
4754 p = skipwhite(*arg);
4755 }
4756
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004757 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4758 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004759 semsg(_(e_white_space_required_before_and_after_str_at_str),
4760 op, *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004761 return FAIL;
4762 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004763
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004764 // TODO: use ppconst if the value is a constant and check
4765 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004766 generate_ppconst(cctx, ppconst);
4767
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004768 // Every part must evaluate to a bool.
4769 if (bool_on_stack(cctx) == FAIL)
4770 {
4771 ga_clear(&end_ga);
4772 return FAIL;
4773 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004774
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004775 if (ga_grow(&end_ga, 1) == FAIL)
4776 {
4777 ga_clear(&end_ga);
4778 return FAIL;
4779 }
4780 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4781 ++end_ga.ga_len;
4782 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004783 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004784
4785 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004786 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004787 {
4788 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004789 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004790 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004791
Bram Moolenaara5565e42020-05-09 15:44:01 +02004792 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4793 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004794 {
4795 ga_clear(&end_ga);
4796 return FAIL;
4797 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004798
4799 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004800 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004801 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004802
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004803 // Every part must evaluate to a bool.
4804 if (bool_on_stack(cctx) == FAIL)
4805 {
4806 ga_clear(&end_ga);
4807 return FAIL;
4808 }
4809
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004810 // Fill in the end label in all jumps.
4811 while (end_ga.ga_len > 0)
4812 {
4813 isn_T *isn;
4814
4815 --end_ga.ga_len;
4816 isn = ((isn_T *)instr->ga_data)
4817 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4818 isn->isn_arg.jump.jump_where = instr->ga_len;
4819 }
4820 ga_clear(&end_ga);
4821 }
4822
4823 return OK;
4824}
4825
4826/*
4827 * expr4a && expr4a && expr4a logical AND
4828 *
4829 * Produces instructions:
4830 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004831 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004832 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004833 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004834 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004835 * EVAL expr4c Push result of "expr4c"
4836 * end:
4837 */
4838 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004839compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004840{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004841 int ppconst_used = ppconst->pp_used;
4842
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004843 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004844 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004845 return FAIL;
4846
4847 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004848 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004849}
4850
4851/*
4852 * expr3a || expr3b || expr3c logical OR
4853 *
4854 * Produces instructions:
4855 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004856 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004857 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004858 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004859 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004860 * EVAL expr3c Push result of "expr3c"
4861 * end:
4862 */
4863 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004864compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004865{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004866 int ppconst_used = ppconst->pp_used;
4867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004868 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004869 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870 return FAIL;
4871
4872 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004873 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004874}
4875
4876/*
4877 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004878 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004879 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004880 * JUMP_IF_FALSE alt jump if false
4881 * EVAL expr1a
4882 * JUMP_ALWAYS end
4883 * alt: EVAL expr1b
4884 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004885 *
4886 * Toplevel expression: expr2 ?? expr1
4887 * Produces instructions:
4888 * EVAL expr2 Push result of "expr2"
4889 * JUMP_AND_KEEP_IF_TRUE end jump if true
4890 * EVAL expr1
4891 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892 */
4893 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004894compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895{
4896 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004897 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004898 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004899
Bram Moolenaar3988f642020-08-27 22:43:03 +02004900 // Ignore all kinds of errors when not producing code.
4901 if (cctx->ctx_skip == SKIP_YES)
4902 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004903 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004904 return OK;
4905 }
4906
Bram Moolenaar61a89812020-05-07 16:58:17 +02004907 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004908 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004909 return FAIL;
4910
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004911 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004912 if (*p == '?')
4913 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004914 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004915 garray_T *instr = &cctx->ctx_instr;
4916 garray_T *stack = &cctx->ctx_type_stack;
4917 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004918 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004919 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004920 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004921 int has_const_expr = FALSE;
4922 int const_value = FALSE;
4923 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004924
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004925 if (next != NULL)
4926 {
4927 *arg = next_line_from_context(cctx, TRUE);
4928 p = skipwhite(*arg);
4929 }
4930
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004931 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004932 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004933 semsg(_(e_white_space_required_before_and_after_str_at_str),
4934 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004935 return FAIL;
4936 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004937
Bram Moolenaara5565e42020-05-09 15:44:01 +02004938 if (ppconst->pp_used == ppconst_used + 1)
4939 {
4940 // the condition is a constant, we know whether the ? or the :
4941 // expression is to be evaluated.
4942 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004943 if (op_falsy)
4944 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4945 else
4946 {
4947 int error = FALSE;
4948
4949 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4950 &error);
4951 if (error)
4952 return FAIL;
4953 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004954 cctx->ctx_skip = save_skip == SKIP_YES ||
4955 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4956
4957 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4958 // "left ?? right" and "left" is truthy: produce "left"
4959 generate_ppconst(cctx, ppconst);
4960 else
4961 {
4962 clear_tv(&ppconst->pp_tv[ppconst_used]);
4963 --ppconst->pp_used;
4964 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004965 }
4966 else
4967 {
4968 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004969 if (op_falsy)
4970 end_idx = instr->ga_len;
4971 generate_JUMP(cctx, op_falsy
4972 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4973 if (op_falsy)
4974 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004975 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004976
4977 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01004978 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004979 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004980 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004981 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004982
Bram Moolenaara5565e42020-05-09 15:44:01 +02004983 if (!has_const_expr)
4984 {
4985 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004986
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004987 if (!op_falsy)
4988 {
4989 // remember the type and drop it
4990 --stack->ga_len;
4991 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004992
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004993 end_idx = instr->ga_len;
4994 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004995
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004996 // jump here from JUMP_IF_FALSE
4997 isn = ((isn_T *)instr->ga_data) + alt_idx;
4998 isn->isn_arg.jump.jump_where = instr->ga_len;
4999 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005000 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005001
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005002 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005003 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005004 // Check for the ":".
5005 p = may_peek_next_line(cctx, *arg, &next);
5006 if (*p != ':')
5007 {
5008 emsg(_(e_missing_colon));
5009 return FAIL;
5010 }
5011 if (next != NULL)
5012 {
5013 *arg = next_line_from_context(cctx, TRUE);
5014 p = skipwhite(*arg);
5015 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005016
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005017 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5018 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005019 semsg(_(e_white_space_required_before_and_after_str_at_str),
5020 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005021 return FAIL;
5022 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005023
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005024 // evaluate the third expression
5025 if (has_const_expr)
5026 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005027 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005028 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005029 return FAIL;
5030 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5031 return FAIL;
5032 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005033
Bram Moolenaara5565e42020-05-09 15:44:01 +02005034 if (!has_const_expr)
5035 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005036 type_T **typep;
5037
Bram Moolenaara5565e42020-05-09 15:44:01 +02005038 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005039
Bram Moolenaara5565e42020-05-09 15:44:01 +02005040 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005041 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5042 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005043
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005044 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005045 isn = ((isn_T *)instr->ga_data) + end_idx;
5046 isn->isn_arg.jump.jump_where = instr->ga_len;
5047 }
5048
5049 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005050 }
5051 return OK;
5052}
5053
5054/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005055 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005056 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5057 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005058 */
5059 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005060compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005061{
5062 ppconst_T ppconst;
5063
5064 CLEAR_FIELD(ppconst);
5065 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5066 {
5067 clear_ppconst(&ppconst);
5068 return FAIL;
5069 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005070 if (is_const != NULL)
5071 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005072 if (generate_ppconst(cctx, &ppconst) == FAIL)
5073 return FAIL;
5074 return OK;
5075}
5076
5077/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005078 * Toplevel expression.
5079 */
5080 static int
5081compile_expr0(char_u **arg, cctx_T *cctx)
5082{
5083 return compile_expr0_ext(arg, cctx, NULL);
5084}
5085
5086/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005087 * compile "return [expr]"
5088 */
5089 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005090compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005091{
5092 char_u *p = arg;
5093 garray_T *stack = &cctx->ctx_type_stack;
5094 type_T *stack_type;
5095
5096 if (*p != NUL && *p != '|' && *p != '\n')
5097 {
5098 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02005099 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005100 return NULL;
5101
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005102 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005103 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005104 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01005105 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005106 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5107 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005108 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005109 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005110 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005111 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005112 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005113 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5114 && stack_type->tt_type != VAR_VOID
5115 && stack_type->tt_type != VAR_UNKNOWN)
5116 {
5117 emsg(_(e_returning_value_in_function_without_return_type));
5118 return NULL;
5119 }
5120 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005121 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005122 return NULL;
5123 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005124 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005125 }
5126 else
5127 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005128 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005129 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005130 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5131 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005132 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005133 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005134 return NULL;
5135 }
5136
5137 // No argument, return zero.
5138 generate_PUSHNR(cctx, 0);
5139 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005140
5141 // Undo any command modifiers.
5142 generate_undo_cmdmods(cctx);
5143
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005144 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005145 return NULL;
5146
5147 // "return val | endif" is possible
5148 return skipwhite(p);
5149}
5150
5151/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005152 * Get a line from the compilation context, compatible with exarg_T getline().
5153 * Return a pointer to the line in allocated memory.
5154 * Return NULL for end-of-file or some error.
5155 */
5156 static char_u *
5157exarg_getline(
5158 int c UNUSED,
5159 void *cookie,
5160 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005161 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005162{
5163 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005164 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005165
Bram Moolenaar66250c92020-08-20 15:02:42 +02005166 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005167 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005168 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005169 return NULL;
5170 ++cctx->ctx_lnum;
5171 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5172 // Comment lines result in NULL pointers, skip them.
5173 if (p != NULL)
5174 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005175 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005176}
5177
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005178 void
5179fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5180{
5181 eap->getline = exarg_getline;
5182 eap->cookie = cctx;
5183}
5184
Bram Moolenaar04b12692020-05-04 23:24:44 +02005185/*
5186 * Compile a nested :def command.
5187 */
5188 static char_u *
5189compile_nested_function(exarg_T *eap, cctx_T *cctx)
5190{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005191 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005192 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005193 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005194 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005195 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005196 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005197
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005198 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005199 {
5200 emsg(_(e_cannot_use_bang_with_nested_def));
5201 return NULL;
5202 }
5203
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005204 if (*name_start == '/')
5205 {
5206 name_end = skip_regexp(name_start + 1, '/', TRUE);
5207 if (*name_end == '/')
5208 ++name_end;
5209 eap->nextcmd = check_nextcmd(name_end);
5210 }
5211 if (name_end == name_start || *skipwhite(name_end) != '(')
5212 {
5213 if (!ends_excmd2(name_start, name_end))
5214 {
5215 semsg(_(e_invalid_command_str), eap->cmd);
5216 return NULL;
5217 }
5218
5219 // "def" or "def Name": list functions
5220 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5221 return NULL;
5222 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5223 }
5224
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005225 // Only g:Func() can use a namespace.
5226 if (name_start[1] == ':' && !is_global)
5227 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005228 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005229 return NULL;
5230 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005231 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005232 return NULL;
5233
Bram Moolenaar04b12692020-05-04 23:24:44 +02005234 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005235 fill_exarg_from_cctx(eap, cctx);
5236
Bram Moolenaar04b12692020-05-04 23:24:44 +02005237 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005238 lambda_name = vim_strsave(get_lambda_name());
5239 if (lambda_name == NULL)
5240 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005241 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005242
Bram Moolenaar822ba242020-05-24 23:00:18 +02005243 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005244 {
5245 r = eap->skip ? OK : FAIL;
5246 goto theend;
5247 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005248
5249 // copy over the block scope IDs before compiling
5250 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5251 {
5252 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5253
5254 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5255 if (ufunc->uf_block_ids != NULL)
5256 {
5257 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5258 sizeof(int) * block_depth);
5259 ufunc->uf_block_depth = block_depth;
5260 }
5261 }
5262
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005263 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5264 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005265 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005266 {
5267 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005268 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005269 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005270
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005271 if (is_global)
5272 {
5273 char_u *func_name = vim_strnsave(name_start + 2,
5274 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005275
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005276 if (func_name == NULL)
5277 r = FAIL;
5278 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005279 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005280 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005281 lambda_name = NULL;
5282 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005283 }
5284 else
5285 {
5286 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005287 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005288 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005289
Bram Moolenaareef21022020-08-01 22:16:43 +02005290 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005291 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005292 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005293 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005294 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5295 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005296 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005297
5298theend:
5299 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005300 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005301}
5302
5303/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005304 * Return the length of an assignment operator, or zero if there isn't one.
5305 */
5306 int
5307assignment_len(char_u *p, int *heredoc)
5308{
5309 if (*p == '=')
5310 {
5311 if (p[1] == '<' && p[2] == '<')
5312 {
5313 *heredoc = TRUE;
5314 return 3;
5315 }
5316 return 1;
5317 }
5318 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5319 return 2;
5320 if (STRNCMP(p, "..=", 3) == 0)
5321 return 3;
5322 return 0;
5323}
5324
5325// words that cannot be used as a variable
5326static char *reserved[] = {
5327 "true",
5328 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005329 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005330 NULL
5331};
5332
Bram Moolenaar752fc692021-01-04 21:57:11 +01005333// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005334typedef enum {
5335 dest_local,
5336 dest_option,
5337 dest_env,
5338 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005339 dest_buffer,
5340 dest_window,
5341 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005342 dest_vimvar,
5343 dest_script,
5344 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005345 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005346} assign_dest_T;
5347
Bram Moolenaar752fc692021-01-04 21:57:11 +01005348// Used by compile_lhs() to store information about the LHS of an assignment
5349// and one argument of ":unlet" with an index.
5350typedef struct {
5351 assign_dest_T lhs_dest; // type of destination
5352
5353 char_u *lhs_name; // allocated name including
5354 // "[expr]" or ".name".
5355 size_t lhs_varlen; // length of the variable without
5356 // "[expr]" or ".name"
5357 char_u *lhs_dest_end; // end of the destination, including
5358 // "[expr]" or ".name".
5359
5360 int lhs_has_index; // has "[expr]" or ".name"
5361
5362 int lhs_new_local; // create new local variable
5363 int lhs_opt_flags; // for when destination is an option
5364 int lhs_vimvaridx; // for when destination is a v:var
5365
5366 lvar_T lhs_local_lvar; // used for existing local destination
5367 lvar_T lhs_arg_lvar; // used for argument destination
5368 lvar_T *lhs_lvar; // points to destination lvar
5369 int lhs_scriptvar_sid;
5370 int lhs_scriptvar_idx;
5371
5372 int lhs_has_type; // type was specified
5373 type_T *lhs_type;
5374 type_T *lhs_member_type;
5375} lhs_T;
5376
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005377/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005378 * Generate the load instruction for "name".
5379 */
5380 static void
5381generate_loadvar(
5382 cctx_T *cctx,
5383 assign_dest_T dest,
5384 char_u *name,
5385 lvar_T *lvar,
5386 type_T *type)
5387{
5388 switch (dest)
5389 {
5390 case dest_option:
5391 // TODO: check the option exists
5392 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5393 break;
5394 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005395 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5396 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5397 else
5398 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005399 break;
5400 case dest_buffer:
5401 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5402 break;
5403 case dest_window:
5404 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5405 break;
5406 case dest_tab:
5407 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5408 break;
5409 case dest_script:
5410 compile_load_scriptvar(cctx,
5411 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5412 break;
5413 case dest_env:
5414 // Include $ in the name here
5415 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5416 break;
5417 case dest_reg:
5418 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5419 break;
5420 case dest_vimvar:
5421 generate_LOADV(cctx, name + 2, TRUE);
5422 break;
5423 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005424 if (lvar->lv_from_outer > 0)
5425 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5426 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005427 else
5428 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5429 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005430 case dest_expr:
5431 // list or dict value should already be on the stack.
5432 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005433 }
5434}
5435
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005436/*
5437 * Skip over "[expr]" or ".member".
5438 * Does not check for any errors.
5439 */
5440 static char_u *
5441skip_index(char_u *start)
5442{
5443 char_u *p = start;
5444
5445 if (*p == '[')
5446 {
5447 p = skipwhite(p + 1);
5448 (void)skip_expr(&p, NULL);
5449 p = skipwhite(p);
5450 if (*p == ']')
5451 return p + 1;
5452 return p;
5453 }
5454 // if (*p == '.')
5455 return to_name_end(p + 1, TRUE);
5456}
5457
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005458 void
5459vim9_declare_error(char_u *name)
5460{
5461 char *scope = "";
5462
5463 switch (*name)
5464 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005465 case 'g': scope = _("global"); break;
5466 case 'b': scope = _("buffer"); break;
5467 case 'w': scope = _("window"); break;
5468 case 't': scope = _("tab"); break;
5469 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005470 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005471 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005472 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005473 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005474 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005475 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005476 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005477 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005478 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005479}
5480
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005481/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005482 * For one assignment figure out the type of destination. Return it in "dest".
5483 * When not recognized "dest" is not set.
5484 * For an option "opt_flags" is set.
5485 * For a v:var "vimvaridx" is set.
5486 * "type" is set to the destination type if known, unchanted otherwise.
5487 * Return FAIL if an error message was given.
5488 */
5489 static int
5490get_var_dest(
5491 char_u *name,
5492 assign_dest_T *dest,
5493 int cmdidx,
5494 int *opt_flags,
5495 int *vimvaridx,
5496 type_T **type,
5497 cctx_T *cctx)
5498{
5499 char_u *p;
5500
5501 if (*name == '&')
5502 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005503 int cc;
5504 long numval;
5505 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005506
5507 *dest = dest_option;
5508 if (cmdidx == CMD_final || cmdidx == CMD_const)
5509 {
5510 emsg(_(e_const_option));
5511 return FAIL;
5512 }
5513 p = name;
5514 p = find_option_end(&p, opt_flags);
5515 if (p == NULL)
5516 {
5517 // cannot happen?
5518 emsg(_(e_letunexp));
5519 return FAIL;
5520 }
5521 cc = *p;
5522 *p = NUL;
5523 opt_type = get_option_value(skip_option_env_lead(name),
5524 &numval, NULL, *opt_flags);
5525 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005526 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005527 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005528 case gov_unknown:
5529 semsg(_(e_unknown_option), name);
5530 return FAIL;
5531 case gov_string:
5532 case gov_hidden_string:
5533 *type = &t_string;
5534 break;
5535 case gov_bool:
5536 case gov_hidden_bool:
5537 *type = &t_bool;
5538 break;
5539 case gov_number:
5540 case gov_hidden_number:
5541 *type = &t_number;
5542 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005543 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005544 }
5545 else if (*name == '$')
5546 {
5547 *dest = dest_env;
5548 *type = &t_string;
5549 }
5550 else if (*name == '@')
5551 {
5552 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5553 {
5554 emsg_invreg(name[1]);
5555 return FAIL;
5556 }
5557 *dest = dest_reg;
5558 *type = &t_string;
5559 }
5560 else if (STRNCMP(name, "g:", 2) == 0)
5561 {
5562 *dest = dest_global;
5563 }
5564 else if (STRNCMP(name, "b:", 2) == 0)
5565 {
5566 *dest = dest_buffer;
5567 }
5568 else if (STRNCMP(name, "w:", 2) == 0)
5569 {
5570 *dest = dest_window;
5571 }
5572 else if (STRNCMP(name, "t:", 2) == 0)
5573 {
5574 *dest = dest_tab;
5575 }
5576 else if (STRNCMP(name, "v:", 2) == 0)
5577 {
5578 typval_T *vtv;
5579 int di_flags;
5580
5581 *vimvaridx = find_vim_var(name + 2, &di_flags);
5582 if (*vimvaridx < 0)
5583 {
5584 semsg(_(e_variable_not_found_str), name);
5585 return FAIL;
5586 }
5587 // We use the current value of "sandbox" here, is that OK?
5588 if (var_check_ro(di_flags, name, FALSE))
5589 return FAIL;
5590 *dest = dest_vimvar;
5591 vtv = get_vim_var_tv(*vimvaridx);
5592 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5593 }
5594 return OK;
5595}
5596
5597/*
5598 * Generate a STORE instruction for "dest", not being "dest_local".
5599 * Return FAIL when out of memory.
5600 */
5601 static int
5602generate_store_var(
5603 cctx_T *cctx,
5604 assign_dest_T dest,
5605 int opt_flags,
5606 int vimvaridx,
5607 int scriptvar_idx,
5608 int scriptvar_sid,
5609 type_T *type,
5610 char_u *name)
5611{
5612 switch (dest)
5613 {
5614 case dest_option:
5615 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5616 opt_flags);
5617 case dest_global:
5618 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005619 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5620 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005621 case dest_buffer:
5622 // include b: with the name, easier to execute that way
5623 return generate_STORE(cctx, ISN_STOREB, 0, name);
5624 case dest_window:
5625 // include w: with the name, easier to execute that way
5626 return generate_STORE(cctx, ISN_STOREW, 0, name);
5627 case dest_tab:
5628 // include t: with the name, easier to execute that way
5629 return generate_STORE(cctx, ISN_STORET, 0, name);
5630 case dest_env:
5631 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5632 case dest_reg:
5633 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5634 case dest_vimvar:
5635 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5636 case dest_script:
5637 if (scriptvar_idx < 0)
5638 {
5639 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005640 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005641
Bram Moolenaar41d61962020-12-06 20:12:43 +01005642 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005643 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5644 scriptvar_sid, type);
5645 if (name_s != name)
5646 vim_free(name_s);
5647 return r;
5648 }
5649 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5650 scriptvar_sid, scriptvar_idx, type);
5651 case dest_local:
5652 case dest_expr:
5653 // cannot happen
5654 break;
5655 }
5656 return FAIL;
5657}
5658
Bram Moolenaar752fc692021-01-04 21:57:11 +01005659 static int
5660is_decl_command(int cmdidx)
5661{
5662 return cmdidx == CMD_let || cmdidx == CMD_var
5663 || cmdidx == CMD_final || cmdidx == CMD_const;
5664}
5665
5666/*
5667 * Figure out the LHS type and other properties for an assignment or one item
5668 * of ":unlet" with an index.
5669 * Returns OK or FAIL.
5670 */
5671 static int
5672compile_lhs(
5673 char_u *var_start,
5674 lhs_T *lhs,
5675 int cmdidx,
5676 int heredoc,
5677 int oplen,
5678 cctx_T *cctx)
5679{
5680 char_u *var_end;
5681 int is_decl = is_decl_command(cmdidx);
5682
5683 CLEAR_POINTER(lhs);
5684 lhs->lhs_dest = dest_local;
5685 lhs->lhs_vimvaridx = -1;
5686 lhs->lhs_scriptvar_idx = -1;
5687
5688 // "dest_end" is the end of the destination, including "[expr]" or
5689 // ".name".
5690 // "var_end" is the end of the variable/option/etc. name.
5691 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5692 if (*var_start == '@')
5693 var_end = var_start + 2;
5694 else
5695 {
5696 // skip over the leading "&", "&l:", "&g:" and "$"
5697 var_end = skip_option_env_lead(var_start);
5698 var_end = to_name_end(var_end, TRUE);
5699 }
5700
5701 // "a: type" is declaring variable "a" with a type, not dict "a:".
5702 if (is_decl && lhs->lhs_dest_end == var_start + 2
5703 && lhs->lhs_dest_end[-1] == ':')
5704 --lhs->lhs_dest_end;
5705 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5706 --var_end;
5707
5708 // compute the length of the destination without "[expr]" or ".name"
5709 lhs->lhs_varlen = var_end - var_start;
5710 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5711 if (lhs->lhs_name == NULL)
5712 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005713
5714 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5715 // Something follows after the variable: "var[idx]" or "var.key".
5716 lhs->lhs_has_index = TRUE;
5717
Bram Moolenaar752fc692021-01-04 21:57:11 +01005718 if (heredoc)
5719 lhs->lhs_type = &t_list_string;
5720 else
5721 lhs->lhs_type = &t_any;
5722
5723 if (cctx->ctx_skip != SKIP_YES)
5724 {
5725 int declare_error = FALSE;
5726
5727 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5728 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5729 &lhs->lhs_type, cctx) == FAIL)
5730 return FAIL;
5731 if (lhs->lhs_dest != dest_local)
5732 {
5733 // Specific kind of variable recognized.
5734 declare_error = is_decl;
5735 }
5736 else
5737 {
5738 int idx;
5739
5740 // No specific kind of variable recognized, just a name.
5741 for (idx = 0; reserved[idx] != NULL; ++idx)
5742 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5743 {
5744 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5745 return FAIL;
5746 }
5747
5748
5749 if (lookup_local(var_start, lhs->lhs_varlen,
5750 &lhs->lhs_local_lvar, cctx) == OK)
5751 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5752 else
5753 {
5754 CLEAR_FIELD(lhs->lhs_arg_lvar);
5755 if (arg_exists(var_start, lhs->lhs_varlen,
5756 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5757 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5758 {
5759 if (is_decl)
5760 {
5761 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5762 return FAIL;
5763 }
5764 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5765 }
5766 }
5767 if (lhs->lhs_lvar != NULL)
5768 {
5769 if (is_decl)
5770 {
5771 semsg(_(e_variable_already_declared), lhs->lhs_name);
5772 return FAIL;
5773 }
5774 }
5775 else
5776 {
5777 int script_namespace = lhs->lhs_varlen > 1
5778 && STRNCMP(var_start, "s:", 2) == 0;
5779 int script_var = (script_namespace
5780 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
5781 FALSE, cctx)
5782 : script_var_exists(var_start, lhs->lhs_varlen,
5783 TRUE, cctx)) == OK;
5784 imported_T *import =
5785 find_imported(var_start, lhs->lhs_varlen, cctx);
5786
5787 if (script_namespace || script_var || import != NULL)
5788 {
5789 char_u *rawname = lhs->lhs_name
5790 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5791
5792 if (is_decl)
5793 {
5794 if (script_namespace)
5795 semsg(_(e_cannot_declare_script_variable_in_function),
5796 lhs->lhs_name);
5797 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005798 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01005799 lhs->lhs_name);
5800 return FAIL;
5801 }
5802 else if (cctx->ctx_ufunc->uf_script_ctx_version
5803 == SCRIPT_VERSION_VIM9
5804 && script_namespace
5805 && !script_var && import == NULL)
5806 {
5807 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5808 return FAIL;
5809 }
5810
5811 lhs->lhs_dest = dest_script;
5812
5813 // existing script-local variables should have a type
5814 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5815 if (import != NULL)
5816 lhs->lhs_scriptvar_sid = import->imp_sid;
5817 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5818 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005819 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005820 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005821 lhs->lhs_scriptvar_sid, rawname,
5822 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5823 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005824 if (lhs->lhs_scriptvar_idx >= 0)
5825 {
5826 scriptitem_T *si = SCRIPT_ITEM(
5827 lhs->lhs_scriptvar_sid);
5828 svar_T *sv =
5829 ((svar_T *)si->sn_var_vals.ga_data)
5830 + lhs->lhs_scriptvar_idx;
5831 lhs->lhs_type = sv->sv_type;
5832 }
5833 }
5834 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005835 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005836 == FAIL)
5837 return FAIL;
5838 }
5839 }
5840
5841 if (declare_error)
5842 {
5843 vim9_declare_error(lhs->lhs_name);
5844 return FAIL;
5845 }
5846 }
5847
5848 // handle "a:name" as a name, not index "name" on "a"
5849 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5850 var_end = lhs->lhs_dest_end;
5851
5852 if (lhs->lhs_dest != dest_option)
5853 {
5854 if (is_decl && *var_end == ':')
5855 {
5856 char_u *p;
5857
5858 // parse optional type: "let var: type = expr"
5859 if (!VIM_ISWHITE(var_end[1]))
5860 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01005861 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005862 return FAIL;
5863 }
5864 p = skipwhite(var_end + 1);
5865 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5866 if (lhs->lhs_type == NULL)
5867 return FAIL;
5868 lhs->lhs_has_type = TRUE;
5869 }
5870 else if (lhs->lhs_lvar != NULL)
5871 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5872 }
5873
5874 if (oplen == 3 && !heredoc && lhs->lhs_dest != dest_global
5875 && lhs->lhs_type->tt_type != VAR_STRING
5876 && lhs->lhs_type->tt_type != VAR_ANY)
5877 {
5878 emsg(_(e_can_only_concatenate_to_string));
5879 return FAIL;
5880 }
5881
5882 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5883 && cctx->ctx_skip != SKIP_YES)
5884 {
5885 if (oplen > 1 && !heredoc)
5886 {
5887 // +=, /=, etc. require an existing variable
5888 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5889 return FAIL;
5890 }
5891 if (!is_decl)
5892 {
5893 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5894 return FAIL;
5895 }
5896
Bram Moolenaar3f327882021-03-17 20:56:38 +01005897 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005898 if ((lhs->lhs_type->tt_type == VAR_FUNC
5899 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01005900 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01005901 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01005902
5903 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005904 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5905 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5906 if (lhs->lhs_lvar == NULL)
5907 return FAIL;
5908 lhs->lhs_new_local = TRUE;
5909 }
5910
5911 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01005912 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005913 {
5914 // Something follows after the variable: "var[idx]" or "var.key".
5915 // TODO: should we also handle "->func()" here?
5916 if (is_decl)
5917 {
5918 emsg(_(e_cannot_use_index_when_declaring_variable));
5919 return FAIL;
5920 }
5921
5922 if (var_start[lhs->lhs_varlen] == '['
5923 || var_start[lhs->lhs_varlen] == '.')
5924 {
5925 char_u *after = var_start + lhs->lhs_varlen;
5926 char_u *p;
5927
5928 // Only the last index is used below, if there are others
5929 // before it generate code for the expression. Thus for
5930 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5931 for (;;)
5932 {
5933 p = skip_index(after);
5934 if (*p != '[' && *p != '.')
5935 break;
5936 after = p;
5937 }
5938 if (after > var_start + lhs->lhs_varlen)
5939 {
5940 lhs->lhs_varlen = after - var_start;
5941 lhs->lhs_dest = dest_expr;
5942 // We don't know the type before evaluating the expression,
5943 // use "any" until then.
5944 lhs->lhs_type = &t_any;
5945 }
5946
Bram Moolenaar752fc692021-01-04 21:57:11 +01005947 if (lhs->lhs_type->tt_member == NULL)
5948 lhs->lhs_member_type = &t_any;
5949 else
5950 lhs->lhs_member_type = lhs->lhs_type->tt_member;
5951 }
5952 else
5953 {
5954 semsg("Not supported yet: %s", var_start);
5955 return FAIL;
5956 }
5957 }
5958 return OK;
5959}
5960
5961/*
5962 * Assignment to a list or dict member, or ":unlet" for the item, using the
5963 * information in "lhs".
5964 * Returns OK or FAIL.
5965 */
5966 static int
5967compile_assign_unlet(
5968 char_u *var_start,
5969 lhs_T *lhs,
5970 int is_assign,
5971 type_T *rhs_type,
5972 cctx_T *cctx)
5973{
5974 char_u *p;
5975 int r;
5976 vartype_T dest_type;
5977 size_t varlen = lhs->lhs_varlen;
5978 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01005979 int range = FALSE;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005980
5981 // Compile the "idx" in "var[idx]" or "key" in "var.key".
5982 p = var_start + varlen;
5983 if (*p == '[')
5984 {
5985 p = skipwhite(p + 1);
5986 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01005987
5988 if (r == OK && *skipwhite(p) == ':')
5989 {
5990 // unlet var[idx : idx]
5991 if (is_assign)
5992 {
5993 semsg(_(e_cannot_use_range_with_assignment_str), p);
5994 return FAIL;
5995 }
5996 range = TRUE;
5997 p = skipwhite(p);
5998 if (!IS_WHITE_OR_NUL(p[-1]) || !IS_WHITE_OR_NUL(p[1]))
5999 {
6000 semsg(_(e_white_space_required_before_and_after_str_at_str),
6001 ":", p);
6002 return FAIL;
6003 }
6004 p = skipwhite(p + 1);
6005 r = compile_expr0(&p, cctx);
6006 }
6007
Bram Moolenaar752fc692021-01-04 21:57:11 +01006008 if (r == OK && *skipwhite(p) != ']')
6009 {
6010 // this should not happen
6011 emsg(_(e_missbrac));
6012 r = FAIL;
6013 }
6014 }
6015 else // if (*p == '.')
6016 {
6017 char_u *key_end = to_name_end(p + 1, TRUE);
6018 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6019
6020 r = generate_PUSHS(cctx, key);
6021 }
6022 if (r == FAIL)
6023 return FAIL;
6024
6025 if (lhs->lhs_type == &t_any)
6026 {
6027 // Index on variable of unknown type: check at runtime.
6028 dest_type = VAR_ANY;
6029 }
6030 else
6031 {
6032 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006033 if (dest_type == VAR_DICT && range)
6034 {
6035 emsg(e_cannot_use_range_with_dictionary);
6036 return FAIL;
6037 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006038 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
6039 return FAIL;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006040 if (dest_type == VAR_LIST)
6041 {
6042 if (range
6043 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 2],
Bram Moolenaarf30a14d2021-01-17 21:51:24 +01006044 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006045 return FAIL;
6046 if (need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
6047 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
6048 return FAIL;
6049 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006050 }
6051
6052 // Load the dict or list. On the stack we then have:
6053 // - value (for assignment, not for :unlet)
6054 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006055 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006056 // - variable
6057 if (lhs->lhs_dest == dest_expr)
6058 {
6059 int c = var_start[varlen];
6060
6061 // Evaluate "ll[expr]" of "ll[expr][idx]"
6062 p = var_start;
6063 var_start[varlen] = NUL;
6064 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6065 {
6066 // this should not happen
6067 emsg(_(e_missbrac));
6068 return FAIL;
6069 }
6070 var_start[varlen] = c;
6071
6072 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6073 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6074 // now we can properly check the type
6075 if (lhs->lhs_type->tt_member != NULL && rhs_type != &t_void
Bram Moolenaar351ead02021-01-16 16:07:01 +01006076 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006077 FALSE, FALSE) == FAIL)
6078 return FAIL;
6079 }
6080 else
6081 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6082 lhs->lhs_lvar, lhs->lhs_type);
6083
6084 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
6085 {
6086 if (is_assign)
6087 {
6088 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
6089
6090 if (isn == NULL)
6091 return FAIL;
6092 isn->isn_arg.vartype = dest_type;
6093 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006094 else if (range)
6095 {
6096 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6097 return FAIL;
6098 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006099 else
6100 {
6101 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6102 return FAIL;
6103 }
6104 }
6105 else
6106 {
6107 emsg(_(e_indexable_type_required));
6108 return FAIL;
6109 }
6110
6111 return OK;
6112}
6113
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006114/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006115 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006116 * "let name"
6117 * "var name = expr"
6118 * "final name = expr"
6119 * "const name = expr"
6120 * "name = expr"
6121 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006122 * Return NULL for an error.
6123 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006124 */
6125 static char_u *
6126compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6127{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006128 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006129 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006130 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006131 char_u *ret = NULL;
6132 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006133 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006134 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006135 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006136 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006137 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006138 int oplen = 0;
6139 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006140 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006141 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006142 int is_decl = is_decl_command(cmdidx);
6143 lhs_T lhs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006144
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006145 // Skip over the "var" or "[var, var]" to get to any "=".
6146 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6147 if (p == NULL)
6148 return *arg == '[' ? arg : NULL;
6149
6150 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006151 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006152 // TODO: should we allow this, and figure out type inference from list
6153 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006154 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006155 return NULL;
6156 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006157 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006158
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006159 sp = p;
6160 p = skipwhite(p);
6161 op = p;
6162 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006163
6164 if (var_count > 0 && oplen == 0)
6165 // can be something like "[1, 2]->func()"
6166 return arg;
6167
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006168 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006169 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006170 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006171 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006172 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006173
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006174 if (heredoc)
6175 {
6176 list_T *l;
6177 listitem_T *li;
6178
6179 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006180 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006181 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006182 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006183 if (l == NULL)
6184 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006185
Bram Moolenaar078269b2020-09-21 20:35:55 +02006186 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006187 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006188 // Push each line and the create the list.
6189 FOR_ALL_LIST_ITEMS(l, li)
6190 {
6191 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6192 li->li_tv.vval.v_string = NULL;
6193 }
6194 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006195 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006196 list_free(l);
6197 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006198 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006199 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006200 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006201 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006202 char_u *wp;
6203
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006204 // for "[var, var] = expr" evaluate the expression here, loop over the
6205 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006206 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006207
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006208 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006209 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006210 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006211 if (compile_expr0(&p, cctx) == FAIL)
6212 return NULL;
6213 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006214
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006215 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006216 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006217 type_T *stacktype;
6218
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006219 stacktype = stack->ga_len == 0 ? &t_void
6220 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006221 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006223 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006224 goto theend;
6225 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006226 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006227 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006228 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006229 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006230 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6231 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006232 if (stacktype->tt_member != NULL)
6233 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006234 }
6235 }
6236
6237 /*
6238 * Loop over variables in "[var, var] = expr".
6239 * For "var = expr" and "let var: type" this is done only once.
6240 */
6241 if (var_count > 0)
6242 var_start = skipwhite(arg + 1); // skip over the "["
6243 else
6244 var_start = arg;
6245 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6246 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006247 int instr_count = -1;
6248
Bram Moolenaar752fc692021-01-04 21:57:11 +01006249 vim_free(lhs.lhs_name);
6250
6251 /*
6252 * Figure out the LHS type and other properties.
6253 */
6254 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6255 goto theend;
6256
6257 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006258 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006259 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006260 goto theend;
6261 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006262 if (!is_decl && lhs.lhs_lvar != NULL
6263 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006264 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006265 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006266 goto theend;
6267 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006268
6269 if (!heredoc)
6270 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006271 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006272 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006273 if (oplen > 0 && var_count == 0)
6274 {
6275 // skip over the "=" and the expression
6276 p = skipwhite(op + oplen);
6277 compile_expr0(&p, cctx);
6278 }
6279 }
6280 else if (oplen > 0)
6281 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006282 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006283 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006284
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006285 // For "var = expr" evaluate the expression.
6286 if (var_count == 0)
6287 {
6288 int r;
6289
6290 // for "+=", "*=", "..=" etc. first load the current value
6291 if (*op != '=')
6292 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006293 generate_loadvar(cctx, lhs.lhs_dest, lhs.lhs_name,
6294 lhs.lhs_lvar, lhs.lhs_type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006295
Bram Moolenaar752fc692021-01-04 21:57:11 +01006296 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006297 {
6298 // TODO: get member from list or dict
6299 emsg("Index with operation not supported yet");
6300 goto theend;
6301 }
6302 }
6303
6304 // Compile the expression. Temporarily hide the new local
6305 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006306 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006307 --cctx->ctx_locals.ga_len;
6308 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006309 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006310 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006311 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006312 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006313 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006314 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006315 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006316 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006317 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006318 ++cctx->ctx_locals.ga_len;
6319 if (r == FAIL)
6320 goto theend;
6321 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006322 else if (semicolon && var_idx == var_count - 1)
6323 {
6324 // For "[var; var] = expr" get the rest of the list
6325 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6326 goto theend;
6327 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006328 else
6329 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006330 // For "[var, var] = expr" get the "var_idx" item from the
6331 // list.
6332 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006333 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006334 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006335
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006336 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006337 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006338 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006339 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006340 if ((rhs_type->tt_type == VAR_FUNC
6341 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006342 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006343 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006344 goto theend;
6345
Bram Moolenaar752fc692021-01-04 21:57:11 +01006346 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006347 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006348 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006349 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006350 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006351 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006352 }
6353 else
6354 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006355 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006356 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006357 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006358 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006359 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006360 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006361 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006362 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006363 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006364 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006365 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006366 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006367 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006368 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006369 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006370
Bram Moolenaar71abe482020-09-14 22:28:30 +02006371 // without operator check type here, otherwise below
Bram Moolenaar752fc692021-01-04 21:57:11 +01006372 if (lhs.lhs_has_index)
6373 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006374 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006375 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006376 goto theend;
6377 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006378 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006379 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006380 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006381 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006382 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006383 else if (cmdidx == CMD_final)
6384 {
6385 emsg(_(e_final_requires_a_value));
6386 goto theend;
6387 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006388 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006389 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006390 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006391 goto theend;
6392 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006393 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006394 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006395 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006396 goto theend;
6397 }
6398 else
6399 {
6400 // variables are always initialized
6401 if (ga_grow(instr, 1) == FAIL)
6402 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006403 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006404 {
6405 case VAR_BOOL:
6406 generate_PUSHBOOL(cctx, VVAL_FALSE);
6407 break;
6408 case VAR_FLOAT:
6409#ifdef FEAT_FLOAT
6410 generate_PUSHF(cctx, 0.0);
6411#endif
6412 break;
6413 case VAR_STRING:
6414 generate_PUSHS(cctx, NULL);
6415 break;
6416 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006417 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006418 break;
6419 case VAR_FUNC:
6420 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6421 break;
6422 case VAR_LIST:
6423 generate_NEWLIST(cctx, 0);
6424 break;
6425 case VAR_DICT:
6426 generate_NEWDICT(cctx, 0);
6427 break;
6428 case VAR_JOB:
6429 generate_PUSHJOB(cctx, NULL);
6430 break;
6431 case VAR_CHANNEL:
6432 generate_PUSHCHANNEL(cctx, NULL);
6433 break;
6434 case VAR_NUMBER:
6435 case VAR_UNKNOWN:
6436 case VAR_ANY:
6437 case VAR_PARTIAL:
6438 case VAR_VOID:
6439 case VAR_SPECIAL: // cannot happen
6440 generate_PUSHNR(cctx, 0);
6441 break;
6442 }
6443 }
6444 if (var_count == 0)
6445 end = p;
6446 }
6447
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006448 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006449 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006450 break;
6451
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006452 if (oplen > 0 && *op != '=')
6453 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006454 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006455 type_T *stacktype;
6456
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006457 if (*op == '.')
6458 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006459 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006460 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006461 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006462 if (
6463#ifdef FEAT_FLOAT
6464 // If variable is float operation with number is OK.
6465 !(expected == &t_float && stacktype == &t_number) &&
6466#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006467 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006468 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006469 goto theend;
6470
6471 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006472 {
6473 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6474 goto theend;
6475 }
6476 else if (*op == '+')
6477 {
6478 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006479 operator_type(lhs.lhs_member_type, stacktype),
6480 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006481 goto theend;
6482 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006483 else if (generate_two_op(cctx, op) == FAIL)
6484 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006485 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006486
Bram Moolenaar752fc692021-01-04 21:57:11 +01006487 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006488 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006489 // Use the info in "lhs" to store the value at the index in the
6490 // list or dict.
6491 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6492 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006493 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006494 }
6495 else
6496 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006497 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
6498 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006499 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006500 generate_LOCKCONST(cctx);
6501
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006502 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006503 && (lhs.lhs_type->tt_type == VAR_DICT
6504 || lhs.lhs_type->tt_type == VAR_LIST)
6505 && lhs.lhs_type->tt_member != NULL
6506 && lhs.lhs_type->tt_member != &t_any
6507 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006508 // Set the type in the list or dict, so that it can be checked,
6509 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006510 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006511
Bram Moolenaar752fc692021-01-04 21:57:11 +01006512 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006513 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006514 if (generate_store_var(cctx, lhs.lhs_dest,
6515 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6516 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6517 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006518 goto theend;
6519 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006520 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006521 {
6522 isn_T *isn = ((isn_T *)instr->ga_data)
6523 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006524
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006525 // optimization: turn "var = 123" from ISN_PUSHNR +
6526 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006527 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006528 && instr->ga_len == instr_count + 1
6529 && isn->isn_type == ISN_PUSHNR)
6530 {
6531 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006532
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006533 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006534 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006535 isn->isn_arg.storenr.stnr_val = val;
6536 if (stack->ga_len > 0)
6537 --stack->ga_len;
6538 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006539 else if (lhs.lhs_lvar->lv_from_outer > 0)
6540 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6541 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006542 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006543 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006544 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006545 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006546
6547 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006548 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006549 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006550
6551 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006552 if (var_count > 0 && !semicolon)
6553 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006554 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006555 goto theend;
6556 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006557
Bram Moolenaarb2097502020-07-19 17:17:02 +02006558 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006559
6560theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006561 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006562 return ret;
6563}
6564
6565/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006566 * Check for an assignment at "eap->cmd", compile it if found.
6567 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6568 */
6569 static int
6570may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6571{
6572 char_u *pskip;
6573 char_u *p;
6574
6575 // Assuming the command starts with a variable or function name,
6576 // find what follows.
6577 // Skip over "var.member", "var[idx]" and the like.
6578 // Also "&opt = val", "$ENV = val" and "@r = val".
6579 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6580 ? eap->cmd + 1 : eap->cmd;
6581 p = to_name_end(pskip, TRUE);
6582 if (p > eap->cmd && *p != NUL)
6583 {
6584 char_u *var_end;
6585 int oplen;
6586 int heredoc;
6587
6588 if (eap->cmd[0] == '@')
6589 var_end = eap->cmd + 2;
6590 else
6591 var_end = find_name_end(pskip, NULL, NULL,
6592 FNE_CHECK_START | FNE_INCL_BR);
6593 oplen = assignment_len(skipwhite(var_end), &heredoc);
6594 if (oplen > 0)
6595 {
6596 size_t len = p - eap->cmd;
6597
6598 // Recognize an assignment if we recognize the variable
6599 // name:
6600 // "g:var = expr"
6601 // "local = expr" where "local" is a local var.
6602 // "script = expr" where "script" is a script-local var.
6603 // "import = expr" where "import" is an imported var
6604 // "&opt = expr"
6605 // "$ENV = expr"
6606 // "@r = expr"
6607 if (*eap->cmd == '&'
6608 || *eap->cmd == '$'
6609 || *eap->cmd == '@'
6610 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01006611 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01006612 {
6613 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6614 if (*line == NULL || *line == eap->cmd)
6615 return FAIL;
6616 return OK;
6617 }
6618 }
6619 }
6620
6621 if (*eap->cmd == '[')
6622 {
6623 // [var, var] = expr
6624 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6625 if (*line == NULL)
6626 return FAIL;
6627 if (*line != eap->cmd)
6628 return OK;
6629 }
6630 return NOTDONE;
6631}
6632
6633/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006634 * Check if "name" can be "unlet".
6635 */
6636 int
6637check_vim9_unlet(char_u *name)
6638{
6639 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6640 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006641 // "unlet s:var" is allowed in legacy script.
6642 if (*name == 's' && !script_is_vim9())
6643 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006644 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006645 return FAIL;
6646 }
6647 return OK;
6648}
6649
6650/*
6651 * Callback passed to ex_unletlock().
6652 */
6653 static int
6654compile_unlet(
6655 lval_T *lvp,
6656 char_u *name_end,
6657 exarg_T *eap,
6658 int deep UNUSED,
6659 void *coookie)
6660{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006661 cctx_T *cctx = coookie;
6662 char_u *p = lvp->ll_name;
6663 int cc = *name_end;
6664 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006665
Bram Moolenaar752fc692021-01-04 21:57:11 +01006666 if (cctx->ctx_skip == SKIP_YES)
6667 return OK;
6668
6669 *name_end = NUL;
6670 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006671 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006672 // :unlet $ENV_VAR
6673 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6674 }
6675 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6676 {
6677 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006678
Bram Moolenaar752fc692021-01-04 21:57:11 +01006679 // This is similar to assigning: lookup the list/dict, compile the
6680 // idx/key. Then instead of storing the value unlet the item.
6681 // unlet {list}[idx]
6682 // unlet {dict}[key] dict.key
6683 //
6684 // Figure out the LHS type and other properties.
6685 //
6686 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6687
6688 // : unlet an indexed item
6689 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006690 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006691 iemsg("called compile_lhs() without an index");
6692 ret = FAIL;
6693 }
6694 else
6695 {
6696 // Use the info in "lhs" to unlet the item at the index in the
6697 // list or dict.
6698 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006699 }
6700
Bram Moolenaar752fc692021-01-04 21:57:11 +01006701 vim_free(lhs.lhs_name);
6702 }
6703 else if (check_vim9_unlet(p) == FAIL)
6704 {
6705 ret = FAIL;
6706 }
6707 else
6708 {
6709 // Normal name. Only supports g:, w:, t: and b: namespaces.
6710 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006711 }
6712
Bram Moolenaar752fc692021-01-04 21:57:11 +01006713 *name_end = cc;
6714 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006715}
6716
6717/*
6718 * compile "unlet var", "lock var" and "unlock var"
6719 * "arg" points to "var".
6720 */
6721 static char_u *
6722compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6723{
6724 char_u *p = arg;
6725
6726 if (eap->cmdidx != CMD_unlet)
6727 {
6728 emsg("Sorry, :lock and unlock not implemented yet");
6729 return NULL;
6730 }
6731
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006732 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6733 compile_unlet, cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006734 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6735}
6736
6737/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006738 * Compile an :import command.
6739 */
6740 static char_u *
6741compile_import(char_u *arg, cctx_T *cctx)
6742{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006743 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006744}
6745
6746/*
6747 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6748 */
6749 static int
6750compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6751{
6752 garray_T *instr = &cctx->ctx_instr;
6753 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6754
6755 if (endlabel == NULL)
6756 return FAIL;
6757 endlabel->el_next = *el;
6758 *el = endlabel;
6759 endlabel->el_end_label = instr->ga_len;
6760
6761 generate_JUMP(cctx, when, 0);
6762 return OK;
6763}
6764
6765 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006766compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006767{
6768 garray_T *instr = &cctx->ctx_instr;
6769
6770 while (*el != NULL)
6771 {
6772 endlabel_T *cur = (*el);
6773 isn_T *isn;
6774
6775 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006776 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006777 *el = cur->el_next;
6778 vim_free(cur);
6779 }
6780}
6781
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006782 static void
6783compile_free_jump_to_end(endlabel_T **el)
6784{
6785 while (*el != NULL)
6786 {
6787 endlabel_T *cur = (*el);
6788
6789 *el = cur->el_next;
6790 vim_free(cur);
6791 }
6792}
6793
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006794/*
6795 * Create a new scope and set up the generic items.
6796 */
6797 static scope_T *
6798new_scope(cctx_T *cctx, scopetype_T type)
6799{
6800 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6801
6802 if (scope == NULL)
6803 return NULL;
6804 scope->se_outer = cctx->ctx_scope;
6805 cctx->ctx_scope = scope;
6806 scope->se_type = type;
6807 scope->se_local_count = cctx->ctx_locals.ga_len;
6808 return scope;
6809}
6810
6811/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006812 * Free the current scope and go back to the outer scope.
6813 */
6814 static void
6815drop_scope(cctx_T *cctx)
6816{
6817 scope_T *scope = cctx->ctx_scope;
6818
6819 if (scope == NULL)
6820 {
6821 iemsg("calling drop_scope() without a scope");
6822 return;
6823 }
6824 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006825 switch (scope->se_type)
6826 {
6827 case IF_SCOPE:
6828 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6829 case FOR_SCOPE:
6830 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6831 case WHILE_SCOPE:
6832 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6833 case TRY_SCOPE:
6834 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6835 case NO_SCOPE:
6836 case BLOCK_SCOPE:
6837 break;
6838 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006839 vim_free(scope);
6840}
6841
6842/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006843 * compile "if expr"
6844 *
6845 * "if expr" Produces instructions:
6846 * EVAL expr Push result of "expr"
6847 * JUMP_IF_FALSE end
6848 * ... body ...
6849 * end:
6850 *
6851 * "if expr | else" Produces instructions:
6852 * EVAL expr Push result of "expr"
6853 * JUMP_IF_FALSE else
6854 * ... body ...
6855 * JUMP_ALWAYS end
6856 * else:
6857 * ... body ...
6858 * end:
6859 *
6860 * "if expr1 | elseif expr2 | else" Produces instructions:
6861 * EVAL expr Push result of "expr"
6862 * JUMP_IF_FALSE elseif
6863 * ... body ...
6864 * JUMP_ALWAYS end
6865 * elseif:
6866 * EVAL expr Push result of "expr"
6867 * JUMP_IF_FALSE else
6868 * ... body ...
6869 * JUMP_ALWAYS end
6870 * else:
6871 * ... body ...
6872 * end:
6873 */
6874 static char_u *
6875compile_if(char_u *arg, cctx_T *cctx)
6876{
6877 char_u *p = arg;
6878 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006879 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006880 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006881 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006882 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006883
Bram Moolenaara5565e42020-05-09 15:44:01 +02006884 CLEAR_FIELD(ppconst);
6885 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006886 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006887 clear_ppconst(&ppconst);
6888 return NULL;
6889 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01006890 if (!ends_excmd2(arg, skipwhite(p)))
6891 {
6892 semsg(_(e_trailing_arg), p);
6893 return NULL;
6894 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006895 if (cctx->ctx_skip == SKIP_YES)
6896 clear_ppconst(&ppconst);
6897 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006898 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006899 int error = FALSE;
6900 int v;
6901
Bram Moolenaara5565e42020-05-09 15:44:01 +02006902 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006903 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006904 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006905 if (error)
6906 return NULL;
6907 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006908 }
6909 else
6910 {
6911 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006912 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006913 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006914 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006915 if (bool_on_stack(cctx) == FAIL)
6916 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006917 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006918
Bram Moolenaara91a7132021-03-25 21:12:15 +01006919 // CMDMOD_REV must come before the jump
6920 generate_undo_cmdmods(cctx);
6921
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006922 scope = new_scope(cctx, IF_SCOPE);
6923 if (scope == NULL)
6924 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006925 scope->se_skip_save = skip_save;
6926 // "is_had_return" will be reset if any block does not end in :return
6927 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006928
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006929 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006930 {
6931 // "where" is set when ":elseif", "else" or ":endif" is found
6932 scope->se_u.se_if.is_if_label = instr->ga_len;
6933 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6934 }
6935 else
6936 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006937
Bram Moolenaarced68a02021-01-24 17:53:47 +01006938#ifdef FEAT_PROFILE
6939 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
6940 && skip_save != SKIP_YES)
6941 {
6942 // generated a profile start, need to generate a profile end, since it
6943 // won't be done after returning
6944 cctx->ctx_skip = SKIP_NOT;
6945 generate_instr(cctx, ISN_PROF_END);
6946 cctx->ctx_skip = SKIP_YES;
6947 }
6948#endif
6949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006950 return p;
6951}
6952
6953 static char_u *
6954compile_elseif(char_u *arg, cctx_T *cctx)
6955{
6956 char_u *p = arg;
6957 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006958 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006959 isn_T *isn;
6960 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006961 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006962 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006963
6964 if (scope == NULL || scope->se_type != IF_SCOPE)
6965 {
6966 emsg(_(e_elseif_without_if));
6967 return NULL;
6968 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006969 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006970 if (!cctx->ctx_had_return)
6971 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006972
Bram Moolenaarced68a02021-01-24 17:53:47 +01006973 if (cctx->ctx_skip == SKIP_NOT)
6974 {
6975 // previous block was executed, this one and following will not
6976 cctx->ctx_skip = SKIP_YES;
6977 scope->se_u.se_if.is_seen_skip_not = TRUE;
6978 }
6979 if (scope->se_u.se_if.is_seen_skip_not)
6980 {
6981 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01006982 // Do not count the "elseif" for profiling and cmdmod
6983 instr->ga_len = current_instr_idx(cctx);
6984
Bram Moolenaarced68a02021-01-24 17:53:47 +01006985 skip_expr_cctx(&p, cctx);
6986 return p;
6987 }
6988
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006989 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006990 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01006991 int moved_cmdmod = FALSE;
6992
6993 // Move any CMDMOD instruction to after the jump
6994 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
6995 {
6996 if (ga_grow(instr, 1) == FAIL)
6997 return NULL;
6998 ((isn_T *)instr->ga_data)[instr->ga_len] =
6999 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7000 --instr->ga_len;
7001 moved_cmdmod = TRUE;
7002 }
7003
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007004 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007005 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007006 return NULL;
7007 // previous "if" or "elseif" jumps here
7008 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7009 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007010 if (moved_cmdmod)
7011 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007012 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007013
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007014 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007015 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007016 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007017 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007018 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007019#ifdef FEAT_PROFILE
7020 if (cctx->ctx_profiling)
7021 {
7022 // the previous block was skipped, need to profile this line
7023 generate_instr(cctx, ISN_PROF_START);
7024 instr_count = instr->ga_len;
7025 }
7026#endif
7027 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007028 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007029 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007030 clear_ppconst(&ppconst);
7031 return NULL;
7032 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007033 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007034 if (!ends_excmd2(arg, skipwhite(p)))
7035 {
7036 semsg(_(e_trailing_arg), p);
7037 return NULL;
7038 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007039 if (scope->se_skip_save == SKIP_YES)
7040 clear_ppconst(&ppconst);
7041 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007042 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007043 int error = FALSE;
7044 int v;
7045
Bram Moolenaar7f141552020-05-09 17:35:53 +02007046 // The expression results in a constant.
7047 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007048 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7049 if (error)
7050 return NULL;
7051 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007052 clear_ppconst(&ppconst);
7053 scope->se_u.se_if.is_if_label = -1;
7054 }
7055 else
7056 {
7057 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007058 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007059 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007060 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007061 if (bool_on_stack(cctx) == FAIL)
7062 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007063
Bram Moolenaara91a7132021-03-25 21:12:15 +01007064 // CMDMOD_REV must come before the jump
7065 generate_undo_cmdmods(cctx);
7066
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007067 // "where" is set when ":elseif", "else" or ":endif" is found
7068 scope->se_u.se_if.is_if_label = instr->ga_len;
7069 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7070 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007071
7072 return p;
7073}
7074
7075 static char_u *
7076compile_else(char_u *arg, cctx_T *cctx)
7077{
7078 char_u *p = arg;
7079 garray_T *instr = &cctx->ctx_instr;
7080 isn_T *isn;
7081 scope_T *scope = cctx->ctx_scope;
7082
7083 if (scope == NULL || scope->se_type != IF_SCOPE)
7084 {
7085 emsg(_(e_else_without_if));
7086 return NULL;
7087 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007088 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007089 if (!cctx->ctx_had_return)
7090 scope->se_u.se_if.is_had_return = FALSE;
7091 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007092
Bram Moolenaarced68a02021-01-24 17:53:47 +01007093#ifdef FEAT_PROFILE
7094 if (cctx->ctx_profiling)
7095 {
7096 if (cctx->ctx_skip == SKIP_NOT
7097 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7098 .isn_type == ISN_PROF_START)
7099 // the previous block was executed, do not count "else" for profiling
7100 --instr->ga_len;
7101 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7102 {
7103 // the previous block was not executed, this one will, do count the
7104 // "else" for profiling
7105 cctx->ctx_skip = SKIP_NOT;
7106 generate_instr(cctx, ISN_PROF_END);
7107 generate_instr(cctx, ISN_PROF_START);
7108 cctx->ctx_skip = SKIP_YES;
7109 }
7110 }
7111#endif
7112
7113 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007114 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007115 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007116 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007117 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007118 if (!cctx->ctx_had_return
7119 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7120 JUMP_ALWAYS, cctx) == FAIL)
7121 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007122 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007123
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007124 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007125 {
7126 if (scope->se_u.se_if.is_if_label >= 0)
7127 {
7128 // previous "if" or "elseif" jumps here
7129 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7130 isn->isn_arg.jump.jump_where = instr->ga_len;
7131 scope->se_u.se_if.is_if_label = -1;
7132 }
7133 }
7134
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007135 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007136 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7137 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007138
7139 return p;
7140}
7141
7142 static char_u *
7143compile_endif(char_u *arg, cctx_T *cctx)
7144{
7145 scope_T *scope = cctx->ctx_scope;
7146 ifscope_T *ifscope;
7147 garray_T *instr = &cctx->ctx_instr;
7148 isn_T *isn;
7149
Bram Moolenaara91a7132021-03-25 21:12:15 +01007150 drop_cmdmod(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007151 if (scope == NULL || scope->se_type != IF_SCOPE)
7152 {
7153 emsg(_(e_endif_without_if));
7154 return NULL;
7155 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007156 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007157 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007158 if (!cctx->ctx_had_return)
7159 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007160
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007161 if (scope->se_u.se_if.is_if_label >= 0)
7162 {
7163 // previous "if" or "elseif" jumps here
7164 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7165 isn->isn_arg.jump.jump_where = instr->ga_len;
7166 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007167 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007168 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007169
7170#ifdef FEAT_PROFILE
7171 // even when skipping we count the endif as executed, unless the block it's
7172 // in is skipped
7173 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7174 && scope->se_skip_save != SKIP_YES)
7175 {
7176 cctx->ctx_skip = SKIP_NOT;
7177 generate_instr(cctx, ISN_PROF_START);
7178 }
7179#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007180 cctx->ctx_skip = scope->se_skip_save;
7181
7182 // If all the blocks end in :return and there is an :else then the
7183 // had_return flag is set.
7184 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007185
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007186 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007187 return arg;
7188}
7189
7190/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007191 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007192 *
7193 * Produces instructions:
7194 * PUSHNR -1
7195 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007196 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007197 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7198 * - if beyond end, jump to "end"
7199 * - otherwise get item from list and push it
7200 * STORE var Store item in "var"
7201 * ... body ...
7202 * JUMP top Jump back to repeat
7203 * end: DROP Drop the result of "expr"
7204 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007205 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7206 * UNPACK 2 Split item in 2
7207 * STORE var1 Store item in "var1"
7208 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007209 */
7210 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007211compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007212{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007213 char_u *arg;
7214 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007215 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007216 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007217 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007218 int var_count = 0;
7219 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007220 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007221 garray_T *stack = &cctx->ctx_type_stack;
7222 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007223 lvar_T *loop_lvar; // loop iteration variable
7224 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007225 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007226 type_T *item_type = &t_any;
7227 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007228
Bram Moolenaar792f7862020-11-23 08:31:18 +01007229 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007230 if (p == NULL)
7231 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007232 if (var_count == 0)
7233 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007234
7235 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007236 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007237 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7238 return NULL;
7239 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007240 {
7241 emsg(_(e_missing_in));
7242 return NULL;
7243 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007244 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007245 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7246 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007247
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007248 scope = new_scope(cctx, FOR_SCOPE);
7249 if (scope == NULL)
7250 return NULL;
7251
Bram Moolenaar792f7862020-11-23 08:31:18 +01007252 // Reserve a variable to store the loop iteration counter and initialize it
7253 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007254 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7255 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007256 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007257 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007258 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007259 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007260 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007261 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007262
7263 // compile "expr", it remains on the stack until "endfor"
7264 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007265 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007266 {
7267 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007268 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007269 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007270 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007271
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007272 // Now that we know the type of "var", check that it is a list, now or at
7273 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007274 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01007275 if (need_type(vartype, &t_list_any, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007276 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007277 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007278 return NULL;
7279 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007280
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007281 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007282 {
7283 if (var_count == 1)
7284 item_type = vartype->tt_member;
7285 else if (vartype->tt_member->tt_type == VAR_LIST
7286 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
7287 item_type = vartype->tt_member->tt_member;
7288 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007289
Bram Moolenaara91a7132021-03-25 21:12:15 +01007290 // CMDMOD_REV must come before the FOR instruction
7291 generate_undo_cmdmods(cctx);
7292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007293 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007294 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007295 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007296
Bram Moolenaar792f7862020-11-23 08:31:18 +01007297 arg = arg_start;
7298 if (var_count > 1)
7299 {
7300 generate_UNPACK(cctx, var_count, semicolon);
7301 arg = skipwhite(arg + 1); // skip white after '['
7302
7303 // the list item is replaced by a number of items
7304 if (ga_grow(stack, var_count - 1) == FAIL)
7305 {
7306 drop_scope(cctx);
7307 return NULL;
7308 }
7309 --stack->ga_len;
7310 for (idx = 0; idx < var_count; ++idx)
7311 {
7312 ((type_T **)stack->ga_data)[stack->ga_len] =
7313 (semicolon && idx == 0) ? vartype : item_type;
7314 ++stack->ga_len;
7315 }
7316 }
7317
7318 for (idx = 0; idx < var_count; ++idx)
7319 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007320 assign_dest_T dest = dest_local;
7321 int opt_flags = 0;
7322 int vimvaridx = -1;
7323 type_T *type = &t_any;
7324
7325 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007326 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007327 name = vim_strnsave(arg, varlen);
7328 if (name == NULL)
7329 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007330
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007331 // TODO: script var not supported?
7332 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7333 &vimvaridx, &type, cctx) == FAIL)
7334 goto failed;
7335 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007336 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007337 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7338 0, 0, type, name) == FAIL)
7339 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007340 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007341 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007342 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007343 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007344 {
7345 semsg(_(e_variable_already_declared), arg);
7346 goto failed;
7347 }
7348
Bram Moolenaarea870692020-12-02 14:24:30 +01007349 if (STRNCMP(name, "s:", 2) == 0)
7350 {
7351 semsg(_(e_cannot_declare_script_variable_in_function), name);
7352 goto failed;
7353 }
7354
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007355 // Reserve a variable to store "var".
7356 // TODO: check for type
7357 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
7358 if (var_lvar == NULL)
7359 // out of memory or used as an argument
7360 goto failed;
7361
7362 if (semicolon && idx == var_count - 1)
7363 var_lvar->lv_type = vartype;
7364 else
7365 var_lvar->lv_type = item_type;
7366 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7367 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007368
Bram Moolenaar036d0712021-01-17 20:23:38 +01007369 if (*p == ':')
7370 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007371 if (*p == ',' || *p == ';')
7372 ++p;
7373 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007374 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007375 }
7376
7377 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007378
7379failed:
7380 vim_free(name);
7381 drop_scope(cctx);
7382 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007383}
7384
7385/*
7386 * compile "endfor"
7387 */
7388 static char_u *
7389compile_endfor(char_u *arg, cctx_T *cctx)
7390{
7391 garray_T *instr = &cctx->ctx_instr;
7392 scope_T *scope = cctx->ctx_scope;
7393 forscope_T *forscope;
7394 isn_T *isn;
7395
Bram Moolenaara91a7132021-03-25 21:12:15 +01007396 drop_cmdmod(cctx);
7397
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007398 if (scope == NULL || scope->se_type != FOR_SCOPE)
7399 {
7400 emsg(_(e_for));
7401 return NULL;
7402 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007403 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007404 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007405 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007406
7407 // At end of ":for" scope jump back to the FOR instruction.
7408 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7409
7410 // Fill in the "end" label in the FOR statement so it can jump here
7411 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7412 isn->isn_arg.forloop.for_end = instr->ga_len;
7413
7414 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007415 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007416
7417 // Below the ":for" scope drop the "expr" list from the stack.
7418 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7419 return NULL;
7420
7421 vim_free(scope);
7422
7423 return arg;
7424}
7425
7426/*
7427 * compile "while expr"
7428 *
7429 * Produces instructions:
7430 * top: EVAL expr Push result of "expr"
7431 * JUMP_IF_FALSE end jump if false
7432 * ... body ...
7433 * JUMP top Jump back to repeat
7434 * end:
7435 *
7436 */
7437 static char_u *
7438compile_while(char_u *arg, cctx_T *cctx)
7439{
7440 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007441 scope_T *scope;
7442
7443 scope = new_scope(cctx, WHILE_SCOPE);
7444 if (scope == NULL)
7445 return NULL;
7446
Bram Moolenaara91a7132021-03-25 21:12:15 +01007447 // "endwhile" jumps back here, one before when profiling or using cmdmods
7448 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007449
7450 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007451 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007452 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007453 if (!ends_excmd2(arg, skipwhite(p)))
7454 {
7455 semsg(_(e_trailing_arg), p);
7456 return NULL;
7457 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007458
Bram Moolenaar13106602020-10-04 16:06:05 +02007459 if (bool_on_stack(cctx) == FAIL)
7460 return FAIL;
7461
Bram Moolenaara91a7132021-03-25 21:12:15 +01007462 // CMDMOD_REV must come before the jump
7463 generate_undo_cmdmods(cctx);
7464
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007465 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007466 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007467 JUMP_IF_FALSE, cctx) == FAIL)
7468 return FAIL;
7469
7470 return p;
7471}
7472
7473/*
7474 * compile "endwhile"
7475 */
7476 static char_u *
7477compile_endwhile(char_u *arg, cctx_T *cctx)
7478{
7479 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007480 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007481
Bram Moolenaara91a7132021-03-25 21:12:15 +01007482 drop_cmdmod(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007483 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7484 {
7485 emsg(_(e_while));
7486 return NULL;
7487 }
7488 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007489 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007490
Bram Moolenaarf002a412021-01-24 13:34:18 +01007491#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007492 // count the endwhile before jumping
7493 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007494#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007495
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007496 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007497 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007498
7499 // Fill in the "end" label in the WHILE statement so it can jump here.
7500 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007501 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7502 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007503
7504 vim_free(scope);
7505
7506 return arg;
7507}
7508
7509/*
7510 * compile "continue"
7511 */
7512 static char_u *
7513compile_continue(char_u *arg, cctx_T *cctx)
7514{
7515 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007516 int try_scopes = 0;
7517 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007518
7519 for (;;)
7520 {
7521 if (scope == NULL)
7522 {
7523 emsg(_(e_continue));
7524 return NULL;
7525 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01007526 if (scope->se_type == FOR_SCOPE)
7527 {
7528 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007529 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007530 }
7531 if (scope->se_type == WHILE_SCOPE)
7532 {
7533 loop_label = scope->se_u.se_while.ws_top_label;
7534 break;
7535 }
7536 if (scope->se_type == TRY_SCOPE)
7537 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007538 scope = scope->se_outer;
7539 }
7540
Bram Moolenaarc150c092021-02-13 15:02:46 +01007541 if (try_scopes > 0)
7542 // Inside one or more try/catch blocks we first need to jump to the
7543 // "finally" or "endtry" to cleanup.
7544 generate_TRYCONT(cctx, try_scopes, loop_label);
7545 else
7546 // Jump back to the FOR or WHILE instruction.
7547 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
7548
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007549 return arg;
7550}
7551
7552/*
7553 * compile "break"
7554 */
7555 static char_u *
7556compile_break(char_u *arg, cctx_T *cctx)
7557{
7558 scope_T *scope = cctx->ctx_scope;
7559 endlabel_T **el;
7560
7561 for (;;)
7562 {
7563 if (scope == NULL)
7564 {
7565 emsg(_(e_break));
7566 return NULL;
7567 }
7568 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7569 break;
7570 scope = scope->se_outer;
7571 }
7572
7573 // Jump to the end of the FOR or WHILE loop.
7574 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007575 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007576 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007577 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007578 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7579 return FAIL;
7580
7581 return arg;
7582}
7583
7584/*
7585 * compile "{" start of block
7586 */
7587 static char_u *
7588compile_block(char_u *arg, cctx_T *cctx)
7589{
7590 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7591 return NULL;
7592 return skipwhite(arg + 1);
7593}
7594
7595/*
7596 * compile end of block: drop one scope
7597 */
7598 static void
7599compile_endblock(cctx_T *cctx)
7600{
7601 scope_T *scope = cctx->ctx_scope;
7602
7603 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007604 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007605 vim_free(scope);
7606}
7607
7608/*
7609 * compile "try"
7610 * Creates a new scope for the try-endtry, pointing to the first catch and
7611 * finally.
7612 * Creates another scope for the "try" block itself.
7613 * TRY instruction sets up exception handling at runtime.
7614 *
7615 * "try"
7616 * TRY -> catch1, -> finally push trystack entry
7617 * ... try block
7618 * "throw {exception}"
7619 * EVAL {exception}
7620 * THROW create exception
7621 * ... try block
7622 * " catch {expr}"
7623 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007624 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007625 * EVAL {expr}
7626 * MATCH
7627 * JUMP nomatch -> catch2
7628 * CATCH remove exception
7629 * ... catch block
7630 * " catch"
7631 * JUMP -> finally
7632 * catch2: CATCH remove exception
7633 * ... catch block
7634 * " finally"
7635 * finally:
7636 * ... finally block
7637 * " endtry"
7638 * ENDTRY pop trystack entry, may rethrow
7639 */
7640 static char_u *
7641compile_try(char_u *arg, cctx_T *cctx)
7642{
7643 garray_T *instr = &cctx->ctx_instr;
7644 scope_T *try_scope;
7645 scope_T *scope;
7646
7647 // scope that holds the jumps that go to catch/finally/endtry
7648 try_scope = new_scope(cctx, TRY_SCOPE);
7649 if (try_scope == NULL)
7650 return NULL;
7651
Bram Moolenaar69f70502021-01-01 16:10:46 +01007652 if (cctx->ctx_skip != SKIP_YES)
7653 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007654 isn_T *isn;
7655
7656 // "try_catch" is set when the first ":catch" is found or when no catch
7657 // is found and ":finally" is found.
7658 // "try_finally" is set when ":finally" is found
7659 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01007660 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007661 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
7662 return NULL;
7663 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
7664 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007665 return NULL;
7666 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007667
7668 // scope for the try block itself
7669 scope = new_scope(cctx, BLOCK_SCOPE);
7670 if (scope == NULL)
7671 return NULL;
7672
7673 return arg;
7674}
7675
7676/*
7677 * compile "catch {expr}"
7678 */
7679 static char_u *
7680compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7681{
7682 scope_T *scope = cctx->ctx_scope;
7683 garray_T *instr = &cctx->ctx_instr;
7684 char_u *p;
7685 isn_T *isn;
7686
7687 // end block scope from :try or :catch
7688 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7689 compile_endblock(cctx);
7690 scope = cctx->ctx_scope;
7691
7692 // Error if not in a :try scope
7693 if (scope == NULL || scope->se_type != TRY_SCOPE)
7694 {
7695 emsg(_(e_catch));
7696 return NULL;
7697 }
7698
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007699 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007700 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007701 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007702 return NULL;
7703 }
7704
Bram Moolenaar69f70502021-01-01 16:10:46 +01007705 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007706 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007707#ifdef FEAT_PROFILE
7708 // the profile-start should be after the jump
7709 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7710 .isn_type == ISN_PROF_START)
7711 --instr->ga_len;
7712#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01007713 // Jump from end of previous block to :finally or :endtry
7714 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7715 JUMP_ALWAYS, cctx) == FAIL)
7716 return NULL;
7717
7718 // End :try or :catch scope: set value in ISN_TRY instruction
7719 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007720 if (isn->isn_arg.try.try_ref->try_catch == 0)
7721 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007722 if (scope->se_u.se_try.ts_catch_label != 0)
7723 {
7724 // Previous catch without match jumps here
7725 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7726 isn->isn_arg.jump.jump_where = instr->ga_len;
7727 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007728#ifdef FEAT_PROFILE
7729 if (cctx->ctx_profiling)
7730 {
7731 // a "throw" that jumps here needs to be counted
7732 generate_instr(cctx, ISN_PROF_END);
7733 // the "catch" is also counted
7734 generate_instr(cctx, ISN_PROF_START);
7735 }
7736#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007737 }
7738
7739 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007740 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007741 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007742 scope->se_u.se_try.ts_caught_all = TRUE;
7743 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007744 }
7745 else
7746 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007747 char_u *end;
7748 char_u *pat;
7749 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007750 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007751 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007752
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007753 // Push v:exception, push {expr} and MATCH
7754 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7755
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007756 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007757 if (*end != *p)
7758 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007759 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007760 vim_free(tofree);
7761 return FAIL;
7762 }
7763 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007764 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007765 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007766 len = (int)(end - tofree);
7767 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007768 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007769 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007770 if (pat == NULL)
7771 return FAIL;
7772 if (generate_PUSHS(cctx, pat) == FAIL)
7773 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007774
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007775 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7776 return NULL;
7777
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007778 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007779 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7780 return NULL;
7781 }
7782
Bram Moolenaar69f70502021-01-01 16:10:46 +01007783 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007784 return NULL;
7785
7786 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7787 return NULL;
7788 return p;
7789}
7790
7791 static char_u *
7792compile_finally(char_u *arg, cctx_T *cctx)
7793{
7794 scope_T *scope = cctx->ctx_scope;
7795 garray_T *instr = &cctx->ctx_instr;
7796 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007797 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007798
7799 // end block scope from :try or :catch
7800 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7801 compile_endblock(cctx);
7802 scope = cctx->ctx_scope;
7803
7804 // Error if not in a :try scope
7805 if (scope == NULL || scope->se_type != TRY_SCOPE)
7806 {
7807 emsg(_(e_finally));
7808 return NULL;
7809 }
7810
7811 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007812 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007813 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007814 {
7815 emsg(_(e_finally_dup));
7816 return NULL;
7817 }
7818
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007819 this_instr = instr->ga_len;
7820#ifdef FEAT_PROFILE
7821 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7822 .isn_type == ISN_PROF_START)
7823 // jump to the profile start of the "finally"
7824 --this_instr;
7825#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007826
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007827 // Fill in the "end" label in jumps at the end of the blocks.
7828 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
7829 this_instr, cctx);
7830
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007831 // If there is no :catch then an exception jumps to :finally.
7832 if (isn->isn_arg.try.try_ref->try_catch == 0)
7833 isn->isn_arg.try.try_ref->try_catch = this_instr;
7834 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007835 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007836 {
7837 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007838 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007839 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02007840 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007841 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007842 if (generate_instr(cctx, ISN_FINALLY) == NULL)
7843 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007844
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007845 // TODO: set index in ts_finally_label jumps
7846
7847 return arg;
7848}
7849
7850 static char_u *
7851compile_endtry(char_u *arg, cctx_T *cctx)
7852{
7853 scope_T *scope = cctx->ctx_scope;
7854 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007855 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007856
7857 // end block scope from :catch or :finally
7858 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7859 compile_endblock(cctx);
7860 scope = cctx->ctx_scope;
7861
7862 // Error if not in a :try scope
7863 if (scope == NULL || scope->se_type != TRY_SCOPE)
7864 {
7865 if (scope == NULL)
7866 emsg(_(e_no_endtry));
7867 else if (scope->se_type == WHILE_SCOPE)
7868 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007869 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007870 emsg(_(e_endfor));
7871 else
7872 emsg(_(e_endif));
7873 return NULL;
7874 }
7875
Bram Moolenaarc150c092021-02-13 15:02:46 +01007876 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007877 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007878 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007879 if (try_isn->isn_arg.try.try_ref->try_catch == 0
7880 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007881 {
7882 emsg(_(e_missing_catch_or_finally));
7883 return NULL;
7884 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007885
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007886#ifdef FEAT_PROFILE
7887 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7888 .isn_type == ISN_PROF_START)
7889 // move the profile start after "endtry" so that it's not counted when
7890 // the exception is rethrown.
7891 --instr->ga_len;
7892#endif
7893
Bram Moolenaar69f70502021-01-01 16:10:46 +01007894 // Fill in the "end" label in jumps at the end of the blocks, if not
7895 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007896 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
7897 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007898
Bram Moolenaar69f70502021-01-01 16:10:46 +01007899 if (scope->se_u.se_try.ts_catch_label != 0)
7900 {
7901 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01007902 isn_T *isn = ((isn_T *)instr->ga_data)
7903 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007904 isn->isn_arg.jump.jump_where = instr->ga_len;
7905 }
Bram Moolenaare8593122020-07-18 15:17:02 +02007906 }
7907
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007908 compile_endblock(cctx);
7909
Bram Moolenaar4afa7742021-02-14 16:34:59 +01007910 if (cctx->ctx_skip != SKIP_YES)
7911 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007912 // End :catch or :finally scope: set instruction index in ISN_TRY
7913 // instruction
7914 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01007915 if (cctx->ctx_skip != SKIP_YES
7916 && generate_instr(cctx, ISN_ENDTRY) == NULL)
7917 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007918#ifdef FEAT_PROFILE
7919 if (cctx->ctx_profiling)
7920 generate_instr(cctx, ISN_PROF_START);
7921#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01007922 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007923 return arg;
7924}
7925
7926/*
7927 * compile "throw {expr}"
7928 */
7929 static char_u *
7930compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7931{
7932 char_u *p = skipwhite(arg);
7933
Bram Moolenaara5565e42020-05-09 15:44:01 +02007934 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007935 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01007936 if (cctx->ctx_skip == SKIP_YES)
7937 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007938 if (may_generate_2STRING(-1, cctx) == FAIL)
7939 return NULL;
7940 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7941 return NULL;
7942
7943 return p;
7944}
7945
7946/*
7947 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007948 * compile "echomsg expr"
7949 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007950 * compile "execute expr"
7951 */
7952 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007953compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007954{
7955 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007956 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007957 int count = 0;
7958
7959 for (;;)
7960 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007961 if (ends_excmd2(prev, p))
7962 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007963 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007964 return NULL;
7965 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007966 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007967 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007968 }
7969
Bram Moolenaare4984292020-12-13 14:19:25 +01007970 if (count > 0)
7971 {
7972 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7973 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7974 else if (cmdidx == CMD_execute)
7975 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7976 else if (cmdidx == CMD_echomsg)
7977 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7978 else
7979 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7980 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007981 return p;
7982}
7983
7984/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007985 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01007986 * instruction to compute it and return OK.
7987 * Otherwise return FAIL, the caller must deal with any range.
7988 */
7989 static int
7990compile_variable_range(exarg_T *eap, cctx_T *cctx)
7991{
7992 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7993 char_u *p = skipdigits(eap->cmd);
7994
7995 if (p == range_end)
7996 return FAIL;
7997 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7998}
7999
8000/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008001 * :put r
8002 * :put ={expr}
8003 */
8004 static char_u *
8005compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8006{
8007 char_u *line = arg;
8008 linenr_T lnum;
8009 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008010 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008011
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008012 eap->regname = *line;
8013
8014 if (eap->regname == '=')
8015 {
8016 char_u *p = line + 1;
8017
8018 if (compile_expr0(&p, cctx) == FAIL)
8019 return NULL;
8020 line = p;
8021 }
8022 else if (eap->regname != NUL)
8023 ++line;
8024
Bram Moolenaar08597872020-12-10 19:43:40 +01008025 if (compile_variable_range(eap, cctx) == OK)
8026 {
8027 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8028 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008029 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008030 {
8031 // Either no range or a number.
8032 // "errormsg" will not be set because the range is ADDR_LINES.
8033 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008034 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008035 return NULL;
8036 if (eap->addr_count == 0)
8037 lnum = -1;
8038 else
8039 lnum = eap->line2;
8040 if (above)
8041 --lnum;
8042 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008043
8044 generate_PUT(cctx, eap->regname, lnum);
8045 return line;
8046}
8047
8048/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008049 * A command that is not compiled, execute with legacy code.
8050 */
8051 static char_u *
8052compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8053{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008054 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008055 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008056 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008057
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008058 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008059 goto theend;
8060
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008061 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008062 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008063 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008064 int usefilter = FALSE;
8065
8066 has_expr = argt & (EX_XFILE | EX_EXPAND);
8067
8068 // If the command can be followed by a bar, find the bar and truncate
8069 // it, so that the following command can be compiled.
8070 // The '|' is overwritten with a NUL, it is put back below.
8071 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8072 && *eap->arg == '!')
8073 // :w !filter or :r !filter or :r! filter
8074 usefilter = TRUE;
8075 if ((argt & EX_TRLBAR) && !usefilter)
8076 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008077 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008078 separate_nextcmd(eap);
8079 if (eap->nextcmd != NULL)
8080 nextcmd = eap->nextcmd;
8081 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008082 else if (eap->cmdidx == CMD_wincmd)
8083 {
8084 p = eap->arg;
8085 if (*p != NUL)
8086 ++p;
8087 if (*p == 'g' || *p == Ctrl_G)
8088 ++p;
8089 p = skipwhite(p);
8090 if (*p == '|')
8091 {
8092 *p = NUL;
8093 nextcmd = p + 1;
8094 }
8095 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008096 }
8097
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008098 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8099 {
8100 // expand filename in "syntax include [@group] filename"
8101 has_expr = TRUE;
8102 eap->arg = skipwhite(eap->arg + 7);
8103 if (*eap->arg == '@')
8104 eap->arg = skiptowhite(eap->arg);
8105 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008106
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008107 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8108 && STRLEN(eap->arg) > 4)
8109 {
8110 int delim = *eap->arg;
8111
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008112 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008113 if (*p == delim)
8114 {
8115 eap->arg = p + 1;
8116 has_expr = TRUE;
8117 }
8118 }
8119
Bram Moolenaarecac5912021-01-05 19:23:28 +01008120 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8121 {
8122 // TODO: should only expand when appropriate for the command
8123 eap->arg = skiptowhite(eap->arg);
8124 has_expr = TRUE;
8125 }
8126
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008127 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008128 {
8129 int count = 0;
8130 char_u *start = skipwhite(line);
8131
8132 // :cmd xxx`=expr1`yyy`=expr2`zzz
8133 // PUSHS ":cmd xxx"
8134 // eval expr1
8135 // PUSHS "yyy"
8136 // eval expr2
8137 // PUSHS "zzz"
8138 // EXECCONCAT 5
8139 for (;;)
8140 {
8141 if (p > start)
8142 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008143 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008144 ++count;
8145 }
8146 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008147 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008148 return NULL;
8149 may_generate_2STRING(-1, cctx);
8150 ++count;
8151 p = skipwhite(p);
8152 if (*p != '`')
8153 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008154 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008155 return NULL;
8156 }
8157 start = p + 1;
8158
8159 p = (char_u *)strstr((char *)start, "`=");
8160 if (p == NULL)
8161 {
8162 if (*skipwhite(start) != NUL)
8163 {
8164 generate_PUSHS(cctx, vim_strsave(start));
8165 ++count;
8166 }
8167 break;
8168 }
8169 }
8170 generate_EXECCONCAT(cctx, count);
8171 }
8172 else
8173 generate_EXEC(cctx, line);
8174
8175theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008176 if (*nextcmd != NUL)
8177 {
8178 // the parser expects a pointer to the bar, put it back
8179 --nextcmd;
8180 *nextcmd = '|';
8181 }
8182
8183 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008184}
8185
8186/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008187 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008188 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008189 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008190 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008191add_def_function(ufunc_T *ufunc)
8192{
8193 dfunc_T *dfunc;
8194
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008195 if (def_functions.ga_len == 0)
8196 {
8197 // The first position is not used, so that a zero uf_dfunc_idx means it
8198 // wasn't set.
8199 if (ga_grow(&def_functions, 1) == FAIL)
8200 return FAIL;
8201 ++def_functions.ga_len;
8202 }
8203
Bram Moolenaar09689a02020-05-09 22:50:08 +02008204 // Add the function to "def_functions".
8205 if (ga_grow(&def_functions, 1) == FAIL)
8206 return FAIL;
8207 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8208 CLEAR_POINTER(dfunc);
8209 dfunc->df_idx = def_functions.ga_len;
8210 ufunc->uf_dfunc_idx = dfunc->df_idx;
8211 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008212 dfunc->df_name = vim_strsave(ufunc->uf_name);
8213 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008214 ++def_functions.ga_len;
8215 return OK;
8216}
8217
8218/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008219 * After ex_function() has collected all the function lines: parse and compile
8220 * the lines into instructions.
8221 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008222 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8223 * the return statement (used for lambda). When uf_ret_type is already set
8224 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008225 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008226 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008227 * This can be used recursively through compile_lambda(), which may reallocate
8228 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008229 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008230 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008231 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008232compile_def_function(
8233 ufunc_T *ufunc,
8234 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008235 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008236 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008237{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008238 char_u *line = NULL;
8239 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008240 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008241 cctx_T cctx;
8242 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008243 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008244 int ret = FAIL;
8245 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008246 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008247 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008248 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008249#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008250 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008251#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008252
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008253 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008254 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008255 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008256 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008257 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8258 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008259 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008260 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008261 else
8262 {
8263 if (add_def_function(ufunc) == FAIL)
8264 return FAIL;
8265 new_def_function = TRUE;
8266 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008267
Bram Moolenaar985116a2020-07-12 17:31:09 +02008268 ufunc->uf_def_status = UF_COMPILING;
8269
Bram Moolenaara80faa82020-04-12 19:37:17 +02008270 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008271
Bram Moolenaarf002a412021-01-24 13:34:18 +01008272#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008273 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008274#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008275 cctx.ctx_ufunc = ufunc;
8276 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008277 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008278 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8279 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8280 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8281 cctx.ctx_type_list = &ufunc->uf_type_list;
8282 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8283 instr = &cctx.ctx_instr;
8284
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008285 // Set the context to the function, it may be compiled when called from
8286 // another script. Set the script version to the most modern one.
8287 // The line number will be set in next_line_from_context().
8288 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008289 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8290
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008291 // Make sure error messages are OK.
8292 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8293 if (do_estack_push)
8294 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008295 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008296
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008297 if (ufunc->uf_def_args.ga_len > 0)
8298 {
8299 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008300 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008301 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008302 int i;
8303 char_u *arg;
8304 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008305 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008306
8307 // Produce instructions for the default values of optional arguments.
8308 // Store the instruction index in uf_def_arg_idx[] so that we know
8309 // where to start when the function is called, depending on the number
8310 // of arguments.
8311 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
8312 if (ufunc->uf_def_arg_idx == NULL)
8313 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008314 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008315 for (i = 0; i < count; ++i)
8316 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008317 garray_T *stack = &cctx.ctx_type_stack;
8318 type_T *val_type;
8319 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008320 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008321 int r;
8322
8323 // Make sure later arguments are not found.
8324 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008325
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008326 ufunc->uf_def_arg_idx[i] = instr->ga_len;
8327 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01008328 r = compile_expr0(&arg, &cctx);
8329
8330 ufunc->uf_args.ga_len = uf_args_len;
8331 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008332 goto erret;
8333
8334 // If no type specified use the type of the default value.
8335 // Otherwise check that the default value type matches the
8336 // specified type.
8337 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008338 where.wt_index = arg_idx + 1;
8339 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008340 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008341 {
8342 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008343 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008344 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02008345 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008346 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008347 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008348
8349 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008350 goto erret;
8351 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008352 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008353
8354 if (did_set_arg_type)
8355 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008356 }
8357
8358 /*
8359 * Loop over all the lines of the function and generate instructions.
8360 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008361 for (;;)
8362 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008363 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008364 int starts_with_colon = FALSE;
8365 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02008366 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008367
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008368 // Bail out on the first error to avoid a flood of errors and report
8369 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01008370 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008371 goto erret;
8372
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008373 if (line != NULL && *line == '|')
8374 // the line continues after a '|'
8375 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02008376 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02008377 && !(*line == '#' && (line == cctx.ctx_line_start
8378 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008379 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02008380 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008381 goto erret;
8382 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01008383 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
8384 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008385 else
8386 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02008387 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02008388 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008389 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008390 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01008391#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008392 if (cctx.ctx_skip != SKIP_YES)
8393 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008394#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008395 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01008396 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008397 }
8398
Bram Moolenaara80faa82020-04-12 19:37:17 +02008399 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008400 ea.cmdlinep = &line;
8401 ea.cmd = skipwhite(line);
8402
Bram Moolenaarb2049902021-01-24 12:53:53 +01008403 if (*ea.cmd == '#')
8404 {
8405 // "#" starts a comment
8406 line = (char_u *)"";
8407 continue;
8408 }
8409
Bram Moolenaarf002a412021-01-24 13:34:18 +01008410#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008411 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
8412 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008413 {
8414 may_generate_prof_end(&cctx, prof_lnum);
8415
8416 prof_lnum = cctx.ctx_lnum;
8417 generate_instr(&cctx, ISN_PROF_START);
8418 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01008419#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008420
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008421 // Some things can be recognized by the first character.
8422 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008423 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008424 case '}':
8425 {
8426 // "}" ends a block scope
8427 scopetype_T stype = cctx.ctx_scope == NULL
8428 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008429
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008430 if (stype == BLOCK_SCOPE)
8431 {
8432 compile_endblock(&cctx);
8433 line = ea.cmd;
8434 }
8435 else
8436 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008437 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008438 goto erret;
8439 }
8440 if (line != NULL)
8441 line = skipwhite(ea.cmd + 1);
8442 continue;
8443 }
8444
8445 case '{':
8446 // "{" starts a block scope
8447 // "{'a': 1}->func() is something else
8448 if (ends_excmd(*skipwhite(ea.cmd + 1)))
8449 {
8450 line = compile_block(ea.cmd, &cctx);
8451 continue;
8452 }
8453 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008454 }
8455
8456 /*
8457 * COMMAND MODIFIERS
8458 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02008459 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02008460 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
8461 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008462 {
8463 if (errormsg != NULL)
8464 goto erret;
8465 // empty line or comment
8466 line = (char_u *)"";
8467 continue;
8468 }
Bram Moolenaare1004402020-10-24 20:49:43 +02008469 generate_cmdmods(&cctx, &local_cmdmod);
8470 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008471
Bram Moolenaare88c8e82020-11-01 17:03:37 +01008472 // Check if there was a colon after the last command modifier or before
8473 // the current position.
8474 for (p = ea.cmd; p >= line; --p)
8475 {
8476 if (*p == ':')
8477 starts_with_colon = TRUE;
8478 if (p < ea.cmd && !VIM_ISWHITE(*p))
8479 break;
8480 }
8481
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008482 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008483 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008484 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008485 {
8486 if (*ea.cmd == '(')
8487 // not for "call()"
8488 ea.cmd = p;
8489 else
8490 ea.cmd = skipwhite(ea.cmd);
8491 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008492
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008493 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008494 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008495 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008496
Bram Moolenaar17126b12021-01-07 22:03:02 +01008497 // Check for assignment after command modifiers.
8498 assign = may_compile_assignment(&ea, &line, &cctx);
8499 if (assign == OK)
8500 goto nextline;
8501 if (assign == FAIL)
8502 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008503 }
8504
8505 /*
8506 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008507 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008508 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008509 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008510 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008511 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008512 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008513 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008514 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008515 if (!starts_with_colon)
8516 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008517 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008518 goto erret;
8519 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01008520 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008521 if (ends_excmd2(line, ea.cmd))
8522 {
8523 // A range without a command: jump to the line.
8524 // TODO: compile to a more efficient command, possibly
8525 // calling parse_cmd_address().
8526 ea.cmdidx = CMD_SIZE;
8527 line = compile_exec(line, &ea, &cctx);
8528 goto nextline;
8529 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008530 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008531 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01008532 p = find_ex_command(&ea, NULL, starts_with_colon
8533 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008534
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008535 if (p == NULL)
8536 {
8537 if (cctx.ctx_skip != SKIP_YES)
8538 emsg(_(e_ambiguous_use_of_user_defined_command));
8539 goto erret;
8540 }
8541
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008542 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8543 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008544 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008545 {
8546 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008547 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008548 }
8549
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008550 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008551 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008552 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008553 // CMD_var cannot happen, compile_assignment() above would be
8554 // used. Most likely an assignment to a non-existing variable.
8555 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008556 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008557 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008558 }
8559
Bram Moolenaar3988f642020-08-27 22:43:03 +02008560 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008561 && ea.cmdidx != CMD_elseif
8562 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008563 && ea.cmdidx != CMD_endif
8564 && ea.cmdidx != CMD_endfor
8565 && ea.cmdidx != CMD_endwhile
8566 && ea.cmdidx != CMD_catch
8567 && ea.cmdidx != CMD_finally
8568 && ea.cmdidx != CMD_endtry)
8569 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008570 emsg(_(e_unreachable_code_after_return));
8571 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008572 }
8573
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008574 p = skipwhite(p);
8575 if (ea.cmdidx != CMD_SIZE
8576 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8577 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008578 if (ea.cmdidx >= 0)
8579 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008580 if ((ea.argt & EX_BANG) && *p == '!')
8581 {
8582 ea.forceit = TRUE;
8583 p = skipwhite(p + 1);
8584 }
8585 }
8586
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008587 switch (ea.cmdidx)
8588 {
8589 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008590 ea.arg = p;
8591 line = compile_nested_function(&ea, &cctx);
8592 break;
8593
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008594 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008595 // TODO: should we allow this, e.g. to declare a global
8596 // function?
8597 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008598 goto erret;
8599
8600 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008601 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008602 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008603 break;
8604
8605 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008606 emsg(_(e_cannot_use_let_in_vim9_script));
8607 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008608 case CMD_var:
8609 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008610 case CMD_const:
8611 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008612 if (line == p)
8613 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008614 break;
8615
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008616 case CMD_unlet:
8617 case CMD_unlockvar:
8618 case CMD_lockvar:
8619 line = compile_unletlock(p, &ea, &cctx);
8620 break;
8621
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008622 case CMD_import:
8623 line = compile_import(p, &cctx);
8624 break;
8625
8626 case CMD_if:
8627 line = compile_if(p, &cctx);
8628 break;
8629 case CMD_elseif:
8630 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008631 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008632 break;
8633 case CMD_else:
8634 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008635 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008636 break;
8637 case CMD_endif:
8638 line = compile_endif(p, &cctx);
8639 break;
8640
8641 case CMD_while:
8642 line = compile_while(p, &cctx);
8643 break;
8644 case CMD_endwhile:
8645 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008646 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008647 break;
8648
8649 case CMD_for:
8650 line = compile_for(p, &cctx);
8651 break;
8652 case CMD_endfor:
8653 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008654 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008655 break;
8656 case CMD_continue:
8657 line = compile_continue(p, &cctx);
8658 break;
8659 case CMD_break:
8660 line = compile_break(p, &cctx);
8661 break;
8662
8663 case CMD_try:
8664 line = compile_try(p, &cctx);
8665 break;
8666 case CMD_catch:
8667 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008668 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008669 break;
8670 case CMD_finally:
8671 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008672 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008673 break;
8674 case CMD_endtry:
8675 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008676 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008677 break;
8678 case CMD_throw:
8679 line = compile_throw(p, &cctx);
8680 break;
8681
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008682 case CMD_eval:
8683 if (compile_expr0(&p, &cctx) == FAIL)
8684 goto erret;
8685
Bram Moolenaar3988f642020-08-27 22:43:03 +02008686 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008687 generate_instr_drop(&cctx, ISN_DROP, 1);
8688
8689 line = skipwhite(p);
8690 break;
8691
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008692 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008693 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008694 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008695 case CMD_echomsg:
8696 case CMD_echoerr:
8697 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008698 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008699
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008700 case CMD_put:
8701 ea.cmd = cmd;
8702 line = compile_put(p, &ea, &cctx);
8703 break;
8704
Bram Moolenaar3988f642020-08-27 22:43:03 +02008705 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008706
Bram Moolenaarae616492020-07-28 20:07:27 +02008707 case CMD_append:
8708 case CMD_change:
8709 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01008710 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008711 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008712 case CMD_xit:
8713 not_in_vim9(&ea);
8714 goto erret;
8715
Bram Moolenaar002262f2020-07-08 17:47:57 +02008716 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008717 if (cctx.ctx_skip != SKIP_YES)
8718 {
8719 semsg(_(e_invalid_command_str), ea.cmd);
8720 goto erret;
8721 }
8722 // We don't check for a next command here.
8723 line = (char_u *)"";
8724 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008725
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008726 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008727 if (cctx.ctx_skip == SKIP_YES)
8728 {
8729 // We don't check for a next command here.
8730 line = (char_u *)"";
8731 }
8732 else
8733 {
8734 // Not recognized, execute with do_cmdline_cmd().
8735 ea.arg = p;
8736 line = compile_exec(line, &ea, &cctx);
8737 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008738 break;
8739 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008740nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008741 if (line == NULL)
8742 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008743 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008744
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008745 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008746 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008747
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008748 if (cctx.ctx_type_stack.ga_len < 0)
8749 {
8750 iemsg("Type stack underflow");
8751 goto erret;
8752 }
8753 }
8754
8755 if (cctx.ctx_scope != NULL)
8756 {
8757 if (cctx.ctx_scope->se_type == IF_SCOPE)
8758 emsg(_(e_endif));
8759 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8760 emsg(_(e_endwhile));
8761 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8762 emsg(_(e_endfor));
8763 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008764 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008765 goto erret;
8766 }
8767
Bram Moolenaarefd88552020-06-18 20:50:10 +02008768 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008769 {
8770 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8771 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008772 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008773 goto erret;
8774 }
8775
8776 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008777 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008778 }
8779
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008780 {
8781 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8782 + ufunc->uf_dfunc_idx;
8783 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008784 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008785#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008786 if (cctx.ctx_profiling)
8787 {
8788 dfunc->df_instr_prof = instr->ga_data;
8789 dfunc->df_instr_prof_count = instr->ga_len;
8790 }
8791 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01008792#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008793 {
8794 dfunc->df_instr = instr->ga_data;
8795 dfunc->df_instr_count = instr->ga_len;
8796 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008797 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008798 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008799 if (cctx.ctx_outer_used)
8800 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008801 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008802 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008803
8804 ret = OK;
8805
8806erret:
8807 if (ret == FAIL)
8808 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008809 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008810 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8811 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008812
8813 for (idx = 0; idx < instr->ga_len; ++idx)
8814 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008815 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008816 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008817
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008818 // If using the last entry in the table and it was added above, we
8819 // might as well remove it.
8820 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008821 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008822 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008823 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008824 ufunc->uf_dfunc_idx = 0;
8825 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008826 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008827
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008828 while (cctx.ctx_scope != NULL)
8829 drop_scope(&cctx);
8830
Bram Moolenaar20431c92020-03-20 18:39:46 +01008831 // Don't execute this function body.
8832 ga_clear_strings(&ufunc->uf_lines);
8833
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008834 if (errormsg != NULL)
8835 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008836 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008837 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008838 }
8839
8840 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008841 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008842 if (do_estack_push)
8843 estack_pop();
8844
Bram Moolenaar20431c92020-03-20 18:39:46 +01008845 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008846 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008847 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008848 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008849}
8850
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008851 void
8852set_function_type(ufunc_T *ufunc)
8853{
8854 int varargs = ufunc->uf_va_name != NULL;
8855 int argcount = ufunc->uf_args.ga_len;
8856
8857 // Create a type for the function, with the return type and any
8858 // argument types.
8859 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8860 // The type is included in "tt_args".
8861 if (argcount > 0 || varargs)
8862 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01008863 if (ufunc->uf_type_list.ga_itemsize == 0)
8864 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008865 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8866 argcount, &ufunc->uf_type_list);
8867 // Add argument types to the function type.
8868 if (func_type_add_arg_types(ufunc->uf_func_type,
8869 argcount + varargs,
8870 &ufunc->uf_type_list) == FAIL)
8871 return;
8872 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8873 ufunc->uf_func_type->tt_min_argcount =
8874 argcount - ufunc->uf_def_args.ga_len;
8875 if (ufunc->uf_arg_types == NULL)
8876 {
8877 int i;
8878
8879 // lambda does not have argument types.
8880 for (i = 0; i < argcount; ++i)
8881 ufunc->uf_func_type->tt_args[i] = &t_any;
8882 }
8883 else
8884 mch_memmove(ufunc->uf_func_type->tt_args,
8885 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8886 if (varargs)
8887 {
8888 ufunc->uf_func_type->tt_args[argcount] =
8889 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8890 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8891 }
8892 }
8893 else
8894 // No arguments, can use a predefined type.
8895 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8896 argcount, &ufunc->uf_type_list);
8897}
8898
8899
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008900/*
8901 * Delete an instruction, free what it contains.
8902 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008903 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008904delete_instr(isn_T *isn)
8905{
8906 switch (isn->isn_type)
8907 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008908 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008909 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008910 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008911 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008912 case ISN_LOADENV:
8913 case ISN_LOADG:
8914 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008915 case ISN_LOADT:
8916 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008917 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008918 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008919 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008920 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008921 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008922 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008923 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008924 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008925 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008926 case ISN_STOREW:
8927 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008928 vim_free(isn->isn_arg.string);
8929 break;
8930
8931 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008932 case ISN_STORES:
8933 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008934 break;
8935
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008936 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008937 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008938 vim_free(isn->isn_arg.unlet.ul_name);
8939 break;
8940
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008941 case ISN_STOREOPT:
8942 vim_free(isn->isn_arg.storeopt.so_name);
8943 break;
8944
8945 case ISN_PUSHBLOB: // push blob isn_arg.blob
8946 blob_unref(isn->isn_arg.blob);
8947 break;
8948
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008949 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008950#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008951 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008952#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008953 break;
8954
8955 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008956#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008957 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008958#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008959 break;
8960
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008961 case ISN_UCALL:
8962 vim_free(isn->isn_arg.ufunc.cuf_name);
8963 break;
8964
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008965 case ISN_FUNCREF:
8966 {
8967 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8968 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01008969 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008970
Bram Moolenaar077a4232020-12-22 18:33:27 +01008971 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
8972 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02008973 }
8974 break;
8975
8976 case ISN_DCALL:
8977 {
8978 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8979 + isn->isn_arg.dfunc.cdf_idx;
8980
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008981 if (dfunc->df_ufunc != NULL
8982 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008983 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008984 }
8985 break;
8986
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008987 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008988 {
8989 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8990 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8991
8992 if (ufunc != NULL)
8993 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008994 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02008995 func_ptr_unref(ufunc);
8996 }
8997
8998 vim_free(lambda);
8999 vim_free(isn->isn_arg.newfunc.nf_global);
9000 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009001 break;
9002
Bram Moolenaar5e654232020-09-16 15:22:00 +02009003 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01009004 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02009005 free_type(isn->isn_arg.type.ct_type);
9006 break;
9007
Bram Moolenaar02194d22020-10-24 23:08:38 +02009008 case ISN_CMDMOD:
9009 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9010 ->cmod_filter_regmatch.regprog);
9011 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9012 break;
9013
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009014 case ISN_LOADSCRIPT:
9015 case ISN_STORESCRIPT:
9016 vim_free(isn->isn_arg.script.scriptref);
9017 break;
9018
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009019 case ISN_TRY:
9020 vim_free(isn->isn_arg.try.try_ref);
9021 break;
9022
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009023 case ISN_2BOOL:
9024 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02009025 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009026 case ISN_ADDBLOB:
9027 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009028 case ISN_ANYINDEX:
9029 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009030 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02009031 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009032 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009033 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009034 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02009035 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009036 case ISN_COMPAREANY:
9037 case ISN_COMPAREBLOB:
9038 case ISN_COMPAREBOOL:
9039 case ISN_COMPAREDICT:
9040 case ISN_COMPAREFLOAT:
9041 case ISN_COMPAREFUNC:
9042 case ISN_COMPARELIST:
9043 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009044 case ISN_COMPARESPECIAL:
9045 case ISN_COMPARESTRING:
9046 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02009047 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009048 case ISN_DROP:
9049 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009050 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009051 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009052 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009053 case ISN_EXECCONCAT:
9054 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009055 case ISN_FINALLY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009056 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009057 case ISN_GETITEM:
9058 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02009059 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02009060 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02009061 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009062 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009063 case ISN_LOADBDICT:
9064 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009065 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009066 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009067 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009068 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009069 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009070 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009071 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009072 case ISN_NEGATENR:
9073 case ISN_NEWDICT:
9074 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009075 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009076 case ISN_OPFLOAT:
9077 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009078 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02009079 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01009080 case ISN_PROF_END:
9081 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009082 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009083 case ISN_PUSHF:
9084 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009085 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009086 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009087 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01009088 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009089 case ISN_SHUFFLE:
9090 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009091 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01009092 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009093 case ISN_STORENR:
9094 case ISN_STOREOUTER:
9095 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009096 case ISN_STOREV:
9097 case ISN_STRINDEX:
9098 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009099 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +01009100 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +01009101 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01009102 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +01009103 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009104 // nothing allocated
9105 break;
9106 }
9107}
9108
9109/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009110 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01009111 */
9112 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009113delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009114{
9115 int idx;
9116
9117 ga_clear(&dfunc->df_def_args_isn);
9118
9119 if (dfunc->df_instr != NULL)
9120 {
9121 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
9122 delete_instr(dfunc->df_instr + idx);
9123 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009124 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009125 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01009126#ifdef FEAT_PROFILE
9127 if (dfunc->df_instr_prof != NULL)
9128 {
9129 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
9130 delete_instr(dfunc->df_instr_prof + idx);
9131 VIM_CLEAR(dfunc->df_instr_prof);
9132 dfunc->df_instr_prof = NULL;
9133 }
9134#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01009135
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009136 if (mark_deleted)
9137 dfunc->df_deleted = TRUE;
9138 if (dfunc->df_ufunc != NULL)
9139 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009140}
9141
9142/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009143 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009144 * function, unless another user function still uses it.
9145 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009146 */
9147 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009148unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009149{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009150 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009151 {
9152 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9153 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009154
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009155 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009156 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009157 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009158 ufunc->uf_dfunc_idx = 0;
9159 if (dfunc->df_ufunc == ufunc)
9160 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009161 }
9162}
9163
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009164/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009165 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009166 */
9167 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009168link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009169{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009170 if (ufunc->uf_dfunc_idx > 0)
9171 {
9172 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9173 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009174
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009175 ++dfunc->df_refcount;
9176 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009177}
9178
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009179#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009180/*
9181 * Free all functions defined with ":def".
9182 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009183 void
9184free_def_functions(void)
9185{
Bram Moolenaar20431c92020-03-20 18:39:46 +01009186 int idx;
9187
9188 for (idx = 0; idx < def_functions.ga_len; ++idx)
9189 {
9190 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
9191
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009192 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009193 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009194 }
9195
9196 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009197}
9198#endif
9199
9200
9201#endif // FEAT_EVAL