blob: ce033c1b3d1a42db6fdadd1276f083b709f203e5 [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
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001632/*
1633 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1634 */
1635 static int
1636generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1637{
1638 isn_T *isn;
1639
1640 RETURN_OK_IF_SKIP(cctx);
1641 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1642 return FAIL;
1643 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1644 // jump_where is set later
1645 return OK;
1646}
1647
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001648 static int
1649generate_FOR(cctx_T *cctx, int loop_idx)
1650{
1651 isn_T *isn;
1652 garray_T *stack = &cctx->ctx_type_stack;
1653
Bram Moolenaar080457c2020-03-03 21:53:32 +01001654 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1656 return FAIL;
1657 isn->isn_arg.forloop.for_idx = loop_idx;
1658
1659 if (ga_grow(stack, 1) == FAIL)
1660 return FAIL;
1661 // type doesn't matter, will be stored next
1662 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1663 ++stack->ga_len;
1664
1665 return OK;
1666}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001667/*
1668 * Generate an ISN_TRYCONT instruction.
1669 */
1670 static int
1671generate_TRYCONT(cctx_T *cctx, int levels, int where)
1672{
1673 isn_T *isn;
1674
1675 RETURN_OK_IF_SKIP(cctx);
1676 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1677 return FAIL;
1678 isn->isn_arg.trycont.tct_levels = levels;
1679 isn->isn_arg.trycont.tct_where = where;
1680
1681 return OK;
1682}
1683
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684
1685/*
1686 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001687 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001688 * Return FAIL if the number of arguments is wrong.
1689 */
1690 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001691generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001692{
1693 isn_T *isn;
1694 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001695 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001696 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001697 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001698
Bram Moolenaar080457c2020-03-03 21:53:32 +01001699 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001700 argoff = check_internal_func(func_idx, argcount);
1701 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702 return FAIL;
1703
Bram Moolenaar389df252020-07-09 21:20:47 +02001704 if (method_call && argoff > 1)
1705 {
1706 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1707 return FAIL;
1708 isn->isn_arg.shuffle.shfl_item = argcount;
1709 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1710 }
1711
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001712 if (argcount > 0)
1713 {
1714 // Check the types of the arguments.
1715 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001716 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1717 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001718 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001719 if (internal_func_is_map(func_idx))
1720 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001721 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001722
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1724 return FAIL;
1725 isn->isn_arg.bfunc.cbf_idx = func_idx;
1726 isn->isn_arg.bfunc.cbf_argcount = argcount;
1727
Bram Moolenaar94738d82020-10-21 14:25:07 +02001728 // Drop the argument types and push the return type.
1729 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730 if (ga_grow(stack, 1) == FAIL)
1731 return FAIL;
1732 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001733 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001734 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001735
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001736 if (maptype != NULL && maptype->tt_member != NULL
1737 && maptype->tt_member != &t_any)
1738 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001739 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001740
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741 return OK;
1742}
1743
1744/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001745 * Generate an ISN_LISTAPPEND instruction. Works like add().
1746 * Argument count is already checked.
1747 */
1748 static int
1749generate_LISTAPPEND(cctx_T *cctx)
1750{
1751 garray_T *stack = &cctx->ctx_type_stack;
1752 type_T *list_type;
1753 type_T *item_type;
1754 type_T *expected;
1755
1756 // Caller already checked that list_type is a list.
1757 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1758 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1759 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001760 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001761 return FAIL;
1762
1763 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1764 return FAIL;
1765
1766 --stack->ga_len; // drop the argument
1767 return OK;
1768}
1769
1770/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001771 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1772 * Argument count is already checked.
1773 */
1774 static int
1775generate_BLOBAPPEND(cctx_T *cctx)
1776{
1777 garray_T *stack = &cctx->ctx_type_stack;
1778 type_T *item_type;
1779
1780 // Caller already checked that blob_type is a blob.
1781 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001782 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001783 return FAIL;
1784
1785 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1786 return FAIL;
1787
1788 --stack->ga_len; // drop the argument
1789 return OK;
1790}
1791
1792/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001793 * Return TRUE if "ufunc" should be compiled, taking into account whether
1794 * "profile" indicates profiling is to be done.
1795 */
1796 int
Bram Moolenaarf002a412021-01-24 13:34:18 +01001797func_needs_compiling(ufunc_T *ufunc, int profile UNUSED)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001798{
1799 switch (ufunc->uf_def_status)
1800 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001801 case UF_NOT_COMPILED: break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001802 case UF_TO_BE_COMPILED: return TRUE;
1803 case UF_COMPILED:
1804 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001805#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001806 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1807 + ufunc->uf_dfunc_idx;
1808
1809 return profile ? dfunc->df_instr_prof == NULL
1810 : dfunc->df_instr == NULL;
Bram Moolenaarf002a412021-01-24 13:34:18 +01001811#else
1812 break;
1813#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01001814 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001815 case UF_COMPILING: break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001816 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001817 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001818}
1819
1820/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001821 * Generate an ISN_DCALL or ISN_UCALL instruction.
1822 * Return FAIL if the number of arguments is wrong.
1823 */
1824 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001825generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826{
1827 isn_T *isn;
1828 garray_T *stack = &cctx->ctx_type_stack;
1829 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001830 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831
Bram Moolenaar080457c2020-03-03 21:53:32 +01001832 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833 if (argcount > regular_args && !has_varargs(ufunc))
1834 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001835 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001836 return FAIL;
1837 }
1838 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1839 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001840 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001841 return FAIL;
1842 }
1843
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001844 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001845 {
1846 int i;
1847
1848 for (i = 0; i < argcount; ++i)
1849 {
1850 type_T *expected;
1851 type_T *actual;
1852
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001853 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1854 if (actual == &t_special
1855 && i >= regular_args - ufunc->uf_def_args.ga_len)
1856 {
1857 // assume v:none used for default argument value
1858 continue;
1859 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001860 if (i < regular_args)
1861 {
1862 if (ufunc->uf_arg_types == NULL)
1863 continue;
1864 expected = ufunc->uf_arg_types[i];
1865 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001866 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1867 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001868 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001869 else
1870 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01001871 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001872 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001873 {
1874 arg_type_mismatch(expected, actual, i + 1);
1875 return FAIL;
1876 }
1877 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001878 if (func_needs_compiling(ufunc, PROFILING(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001879 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001880 PROFILING(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001881 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001882 }
1883
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001884 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001885 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001886 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001887 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001888 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001889 {
1890 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1891 isn->isn_arg.dfunc.cdf_argcount = argcount;
1892 }
1893 else
1894 {
1895 // A user function may be deleted and redefined later, can't use the
1896 // ufunc pointer, need to look it up again at runtime.
1897 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1898 isn->isn_arg.ufunc.cuf_argcount = argcount;
1899 }
1900
1901 stack->ga_len -= argcount; // drop the arguments
1902 if (ga_grow(stack, 1) == FAIL)
1903 return FAIL;
1904 // add return value
1905 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1906 ++stack->ga_len;
1907
1908 return OK;
1909}
1910
1911/*
1912 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1913 */
1914 static int
1915generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1916{
1917 isn_T *isn;
1918 garray_T *stack = &cctx->ctx_type_stack;
1919
Bram Moolenaar080457c2020-03-03 21:53:32 +01001920 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001921 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1922 return FAIL;
1923 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1924 isn->isn_arg.ufunc.cuf_argcount = argcount;
1925
1926 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001927 if (ga_grow(stack, 1) == FAIL)
1928 return FAIL;
1929 // add return value
1930 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1931 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932
1933 return OK;
1934}
1935
1936/*
1937 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001938 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 */
1940 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001941generate_PCALL(
1942 cctx_T *cctx,
1943 int argcount,
1944 char_u *name,
1945 type_T *type,
1946 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001947{
1948 isn_T *isn;
1949 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001950 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001951
Bram Moolenaar080457c2020-03-03 21:53:32 +01001952 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001953
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001954 if (type->tt_type == VAR_ANY)
1955 ret_type = &t_any;
1956 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001957 {
1958 if (type->tt_argcount != -1)
1959 {
1960 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1961
1962 if (argcount < type->tt_min_argcount - varargs)
1963 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001964 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001965 return FAIL;
1966 }
1967 if (!varargs && argcount > type->tt_argcount)
1968 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001969 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001970 return FAIL;
1971 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001972 if (type->tt_args != NULL)
1973 {
1974 int i;
1975
1976 for (i = 0; i < argcount; ++i)
1977 {
1978 int offset = -argcount + i - 1;
1979 type_T *actual = ((type_T **)stack->ga_data)[
1980 stack->ga_len + offset];
1981 type_T *expected;
1982
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001983 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001984 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001985 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001986 else if (i >= type->tt_min_argcount
1987 && actual == &t_special)
1988 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001989 else
1990 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001991 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001992 cctx, TRUE, FALSE) == FAIL)
1993 {
1994 arg_type_mismatch(expected, actual, i + 1);
1995 return FAIL;
1996 }
1997 }
1998 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001999 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002000 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002001 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002002 else
2003 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002004 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002005 return FAIL;
2006 }
2007
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2009 return FAIL;
2010 isn->isn_arg.pfunc.cpf_top = at_top;
2011 isn->isn_arg.pfunc.cpf_argcount = argcount;
2012
2013 stack->ga_len -= argcount; // drop the arguments
2014
2015 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002016 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002018 // If partial is above the arguments it must be cleared and replaced with
2019 // the return value.
2020 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2021 return FAIL;
2022
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002023 return OK;
2024}
2025
2026/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002027 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002028 */
2029 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002030generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031{
2032 isn_T *isn;
2033 garray_T *stack = &cctx->ctx_type_stack;
2034 type_T *type;
2035
Bram Moolenaar080457c2020-03-03 21:53:32 +01002036 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002037 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002038 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002039 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002041 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002043 if (type->tt_type != VAR_DICT && type != &t_any)
2044 {
2045 emsg(_(e_dictreq));
2046 return FAIL;
2047 }
2048 // change dict type to dict member type
2049 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002050 {
2051 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2052 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2053 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002054
2055 return OK;
2056}
2057
2058/*
2059 * Generate an ISN_ECHO instruction.
2060 */
2061 static int
2062generate_ECHO(cctx_T *cctx, int with_white, int count)
2063{
2064 isn_T *isn;
2065
Bram Moolenaar080457c2020-03-03 21:53:32 +01002066 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002067 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2068 return FAIL;
2069 isn->isn_arg.echo.echo_with_white = with_white;
2070 isn->isn_arg.echo.echo_count = count;
2071
2072 return OK;
2073}
2074
Bram Moolenaarad39c092020-02-26 18:23:43 +01002075/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002076 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002077 */
2078 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002079generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002080{
2081 isn_T *isn;
2082
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002083 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002084 return FAIL;
2085 isn->isn_arg.number = count;
2086
2087 return OK;
2088}
2089
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002090/*
2091 * Generate an ISN_PUT instruction.
2092 */
2093 static int
2094generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2095{
2096 isn_T *isn;
2097
2098 RETURN_OK_IF_SKIP(cctx);
2099 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2100 return FAIL;
2101 isn->isn_arg.put.put_regname = regname;
2102 isn->isn_arg.put.put_lnum = lnum;
2103 return OK;
2104}
2105
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002106 static int
2107generate_EXEC(cctx_T *cctx, char_u *line)
2108{
2109 isn_T *isn;
2110
Bram Moolenaar080457c2020-03-03 21:53:32 +01002111 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2113 return FAIL;
2114 isn->isn_arg.string = vim_strsave(line);
2115 return OK;
2116}
2117
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002118 static int
2119generate_EXECCONCAT(cctx_T *cctx, int count)
2120{
2121 isn_T *isn;
2122
2123 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2124 return FAIL;
2125 isn->isn_arg.number = count;
2126 return OK;
2127}
2128
Bram Moolenaar08597872020-12-10 19:43:40 +01002129/*
2130 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2131 */
2132 static int
2133generate_RANGE(cctx_T *cctx, char_u *range)
2134{
2135 isn_T *isn;
2136 garray_T *stack = &cctx->ctx_type_stack;
2137
2138 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2139 return FAIL;
2140 isn->isn_arg.string = range;
2141
2142 if (ga_grow(stack, 1) == FAIL)
2143 return FAIL;
2144 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2145 ++stack->ga_len;
2146 return OK;
2147}
2148
Bram Moolenaar792f7862020-11-23 08:31:18 +01002149 static int
2150generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2151{
2152 isn_T *isn;
2153
2154 RETURN_OK_IF_SKIP(cctx);
2155 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2156 return FAIL;
2157 isn->isn_arg.unpack.unp_count = var_count;
2158 isn->isn_arg.unpack.unp_semicolon = semicolon;
2159 return OK;
2160}
2161
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002163 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002164 */
2165 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002166generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002167{
2168 isn_T *isn;
2169
Bram Moolenaarfa984412021-03-25 22:15:28 +01002170 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002171 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002172 cctx->ctx_has_cmdmod = TRUE;
2173
2174 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002175 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002176 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2177 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2178 return FAIL;
2179 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002180 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002181 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002182 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002183
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002184 return OK;
2185}
2186
2187 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002188generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002189{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002190 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2191 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002192 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002193 return OK;
2194}
2195
Bram Moolenaarfa984412021-03-25 22:15:28 +01002196 static int
2197misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002198{
2199 garray_T *instr = &cctx->ctx_instr;
2200
Bram Moolenaara91a7132021-03-25 21:12:15 +01002201 if (cctx->ctx_has_cmdmod
2202 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2203 == ISN_CMDMOD)
2204 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002205 emsg(_(e_misplaced_command_modifier));
2206 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002207 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002208 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002209}
2210
2211/*
2212 * Get the index of the current instruction.
2213 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2214 */
2215 static int
2216current_instr_idx(cctx_T *cctx)
2217{
2218 garray_T *instr = &cctx->ctx_instr;
2219 int idx = instr->ga_len;
2220
2221 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
2222 .isn_type == ISN_CMDMOD)
2223 --idx;
2224#ifdef FEAT_PROFILE
2225 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[idx - 1]
2226 .isn_type == ISN_PROF_START)
2227 --idx;
2228#endif
2229 return idx;
2230}
2231
Bram Moolenaarf002a412021-01-24 13:34:18 +01002232#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002233 static void
2234may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2235{
2236 if (cctx->ctx_profiling && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002237 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002238}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002239#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002240
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002241/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002242 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002243 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002244 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002245 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002246reserve_local(
2247 cctx_T *cctx,
2248 char_u *name,
2249 size_t len,
2250 int isConst,
2251 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002252{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253 lvar_T *lvar;
2254
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002255 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002256 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002257 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002258 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002259 }
2260
2261 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002262 return NULL;
2263 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002264 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002265
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002266 // Every local variable uses the next entry on the stack. We could re-use
2267 // the last ones when leaving a scope, but then variables used in a closure
2268 // might get overwritten. To keep things simple do not re-use stack
2269 // entries. This is less efficient, but memory is cheap these days.
2270 lvar->lv_idx = cctx->ctx_locals_count++;
2271
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002272 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002273 lvar->lv_const = isConst;
2274 lvar->lv_type = type;
2275
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002276 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002277}
2278
2279/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002280 * Remove local variables above "new_top".
2281 */
2282 static void
2283unwind_locals(cctx_T *cctx, int new_top)
2284{
2285 if (cctx->ctx_locals.ga_len > new_top)
2286 {
2287 int idx;
2288 lvar_T *lvar;
2289
2290 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2291 {
2292 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2293 vim_free(lvar->lv_name);
2294 }
2295 }
2296 cctx->ctx_locals.ga_len = new_top;
2297}
2298
2299/*
2300 * Free all local variables.
2301 */
2302 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002303free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002304{
2305 unwind_locals(cctx, 0);
2306 ga_clear(&cctx->ctx_locals);
2307}
2308
2309/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002310 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2311 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2312 * error if the variable was defined with :const.
2313 */
2314 static int
2315check_item_writable(svar_T *sv, int check_writable, char_u *name)
2316{
2317 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2318 || (check_writable == ASSIGN_FINAL
2319 && sv->sv_const == ASSIGN_CONST))
2320 {
2321 semsg(_(e_readonlyvar), name);
2322 return FAIL;
2323 }
2324 return OK;
2325}
2326
2327/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002328 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002329 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002330 * Returns the index in "sn_var_vals" if found.
2331 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002332 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333 */
2334 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002335get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002336{
2337 hashtab_T *ht;
2338 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002339 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002340 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002341 int idx;
2342
Bram Moolenaare3d46852020-08-29 13:39:17 +02002343 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002344 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002345 if (sid == current_sctx.sc_sid)
2346 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002347 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002348
2349 if (sav == NULL)
2350 return -2;
2351 idx = sav->sav_var_vals_idx;
2352 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002353 if (check_item_writable(sv, check_writable, name) == FAIL)
2354 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002355 return idx;
2356 }
2357
2358 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002359 ht = &SCRIPT_VARS(sid);
2360 di = find_var_in_ht(ht, 0, name, TRUE);
2361 if (di == NULL)
2362 return -2;
2363
2364 // Now find the svar_T index in sn_var_vals.
2365 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2366 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002367 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368 if (sv->sv_tv == &di->di_tv)
2369 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002370 if (check_item_writable(sv, check_writable, name) == FAIL)
2371 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372 return idx;
2373 }
2374 }
2375 return -1;
2376}
2377
2378/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002379 * Find "name" in imported items of the current script or in "cctx" if not
2380 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002381 */
2382 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002383find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002385 int idx;
2386
Bram Moolenaare3d46852020-08-29 13:39:17 +02002387 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002388 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002389 if (cctx != NULL)
2390 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2391 {
2392 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2393 + idx;
2394
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002395 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2396 : STRLEN(import->imp_name) == len
2397 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002398 return import;
2399 }
2400
Bram Moolenaarefa94442020-08-08 22:16:00 +02002401 return find_imported_in_script(name, len, current_sctx.sc_sid);
2402}
2403
2404 imported_T *
2405find_imported_in_script(char_u *name, size_t len, int sid)
2406{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002407 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002408 int idx;
2409
Bram Moolenaare3d46852020-08-29 13:39:17 +02002410 if (!SCRIPT_ID_VALID(sid))
2411 return NULL;
2412 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2414 {
2415 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2416
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002417 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2418 : STRLEN(import->imp_name) == len
2419 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002420 return import;
2421 }
2422 return NULL;
2423}
2424
2425/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002426 * Free all imported variables.
2427 */
2428 static void
2429free_imported(cctx_T *cctx)
2430{
2431 int idx;
2432
2433 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2434 {
2435 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2436
2437 vim_free(import->imp_name);
2438 }
2439 ga_clear(&cctx->ctx_imports);
2440}
2441
2442/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002443 * Return a pointer to the next line that isn't empty or only contains a
2444 * comment. Skips over white space.
2445 * Returns NULL if there is none.
2446 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002447 char_u *
2448peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002449{
2450 int lnum = cctx->ctx_lnum;
2451
2452 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2453 {
2454 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002455 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002456
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002457 // ignore NULLs inserted for continuation lines
2458 if (line != NULL)
2459 {
2460 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002461 if (vim9_bad_comment(p))
2462 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002463 if (*p != NUL && !vim9_comment_start(p))
2464 return p;
2465 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002466 }
2467 return NULL;
2468}
2469
2470/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002471 * Called when checking for a following operator at "arg". When the rest of
2472 * the line is empty or only a comment, peek the next line. If there is a next
2473 * line return a pointer to it and set "nextp".
2474 * Otherwise skip over white space.
2475 */
2476 static char_u *
2477may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2478{
2479 char_u *p = skipwhite(arg);
2480
2481 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002482 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002483 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002484 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002485 if (*nextp != NULL)
2486 return *nextp;
2487 }
2488 return p;
2489}
2490
2491/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002492 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002493 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002494 * Returns NULL when at the end.
2495 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002496 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002497next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002498{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002499 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002500
2501 do
2502 {
2503 ++cctx->ctx_lnum;
2504 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002505 {
2506 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002507 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002508 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002509 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002510 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002511 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002512 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002513 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002514 return line;
2515}
2516
2517/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002518 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002519 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002520 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002521 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2522 */
2523 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002524may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002525{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002526 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002527 if (vim9_bad_comment(*arg))
2528 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002529 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002530 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002531 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002532
2533 if (next == NULL)
2534 return FAIL;
2535 *arg = skipwhite(next);
2536 }
2537 return OK;
2538}
2539
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002540/*
2541 * Idem, and give an error when failed.
2542 */
2543 static int
2544may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2545{
2546 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2547 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002548 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002549 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002550 return FAIL;
2551 }
2552 return OK;
2553}
2554
2555
Bram Moolenaara5565e42020-05-09 15:44:01 +02002556// Structure passed between the compile_expr* functions to keep track of
2557// constants that have been parsed but for which no code was produced yet. If
2558// possible expressions on these constants are applied at compile time. If
2559// that is not possible, the code to push the constants needs to be generated
2560// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002561// Using 50 should be more than enough of 5 levels of ().
2562#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002563typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002564 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002565 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002566 int pp_is_const; // all generated code was constants, used for a
2567 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002568} ppconst_T;
2569
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002570static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002571static int compile_expr0(char_u **arg, cctx_T *cctx);
2572static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2573
Bram Moolenaara5565e42020-05-09 15:44:01 +02002574/*
2575 * Generate a PUSH instruction for "tv".
2576 * "tv" will be consumed or cleared.
2577 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2578 */
2579 static int
2580generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2581{
2582 if (tv != NULL)
2583 {
2584 switch (tv->v_type)
2585 {
2586 case VAR_UNKNOWN:
2587 break;
2588 case VAR_BOOL:
2589 generate_PUSHBOOL(cctx, tv->vval.v_number);
2590 break;
2591 case VAR_SPECIAL:
2592 generate_PUSHSPEC(cctx, tv->vval.v_number);
2593 break;
2594 case VAR_NUMBER:
2595 generate_PUSHNR(cctx, tv->vval.v_number);
2596 break;
2597#ifdef FEAT_FLOAT
2598 case VAR_FLOAT:
2599 generate_PUSHF(cctx, tv->vval.v_float);
2600 break;
2601#endif
2602 case VAR_BLOB:
2603 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2604 tv->vval.v_blob = NULL;
2605 break;
2606 case VAR_STRING:
2607 generate_PUSHS(cctx, tv->vval.v_string);
2608 tv->vval.v_string = NULL;
2609 break;
2610 default:
2611 iemsg("constant type not supported");
2612 clear_tv(tv);
2613 return FAIL;
2614 }
2615 tv->v_type = VAR_UNKNOWN;
2616 }
2617 return OK;
2618}
2619
2620/*
2621 * Generate code for any ppconst entries.
2622 */
2623 static int
2624generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2625{
2626 int i;
2627 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002628 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002629
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002630 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002631 for (i = 0; i < ppconst->pp_used; ++i)
2632 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2633 ret = FAIL;
2634 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002635 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002636 return ret;
2637}
2638
2639/*
2640 * Clear ppconst constants. Used when failing.
2641 */
2642 static void
2643clear_ppconst(ppconst_T *ppconst)
2644{
2645 int i;
2646
2647 for (i = 0; i < ppconst->pp_used; ++i)
2648 clear_tv(&ppconst->pp_tv[i]);
2649 ppconst->pp_used = 0;
2650}
2651
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002652/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002653 * Generate an instruction to load script-local variable "name", without the
2654 * leading "s:".
2655 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002656 */
2657 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002658compile_load_scriptvar(
2659 cctx_T *cctx,
2660 char_u *name, // variable NUL terminated
2661 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002662 char_u **end, // end of variable
2663 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002664{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002665 scriptitem_T *si;
2666 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002667 imported_T *import;
2668
Bram Moolenaare3d46852020-08-29 13:39:17 +02002669 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2670 return FAIL;
2671 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002672 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002673 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002674 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002675 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002676 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2677 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002678 }
2679 if (idx >= 0)
2680 {
2681 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2682
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002683 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 current_sctx.sc_sid, idx, sv->sv_type);
2685 return OK;
2686 }
2687
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002688 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002689 if (import != NULL)
2690 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002691 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002692 {
2693 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002694 char_u *exp_name;
2695 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002696 ufunc_T *ufunc;
2697 type_T *type;
2698
2699 // Used "import * as Name", need to lookup the member.
2700 if (*p != '.')
2701 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002702 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002703 return FAIL;
2704 }
2705 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002706 if (VIM_ISWHITE(*p))
2707 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002708 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002709 return FAIL;
2710 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002711
Bram Moolenaar1c991142020-07-04 13:15:31 +02002712 // isolate one name
2713 exp_name = p;
2714 while (eval_isnamec(*p))
2715 ++p;
2716 cc = *p;
2717 *p = NUL;
2718
Bram Moolenaaredba7072021-03-13 21:14:18 +01002719 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2720 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002721 *p = cc;
2722 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002723 *end = p;
2724
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002725 if (idx < 0)
2726 {
2727 if (*p == '(' && ufunc != NULL)
2728 {
2729 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
2730 return OK;
2731 }
2732 return FAIL;
2733 }
2734
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002735 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2736 import->imp_sid,
2737 idx,
2738 type);
2739 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002740 else if (import->imp_funcname != NULL)
2741 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002742 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002743 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2744 import->imp_sid,
2745 import->imp_var_vals_idx,
2746 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002747 return OK;
2748 }
2749
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002750 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002751 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752 return FAIL;
2753}
2754
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002755 static int
2756generate_funcref(cctx_T *cctx, char_u *name)
2757{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002758 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002759
2760 if (ufunc == NULL)
2761 return FAIL;
2762
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002763 // Need to compile any default values to get the argument types.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01002764 if (func_needs_compiling(ufunc, PROFILING(ufunc))
2765 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002766 == FAIL)
2767 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002768 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002769}
2770
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771/*
2772 * Compile a variable name into a load instruction.
2773 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002774 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775 * When "error" is FALSE do not give an error when not found.
2776 */
2777 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002778compile_load(
2779 char_u **arg,
2780 char_u *end_arg,
2781 cctx_T *cctx,
2782 int is_expr,
2783 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002784{
2785 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002786 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002787 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002789 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002790
2791 if (*(*arg + 1) == ':')
2792 {
2793 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002794 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002795 {
2796 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002798 switch (**arg)
2799 {
2800 case 'g': isn_type = ISN_LOADGDICT; break;
2801 case 'w': isn_type = ISN_LOADWDICT; break;
2802 case 't': isn_type = ISN_LOADTDICT; break;
2803 case 'b': isn_type = ISN_LOADBDICT; break;
2804 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002805 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002806 goto theend;
2807 }
2808 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2809 goto theend;
2810 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002811 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002812 else
2813 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002814 isntype_T isn_type = ISN_DROP;
2815
2816 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2817 if (name == NULL)
2818 return FAIL;
2819
2820 switch (**arg)
2821 {
2822 case 'v': res = generate_LOADV(cctx, name, error);
2823 break;
2824 case 's': res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02002825 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002826 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002827 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2828 isn_type = ISN_LOADG;
2829 else
2830 {
2831 isn_type = ISN_LOADAUTO;
2832 vim_free(name);
2833 name = vim_strnsave(*arg, end - *arg);
2834 if (name == NULL)
2835 return FAIL;
2836 }
2837 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002838 case 'w': isn_type = ISN_LOADW; break;
2839 case 't': isn_type = ISN_LOADT; break;
2840 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002841 default: // cannot happen, just in case
2842 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002843 goto theend;
2844 }
2845 if (isn_type != ISN_DROP)
2846 {
2847 // Global, Buffer-local, Window-local and Tabpage-local
2848 // variables can be defined later, thus we don't check if it
2849 // exists, give error at runtime.
2850 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2851 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 }
2853 }
2854 else
2855 {
2856 size_t len = end - *arg;
2857 int idx;
2858 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002859 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002860
2861 name = vim_strnsave(*arg, end - *arg);
2862 if (name == NULL)
2863 return FAIL;
2864
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002865 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002867 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002868 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002869 }
2870 else
2871 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002872 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002873
Bram Moolenaar709664c2020-12-12 14:33:41 +01002874 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002876 type = lvar.lv_type;
2877 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01002878 if (lvar.lv_from_outer != 0)
2879 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002880 else
2881 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 }
2883 else
2884 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002885 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002886 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002887 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002888 || find_imported(name, 0, cctx) != NULL)
2889 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002890
Bram Moolenaar0f769812020-09-12 18:32:34 +02002891 // When evaluating an expression and the name starts with an
2892 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002893 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002894 if (res == FAIL && is_expr
2895 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002896 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897 }
2898 }
2899 if (gen_load)
2900 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01002901 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002902 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002903 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002904 cctx->ctx_outer_used = TRUE;
2905 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906 }
2907
2908 *arg = end;
2909
2910theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002911 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002912 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913 vim_free(name);
2914 return res;
2915}
2916
2917/*
2918 * Compile the argument expressions.
2919 * "arg" points to just after the "(" and is advanced to after the ")"
2920 */
2921 static int
2922compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2923{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002924 char_u *p = *arg;
2925 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002926 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927
Bram Moolenaare6085c52020-04-12 20:19:16 +02002928 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002929 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002930 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2931 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002932 if (*p == ')')
2933 {
2934 *arg = p + 1;
2935 return OK;
2936 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002937 if (must_end)
2938 {
2939 semsg(_(e_missing_comma_before_argument_str), p);
2940 return FAIL;
2941 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002942
Bram Moolenaara5565e42020-05-09 15:44:01 +02002943 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944 return FAIL;
2945 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002946
2947 if (*p != ',' && *skipwhite(p) == ',')
2948 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01002949 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002950 p = skipwhite(p);
2951 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002953 {
2954 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002955 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01002956 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002957 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002958 else
2959 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002960 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002961 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002963failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002964 emsg(_(e_missing_close));
2965 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966}
2967
2968/*
2969 * Compile a function call: name(arg1, arg2)
2970 * "arg" points to "name", "arg + varlen" to the "(".
2971 * "argcount_init" is 1 for "value->method()"
2972 * Instructions:
2973 * EVAL arg1
2974 * EVAL arg2
2975 * BCALL / DCALL / UCALL
2976 */
2977 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002978compile_call(
2979 char_u **arg,
2980 size_t varlen,
2981 cctx_T *cctx,
2982 ppconst_T *ppconst,
2983 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984{
2985 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002986 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 int argcount = argcount_init;
2988 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002989 char_u fname_buf[FLEN_FIXED + 1];
2990 char_u *tofree = NULL;
2991 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002992 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002993 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002994 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995
Bram Moolenaara5565e42020-05-09 15:44:01 +02002996 // we can evaluate "has('name')" at compile time
2997 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2998 {
2999 char_u *s = skipwhite(*arg + varlen + 1);
3000 typval_T argvars[2];
3001
3002 argvars[0].v_type = VAR_UNKNOWN;
3003 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003004 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003005 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003006 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003007 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003008 if (*s == ')' && argvars[0].v_type == VAR_STRING
3009 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003010 {
3011 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3012
3013 *arg = s + 1;
3014 argvars[1].v_type = VAR_UNKNOWN;
3015 tv->v_type = VAR_NUMBER;
3016 tv->vval.v_number = 0;
3017 f_has(argvars, tv);
3018 clear_tv(&argvars[0]);
3019 ++ppconst->pp_used;
3020 return OK;
3021 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003022 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003023 }
3024
3025 if (generate_ppconst(cctx, ppconst) == FAIL)
3026 return FAIL;
3027
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 if (varlen >= sizeof(namebuf))
3029 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003030 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 return FAIL;
3032 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003033 vim_strncpy(namebuf, *arg, varlen);
3034 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035
3036 *arg = skipwhite(*arg + varlen + 1);
3037 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003038 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039
Bram Moolenaar03290b82020-12-19 16:30:44 +01003040 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003041 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042 {
3043 int idx;
3044
3045 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003046 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003048 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003049 if (STRCMP(name, "flatten") == 0)
3050 {
3051 emsg(_(e_cannot_use_flatten_in_vim9_script));
3052 goto theend;
3053 }
3054
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003055 if (STRCMP(name, "add") == 0 && argcount == 2)
3056 {
3057 garray_T *stack = &cctx->ctx_type_stack;
3058 type_T *type = ((type_T **)stack->ga_data)[
3059 stack->ga_len - 2];
3060
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003061 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003062 if (type->tt_type == VAR_LIST)
3063 {
3064 // inline "add(list, item)" so that the type can be checked
3065 res = generate_LISTAPPEND(cctx);
3066 idx = -1;
3067 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003068 else if (type->tt_type == VAR_BLOB)
3069 {
3070 // inline "add(blob, nr)" so that the type can be checked
3071 res = generate_BLOBAPPEND(cctx);
3072 idx = -1;
3073 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003074 }
3075
3076 if (idx >= 0)
3077 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3078 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003079 else
3080 semsg(_(e_unknownfunc), namebuf);
3081 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082 }
3083
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003084 // An argument or local variable can be a function reference, this
3085 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003086 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003087 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003088 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003089 // If we can find the function by name generate the right call.
3090 // Skip global functions here, a local funcref takes precedence.
3091 ufunc = find_func(name, FALSE, cctx);
3092 if (ufunc != NULL && !func_is_global(ufunc))
3093 {
3094 res = generate_CALL(cctx, ufunc, argcount);
3095 goto theend;
3096 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003097 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098
3099 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003100 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003101 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003103 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003104 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003105 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003106 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003107 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003108
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003109 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003110 goto theend;
3111 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112
Bram Moolenaar0f769812020-09-12 18:32:34 +02003113 // If we can find a global function by name generate the right call.
3114 if (ufunc != NULL)
3115 {
3116 res = generate_CALL(cctx, ufunc, argcount);
3117 goto theend;
3118 }
3119
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003120 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003121 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003122 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003123 res = generate_UCALL(cctx, name, argcount);
3124 else
3125 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003126
3127theend:
3128 vim_free(tofree);
3129 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130}
3131
3132// like NAMESPACE_CHAR but with 'a' and 'l'.
3133#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3134
3135/*
3136 * Find the end of a variable or function name. Unlike find_name_end() this
3137 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003138 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139 * Return a pointer to just after the name. Equal to "arg" if there is no
3140 * valid name.
3141 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003142 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003143to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144{
3145 char_u *p;
3146
3147 // Quick check for valid starting character.
3148 if (!eval_isnamec1(*arg))
3149 return arg;
3150
3151 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3152 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3153 // and can be used in slice "[n:]".
3154 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003155 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3157 break;
3158 return p;
3159}
3160
3161/*
3162 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003163 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 */
3165 char_u *
3166to_name_const_end(char_u *arg)
3167{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003168 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 typval_T rettv;
3170
3171 if (p == arg && *arg == '[')
3172 {
3173
3174 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003175 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 p = arg;
3177 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 return p;
3179}
3180
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181/*
3182 * parse a list: [expr, expr]
3183 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003184 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 */
3186 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003187compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188{
3189 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003190 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003192 int is_const;
3193 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003194
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003195 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003197 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003198 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003199 semsg(_(e_list_end), *arg);
3200 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003201 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003202 if (*p == ',')
3203 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003204 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003205 return FAIL;
3206 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003207 if (*p == ']')
3208 {
3209 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003210 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003211 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003212 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003213 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003214 if (!is_const)
3215 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216 ++count;
3217 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003218 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003220 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3221 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003222 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003223 return FAIL;
3224 }
3225 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003226 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003227 p = skipwhite(p);
3228 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003229 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003231 ppconst->pp_is_const = is_all_const;
3232 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233}
3234
3235/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003236 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003237 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003238 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239 */
3240 static int
3241compile_lambda(char_u **arg, cctx_T *cctx)
3242{
Bram Moolenaare462f522020-12-27 14:43:30 +01003243 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 typval_T rettv;
3245 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003246 evalarg_T evalarg;
3247
3248 CLEAR_FIELD(evalarg);
3249 evalarg.eval_flags = EVAL_EVALUATE;
3250 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251
3252 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003253 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3254 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003255 {
3256 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003257 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003258 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003259
Bram Moolenaar65c44152020-12-24 15:14:01 +01003260 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003262 ++ufunc->uf_refcount;
3263 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264
Bram Moolenaar65c44152020-12-24 15:14:01 +01003265 // Compile the function into instructions.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01003266 compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003268 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3269 // points into it. Point to the original line to avoid a dangling pointer.
3270 if (evalarg.eval_tofree_cmdline != NULL)
3271 {
3272 size_t off = *arg - evalarg.eval_tofree_cmdline;
3273
3274 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3275 + off;
3276 }
3277
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003278 clear_evalarg(&evalarg, NULL);
3279
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003280 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003281 {
3282 // The return type will now be known.
3283 set_function_type(ufunc);
3284
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003285 // The function reference count will be 1. When the ISN_FUNCREF
3286 // instruction is deleted the reference count is decremented and the
3287 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003288 return generate_FUNCREF(cctx, ufunc);
3289 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003290
3291 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 return FAIL;
3293}
3294
3295/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003296 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003298 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 */
3300 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003301compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302{
3303 garray_T *instr = &cctx->ctx_instr;
3304 int count = 0;
3305 dict_T *d = dict_alloc();
3306 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003307 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003308 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003309 int is_const;
3310 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003311
3312 if (d == NULL)
3313 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003314 if (generate_ppconst(cctx, ppconst) == FAIL)
3315 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003316 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003318 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319
Bram Moolenaar23c55272020-06-21 16:58:13 +02003320 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003321 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003322 *arg = NULL;
3323 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003324 }
3325
3326 if (**arg == '}')
3327 break;
3328
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003329 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003330 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003331 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003333 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003334 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003335 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003336 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003337 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003338 if (isn->isn_type == ISN_PUSHNR)
3339 {
3340 char buf[NUMBUFLEN];
3341
3342 // Convert to string at compile time.
3343 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3344 isn->isn_type = ISN_PUSHS;
3345 isn->isn_arg.string = vim_strsave((char_u *)buf);
3346 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003347 if (isn->isn_type == ISN_PUSHS)
3348 key = isn->isn_arg.string;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003349 else if (may_generate_2STRING(-1, cctx) == FAIL)
3350 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003351 *arg = skipwhite(*arg);
3352 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003353 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003354 emsg(_(e_missing_matching_bracket_after_dict_key));
3355 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003356 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003357 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003358 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003359 else
3360 {
3361 // {"name": value},
3362 // {'name': value},
3363 // {name: value} use "name" as a literal key
3364 key = get_literal_key(arg);
3365 if (key == NULL)
3366 return FAIL;
3367 if (generate_PUSHS(cctx, key) == FAIL)
3368 return FAIL;
3369 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370
3371 // Check for duplicate keys, if using string keys.
3372 if (key != NULL)
3373 {
3374 item = dict_find(d, key, -1);
3375 if (item != NULL)
3376 {
3377 semsg(_(e_duplicate_key), key);
3378 goto failret;
3379 }
3380 item = dictitem_alloc(key);
3381 if (item != NULL)
3382 {
3383 item->di_tv.v_type = VAR_UNKNOWN;
3384 item->di_tv.v_lock = 0;
3385 if (dict_add(d, item) == FAIL)
3386 dictitem_free(item);
3387 }
3388 }
3389
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390 if (**arg != ':')
3391 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003392 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003393 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003394 else
3395 semsg(_(e_missing_dict_colon), *arg);
3396 return FAIL;
3397 }
3398 whitep = *arg + 1;
3399 if (!IS_WHITE_OR_NUL(*whitep))
3400 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003401 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 return FAIL;
3403 }
3404
Bram Moolenaar23c55272020-06-21 16:58:13 +02003405 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003406 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003407 *arg = NULL;
3408 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003409 }
3410
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003411 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003413 if (!is_const)
3414 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415 ++count;
3416
Bram Moolenaar2c330432020-04-13 14:41:35 +02003417 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003418 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003419 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003420 *arg = NULL;
3421 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003422 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423 if (**arg == '}')
3424 break;
3425 if (**arg != ',')
3426 {
3427 semsg(_(e_missing_dict_comma), *arg);
3428 goto failret;
3429 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003430 if (IS_WHITE_OR_NUL(*whitep))
3431 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003432 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003433 return FAIL;
3434 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003435 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003436 if (!IS_WHITE_OR_NUL(*whitep))
3437 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003438 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003439 return FAIL;
3440 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003441 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 }
3443
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003444 *arg = *arg + 1;
3445
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003446 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003447 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003448 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003449 *arg += STRLEN(*arg);
3450
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003452 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 return generate_NEWDICT(cctx, count);
3454
3455failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003456 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003457 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003458 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003459 *arg = (char_u *)"";
3460 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 dict_unref(d);
3462 return FAIL;
3463}
3464
3465/*
3466 * Compile "&option".
3467 */
3468 static int
3469compile_get_option(char_u **arg, cctx_T *cctx)
3470{
3471 typval_T rettv;
3472 char_u *start = *arg;
3473 int ret;
3474
3475 // parse the option and get the current value to get the type.
3476 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003477 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 if (ret == OK)
3479 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003480 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003481 char_u *name = vim_strnsave(start, *arg - start);
3482 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3483 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484
3485 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3486 vim_free(name);
3487 }
3488 clear_tv(&rettv);
3489
3490 return ret;
3491}
3492
3493/*
3494 * Compile "$VAR".
3495 */
3496 static int
3497compile_get_env(char_u **arg, cctx_T *cctx)
3498{
3499 char_u *start = *arg;
3500 int len;
3501 int ret;
3502 char_u *name;
3503
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 ++*arg;
3505 len = get_env_len(arg);
3506 if (len == 0)
3507 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003508 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509 return FAIL;
3510 }
3511
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003512 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 name = vim_strnsave(start, len + 1);
3514 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3515 vim_free(name);
3516 return ret;
3517}
3518
3519/*
3520 * Compile "@r".
3521 */
3522 static int
3523compile_get_register(char_u **arg, cctx_T *cctx)
3524{
3525 int ret;
3526
3527 ++*arg;
3528 if (**arg == NUL)
3529 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003530 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 return FAIL;
3532 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003533 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534 {
3535 emsg_invreg(**arg);
3536 return FAIL;
3537 }
3538 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3539 ++*arg;
3540 return ret;
3541}
3542
3543/*
3544 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003545 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003546 */
3547 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003548apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003550 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551
3552 // this works from end to start
3553 while (p > start)
3554 {
3555 --p;
3556 if (*p == '-' || *p == '+')
3557 {
3558 // only '-' has an effect, for '+' we only check the type
3559#ifdef FEAT_FLOAT
3560 if (rettv->v_type == VAR_FLOAT)
3561 {
3562 if (*p == '-')
3563 rettv->vval.v_float = -rettv->vval.v_float;
3564 }
3565 else
3566#endif
3567 {
3568 varnumber_T val;
3569 int error = FALSE;
3570
3571 // tv_get_number_chk() accepts a string, but we don't want that
3572 // here
3573 if (check_not_string(rettv) == FAIL)
3574 return FAIL;
3575 val = tv_get_number_chk(rettv, &error);
3576 clear_tv(rettv);
3577 if (error)
3578 return FAIL;
3579 if (*p == '-')
3580 val = -val;
3581 rettv->v_type = VAR_NUMBER;
3582 rettv->vval.v_number = val;
3583 }
3584 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003585 else if (numeric_only)
3586 {
3587 ++p;
3588 break;
3589 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003590 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591 {
3592 int v = tv2bool(rettv);
3593
3594 // '!' is permissive in the type.
3595 clear_tv(rettv);
3596 rettv->v_type = VAR_BOOL;
3597 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3598 }
3599 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003600 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601 return OK;
3602}
3603
3604/*
3605 * Recognize v: variables that are constants and set "rettv".
3606 */
3607 static void
3608get_vim_constant(char_u **arg, typval_T *rettv)
3609{
3610 if (STRNCMP(*arg, "v:true", 6) == 0)
3611 {
3612 rettv->v_type = VAR_BOOL;
3613 rettv->vval.v_number = VVAL_TRUE;
3614 *arg += 6;
3615 }
3616 else if (STRNCMP(*arg, "v:false", 7) == 0)
3617 {
3618 rettv->v_type = VAR_BOOL;
3619 rettv->vval.v_number = VVAL_FALSE;
3620 *arg += 7;
3621 }
3622 else if (STRNCMP(*arg, "v:null", 6) == 0)
3623 {
3624 rettv->v_type = VAR_SPECIAL;
3625 rettv->vval.v_number = VVAL_NULL;
3626 *arg += 6;
3627 }
3628 else if (STRNCMP(*arg, "v:none", 6) == 0)
3629 {
3630 rettv->v_type = VAR_SPECIAL;
3631 rettv->vval.v_number = VVAL_NONE;
3632 *arg += 6;
3633 }
3634}
3635
Bram Moolenaar657137c2021-01-09 15:45:23 +01003636 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003637get_compare_type(char_u *p, int *len, int *type_is)
3638{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003639 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003640 int i;
3641
3642 switch (p[0])
3643 {
3644 case '=': if (p[1] == '=')
3645 type = EXPR_EQUAL;
3646 else if (p[1] == '~')
3647 type = EXPR_MATCH;
3648 break;
3649 case '!': if (p[1] == '=')
3650 type = EXPR_NEQUAL;
3651 else if (p[1] == '~')
3652 type = EXPR_NOMATCH;
3653 break;
3654 case '>': if (p[1] != '=')
3655 {
3656 type = EXPR_GREATER;
3657 *len = 1;
3658 }
3659 else
3660 type = EXPR_GEQUAL;
3661 break;
3662 case '<': if (p[1] != '=')
3663 {
3664 type = EXPR_SMALLER;
3665 *len = 1;
3666 }
3667 else
3668 type = EXPR_SEQUAL;
3669 break;
3670 case 'i': if (p[1] == 's')
3671 {
3672 // "is" and "isnot"; but not a prefix of a name
3673 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3674 *len = 5;
3675 i = p[*len];
3676 if (!isalnum(i) && i != '_')
3677 {
3678 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3679 *type_is = TRUE;
3680 }
3681 }
3682 break;
3683 }
3684 return type;
3685}
3686
3687/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003688 * Skip over an expression, ignoring most errors.
3689 */
3690 static void
3691skip_expr_cctx(char_u **arg, cctx_T *cctx)
3692{
3693 evalarg_T evalarg;
3694
3695 CLEAR_FIELD(evalarg);
3696 evalarg.eval_cctx = cctx;
3697 skip_expr(arg, &evalarg);
3698}
3699
3700/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003702 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703 */
3704 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003705compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003707 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708
3709 // this works from end to start
3710 while (p > start)
3711 {
3712 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003713 while (VIM_ISWHITE(*p))
3714 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715 if (*p == '-' || *p == '+')
3716 {
3717 int negate = *p == '-';
3718 isn_T *isn;
3719
3720 // TODO: check type
3721 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3722 {
3723 --p;
3724 if (*p == '-')
3725 negate = !negate;
3726 }
3727 // only '-' has an effect, for '+' we only check the type
3728 if (negate)
3729 isn = generate_instr(cctx, ISN_NEGATENR);
3730 else
3731 isn = generate_instr(cctx, ISN_CHECKNR);
3732 if (isn == NULL)
3733 return FAIL;
3734 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003735 else if (numeric_only)
3736 {
3737 ++p;
3738 break;
3739 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740 else
3741 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003742 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003743
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003744 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003745 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003746 if (p[-1] == '!')
3747 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749 }
3750 if (generate_2BOOL(cctx, invert) == FAIL)
3751 return FAIL;
3752 }
3753 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003754 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003755 return OK;
3756}
3757
3758/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003759 * Compile "(expression)": recursive!
3760 * Return FAIL/OK.
3761 */
3762 static int
3763compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3764{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003765 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003766 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003767
Bram Moolenaar24156692021-01-14 20:35:49 +01003768 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3769 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003770 if (ppconst->pp_used <= PPSIZE - 10)
3771 {
3772 ret = compile_expr1(arg, cctx, ppconst);
3773 }
3774 else
3775 {
3776 // Not enough space in ppconst, flush constants.
3777 if (generate_ppconst(cctx, ppconst) == FAIL)
3778 return FAIL;
3779 ret = compile_expr0(arg, cctx);
3780 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003781 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3782 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003783 if (**arg == ')')
3784 ++*arg;
3785 else if (ret == OK)
3786 {
3787 emsg(_(e_missing_close));
3788 ret = FAIL;
3789 }
3790 return ret;
3791}
3792
3793/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003795 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003796 */
3797 static int
3798compile_subscript(
3799 char_u **arg,
3800 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003801 char_u *start_leader,
3802 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003803 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003804{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003805 char_u *name_start = *end_leader;
3806
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807 for (;;)
3808 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003809 char_u *p = skipwhite(*arg);
3810
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003811 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003812 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003813 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003814
3815 // If a following line starts with "->{" or "->X" advance to that
3816 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003817 // Also if a following line starts with ".x".
3818 if (next != NULL &&
3819 ((next[0] == '-' && next[1] == '>'
3820 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003821 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003822 {
3823 next = next_line_from_context(cctx, TRUE);
3824 if (next == NULL)
3825 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003826 *arg = next;
3827 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003828 }
3829 }
3830
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003831 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003832 // not a function call.
3833 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003835 garray_T *stack = &cctx->ctx_type_stack;
3836 type_T *type;
3837 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003839 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003840 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003841 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003842
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003843 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003844 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3845
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003846 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3848 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003849 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 return FAIL;
3851 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003852 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003854 char_u *pstart = p;
3855
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003856 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003857 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003858 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003859
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860 // something->method()
3861 // Apply the '!', '-' and '+' first:
3862 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003863 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003866 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003867 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003868 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003869 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003870 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003871 int argcount = 1;
3872 char_u *expr;
3873 garray_T *stack;
3874 type_T *type;
3875
3876 // Funcref call: list->(Refs[2])(arg)
3877 // or lambda: list->((arg) => expr)(arg)
3878 // Fist compile the arguments.
3879 expr = *arg;
3880 *arg = skipwhite(*arg + 1);
3881 skip_expr_cctx(arg, cctx);
3882 *arg = skipwhite(*arg);
3883 if (**arg != ')')
3884 {
3885 semsg(_(e_missing_paren), *arg);
3886 return FAIL;
3887 }
3888 ++*arg;
3889 if (**arg != '(')
3890 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003891 if (*skipwhite(*arg) == '(')
3892 emsg(_(e_nowhitespace));
3893 else
3894 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01003895 return FAIL;
3896 }
3897
3898 *arg = skipwhite(*arg + 1);
3899 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3900 return FAIL;
3901
3902 // Compile the function expression.
3903 if (compile_parenthesis(&expr, cctx, ppconst) == FAIL)
3904 return FAIL;
3905
3906 stack = &cctx->ctx_type_stack;
3907 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3908 if (generate_PCALL(cctx, argcount,
3909 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003910 return FAIL;
3911 }
3912 else
3913 {
3914 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003915 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003916 if (!eval_isnamec1(*p))
3917 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003918 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003919 return FAIL;
3920 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003921 if (ASCII_ISALPHA(*p) && p[1] == ':')
3922 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003923 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 ;
3925 if (*p != '(')
3926 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003927 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003928 return FAIL;
3929 }
3930 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003931 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932 return FAIL;
3933 }
3934 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003935 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003936 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003937 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003938 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003939 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003940 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003941 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003942
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003943 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003944 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003945 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003946 // TODO: blob index
3947 // TODO: more arguments
3948 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003949 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003950 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003951 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003952
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003953 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003954 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003955 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003956 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003957 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003958 // missing first index is equal to zero
3959 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003960 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003961 else
3962 {
3963 if (compile_expr0(arg, cctx) == FAIL)
3964 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003965 if (**arg == ':')
3966 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003967 semsg(_(e_white_space_required_before_and_after_str_at_str),
3968 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003969 return FAIL;
3970 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003971 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003972 return FAIL;
3973 *arg = skipwhite(*arg);
3974 }
3975 if (**arg == ':')
3976 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003977 is_slice = TRUE;
3978 ++*arg;
3979 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
3980 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003981 semsg(_(e_white_space_required_before_and_after_str_at_str),
3982 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003983 return FAIL;
3984 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003985 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003986 return FAIL;
3987 if (**arg == ']')
3988 // missing second index is equal to end of string
3989 generate_PUSHNR(cctx, -1);
3990 else
3991 {
3992 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003993 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003994 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003995 return FAIL;
3996 *arg = skipwhite(*arg);
3997 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003998 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003999
4000 if (**arg != ']')
4001 {
4002 emsg(_(e_missbrac));
4003 return FAIL;
4004 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004005 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004006
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004007 // We can index a list and a dict. If we don't know the type
4008 // we can use the index value type.
4009 // TODO: If we don't know use an instruction to figure it out at
4010 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004011 typep = ((type_T **)stack->ga_data) + stack->ga_len
4012 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004013 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02004014 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4015 // If the index is a string, the variable must be a Dict.
4016 if (*typep == &t_any && valtype == &t_string)
4017 vtype = VAR_DICT;
4018 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004019 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004020 if (need_type(valtype, &t_number, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004021 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02004022 return FAIL;
4023 if (is_slice)
4024 {
4025 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar351ead02021-01-16 16:07:01 +01004026 if (need_type(valtype, &t_number, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004027 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02004028 return FAIL;
4029 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004030 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02004031
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004032 if (vtype == VAR_DICT)
4033 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004034 if (is_slice)
4035 {
4036 emsg(_(e_cannot_slice_dictionary));
4037 return FAIL;
4038 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004039 if ((*typep)->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01004040 {
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004041 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01004042 if (*typep == &t_unknown)
4043 // empty dict was used
4044 *typep = &t_any;
4045 }
Bram Moolenaar7892b952020-07-20 22:09:34 +02004046 else
4047 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004048 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004049 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02004050 return FAIL;
4051 *typep = &t_any;
4052 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004053 if (may_generate_2STRING(-1, cctx) == FAIL)
4054 return FAIL;
4055 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
4056 return FAIL;
4057 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004058 else if (vtype == VAR_STRING)
4059 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004060 *typep = &t_string;
4061 if ((is_slice
4062 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
4063 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004064 return FAIL;
4065 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004066 else if (vtype == VAR_BLOB)
4067 {
4068 emsg("Sorry, blob index and slice not implemented yet");
4069 return FAIL;
4070 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004071 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01004072 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004073 if (is_slice)
4074 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004075 if (generate_instr_drop(cctx,
4076 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
4077 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02004078 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004079 }
Bram Moolenaared591872020-08-15 22:14:53 +02004080 else
4081 {
4082 if ((*typep)->tt_type == VAR_LIST)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01004083 {
Bram Moolenaared591872020-08-15 22:14:53 +02004084 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01004085 if (*typep == &t_unknown)
4086 // empty list was used
4087 *typep = &t_any;
4088 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004089 if (generate_instr_drop(cctx,
4090 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
4091 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02004092 return FAIL;
4093 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004094 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004095 else
4096 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02004097 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01004098 return FAIL;
4099 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004100 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004101 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004102 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004103 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004104 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004105 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004106 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004107
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004108 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004109 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004110 {
4111 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004112 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004113 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004114 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004115 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004116 while (eval_isnamec(*p))
4117 MB_PTR_ADV(p);
4118 if (p == *arg)
4119 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004120 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121 return FAIL;
4122 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004123 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124 return FAIL;
4125 *arg = p;
4126 }
4127 else
4128 break;
4129 }
4130
4131 // TODO - see handle_subscript():
4132 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4133 // Don't do this when "Func" is already a partial that was bound
4134 // explicitly (pt_auto is FALSE).
4135
4136 return OK;
4137}
4138
4139/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004140 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4141 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004143 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004144 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004145 *
4146 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147 */
4148
4149/*
4150 * number number constant
4151 * 0zFFFFFFFF Blob constant
4152 * "string" string constant
4153 * 'string' literal string constant
4154 * &option-name option value
4155 * @r register contents
4156 * identifier variable value
4157 * function() function call
4158 * $VAR environment variable
4159 * (expression) nested expression
4160 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004161 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004162 *
4163 * Also handle:
4164 * ! in front logical NOT
4165 * - in front unary minus
4166 * + in front unary plus (ignored)
4167 * trailing (arg) funcref/partial call
4168 * trailing [] subscript in String or List
4169 * trailing .name entry in Dictionary
4170 * trailing ->name() method call
4171 */
4172 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004173compile_expr7(
4174 char_u **arg,
4175 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004176 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004178 char_u *start_leader, *end_leader;
4179 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004180 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004181 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004182
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004183 ppconst->pp_is_const = FALSE;
4184
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 /*
4186 * Skip '!', '-' and '+' characters. They are handled later.
4187 */
4188 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004189 if (eval_leader(arg, TRUE) == FAIL)
4190 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004191 end_leader = *arg;
4192
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004193 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194 switch (**arg)
4195 {
4196 /*
4197 * Number constant.
4198 */
4199 case '0': // also for blob starting with 0z
4200 case '1':
4201 case '2':
4202 case '3':
4203 case '4':
4204 case '5':
4205 case '6':
4206 case '7':
4207 case '8':
4208 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004209 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004211 // Apply "-" and "+" just before the number now, right to
4212 // left. Matters especially when "->" follows. Stops at
4213 // '!'.
4214 if (apply_leader(rettv, TRUE,
4215 start_leader, &end_leader) == FAIL)
4216 {
4217 clear_tv(rettv);
4218 return FAIL;
4219 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 break;
4221
4222 /*
4223 * String constant: "string".
4224 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004225 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226 return FAIL;
4227 break;
4228
4229 /*
4230 * Literal string constant: 'str''ing'.
4231 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004232 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233 return FAIL;
4234 break;
4235
4236 /*
4237 * Constant Vim variable.
4238 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004239 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240 ret = NOTDONE;
4241 break;
4242
4243 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004244 * "true" constant
4245 */
4246 case 't': if (STRNCMP(*arg, "true", 4) == 0
4247 && !eval_isnamec((*arg)[4]))
4248 {
4249 *arg += 4;
4250 rettv->v_type = VAR_BOOL;
4251 rettv->vval.v_number = VVAL_TRUE;
4252 }
4253 else
4254 ret = NOTDONE;
4255 break;
4256
4257 /*
4258 * "false" constant
4259 */
4260 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4261 && !eval_isnamec((*arg)[5]))
4262 {
4263 *arg += 5;
4264 rettv->v_type = VAR_BOOL;
4265 rettv->vval.v_number = VVAL_FALSE;
4266 }
4267 else
4268 ret = NOTDONE;
4269 break;
4270
4271 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004272 * "null" constant
4273 */
4274 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004275 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004276 {
4277 *arg += 4;
4278 rettv->v_type = VAR_SPECIAL;
4279 rettv->vval.v_number = VVAL_NULL;
4280 }
4281 else
4282 ret = NOTDONE;
4283 break;
4284
4285 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004286 * List: [expr, expr]
4287 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004288 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4289 return FAIL;
4290 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004291 break;
4292
4293 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004294 * Dictionary: {'key': val, 'key': val}
4295 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004296 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4297 return FAIL;
4298 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 break;
4300
4301 /*
4302 * Option value: &name
4303 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004304 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4305 return FAIL;
4306 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307 break;
4308
4309 /*
4310 * Environment variable: $VAR.
4311 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004312 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4313 return FAIL;
4314 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315 break;
4316
4317 /*
4318 * Register contents: @r.
4319 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004320 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4321 return FAIL;
4322 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323 break;
4324 /*
4325 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004326 * lambda: (arg, arg) => expr
4327 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004329 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4330 ret = compile_lambda(arg, cctx);
4331 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004332 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004333 break;
4334
4335 default: ret = NOTDONE;
4336 break;
4337 }
4338 if (ret == FAIL)
4339 return FAIL;
4340
Bram Moolenaar1c747212020-05-09 18:28:34 +02004341 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004343 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004344 clear_tv(rettv);
4345 else
4346 // A constant expression can possibly be handled compile time,
4347 // return the value instead of generating code.
4348 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349 }
4350 else if (ret == NOTDONE)
4351 {
4352 char_u *p;
4353 int r;
4354
4355 if (!eval_isnamec1(**arg))
4356 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004357 if (!vim9_bad_comment(*arg))
4358 {
4359 if (ends_excmd(*skipwhite(*arg)))
4360 semsg(_(e_empty_expression_str), *arg);
4361 else
4362 semsg(_(e_name_expected_str), *arg);
4363 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004364 return FAIL;
4365 }
4366
4367 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004368 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004369 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004370 {
4371 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4372 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004374 {
4375 if (generate_ppconst(cctx, ppconst) == FAIL)
4376 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004377 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004378 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004379 if (r == FAIL)
4380 return FAIL;
4381 }
4382
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004383 // Handle following "[]", ".member", etc.
4384 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004385 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004386 ppconst) == FAIL)
4387 return FAIL;
4388 if (ppconst->pp_used > 0)
4389 {
4390 // apply the '!', '-' and '+' before the constant
4391 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004392 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004393 return FAIL;
4394 return OK;
4395 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004396 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004397 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004398 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399}
4400
4401/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004402 * Give the "white on both sides" error, taking the operator from "p[len]".
4403 */
4404 void
4405error_white_both(char_u *op, int len)
4406{
4407 char_u buf[10];
4408
4409 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004410 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004411}
4412
4413/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004414 * <type>expr7: runtime type check / conversion
4415 */
4416 static int
4417compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4418{
4419 type_T *want_type = NULL;
4420
4421 // Recognize <type>
4422 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4423 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004424 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004425 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4426 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004427 return FAIL;
4428
4429 if (**arg != '>')
4430 {
4431 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004432 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004433 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004434 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004435 return FAIL;
4436 }
4437 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004438 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004439 return FAIL;
4440 }
4441
4442 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4443 return FAIL;
4444
4445 if (want_type != NULL)
4446 {
4447 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004448 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004449 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004450
Bram Moolenaard1103582020-08-14 22:44:25 +02004451 generate_ppconst(cctx, ppconst);
4452 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004453 where.wt_index = 0;
4454 where.wt_variable = FALSE;
4455 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004456 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004457 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004458 return FAIL;
4459 }
4460 }
4461
4462 return OK;
4463}
4464
4465/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466 * * number multiplication
4467 * / number division
4468 * % number modulo
4469 */
4470 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004471compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472{
4473 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004474 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004475 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004477 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004478 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004479 return FAIL;
4480
4481 /*
4482 * Repeat computing, until no "*", "/" or "%" is following.
4483 */
4484 for (;;)
4485 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004486 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487 if (*op != '*' && *op != '/' && *op != '%')
4488 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004489 if (next != NULL)
4490 {
4491 *arg = next_line_from_context(cctx, TRUE);
4492 op = skipwhite(*arg);
4493 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004494
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004495 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004496 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004497 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004498 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004499 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004500 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004501 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004502
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004503 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004504 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004505 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004506
4507 if (ppconst->pp_used == ppconst_used + 2
4508 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4509 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004510 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004511 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4512 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4513 varnumber_T res = 0;
4514 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004515
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004516 // both are numbers: compute the result
4517 switch (*op)
4518 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004519 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004520 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004521 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004522 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004523 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004524 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004525 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004526 break;
4527 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004528 if (failed)
4529 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004530 tv1->vval.v_number = res;
4531 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004532 }
4533 else
4534 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004535 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004536 generate_two_op(cctx, op);
4537 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004538 }
4539
4540 return OK;
4541}
4542
4543/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004544 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 * - number subtraction
4546 * .. string concatenation
4547 */
4548 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004549compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004550{
4551 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004552 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004554 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555
4556 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004557 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004558 return FAIL;
4559
4560 /*
4561 * Repeat computing, until no "+", "-" or ".." is following.
4562 */
4563 for (;;)
4564 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004565 op = may_peek_next_line(cctx, *arg, &next);
4566 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004567 break;
4568 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004569 if (next != NULL)
4570 {
4571 *arg = next_line_from_context(cctx, TRUE);
4572 op = skipwhite(*arg);
4573 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004574
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004575 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004577 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004578 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004579 }
4580
Bram Moolenaare0de1712020-12-02 17:36:54 +01004581 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004582 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004583
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004584 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004585 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004586 return FAIL;
4587
Bram Moolenaara5565e42020-05-09 15:44:01 +02004588 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004589 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004590 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4591 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4592 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4593 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004595 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4596 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004597
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004598 // concat/subtract/add constant numbers
4599 if (*op == '+')
4600 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4601 else if (*op == '-')
4602 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4603 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004604 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004605 // concatenate constant strings
4606 char_u *s1 = tv1->vval.v_string;
4607 char_u *s2 = tv2->vval.v_string;
4608 size_t len1 = STRLEN(s1);
4609
4610 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4611 if (tv1->vval.v_string == NULL)
4612 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004613 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004614 return FAIL;
4615 }
4616 mch_memmove(tv1->vval.v_string, s1, len1);
4617 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004618 vim_free(s1);
4619 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004620 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004621 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622 }
4623 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004624 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004625 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004626 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004627 if (*op == '.')
4628 {
4629 if (may_generate_2STRING(-2, cctx) == FAIL
4630 || may_generate_2STRING(-1, cctx) == FAIL)
4631 return FAIL;
4632 generate_instr_drop(cctx, ISN_CONCAT, 1);
4633 }
4634 else
4635 generate_two_op(cctx, op);
4636 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637 }
4638
4639 return OK;
4640}
4641
4642/*
4643 * expr5a == expr5b
4644 * expr5a =~ expr5b
4645 * expr5a != expr5b
4646 * expr5a !~ expr5b
4647 * expr5a > expr5b
4648 * expr5a >= expr5b
4649 * expr5a < expr5b
4650 * expr5a <= expr5b
4651 * expr5a is expr5b
4652 * expr5a isnot expr5b
4653 *
4654 * Produces instructions:
4655 * EVAL expr5a Push result of "expr5a"
4656 * EVAL expr5b Push result of "expr5b"
4657 * COMPARE one of the compare instructions
4658 */
4659 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004660compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004661{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004662 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004663 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004664 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004666 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004667 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668
4669 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004670 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004671 return FAIL;
4672
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004673 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004674 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004675
4676 /*
4677 * If there is a comparative operator, use it.
4678 */
4679 if (type != EXPR_UNKNOWN)
4680 {
4681 int ic = FALSE; // Default: do not ignore case
4682
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004683 if (next != NULL)
4684 {
4685 *arg = next_line_from_context(cctx, TRUE);
4686 p = skipwhite(*arg);
4687 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004688 if (type_is && (p[len] == '?' || p[len] == '#'))
4689 {
4690 semsg(_(e_invexpr2), *arg);
4691 return FAIL;
4692 }
4693 // extra question mark appended: ignore case
4694 if (p[len] == '?')
4695 {
4696 ic = TRUE;
4697 ++len;
4698 }
4699 // extra '#' appended: match case (ignored)
4700 else if (p[len] == '#')
4701 ++len;
4702 // nothing appended: match case
4703
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004704 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004706 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004707 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004708 }
4709
4710 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004711 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004712 return FAIL;
4713
Bram Moolenaara5565e42020-05-09 15:44:01 +02004714 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715 return FAIL;
4716
Bram Moolenaara5565e42020-05-09 15:44:01 +02004717 if (ppconst->pp_used == ppconst_used + 2)
4718 {
4719 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4720 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4721 int ret;
4722
4723 // Both sides are a constant, compute the result now.
4724 // First check for a valid combination of types, this is more
4725 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004726 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004727 ret = FAIL;
4728 else
4729 {
4730 ret = typval_compare(tv1, tv2, type, ic);
4731 tv1->v_type = VAR_BOOL;
4732 tv1->vval.v_number = tv1->vval.v_number
4733 ? VVAL_TRUE : VVAL_FALSE;
4734 clear_tv(tv2);
4735 --ppconst->pp_used;
4736 }
4737 return ret;
4738 }
4739
4740 generate_ppconst(cctx, ppconst);
4741 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004742 }
4743
4744 return OK;
4745}
4746
Bram Moolenaar7f141552020-05-09 17:35:53 +02004747static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4748
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749/*
4750 * Compile || or &&.
4751 */
4752 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004753compile_and_or(
4754 char_u **arg,
4755 cctx_T *cctx,
4756 char *op,
4757 ppconst_T *ppconst,
4758 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004759{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004760 char_u *next;
4761 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762 int opchar = *op;
4763
4764 if (p[0] == opchar && p[1] == opchar)
4765 {
4766 garray_T *instr = &cctx->ctx_instr;
4767 garray_T end_ga;
4768
4769 /*
4770 * Repeat until there is no following "||" or "&&"
4771 */
4772 ga_init2(&end_ga, sizeof(int), 10);
4773 while (p[0] == opchar && p[1] == opchar)
4774 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004775 long start_lnum = SOURCING_LNUM;
4776 int start_ctx_lnum = cctx->ctx_lnum;
4777 int save_lnum;
4778
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004779 if (next != NULL)
4780 {
4781 *arg = next_line_from_context(cctx, TRUE);
4782 p = skipwhite(*arg);
4783 }
4784
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004785 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4786 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004787 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02004788 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004789 return FAIL;
4790 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004791
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004792 // TODO: use ppconst if the value is a constant and check
4793 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004794 generate_ppconst(cctx, ppconst);
4795
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004796 // Every part must evaluate to a bool.
Bram Moolenaara7511c02021-04-03 21:47:07 +02004797 SOURCING_LNUM = start_lnum;
4798 save_lnum = cctx->ctx_lnum;
4799 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004800 if (bool_on_stack(cctx) == FAIL)
4801 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004802 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004803 ga_clear(&end_ga);
4804 return FAIL;
4805 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02004806 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004807
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004808 if (ga_grow(&end_ga, 1) == FAIL)
4809 {
4810 ga_clear(&end_ga);
4811 return FAIL;
4812 }
4813 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4814 ++end_ga.ga_len;
4815 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004816 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004817
4818 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004819 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004820 {
4821 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004822 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004823 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004824
Bram Moolenaara5565e42020-05-09 15:44:01 +02004825 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4826 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004827 {
4828 ga_clear(&end_ga);
4829 return FAIL;
4830 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004831
4832 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004833 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004834 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004835
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004836 // Every part must evaluate to a bool.
4837 if (bool_on_stack(cctx) == FAIL)
4838 {
4839 ga_clear(&end_ga);
4840 return FAIL;
4841 }
4842
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004843 // Fill in the end label in all jumps.
4844 while (end_ga.ga_len > 0)
4845 {
4846 isn_T *isn;
4847
4848 --end_ga.ga_len;
4849 isn = ((isn_T *)instr->ga_data)
4850 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4851 isn->isn_arg.jump.jump_where = instr->ga_len;
4852 }
4853 ga_clear(&end_ga);
4854 }
4855
4856 return OK;
4857}
4858
4859/*
4860 * expr4a && expr4a && expr4a logical AND
4861 *
4862 * Produces instructions:
4863 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004864 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004865 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004866 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004867 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004868 * EVAL expr4c Push result of "expr4c"
4869 * end:
4870 */
4871 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004872compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004873{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004874 int ppconst_used = ppconst->pp_used;
4875
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004876 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004877 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004878 return FAIL;
4879
4880 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004881 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004882}
4883
4884/*
4885 * expr3a || expr3b || expr3c logical OR
4886 *
4887 * Produces instructions:
4888 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004889 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004890 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004891 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004892 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893 * EVAL expr3c Push result of "expr3c"
4894 * end:
4895 */
4896 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004897compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004898{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004899 int ppconst_used = ppconst->pp_used;
4900
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004901 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004902 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004903 return FAIL;
4904
4905 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004906 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004907}
4908
4909/*
4910 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004911 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004912 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004913 * JUMP_IF_FALSE alt jump if false
4914 * EVAL expr1a
4915 * JUMP_ALWAYS end
4916 * alt: EVAL expr1b
4917 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004918 *
4919 * Toplevel expression: expr2 ?? expr1
4920 * Produces instructions:
4921 * EVAL expr2 Push result of "expr2"
4922 * JUMP_AND_KEEP_IF_TRUE end jump if true
4923 * EVAL expr1
4924 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004925 */
4926 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004927compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004928{
4929 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004930 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004931 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004932
Bram Moolenaar3988f642020-08-27 22:43:03 +02004933 // Ignore all kinds of errors when not producing code.
4934 if (cctx->ctx_skip == SKIP_YES)
4935 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004936 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004937 return OK;
4938 }
4939
Bram Moolenaar61a89812020-05-07 16:58:17 +02004940 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004941 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004942 return FAIL;
4943
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004944 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004945 if (*p == '?')
4946 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004947 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948 garray_T *instr = &cctx->ctx_instr;
4949 garray_T *stack = &cctx->ctx_type_stack;
4950 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004951 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004952 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004953 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004954 int has_const_expr = FALSE;
4955 int const_value = FALSE;
4956 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004957
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004958 if (next != NULL)
4959 {
4960 *arg = next_line_from_context(cctx, TRUE);
4961 p = skipwhite(*arg);
4962 }
4963
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004964 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004965 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004966 semsg(_(e_white_space_required_before_and_after_str_at_str),
4967 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004968 return FAIL;
4969 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004970
Bram Moolenaara5565e42020-05-09 15:44:01 +02004971 if (ppconst->pp_used == ppconst_used + 1)
4972 {
4973 // the condition is a constant, we know whether the ? or the :
4974 // expression is to be evaluated.
4975 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004976 if (op_falsy)
4977 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4978 else
4979 {
4980 int error = FALSE;
4981
4982 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4983 &error);
4984 if (error)
4985 return FAIL;
4986 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004987 cctx->ctx_skip = save_skip == SKIP_YES ||
4988 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4989
4990 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4991 // "left ?? right" and "left" is truthy: produce "left"
4992 generate_ppconst(cctx, ppconst);
4993 else
4994 {
4995 clear_tv(&ppconst->pp_tv[ppconst_used]);
4996 --ppconst->pp_used;
4997 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004998 }
4999 else
5000 {
5001 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005002 if (op_falsy)
5003 end_idx = instr->ga_len;
5004 generate_JUMP(cctx, op_falsy
5005 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5006 if (op_falsy)
5007 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005008 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005009
5010 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005011 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005012 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005013 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005014 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005015
Bram Moolenaara5565e42020-05-09 15:44:01 +02005016 if (!has_const_expr)
5017 {
5018 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005019
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005020 if (!op_falsy)
5021 {
5022 // remember the type and drop it
5023 --stack->ga_len;
5024 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005025
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005026 end_idx = instr->ga_len;
5027 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005028
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005029 // jump here from JUMP_IF_FALSE
5030 isn = ((isn_T *)instr->ga_data) + alt_idx;
5031 isn->isn_arg.jump.jump_where = instr->ga_len;
5032 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005033 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005034
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005035 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005036 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005037 // Check for the ":".
5038 p = may_peek_next_line(cctx, *arg, &next);
5039 if (*p != ':')
5040 {
5041 emsg(_(e_missing_colon));
5042 return FAIL;
5043 }
5044 if (next != NULL)
5045 {
5046 *arg = next_line_from_context(cctx, TRUE);
5047 p = skipwhite(*arg);
5048 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005049
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005050 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5051 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005052 semsg(_(e_white_space_required_before_and_after_str_at_str),
5053 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005054 return FAIL;
5055 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005056
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005057 // evaluate the third expression
5058 if (has_const_expr)
5059 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005060 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005061 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005062 return FAIL;
5063 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5064 return FAIL;
5065 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005066
Bram Moolenaara5565e42020-05-09 15:44:01 +02005067 if (!has_const_expr)
5068 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005069 type_T **typep;
5070
Bram Moolenaara5565e42020-05-09 15:44:01 +02005071 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005072
Bram Moolenaara5565e42020-05-09 15:44:01 +02005073 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005074 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5075 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005076
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005077 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005078 isn = ((isn_T *)instr->ga_data) + end_idx;
5079 isn->isn_arg.jump.jump_where = instr->ga_len;
5080 }
5081
5082 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005083 }
5084 return OK;
5085}
5086
5087/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005088 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005089 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5090 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005091 */
5092 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005093compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005094{
5095 ppconst_T ppconst;
5096
5097 CLEAR_FIELD(ppconst);
5098 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5099 {
5100 clear_ppconst(&ppconst);
5101 return FAIL;
5102 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005103 if (is_const != NULL)
5104 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005105 if (generate_ppconst(cctx, &ppconst) == FAIL)
5106 return FAIL;
5107 return OK;
5108}
5109
5110/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005111 * Toplevel expression.
5112 */
5113 static int
5114compile_expr0(char_u **arg, cctx_T *cctx)
5115{
5116 return compile_expr0_ext(arg, cctx, NULL);
5117}
5118
5119/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005120 * compile "return [expr]"
5121 */
5122 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005123compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005124{
5125 char_u *p = arg;
5126 garray_T *stack = &cctx->ctx_type_stack;
5127 type_T *stack_type;
5128
5129 if (*p != NUL && *p != '|' && *p != '\n')
5130 {
5131 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02005132 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005133 return NULL;
5134
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005135 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005136 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005137 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01005138 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005139 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5140 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005141 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005142 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005143 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005144 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005145 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005146 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5147 && stack_type->tt_type != VAR_VOID
5148 && stack_type->tt_type != VAR_UNKNOWN)
5149 {
5150 emsg(_(e_returning_value_in_function_without_return_type));
5151 return NULL;
5152 }
5153 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005154 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005155 return NULL;
5156 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005157 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005158 }
5159 else
5160 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005161 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005162 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005163 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5164 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005165 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005166 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005167 return NULL;
5168 }
5169
5170 // No argument, return zero.
5171 generate_PUSHNR(cctx, 0);
5172 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005173
5174 // Undo any command modifiers.
5175 generate_undo_cmdmods(cctx);
5176
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005177 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005178 return NULL;
5179
5180 // "return val | endif" is possible
5181 return skipwhite(p);
5182}
5183
5184/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005185 * Get a line from the compilation context, compatible with exarg_T getline().
5186 * Return a pointer to the line in allocated memory.
5187 * Return NULL for end-of-file or some error.
5188 */
5189 static char_u *
5190exarg_getline(
5191 int c UNUSED,
5192 void *cookie,
5193 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005194 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005195{
5196 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005197 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005198
Bram Moolenaar66250c92020-08-20 15:02:42 +02005199 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005200 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005201 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005202 return NULL;
5203 ++cctx->ctx_lnum;
5204 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5205 // Comment lines result in NULL pointers, skip them.
5206 if (p != NULL)
5207 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005208 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005209}
5210
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005211 void
5212fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5213{
5214 eap->getline = exarg_getline;
5215 eap->cookie = cctx;
5216}
5217
Bram Moolenaar04b12692020-05-04 23:24:44 +02005218/*
5219 * Compile a nested :def command.
5220 */
5221 static char_u *
5222compile_nested_function(exarg_T *eap, cctx_T *cctx)
5223{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005224 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005225 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005226 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005227 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005228 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005229 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005230
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005231 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005232 {
5233 emsg(_(e_cannot_use_bang_with_nested_def));
5234 return NULL;
5235 }
5236
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005237 if (*name_start == '/')
5238 {
5239 name_end = skip_regexp(name_start + 1, '/', TRUE);
5240 if (*name_end == '/')
5241 ++name_end;
5242 eap->nextcmd = check_nextcmd(name_end);
5243 }
5244 if (name_end == name_start || *skipwhite(name_end) != '(')
5245 {
5246 if (!ends_excmd2(name_start, name_end))
5247 {
5248 semsg(_(e_invalid_command_str), eap->cmd);
5249 return NULL;
5250 }
5251
5252 // "def" or "def Name": list functions
5253 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5254 return NULL;
5255 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5256 }
5257
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005258 // Only g:Func() can use a namespace.
5259 if (name_start[1] == ':' && !is_global)
5260 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005261 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005262 return NULL;
5263 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005264 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005265 return NULL;
5266
Bram Moolenaar04b12692020-05-04 23:24:44 +02005267 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005268 fill_exarg_from_cctx(eap, cctx);
5269
Bram Moolenaar04b12692020-05-04 23:24:44 +02005270 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005271 lambda_name = vim_strsave(get_lambda_name());
5272 if (lambda_name == NULL)
5273 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005274 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005275
Bram Moolenaar822ba242020-05-24 23:00:18 +02005276 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005277 {
5278 r = eap->skip ? OK : FAIL;
5279 goto theend;
5280 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005281
5282 // copy over the block scope IDs before compiling
5283 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5284 {
5285 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5286
5287 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5288 if (ufunc->uf_block_ids != NULL)
5289 {
5290 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5291 sizeof(int) * block_depth);
5292 ufunc->uf_block_depth = block_depth;
5293 }
5294 }
5295
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005296 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5297 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005298 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005299 {
5300 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005301 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005302 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005303
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005304 if (is_global)
5305 {
5306 char_u *func_name = vim_strnsave(name_start + 2,
5307 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005308
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005309 if (func_name == NULL)
5310 r = FAIL;
5311 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005312 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005313 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005314 lambda_name = NULL;
5315 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005316 }
5317 else
5318 {
5319 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005320 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005321 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005322
Bram Moolenaareef21022020-08-01 22:16:43 +02005323 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005324 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005325 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005326 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005327 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5328 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005329 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005330
5331theend:
5332 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005333 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005334}
5335
5336/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005337 * Return the length of an assignment operator, or zero if there isn't one.
5338 */
5339 int
5340assignment_len(char_u *p, int *heredoc)
5341{
5342 if (*p == '=')
5343 {
5344 if (p[1] == '<' && p[2] == '<')
5345 {
5346 *heredoc = TRUE;
5347 return 3;
5348 }
5349 return 1;
5350 }
5351 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5352 return 2;
5353 if (STRNCMP(p, "..=", 3) == 0)
5354 return 3;
5355 return 0;
5356}
5357
5358// words that cannot be used as a variable
5359static char *reserved[] = {
5360 "true",
5361 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005362 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005363 NULL
5364};
5365
Bram Moolenaar752fc692021-01-04 21:57:11 +01005366// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005367typedef enum {
5368 dest_local,
5369 dest_option,
5370 dest_env,
5371 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005372 dest_buffer,
5373 dest_window,
5374 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005375 dest_vimvar,
5376 dest_script,
5377 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005378 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005379} assign_dest_T;
5380
Bram Moolenaar752fc692021-01-04 21:57:11 +01005381// Used by compile_lhs() to store information about the LHS of an assignment
5382// and one argument of ":unlet" with an index.
5383typedef struct {
5384 assign_dest_T lhs_dest; // type of destination
5385
5386 char_u *lhs_name; // allocated name including
5387 // "[expr]" or ".name".
5388 size_t lhs_varlen; // length of the variable without
5389 // "[expr]" or ".name"
5390 char_u *lhs_dest_end; // end of the destination, including
5391 // "[expr]" or ".name".
5392
5393 int lhs_has_index; // has "[expr]" or ".name"
5394
5395 int lhs_new_local; // create new local variable
5396 int lhs_opt_flags; // for when destination is an option
5397 int lhs_vimvaridx; // for when destination is a v:var
5398
5399 lvar_T lhs_local_lvar; // used for existing local destination
5400 lvar_T lhs_arg_lvar; // used for argument destination
5401 lvar_T *lhs_lvar; // points to destination lvar
5402 int lhs_scriptvar_sid;
5403 int lhs_scriptvar_idx;
5404
5405 int lhs_has_type; // type was specified
5406 type_T *lhs_type;
5407 type_T *lhs_member_type;
5408} lhs_T;
5409
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005410/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005411 * Generate the load instruction for "name".
5412 */
5413 static void
5414generate_loadvar(
5415 cctx_T *cctx,
5416 assign_dest_T dest,
5417 char_u *name,
5418 lvar_T *lvar,
5419 type_T *type)
5420{
5421 switch (dest)
5422 {
5423 case dest_option:
5424 // TODO: check the option exists
5425 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5426 break;
5427 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005428 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5429 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5430 else
5431 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005432 break;
5433 case dest_buffer:
5434 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5435 break;
5436 case dest_window:
5437 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5438 break;
5439 case dest_tab:
5440 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5441 break;
5442 case dest_script:
5443 compile_load_scriptvar(cctx,
5444 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5445 break;
5446 case dest_env:
5447 // Include $ in the name here
5448 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5449 break;
5450 case dest_reg:
5451 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5452 break;
5453 case dest_vimvar:
5454 generate_LOADV(cctx, name + 2, TRUE);
5455 break;
5456 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005457 if (lvar->lv_from_outer > 0)
5458 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5459 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005460 else
5461 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5462 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005463 case dest_expr:
5464 // list or dict value should already be on the stack.
5465 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005466 }
5467}
5468
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005469/*
5470 * Skip over "[expr]" or ".member".
5471 * Does not check for any errors.
5472 */
5473 static char_u *
5474skip_index(char_u *start)
5475{
5476 char_u *p = start;
5477
5478 if (*p == '[')
5479 {
5480 p = skipwhite(p + 1);
5481 (void)skip_expr(&p, NULL);
5482 p = skipwhite(p);
5483 if (*p == ']')
5484 return p + 1;
5485 return p;
5486 }
5487 // if (*p == '.')
5488 return to_name_end(p + 1, TRUE);
5489}
5490
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005491 void
5492vim9_declare_error(char_u *name)
5493{
5494 char *scope = "";
5495
5496 switch (*name)
5497 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005498 case 'g': scope = _("global"); break;
5499 case 'b': scope = _("buffer"); break;
5500 case 'w': scope = _("window"); break;
5501 case 't': scope = _("tab"); break;
5502 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005503 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005504 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005505 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005506 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005507 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005508 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005509 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005510 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005511 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005512}
5513
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005514/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005515 * For one assignment figure out the type of destination. Return it in "dest".
5516 * When not recognized "dest" is not set.
5517 * For an option "opt_flags" is set.
5518 * For a v:var "vimvaridx" is set.
5519 * "type" is set to the destination type if known, unchanted otherwise.
5520 * Return FAIL if an error message was given.
5521 */
5522 static int
5523get_var_dest(
5524 char_u *name,
5525 assign_dest_T *dest,
5526 int cmdidx,
5527 int *opt_flags,
5528 int *vimvaridx,
5529 type_T **type,
5530 cctx_T *cctx)
5531{
5532 char_u *p;
5533
5534 if (*name == '&')
5535 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005536 int cc;
5537 long numval;
5538 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005539
5540 *dest = dest_option;
5541 if (cmdidx == CMD_final || cmdidx == CMD_const)
5542 {
5543 emsg(_(e_const_option));
5544 return FAIL;
5545 }
5546 p = name;
5547 p = find_option_end(&p, opt_flags);
5548 if (p == NULL)
5549 {
5550 // cannot happen?
5551 emsg(_(e_letunexp));
5552 return FAIL;
5553 }
5554 cc = *p;
5555 *p = NUL;
5556 opt_type = get_option_value(skip_option_env_lead(name),
5557 &numval, NULL, *opt_flags);
5558 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005559 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005560 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005561 case gov_unknown:
5562 semsg(_(e_unknown_option), name);
5563 return FAIL;
5564 case gov_string:
5565 case gov_hidden_string:
5566 *type = &t_string;
5567 break;
5568 case gov_bool:
5569 case gov_hidden_bool:
5570 *type = &t_bool;
5571 break;
5572 case gov_number:
5573 case gov_hidden_number:
5574 *type = &t_number;
5575 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005576 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005577 }
5578 else if (*name == '$')
5579 {
5580 *dest = dest_env;
5581 *type = &t_string;
5582 }
5583 else if (*name == '@')
5584 {
5585 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5586 {
5587 emsg_invreg(name[1]);
5588 return FAIL;
5589 }
5590 *dest = dest_reg;
5591 *type = &t_string;
5592 }
5593 else if (STRNCMP(name, "g:", 2) == 0)
5594 {
5595 *dest = dest_global;
5596 }
5597 else if (STRNCMP(name, "b:", 2) == 0)
5598 {
5599 *dest = dest_buffer;
5600 }
5601 else if (STRNCMP(name, "w:", 2) == 0)
5602 {
5603 *dest = dest_window;
5604 }
5605 else if (STRNCMP(name, "t:", 2) == 0)
5606 {
5607 *dest = dest_tab;
5608 }
5609 else if (STRNCMP(name, "v:", 2) == 0)
5610 {
5611 typval_T *vtv;
5612 int di_flags;
5613
5614 *vimvaridx = find_vim_var(name + 2, &di_flags);
5615 if (*vimvaridx < 0)
5616 {
5617 semsg(_(e_variable_not_found_str), name);
5618 return FAIL;
5619 }
5620 // We use the current value of "sandbox" here, is that OK?
5621 if (var_check_ro(di_flags, name, FALSE))
5622 return FAIL;
5623 *dest = dest_vimvar;
5624 vtv = get_vim_var_tv(*vimvaridx);
5625 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5626 }
5627 return OK;
5628}
5629
5630/*
5631 * Generate a STORE instruction for "dest", not being "dest_local".
5632 * Return FAIL when out of memory.
5633 */
5634 static int
5635generate_store_var(
5636 cctx_T *cctx,
5637 assign_dest_T dest,
5638 int opt_flags,
5639 int vimvaridx,
5640 int scriptvar_idx,
5641 int scriptvar_sid,
5642 type_T *type,
5643 char_u *name)
5644{
5645 switch (dest)
5646 {
5647 case dest_option:
5648 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5649 opt_flags);
5650 case dest_global:
5651 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005652 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5653 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005654 case dest_buffer:
5655 // include b: with the name, easier to execute that way
5656 return generate_STORE(cctx, ISN_STOREB, 0, name);
5657 case dest_window:
5658 // include w: with the name, easier to execute that way
5659 return generate_STORE(cctx, ISN_STOREW, 0, name);
5660 case dest_tab:
5661 // include t: with the name, easier to execute that way
5662 return generate_STORE(cctx, ISN_STORET, 0, name);
5663 case dest_env:
5664 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5665 case dest_reg:
5666 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5667 case dest_vimvar:
5668 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5669 case dest_script:
5670 if (scriptvar_idx < 0)
5671 {
5672 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005673 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005674
Bram Moolenaar41d61962020-12-06 20:12:43 +01005675 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005676 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5677 scriptvar_sid, type);
5678 if (name_s != name)
5679 vim_free(name_s);
5680 return r;
5681 }
5682 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5683 scriptvar_sid, scriptvar_idx, type);
5684 case dest_local:
5685 case dest_expr:
5686 // cannot happen
5687 break;
5688 }
5689 return FAIL;
5690}
5691
Bram Moolenaar752fc692021-01-04 21:57:11 +01005692 static int
5693is_decl_command(int cmdidx)
5694{
5695 return cmdidx == CMD_let || cmdidx == CMD_var
5696 || cmdidx == CMD_final || cmdidx == CMD_const;
5697}
5698
5699/*
5700 * Figure out the LHS type and other properties for an assignment or one item
5701 * of ":unlet" with an index.
5702 * Returns OK or FAIL.
5703 */
5704 static int
5705compile_lhs(
5706 char_u *var_start,
5707 lhs_T *lhs,
5708 int cmdidx,
5709 int heredoc,
5710 int oplen,
5711 cctx_T *cctx)
5712{
5713 char_u *var_end;
5714 int is_decl = is_decl_command(cmdidx);
5715
5716 CLEAR_POINTER(lhs);
5717 lhs->lhs_dest = dest_local;
5718 lhs->lhs_vimvaridx = -1;
5719 lhs->lhs_scriptvar_idx = -1;
5720
5721 // "dest_end" is the end of the destination, including "[expr]" or
5722 // ".name".
5723 // "var_end" is the end of the variable/option/etc. name.
5724 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5725 if (*var_start == '@')
5726 var_end = var_start + 2;
5727 else
5728 {
5729 // skip over the leading "&", "&l:", "&g:" and "$"
5730 var_end = skip_option_env_lead(var_start);
5731 var_end = to_name_end(var_end, TRUE);
5732 }
5733
5734 // "a: type" is declaring variable "a" with a type, not dict "a:".
5735 if (is_decl && lhs->lhs_dest_end == var_start + 2
5736 && lhs->lhs_dest_end[-1] == ':')
5737 --lhs->lhs_dest_end;
5738 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5739 --var_end;
5740
5741 // compute the length of the destination without "[expr]" or ".name"
5742 lhs->lhs_varlen = var_end - var_start;
5743 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5744 if (lhs->lhs_name == NULL)
5745 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005746
5747 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5748 // Something follows after the variable: "var[idx]" or "var.key".
5749 lhs->lhs_has_index = TRUE;
5750
Bram Moolenaar752fc692021-01-04 21:57:11 +01005751 if (heredoc)
5752 lhs->lhs_type = &t_list_string;
5753 else
5754 lhs->lhs_type = &t_any;
5755
5756 if (cctx->ctx_skip != SKIP_YES)
5757 {
5758 int declare_error = FALSE;
5759
5760 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5761 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5762 &lhs->lhs_type, cctx) == FAIL)
5763 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02005764 if (lhs->lhs_dest != dest_local
5765 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005766 {
5767 // Specific kind of variable recognized.
5768 declare_error = is_decl;
5769 }
5770 else
5771 {
5772 int idx;
5773
5774 // No specific kind of variable recognized, just a name.
5775 for (idx = 0; reserved[idx] != NULL; ++idx)
5776 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5777 {
5778 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5779 return FAIL;
5780 }
5781
5782
5783 if (lookup_local(var_start, lhs->lhs_varlen,
5784 &lhs->lhs_local_lvar, cctx) == OK)
5785 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5786 else
5787 {
5788 CLEAR_FIELD(lhs->lhs_arg_lvar);
5789 if (arg_exists(var_start, lhs->lhs_varlen,
5790 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5791 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5792 {
5793 if (is_decl)
5794 {
5795 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5796 return FAIL;
5797 }
5798 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5799 }
5800 }
5801 if (lhs->lhs_lvar != NULL)
5802 {
5803 if (is_decl)
5804 {
5805 semsg(_(e_variable_already_declared), lhs->lhs_name);
5806 return FAIL;
5807 }
5808 }
5809 else
5810 {
5811 int script_namespace = lhs->lhs_varlen > 1
5812 && STRNCMP(var_start, "s:", 2) == 0;
5813 int script_var = (script_namespace
5814 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
5815 FALSE, cctx)
5816 : script_var_exists(var_start, lhs->lhs_varlen,
5817 TRUE, cctx)) == OK;
5818 imported_T *import =
5819 find_imported(var_start, lhs->lhs_varlen, cctx);
5820
5821 if (script_namespace || script_var || import != NULL)
5822 {
5823 char_u *rawname = lhs->lhs_name
5824 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5825
5826 if (is_decl)
5827 {
5828 if (script_namespace)
5829 semsg(_(e_cannot_declare_script_variable_in_function),
5830 lhs->lhs_name);
5831 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005832 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01005833 lhs->lhs_name);
5834 return FAIL;
5835 }
5836 else if (cctx->ctx_ufunc->uf_script_ctx_version
5837 == SCRIPT_VERSION_VIM9
5838 && script_namespace
5839 && !script_var && import == NULL)
5840 {
5841 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5842 return FAIL;
5843 }
5844
5845 lhs->lhs_dest = dest_script;
5846
5847 // existing script-local variables should have a type
5848 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5849 if (import != NULL)
5850 lhs->lhs_scriptvar_sid = import->imp_sid;
5851 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5852 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005853 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005854 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005855 lhs->lhs_scriptvar_sid, rawname,
5856 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5857 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005858 if (lhs->lhs_scriptvar_idx >= 0)
5859 {
5860 scriptitem_T *si = SCRIPT_ITEM(
5861 lhs->lhs_scriptvar_sid);
5862 svar_T *sv =
5863 ((svar_T *)si->sn_var_vals.ga_data)
5864 + lhs->lhs_scriptvar_idx;
5865 lhs->lhs_type = sv->sv_type;
5866 }
5867 }
5868 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005869 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005870 == FAIL)
5871 return FAIL;
5872 }
5873 }
5874
5875 if (declare_error)
5876 {
5877 vim9_declare_error(lhs->lhs_name);
5878 return FAIL;
5879 }
5880 }
5881
5882 // handle "a:name" as a name, not index "name" on "a"
5883 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5884 var_end = lhs->lhs_dest_end;
5885
5886 if (lhs->lhs_dest != dest_option)
5887 {
5888 if (is_decl && *var_end == ':')
5889 {
5890 char_u *p;
5891
5892 // parse optional type: "let var: type = expr"
5893 if (!VIM_ISWHITE(var_end[1]))
5894 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01005895 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005896 return FAIL;
5897 }
5898 p = skipwhite(var_end + 1);
5899 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5900 if (lhs->lhs_type == NULL)
5901 return FAIL;
5902 lhs->lhs_has_type = TRUE;
5903 }
5904 else if (lhs->lhs_lvar != NULL)
5905 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5906 }
5907
5908 if (oplen == 3 && !heredoc && lhs->lhs_dest != dest_global
5909 && lhs->lhs_type->tt_type != VAR_STRING
5910 && lhs->lhs_type->tt_type != VAR_ANY)
5911 {
5912 emsg(_(e_can_only_concatenate_to_string));
5913 return FAIL;
5914 }
5915
5916 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5917 && cctx->ctx_skip != SKIP_YES)
5918 {
5919 if (oplen > 1 && !heredoc)
5920 {
5921 // +=, /=, etc. require an existing variable
5922 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5923 return FAIL;
5924 }
5925 if (!is_decl)
5926 {
5927 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5928 return FAIL;
5929 }
5930
Bram Moolenaar3f327882021-03-17 20:56:38 +01005931 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005932 if ((lhs->lhs_type->tt_type == VAR_FUNC
5933 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01005934 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01005935 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01005936
5937 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005938 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5939 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5940 if (lhs->lhs_lvar == NULL)
5941 return FAIL;
5942 lhs->lhs_new_local = TRUE;
5943 }
5944
5945 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01005946 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005947 {
5948 // Something follows after the variable: "var[idx]" or "var.key".
5949 // TODO: should we also handle "->func()" here?
5950 if (is_decl)
5951 {
5952 emsg(_(e_cannot_use_index_when_declaring_variable));
5953 return FAIL;
5954 }
5955
5956 if (var_start[lhs->lhs_varlen] == '['
5957 || var_start[lhs->lhs_varlen] == '.')
5958 {
5959 char_u *after = var_start + lhs->lhs_varlen;
5960 char_u *p;
5961
5962 // Only the last index is used below, if there are others
5963 // before it generate code for the expression. Thus for
5964 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5965 for (;;)
5966 {
5967 p = skip_index(after);
5968 if (*p != '[' && *p != '.')
5969 break;
5970 after = p;
5971 }
5972 if (after > var_start + lhs->lhs_varlen)
5973 {
5974 lhs->lhs_varlen = after - var_start;
5975 lhs->lhs_dest = dest_expr;
5976 // We don't know the type before evaluating the expression,
5977 // use "any" until then.
5978 lhs->lhs_type = &t_any;
5979 }
5980
Bram Moolenaar752fc692021-01-04 21:57:11 +01005981 if (lhs->lhs_type->tt_member == NULL)
5982 lhs->lhs_member_type = &t_any;
5983 else
5984 lhs->lhs_member_type = lhs->lhs_type->tt_member;
5985 }
5986 else
5987 {
5988 semsg("Not supported yet: %s", var_start);
5989 return FAIL;
5990 }
5991 }
5992 return OK;
5993}
5994
5995/*
5996 * Assignment to a list or dict member, or ":unlet" for the item, using the
5997 * information in "lhs".
5998 * Returns OK or FAIL.
5999 */
6000 static int
6001compile_assign_unlet(
6002 char_u *var_start,
6003 lhs_T *lhs,
6004 int is_assign,
6005 type_T *rhs_type,
6006 cctx_T *cctx)
6007{
6008 char_u *p;
6009 int r;
6010 vartype_T dest_type;
6011 size_t varlen = lhs->lhs_varlen;
6012 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006013 int range = FALSE;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006014
6015 // Compile the "idx" in "var[idx]" or "key" in "var.key".
6016 p = var_start + varlen;
6017 if (*p == '[')
6018 {
6019 p = skipwhite(p + 1);
6020 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006021
6022 if (r == OK && *skipwhite(p) == ':')
6023 {
6024 // unlet var[idx : idx]
6025 if (is_assign)
6026 {
6027 semsg(_(e_cannot_use_range_with_assignment_str), p);
6028 return FAIL;
6029 }
6030 range = TRUE;
6031 p = skipwhite(p);
6032 if (!IS_WHITE_OR_NUL(p[-1]) || !IS_WHITE_OR_NUL(p[1]))
6033 {
6034 semsg(_(e_white_space_required_before_and_after_str_at_str),
6035 ":", p);
6036 return FAIL;
6037 }
6038 p = skipwhite(p + 1);
6039 r = compile_expr0(&p, cctx);
6040 }
6041
Bram Moolenaar752fc692021-01-04 21:57:11 +01006042 if (r == OK && *skipwhite(p) != ']')
6043 {
6044 // this should not happen
6045 emsg(_(e_missbrac));
6046 r = FAIL;
6047 }
6048 }
6049 else // if (*p == '.')
6050 {
6051 char_u *key_end = to_name_end(p + 1, TRUE);
6052 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6053
6054 r = generate_PUSHS(cctx, key);
6055 }
6056 if (r == FAIL)
6057 return FAIL;
6058
6059 if (lhs->lhs_type == &t_any)
6060 {
6061 // Index on variable of unknown type: check at runtime.
6062 dest_type = VAR_ANY;
6063 }
6064 else
6065 {
6066 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006067 if (dest_type == VAR_DICT && range)
6068 {
6069 emsg(e_cannot_use_range_with_dictionary);
6070 return FAIL;
6071 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006072 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
6073 return FAIL;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006074 if (dest_type == VAR_LIST)
6075 {
6076 if (range
6077 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 2],
Bram Moolenaarf30a14d2021-01-17 21:51:24 +01006078 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006079 return FAIL;
6080 if (need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
6081 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
6082 return FAIL;
6083 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006084 }
6085
6086 // Load the dict or list. On the stack we then have:
6087 // - value (for assignment, not for :unlet)
6088 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006089 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006090 // - variable
6091 if (lhs->lhs_dest == dest_expr)
6092 {
6093 int c = var_start[varlen];
6094
6095 // Evaluate "ll[expr]" of "ll[expr][idx]"
6096 p = var_start;
6097 var_start[varlen] = NUL;
6098 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6099 {
6100 // this should not happen
6101 emsg(_(e_missbrac));
6102 return FAIL;
6103 }
6104 var_start[varlen] = c;
6105
6106 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6107 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6108 // now we can properly check the type
6109 if (lhs->lhs_type->tt_member != NULL && rhs_type != &t_void
Bram Moolenaar351ead02021-01-16 16:07:01 +01006110 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006111 FALSE, FALSE) == FAIL)
6112 return FAIL;
6113 }
6114 else
6115 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6116 lhs->lhs_lvar, lhs->lhs_type);
6117
6118 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
6119 {
6120 if (is_assign)
6121 {
6122 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
6123
6124 if (isn == NULL)
6125 return FAIL;
6126 isn->isn_arg.vartype = dest_type;
6127 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006128 else if (range)
6129 {
6130 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6131 return FAIL;
6132 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006133 else
6134 {
6135 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6136 return FAIL;
6137 }
6138 }
6139 else
6140 {
6141 emsg(_(e_indexable_type_required));
6142 return FAIL;
6143 }
6144
6145 return OK;
6146}
6147
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006148/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006149 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006150 * "let name"
6151 * "var name = expr"
6152 * "final name = expr"
6153 * "const name = expr"
6154 * "name = expr"
6155 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006156 * Return NULL for an error.
6157 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006158 */
6159 static char_u *
6160compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6161{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006162 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006163 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006164 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006165 char_u *ret = NULL;
6166 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006167 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006168 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006169 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006170 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006171 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006172 int oplen = 0;
6173 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006174 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006175 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006176 int is_decl = is_decl_command(cmdidx);
6177 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006178 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006179
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006180 // Skip over the "var" or "[var, var]" to get to any "=".
6181 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6182 if (p == NULL)
6183 return *arg == '[' ? arg : NULL;
6184
6185 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006186 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006187 // TODO: should we allow this, and figure out type inference from list
6188 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006189 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006190 return NULL;
6191 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006192 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006193
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006194 sp = p;
6195 p = skipwhite(p);
6196 op = p;
6197 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006198
6199 if (var_count > 0 && oplen == 0)
6200 // can be something like "[1, 2]->func()"
6201 return arg;
6202
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006203 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006204 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006205 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006206 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006207 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006208
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006209 if (heredoc)
6210 {
6211 list_T *l;
6212 listitem_T *li;
6213
6214 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006215 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006216 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006217 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006218 if (l == NULL)
6219 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006220
Bram Moolenaar078269b2020-09-21 20:35:55 +02006221 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006223 // Push each line and the create the list.
6224 FOR_ALL_LIST_ITEMS(l, li)
6225 {
6226 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6227 li->li_tv.vval.v_string = NULL;
6228 }
6229 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006230 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006231 list_free(l);
6232 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006233 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006234 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006235 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006236 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006237 char_u *wp;
6238
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006239 // for "[var, var] = expr" evaluate the expression here, loop over the
6240 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006241 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006242
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006243 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006244 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006245 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006246 if (compile_expr0(&p, cctx) == FAIL)
6247 return NULL;
6248 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006249
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006250 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006251 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006252 type_T *stacktype;
6253
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006254 stacktype = stack->ga_len == 0 ? &t_void
6255 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006256 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006257 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006258 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006259 goto theend;
6260 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006261 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006262 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006263 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006264 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006265 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6266 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006267 if (stacktype->tt_member != NULL)
6268 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006269 }
6270 }
6271
6272 /*
6273 * Loop over variables in "[var, var] = expr".
6274 * For "var = expr" and "let var: type" this is done only once.
6275 */
6276 if (var_count > 0)
6277 var_start = skipwhite(arg + 1); // skip over the "["
6278 else
6279 var_start = arg;
6280 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6281 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006282 int instr_count = -1;
6283
Bram Moolenaar752fc692021-01-04 21:57:11 +01006284 vim_free(lhs.lhs_name);
6285
6286 /*
6287 * Figure out the LHS type and other properties.
6288 */
6289 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6290 goto theend;
6291
6292 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006293 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006294 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006295 goto theend;
6296 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006297 if (!is_decl && lhs.lhs_lvar != NULL
6298 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006299 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006300 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006301 goto theend;
6302 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006303
6304 if (!heredoc)
6305 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006306 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006307 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006308 if (oplen > 0 && var_count == 0)
6309 {
6310 // skip over the "=" and the expression
6311 p = skipwhite(op + oplen);
6312 compile_expr0(&p, cctx);
6313 }
6314 }
6315 else if (oplen > 0)
6316 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006317 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006318 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006319
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006320 // For "var = expr" evaluate the expression.
6321 if (var_count == 0)
6322 {
6323 int r;
6324
6325 // for "+=", "*=", "..=" etc. first load the current value
6326 if (*op != '=')
6327 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006328 generate_loadvar(cctx, lhs.lhs_dest, lhs.lhs_name,
6329 lhs.lhs_lvar, lhs.lhs_type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006330
Bram Moolenaar752fc692021-01-04 21:57:11 +01006331 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006332 {
6333 // TODO: get member from list or dict
6334 emsg("Index with operation not supported yet");
6335 goto theend;
6336 }
6337 }
6338
6339 // Compile the expression. Temporarily hide the new local
6340 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006341 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006342 --cctx->ctx_locals.ga_len;
6343 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006344 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006345 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006346 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006347 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006348 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006349 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006350 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006351 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006352 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006353 ++cctx->ctx_locals.ga_len;
6354 if (r == FAIL)
6355 goto theend;
6356 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006357 else if (semicolon && var_idx == var_count - 1)
6358 {
6359 // For "[var; var] = expr" get the rest of the list
6360 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6361 goto theend;
6362 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006363 else
6364 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006365 // For "[var, var] = expr" get the "var_idx" item from the
6366 // list.
6367 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006368 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006369 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006370
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006371 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006372 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006373 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006374 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006375 if ((rhs_type->tt_type == VAR_FUNC
6376 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006377 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006378 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006379 goto theend;
6380
Bram Moolenaar752fc692021-01-04 21:57:11 +01006381 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006382 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006383 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006384 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006385 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006386 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006387 }
6388 else
6389 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006390 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006391 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006392 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006393 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006394 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006395 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006396 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006397 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006398 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006399 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006400 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006401 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006402 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006403 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006404 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006405
Bram Moolenaar77709b12021-04-03 21:01:01 +02006406 // Without operator check type here, otherwise below.
6407 // Use the line number of the assignment.
6408 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006409 if (lhs.lhs_has_index)
6410 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006411 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006412 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006413 goto theend;
6414 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006415 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006416 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006417 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006418 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006419 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006420 else if (cmdidx == CMD_final)
6421 {
6422 emsg(_(e_final_requires_a_value));
6423 goto theend;
6424 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006425 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006426 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006427 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006428 goto theend;
6429 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006430 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006431 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006432 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006433 goto theend;
6434 }
6435 else
6436 {
6437 // variables are always initialized
6438 if (ga_grow(instr, 1) == FAIL)
6439 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006440 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006441 {
6442 case VAR_BOOL:
6443 generate_PUSHBOOL(cctx, VVAL_FALSE);
6444 break;
6445 case VAR_FLOAT:
6446#ifdef FEAT_FLOAT
6447 generate_PUSHF(cctx, 0.0);
6448#endif
6449 break;
6450 case VAR_STRING:
6451 generate_PUSHS(cctx, NULL);
6452 break;
6453 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006454 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006455 break;
6456 case VAR_FUNC:
6457 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6458 break;
6459 case VAR_LIST:
6460 generate_NEWLIST(cctx, 0);
6461 break;
6462 case VAR_DICT:
6463 generate_NEWDICT(cctx, 0);
6464 break;
6465 case VAR_JOB:
6466 generate_PUSHJOB(cctx, NULL);
6467 break;
6468 case VAR_CHANNEL:
6469 generate_PUSHCHANNEL(cctx, NULL);
6470 break;
6471 case VAR_NUMBER:
6472 case VAR_UNKNOWN:
6473 case VAR_ANY:
6474 case VAR_PARTIAL:
6475 case VAR_VOID:
6476 case VAR_SPECIAL: // cannot happen
6477 generate_PUSHNR(cctx, 0);
6478 break;
6479 }
6480 }
6481 if (var_count == 0)
6482 end = p;
6483 }
6484
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006485 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006486 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006487 break;
6488
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006489 if (oplen > 0 && *op != '=')
6490 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006491 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006492 type_T *stacktype;
6493
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006494 if (*op == '.')
6495 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006496 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006497 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006498 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006499 if (
6500#ifdef FEAT_FLOAT
6501 // If variable is float operation with number is OK.
6502 !(expected == &t_float && stacktype == &t_number) &&
6503#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006504 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006505 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006506 goto theend;
6507
6508 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006509 {
6510 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6511 goto theend;
6512 }
6513 else if (*op == '+')
6514 {
6515 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006516 operator_type(lhs.lhs_member_type, stacktype),
6517 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006518 goto theend;
6519 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006520 else if (generate_two_op(cctx, op) == FAIL)
6521 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006522 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006523
Bram Moolenaar752fc692021-01-04 21:57:11 +01006524 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006525 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006526 // Use the info in "lhs" to store the value at the index in the
6527 // list or dict.
6528 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6529 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006530 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006531 }
6532 else
6533 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006534 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02006535 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01006536 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006537 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006538 generate_LOCKCONST(cctx);
6539
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006540 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006541 && (lhs.lhs_type->tt_type == VAR_DICT
6542 || lhs.lhs_type->tt_type == VAR_LIST)
6543 && lhs.lhs_type->tt_member != NULL
6544 && lhs.lhs_type->tt_member != &t_any
6545 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006546 // Set the type in the list or dict, so that it can be checked,
6547 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006548 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006549
Bram Moolenaar752fc692021-01-04 21:57:11 +01006550 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006551 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006552 if (generate_store_var(cctx, lhs.lhs_dest,
6553 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6554 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6555 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006556 goto theend;
6557 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006558 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006559 {
6560 isn_T *isn = ((isn_T *)instr->ga_data)
6561 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006562
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006563 // optimization: turn "var = 123" from ISN_PUSHNR +
6564 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006565 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006566 && instr->ga_len == instr_count + 1
6567 && isn->isn_type == ISN_PUSHNR)
6568 {
6569 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006570
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006571 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006572 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006573 isn->isn_arg.storenr.stnr_val = val;
6574 if (stack->ga_len > 0)
6575 --stack->ga_len;
6576 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006577 else if (lhs.lhs_lvar->lv_from_outer > 0)
6578 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6579 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006580 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006581 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006582 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006583 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006584
6585 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006586 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006587 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006588
6589 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006590 if (var_count > 0 && !semicolon)
6591 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006592 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006593 goto theend;
6594 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006595
Bram Moolenaarb2097502020-07-19 17:17:02 +02006596 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006597
6598theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006599 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006600 return ret;
6601}
6602
6603/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006604 * Check for an assignment at "eap->cmd", compile it if found.
6605 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6606 */
6607 static int
6608may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6609{
6610 char_u *pskip;
6611 char_u *p;
6612
6613 // Assuming the command starts with a variable or function name,
6614 // find what follows.
6615 // Skip over "var.member", "var[idx]" and the like.
6616 // Also "&opt = val", "$ENV = val" and "@r = val".
6617 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6618 ? eap->cmd + 1 : eap->cmd;
6619 p = to_name_end(pskip, TRUE);
6620 if (p > eap->cmd && *p != NUL)
6621 {
6622 char_u *var_end;
6623 int oplen;
6624 int heredoc;
6625
6626 if (eap->cmd[0] == '@')
6627 var_end = eap->cmd + 2;
6628 else
6629 var_end = find_name_end(pskip, NULL, NULL,
6630 FNE_CHECK_START | FNE_INCL_BR);
6631 oplen = assignment_len(skipwhite(var_end), &heredoc);
6632 if (oplen > 0)
6633 {
6634 size_t len = p - eap->cmd;
6635
6636 // Recognize an assignment if we recognize the variable
6637 // name:
6638 // "g:var = expr"
6639 // "local = expr" where "local" is a local var.
6640 // "script = expr" where "script" is a script-local var.
6641 // "import = expr" where "import" is an imported var
6642 // "&opt = expr"
6643 // "$ENV = expr"
6644 // "@r = expr"
6645 if (*eap->cmd == '&'
6646 || *eap->cmd == '$'
6647 || *eap->cmd == '@'
6648 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01006649 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01006650 {
6651 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6652 if (*line == NULL || *line == eap->cmd)
6653 return FAIL;
6654 return OK;
6655 }
6656 }
6657 }
6658
6659 if (*eap->cmd == '[')
6660 {
6661 // [var, var] = expr
6662 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6663 if (*line == NULL)
6664 return FAIL;
6665 if (*line != eap->cmd)
6666 return OK;
6667 }
6668 return NOTDONE;
6669}
6670
6671/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006672 * Check if "name" can be "unlet".
6673 */
6674 int
6675check_vim9_unlet(char_u *name)
6676{
6677 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6678 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006679 // "unlet s:var" is allowed in legacy script.
6680 if (*name == 's' && !script_is_vim9())
6681 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006682 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006683 return FAIL;
6684 }
6685 return OK;
6686}
6687
6688/*
6689 * Callback passed to ex_unletlock().
6690 */
6691 static int
6692compile_unlet(
6693 lval_T *lvp,
6694 char_u *name_end,
6695 exarg_T *eap,
6696 int deep UNUSED,
6697 void *coookie)
6698{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006699 cctx_T *cctx = coookie;
6700 char_u *p = lvp->ll_name;
6701 int cc = *name_end;
6702 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006703
Bram Moolenaar752fc692021-01-04 21:57:11 +01006704 if (cctx->ctx_skip == SKIP_YES)
6705 return OK;
6706
6707 *name_end = NUL;
6708 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006709 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006710 // :unlet $ENV_VAR
6711 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6712 }
6713 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6714 {
6715 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006716
Bram Moolenaar752fc692021-01-04 21:57:11 +01006717 // This is similar to assigning: lookup the list/dict, compile the
6718 // idx/key. Then instead of storing the value unlet the item.
6719 // unlet {list}[idx]
6720 // unlet {dict}[key] dict.key
6721 //
6722 // Figure out the LHS type and other properties.
6723 //
6724 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6725
6726 // : unlet an indexed item
6727 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006728 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006729 iemsg("called compile_lhs() without an index");
6730 ret = FAIL;
6731 }
6732 else
6733 {
6734 // Use the info in "lhs" to unlet the item at the index in the
6735 // list or dict.
6736 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006737 }
6738
Bram Moolenaar752fc692021-01-04 21:57:11 +01006739 vim_free(lhs.lhs_name);
6740 }
6741 else if (check_vim9_unlet(p) == FAIL)
6742 {
6743 ret = FAIL;
6744 }
6745 else
6746 {
6747 // Normal name. Only supports g:, w:, t: and b: namespaces.
6748 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006749 }
6750
Bram Moolenaar752fc692021-01-04 21:57:11 +01006751 *name_end = cc;
6752 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006753}
6754
6755/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02006756 * Callback passed to ex_unletlock().
6757 */
6758 static int
6759compile_lock_unlock(
6760 lval_T *lvp,
6761 char_u *name_end,
6762 exarg_T *eap,
6763 int deep UNUSED,
6764 void *coookie)
6765{
6766 cctx_T *cctx = coookie;
6767 int cc = *name_end;
6768 char_u *p = lvp->ll_name;
6769 int ret = OK;
6770 size_t len;
6771 char_u *buf;
6772
6773 if (cctx->ctx_skip == SKIP_YES)
6774 return OK;
6775
6776 // Cannot use :lockvar and :unlockvar on local variables.
6777 if (p[1] != ':')
6778 {
6779 char_u *end = skip_var_one(p, FALSE);
6780
6781 if (lookup_local(p, end - p, NULL, cctx) == OK)
6782 {
6783 emsg(_(e_cannot_lock_unlock_local_variable));
6784 return FAIL;
6785 }
6786 }
6787
6788 // Checking is done at runtime.
6789 *name_end = NUL;
6790 len = name_end - p + 20;
6791 buf = alloc(len);
6792 if (buf == NULL)
6793 ret = FAIL;
6794 else
6795 {
6796 vim_snprintf((char *)buf, len, "%s %s",
6797 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
6798 p);
6799 ret = generate_EXEC(cctx, buf);
6800
6801 vim_free(buf);
6802 *name_end = cc;
6803 }
6804 return ret;
6805}
6806
6807/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006808 * compile "unlet var", "lock var" and "unlock var"
6809 * "arg" points to "var".
6810 */
6811 static char_u *
6812compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6813{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02006814 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6815 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
6816 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006817 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6818}
6819
6820/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006821 * Compile an :import command.
6822 */
6823 static char_u *
6824compile_import(char_u *arg, cctx_T *cctx)
6825{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006826 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006827}
6828
6829/*
6830 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6831 */
6832 static int
6833compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6834{
6835 garray_T *instr = &cctx->ctx_instr;
6836 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6837
6838 if (endlabel == NULL)
6839 return FAIL;
6840 endlabel->el_next = *el;
6841 *el = endlabel;
6842 endlabel->el_end_label = instr->ga_len;
6843
6844 generate_JUMP(cctx, when, 0);
6845 return OK;
6846}
6847
6848 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006849compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006850{
6851 garray_T *instr = &cctx->ctx_instr;
6852
6853 while (*el != NULL)
6854 {
6855 endlabel_T *cur = (*el);
6856 isn_T *isn;
6857
6858 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006859 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006860 *el = cur->el_next;
6861 vim_free(cur);
6862 }
6863}
6864
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006865 static void
6866compile_free_jump_to_end(endlabel_T **el)
6867{
6868 while (*el != NULL)
6869 {
6870 endlabel_T *cur = (*el);
6871
6872 *el = cur->el_next;
6873 vim_free(cur);
6874 }
6875}
6876
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006877/*
6878 * Create a new scope and set up the generic items.
6879 */
6880 static scope_T *
6881new_scope(cctx_T *cctx, scopetype_T type)
6882{
6883 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6884
6885 if (scope == NULL)
6886 return NULL;
6887 scope->se_outer = cctx->ctx_scope;
6888 cctx->ctx_scope = scope;
6889 scope->se_type = type;
6890 scope->se_local_count = cctx->ctx_locals.ga_len;
6891 return scope;
6892}
6893
6894/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006895 * Free the current scope and go back to the outer scope.
6896 */
6897 static void
6898drop_scope(cctx_T *cctx)
6899{
6900 scope_T *scope = cctx->ctx_scope;
6901
6902 if (scope == NULL)
6903 {
6904 iemsg("calling drop_scope() without a scope");
6905 return;
6906 }
6907 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006908 switch (scope->se_type)
6909 {
6910 case IF_SCOPE:
6911 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6912 case FOR_SCOPE:
6913 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6914 case WHILE_SCOPE:
6915 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6916 case TRY_SCOPE:
6917 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6918 case NO_SCOPE:
6919 case BLOCK_SCOPE:
6920 break;
6921 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006922 vim_free(scope);
6923}
6924
6925/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006926 * compile "if expr"
6927 *
6928 * "if expr" Produces instructions:
6929 * EVAL expr Push result of "expr"
6930 * JUMP_IF_FALSE end
6931 * ... body ...
6932 * end:
6933 *
6934 * "if expr | else" Produces instructions:
6935 * EVAL expr Push result of "expr"
6936 * JUMP_IF_FALSE else
6937 * ... body ...
6938 * JUMP_ALWAYS end
6939 * else:
6940 * ... body ...
6941 * end:
6942 *
6943 * "if expr1 | elseif expr2 | else" Produces instructions:
6944 * EVAL expr Push result of "expr"
6945 * JUMP_IF_FALSE elseif
6946 * ... body ...
6947 * JUMP_ALWAYS end
6948 * elseif:
6949 * EVAL expr Push result of "expr"
6950 * JUMP_IF_FALSE else
6951 * ... body ...
6952 * JUMP_ALWAYS end
6953 * else:
6954 * ... body ...
6955 * end:
6956 */
6957 static char_u *
6958compile_if(char_u *arg, cctx_T *cctx)
6959{
6960 char_u *p = arg;
6961 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006962 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006963 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006964 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006965 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006966
Bram Moolenaara5565e42020-05-09 15:44:01 +02006967 CLEAR_FIELD(ppconst);
6968 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006969 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006970 clear_ppconst(&ppconst);
6971 return NULL;
6972 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01006973 if (!ends_excmd2(arg, skipwhite(p)))
6974 {
6975 semsg(_(e_trailing_arg), p);
6976 return NULL;
6977 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006978 if (cctx->ctx_skip == SKIP_YES)
6979 clear_ppconst(&ppconst);
6980 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006981 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006982 int error = FALSE;
6983 int v;
6984
Bram Moolenaara5565e42020-05-09 15:44:01 +02006985 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006986 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006987 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006988 if (error)
6989 return NULL;
6990 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006991 }
6992 else
6993 {
6994 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006995 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006996 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006997 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006998 if (bool_on_stack(cctx) == FAIL)
6999 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007000 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007001
Bram Moolenaara91a7132021-03-25 21:12:15 +01007002 // CMDMOD_REV must come before the jump
7003 generate_undo_cmdmods(cctx);
7004
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007005 scope = new_scope(cctx, IF_SCOPE);
7006 if (scope == NULL)
7007 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007008 scope->se_skip_save = skip_save;
7009 // "is_had_return" will be reset if any block does not end in :return
7010 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007011
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007012 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007013 {
7014 // "where" is set when ":elseif", "else" or ":endif" is found
7015 scope->se_u.se_if.is_if_label = instr->ga_len;
7016 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7017 }
7018 else
7019 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007020
Bram Moolenaarced68a02021-01-24 17:53:47 +01007021#ifdef FEAT_PROFILE
7022 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7023 && skip_save != SKIP_YES)
7024 {
7025 // generated a profile start, need to generate a profile end, since it
7026 // won't be done after returning
7027 cctx->ctx_skip = SKIP_NOT;
7028 generate_instr(cctx, ISN_PROF_END);
7029 cctx->ctx_skip = SKIP_YES;
7030 }
7031#endif
7032
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007033 return p;
7034}
7035
7036 static char_u *
7037compile_elseif(char_u *arg, cctx_T *cctx)
7038{
7039 char_u *p = arg;
7040 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007041 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007042 isn_T *isn;
7043 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007044 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007045 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007046
7047 if (scope == NULL || scope->se_type != IF_SCOPE)
7048 {
7049 emsg(_(e_elseif_without_if));
7050 return NULL;
7051 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007052 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007053 if (!cctx->ctx_had_return)
7054 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007055
Bram Moolenaarced68a02021-01-24 17:53:47 +01007056 if (cctx->ctx_skip == SKIP_NOT)
7057 {
7058 // previous block was executed, this one and following will not
7059 cctx->ctx_skip = SKIP_YES;
7060 scope->se_u.se_if.is_seen_skip_not = TRUE;
7061 }
7062 if (scope->se_u.se_if.is_seen_skip_not)
7063 {
7064 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007065 // Do not count the "elseif" for profiling and cmdmod
7066 instr->ga_len = current_instr_idx(cctx);
7067
Bram Moolenaarced68a02021-01-24 17:53:47 +01007068 skip_expr_cctx(&p, cctx);
7069 return p;
7070 }
7071
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007072 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007073 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007074 int moved_cmdmod = FALSE;
7075
7076 // Move any CMDMOD instruction to after the jump
7077 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7078 {
7079 if (ga_grow(instr, 1) == FAIL)
7080 return NULL;
7081 ((isn_T *)instr->ga_data)[instr->ga_len] =
7082 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7083 --instr->ga_len;
7084 moved_cmdmod = TRUE;
7085 }
7086
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007087 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007088 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007089 return NULL;
7090 // previous "if" or "elseif" jumps here
7091 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7092 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007093 if (moved_cmdmod)
7094 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007095 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007096
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007097 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007098 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007099 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007100 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007101 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007102#ifdef FEAT_PROFILE
7103 if (cctx->ctx_profiling)
7104 {
7105 // the previous block was skipped, need to profile this line
7106 generate_instr(cctx, ISN_PROF_START);
7107 instr_count = instr->ga_len;
7108 }
7109#endif
7110 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007111 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007112 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007113 clear_ppconst(&ppconst);
7114 return NULL;
7115 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007116 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007117 if (!ends_excmd2(arg, skipwhite(p)))
7118 {
7119 semsg(_(e_trailing_arg), p);
7120 return NULL;
7121 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007122 if (scope->se_skip_save == SKIP_YES)
7123 clear_ppconst(&ppconst);
7124 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007125 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007126 int error = FALSE;
7127 int v;
7128
Bram Moolenaar7f141552020-05-09 17:35:53 +02007129 // The expression results in a constant.
7130 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007131 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7132 if (error)
7133 return NULL;
7134 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007135 clear_ppconst(&ppconst);
7136 scope->se_u.se_if.is_if_label = -1;
7137 }
7138 else
7139 {
7140 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007141 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007142 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007143 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007144 if (bool_on_stack(cctx) == FAIL)
7145 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007146
Bram Moolenaara91a7132021-03-25 21:12:15 +01007147 // CMDMOD_REV must come before the jump
7148 generate_undo_cmdmods(cctx);
7149
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007150 // "where" is set when ":elseif", "else" or ":endif" is found
7151 scope->se_u.se_if.is_if_label = instr->ga_len;
7152 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7153 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007154
7155 return p;
7156}
7157
7158 static char_u *
7159compile_else(char_u *arg, cctx_T *cctx)
7160{
7161 char_u *p = arg;
7162 garray_T *instr = &cctx->ctx_instr;
7163 isn_T *isn;
7164 scope_T *scope = cctx->ctx_scope;
7165
7166 if (scope == NULL || scope->se_type != IF_SCOPE)
7167 {
7168 emsg(_(e_else_without_if));
7169 return NULL;
7170 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007171 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007172 if (!cctx->ctx_had_return)
7173 scope->se_u.se_if.is_had_return = FALSE;
7174 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007175
Bram Moolenaarced68a02021-01-24 17:53:47 +01007176#ifdef FEAT_PROFILE
7177 if (cctx->ctx_profiling)
7178 {
7179 if (cctx->ctx_skip == SKIP_NOT
7180 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7181 .isn_type == ISN_PROF_START)
7182 // the previous block was executed, do not count "else" for profiling
7183 --instr->ga_len;
7184 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7185 {
7186 // the previous block was not executed, this one will, do count the
7187 // "else" for profiling
7188 cctx->ctx_skip = SKIP_NOT;
7189 generate_instr(cctx, ISN_PROF_END);
7190 generate_instr(cctx, ISN_PROF_START);
7191 cctx->ctx_skip = SKIP_YES;
7192 }
7193 }
7194#endif
7195
7196 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007197 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007198 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007199 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007200 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007201 if (!cctx->ctx_had_return
7202 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7203 JUMP_ALWAYS, cctx) == FAIL)
7204 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007205 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007206
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007207 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007208 {
7209 if (scope->se_u.se_if.is_if_label >= 0)
7210 {
7211 // previous "if" or "elseif" jumps here
7212 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7213 isn->isn_arg.jump.jump_where = instr->ga_len;
7214 scope->se_u.se_if.is_if_label = -1;
7215 }
7216 }
7217
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007218 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007219 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7220 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007221
7222 return p;
7223}
7224
7225 static char_u *
7226compile_endif(char_u *arg, cctx_T *cctx)
7227{
7228 scope_T *scope = cctx->ctx_scope;
7229 ifscope_T *ifscope;
7230 garray_T *instr = &cctx->ctx_instr;
7231 isn_T *isn;
7232
Bram Moolenaarfa984412021-03-25 22:15:28 +01007233 if (misplaced_cmdmod(cctx))
7234 return NULL;
7235
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007236 if (scope == NULL || scope->se_type != IF_SCOPE)
7237 {
7238 emsg(_(e_endif_without_if));
7239 return NULL;
7240 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007241 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007242 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007243 if (!cctx->ctx_had_return)
7244 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007245
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007246 if (scope->se_u.se_if.is_if_label >= 0)
7247 {
7248 // previous "if" or "elseif" jumps here
7249 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7250 isn->isn_arg.jump.jump_where = instr->ga_len;
7251 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007252 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007253 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007254
7255#ifdef FEAT_PROFILE
7256 // even when skipping we count the endif as executed, unless the block it's
7257 // in is skipped
7258 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7259 && scope->se_skip_save != SKIP_YES)
7260 {
7261 cctx->ctx_skip = SKIP_NOT;
7262 generate_instr(cctx, ISN_PROF_START);
7263 }
7264#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007265 cctx->ctx_skip = scope->se_skip_save;
7266
7267 // If all the blocks end in :return and there is an :else then the
7268 // had_return flag is set.
7269 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007270
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007271 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007272 return arg;
7273}
7274
7275/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007276 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007277 *
7278 * Produces instructions:
7279 * PUSHNR -1
7280 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007281 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007282 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7283 * - if beyond end, jump to "end"
7284 * - otherwise get item from list and push it
7285 * STORE var Store item in "var"
7286 * ... body ...
7287 * JUMP top Jump back to repeat
7288 * end: DROP Drop the result of "expr"
7289 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007290 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7291 * UNPACK 2 Split item in 2
7292 * STORE var1 Store item in "var1"
7293 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007294 */
7295 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007296compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007297{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007298 char_u *arg;
7299 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007300 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007301 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007302 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007303 int var_count = 0;
7304 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007305 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007306 garray_T *stack = &cctx->ctx_type_stack;
7307 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007308 lvar_T *loop_lvar; // loop iteration variable
7309 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007310 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007311 type_T *item_type = &t_any;
7312 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007313
Bram Moolenaar792f7862020-11-23 08:31:18 +01007314 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007315 if (p == NULL)
7316 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007317 if (var_count == 0)
7318 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007319
7320 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007321 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007322 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7323 return NULL;
7324 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007325 {
7326 emsg(_(e_missing_in));
7327 return NULL;
7328 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007329 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007330 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7331 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007332
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007333 scope = new_scope(cctx, FOR_SCOPE);
7334 if (scope == NULL)
7335 return NULL;
7336
Bram Moolenaar792f7862020-11-23 08:31:18 +01007337 // Reserve a variable to store the loop iteration counter and initialize it
7338 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007339 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7340 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007341 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007342 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007343 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007344 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007345 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007346 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007347
7348 // compile "expr", it remains on the stack until "endfor"
7349 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007350 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007351 {
7352 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007353 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007354 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007355 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007356
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007357 // If we know the type of "var" and it is a not a list or string we can
7358 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007359 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007360 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
7361 && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007362 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007363 // TODO: support Blob
7364 semsg(_(e_for_loop_on_str_not_supported),
7365 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007366 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007367 return NULL;
7368 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007369
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007370 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007371 {
7372 if (var_count == 1)
7373 item_type = vartype->tt_member;
7374 else if (vartype->tt_member->tt_type == VAR_LIST
7375 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
7376 item_type = vartype->tt_member->tt_member;
7377 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007378
Bram Moolenaara91a7132021-03-25 21:12:15 +01007379 // CMDMOD_REV must come before the FOR instruction
7380 generate_undo_cmdmods(cctx);
7381
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007382 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007383 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007384 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007385
Bram Moolenaar792f7862020-11-23 08:31:18 +01007386 arg = arg_start;
7387 if (var_count > 1)
7388 {
7389 generate_UNPACK(cctx, var_count, semicolon);
7390 arg = skipwhite(arg + 1); // skip white after '['
7391
7392 // the list item is replaced by a number of items
7393 if (ga_grow(stack, var_count - 1) == FAIL)
7394 {
7395 drop_scope(cctx);
7396 return NULL;
7397 }
7398 --stack->ga_len;
7399 for (idx = 0; idx < var_count; ++idx)
7400 {
7401 ((type_T **)stack->ga_data)[stack->ga_len] =
7402 (semicolon && idx == 0) ? vartype : item_type;
7403 ++stack->ga_len;
7404 }
7405 }
7406
7407 for (idx = 0; idx < var_count; ++idx)
7408 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007409 assign_dest_T dest = dest_local;
7410 int opt_flags = 0;
7411 int vimvaridx = -1;
7412 type_T *type = &t_any;
7413
7414 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007415 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007416 name = vim_strnsave(arg, varlen);
7417 if (name == NULL)
7418 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007419
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007420 // TODO: script var not supported?
7421 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7422 &vimvaridx, &type, cctx) == FAIL)
7423 goto failed;
7424 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007425 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007426 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7427 0, 0, type, name) == FAIL)
7428 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007429 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007430 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007431 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007432 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007433 {
7434 semsg(_(e_variable_already_declared), arg);
7435 goto failed;
7436 }
7437
Bram Moolenaarea870692020-12-02 14:24:30 +01007438 if (STRNCMP(name, "s:", 2) == 0)
7439 {
7440 semsg(_(e_cannot_declare_script_variable_in_function), name);
7441 goto failed;
7442 }
7443
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007444 // Reserve a variable to store "var".
7445 // TODO: check for type
7446 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
7447 if (var_lvar == NULL)
7448 // out of memory or used as an argument
7449 goto failed;
7450
7451 if (semicolon && idx == var_count - 1)
7452 var_lvar->lv_type = vartype;
7453 else
7454 var_lvar->lv_type = item_type;
7455 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7456 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007457
Bram Moolenaar036d0712021-01-17 20:23:38 +01007458 if (*p == ':')
7459 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007460 if (*p == ',' || *p == ';')
7461 ++p;
7462 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007463 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007464 }
7465
7466 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007467
7468failed:
7469 vim_free(name);
7470 drop_scope(cctx);
7471 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007472}
7473
7474/*
7475 * compile "endfor"
7476 */
7477 static char_u *
7478compile_endfor(char_u *arg, cctx_T *cctx)
7479{
7480 garray_T *instr = &cctx->ctx_instr;
7481 scope_T *scope = cctx->ctx_scope;
7482 forscope_T *forscope;
7483 isn_T *isn;
7484
Bram Moolenaarfa984412021-03-25 22:15:28 +01007485 if (misplaced_cmdmod(cctx))
7486 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007487
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007488 if (scope == NULL || scope->se_type != FOR_SCOPE)
7489 {
7490 emsg(_(e_for));
7491 return NULL;
7492 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007493 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007494 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007495 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007496
7497 // At end of ":for" scope jump back to the FOR instruction.
7498 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7499
7500 // Fill in the "end" label in the FOR statement so it can jump here
7501 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7502 isn->isn_arg.forloop.for_end = instr->ga_len;
7503
7504 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007505 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007506
7507 // Below the ":for" scope drop the "expr" list from the stack.
7508 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7509 return NULL;
7510
7511 vim_free(scope);
7512
7513 return arg;
7514}
7515
7516/*
7517 * compile "while expr"
7518 *
7519 * Produces instructions:
7520 * top: EVAL expr Push result of "expr"
7521 * JUMP_IF_FALSE end jump if false
7522 * ... body ...
7523 * JUMP top Jump back to repeat
7524 * end:
7525 *
7526 */
7527 static char_u *
7528compile_while(char_u *arg, cctx_T *cctx)
7529{
7530 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007531 scope_T *scope;
7532
7533 scope = new_scope(cctx, WHILE_SCOPE);
7534 if (scope == NULL)
7535 return NULL;
7536
Bram Moolenaara91a7132021-03-25 21:12:15 +01007537 // "endwhile" jumps back here, one before when profiling or using cmdmods
7538 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007539
7540 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007541 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007542 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007543 if (!ends_excmd2(arg, skipwhite(p)))
7544 {
7545 semsg(_(e_trailing_arg), p);
7546 return NULL;
7547 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007548
Bram Moolenaar13106602020-10-04 16:06:05 +02007549 if (bool_on_stack(cctx) == FAIL)
7550 return FAIL;
7551
Bram Moolenaara91a7132021-03-25 21:12:15 +01007552 // CMDMOD_REV must come before the jump
7553 generate_undo_cmdmods(cctx);
7554
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007555 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007556 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007557 JUMP_IF_FALSE, cctx) == FAIL)
7558 return FAIL;
7559
7560 return p;
7561}
7562
7563/*
7564 * compile "endwhile"
7565 */
7566 static char_u *
7567compile_endwhile(char_u *arg, cctx_T *cctx)
7568{
7569 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007570 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007571
Bram Moolenaarfa984412021-03-25 22:15:28 +01007572 if (misplaced_cmdmod(cctx))
7573 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007574 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7575 {
7576 emsg(_(e_while));
7577 return NULL;
7578 }
7579 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007580 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007581
Bram Moolenaarf002a412021-01-24 13:34:18 +01007582#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007583 // count the endwhile before jumping
7584 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007585#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007586
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007587 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007588 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007589
7590 // Fill in the "end" label in the WHILE statement so it can jump here.
7591 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007592 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7593 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007594
7595 vim_free(scope);
7596
7597 return arg;
7598}
7599
7600/*
7601 * compile "continue"
7602 */
7603 static char_u *
7604compile_continue(char_u *arg, cctx_T *cctx)
7605{
7606 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007607 int try_scopes = 0;
7608 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007609
7610 for (;;)
7611 {
7612 if (scope == NULL)
7613 {
7614 emsg(_(e_continue));
7615 return NULL;
7616 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01007617 if (scope->se_type == FOR_SCOPE)
7618 {
7619 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007620 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007621 }
7622 if (scope->se_type == WHILE_SCOPE)
7623 {
7624 loop_label = scope->se_u.se_while.ws_top_label;
7625 break;
7626 }
7627 if (scope->se_type == TRY_SCOPE)
7628 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007629 scope = scope->se_outer;
7630 }
7631
Bram Moolenaarc150c092021-02-13 15:02:46 +01007632 if (try_scopes > 0)
7633 // Inside one or more try/catch blocks we first need to jump to the
7634 // "finally" or "endtry" to cleanup.
7635 generate_TRYCONT(cctx, try_scopes, loop_label);
7636 else
7637 // Jump back to the FOR or WHILE instruction.
7638 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
7639
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007640 return arg;
7641}
7642
7643/*
7644 * compile "break"
7645 */
7646 static char_u *
7647compile_break(char_u *arg, cctx_T *cctx)
7648{
7649 scope_T *scope = cctx->ctx_scope;
7650 endlabel_T **el;
7651
7652 for (;;)
7653 {
7654 if (scope == NULL)
7655 {
7656 emsg(_(e_break));
7657 return NULL;
7658 }
7659 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7660 break;
7661 scope = scope->se_outer;
7662 }
7663
7664 // Jump to the end of the FOR or WHILE loop.
7665 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007666 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007667 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007668 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007669 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7670 return FAIL;
7671
7672 return arg;
7673}
7674
7675/*
7676 * compile "{" start of block
7677 */
7678 static char_u *
7679compile_block(char_u *arg, cctx_T *cctx)
7680{
7681 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7682 return NULL;
7683 return skipwhite(arg + 1);
7684}
7685
7686/*
7687 * compile end of block: drop one scope
7688 */
7689 static void
7690compile_endblock(cctx_T *cctx)
7691{
7692 scope_T *scope = cctx->ctx_scope;
7693
7694 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007695 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007696 vim_free(scope);
7697}
7698
7699/*
7700 * compile "try"
7701 * Creates a new scope for the try-endtry, pointing to the first catch and
7702 * finally.
7703 * Creates another scope for the "try" block itself.
7704 * TRY instruction sets up exception handling at runtime.
7705 *
7706 * "try"
7707 * TRY -> catch1, -> finally push trystack entry
7708 * ... try block
7709 * "throw {exception}"
7710 * EVAL {exception}
7711 * THROW create exception
7712 * ... try block
7713 * " catch {expr}"
7714 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007715 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007716 * EVAL {expr}
7717 * MATCH
7718 * JUMP nomatch -> catch2
7719 * CATCH remove exception
7720 * ... catch block
7721 * " catch"
7722 * JUMP -> finally
7723 * catch2: CATCH remove exception
7724 * ... catch block
7725 * " finally"
7726 * finally:
7727 * ... finally block
7728 * " endtry"
7729 * ENDTRY pop trystack entry, may rethrow
7730 */
7731 static char_u *
7732compile_try(char_u *arg, cctx_T *cctx)
7733{
7734 garray_T *instr = &cctx->ctx_instr;
7735 scope_T *try_scope;
7736 scope_T *scope;
7737
Bram Moolenaarfa984412021-03-25 22:15:28 +01007738 if (misplaced_cmdmod(cctx))
7739 return NULL;
7740
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007741 // scope that holds the jumps that go to catch/finally/endtry
7742 try_scope = new_scope(cctx, TRY_SCOPE);
7743 if (try_scope == NULL)
7744 return NULL;
7745
Bram Moolenaar69f70502021-01-01 16:10:46 +01007746 if (cctx->ctx_skip != SKIP_YES)
7747 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007748 isn_T *isn;
7749
7750 // "try_catch" is set when the first ":catch" is found or when no catch
7751 // is found and ":finally" is found.
7752 // "try_finally" is set when ":finally" is found
7753 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01007754 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007755 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
7756 return NULL;
7757 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
7758 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007759 return NULL;
7760 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007761
7762 // scope for the try block itself
7763 scope = new_scope(cctx, BLOCK_SCOPE);
7764 if (scope == NULL)
7765 return NULL;
7766
7767 return arg;
7768}
7769
7770/*
7771 * compile "catch {expr}"
7772 */
7773 static char_u *
7774compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7775{
7776 scope_T *scope = cctx->ctx_scope;
7777 garray_T *instr = &cctx->ctx_instr;
7778 char_u *p;
7779 isn_T *isn;
7780
Bram Moolenaarfa984412021-03-25 22:15:28 +01007781 if (misplaced_cmdmod(cctx))
7782 return NULL;
7783
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007784 // end block scope from :try or :catch
7785 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7786 compile_endblock(cctx);
7787 scope = cctx->ctx_scope;
7788
7789 // Error if not in a :try scope
7790 if (scope == NULL || scope->se_type != TRY_SCOPE)
7791 {
7792 emsg(_(e_catch));
7793 return NULL;
7794 }
7795
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007796 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007797 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007798 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007799 return NULL;
7800 }
7801
Bram Moolenaar69f70502021-01-01 16:10:46 +01007802 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007803 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007804#ifdef FEAT_PROFILE
7805 // the profile-start should be after the jump
7806 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7807 .isn_type == ISN_PROF_START)
7808 --instr->ga_len;
7809#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01007810 // Jump from end of previous block to :finally or :endtry
7811 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7812 JUMP_ALWAYS, cctx) == FAIL)
7813 return NULL;
7814
7815 // End :try or :catch scope: set value in ISN_TRY instruction
7816 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007817 if (isn->isn_arg.try.try_ref->try_catch == 0)
7818 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007819 if (scope->se_u.se_try.ts_catch_label != 0)
7820 {
7821 // Previous catch without match jumps here
7822 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7823 isn->isn_arg.jump.jump_where = instr->ga_len;
7824 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007825#ifdef FEAT_PROFILE
7826 if (cctx->ctx_profiling)
7827 {
7828 // a "throw" that jumps here needs to be counted
7829 generate_instr(cctx, ISN_PROF_END);
7830 // the "catch" is also counted
7831 generate_instr(cctx, ISN_PROF_START);
7832 }
7833#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007834 }
7835
7836 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007837 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007838 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007839 scope->se_u.se_try.ts_caught_all = TRUE;
7840 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007841 }
7842 else
7843 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007844 char_u *end;
7845 char_u *pat;
7846 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007847 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007848 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007849
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007850 // Push v:exception, push {expr} and MATCH
7851 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7852
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007853 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007854 if (*end != *p)
7855 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007856 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007857 vim_free(tofree);
7858 return FAIL;
7859 }
7860 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007861 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007862 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007863 len = (int)(end - tofree);
7864 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007865 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007866 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007867 if (pat == NULL)
7868 return FAIL;
7869 if (generate_PUSHS(cctx, pat) == FAIL)
7870 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007871
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007872 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7873 return NULL;
7874
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007875 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007876 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7877 return NULL;
7878 }
7879
Bram Moolenaar69f70502021-01-01 16:10:46 +01007880 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007881 return NULL;
7882
7883 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7884 return NULL;
7885 return p;
7886}
7887
7888 static char_u *
7889compile_finally(char_u *arg, cctx_T *cctx)
7890{
7891 scope_T *scope = cctx->ctx_scope;
7892 garray_T *instr = &cctx->ctx_instr;
7893 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007894 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007895
Bram Moolenaarfa984412021-03-25 22:15:28 +01007896 if (misplaced_cmdmod(cctx))
7897 return NULL;
7898
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007899 // end block scope from :try or :catch
7900 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7901 compile_endblock(cctx);
7902 scope = cctx->ctx_scope;
7903
7904 // Error if not in a :try scope
7905 if (scope == NULL || scope->se_type != TRY_SCOPE)
7906 {
7907 emsg(_(e_finally));
7908 return NULL;
7909 }
7910
7911 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007912 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007913 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007914 {
7915 emsg(_(e_finally_dup));
7916 return NULL;
7917 }
7918
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007919 this_instr = instr->ga_len;
7920#ifdef FEAT_PROFILE
7921 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7922 .isn_type == ISN_PROF_START)
7923 // jump to the profile start of the "finally"
7924 --this_instr;
7925#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007926
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007927 // Fill in the "end" label in jumps at the end of the blocks.
7928 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
7929 this_instr, cctx);
7930
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007931 // If there is no :catch then an exception jumps to :finally.
7932 if (isn->isn_arg.try.try_ref->try_catch == 0)
7933 isn->isn_arg.try.try_ref->try_catch = this_instr;
7934 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007935 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007936 {
7937 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007938 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007939 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02007940 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007941 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007942 if (generate_instr(cctx, ISN_FINALLY) == NULL)
7943 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007944
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007945 // TODO: set index in ts_finally_label jumps
7946
7947 return arg;
7948}
7949
7950 static char_u *
7951compile_endtry(char_u *arg, cctx_T *cctx)
7952{
7953 scope_T *scope = cctx->ctx_scope;
7954 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007955 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007956
Bram Moolenaarfa984412021-03-25 22:15:28 +01007957 if (misplaced_cmdmod(cctx))
7958 return NULL;
7959
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007960 // end block scope from :catch or :finally
7961 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7962 compile_endblock(cctx);
7963 scope = cctx->ctx_scope;
7964
7965 // Error if not in a :try scope
7966 if (scope == NULL || scope->se_type != TRY_SCOPE)
7967 {
7968 if (scope == NULL)
7969 emsg(_(e_no_endtry));
7970 else if (scope->se_type == WHILE_SCOPE)
7971 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007972 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007973 emsg(_(e_endfor));
7974 else
7975 emsg(_(e_endif));
7976 return NULL;
7977 }
7978
Bram Moolenaarc150c092021-02-13 15:02:46 +01007979 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007980 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007981 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007982 if (try_isn->isn_arg.try.try_ref->try_catch == 0
7983 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007984 {
7985 emsg(_(e_missing_catch_or_finally));
7986 return NULL;
7987 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007988
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007989#ifdef FEAT_PROFILE
7990 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7991 .isn_type == ISN_PROF_START)
7992 // move the profile start after "endtry" so that it's not counted when
7993 // the exception is rethrown.
7994 --instr->ga_len;
7995#endif
7996
Bram Moolenaar69f70502021-01-01 16:10:46 +01007997 // Fill in the "end" label in jumps at the end of the blocks, if not
7998 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007999 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8000 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008001
Bram Moolenaar69f70502021-01-01 16:10:46 +01008002 if (scope->se_u.se_try.ts_catch_label != 0)
8003 {
8004 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008005 isn_T *isn = ((isn_T *)instr->ga_data)
8006 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008007 isn->isn_arg.jump.jump_where = instr->ga_len;
8008 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008009 }
8010
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008011 compile_endblock(cctx);
8012
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008013 if (cctx->ctx_skip != SKIP_YES)
8014 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008015 // End :catch or :finally scope: set instruction index in ISN_TRY
8016 // instruction
8017 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008018 if (cctx->ctx_skip != SKIP_YES
8019 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8020 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008021#ifdef FEAT_PROFILE
8022 if (cctx->ctx_profiling)
8023 generate_instr(cctx, ISN_PROF_START);
8024#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008025 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008026 return arg;
8027}
8028
8029/*
8030 * compile "throw {expr}"
8031 */
8032 static char_u *
8033compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8034{
8035 char_u *p = skipwhite(arg);
8036
Bram Moolenaara5565e42020-05-09 15:44:01 +02008037 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008038 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008039 if (cctx->ctx_skip == SKIP_YES)
8040 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008041 if (may_generate_2STRING(-1, cctx) == FAIL)
8042 return NULL;
8043 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8044 return NULL;
8045
8046 return p;
8047}
8048
8049/*
8050 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008051 * compile "echomsg expr"
8052 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008053 * compile "execute expr"
8054 */
8055 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008056compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008057{
8058 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008059 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008060 int count = 0;
8061
8062 for (;;)
8063 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008064 if (ends_excmd2(prev, p))
8065 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008066 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008067 return NULL;
8068 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008069 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008070 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008071 }
8072
Bram Moolenaare4984292020-12-13 14:19:25 +01008073 if (count > 0)
8074 {
8075 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8076 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8077 else if (cmdidx == CMD_execute)
8078 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8079 else if (cmdidx == CMD_echomsg)
8080 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8081 else
8082 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
8083 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008084 return p;
8085}
8086
8087/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008088 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008089 * instruction to compute it and return OK.
8090 * Otherwise return FAIL, the caller must deal with any range.
8091 */
8092 static int
8093compile_variable_range(exarg_T *eap, cctx_T *cctx)
8094{
8095 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8096 char_u *p = skipdigits(eap->cmd);
8097
8098 if (p == range_end)
8099 return FAIL;
8100 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8101}
8102
8103/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008104 * :put r
8105 * :put ={expr}
8106 */
8107 static char_u *
8108compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8109{
8110 char_u *line = arg;
8111 linenr_T lnum;
8112 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008113 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008114
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008115 eap->regname = *line;
8116
8117 if (eap->regname == '=')
8118 {
8119 char_u *p = line + 1;
8120
8121 if (compile_expr0(&p, cctx) == FAIL)
8122 return NULL;
8123 line = p;
8124 }
8125 else if (eap->regname != NUL)
8126 ++line;
8127
Bram Moolenaar08597872020-12-10 19:43:40 +01008128 if (compile_variable_range(eap, cctx) == OK)
8129 {
8130 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8131 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008132 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008133 {
8134 // Either no range or a number.
8135 // "errormsg" will not be set because the range is ADDR_LINES.
8136 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008137 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008138 return NULL;
8139 if (eap->addr_count == 0)
8140 lnum = -1;
8141 else
8142 lnum = eap->line2;
8143 if (above)
8144 --lnum;
8145 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008146
8147 generate_PUT(cctx, eap->regname, lnum);
8148 return line;
8149}
8150
8151/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008152 * A command that is not compiled, execute with legacy code.
8153 */
8154 static char_u *
8155compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8156{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008157 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008158 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008159 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008160
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008161 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008162 goto theend;
8163
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008164 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008165 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008166 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008167 int usefilter = FALSE;
8168
8169 has_expr = argt & (EX_XFILE | EX_EXPAND);
8170
8171 // If the command can be followed by a bar, find the bar and truncate
8172 // it, so that the following command can be compiled.
8173 // The '|' is overwritten with a NUL, it is put back below.
8174 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8175 && *eap->arg == '!')
8176 // :w !filter or :r !filter or :r! filter
8177 usefilter = TRUE;
8178 if ((argt & EX_TRLBAR) && !usefilter)
8179 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008180 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008181 separate_nextcmd(eap);
8182 if (eap->nextcmd != NULL)
8183 nextcmd = eap->nextcmd;
8184 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008185 else if (eap->cmdidx == CMD_wincmd)
8186 {
8187 p = eap->arg;
8188 if (*p != NUL)
8189 ++p;
8190 if (*p == 'g' || *p == Ctrl_G)
8191 ++p;
8192 p = skipwhite(p);
8193 if (*p == '|')
8194 {
8195 *p = NUL;
8196 nextcmd = p + 1;
8197 }
8198 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008199 }
8200
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008201 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8202 {
8203 // expand filename in "syntax include [@group] filename"
8204 has_expr = TRUE;
8205 eap->arg = skipwhite(eap->arg + 7);
8206 if (*eap->arg == '@')
8207 eap->arg = skiptowhite(eap->arg);
8208 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008209
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008210 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8211 && STRLEN(eap->arg) > 4)
8212 {
8213 int delim = *eap->arg;
8214
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008215 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008216 if (*p == delim)
8217 {
8218 eap->arg = p + 1;
8219 has_expr = TRUE;
8220 }
8221 }
8222
Bram Moolenaarecac5912021-01-05 19:23:28 +01008223 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8224 {
8225 // TODO: should only expand when appropriate for the command
8226 eap->arg = skiptowhite(eap->arg);
8227 has_expr = TRUE;
8228 }
8229
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008230 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008231 {
8232 int count = 0;
8233 char_u *start = skipwhite(line);
8234
8235 // :cmd xxx`=expr1`yyy`=expr2`zzz
8236 // PUSHS ":cmd xxx"
8237 // eval expr1
8238 // PUSHS "yyy"
8239 // eval expr2
8240 // PUSHS "zzz"
8241 // EXECCONCAT 5
8242 for (;;)
8243 {
8244 if (p > start)
8245 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008246 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008247 ++count;
8248 }
8249 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008250 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008251 return NULL;
8252 may_generate_2STRING(-1, cctx);
8253 ++count;
8254 p = skipwhite(p);
8255 if (*p != '`')
8256 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008257 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008258 return NULL;
8259 }
8260 start = p + 1;
8261
8262 p = (char_u *)strstr((char *)start, "`=");
8263 if (p == NULL)
8264 {
8265 if (*skipwhite(start) != NUL)
8266 {
8267 generate_PUSHS(cctx, vim_strsave(start));
8268 ++count;
8269 }
8270 break;
8271 }
8272 }
8273 generate_EXECCONCAT(cctx, count);
8274 }
8275 else
8276 generate_EXEC(cctx, line);
8277
8278theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008279 if (*nextcmd != NUL)
8280 {
8281 // the parser expects a pointer to the bar, put it back
8282 --nextcmd;
8283 *nextcmd = '|';
8284 }
8285
8286 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008287}
8288
8289/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008290 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008291 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008292 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008293 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008294add_def_function(ufunc_T *ufunc)
8295{
8296 dfunc_T *dfunc;
8297
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008298 if (def_functions.ga_len == 0)
8299 {
8300 // The first position is not used, so that a zero uf_dfunc_idx means it
8301 // wasn't set.
8302 if (ga_grow(&def_functions, 1) == FAIL)
8303 return FAIL;
8304 ++def_functions.ga_len;
8305 }
8306
Bram Moolenaar09689a02020-05-09 22:50:08 +02008307 // Add the function to "def_functions".
8308 if (ga_grow(&def_functions, 1) == FAIL)
8309 return FAIL;
8310 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8311 CLEAR_POINTER(dfunc);
8312 dfunc->df_idx = def_functions.ga_len;
8313 ufunc->uf_dfunc_idx = dfunc->df_idx;
8314 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008315 dfunc->df_name = vim_strsave(ufunc->uf_name);
8316 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008317 ++def_functions.ga_len;
8318 return OK;
8319}
8320
8321/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008322 * After ex_function() has collected all the function lines: parse and compile
8323 * the lines into instructions.
8324 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008325 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8326 * the return statement (used for lambda). When uf_ret_type is already set
8327 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008328 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008329 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008330 * This can be used recursively through compile_lambda(), which may reallocate
8331 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008332 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008333 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008334 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008335compile_def_function(
8336 ufunc_T *ufunc,
8337 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008338 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008339 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008340{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008341 char_u *line = NULL;
8342 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008343 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008344 cctx_T cctx;
8345 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008346 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008347 int ret = FAIL;
8348 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008349 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008350 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008351 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008352#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008353 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008354#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008355
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008356 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008357 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008358 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008359 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008360 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8361 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008362 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008363 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008364 else
8365 {
8366 if (add_def_function(ufunc) == FAIL)
8367 return FAIL;
8368 new_def_function = TRUE;
8369 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008370
Bram Moolenaar985116a2020-07-12 17:31:09 +02008371 ufunc->uf_def_status = UF_COMPILING;
8372
Bram Moolenaara80faa82020-04-12 19:37:17 +02008373 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008374
Bram Moolenaarf002a412021-01-24 13:34:18 +01008375#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008376 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008377#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008378 cctx.ctx_ufunc = ufunc;
8379 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008380 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008381 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8382 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8383 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8384 cctx.ctx_type_list = &ufunc->uf_type_list;
8385 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8386 instr = &cctx.ctx_instr;
8387
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008388 // Set the context to the function, it may be compiled when called from
8389 // another script. Set the script version to the most modern one.
8390 // The line number will be set in next_line_from_context().
8391 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008392 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8393
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008394 // Make sure error messages are OK.
8395 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8396 if (do_estack_push)
8397 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008398 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008399
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008400 if (ufunc->uf_def_args.ga_len > 0)
8401 {
8402 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008403 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008404 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008405 int i;
8406 char_u *arg;
8407 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008408 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008409
8410 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01008411 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008412 for (i = 0; i < count; ++i)
8413 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008414 garray_T *stack = &cctx.ctx_type_stack;
8415 type_T *val_type;
8416 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008417 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008418 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008419 int jump_instr_idx = instr->ga_len;
8420 isn_T *isn;
8421
8422 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
8423 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
8424 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008425
8426 // Make sure later arguments are not found.
8427 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008428
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008429 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01008430 r = compile_expr0(&arg, &cctx);
8431
8432 ufunc->uf_args.ga_len = uf_args_len;
8433 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008434 goto erret;
8435
8436 // If no type specified use the type of the default value.
8437 // Otherwise check that the default value type matches the
8438 // specified type.
8439 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008440 where.wt_index = arg_idx + 1;
8441 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008442 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008443 {
8444 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008445 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008446 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02008447 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008448 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008449 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008450
8451 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008452 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008453
8454 // set instruction index in JUMP_IF_ARG_SET to here
8455 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
8456 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008457 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008458
8459 if (did_set_arg_type)
8460 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008461 }
8462
8463 /*
8464 * Loop over all the lines of the function and generate instructions.
8465 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008466 for (;;)
8467 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008468 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008469 int starts_with_colon = FALSE;
8470 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02008471 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008472
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008473 // Bail out on the first error to avoid a flood of errors and report
8474 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01008475 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008476 goto erret;
8477
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008478 if (line != NULL && *line == '|')
8479 // the line continues after a '|'
8480 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02008481 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02008482 && !(*line == '#' && (line == cctx.ctx_line_start
8483 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008484 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02008485 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008486 goto erret;
8487 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01008488 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
8489 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008490 else
8491 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02008492 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02008493 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008494 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008495 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01008496#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008497 if (cctx.ctx_skip != SKIP_YES)
8498 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008499#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008500 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01008501 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008502 }
8503
Bram Moolenaara80faa82020-04-12 19:37:17 +02008504 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008505 ea.cmdlinep = &line;
8506 ea.cmd = skipwhite(line);
8507
Bram Moolenaarb2049902021-01-24 12:53:53 +01008508 if (*ea.cmd == '#')
8509 {
8510 // "#" starts a comment
8511 line = (char_u *)"";
8512 continue;
8513 }
8514
Bram Moolenaarf002a412021-01-24 13:34:18 +01008515#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008516 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
8517 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008518 {
8519 may_generate_prof_end(&cctx, prof_lnum);
8520
8521 prof_lnum = cctx.ctx_lnum;
8522 generate_instr(&cctx, ISN_PROF_START);
8523 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01008524#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008525
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008526 // Some things can be recognized by the first character.
8527 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008528 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008529 case '}':
8530 {
8531 // "}" ends a block scope
8532 scopetype_T stype = cctx.ctx_scope == NULL
8533 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008534
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008535 if (stype == BLOCK_SCOPE)
8536 {
8537 compile_endblock(&cctx);
8538 line = ea.cmd;
8539 }
8540 else
8541 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008542 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008543 goto erret;
8544 }
8545 if (line != NULL)
8546 line = skipwhite(ea.cmd + 1);
8547 continue;
8548 }
8549
8550 case '{':
8551 // "{" starts a block scope
8552 // "{'a': 1}->func() is something else
8553 if (ends_excmd(*skipwhite(ea.cmd + 1)))
8554 {
8555 line = compile_block(ea.cmd, &cctx);
8556 continue;
8557 }
8558 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008559 }
8560
8561 /*
8562 * COMMAND MODIFIERS
8563 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02008564 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02008565 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
8566 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008567 {
8568 if (errormsg != NULL)
8569 goto erret;
8570 // empty line or comment
8571 line = (char_u *)"";
8572 continue;
8573 }
Bram Moolenaare1004402020-10-24 20:49:43 +02008574 generate_cmdmods(&cctx, &local_cmdmod);
8575 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008576
Bram Moolenaare88c8e82020-11-01 17:03:37 +01008577 // Check if there was a colon after the last command modifier or before
8578 // the current position.
8579 for (p = ea.cmd; p >= line; --p)
8580 {
8581 if (*p == ':')
8582 starts_with_colon = TRUE;
8583 if (p < ea.cmd && !VIM_ISWHITE(*p))
8584 break;
8585 }
8586
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008587 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008588 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008589 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008590 {
8591 if (*ea.cmd == '(')
8592 // not for "call()"
8593 ea.cmd = p;
8594 else
8595 ea.cmd = skipwhite(ea.cmd);
8596 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008597
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008598 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008599 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008600 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008601
Bram Moolenaar17126b12021-01-07 22:03:02 +01008602 // Check for assignment after command modifiers.
8603 assign = may_compile_assignment(&ea, &line, &cctx);
8604 if (assign == OK)
8605 goto nextline;
8606 if (assign == FAIL)
8607 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008608 }
8609
8610 /*
8611 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008612 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008613 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008614 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008615 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008616 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008617 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008618 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008619 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008620 if (!starts_with_colon)
8621 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008622 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008623 goto erret;
8624 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01008625 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008626 if (ends_excmd2(line, ea.cmd))
8627 {
8628 // A range without a command: jump to the line.
8629 // TODO: compile to a more efficient command, possibly
8630 // calling parse_cmd_address().
8631 ea.cmdidx = CMD_SIZE;
8632 line = compile_exec(line, &ea, &cctx);
8633 goto nextline;
8634 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008635 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008636 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01008637 p = find_ex_command(&ea, NULL, starts_with_colon
8638 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008639
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008640 if (p == NULL)
8641 {
8642 if (cctx.ctx_skip != SKIP_YES)
8643 emsg(_(e_ambiguous_use_of_user_defined_command));
8644 goto erret;
8645 }
8646
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008647 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8648 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008649 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008650 {
8651 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008652 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008653 }
8654
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008655 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008656 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008657 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008658 // CMD_var cannot happen, compile_assignment() above would be
8659 // used. Most likely an assignment to a non-existing variable.
8660 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008661 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008662 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008663 }
8664
Bram Moolenaar3988f642020-08-27 22:43:03 +02008665 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008666 && ea.cmdidx != CMD_elseif
8667 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008668 && ea.cmdidx != CMD_endif
8669 && ea.cmdidx != CMD_endfor
8670 && ea.cmdidx != CMD_endwhile
8671 && ea.cmdidx != CMD_catch
8672 && ea.cmdidx != CMD_finally
8673 && ea.cmdidx != CMD_endtry)
8674 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008675 emsg(_(e_unreachable_code_after_return));
8676 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008677 }
8678
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008679 p = skipwhite(p);
8680 if (ea.cmdidx != CMD_SIZE
8681 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8682 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008683 if (ea.cmdidx >= 0)
8684 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008685 if ((ea.argt & EX_BANG) && *p == '!')
8686 {
8687 ea.forceit = TRUE;
8688 p = skipwhite(p + 1);
8689 }
8690 }
8691
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008692 switch (ea.cmdidx)
8693 {
8694 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008695 ea.arg = p;
8696 line = compile_nested_function(&ea, &cctx);
8697 break;
8698
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008699 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008700 // TODO: should we allow this, e.g. to declare a global
8701 // function?
8702 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008703 goto erret;
8704
8705 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008706 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008707 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008708 break;
8709
8710 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008711 emsg(_(e_cannot_use_let_in_vim9_script));
8712 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008713 case CMD_var:
8714 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008715 case CMD_const:
8716 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008717 if (line == p)
8718 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008719 break;
8720
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008721 case CMD_unlet:
8722 case CMD_unlockvar:
8723 case CMD_lockvar:
8724 line = compile_unletlock(p, &ea, &cctx);
8725 break;
8726
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008727 case CMD_import:
8728 line = compile_import(p, &cctx);
8729 break;
8730
8731 case CMD_if:
8732 line = compile_if(p, &cctx);
8733 break;
8734 case CMD_elseif:
8735 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008736 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008737 break;
8738 case CMD_else:
8739 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008740 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008741 break;
8742 case CMD_endif:
8743 line = compile_endif(p, &cctx);
8744 break;
8745
8746 case CMD_while:
8747 line = compile_while(p, &cctx);
8748 break;
8749 case CMD_endwhile:
8750 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008751 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008752 break;
8753
8754 case CMD_for:
8755 line = compile_for(p, &cctx);
8756 break;
8757 case CMD_endfor:
8758 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008759 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008760 break;
8761 case CMD_continue:
8762 line = compile_continue(p, &cctx);
8763 break;
8764 case CMD_break:
8765 line = compile_break(p, &cctx);
8766 break;
8767
8768 case CMD_try:
8769 line = compile_try(p, &cctx);
8770 break;
8771 case CMD_catch:
8772 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008773 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008774 break;
8775 case CMD_finally:
8776 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008777 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008778 break;
8779 case CMD_endtry:
8780 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008781 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008782 break;
8783 case CMD_throw:
8784 line = compile_throw(p, &cctx);
8785 break;
8786
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008787 case CMD_eval:
8788 if (compile_expr0(&p, &cctx) == FAIL)
8789 goto erret;
8790
Bram Moolenaar3988f642020-08-27 22:43:03 +02008791 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008792 generate_instr_drop(&cctx, ISN_DROP, 1);
8793
8794 line = skipwhite(p);
8795 break;
8796
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008797 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008798 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008799 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008800 case CMD_echomsg:
8801 case CMD_echoerr:
8802 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008803 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008804
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008805 case CMD_put:
8806 ea.cmd = cmd;
8807 line = compile_put(p, &ea, &cctx);
8808 break;
8809
Bram Moolenaar3988f642020-08-27 22:43:03 +02008810 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008811
Bram Moolenaarae616492020-07-28 20:07:27 +02008812 case CMD_append:
8813 case CMD_change:
8814 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01008815 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008816 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008817 case CMD_xit:
8818 not_in_vim9(&ea);
8819 goto erret;
8820
Bram Moolenaar002262f2020-07-08 17:47:57 +02008821 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008822 if (cctx.ctx_skip != SKIP_YES)
8823 {
8824 semsg(_(e_invalid_command_str), ea.cmd);
8825 goto erret;
8826 }
8827 // We don't check for a next command here.
8828 line = (char_u *)"";
8829 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008830
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008831 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008832 if (cctx.ctx_skip == SKIP_YES)
8833 {
8834 // We don't check for a next command here.
8835 line = (char_u *)"";
8836 }
8837 else
8838 {
8839 // Not recognized, execute with do_cmdline_cmd().
8840 ea.arg = p;
8841 line = compile_exec(line, &ea, &cctx);
8842 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008843 break;
8844 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008845nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008846 if (line == NULL)
8847 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008848 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008849
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008850 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008851 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008852
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008853 if (cctx.ctx_type_stack.ga_len < 0)
8854 {
8855 iemsg("Type stack underflow");
8856 goto erret;
8857 }
8858 }
8859
8860 if (cctx.ctx_scope != NULL)
8861 {
8862 if (cctx.ctx_scope->se_type == IF_SCOPE)
8863 emsg(_(e_endif));
8864 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8865 emsg(_(e_endwhile));
8866 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8867 emsg(_(e_endfor));
8868 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008869 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008870 goto erret;
8871 }
8872
Bram Moolenaarefd88552020-06-18 20:50:10 +02008873 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008874 {
8875 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8876 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008877 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008878 goto erret;
8879 }
8880
8881 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008882 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008883 }
8884
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008885 {
8886 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8887 + ufunc->uf_dfunc_idx;
8888 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008889 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008890#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008891 if (cctx.ctx_profiling)
8892 {
8893 dfunc->df_instr_prof = instr->ga_data;
8894 dfunc->df_instr_prof_count = instr->ga_len;
8895 }
8896 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01008897#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008898 {
8899 dfunc->df_instr = instr->ga_data;
8900 dfunc->df_instr_count = instr->ga_len;
8901 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008902 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008903 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008904 if (cctx.ctx_outer_used)
8905 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008906 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008907 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008908
8909 ret = OK;
8910
8911erret:
8912 if (ret == FAIL)
8913 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008914 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008915 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8916 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008917
8918 for (idx = 0; idx < instr->ga_len; ++idx)
8919 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008920 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008921 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008922
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008923 // If using the last entry in the table and it was added above, we
8924 // might as well remove it.
8925 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008926 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008927 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008928 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008929 ufunc->uf_dfunc_idx = 0;
8930 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008931 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008932
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008933 while (cctx.ctx_scope != NULL)
8934 drop_scope(&cctx);
8935
Bram Moolenaar20431c92020-03-20 18:39:46 +01008936 // Don't execute this function body.
8937 ga_clear_strings(&ufunc->uf_lines);
8938
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008939 if (errormsg != NULL)
8940 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008941 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008942 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008943 }
8944
8945 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008946 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008947 if (do_estack_push)
8948 estack_pop();
8949
Bram Moolenaar20431c92020-03-20 18:39:46 +01008950 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008951 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008952 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008953 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008954}
8955
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008956 void
8957set_function_type(ufunc_T *ufunc)
8958{
8959 int varargs = ufunc->uf_va_name != NULL;
8960 int argcount = ufunc->uf_args.ga_len;
8961
8962 // Create a type for the function, with the return type and any
8963 // argument types.
8964 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8965 // The type is included in "tt_args".
8966 if (argcount > 0 || varargs)
8967 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01008968 if (ufunc->uf_type_list.ga_itemsize == 0)
8969 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008970 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8971 argcount, &ufunc->uf_type_list);
8972 // Add argument types to the function type.
8973 if (func_type_add_arg_types(ufunc->uf_func_type,
8974 argcount + varargs,
8975 &ufunc->uf_type_list) == FAIL)
8976 return;
8977 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8978 ufunc->uf_func_type->tt_min_argcount =
8979 argcount - ufunc->uf_def_args.ga_len;
8980 if (ufunc->uf_arg_types == NULL)
8981 {
8982 int i;
8983
8984 // lambda does not have argument types.
8985 for (i = 0; i < argcount; ++i)
8986 ufunc->uf_func_type->tt_args[i] = &t_any;
8987 }
8988 else
8989 mch_memmove(ufunc->uf_func_type->tt_args,
8990 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8991 if (varargs)
8992 {
8993 ufunc->uf_func_type->tt_args[argcount] =
8994 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8995 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8996 }
8997 }
8998 else
8999 // No arguments, can use a predefined type.
9000 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9001 argcount, &ufunc->uf_type_list);
9002}
9003
9004
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009005/*
9006 * Delete an instruction, free what it contains.
9007 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009008 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009009delete_instr(isn_T *isn)
9010{
9011 switch (isn->isn_type)
9012 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009013 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009014 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009015 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009016 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009017 case ISN_LOADENV:
9018 case ISN_LOADG:
9019 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009020 case ISN_LOADT:
9021 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009022 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009023 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009024 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009025 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009026 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009027 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009028 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009029 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009030 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009031 case ISN_STOREW:
9032 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009033 vim_free(isn->isn_arg.string);
9034 break;
9035
9036 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009037 case ISN_STORES:
9038 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009039 break;
9040
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009041 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02009042 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009043 vim_free(isn->isn_arg.unlet.ul_name);
9044 break;
9045
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009046 case ISN_STOREOPT:
9047 vim_free(isn->isn_arg.storeopt.so_name);
9048 break;
9049
9050 case ISN_PUSHBLOB: // push blob isn_arg.blob
9051 blob_unref(isn->isn_arg.blob);
9052 break;
9053
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009054 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009055#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009056 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009057#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009058 break;
9059
9060 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009061#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009062 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009063#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009064 break;
9065
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009066 case ISN_UCALL:
9067 vim_free(isn->isn_arg.ufunc.cuf_name);
9068 break;
9069
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009070 case ISN_FUNCREF:
9071 {
9072 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9073 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01009074 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02009075
Bram Moolenaar077a4232020-12-22 18:33:27 +01009076 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
9077 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02009078 }
9079 break;
9080
9081 case ISN_DCALL:
9082 {
9083 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9084 + isn->isn_arg.dfunc.cdf_idx;
9085
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009086 if (dfunc->df_ufunc != NULL
9087 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02009088 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009089 }
9090 break;
9091
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009092 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02009093 {
9094 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
9095 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
9096
9097 if (ufunc != NULL)
9098 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009099 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02009100 func_ptr_unref(ufunc);
9101 }
9102
9103 vim_free(lambda);
9104 vim_free(isn->isn_arg.newfunc.nf_global);
9105 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009106 break;
9107
Bram Moolenaar5e654232020-09-16 15:22:00 +02009108 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01009109 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02009110 free_type(isn->isn_arg.type.ct_type);
9111 break;
9112
Bram Moolenaar02194d22020-10-24 23:08:38 +02009113 case ISN_CMDMOD:
9114 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9115 ->cmod_filter_regmatch.regprog);
9116 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9117 break;
9118
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009119 case ISN_LOADSCRIPT:
9120 case ISN_STORESCRIPT:
9121 vim_free(isn->isn_arg.script.scriptref);
9122 break;
9123
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009124 case ISN_TRY:
9125 vim_free(isn->isn_arg.try.try_ref);
9126 break;
9127
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009128 case ISN_2BOOL:
9129 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02009130 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009131 case ISN_ADDBLOB:
9132 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009133 case ISN_ANYINDEX:
9134 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009135 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02009136 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009137 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009138 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009139 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02009140 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009141 case ISN_COMPAREANY:
9142 case ISN_COMPAREBLOB:
9143 case ISN_COMPAREBOOL:
9144 case ISN_COMPAREDICT:
9145 case ISN_COMPAREFLOAT:
9146 case ISN_COMPAREFUNC:
9147 case ISN_COMPARELIST:
9148 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009149 case ISN_COMPARESPECIAL:
9150 case ISN_COMPARESTRING:
9151 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02009152 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009153 case ISN_DROP:
9154 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009155 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009156 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009157 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009158 case ISN_EXECCONCAT:
9159 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009160 case ISN_FINALLY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009161 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009162 case ISN_GETITEM:
9163 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009164 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02009165 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02009166 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02009167 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009168 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009169 case ISN_LOADBDICT:
9170 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009171 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009172 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009173 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009174 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009175 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009176 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009177 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009178 case ISN_NEGATENR:
9179 case ISN_NEWDICT:
9180 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009181 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009182 case ISN_OPFLOAT:
9183 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009184 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02009185 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01009186 case ISN_PROF_END:
9187 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009188 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009189 case ISN_PUSHF:
9190 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009191 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009192 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009193 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01009194 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009195 case ISN_SHUFFLE:
9196 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009197 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01009198 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009199 case ISN_STORENR:
9200 case ISN_STOREOUTER:
9201 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009202 case ISN_STOREV:
9203 case ISN_STRINDEX:
9204 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009205 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +01009206 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +01009207 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01009208 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +01009209 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009210 // nothing allocated
9211 break;
9212 }
9213}
9214
9215/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009216 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01009217 */
9218 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009219delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009220{
9221 int idx;
9222
9223 ga_clear(&dfunc->df_def_args_isn);
9224
9225 if (dfunc->df_instr != NULL)
9226 {
9227 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
9228 delete_instr(dfunc->df_instr + idx);
9229 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009230 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009231 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01009232#ifdef FEAT_PROFILE
9233 if (dfunc->df_instr_prof != NULL)
9234 {
9235 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
9236 delete_instr(dfunc->df_instr_prof + idx);
9237 VIM_CLEAR(dfunc->df_instr_prof);
9238 dfunc->df_instr_prof = NULL;
9239 }
9240#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01009241
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009242 if (mark_deleted)
9243 dfunc->df_deleted = TRUE;
9244 if (dfunc->df_ufunc != NULL)
9245 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009246}
9247
9248/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009249 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009250 * function, unless another user function still uses it.
9251 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009252 */
9253 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009254unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009255{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009256 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009257 {
9258 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9259 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009260
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009261 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009262 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009263 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009264 ufunc->uf_dfunc_idx = 0;
9265 if (dfunc->df_ufunc == ufunc)
9266 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009267 }
9268}
9269
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009270/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009271 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009272 */
9273 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009274link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009275{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009276 if (ufunc->uf_dfunc_idx > 0)
9277 {
9278 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9279 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009280
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009281 ++dfunc->df_refcount;
9282 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009283}
9284
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009285#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009286/*
9287 * Free all functions defined with ":def".
9288 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009289 void
9290free_def_functions(void)
9291{
Bram Moolenaar20431c92020-03-20 18:39:46 +01009292 int idx;
9293
9294 for (idx = 0; idx < def_functions.ga_len; ++idx)
9295 {
9296 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
9297
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009298 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009299 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009300 }
9301
9302 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009303}
9304#endif
9305
9306
9307#endif // FEAT_EVAL