blob: c9ca5953fc681d00ac1be424f3c17d1768a2cafc [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 Moolenaare42939a2021-04-05 17:11:17 +02002653 * Compile getting a member from a list/dict/string/blob. Stack has the
2654 * indexable value and the index.
2655 */
2656 static int
2657compile_member(int is_slice, cctx_T *cctx)
2658{
2659 type_T **typep;
2660 garray_T *stack = &cctx->ctx_type_stack;
2661 vartype_T vtype;
2662 type_T *valtype;
2663
2664 // We can index a list and a dict. If we don't know the type
2665 // we can use the index value type.
2666 // TODO: If we don't know use an instruction to figure it out at
2667 // runtime.
2668 typep = ((type_T **)stack->ga_data) + stack->ga_len
2669 - (is_slice ? 3 : 2);
2670 vtype = (*typep)->tt_type;
2671 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2672 // If the index is a string, the variable must be a Dict.
2673 if (*typep == &t_any && valtype == &t_string)
2674 vtype = VAR_DICT;
2675 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
2676 {
2677 if (need_type(valtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
2678 return FAIL;
2679 if (is_slice)
2680 {
2681 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2682 if (need_type(valtype, &t_number, -2, 0, cctx,
2683 FALSE, FALSE) == FAIL)
2684 return FAIL;
2685 }
2686 }
2687
2688 if (vtype == VAR_DICT)
2689 {
2690 if (is_slice)
2691 {
2692 emsg(_(e_cannot_slice_dictionary));
2693 return FAIL;
2694 }
2695 if ((*typep)->tt_type == VAR_DICT)
2696 {
2697 *typep = (*typep)->tt_member;
2698 if (*typep == &t_unknown)
2699 // empty dict was used
2700 *typep = &t_any;
2701 }
2702 else
2703 {
2704 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2705 FALSE, FALSE) == FAIL)
2706 return FAIL;
2707 *typep = &t_any;
2708 }
2709 if (may_generate_2STRING(-1, cctx) == FAIL)
2710 return FAIL;
2711 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2712 return FAIL;
2713 }
2714 else if (vtype == VAR_STRING)
2715 {
2716 *typep = &t_string;
2717 if ((is_slice
2718 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2719 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2720 return FAIL;
2721 }
2722 else if (vtype == VAR_BLOB)
2723 {
2724 emsg("Sorry, blob index and slice not implemented yet");
2725 return FAIL;
2726 }
2727 else if (vtype == VAR_LIST || *typep == &t_any)
2728 {
2729 if (is_slice)
2730 {
2731 if (generate_instr_drop(cctx,
2732 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
2733 2) == FAIL)
2734 return FAIL;
2735 }
2736 else
2737 {
2738 if ((*typep)->tt_type == VAR_LIST)
2739 {
2740 *typep = (*typep)->tt_member;
2741 if (*typep == &t_unknown)
2742 // empty list was used
2743 *typep = &t_any;
2744 }
2745 if (generate_instr_drop(cctx,
2746 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1) == FAIL)
2747 return FAIL;
2748 }
2749 }
2750 else
2751 {
2752 emsg(_(e_string_list_dict_or_blob_required));
2753 return FAIL;
2754 }
2755 return OK;
2756}
2757
2758/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002759 * Generate an instruction to load script-local variable "name", without the
2760 * leading "s:".
2761 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 */
2763 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002764compile_load_scriptvar(
2765 cctx_T *cctx,
2766 char_u *name, // variable NUL terminated
2767 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002768 char_u **end, // end of variable
2769 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002770{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002771 scriptitem_T *si;
2772 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 imported_T *import;
2774
Bram Moolenaare3d46852020-08-29 13:39:17 +02002775 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2776 return FAIL;
2777 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002778 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002779 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002780 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002781 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002782 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2783 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002784 }
2785 if (idx >= 0)
2786 {
2787 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2788
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002789 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002790 current_sctx.sc_sid, idx, sv->sv_type);
2791 return OK;
2792 }
2793
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002794 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002795 if (import != NULL)
2796 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002797 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002798 {
2799 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002800 char_u *exp_name;
2801 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002802 ufunc_T *ufunc;
2803 type_T *type;
2804
2805 // Used "import * as Name", need to lookup the member.
2806 if (*p != '.')
2807 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002808 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002809 return FAIL;
2810 }
2811 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002812 if (VIM_ISWHITE(*p))
2813 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002814 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002815 return FAIL;
2816 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002817
Bram Moolenaar1c991142020-07-04 13:15:31 +02002818 // isolate one name
2819 exp_name = p;
2820 while (eval_isnamec(*p))
2821 ++p;
2822 cc = *p;
2823 *p = NUL;
2824
Bram Moolenaaredba7072021-03-13 21:14:18 +01002825 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2826 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002827 *p = cc;
2828 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002829 *end = p;
2830
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002831 if (idx < 0)
2832 {
2833 if (*p == '(' && ufunc != NULL)
2834 {
2835 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
2836 return OK;
2837 }
2838 return FAIL;
2839 }
2840
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002841 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2842 import->imp_sid,
2843 idx,
2844 type);
2845 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002846 else if (import->imp_funcname != NULL)
2847 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002848 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002849 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2850 import->imp_sid,
2851 import->imp_var_vals_idx,
2852 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 return OK;
2854 }
2855
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002856 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002857 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 return FAIL;
2859}
2860
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002861 static int
2862generate_funcref(cctx_T *cctx, char_u *name)
2863{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002864 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002865
2866 if (ufunc == NULL)
2867 return FAIL;
2868
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002869 // Need to compile any default values to get the argument types.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01002870 if (func_needs_compiling(ufunc, PROFILING(ufunc))
2871 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002872 == FAIL)
2873 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002874 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002875}
2876
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877/*
2878 * Compile a variable name into a load instruction.
2879 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002880 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881 * When "error" is FALSE do not give an error when not found.
2882 */
2883 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002884compile_load(
2885 char_u **arg,
2886 char_u *end_arg,
2887 cctx_T *cctx,
2888 int is_expr,
2889 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890{
2891 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002892 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002893 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002895 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896
2897 if (*(*arg + 1) == ':')
2898 {
2899 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002900 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002901 {
2902 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002903
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002904 switch (**arg)
2905 {
2906 case 'g': isn_type = ISN_LOADGDICT; break;
2907 case 'w': isn_type = ISN_LOADWDICT; break;
2908 case 't': isn_type = ISN_LOADTDICT; break;
2909 case 'b': isn_type = ISN_LOADBDICT; break;
2910 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002911 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002912 goto theend;
2913 }
2914 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2915 goto theend;
2916 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002917 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 else
2919 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002920 isntype_T isn_type = ISN_DROP;
2921
2922 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2923 if (name == NULL)
2924 return FAIL;
2925
2926 switch (**arg)
2927 {
2928 case 'v': res = generate_LOADV(cctx, name, error);
2929 break;
2930 case 's': res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02002931 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002932 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002933 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2934 isn_type = ISN_LOADG;
2935 else
2936 {
2937 isn_type = ISN_LOADAUTO;
2938 vim_free(name);
2939 name = vim_strnsave(*arg, end - *arg);
2940 if (name == NULL)
2941 return FAIL;
2942 }
2943 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002944 case 'w': isn_type = ISN_LOADW; break;
2945 case 't': isn_type = ISN_LOADT; break;
2946 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002947 default: // cannot happen, just in case
2948 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002949 goto theend;
2950 }
2951 if (isn_type != ISN_DROP)
2952 {
2953 // Global, Buffer-local, Window-local and Tabpage-local
2954 // variables can be defined later, thus we don't check if it
2955 // exists, give error at runtime.
2956 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2957 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 }
2959 }
2960 else
2961 {
2962 size_t len = end - *arg;
2963 int idx;
2964 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002965 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966
2967 name = vim_strnsave(*arg, end - *arg);
2968 if (name == NULL)
2969 return FAIL;
2970
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002971 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002973 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002974 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 }
2976 else
2977 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002978 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002979
Bram Moolenaar709664c2020-12-12 14:33:41 +01002980 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002982 type = lvar.lv_type;
2983 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01002984 if (lvar.lv_from_outer != 0)
2985 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002986 else
2987 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002988 }
2989 else
2990 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002991 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002992 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002993 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002994 || find_imported(name, 0, cctx) != NULL)
2995 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002996
Bram Moolenaar0f769812020-09-12 18:32:34 +02002997 // When evaluating an expression and the name starts with an
2998 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002999 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02003000 if (res == FAIL && is_expr
3001 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003002 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 }
3004 }
3005 if (gen_load)
3006 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003007 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003008 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003009 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003010 cctx->ctx_outer_used = TRUE;
3011 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012 }
3013
3014 *arg = end;
3015
3016theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003017 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003018 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 vim_free(name);
3020 return res;
3021}
3022
3023/*
3024 * Compile the argument expressions.
3025 * "arg" points to just after the "(" and is advanced to after the ")"
3026 */
3027 static int
3028compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
3029{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003030 char_u *p = *arg;
3031 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003032 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033
Bram Moolenaare6085c52020-04-12 20:19:16 +02003034 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003036 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3037 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003038 if (*p == ')')
3039 {
3040 *arg = p + 1;
3041 return OK;
3042 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003043 if (must_end)
3044 {
3045 semsg(_(e_missing_comma_before_argument_str), p);
3046 return FAIL;
3047 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003048
Bram Moolenaara5565e42020-05-09 15:44:01 +02003049 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 return FAIL;
3051 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003052
3053 if (*p != ',' && *skipwhite(p) == ',')
3054 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003055 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003056 p = skipwhite(p);
3057 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003059 {
3060 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003061 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003062 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003063 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003064 else
3065 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003066 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003067 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003069failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003070 emsg(_(e_missing_close));
3071 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072}
3073
3074/*
3075 * Compile a function call: name(arg1, arg2)
3076 * "arg" points to "name", "arg + varlen" to the "(".
3077 * "argcount_init" is 1 for "value->method()"
3078 * Instructions:
3079 * EVAL arg1
3080 * EVAL arg2
3081 * BCALL / DCALL / UCALL
3082 */
3083 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003084compile_call(
3085 char_u **arg,
3086 size_t varlen,
3087 cctx_T *cctx,
3088 ppconst_T *ppconst,
3089 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090{
3091 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003092 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 int argcount = argcount_init;
3094 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003095 char_u fname_buf[FLEN_FIXED + 1];
3096 char_u *tofree = NULL;
3097 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003098 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003099 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003100 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101
Bram Moolenaara5565e42020-05-09 15:44:01 +02003102 // we can evaluate "has('name')" at compile time
3103 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3104 {
3105 char_u *s = skipwhite(*arg + varlen + 1);
3106 typval_T argvars[2];
3107
3108 argvars[0].v_type = VAR_UNKNOWN;
3109 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003110 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003111 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003112 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003113 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003114 if (*s == ')' && argvars[0].v_type == VAR_STRING
3115 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003116 {
3117 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3118
3119 *arg = s + 1;
3120 argvars[1].v_type = VAR_UNKNOWN;
3121 tv->v_type = VAR_NUMBER;
3122 tv->vval.v_number = 0;
3123 f_has(argvars, tv);
3124 clear_tv(&argvars[0]);
3125 ++ppconst->pp_used;
3126 return OK;
3127 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003128 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003129 }
3130
3131 if (generate_ppconst(cctx, ppconst) == FAIL)
3132 return FAIL;
3133
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 if (varlen >= sizeof(namebuf))
3135 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003136 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 return FAIL;
3138 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003139 vim_strncpy(namebuf, *arg, varlen);
3140 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141
3142 *arg = skipwhite(*arg + varlen + 1);
3143 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003144 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145
Bram Moolenaar03290b82020-12-19 16:30:44 +01003146 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003147 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 {
3149 int idx;
3150
3151 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003152 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003153 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003154 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003155 if (STRCMP(name, "flatten") == 0)
3156 {
3157 emsg(_(e_cannot_use_flatten_in_vim9_script));
3158 goto theend;
3159 }
3160
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003161 if (STRCMP(name, "add") == 0 && argcount == 2)
3162 {
3163 garray_T *stack = &cctx->ctx_type_stack;
3164 type_T *type = ((type_T **)stack->ga_data)[
3165 stack->ga_len - 2];
3166
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003167 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003168 if (type->tt_type == VAR_LIST)
3169 {
3170 // inline "add(list, item)" so that the type can be checked
3171 res = generate_LISTAPPEND(cctx);
3172 idx = -1;
3173 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003174 else if (type->tt_type == VAR_BLOB)
3175 {
3176 // inline "add(blob, nr)" so that the type can be checked
3177 res = generate_BLOBAPPEND(cctx);
3178 idx = -1;
3179 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003180 }
3181
3182 if (idx >= 0)
3183 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3184 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003185 else
3186 semsg(_(e_unknownfunc), namebuf);
3187 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 }
3189
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003190 // An argument or local variable can be a function reference, this
3191 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003192 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003193 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003194 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003195 // If we can find the function by name generate the right call.
3196 // Skip global functions here, a local funcref takes precedence.
3197 ufunc = find_func(name, FALSE, cctx);
3198 if (ufunc != NULL && !func_is_global(ufunc))
3199 {
3200 res = generate_CALL(cctx, ufunc, argcount);
3201 goto theend;
3202 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003203 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204
3205 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003206 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003207 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003209 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003210 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003211 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003212 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003213 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003214
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003215 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003216 goto theend;
3217 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218
Bram Moolenaar0f769812020-09-12 18:32:34 +02003219 // If we can find a global function by name generate the right call.
3220 if (ufunc != NULL)
3221 {
3222 res = generate_CALL(cctx, ufunc, argcount);
3223 goto theend;
3224 }
3225
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003226 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003227 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003228 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003229 res = generate_UCALL(cctx, name, argcount);
3230 else
3231 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003232
3233theend:
3234 vim_free(tofree);
3235 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236}
3237
3238// like NAMESPACE_CHAR but with 'a' and 'l'.
3239#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3240
3241/*
3242 * Find the end of a variable or function name. Unlike find_name_end() this
3243 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003244 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245 * Return a pointer to just after the name. Equal to "arg" if there is no
3246 * valid name.
3247 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003248 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003249to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250{
3251 char_u *p;
3252
3253 // Quick check for valid starting character.
3254 if (!eval_isnamec1(*arg))
3255 return arg;
3256
3257 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3258 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3259 // and can be used in slice "[n:]".
3260 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003261 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3263 break;
3264 return p;
3265}
3266
3267/*
3268 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003269 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270 */
3271 char_u *
3272to_name_const_end(char_u *arg)
3273{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003274 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 typval_T rettv;
3276
3277 if (p == arg && *arg == '[')
3278 {
3279
3280 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003281 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 p = arg;
3283 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 return p;
3285}
3286
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003287/*
3288 * parse a list: [expr, expr]
3289 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003290 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291 */
3292 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003293compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294{
3295 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003296 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003298 int is_const;
3299 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003301 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003303 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003304 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003305 semsg(_(e_list_end), *arg);
3306 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003307 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003308 if (*p == ',')
3309 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003310 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003311 return FAIL;
3312 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003313 if (*p == ']')
3314 {
3315 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003316 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003317 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003318 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003319 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003320 if (!is_const)
3321 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003322 ++count;
3323 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003324 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003325 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003326 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3327 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003328 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003329 return FAIL;
3330 }
3331 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003332 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333 p = skipwhite(p);
3334 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003335 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003336
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003337 ppconst->pp_is_const = is_all_const;
3338 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339}
3340
3341/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003342 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003343 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003344 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003345 */
3346 static int
3347compile_lambda(char_u **arg, cctx_T *cctx)
3348{
Bram Moolenaare462f522020-12-27 14:43:30 +01003349 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003350 typval_T rettv;
3351 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003352 evalarg_T evalarg;
3353
3354 CLEAR_FIELD(evalarg);
3355 evalarg.eval_flags = EVAL_EVALUATE;
3356 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357
3358 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003359 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3360 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003361 {
3362 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003363 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003364 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003365
Bram Moolenaar65c44152020-12-24 15:14:01 +01003366 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003368 ++ufunc->uf_refcount;
3369 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370
Bram Moolenaar65c44152020-12-24 15:14:01 +01003371 // Compile the function into instructions.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01003372 compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003374 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3375 // points into it. Point to the original line to avoid a dangling pointer.
3376 if (evalarg.eval_tofree_cmdline != NULL)
3377 {
3378 size_t off = *arg - evalarg.eval_tofree_cmdline;
3379
3380 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3381 + off;
3382 }
3383
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003384 clear_evalarg(&evalarg, NULL);
3385
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003386 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003387 {
3388 // The return type will now be known.
3389 set_function_type(ufunc);
3390
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003391 // The function reference count will be 1. When the ISN_FUNCREF
3392 // instruction is deleted the reference count is decremented and the
3393 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003394 return generate_FUNCREF(cctx, ufunc);
3395 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003396
3397 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 return FAIL;
3399}
3400
3401/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003402 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003404 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405 */
3406 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003407compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408{
3409 garray_T *instr = &cctx->ctx_instr;
3410 int count = 0;
3411 dict_T *d = dict_alloc();
3412 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003413 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003414 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003415 int is_const;
3416 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417
3418 if (d == NULL)
3419 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003420 if (generate_ppconst(cctx, ppconst) == FAIL)
3421 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003422 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003424 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425
Bram Moolenaar23c55272020-06-21 16:58:13 +02003426 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003427 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003428 *arg = NULL;
3429 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003430 }
3431
3432 if (**arg == '}')
3433 break;
3434
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003435 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003437 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003439 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003440 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003441 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003443 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003444 if (isn->isn_type == ISN_PUSHNR)
3445 {
3446 char buf[NUMBUFLEN];
3447
3448 // Convert to string at compile time.
3449 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3450 isn->isn_type = ISN_PUSHS;
3451 isn->isn_arg.string = vim_strsave((char_u *)buf);
3452 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 if (isn->isn_type == ISN_PUSHS)
3454 key = isn->isn_arg.string;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003455 else if (may_generate_2STRING(-1, cctx) == FAIL)
3456 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003457 *arg = skipwhite(*arg);
3458 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003459 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003460 emsg(_(e_missing_matching_bracket_after_dict_key));
3461 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003462 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003463 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003464 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003465 else
3466 {
3467 // {"name": value},
3468 // {'name': value},
3469 // {name: value} use "name" as a literal key
3470 key = get_literal_key(arg);
3471 if (key == NULL)
3472 return FAIL;
3473 if (generate_PUSHS(cctx, key) == FAIL)
3474 return FAIL;
3475 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003476
3477 // Check for duplicate keys, if using string keys.
3478 if (key != NULL)
3479 {
3480 item = dict_find(d, key, -1);
3481 if (item != NULL)
3482 {
3483 semsg(_(e_duplicate_key), key);
3484 goto failret;
3485 }
3486 item = dictitem_alloc(key);
3487 if (item != NULL)
3488 {
3489 item->di_tv.v_type = VAR_UNKNOWN;
3490 item->di_tv.v_lock = 0;
3491 if (dict_add(d, item) == FAIL)
3492 dictitem_free(item);
3493 }
3494 }
3495
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496 if (**arg != ':')
3497 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003498 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003499 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003500 else
3501 semsg(_(e_missing_dict_colon), *arg);
3502 return FAIL;
3503 }
3504 whitep = *arg + 1;
3505 if (!IS_WHITE_OR_NUL(*whitep))
3506 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003507 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003508 return FAIL;
3509 }
3510
Bram Moolenaar23c55272020-06-21 16:58:13 +02003511 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003512 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003513 *arg = NULL;
3514 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003515 }
3516
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003517 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003519 if (!is_const)
3520 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003521 ++count;
3522
Bram Moolenaar2c330432020-04-13 14:41:35 +02003523 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003524 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003525 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003526 *arg = NULL;
3527 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003528 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003529 if (**arg == '}')
3530 break;
3531 if (**arg != ',')
3532 {
3533 semsg(_(e_missing_dict_comma), *arg);
3534 goto failret;
3535 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003536 if (IS_WHITE_OR_NUL(*whitep))
3537 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003538 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003539 return FAIL;
3540 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003541 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003542 if (!IS_WHITE_OR_NUL(*whitep))
3543 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003544 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003545 return FAIL;
3546 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003547 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548 }
3549
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 *arg = *arg + 1;
3551
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003552 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003553 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003554 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003555 *arg += STRLEN(*arg);
3556
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003557 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003558 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 return generate_NEWDICT(cctx, count);
3560
3561failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003562 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003563 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003564 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003565 *arg = (char_u *)"";
3566 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003567 dict_unref(d);
3568 return FAIL;
3569}
3570
3571/*
3572 * Compile "&option".
3573 */
3574 static int
3575compile_get_option(char_u **arg, cctx_T *cctx)
3576{
3577 typval_T rettv;
3578 char_u *start = *arg;
3579 int ret;
3580
3581 // parse the option and get the current value to get the type.
3582 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003583 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584 if (ret == OK)
3585 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003586 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003587 char_u *name = vim_strnsave(start, *arg - start);
3588 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3589 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590
3591 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3592 vim_free(name);
3593 }
3594 clear_tv(&rettv);
3595
3596 return ret;
3597}
3598
3599/*
3600 * Compile "$VAR".
3601 */
3602 static int
3603compile_get_env(char_u **arg, cctx_T *cctx)
3604{
3605 char_u *start = *arg;
3606 int len;
3607 int ret;
3608 char_u *name;
3609
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 ++*arg;
3611 len = get_env_len(arg);
3612 if (len == 0)
3613 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003614 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 return FAIL;
3616 }
3617
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003618 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619 name = vim_strnsave(start, len + 1);
3620 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3621 vim_free(name);
3622 return ret;
3623}
3624
3625/*
3626 * Compile "@r".
3627 */
3628 static int
3629compile_get_register(char_u **arg, cctx_T *cctx)
3630{
3631 int ret;
3632
3633 ++*arg;
3634 if (**arg == NUL)
3635 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003636 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 return FAIL;
3638 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003639 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 {
3641 emsg_invreg(**arg);
3642 return FAIL;
3643 }
3644 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3645 ++*arg;
3646 return ret;
3647}
3648
3649/*
3650 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003651 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 */
3653 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003654apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003656 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657
3658 // this works from end to start
3659 while (p > start)
3660 {
3661 --p;
3662 if (*p == '-' || *p == '+')
3663 {
3664 // only '-' has an effect, for '+' we only check the type
3665#ifdef FEAT_FLOAT
3666 if (rettv->v_type == VAR_FLOAT)
3667 {
3668 if (*p == '-')
3669 rettv->vval.v_float = -rettv->vval.v_float;
3670 }
3671 else
3672#endif
3673 {
3674 varnumber_T val;
3675 int error = FALSE;
3676
3677 // tv_get_number_chk() accepts a string, but we don't want that
3678 // here
3679 if (check_not_string(rettv) == FAIL)
3680 return FAIL;
3681 val = tv_get_number_chk(rettv, &error);
3682 clear_tv(rettv);
3683 if (error)
3684 return FAIL;
3685 if (*p == '-')
3686 val = -val;
3687 rettv->v_type = VAR_NUMBER;
3688 rettv->vval.v_number = val;
3689 }
3690 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003691 else if (numeric_only)
3692 {
3693 ++p;
3694 break;
3695 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003696 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 {
3698 int v = tv2bool(rettv);
3699
3700 // '!' is permissive in the type.
3701 clear_tv(rettv);
3702 rettv->v_type = VAR_BOOL;
3703 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3704 }
3705 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003706 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707 return OK;
3708}
3709
3710/*
3711 * Recognize v: variables that are constants and set "rettv".
3712 */
3713 static void
3714get_vim_constant(char_u **arg, typval_T *rettv)
3715{
3716 if (STRNCMP(*arg, "v:true", 6) == 0)
3717 {
3718 rettv->v_type = VAR_BOOL;
3719 rettv->vval.v_number = VVAL_TRUE;
3720 *arg += 6;
3721 }
3722 else if (STRNCMP(*arg, "v:false", 7) == 0)
3723 {
3724 rettv->v_type = VAR_BOOL;
3725 rettv->vval.v_number = VVAL_FALSE;
3726 *arg += 7;
3727 }
3728 else if (STRNCMP(*arg, "v:null", 6) == 0)
3729 {
3730 rettv->v_type = VAR_SPECIAL;
3731 rettv->vval.v_number = VVAL_NULL;
3732 *arg += 6;
3733 }
3734 else if (STRNCMP(*arg, "v:none", 6) == 0)
3735 {
3736 rettv->v_type = VAR_SPECIAL;
3737 rettv->vval.v_number = VVAL_NONE;
3738 *arg += 6;
3739 }
3740}
3741
Bram Moolenaar657137c2021-01-09 15:45:23 +01003742 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003743get_compare_type(char_u *p, int *len, int *type_is)
3744{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003745 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003746 int i;
3747
3748 switch (p[0])
3749 {
3750 case '=': if (p[1] == '=')
3751 type = EXPR_EQUAL;
3752 else if (p[1] == '~')
3753 type = EXPR_MATCH;
3754 break;
3755 case '!': if (p[1] == '=')
3756 type = EXPR_NEQUAL;
3757 else if (p[1] == '~')
3758 type = EXPR_NOMATCH;
3759 break;
3760 case '>': if (p[1] != '=')
3761 {
3762 type = EXPR_GREATER;
3763 *len = 1;
3764 }
3765 else
3766 type = EXPR_GEQUAL;
3767 break;
3768 case '<': if (p[1] != '=')
3769 {
3770 type = EXPR_SMALLER;
3771 *len = 1;
3772 }
3773 else
3774 type = EXPR_SEQUAL;
3775 break;
3776 case 'i': if (p[1] == 's')
3777 {
3778 // "is" and "isnot"; but not a prefix of a name
3779 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3780 *len = 5;
3781 i = p[*len];
3782 if (!isalnum(i) && i != '_')
3783 {
3784 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3785 *type_is = TRUE;
3786 }
3787 }
3788 break;
3789 }
3790 return type;
3791}
3792
3793/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003794 * Skip over an expression, ignoring most errors.
3795 */
3796 static void
3797skip_expr_cctx(char_u **arg, cctx_T *cctx)
3798{
3799 evalarg_T evalarg;
3800
3801 CLEAR_FIELD(evalarg);
3802 evalarg.eval_cctx = cctx;
3803 skip_expr(arg, &evalarg);
3804}
3805
3806/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003808 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 */
3810 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003811compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003812{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003813 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814
3815 // this works from end to start
3816 while (p > start)
3817 {
3818 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003819 while (VIM_ISWHITE(*p))
3820 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821 if (*p == '-' || *p == '+')
3822 {
3823 int negate = *p == '-';
3824 isn_T *isn;
3825
3826 // TODO: check type
3827 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3828 {
3829 --p;
3830 if (*p == '-')
3831 negate = !negate;
3832 }
3833 // only '-' has an effect, for '+' we only check the type
3834 if (negate)
3835 isn = generate_instr(cctx, ISN_NEGATENR);
3836 else
3837 isn = generate_instr(cctx, ISN_CHECKNR);
3838 if (isn == NULL)
3839 return FAIL;
3840 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003841 else if (numeric_only)
3842 {
3843 ++p;
3844 break;
3845 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003846 else
3847 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003848 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003850 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003852 if (p[-1] == '!')
3853 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003855 }
3856 if (generate_2BOOL(cctx, invert) == FAIL)
3857 return FAIL;
3858 }
3859 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003860 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861 return OK;
3862}
3863
3864/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003865 * Compile "(expression)": recursive!
3866 * Return FAIL/OK.
3867 */
3868 static int
3869compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3870{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003871 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003872 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003873
Bram Moolenaar24156692021-01-14 20:35:49 +01003874 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3875 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003876 if (ppconst->pp_used <= PPSIZE - 10)
3877 {
3878 ret = compile_expr1(arg, cctx, ppconst);
3879 }
3880 else
3881 {
3882 // Not enough space in ppconst, flush constants.
3883 if (generate_ppconst(cctx, ppconst) == FAIL)
3884 return FAIL;
3885 ret = compile_expr0(arg, cctx);
3886 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003887 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3888 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003889 if (**arg == ')')
3890 ++*arg;
3891 else if (ret == OK)
3892 {
3893 emsg(_(e_missing_close));
3894 ret = FAIL;
3895 }
3896 return ret;
3897}
3898
3899/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003901 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003902 */
3903 static int
3904compile_subscript(
3905 char_u **arg,
3906 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003907 char_u *start_leader,
3908 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003909 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003910{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003911 char_u *name_start = *end_leader;
3912
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913 for (;;)
3914 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003915 char_u *p = skipwhite(*arg);
3916
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003917 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003918 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003919 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003920
3921 // If a following line starts with "->{" or "->X" advance to that
3922 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003923 // Also if a following line starts with ".x".
3924 if (next != NULL &&
3925 ((next[0] == '-' && next[1] == '>'
3926 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003927 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003928 {
3929 next = next_line_from_context(cctx, TRUE);
3930 if (next == NULL)
3931 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003932 *arg = next;
3933 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003934 }
3935 }
3936
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003937 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003938 // not a function call.
3939 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003941 garray_T *stack = &cctx->ctx_type_stack;
3942 type_T *type;
3943 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003945 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003946 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003947 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003948
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003950 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3951
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003952 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003953 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3954 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003955 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003956 return FAIL;
3957 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003958 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003960 char_u *pstart = p;
3961
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003962 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003963 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003964 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003965
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966 // something->method()
3967 // Apply the '!', '-' and '+' first:
3968 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003969 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003971
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003972 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003973 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003974 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003975 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003976 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003977 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02003978 garray_T *stack = &cctx->ctx_type_stack;
3979 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003980 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02003981 int expr_isn_start = cctx->ctx_instr.ga_len;
3982 int expr_isn_end;
3983 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003984
3985 // Funcref call: list->(Refs[2])(arg)
3986 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02003987 //
3988 // Fist compile the function expression.
3989 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01003990 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02003991
3992 // Remember the next instruction index, where the instructions
3993 // for arguments are being written.
3994 expr_isn_end = cctx->ctx_instr.ga_len;
3995
3996 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01003997 if (**arg != '(')
3998 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003999 if (*skipwhite(*arg) == '(')
4000 emsg(_(e_nowhitespace));
4001 else
4002 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004003 return FAIL;
4004 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004005 *arg = skipwhite(*arg + 1);
4006 if (compile_arguments(arg, cctx, &argcount) == FAIL)
4007 return FAIL;
4008
Bram Moolenaar2927c072021-04-05 19:41:21 +02004009 // Move the instructions for the arguments to before the
4010 // instructions of the expression and move the type of the
4011 // expression after the argument types. This is what ISN_PCALL
4012 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004013 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004014 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4015 if (arg_isn_count > 0)
4016 {
4017 int expr_isn_count = expr_isn_end - expr_isn_start;
4018 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4019
4020 if (isn == NULL)
4021 return FAIL;
4022 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4023 + expr_isn_start,
4024 sizeof(isn_T) * expr_isn_count);
4025 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4026 + expr_isn_start,
4027 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4028 sizeof(isn_T) * arg_isn_count);
4029 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4030 + expr_isn_start + arg_isn_count,
4031 isn, sizeof(isn_T) * expr_isn_count);
4032 vim_free(isn);
4033
4034 type = ((type_T **)stack->ga_data)[type_idx_start];
4035 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4036 ((type_T **)stack->ga_data) + type_idx_start + 1,
4037 sizeof(type_T *)
4038 * (stack->ga_len - type_idx_start - 1));
4039 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4040 }
4041
Bram Moolenaar7e368202020-12-25 21:56:57 +01004042 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4043 if (generate_PCALL(cctx, argcount,
4044 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045 return FAIL;
4046 }
4047 else
4048 {
4049 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004050 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004051 if (!eval_isnamec1(*p))
4052 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004053 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004054 return FAIL;
4055 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004056 if (ASCII_ISALPHA(*p) && p[1] == ':')
4057 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004058 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059 ;
4060 if (*p != '(')
4061 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004062 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004063 return FAIL;
4064 }
4065 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02004066 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004067 return FAIL;
4068 }
4069 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004070 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004072 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004073
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004074 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004075 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004076 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004077 // TODO: blob index
4078 // TODO: more arguments
4079 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004080 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004081 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004082 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004083
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004084 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004085 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004086 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004087 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004088 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004089 // missing first index is equal to zero
4090 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004091 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004092 else
4093 {
4094 if (compile_expr0(arg, cctx) == FAIL)
4095 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004096 if (**arg == ':')
4097 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004098 semsg(_(e_white_space_required_before_and_after_str_at_str),
4099 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004100 return FAIL;
4101 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004102 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004103 return FAIL;
4104 *arg = skipwhite(*arg);
4105 }
4106 if (**arg == ':')
4107 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004108 is_slice = TRUE;
4109 ++*arg;
4110 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4111 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004112 semsg(_(e_white_space_required_before_and_after_str_at_str),
4113 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004114 return FAIL;
4115 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004116 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004117 return FAIL;
4118 if (**arg == ']')
4119 // missing second index is equal to end of string
4120 generate_PUSHNR(cctx, -1);
4121 else
4122 {
4123 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004124 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004125 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004126 return FAIL;
4127 *arg = skipwhite(*arg);
4128 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004129 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130
4131 if (**arg != ']')
4132 {
4133 emsg(_(e_missbrac));
4134 return FAIL;
4135 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004136 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137
Bram Moolenaare42939a2021-04-05 17:11:17 +02004138 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004139 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004141 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004143 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004144 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004145 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004146 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004147
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004148 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004149 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004150 {
4151 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004152 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004153 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004154 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004155 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004156 while (eval_isnamec(*p))
4157 MB_PTR_ADV(p);
4158 if (p == *arg)
4159 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004160 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161 return FAIL;
4162 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004163 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004164 return FAIL;
4165 *arg = p;
4166 }
4167 else
4168 break;
4169 }
4170
4171 // TODO - see handle_subscript():
4172 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4173 // Don't do this when "Func" is already a partial that was bound
4174 // explicitly (pt_auto is FALSE).
4175
4176 return OK;
4177}
4178
4179/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004180 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4181 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004182 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004183 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004184 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004185 *
4186 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004187 */
4188
4189/*
4190 * number number constant
4191 * 0zFFFFFFFF Blob constant
4192 * "string" string constant
4193 * 'string' literal string constant
4194 * &option-name option value
4195 * @r register contents
4196 * identifier variable value
4197 * function() function call
4198 * $VAR environment variable
4199 * (expression) nested expression
4200 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004201 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202 *
4203 * Also handle:
4204 * ! in front logical NOT
4205 * - in front unary minus
4206 * + in front unary plus (ignored)
4207 * trailing (arg) funcref/partial call
4208 * trailing [] subscript in String or List
4209 * trailing .name entry in Dictionary
4210 * trailing ->name() method call
4211 */
4212 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004213compile_expr7(
4214 char_u **arg,
4215 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004216 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 char_u *start_leader, *end_leader;
4219 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004220 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004221 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004223 ppconst->pp_is_const = FALSE;
4224
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004225 /*
4226 * Skip '!', '-' and '+' characters. They are handled later.
4227 */
4228 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004229 if (eval_leader(arg, TRUE) == FAIL)
4230 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231 end_leader = *arg;
4232
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004233 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234 switch (**arg)
4235 {
4236 /*
4237 * Number constant.
4238 */
4239 case '0': // also for blob starting with 0z
4240 case '1':
4241 case '2':
4242 case '3':
4243 case '4':
4244 case '5':
4245 case '6':
4246 case '7':
4247 case '8':
4248 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004249 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004251 // Apply "-" and "+" just before the number now, right to
4252 // left. Matters especially when "->" follows. Stops at
4253 // '!'.
4254 if (apply_leader(rettv, TRUE,
4255 start_leader, &end_leader) == FAIL)
4256 {
4257 clear_tv(rettv);
4258 return FAIL;
4259 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260 break;
4261
4262 /*
4263 * String constant: "string".
4264 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004265 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266 return FAIL;
4267 break;
4268
4269 /*
4270 * Literal string constant: 'str''ing'.
4271 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004272 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273 return FAIL;
4274 break;
4275
4276 /*
4277 * Constant Vim variable.
4278 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004279 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280 ret = NOTDONE;
4281 break;
4282
4283 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004284 * "true" constant
4285 */
4286 case 't': if (STRNCMP(*arg, "true", 4) == 0
4287 && !eval_isnamec((*arg)[4]))
4288 {
4289 *arg += 4;
4290 rettv->v_type = VAR_BOOL;
4291 rettv->vval.v_number = VVAL_TRUE;
4292 }
4293 else
4294 ret = NOTDONE;
4295 break;
4296
4297 /*
4298 * "false" constant
4299 */
4300 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4301 && !eval_isnamec((*arg)[5]))
4302 {
4303 *arg += 5;
4304 rettv->v_type = VAR_BOOL;
4305 rettv->vval.v_number = VVAL_FALSE;
4306 }
4307 else
4308 ret = NOTDONE;
4309 break;
4310
4311 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004312 * "null" constant
4313 */
4314 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004315 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004316 {
4317 *arg += 4;
4318 rettv->v_type = VAR_SPECIAL;
4319 rettv->vval.v_number = VVAL_NULL;
4320 }
4321 else
4322 ret = NOTDONE;
4323 break;
4324
4325 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326 * List: [expr, expr]
4327 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004328 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4329 return FAIL;
4330 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331 break;
4332
4333 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004334 * Dictionary: {'key': val, 'key': val}
4335 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004336 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4337 return FAIL;
4338 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004339 break;
4340
4341 /*
4342 * Option value: &name
4343 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004344 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4345 return FAIL;
4346 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347 break;
4348
4349 /*
4350 * Environment variable: $VAR.
4351 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004352 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4353 return FAIL;
4354 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355 break;
4356
4357 /*
4358 * Register contents: @r.
4359 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004360 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4361 return FAIL;
4362 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363 break;
4364 /*
4365 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004366 * lambda: (arg, arg) => expr
4367 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004368 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004369 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4370 ret = compile_lambda(arg, cctx);
4371 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004372 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373 break;
4374
4375 default: ret = NOTDONE;
4376 break;
4377 }
4378 if (ret == FAIL)
4379 return FAIL;
4380
Bram Moolenaar1c747212020-05-09 18:28:34 +02004381 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004382 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004383 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004384 clear_tv(rettv);
4385 else
4386 // A constant expression can possibly be handled compile time,
4387 // return the value instead of generating code.
4388 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 }
4390 else if (ret == NOTDONE)
4391 {
4392 char_u *p;
4393 int r;
4394
4395 if (!eval_isnamec1(**arg))
4396 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004397 if (!vim9_bad_comment(*arg))
4398 {
4399 if (ends_excmd(*skipwhite(*arg)))
4400 semsg(_(e_empty_expression_str), *arg);
4401 else
4402 semsg(_(e_name_expected_str), *arg);
4403 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004404 return FAIL;
4405 }
4406
4407 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004408 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004410 {
4411 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4412 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004414 {
4415 if (generate_ppconst(cctx, ppconst) == FAIL)
4416 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004417 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004418 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419 if (r == FAIL)
4420 return FAIL;
4421 }
4422
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004423 // Handle following "[]", ".member", etc.
4424 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004425 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004426 ppconst) == FAIL)
4427 return FAIL;
4428 if (ppconst->pp_used > 0)
4429 {
4430 // apply the '!', '-' and '+' before the constant
4431 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004432 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004433 return FAIL;
4434 return OK;
4435 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004436 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004437 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004438 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439}
4440
4441/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004442 * Give the "white on both sides" error, taking the operator from "p[len]".
4443 */
4444 void
4445error_white_both(char_u *op, int len)
4446{
4447 char_u buf[10];
4448
4449 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004450 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004451}
4452
4453/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004454 * <type>expr7: runtime type check / conversion
4455 */
4456 static int
4457compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4458{
4459 type_T *want_type = NULL;
4460
4461 // Recognize <type>
4462 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4463 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004464 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004465 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4466 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004467 return FAIL;
4468
4469 if (**arg != '>')
4470 {
4471 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004472 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004473 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004474 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004475 return FAIL;
4476 }
4477 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004478 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004479 return FAIL;
4480 }
4481
4482 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4483 return FAIL;
4484
4485 if (want_type != NULL)
4486 {
4487 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004488 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004489 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004490
Bram Moolenaard1103582020-08-14 22:44:25 +02004491 generate_ppconst(cctx, ppconst);
4492 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004493 where.wt_index = 0;
4494 where.wt_variable = FALSE;
4495 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004496 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004497 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004498 return FAIL;
4499 }
4500 }
4501
4502 return OK;
4503}
4504
4505/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004506 * * number multiplication
4507 * / number division
4508 * % number modulo
4509 */
4510 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004511compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512{
4513 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004514 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004515 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004517 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004518 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004519 return FAIL;
4520
4521 /*
4522 * Repeat computing, until no "*", "/" or "%" is following.
4523 */
4524 for (;;)
4525 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004526 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004527 if (*op != '*' && *op != '/' && *op != '%')
4528 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004529 if (next != NULL)
4530 {
4531 *arg = next_line_from_context(cctx, TRUE);
4532 op = skipwhite(*arg);
4533 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004534
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004535 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004537 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004538 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004539 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004540 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004541 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004543 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004544 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004546
4547 if (ppconst->pp_used == ppconst_used + 2
4548 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4549 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004550 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004551 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4552 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4553 varnumber_T res = 0;
4554 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004556 // both are numbers: compute the result
4557 switch (*op)
4558 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004559 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004560 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004561 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004562 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004563 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004564 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004565 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004566 break;
4567 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004568 if (failed)
4569 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004570 tv1->vval.v_number = res;
4571 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004572 }
4573 else
4574 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004575 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004576 generate_two_op(cctx, op);
4577 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004578 }
4579
4580 return OK;
4581}
4582
4583/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004584 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 * - number subtraction
4586 * .. string concatenation
4587 */
4588 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004589compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004590{
4591 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004592 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004593 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004594 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595
4596 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004597 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 return FAIL;
4599
4600 /*
4601 * Repeat computing, until no "+", "-" or ".." is following.
4602 */
4603 for (;;)
4604 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004605 op = may_peek_next_line(cctx, *arg, &next);
4606 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607 break;
4608 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004609 if (next != NULL)
4610 {
4611 *arg = next_line_from_context(cctx, TRUE);
4612 op = skipwhite(*arg);
4613 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004615 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004617 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004618 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619 }
4620
Bram Moolenaare0de1712020-12-02 17:36:54 +01004621 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004622 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004623
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004624 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004625 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004626 return FAIL;
4627
Bram Moolenaara5565e42020-05-09 15:44:01 +02004628 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004629 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004630 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4631 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4632 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4633 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004635 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4636 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004637
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004638 // concat/subtract/add constant numbers
4639 if (*op == '+')
4640 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4641 else if (*op == '-')
4642 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4643 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004644 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004645 // concatenate constant strings
4646 char_u *s1 = tv1->vval.v_string;
4647 char_u *s2 = tv2->vval.v_string;
4648 size_t len1 = STRLEN(s1);
4649
4650 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4651 if (tv1->vval.v_string == NULL)
4652 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004653 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004654 return FAIL;
4655 }
4656 mch_memmove(tv1->vval.v_string, s1, len1);
4657 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004658 vim_free(s1);
4659 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004660 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004661 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004662 }
4663 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004664 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004665 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004666 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004667 if (*op == '.')
4668 {
4669 if (may_generate_2STRING(-2, cctx) == FAIL
4670 || may_generate_2STRING(-1, cctx) == FAIL)
4671 return FAIL;
4672 generate_instr_drop(cctx, ISN_CONCAT, 1);
4673 }
4674 else
4675 generate_two_op(cctx, op);
4676 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004677 }
4678
4679 return OK;
4680}
4681
4682/*
4683 * expr5a == expr5b
4684 * expr5a =~ expr5b
4685 * expr5a != expr5b
4686 * expr5a !~ expr5b
4687 * expr5a > expr5b
4688 * expr5a >= expr5b
4689 * expr5a < expr5b
4690 * expr5a <= expr5b
4691 * expr5a is expr5b
4692 * expr5a isnot expr5b
4693 *
4694 * Produces instructions:
4695 * EVAL expr5a Push result of "expr5a"
4696 * EVAL expr5b Push result of "expr5b"
4697 * COMPARE one of the compare instructions
4698 */
4699 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004700compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004701{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004702 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004703 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004704 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004707 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004708
4709 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004710 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004711 return FAIL;
4712
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004713 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004714 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715
4716 /*
4717 * If there is a comparative operator, use it.
4718 */
4719 if (type != EXPR_UNKNOWN)
4720 {
4721 int ic = FALSE; // Default: do not ignore case
4722
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004723 if (next != NULL)
4724 {
4725 *arg = next_line_from_context(cctx, TRUE);
4726 p = skipwhite(*arg);
4727 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004728 if (type_is && (p[len] == '?' || p[len] == '#'))
4729 {
4730 semsg(_(e_invexpr2), *arg);
4731 return FAIL;
4732 }
4733 // extra question mark appended: ignore case
4734 if (p[len] == '?')
4735 {
4736 ic = TRUE;
4737 ++len;
4738 }
4739 // extra '#' appended: match case (ignored)
4740 else if (p[len] == '#')
4741 ++len;
4742 // nothing appended: match case
4743
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004744 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004745 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004746 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004747 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004748 }
4749
4750 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004751 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004752 return FAIL;
4753
Bram Moolenaara5565e42020-05-09 15:44:01 +02004754 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004755 return FAIL;
4756
Bram Moolenaara5565e42020-05-09 15:44:01 +02004757 if (ppconst->pp_used == ppconst_used + 2)
4758 {
4759 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4760 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4761 int ret;
4762
4763 // Both sides are a constant, compute the result now.
4764 // First check for a valid combination of types, this is more
4765 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004766 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004767 ret = FAIL;
4768 else
4769 {
4770 ret = typval_compare(tv1, tv2, type, ic);
4771 tv1->v_type = VAR_BOOL;
4772 tv1->vval.v_number = tv1->vval.v_number
4773 ? VVAL_TRUE : VVAL_FALSE;
4774 clear_tv(tv2);
4775 --ppconst->pp_used;
4776 }
4777 return ret;
4778 }
4779
4780 generate_ppconst(cctx, ppconst);
4781 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004782 }
4783
4784 return OK;
4785}
4786
Bram Moolenaar7f141552020-05-09 17:35:53 +02004787static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4788
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004789/*
4790 * Compile || or &&.
4791 */
4792 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004793compile_and_or(
4794 char_u **arg,
4795 cctx_T *cctx,
4796 char *op,
4797 ppconst_T *ppconst,
4798 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004799{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004800 char_u *next;
4801 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004802 int opchar = *op;
4803
4804 if (p[0] == opchar && p[1] == opchar)
4805 {
4806 garray_T *instr = &cctx->ctx_instr;
4807 garray_T end_ga;
4808
4809 /*
4810 * Repeat until there is no following "||" or "&&"
4811 */
4812 ga_init2(&end_ga, sizeof(int), 10);
4813 while (p[0] == opchar && p[1] == opchar)
4814 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004815 long start_lnum = SOURCING_LNUM;
4816 int start_ctx_lnum = cctx->ctx_lnum;
4817 int save_lnum;
4818
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004819 if (next != NULL)
4820 {
4821 *arg = next_line_from_context(cctx, TRUE);
4822 p = skipwhite(*arg);
4823 }
4824
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004825 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4826 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004827 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02004828 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004829 return FAIL;
4830 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004831
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004832 // TODO: use ppconst if the value is a constant and check
4833 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004834 generate_ppconst(cctx, ppconst);
4835
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004836 // Every part must evaluate to a bool.
Bram Moolenaara7511c02021-04-03 21:47:07 +02004837 SOURCING_LNUM = start_lnum;
4838 save_lnum = cctx->ctx_lnum;
4839 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004840 if (bool_on_stack(cctx) == FAIL)
4841 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004842 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004843 ga_clear(&end_ga);
4844 return FAIL;
4845 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02004846 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004847
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004848 if (ga_grow(&end_ga, 1) == FAIL)
4849 {
4850 ga_clear(&end_ga);
4851 return FAIL;
4852 }
4853 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4854 ++end_ga.ga_len;
4855 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004856 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004857
4858 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004859 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004860 {
4861 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004862 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004863 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004864
Bram Moolenaara5565e42020-05-09 15:44:01 +02004865 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4866 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004867 {
4868 ga_clear(&end_ga);
4869 return FAIL;
4870 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004871
4872 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004873 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004874 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004875
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004876 // Every part must evaluate to a bool.
4877 if (bool_on_stack(cctx) == FAIL)
4878 {
4879 ga_clear(&end_ga);
4880 return FAIL;
4881 }
4882
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004883 // Fill in the end label in all jumps.
4884 while (end_ga.ga_len > 0)
4885 {
4886 isn_T *isn;
4887
4888 --end_ga.ga_len;
4889 isn = ((isn_T *)instr->ga_data)
4890 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4891 isn->isn_arg.jump.jump_where = instr->ga_len;
4892 }
4893 ga_clear(&end_ga);
4894 }
4895
4896 return OK;
4897}
4898
4899/*
4900 * expr4a && expr4a && expr4a logical AND
4901 *
4902 * Produces instructions:
4903 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004904 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004905 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004906 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004907 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004908 * EVAL expr4c Push result of "expr4c"
4909 * end:
4910 */
4911 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004912compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004913{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004914 int ppconst_used = ppconst->pp_used;
4915
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004916 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004917 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004918 return FAIL;
4919
4920 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004921 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004922}
4923
4924/*
4925 * expr3a || expr3b || expr3c logical OR
4926 *
4927 * Produces instructions:
4928 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004929 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004930 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004931 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004932 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004933 * EVAL expr3c Push result of "expr3c"
4934 * end:
4935 */
4936 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004937compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004939 int ppconst_used = ppconst->pp_used;
4940
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004942 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004943 return FAIL;
4944
4945 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004946 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004947}
4948
4949/*
4950 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004951 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004952 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004953 * JUMP_IF_FALSE alt jump if false
4954 * EVAL expr1a
4955 * JUMP_ALWAYS end
4956 * alt: EVAL expr1b
4957 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004958 *
4959 * Toplevel expression: expr2 ?? expr1
4960 * Produces instructions:
4961 * EVAL expr2 Push result of "expr2"
4962 * JUMP_AND_KEEP_IF_TRUE end jump if true
4963 * EVAL expr1
4964 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004965 */
4966 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004967compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004968{
4969 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004970 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004971 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004972
Bram Moolenaar3988f642020-08-27 22:43:03 +02004973 // Ignore all kinds of errors when not producing code.
4974 if (cctx->ctx_skip == SKIP_YES)
4975 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004976 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004977 return OK;
4978 }
4979
Bram Moolenaar61a89812020-05-07 16:58:17 +02004980 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004981 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004982 return FAIL;
4983
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004984 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004985 if (*p == '?')
4986 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004987 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004988 garray_T *instr = &cctx->ctx_instr;
4989 garray_T *stack = &cctx->ctx_type_stack;
4990 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004991 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004992 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004993 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004994 int has_const_expr = FALSE;
4995 int const_value = FALSE;
4996 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004997
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004998 if (next != NULL)
4999 {
5000 *arg = next_line_from_context(cctx, TRUE);
5001 p = skipwhite(*arg);
5002 }
5003
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005004 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005005 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005006 semsg(_(e_white_space_required_before_and_after_str_at_str),
5007 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005008 return FAIL;
5009 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005010
Bram Moolenaara5565e42020-05-09 15:44:01 +02005011 if (ppconst->pp_used == ppconst_used + 1)
5012 {
5013 // the condition is a constant, we know whether the ? or the :
5014 // expression is to be evaluated.
5015 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005016 if (op_falsy)
5017 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5018 else
5019 {
5020 int error = FALSE;
5021
5022 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5023 &error);
5024 if (error)
5025 return FAIL;
5026 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005027 cctx->ctx_skip = save_skip == SKIP_YES ||
5028 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5029
5030 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5031 // "left ?? right" and "left" is truthy: produce "left"
5032 generate_ppconst(cctx, ppconst);
5033 else
5034 {
5035 clear_tv(&ppconst->pp_tv[ppconst_used]);
5036 --ppconst->pp_used;
5037 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005038 }
5039 else
5040 {
5041 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005042 if (op_falsy)
5043 end_idx = instr->ga_len;
5044 generate_JUMP(cctx, op_falsy
5045 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5046 if (op_falsy)
5047 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005048 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005049
5050 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005051 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005052 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005053 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005054 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005055
Bram Moolenaara5565e42020-05-09 15:44:01 +02005056 if (!has_const_expr)
5057 {
5058 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005059
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005060 if (!op_falsy)
5061 {
5062 // remember the type and drop it
5063 --stack->ga_len;
5064 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005065
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005066 end_idx = instr->ga_len;
5067 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005068
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005069 // jump here from JUMP_IF_FALSE
5070 isn = ((isn_T *)instr->ga_data) + alt_idx;
5071 isn->isn_arg.jump.jump_where = instr->ga_len;
5072 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005073 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005074
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005075 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005076 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005077 // Check for the ":".
5078 p = may_peek_next_line(cctx, *arg, &next);
5079 if (*p != ':')
5080 {
5081 emsg(_(e_missing_colon));
5082 return FAIL;
5083 }
5084 if (next != NULL)
5085 {
5086 *arg = next_line_from_context(cctx, TRUE);
5087 p = skipwhite(*arg);
5088 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005089
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005090 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5091 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005092 semsg(_(e_white_space_required_before_and_after_str_at_str),
5093 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005094 return FAIL;
5095 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005096
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005097 // evaluate the third expression
5098 if (has_const_expr)
5099 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005100 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005101 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005102 return FAIL;
5103 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5104 return FAIL;
5105 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005106
Bram Moolenaara5565e42020-05-09 15:44:01 +02005107 if (!has_const_expr)
5108 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005109 type_T **typep;
5110
Bram Moolenaara5565e42020-05-09 15:44:01 +02005111 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005112
Bram Moolenaara5565e42020-05-09 15:44:01 +02005113 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005114 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5115 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005116
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005117 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005118 isn = ((isn_T *)instr->ga_data) + end_idx;
5119 isn->isn_arg.jump.jump_where = instr->ga_len;
5120 }
5121
5122 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005123 }
5124 return OK;
5125}
5126
5127/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005128 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005129 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5130 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005131 */
5132 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005133compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005134{
5135 ppconst_T ppconst;
5136
5137 CLEAR_FIELD(ppconst);
5138 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5139 {
5140 clear_ppconst(&ppconst);
5141 return FAIL;
5142 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005143 if (is_const != NULL)
5144 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005145 if (generate_ppconst(cctx, &ppconst) == FAIL)
5146 return FAIL;
5147 return OK;
5148}
5149
5150/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005151 * Toplevel expression.
5152 */
5153 static int
5154compile_expr0(char_u **arg, cctx_T *cctx)
5155{
5156 return compile_expr0_ext(arg, cctx, NULL);
5157}
5158
5159/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005160 * compile "return [expr]"
5161 */
5162 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005163compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005164{
5165 char_u *p = arg;
5166 garray_T *stack = &cctx->ctx_type_stack;
5167 type_T *stack_type;
5168
5169 if (*p != NUL && *p != '|' && *p != '\n')
5170 {
5171 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02005172 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005173 return NULL;
5174
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005175 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005176 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005177 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01005178 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005179 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5180 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005181 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005182 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005183 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005184 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005185 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005186 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5187 && stack_type->tt_type != VAR_VOID
5188 && stack_type->tt_type != VAR_UNKNOWN)
5189 {
5190 emsg(_(e_returning_value_in_function_without_return_type));
5191 return NULL;
5192 }
5193 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005194 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005195 return NULL;
5196 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005197 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005198 }
5199 else
5200 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005201 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005202 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005203 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5204 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005205 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005206 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005207 return NULL;
5208 }
5209
5210 // No argument, return zero.
5211 generate_PUSHNR(cctx, 0);
5212 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005213
5214 // Undo any command modifiers.
5215 generate_undo_cmdmods(cctx);
5216
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005217 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005218 return NULL;
5219
5220 // "return val | endif" is possible
5221 return skipwhite(p);
5222}
5223
5224/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005225 * Get a line from the compilation context, compatible with exarg_T getline().
5226 * Return a pointer to the line in allocated memory.
5227 * Return NULL for end-of-file or some error.
5228 */
5229 static char_u *
5230exarg_getline(
5231 int c UNUSED,
5232 void *cookie,
5233 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005234 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005235{
5236 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005237 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005238
Bram Moolenaar66250c92020-08-20 15:02:42 +02005239 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005240 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005241 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005242 return NULL;
5243 ++cctx->ctx_lnum;
5244 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5245 // Comment lines result in NULL pointers, skip them.
5246 if (p != NULL)
5247 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005248 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005249}
5250
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005251 void
5252fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5253{
5254 eap->getline = exarg_getline;
5255 eap->cookie = cctx;
5256}
5257
Bram Moolenaar04b12692020-05-04 23:24:44 +02005258/*
5259 * Compile a nested :def command.
5260 */
5261 static char_u *
5262compile_nested_function(exarg_T *eap, cctx_T *cctx)
5263{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005264 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005265 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005266 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005267 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005268 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005269 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005270
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005271 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005272 {
5273 emsg(_(e_cannot_use_bang_with_nested_def));
5274 return NULL;
5275 }
5276
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005277 if (*name_start == '/')
5278 {
5279 name_end = skip_regexp(name_start + 1, '/', TRUE);
5280 if (*name_end == '/')
5281 ++name_end;
5282 eap->nextcmd = check_nextcmd(name_end);
5283 }
5284 if (name_end == name_start || *skipwhite(name_end) != '(')
5285 {
5286 if (!ends_excmd2(name_start, name_end))
5287 {
5288 semsg(_(e_invalid_command_str), eap->cmd);
5289 return NULL;
5290 }
5291
5292 // "def" or "def Name": list functions
5293 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5294 return NULL;
5295 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5296 }
5297
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005298 // Only g:Func() can use a namespace.
5299 if (name_start[1] == ':' && !is_global)
5300 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005301 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005302 return NULL;
5303 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005304 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005305 return NULL;
5306
Bram Moolenaar04b12692020-05-04 23:24:44 +02005307 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005308 fill_exarg_from_cctx(eap, cctx);
5309
Bram Moolenaar04b12692020-05-04 23:24:44 +02005310 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005311 lambda_name = vim_strsave(get_lambda_name());
5312 if (lambda_name == NULL)
5313 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005314 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005315
Bram Moolenaar822ba242020-05-24 23:00:18 +02005316 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005317 {
5318 r = eap->skip ? OK : FAIL;
5319 goto theend;
5320 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005321
5322 // copy over the block scope IDs before compiling
5323 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5324 {
5325 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5326
5327 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5328 if (ufunc->uf_block_ids != NULL)
5329 {
5330 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5331 sizeof(int) * block_depth);
5332 ufunc->uf_block_depth = block_depth;
5333 }
5334 }
5335
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005336 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5337 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005338 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005339 {
5340 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005341 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005342 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005343
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005344 if (is_global)
5345 {
5346 char_u *func_name = vim_strnsave(name_start + 2,
5347 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005348
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005349 if (func_name == NULL)
5350 r = FAIL;
5351 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005352 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005353 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005354 lambda_name = NULL;
5355 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005356 }
5357 else
5358 {
5359 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005360 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005361 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005362
Bram Moolenaareef21022020-08-01 22:16:43 +02005363 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005364 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005365 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005366 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005367 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5368 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005369 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005370
5371theend:
5372 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005373 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005374}
5375
5376/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005377 * Return the length of an assignment operator, or zero if there isn't one.
5378 */
5379 int
5380assignment_len(char_u *p, int *heredoc)
5381{
5382 if (*p == '=')
5383 {
5384 if (p[1] == '<' && p[2] == '<')
5385 {
5386 *heredoc = TRUE;
5387 return 3;
5388 }
5389 return 1;
5390 }
5391 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5392 return 2;
5393 if (STRNCMP(p, "..=", 3) == 0)
5394 return 3;
5395 return 0;
5396}
5397
5398// words that cannot be used as a variable
5399static char *reserved[] = {
5400 "true",
5401 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005402 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005403 NULL
5404};
5405
Bram Moolenaar752fc692021-01-04 21:57:11 +01005406// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005407typedef enum {
5408 dest_local,
5409 dest_option,
5410 dest_env,
5411 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005412 dest_buffer,
5413 dest_window,
5414 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005415 dest_vimvar,
5416 dest_script,
5417 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005418 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005419} assign_dest_T;
5420
Bram Moolenaar752fc692021-01-04 21:57:11 +01005421// Used by compile_lhs() to store information about the LHS of an assignment
5422// and one argument of ":unlet" with an index.
5423typedef struct {
5424 assign_dest_T lhs_dest; // type of destination
5425
5426 char_u *lhs_name; // allocated name including
5427 // "[expr]" or ".name".
5428 size_t lhs_varlen; // length of the variable without
5429 // "[expr]" or ".name"
5430 char_u *lhs_dest_end; // end of the destination, including
5431 // "[expr]" or ".name".
5432
5433 int lhs_has_index; // has "[expr]" or ".name"
5434
5435 int lhs_new_local; // create new local variable
5436 int lhs_opt_flags; // for when destination is an option
5437 int lhs_vimvaridx; // for when destination is a v:var
5438
5439 lvar_T lhs_local_lvar; // used for existing local destination
5440 lvar_T lhs_arg_lvar; // used for argument destination
5441 lvar_T *lhs_lvar; // points to destination lvar
5442 int lhs_scriptvar_sid;
5443 int lhs_scriptvar_idx;
5444
5445 int lhs_has_type; // type was specified
5446 type_T *lhs_type;
5447 type_T *lhs_member_type;
5448} lhs_T;
5449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005450/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005451 * Generate the load instruction for "name".
5452 */
5453 static void
5454generate_loadvar(
5455 cctx_T *cctx,
5456 assign_dest_T dest,
5457 char_u *name,
5458 lvar_T *lvar,
5459 type_T *type)
5460{
5461 switch (dest)
5462 {
5463 case dest_option:
5464 // TODO: check the option exists
5465 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5466 break;
5467 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005468 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5469 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5470 else
5471 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005472 break;
5473 case dest_buffer:
5474 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5475 break;
5476 case dest_window:
5477 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5478 break;
5479 case dest_tab:
5480 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5481 break;
5482 case dest_script:
5483 compile_load_scriptvar(cctx,
5484 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5485 break;
5486 case dest_env:
5487 // Include $ in the name here
5488 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5489 break;
5490 case dest_reg:
5491 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5492 break;
5493 case dest_vimvar:
5494 generate_LOADV(cctx, name + 2, TRUE);
5495 break;
5496 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005497 if (lvar->lv_from_outer > 0)
5498 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5499 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005500 else
5501 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5502 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005503 case dest_expr:
5504 // list or dict value should already be on the stack.
5505 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005506 }
5507}
5508
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005509/*
5510 * Skip over "[expr]" or ".member".
5511 * Does not check for any errors.
5512 */
5513 static char_u *
5514skip_index(char_u *start)
5515{
5516 char_u *p = start;
5517
5518 if (*p == '[')
5519 {
5520 p = skipwhite(p + 1);
5521 (void)skip_expr(&p, NULL);
5522 p = skipwhite(p);
5523 if (*p == ']')
5524 return p + 1;
5525 return p;
5526 }
5527 // if (*p == '.')
5528 return to_name_end(p + 1, TRUE);
5529}
5530
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005531 void
5532vim9_declare_error(char_u *name)
5533{
5534 char *scope = "";
5535
5536 switch (*name)
5537 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005538 case 'g': scope = _("global"); break;
5539 case 'b': scope = _("buffer"); break;
5540 case 'w': scope = _("window"); break;
5541 case 't': scope = _("tab"); break;
5542 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005543 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005544 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005545 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005546 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005547 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005548 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005549 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005550 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005551 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005552}
5553
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005554/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005555 * For one assignment figure out the type of destination. Return it in "dest".
5556 * When not recognized "dest" is not set.
5557 * For an option "opt_flags" is set.
5558 * For a v:var "vimvaridx" is set.
5559 * "type" is set to the destination type if known, unchanted otherwise.
5560 * Return FAIL if an error message was given.
5561 */
5562 static int
5563get_var_dest(
5564 char_u *name,
5565 assign_dest_T *dest,
5566 int cmdidx,
5567 int *opt_flags,
5568 int *vimvaridx,
5569 type_T **type,
5570 cctx_T *cctx)
5571{
5572 char_u *p;
5573
5574 if (*name == '&')
5575 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005576 int cc;
5577 long numval;
5578 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005579
5580 *dest = dest_option;
5581 if (cmdidx == CMD_final || cmdidx == CMD_const)
5582 {
5583 emsg(_(e_const_option));
5584 return FAIL;
5585 }
5586 p = name;
5587 p = find_option_end(&p, opt_flags);
5588 if (p == NULL)
5589 {
5590 // cannot happen?
5591 emsg(_(e_letunexp));
5592 return FAIL;
5593 }
5594 cc = *p;
5595 *p = NUL;
5596 opt_type = get_option_value(skip_option_env_lead(name),
5597 &numval, NULL, *opt_flags);
5598 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005599 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005600 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005601 case gov_unknown:
5602 semsg(_(e_unknown_option), name);
5603 return FAIL;
5604 case gov_string:
5605 case gov_hidden_string:
5606 *type = &t_string;
5607 break;
5608 case gov_bool:
5609 case gov_hidden_bool:
5610 *type = &t_bool;
5611 break;
5612 case gov_number:
5613 case gov_hidden_number:
5614 *type = &t_number;
5615 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005616 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005617 }
5618 else if (*name == '$')
5619 {
5620 *dest = dest_env;
5621 *type = &t_string;
5622 }
5623 else if (*name == '@')
5624 {
5625 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5626 {
5627 emsg_invreg(name[1]);
5628 return FAIL;
5629 }
5630 *dest = dest_reg;
5631 *type = &t_string;
5632 }
5633 else if (STRNCMP(name, "g:", 2) == 0)
5634 {
5635 *dest = dest_global;
5636 }
5637 else if (STRNCMP(name, "b:", 2) == 0)
5638 {
5639 *dest = dest_buffer;
5640 }
5641 else if (STRNCMP(name, "w:", 2) == 0)
5642 {
5643 *dest = dest_window;
5644 }
5645 else if (STRNCMP(name, "t:", 2) == 0)
5646 {
5647 *dest = dest_tab;
5648 }
5649 else if (STRNCMP(name, "v:", 2) == 0)
5650 {
5651 typval_T *vtv;
5652 int di_flags;
5653
5654 *vimvaridx = find_vim_var(name + 2, &di_flags);
5655 if (*vimvaridx < 0)
5656 {
5657 semsg(_(e_variable_not_found_str), name);
5658 return FAIL;
5659 }
5660 // We use the current value of "sandbox" here, is that OK?
5661 if (var_check_ro(di_flags, name, FALSE))
5662 return FAIL;
5663 *dest = dest_vimvar;
5664 vtv = get_vim_var_tv(*vimvaridx);
5665 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5666 }
5667 return OK;
5668}
5669
5670/*
5671 * Generate a STORE instruction for "dest", not being "dest_local".
5672 * Return FAIL when out of memory.
5673 */
5674 static int
5675generate_store_var(
5676 cctx_T *cctx,
5677 assign_dest_T dest,
5678 int opt_flags,
5679 int vimvaridx,
5680 int scriptvar_idx,
5681 int scriptvar_sid,
5682 type_T *type,
5683 char_u *name)
5684{
5685 switch (dest)
5686 {
5687 case dest_option:
5688 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5689 opt_flags);
5690 case dest_global:
5691 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005692 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5693 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005694 case dest_buffer:
5695 // include b: with the name, easier to execute that way
5696 return generate_STORE(cctx, ISN_STOREB, 0, name);
5697 case dest_window:
5698 // include w: with the name, easier to execute that way
5699 return generate_STORE(cctx, ISN_STOREW, 0, name);
5700 case dest_tab:
5701 // include t: with the name, easier to execute that way
5702 return generate_STORE(cctx, ISN_STORET, 0, name);
5703 case dest_env:
5704 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5705 case dest_reg:
5706 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5707 case dest_vimvar:
5708 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5709 case dest_script:
5710 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005711 // "s:" may be included in the name.
5712 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005713 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005714 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5715 scriptvar_sid, scriptvar_idx, type);
5716 case dest_local:
5717 case dest_expr:
5718 // cannot happen
5719 break;
5720 }
5721 return FAIL;
5722}
5723
Bram Moolenaar752fc692021-01-04 21:57:11 +01005724 static int
5725is_decl_command(int cmdidx)
5726{
5727 return cmdidx == CMD_let || cmdidx == CMD_var
5728 || cmdidx == CMD_final || cmdidx == CMD_const;
5729}
5730
5731/*
5732 * Figure out the LHS type and other properties for an assignment or one item
5733 * of ":unlet" with an index.
5734 * Returns OK or FAIL.
5735 */
5736 static int
5737compile_lhs(
5738 char_u *var_start,
5739 lhs_T *lhs,
5740 int cmdidx,
5741 int heredoc,
5742 int oplen,
5743 cctx_T *cctx)
5744{
5745 char_u *var_end;
5746 int is_decl = is_decl_command(cmdidx);
5747
5748 CLEAR_POINTER(lhs);
5749 lhs->lhs_dest = dest_local;
5750 lhs->lhs_vimvaridx = -1;
5751 lhs->lhs_scriptvar_idx = -1;
5752
5753 // "dest_end" is the end of the destination, including "[expr]" or
5754 // ".name".
5755 // "var_end" is the end of the variable/option/etc. name.
5756 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5757 if (*var_start == '@')
5758 var_end = var_start + 2;
5759 else
5760 {
5761 // skip over the leading "&", "&l:", "&g:" and "$"
5762 var_end = skip_option_env_lead(var_start);
5763 var_end = to_name_end(var_end, TRUE);
5764 }
5765
5766 // "a: type" is declaring variable "a" with a type, not dict "a:".
5767 if (is_decl && lhs->lhs_dest_end == var_start + 2
5768 && lhs->lhs_dest_end[-1] == ':')
5769 --lhs->lhs_dest_end;
5770 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5771 --var_end;
5772
5773 // compute the length of the destination without "[expr]" or ".name"
5774 lhs->lhs_varlen = var_end - var_start;
5775 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5776 if (lhs->lhs_name == NULL)
5777 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005778
5779 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5780 // Something follows after the variable: "var[idx]" or "var.key".
5781 lhs->lhs_has_index = TRUE;
5782
Bram Moolenaar752fc692021-01-04 21:57:11 +01005783 if (heredoc)
5784 lhs->lhs_type = &t_list_string;
5785 else
5786 lhs->lhs_type = &t_any;
5787
5788 if (cctx->ctx_skip != SKIP_YES)
5789 {
5790 int declare_error = FALSE;
5791
5792 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5793 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5794 &lhs->lhs_type, cctx) == FAIL)
5795 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02005796 if (lhs->lhs_dest != dest_local
5797 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005798 {
5799 // Specific kind of variable recognized.
5800 declare_error = is_decl;
5801 }
5802 else
5803 {
5804 int idx;
5805
5806 // No specific kind of variable recognized, just a name.
5807 for (idx = 0; reserved[idx] != NULL; ++idx)
5808 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5809 {
5810 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5811 return FAIL;
5812 }
5813
5814
5815 if (lookup_local(var_start, lhs->lhs_varlen,
5816 &lhs->lhs_local_lvar, cctx) == OK)
5817 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5818 else
5819 {
5820 CLEAR_FIELD(lhs->lhs_arg_lvar);
5821 if (arg_exists(var_start, lhs->lhs_varlen,
5822 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5823 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5824 {
5825 if (is_decl)
5826 {
5827 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5828 return FAIL;
5829 }
5830 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5831 }
5832 }
5833 if (lhs->lhs_lvar != NULL)
5834 {
5835 if (is_decl)
5836 {
5837 semsg(_(e_variable_already_declared), lhs->lhs_name);
5838 return FAIL;
5839 }
5840 }
5841 else
5842 {
5843 int script_namespace = lhs->lhs_varlen > 1
5844 && STRNCMP(var_start, "s:", 2) == 0;
5845 int script_var = (script_namespace
5846 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
5847 FALSE, cctx)
5848 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005849 FALSE, cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005850 imported_T *import =
5851 find_imported(var_start, lhs->lhs_varlen, cctx);
5852
5853 if (script_namespace || script_var || import != NULL)
5854 {
5855 char_u *rawname = lhs->lhs_name
5856 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5857
5858 if (is_decl)
5859 {
5860 if (script_namespace)
5861 semsg(_(e_cannot_declare_script_variable_in_function),
5862 lhs->lhs_name);
5863 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005864 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01005865 lhs->lhs_name);
5866 return FAIL;
5867 }
5868 else if (cctx->ctx_ufunc->uf_script_ctx_version
5869 == SCRIPT_VERSION_VIM9
5870 && script_namespace
5871 && !script_var && import == NULL)
5872 {
5873 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5874 return FAIL;
5875 }
5876
5877 lhs->lhs_dest = dest_script;
5878
5879 // existing script-local variables should have a type
5880 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5881 if (import != NULL)
5882 lhs->lhs_scriptvar_sid = import->imp_sid;
5883 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5884 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005885 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005886 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005887 lhs->lhs_scriptvar_sid, rawname,
5888 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5889 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005890 if (lhs->lhs_scriptvar_idx >= 0)
5891 {
5892 scriptitem_T *si = SCRIPT_ITEM(
5893 lhs->lhs_scriptvar_sid);
5894 svar_T *sv =
5895 ((svar_T *)si->sn_var_vals.ga_data)
5896 + lhs->lhs_scriptvar_idx;
5897 lhs->lhs_type = sv->sv_type;
5898 }
5899 }
5900 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005901 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005902 == FAIL)
5903 return FAIL;
5904 }
5905 }
5906
5907 if (declare_error)
5908 {
5909 vim9_declare_error(lhs->lhs_name);
5910 return FAIL;
5911 }
5912 }
5913
5914 // handle "a:name" as a name, not index "name" on "a"
5915 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5916 var_end = lhs->lhs_dest_end;
5917
5918 if (lhs->lhs_dest != dest_option)
5919 {
5920 if (is_decl && *var_end == ':')
5921 {
5922 char_u *p;
5923
5924 // parse optional type: "let var: type = expr"
5925 if (!VIM_ISWHITE(var_end[1]))
5926 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01005927 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005928 return FAIL;
5929 }
5930 p = skipwhite(var_end + 1);
5931 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5932 if (lhs->lhs_type == NULL)
5933 return FAIL;
5934 lhs->lhs_has_type = TRUE;
5935 }
5936 else if (lhs->lhs_lvar != NULL)
5937 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5938 }
5939
Bram Moolenaare42939a2021-04-05 17:11:17 +02005940 if (oplen == 3 && !heredoc
5941 && lhs->lhs_dest != dest_global
5942 && !lhs->lhs_has_index
5943 && lhs->lhs_type->tt_type != VAR_STRING
5944 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005945 {
5946 emsg(_(e_can_only_concatenate_to_string));
5947 return FAIL;
5948 }
5949
5950 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5951 && cctx->ctx_skip != SKIP_YES)
5952 {
5953 if (oplen > 1 && !heredoc)
5954 {
5955 // +=, /=, etc. require an existing variable
5956 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5957 return FAIL;
5958 }
5959 if (!is_decl)
5960 {
5961 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5962 return FAIL;
5963 }
5964
Bram Moolenaar3f327882021-03-17 20:56:38 +01005965 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005966 if ((lhs->lhs_type->tt_type == VAR_FUNC
5967 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01005968 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01005969 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01005970
5971 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005972 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5973 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5974 if (lhs->lhs_lvar == NULL)
5975 return FAIL;
5976 lhs->lhs_new_local = TRUE;
5977 }
5978
5979 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01005980 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005981 {
5982 // Something follows after the variable: "var[idx]" or "var.key".
5983 // TODO: should we also handle "->func()" here?
5984 if (is_decl)
5985 {
5986 emsg(_(e_cannot_use_index_when_declaring_variable));
5987 return FAIL;
5988 }
5989
5990 if (var_start[lhs->lhs_varlen] == '['
5991 || var_start[lhs->lhs_varlen] == '.')
5992 {
5993 char_u *after = var_start + lhs->lhs_varlen;
5994 char_u *p;
5995
5996 // Only the last index is used below, if there are others
5997 // before it generate code for the expression. Thus for
5998 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5999 for (;;)
6000 {
6001 p = skip_index(after);
6002 if (*p != '[' && *p != '.')
6003 break;
6004 after = p;
6005 }
6006 if (after > var_start + lhs->lhs_varlen)
6007 {
6008 lhs->lhs_varlen = after - var_start;
6009 lhs->lhs_dest = dest_expr;
6010 // We don't know the type before evaluating the expression,
6011 // use "any" until then.
6012 lhs->lhs_type = &t_any;
6013 }
6014
Bram Moolenaar752fc692021-01-04 21:57:11 +01006015 if (lhs->lhs_type->tt_member == NULL)
6016 lhs->lhs_member_type = &t_any;
6017 else
6018 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6019 }
6020 else
6021 {
6022 semsg("Not supported yet: %s", var_start);
6023 return FAIL;
6024 }
6025 }
6026 return OK;
6027}
6028
6029/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006030 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6031 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006032 */
6033 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006034compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006035 char_u *var_start,
6036 lhs_T *lhs,
6037 int is_assign,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006038 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006039 cctx_T *cctx)
6040{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006041 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006042 char_u *p;
6043 int r = OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006044
Bram Moolenaar752fc692021-01-04 21:57:11 +01006045 p = var_start + varlen;
6046 if (*p == '[')
6047 {
6048 p = skipwhite(p + 1);
6049 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006050
6051 if (r == OK && *skipwhite(p) == ':')
6052 {
6053 // unlet var[idx : idx]
6054 if (is_assign)
6055 {
6056 semsg(_(e_cannot_use_range_with_assignment_str), p);
6057 return FAIL;
6058 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006059 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006060 p = skipwhite(p);
6061 if (!IS_WHITE_OR_NUL(p[-1]) || !IS_WHITE_OR_NUL(p[1]))
6062 {
6063 semsg(_(e_white_space_required_before_and_after_str_at_str),
6064 ":", p);
6065 return FAIL;
6066 }
6067 p = skipwhite(p + 1);
6068 r = compile_expr0(&p, cctx);
6069 }
6070
Bram Moolenaar752fc692021-01-04 21:57:11 +01006071 if (r == OK && *skipwhite(p) != ']')
6072 {
6073 // this should not happen
6074 emsg(_(e_missbrac));
6075 r = FAIL;
6076 }
6077 }
6078 else // if (*p == '.')
6079 {
6080 char_u *key_end = to_name_end(p + 1, TRUE);
6081 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6082
6083 r = generate_PUSHS(cctx, key);
6084 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006085 return r;
6086}
6087
6088/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006089 * For a LHS with an index, load the variable to be indexed.
6090 */
6091 static int
6092compile_load_lhs(
6093 lhs_T *lhs,
6094 char_u *var_start,
6095 type_T *rhs_type,
6096 cctx_T *cctx)
6097{
6098 if (lhs->lhs_dest == dest_expr)
6099 {
6100 size_t varlen = lhs->lhs_varlen;
6101 int c = var_start[varlen];
6102 char_u *p = var_start;
6103 garray_T *stack = &cctx->ctx_type_stack;
6104
6105 // Evaluate "ll[expr]" of "ll[expr][idx]"
6106 var_start[varlen] = NUL;
6107 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6108 {
6109 // this should not happen
6110 emsg(_(e_missbrac));
6111 return FAIL;
6112 }
6113 var_start[varlen] = c;
6114
6115 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6116 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6117 // now we can properly check the type
6118 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6119 && rhs_type != &t_void
6120 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6121 FALSE, FALSE) == FAIL)
6122 return FAIL;
6123 }
6124 else
6125 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6126 lhs->lhs_lvar, lhs->lhs_type);
6127 return OK;
6128}
6129
6130/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006131 * Assignment to a list or dict member, or ":unlet" for the item, using the
6132 * information in "lhs".
6133 * Returns OK or FAIL.
6134 */
6135 static int
6136compile_assign_unlet(
6137 char_u *var_start,
6138 lhs_T *lhs,
6139 int is_assign,
6140 type_T *rhs_type,
6141 cctx_T *cctx)
6142{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006143 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006144 garray_T *stack = &cctx->ctx_type_stack;
6145 int range = FALSE;
6146
6147 if (compile_assign_index(var_start, lhs, is_assign, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006148 return FAIL;
6149
6150 if (lhs->lhs_type == &t_any)
6151 {
6152 // Index on variable of unknown type: check at runtime.
6153 dest_type = VAR_ANY;
6154 }
6155 else
6156 {
6157 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006158 if (dest_type == VAR_DICT && range)
6159 {
6160 emsg(e_cannot_use_range_with_dictionary);
6161 return FAIL;
6162 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006163 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
6164 return FAIL;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006165 if (dest_type == VAR_LIST)
6166 {
6167 if (range
6168 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 2],
Bram Moolenaarf30a14d2021-01-17 21:51:24 +01006169 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006170 return FAIL;
6171 if (need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
6172 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
6173 return FAIL;
6174 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006175 }
6176
6177 // Load the dict or list. On the stack we then have:
6178 // - value (for assignment, not for :unlet)
6179 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006180 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006181 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006182 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6183 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006184
6185 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
6186 {
6187 if (is_assign)
6188 {
6189 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
6190
6191 if (isn == NULL)
6192 return FAIL;
6193 isn->isn_arg.vartype = dest_type;
6194 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006195 else if (range)
6196 {
6197 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6198 return FAIL;
6199 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006200 else
6201 {
6202 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6203 return FAIL;
6204 }
6205 }
6206 else
6207 {
6208 emsg(_(e_indexable_type_required));
6209 return FAIL;
6210 }
6211
6212 return OK;
6213}
6214
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006215/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006216 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006217 * "let name"
6218 * "var name = expr"
6219 * "final name = expr"
6220 * "const name = expr"
6221 * "name = expr"
6222 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006223 * Return NULL for an error.
6224 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006225 */
6226 static char_u *
6227compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6228{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006229 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006230 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006231 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006232 char_u *ret = NULL;
6233 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006234 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006235 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006236 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006237 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006238 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006239 int oplen = 0;
6240 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006241 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006242 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006243 int is_decl = is_decl_command(cmdidx);
6244 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006245 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006246
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006247 // Skip over the "var" or "[var, var]" to get to any "=".
6248 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6249 if (p == NULL)
6250 return *arg == '[' ? arg : NULL;
6251
6252 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006253 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006254 // TODO: should we allow this, and figure out type inference from list
6255 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006256 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006257 return NULL;
6258 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006259 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006260
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006261 sp = p;
6262 p = skipwhite(p);
6263 op = p;
6264 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006265
6266 if (var_count > 0 && oplen == 0)
6267 // can be something like "[1, 2]->func()"
6268 return arg;
6269
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006270 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006271 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006272 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006273 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006274 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006275
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006276 if (heredoc)
6277 {
6278 list_T *l;
6279 listitem_T *li;
6280
6281 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006282 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006283 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006284 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006285 if (l == NULL)
6286 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006287
Bram Moolenaar078269b2020-09-21 20:35:55 +02006288 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006289 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006290 // Push each line and the create the list.
6291 FOR_ALL_LIST_ITEMS(l, li)
6292 {
6293 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6294 li->li_tv.vval.v_string = NULL;
6295 }
6296 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006297 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006298 list_free(l);
6299 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006300 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006301 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006302 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006303 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006304 char_u *wp;
6305
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006306 // for "[var, var] = expr" evaluate the expression here, loop over the
6307 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006308 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006309
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006310 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006311 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006312 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006313 if (compile_expr0(&p, cctx) == FAIL)
6314 return NULL;
6315 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006316
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006317 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006318 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006319 type_T *stacktype;
6320
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006321 stacktype = stack->ga_len == 0 ? &t_void
6322 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006323 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006324 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006325 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006326 goto theend;
6327 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006328 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006329 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006330 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006331 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006332 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6333 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006334 if (stacktype->tt_member != NULL)
6335 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006336 }
6337 }
6338
6339 /*
6340 * Loop over variables in "[var, var] = expr".
6341 * For "var = expr" and "let var: type" this is done only once.
6342 */
6343 if (var_count > 0)
6344 var_start = skipwhite(arg + 1); // skip over the "["
6345 else
6346 var_start = arg;
6347 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6348 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006349 int instr_count = -1;
6350
Bram Moolenaar752fc692021-01-04 21:57:11 +01006351 vim_free(lhs.lhs_name);
6352
6353 /*
6354 * Figure out the LHS type and other properties.
6355 */
6356 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6357 goto theend;
6358
6359 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006360 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006361 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006362 goto theend;
6363 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006364 if (!is_decl && lhs.lhs_lvar != NULL
6365 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006366 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006367 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006368 goto theend;
6369 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006370
6371 if (!heredoc)
6372 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006373 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006374 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006375 if (oplen > 0 && var_count == 0)
6376 {
6377 // skip over the "=" and the expression
6378 p = skipwhite(op + oplen);
6379 compile_expr0(&p, cctx);
6380 }
6381 }
6382 else if (oplen > 0)
6383 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006384 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006385 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006386
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006387 // For "var = expr" evaluate the expression.
6388 if (var_count == 0)
6389 {
6390 int r;
6391
6392 // for "+=", "*=", "..=" etc. first load the current value
6393 if (*op != '=')
6394 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006395 compile_load_lhs(&lhs, var_start, NULL, cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006396
Bram Moolenaar752fc692021-01-04 21:57:11 +01006397 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006398 {
Bram Moolenaare42939a2021-04-05 17:11:17 +02006399 int range = FALSE;
6400
6401 // Get member from list or dict. First compile the
6402 // index value.
6403 if (compile_assign_index(var_start, &lhs,
6404 TRUE, &range, cctx) == FAIL)
6405 goto theend;
6406
6407 // Get the member.
6408 if (compile_member(FALSE, cctx) == FAIL)
6409 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006410 }
6411 }
6412
6413 // Compile the expression. Temporarily hide the new local
6414 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006415 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006416 --cctx->ctx_locals.ga_len;
6417 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006418 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006419 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006420 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006421 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006422 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006423 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006424 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006425 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006426 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006427 ++cctx->ctx_locals.ga_len;
6428 if (r == FAIL)
6429 goto theend;
6430 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006431 else if (semicolon && var_idx == var_count - 1)
6432 {
6433 // For "[var; var] = expr" get the rest of the list
6434 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6435 goto theend;
6436 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006437 else
6438 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006439 // For "[var, var] = expr" get the "var_idx" item from the
6440 // list.
6441 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006442 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006443 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006444
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006445 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006446 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006447 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006448 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006449 if ((rhs_type->tt_type == VAR_FUNC
6450 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006451 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006452 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006453 goto theend;
6454
Bram Moolenaar752fc692021-01-04 21:57:11 +01006455 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006456 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006457 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006458 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006459 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006460 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006461 }
6462 else
6463 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006464 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006465 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006466 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006467 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006468 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006469 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006470 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006471 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006472 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006473 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006474 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006475 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006476 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006477 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006478 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006479
Bram Moolenaar77709b12021-04-03 21:01:01 +02006480 // Without operator check type here, otherwise below.
6481 // Use the line number of the assignment.
6482 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006483 if (lhs.lhs_has_index)
6484 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006485 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006486 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006487 goto theend;
6488 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006489 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006490 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006491 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006492 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006493 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006494 else if (cmdidx == CMD_final)
6495 {
6496 emsg(_(e_final_requires_a_value));
6497 goto theend;
6498 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006499 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006500 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006501 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006502 goto theend;
6503 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006504 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006505 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006506 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006507 goto theend;
6508 }
6509 else
6510 {
6511 // variables are always initialized
6512 if (ga_grow(instr, 1) == FAIL)
6513 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006514 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006515 {
6516 case VAR_BOOL:
6517 generate_PUSHBOOL(cctx, VVAL_FALSE);
6518 break;
6519 case VAR_FLOAT:
6520#ifdef FEAT_FLOAT
6521 generate_PUSHF(cctx, 0.0);
6522#endif
6523 break;
6524 case VAR_STRING:
6525 generate_PUSHS(cctx, NULL);
6526 break;
6527 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006528 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006529 break;
6530 case VAR_FUNC:
6531 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6532 break;
6533 case VAR_LIST:
6534 generate_NEWLIST(cctx, 0);
6535 break;
6536 case VAR_DICT:
6537 generate_NEWDICT(cctx, 0);
6538 break;
6539 case VAR_JOB:
6540 generate_PUSHJOB(cctx, NULL);
6541 break;
6542 case VAR_CHANNEL:
6543 generate_PUSHCHANNEL(cctx, NULL);
6544 break;
6545 case VAR_NUMBER:
6546 case VAR_UNKNOWN:
6547 case VAR_ANY:
6548 case VAR_PARTIAL:
6549 case VAR_VOID:
6550 case VAR_SPECIAL: // cannot happen
6551 generate_PUSHNR(cctx, 0);
6552 break;
6553 }
6554 }
6555 if (var_count == 0)
6556 end = p;
6557 }
6558
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006559 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006560 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006561 break;
6562
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006563 if (oplen > 0 && *op != '=')
6564 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006565 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006566 type_T *stacktype;
6567
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006568 if (*op == '.')
6569 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006570 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006571 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006572 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006573 if (
6574#ifdef FEAT_FLOAT
6575 // If variable is float operation with number is OK.
6576 !(expected == &t_float && stacktype == &t_number) &&
6577#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006578 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006579 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006580 goto theend;
6581
6582 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006583 {
6584 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6585 goto theend;
6586 }
6587 else if (*op == '+')
6588 {
6589 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006590 operator_type(lhs.lhs_member_type, stacktype),
6591 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006592 goto theend;
6593 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006594 else if (generate_two_op(cctx, op) == FAIL)
6595 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006596 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006597
Bram Moolenaar752fc692021-01-04 21:57:11 +01006598 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006599 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006600 // Use the info in "lhs" to store the value at the index in the
6601 // list or dict.
6602 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6603 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006604 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006605 }
6606 else
6607 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006608 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02006609 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01006610 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006611 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006612 generate_LOCKCONST(cctx);
6613
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006614 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006615 && (lhs.lhs_type->tt_type == VAR_DICT
6616 || lhs.lhs_type->tt_type == VAR_LIST)
6617 && lhs.lhs_type->tt_member != NULL
6618 && lhs.lhs_type->tt_member != &t_any
6619 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006620 // Set the type in the list or dict, so that it can be checked,
6621 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006622 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006623
Bram Moolenaar752fc692021-01-04 21:57:11 +01006624 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006625 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006626 if (generate_store_var(cctx, lhs.lhs_dest,
6627 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6628 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6629 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006630 goto theend;
6631 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006632 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006633 {
6634 isn_T *isn = ((isn_T *)instr->ga_data)
6635 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006636
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006637 // optimization: turn "var = 123" from ISN_PUSHNR +
6638 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006639 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006640 && instr->ga_len == instr_count + 1
6641 && isn->isn_type == ISN_PUSHNR)
6642 {
6643 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006644
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006645 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006646 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006647 isn->isn_arg.storenr.stnr_val = val;
6648 if (stack->ga_len > 0)
6649 --stack->ga_len;
6650 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006651 else if (lhs.lhs_lvar->lv_from_outer > 0)
6652 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6653 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006654 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006655 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006656 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006657 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006658
6659 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006660 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006661 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006662
6663 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006664 if (var_count > 0 && !semicolon)
6665 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006666 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006667 goto theend;
6668 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006669
Bram Moolenaarb2097502020-07-19 17:17:02 +02006670 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006671
6672theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006673 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006674 return ret;
6675}
6676
6677/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006678 * Check for an assignment at "eap->cmd", compile it if found.
6679 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6680 */
6681 static int
6682may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6683{
6684 char_u *pskip;
6685 char_u *p;
6686
6687 // Assuming the command starts with a variable or function name,
6688 // find what follows.
6689 // Skip over "var.member", "var[idx]" and the like.
6690 // Also "&opt = val", "$ENV = val" and "@r = val".
6691 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6692 ? eap->cmd + 1 : eap->cmd;
6693 p = to_name_end(pskip, TRUE);
6694 if (p > eap->cmd && *p != NUL)
6695 {
6696 char_u *var_end;
6697 int oplen;
6698 int heredoc;
6699
6700 if (eap->cmd[0] == '@')
6701 var_end = eap->cmd + 2;
6702 else
6703 var_end = find_name_end(pskip, NULL, NULL,
6704 FNE_CHECK_START | FNE_INCL_BR);
6705 oplen = assignment_len(skipwhite(var_end), &heredoc);
6706 if (oplen > 0)
6707 {
6708 size_t len = p - eap->cmd;
6709
6710 // Recognize an assignment if we recognize the variable
6711 // name:
6712 // "g:var = expr"
6713 // "local = expr" where "local" is a local var.
6714 // "script = expr" where "script" is a script-local var.
6715 // "import = expr" where "import" is an imported var
6716 // "&opt = expr"
6717 // "$ENV = expr"
6718 // "@r = expr"
6719 if (*eap->cmd == '&'
6720 || *eap->cmd == '$'
6721 || *eap->cmd == '@'
6722 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01006723 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01006724 {
6725 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6726 if (*line == NULL || *line == eap->cmd)
6727 return FAIL;
6728 return OK;
6729 }
6730 }
6731 }
6732
6733 if (*eap->cmd == '[')
6734 {
6735 // [var, var] = expr
6736 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6737 if (*line == NULL)
6738 return FAIL;
6739 if (*line != eap->cmd)
6740 return OK;
6741 }
6742 return NOTDONE;
6743}
6744
6745/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006746 * Check if "name" can be "unlet".
6747 */
6748 int
6749check_vim9_unlet(char_u *name)
6750{
6751 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6752 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006753 // "unlet s:var" is allowed in legacy script.
6754 if (*name == 's' && !script_is_vim9())
6755 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006756 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006757 return FAIL;
6758 }
6759 return OK;
6760}
6761
6762/*
6763 * Callback passed to ex_unletlock().
6764 */
6765 static int
6766compile_unlet(
6767 lval_T *lvp,
6768 char_u *name_end,
6769 exarg_T *eap,
6770 int deep UNUSED,
6771 void *coookie)
6772{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006773 cctx_T *cctx = coookie;
6774 char_u *p = lvp->ll_name;
6775 int cc = *name_end;
6776 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006777
Bram Moolenaar752fc692021-01-04 21:57:11 +01006778 if (cctx->ctx_skip == SKIP_YES)
6779 return OK;
6780
6781 *name_end = NUL;
6782 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006783 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006784 // :unlet $ENV_VAR
6785 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6786 }
6787 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6788 {
6789 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006790
Bram Moolenaar752fc692021-01-04 21:57:11 +01006791 // This is similar to assigning: lookup the list/dict, compile the
6792 // idx/key. Then instead of storing the value unlet the item.
6793 // unlet {list}[idx]
6794 // unlet {dict}[key] dict.key
6795 //
6796 // Figure out the LHS type and other properties.
6797 //
6798 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6799
6800 // : unlet an indexed item
6801 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006802 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006803 iemsg("called compile_lhs() without an index");
6804 ret = FAIL;
6805 }
6806 else
6807 {
6808 // Use the info in "lhs" to unlet the item at the index in the
6809 // list or dict.
6810 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006811 }
6812
Bram Moolenaar752fc692021-01-04 21:57:11 +01006813 vim_free(lhs.lhs_name);
6814 }
6815 else if (check_vim9_unlet(p) == FAIL)
6816 {
6817 ret = FAIL;
6818 }
6819 else
6820 {
6821 // Normal name. Only supports g:, w:, t: and b: namespaces.
6822 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006823 }
6824
Bram Moolenaar752fc692021-01-04 21:57:11 +01006825 *name_end = cc;
6826 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006827}
6828
6829/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02006830 * Callback passed to ex_unletlock().
6831 */
6832 static int
6833compile_lock_unlock(
6834 lval_T *lvp,
6835 char_u *name_end,
6836 exarg_T *eap,
6837 int deep UNUSED,
6838 void *coookie)
6839{
6840 cctx_T *cctx = coookie;
6841 int cc = *name_end;
6842 char_u *p = lvp->ll_name;
6843 int ret = OK;
6844 size_t len;
6845 char_u *buf;
6846
6847 if (cctx->ctx_skip == SKIP_YES)
6848 return OK;
6849
6850 // Cannot use :lockvar and :unlockvar on local variables.
6851 if (p[1] != ':')
6852 {
6853 char_u *end = skip_var_one(p, FALSE);
6854
6855 if (lookup_local(p, end - p, NULL, cctx) == OK)
6856 {
6857 emsg(_(e_cannot_lock_unlock_local_variable));
6858 return FAIL;
6859 }
6860 }
6861
6862 // Checking is done at runtime.
6863 *name_end = NUL;
6864 len = name_end - p + 20;
6865 buf = alloc(len);
6866 if (buf == NULL)
6867 ret = FAIL;
6868 else
6869 {
6870 vim_snprintf((char *)buf, len, "%s %s",
6871 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
6872 p);
6873 ret = generate_EXEC(cctx, buf);
6874
6875 vim_free(buf);
6876 *name_end = cc;
6877 }
6878 return ret;
6879}
6880
6881/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006882 * compile "unlet var", "lock var" and "unlock var"
6883 * "arg" points to "var".
6884 */
6885 static char_u *
6886compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6887{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02006888 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6889 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
6890 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006891 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6892}
6893
6894/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006895 * Compile an :import command.
6896 */
6897 static char_u *
6898compile_import(char_u *arg, cctx_T *cctx)
6899{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006900 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006901}
6902
6903/*
6904 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6905 */
6906 static int
6907compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6908{
6909 garray_T *instr = &cctx->ctx_instr;
6910 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6911
6912 if (endlabel == NULL)
6913 return FAIL;
6914 endlabel->el_next = *el;
6915 *el = endlabel;
6916 endlabel->el_end_label = instr->ga_len;
6917
6918 generate_JUMP(cctx, when, 0);
6919 return OK;
6920}
6921
6922 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006923compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006924{
6925 garray_T *instr = &cctx->ctx_instr;
6926
6927 while (*el != NULL)
6928 {
6929 endlabel_T *cur = (*el);
6930 isn_T *isn;
6931
6932 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006933 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006934 *el = cur->el_next;
6935 vim_free(cur);
6936 }
6937}
6938
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006939 static void
6940compile_free_jump_to_end(endlabel_T **el)
6941{
6942 while (*el != NULL)
6943 {
6944 endlabel_T *cur = (*el);
6945
6946 *el = cur->el_next;
6947 vim_free(cur);
6948 }
6949}
6950
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006951/*
6952 * Create a new scope and set up the generic items.
6953 */
6954 static scope_T *
6955new_scope(cctx_T *cctx, scopetype_T type)
6956{
6957 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6958
6959 if (scope == NULL)
6960 return NULL;
6961 scope->se_outer = cctx->ctx_scope;
6962 cctx->ctx_scope = scope;
6963 scope->se_type = type;
6964 scope->se_local_count = cctx->ctx_locals.ga_len;
6965 return scope;
6966}
6967
6968/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006969 * Free the current scope and go back to the outer scope.
6970 */
6971 static void
6972drop_scope(cctx_T *cctx)
6973{
6974 scope_T *scope = cctx->ctx_scope;
6975
6976 if (scope == NULL)
6977 {
6978 iemsg("calling drop_scope() without a scope");
6979 return;
6980 }
6981 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006982 switch (scope->se_type)
6983 {
6984 case IF_SCOPE:
6985 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6986 case FOR_SCOPE:
6987 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6988 case WHILE_SCOPE:
6989 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6990 case TRY_SCOPE:
6991 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6992 case NO_SCOPE:
6993 case BLOCK_SCOPE:
6994 break;
6995 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006996 vim_free(scope);
6997}
6998
6999/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007000 * compile "if expr"
7001 *
7002 * "if expr" Produces instructions:
7003 * EVAL expr Push result of "expr"
7004 * JUMP_IF_FALSE end
7005 * ... body ...
7006 * end:
7007 *
7008 * "if expr | else" Produces instructions:
7009 * EVAL expr Push result of "expr"
7010 * JUMP_IF_FALSE else
7011 * ... body ...
7012 * JUMP_ALWAYS end
7013 * else:
7014 * ... body ...
7015 * end:
7016 *
7017 * "if expr1 | elseif expr2 | else" Produces instructions:
7018 * EVAL expr Push result of "expr"
7019 * JUMP_IF_FALSE elseif
7020 * ... body ...
7021 * JUMP_ALWAYS end
7022 * elseif:
7023 * EVAL expr Push result of "expr"
7024 * JUMP_IF_FALSE else
7025 * ... body ...
7026 * JUMP_ALWAYS end
7027 * else:
7028 * ... body ...
7029 * end:
7030 */
7031 static char_u *
7032compile_if(char_u *arg, cctx_T *cctx)
7033{
7034 char_u *p = arg;
7035 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007036 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007037 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007038 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007039 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007040
Bram Moolenaara5565e42020-05-09 15:44:01 +02007041 CLEAR_FIELD(ppconst);
7042 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007043 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007044 clear_ppconst(&ppconst);
7045 return NULL;
7046 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007047 if (!ends_excmd2(arg, skipwhite(p)))
7048 {
7049 semsg(_(e_trailing_arg), p);
7050 return NULL;
7051 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007052 if (cctx->ctx_skip == SKIP_YES)
7053 clear_ppconst(&ppconst);
7054 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007055 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007056 int error = FALSE;
7057 int v;
7058
Bram Moolenaara5565e42020-05-09 15:44:01 +02007059 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007060 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007061 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007062 if (error)
7063 return NULL;
7064 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007065 }
7066 else
7067 {
7068 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007069 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007070 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007071 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007072 if (bool_on_stack(cctx) == FAIL)
7073 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007074 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007075
Bram Moolenaara91a7132021-03-25 21:12:15 +01007076 // CMDMOD_REV must come before the jump
7077 generate_undo_cmdmods(cctx);
7078
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007079 scope = new_scope(cctx, IF_SCOPE);
7080 if (scope == NULL)
7081 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007082 scope->se_skip_save = skip_save;
7083 // "is_had_return" will be reset if any block does not end in :return
7084 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007085
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007086 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007087 {
7088 // "where" is set when ":elseif", "else" or ":endif" is found
7089 scope->se_u.se_if.is_if_label = instr->ga_len;
7090 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7091 }
7092 else
7093 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007094
Bram Moolenaarced68a02021-01-24 17:53:47 +01007095#ifdef FEAT_PROFILE
7096 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7097 && skip_save != SKIP_YES)
7098 {
7099 // generated a profile start, need to generate a profile end, since it
7100 // won't be done after returning
7101 cctx->ctx_skip = SKIP_NOT;
7102 generate_instr(cctx, ISN_PROF_END);
7103 cctx->ctx_skip = SKIP_YES;
7104 }
7105#endif
7106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007107 return p;
7108}
7109
7110 static char_u *
7111compile_elseif(char_u *arg, cctx_T *cctx)
7112{
7113 char_u *p = arg;
7114 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007115 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007116 isn_T *isn;
7117 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007118 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007119 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007120
7121 if (scope == NULL || scope->se_type != IF_SCOPE)
7122 {
7123 emsg(_(e_elseif_without_if));
7124 return NULL;
7125 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007126 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007127 if (!cctx->ctx_had_return)
7128 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007129
Bram Moolenaarced68a02021-01-24 17:53:47 +01007130 if (cctx->ctx_skip == SKIP_NOT)
7131 {
7132 // previous block was executed, this one and following will not
7133 cctx->ctx_skip = SKIP_YES;
7134 scope->se_u.se_if.is_seen_skip_not = TRUE;
7135 }
7136 if (scope->se_u.se_if.is_seen_skip_not)
7137 {
7138 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007139 // Do not count the "elseif" for profiling and cmdmod
7140 instr->ga_len = current_instr_idx(cctx);
7141
Bram Moolenaarced68a02021-01-24 17:53:47 +01007142 skip_expr_cctx(&p, cctx);
7143 return p;
7144 }
7145
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007146 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007147 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007148 int moved_cmdmod = FALSE;
7149
7150 // Move any CMDMOD instruction to after the jump
7151 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7152 {
7153 if (ga_grow(instr, 1) == FAIL)
7154 return NULL;
7155 ((isn_T *)instr->ga_data)[instr->ga_len] =
7156 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7157 --instr->ga_len;
7158 moved_cmdmod = TRUE;
7159 }
7160
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007161 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007162 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007163 return NULL;
7164 // previous "if" or "elseif" jumps here
7165 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7166 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007167 if (moved_cmdmod)
7168 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007169 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007170
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007171 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007172 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007173 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007174 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007175 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007176#ifdef FEAT_PROFILE
7177 if (cctx->ctx_profiling)
7178 {
7179 // the previous block was skipped, need to profile this line
7180 generate_instr(cctx, ISN_PROF_START);
7181 instr_count = instr->ga_len;
7182 }
7183#endif
7184 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007185 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007186 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007187 clear_ppconst(&ppconst);
7188 return NULL;
7189 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007190 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007191 if (!ends_excmd2(arg, skipwhite(p)))
7192 {
7193 semsg(_(e_trailing_arg), p);
7194 return NULL;
7195 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007196 if (scope->se_skip_save == SKIP_YES)
7197 clear_ppconst(&ppconst);
7198 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007199 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007200 int error = FALSE;
7201 int v;
7202
Bram Moolenaar7f141552020-05-09 17:35:53 +02007203 // The expression results in a constant.
7204 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007205 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7206 if (error)
7207 return NULL;
7208 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007209 clear_ppconst(&ppconst);
7210 scope->se_u.se_if.is_if_label = -1;
7211 }
7212 else
7213 {
7214 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007215 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007216 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007217 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007218 if (bool_on_stack(cctx) == FAIL)
7219 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007220
Bram Moolenaara91a7132021-03-25 21:12:15 +01007221 // CMDMOD_REV must come before the jump
7222 generate_undo_cmdmods(cctx);
7223
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007224 // "where" is set when ":elseif", "else" or ":endif" is found
7225 scope->se_u.se_if.is_if_label = instr->ga_len;
7226 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7227 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007228
7229 return p;
7230}
7231
7232 static char_u *
7233compile_else(char_u *arg, cctx_T *cctx)
7234{
7235 char_u *p = arg;
7236 garray_T *instr = &cctx->ctx_instr;
7237 isn_T *isn;
7238 scope_T *scope = cctx->ctx_scope;
7239
7240 if (scope == NULL || scope->se_type != IF_SCOPE)
7241 {
7242 emsg(_(e_else_without_if));
7243 return NULL;
7244 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007245 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007246 if (!cctx->ctx_had_return)
7247 scope->se_u.se_if.is_had_return = FALSE;
7248 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007249
Bram Moolenaarced68a02021-01-24 17:53:47 +01007250#ifdef FEAT_PROFILE
7251 if (cctx->ctx_profiling)
7252 {
7253 if (cctx->ctx_skip == SKIP_NOT
7254 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7255 .isn_type == ISN_PROF_START)
7256 // the previous block was executed, do not count "else" for profiling
7257 --instr->ga_len;
7258 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7259 {
7260 // the previous block was not executed, this one will, do count the
7261 // "else" for profiling
7262 cctx->ctx_skip = SKIP_NOT;
7263 generate_instr(cctx, ISN_PROF_END);
7264 generate_instr(cctx, ISN_PROF_START);
7265 cctx->ctx_skip = SKIP_YES;
7266 }
7267 }
7268#endif
7269
7270 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007271 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007272 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007273 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007274 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007275 if (!cctx->ctx_had_return
7276 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7277 JUMP_ALWAYS, cctx) == FAIL)
7278 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007279 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007280
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007281 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007282 {
7283 if (scope->se_u.se_if.is_if_label >= 0)
7284 {
7285 // previous "if" or "elseif" jumps here
7286 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7287 isn->isn_arg.jump.jump_where = instr->ga_len;
7288 scope->se_u.se_if.is_if_label = -1;
7289 }
7290 }
7291
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007292 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007293 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7294 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007295
7296 return p;
7297}
7298
7299 static char_u *
7300compile_endif(char_u *arg, cctx_T *cctx)
7301{
7302 scope_T *scope = cctx->ctx_scope;
7303 ifscope_T *ifscope;
7304 garray_T *instr = &cctx->ctx_instr;
7305 isn_T *isn;
7306
Bram Moolenaarfa984412021-03-25 22:15:28 +01007307 if (misplaced_cmdmod(cctx))
7308 return NULL;
7309
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007310 if (scope == NULL || scope->se_type != IF_SCOPE)
7311 {
7312 emsg(_(e_endif_without_if));
7313 return NULL;
7314 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007315 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007316 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007317 if (!cctx->ctx_had_return)
7318 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007319
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007320 if (scope->se_u.se_if.is_if_label >= 0)
7321 {
7322 // previous "if" or "elseif" jumps here
7323 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7324 isn->isn_arg.jump.jump_where = instr->ga_len;
7325 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007326 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007327 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007328
7329#ifdef FEAT_PROFILE
7330 // even when skipping we count the endif as executed, unless the block it's
7331 // in is skipped
7332 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7333 && scope->se_skip_save != SKIP_YES)
7334 {
7335 cctx->ctx_skip = SKIP_NOT;
7336 generate_instr(cctx, ISN_PROF_START);
7337 }
7338#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007339 cctx->ctx_skip = scope->se_skip_save;
7340
7341 // If all the blocks end in :return and there is an :else then the
7342 // had_return flag is set.
7343 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007344
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007345 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007346 return arg;
7347}
7348
7349/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007350 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007351 *
7352 * Produces instructions:
7353 * PUSHNR -1
7354 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007355 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007356 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7357 * - if beyond end, jump to "end"
7358 * - otherwise get item from list and push it
7359 * STORE var Store item in "var"
7360 * ... body ...
7361 * JUMP top Jump back to repeat
7362 * end: DROP Drop the result of "expr"
7363 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007364 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7365 * UNPACK 2 Split item in 2
7366 * STORE var1 Store item in "var1"
7367 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007368 */
7369 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007370compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007371{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007372 char_u *arg;
7373 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007374 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007375 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007376 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007377 int var_count = 0;
7378 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007379 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007380 garray_T *stack = &cctx->ctx_type_stack;
7381 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007382 lvar_T *loop_lvar; // loop iteration variable
7383 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007384 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007385 type_T *item_type = &t_any;
7386 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007387
Bram Moolenaar792f7862020-11-23 08:31:18 +01007388 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007389 if (p == NULL)
7390 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007391 if (var_count == 0)
7392 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007393
7394 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007395 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007396 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7397 return NULL;
7398 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007399 {
7400 emsg(_(e_missing_in));
7401 return NULL;
7402 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007403 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007404 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7405 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007406
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007407 scope = new_scope(cctx, FOR_SCOPE);
7408 if (scope == NULL)
7409 return NULL;
7410
Bram Moolenaar792f7862020-11-23 08:31:18 +01007411 // Reserve a variable to store the loop iteration counter and initialize it
7412 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007413 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7414 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007415 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007416 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007417 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007418 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007419 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007420 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007421
7422 // compile "expr", it remains on the stack until "endfor"
7423 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007424 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007425 {
7426 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007427 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007428 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007429 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007430
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007431 // If we know the type of "var" and it is a not a list or string we can
7432 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007433 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007434 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
7435 && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007436 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007437 // TODO: support Blob
7438 semsg(_(e_for_loop_on_str_not_supported),
7439 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007440 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007441 return NULL;
7442 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007443
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007444 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007445 {
7446 if (var_count == 1)
7447 item_type = vartype->tt_member;
7448 else if (vartype->tt_member->tt_type == VAR_LIST
7449 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
7450 item_type = vartype->tt_member->tt_member;
7451 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007452
Bram Moolenaara91a7132021-03-25 21:12:15 +01007453 // CMDMOD_REV must come before the FOR instruction
7454 generate_undo_cmdmods(cctx);
7455
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007456 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007457 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007458 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007459
Bram Moolenaar792f7862020-11-23 08:31:18 +01007460 arg = arg_start;
7461 if (var_count > 1)
7462 {
7463 generate_UNPACK(cctx, var_count, semicolon);
7464 arg = skipwhite(arg + 1); // skip white after '['
7465
7466 // the list item is replaced by a number of items
7467 if (ga_grow(stack, var_count - 1) == FAIL)
7468 {
7469 drop_scope(cctx);
7470 return NULL;
7471 }
7472 --stack->ga_len;
7473 for (idx = 0; idx < var_count; ++idx)
7474 {
7475 ((type_T **)stack->ga_data)[stack->ga_len] =
7476 (semicolon && idx == 0) ? vartype : item_type;
7477 ++stack->ga_len;
7478 }
7479 }
7480
7481 for (idx = 0; idx < var_count; ++idx)
7482 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007483 assign_dest_T dest = dest_local;
7484 int opt_flags = 0;
7485 int vimvaridx = -1;
7486 type_T *type = &t_any;
7487
7488 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007489 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007490 name = vim_strnsave(arg, varlen);
7491 if (name == NULL)
7492 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007493
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007494 // TODO: script var not supported?
7495 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7496 &vimvaridx, &type, cctx) == FAIL)
7497 goto failed;
7498 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007499 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007500 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7501 0, 0, type, name) == FAIL)
7502 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007503 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007504 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007505 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007506 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007507 {
7508 semsg(_(e_variable_already_declared), arg);
7509 goto failed;
7510 }
7511
Bram Moolenaarea870692020-12-02 14:24:30 +01007512 if (STRNCMP(name, "s:", 2) == 0)
7513 {
7514 semsg(_(e_cannot_declare_script_variable_in_function), name);
7515 goto failed;
7516 }
7517
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007518 // Reserve a variable to store "var".
7519 // TODO: check for type
7520 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
7521 if (var_lvar == NULL)
7522 // out of memory or used as an argument
7523 goto failed;
7524
7525 if (semicolon && idx == var_count - 1)
7526 var_lvar->lv_type = vartype;
7527 else
7528 var_lvar->lv_type = item_type;
7529 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7530 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007531
Bram Moolenaar036d0712021-01-17 20:23:38 +01007532 if (*p == ':')
7533 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007534 if (*p == ',' || *p == ';')
7535 ++p;
7536 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007537 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007538 }
7539
7540 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007541
7542failed:
7543 vim_free(name);
7544 drop_scope(cctx);
7545 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007546}
7547
7548/*
7549 * compile "endfor"
7550 */
7551 static char_u *
7552compile_endfor(char_u *arg, cctx_T *cctx)
7553{
7554 garray_T *instr = &cctx->ctx_instr;
7555 scope_T *scope = cctx->ctx_scope;
7556 forscope_T *forscope;
7557 isn_T *isn;
7558
Bram Moolenaarfa984412021-03-25 22:15:28 +01007559 if (misplaced_cmdmod(cctx))
7560 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007561
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007562 if (scope == NULL || scope->se_type != FOR_SCOPE)
7563 {
7564 emsg(_(e_for));
7565 return NULL;
7566 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007567 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007568 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007569 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007570
7571 // At end of ":for" scope jump back to the FOR instruction.
7572 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7573
7574 // Fill in the "end" label in the FOR statement so it can jump here
7575 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7576 isn->isn_arg.forloop.for_end = instr->ga_len;
7577
7578 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007579 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007580
7581 // Below the ":for" scope drop the "expr" list from the stack.
7582 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7583 return NULL;
7584
7585 vim_free(scope);
7586
7587 return arg;
7588}
7589
7590/*
7591 * compile "while expr"
7592 *
7593 * Produces instructions:
7594 * top: EVAL expr Push result of "expr"
7595 * JUMP_IF_FALSE end jump if false
7596 * ... body ...
7597 * JUMP top Jump back to repeat
7598 * end:
7599 *
7600 */
7601 static char_u *
7602compile_while(char_u *arg, cctx_T *cctx)
7603{
7604 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007605 scope_T *scope;
7606
7607 scope = new_scope(cctx, WHILE_SCOPE);
7608 if (scope == NULL)
7609 return NULL;
7610
Bram Moolenaara91a7132021-03-25 21:12:15 +01007611 // "endwhile" jumps back here, one before when profiling or using cmdmods
7612 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007613
7614 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007615 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007616 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007617 if (!ends_excmd2(arg, skipwhite(p)))
7618 {
7619 semsg(_(e_trailing_arg), p);
7620 return NULL;
7621 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007622
Bram Moolenaar13106602020-10-04 16:06:05 +02007623 if (bool_on_stack(cctx) == FAIL)
7624 return FAIL;
7625
Bram Moolenaara91a7132021-03-25 21:12:15 +01007626 // CMDMOD_REV must come before the jump
7627 generate_undo_cmdmods(cctx);
7628
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007629 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007630 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007631 JUMP_IF_FALSE, cctx) == FAIL)
7632 return FAIL;
7633
7634 return p;
7635}
7636
7637/*
7638 * compile "endwhile"
7639 */
7640 static char_u *
7641compile_endwhile(char_u *arg, cctx_T *cctx)
7642{
7643 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007644 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007645
Bram Moolenaarfa984412021-03-25 22:15:28 +01007646 if (misplaced_cmdmod(cctx))
7647 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007648 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7649 {
7650 emsg(_(e_while));
7651 return NULL;
7652 }
7653 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007654 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007655
Bram Moolenaarf002a412021-01-24 13:34:18 +01007656#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007657 // count the endwhile before jumping
7658 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007659#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007660
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007661 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007662 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007663
7664 // Fill in the "end" label in the WHILE statement so it can jump here.
7665 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007666 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7667 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007668
7669 vim_free(scope);
7670
7671 return arg;
7672}
7673
7674/*
7675 * compile "continue"
7676 */
7677 static char_u *
7678compile_continue(char_u *arg, cctx_T *cctx)
7679{
7680 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007681 int try_scopes = 0;
7682 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007683
7684 for (;;)
7685 {
7686 if (scope == NULL)
7687 {
7688 emsg(_(e_continue));
7689 return NULL;
7690 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01007691 if (scope->se_type == FOR_SCOPE)
7692 {
7693 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007694 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007695 }
7696 if (scope->se_type == WHILE_SCOPE)
7697 {
7698 loop_label = scope->se_u.se_while.ws_top_label;
7699 break;
7700 }
7701 if (scope->se_type == TRY_SCOPE)
7702 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007703 scope = scope->se_outer;
7704 }
7705
Bram Moolenaarc150c092021-02-13 15:02:46 +01007706 if (try_scopes > 0)
7707 // Inside one or more try/catch blocks we first need to jump to the
7708 // "finally" or "endtry" to cleanup.
7709 generate_TRYCONT(cctx, try_scopes, loop_label);
7710 else
7711 // Jump back to the FOR or WHILE instruction.
7712 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
7713
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007714 return arg;
7715}
7716
7717/*
7718 * compile "break"
7719 */
7720 static char_u *
7721compile_break(char_u *arg, cctx_T *cctx)
7722{
7723 scope_T *scope = cctx->ctx_scope;
7724 endlabel_T **el;
7725
7726 for (;;)
7727 {
7728 if (scope == NULL)
7729 {
7730 emsg(_(e_break));
7731 return NULL;
7732 }
7733 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7734 break;
7735 scope = scope->se_outer;
7736 }
7737
7738 // Jump to the end of the FOR or WHILE loop.
7739 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007740 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007741 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007742 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007743 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7744 return FAIL;
7745
7746 return arg;
7747}
7748
7749/*
7750 * compile "{" start of block
7751 */
7752 static char_u *
7753compile_block(char_u *arg, cctx_T *cctx)
7754{
7755 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7756 return NULL;
7757 return skipwhite(arg + 1);
7758}
7759
7760/*
7761 * compile end of block: drop one scope
7762 */
7763 static void
7764compile_endblock(cctx_T *cctx)
7765{
7766 scope_T *scope = cctx->ctx_scope;
7767
7768 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007769 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007770 vim_free(scope);
7771}
7772
7773/*
7774 * compile "try"
7775 * Creates a new scope for the try-endtry, pointing to the first catch and
7776 * finally.
7777 * Creates another scope for the "try" block itself.
7778 * TRY instruction sets up exception handling at runtime.
7779 *
7780 * "try"
7781 * TRY -> catch1, -> finally push trystack entry
7782 * ... try block
7783 * "throw {exception}"
7784 * EVAL {exception}
7785 * THROW create exception
7786 * ... try block
7787 * " catch {expr}"
7788 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007789 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007790 * EVAL {expr}
7791 * MATCH
7792 * JUMP nomatch -> catch2
7793 * CATCH remove exception
7794 * ... catch block
7795 * " catch"
7796 * JUMP -> finally
7797 * catch2: CATCH remove exception
7798 * ... catch block
7799 * " finally"
7800 * finally:
7801 * ... finally block
7802 * " endtry"
7803 * ENDTRY pop trystack entry, may rethrow
7804 */
7805 static char_u *
7806compile_try(char_u *arg, cctx_T *cctx)
7807{
7808 garray_T *instr = &cctx->ctx_instr;
7809 scope_T *try_scope;
7810 scope_T *scope;
7811
Bram Moolenaarfa984412021-03-25 22:15:28 +01007812 if (misplaced_cmdmod(cctx))
7813 return NULL;
7814
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007815 // scope that holds the jumps that go to catch/finally/endtry
7816 try_scope = new_scope(cctx, TRY_SCOPE);
7817 if (try_scope == NULL)
7818 return NULL;
7819
Bram Moolenaar69f70502021-01-01 16:10:46 +01007820 if (cctx->ctx_skip != SKIP_YES)
7821 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007822 isn_T *isn;
7823
7824 // "try_catch" is set when the first ":catch" is found or when no catch
7825 // is found and ":finally" is found.
7826 // "try_finally" is set when ":finally" is found
7827 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01007828 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007829 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
7830 return NULL;
7831 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
7832 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007833 return NULL;
7834 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007835
7836 // scope for the try block itself
7837 scope = new_scope(cctx, BLOCK_SCOPE);
7838 if (scope == NULL)
7839 return NULL;
7840
7841 return arg;
7842}
7843
7844/*
7845 * compile "catch {expr}"
7846 */
7847 static char_u *
7848compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7849{
7850 scope_T *scope = cctx->ctx_scope;
7851 garray_T *instr = &cctx->ctx_instr;
7852 char_u *p;
7853 isn_T *isn;
7854
Bram Moolenaarfa984412021-03-25 22:15:28 +01007855 if (misplaced_cmdmod(cctx))
7856 return NULL;
7857
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007858 // end block scope from :try or :catch
7859 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7860 compile_endblock(cctx);
7861 scope = cctx->ctx_scope;
7862
7863 // Error if not in a :try scope
7864 if (scope == NULL || scope->se_type != TRY_SCOPE)
7865 {
7866 emsg(_(e_catch));
7867 return NULL;
7868 }
7869
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007870 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007871 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007872 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007873 return NULL;
7874 }
7875
Bram Moolenaar69f70502021-01-01 16:10:46 +01007876 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007877 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007878#ifdef FEAT_PROFILE
7879 // the profile-start should be after the jump
7880 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7881 .isn_type == ISN_PROF_START)
7882 --instr->ga_len;
7883#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01007884 // Jump from end of previous block to :finally or :endtry
7885 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7886 JUMP_ALWAYS, cctx) == FAIL)
7887 return NULL;
7888
7889 // End :try or :catch scope: set value in ISN_TRY instruction
7890 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007891 if (isn->isn_arg.try.try_ref->try_catch == 0)
7892 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007893 if (scope->se_u.se_try.ts_catch_label != 0)
7894 {
7895 // Previous catch without match jumps here
7896 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7897 isn->isn_arg.jump.jump_where = instr->ga_len;
7898 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007899#ifdef FEAT_PROFILE
7900 if (cctx->ctx_profiling)
7901 {
7902 // a "throw" that jumps here needs to be counted
7903 generate_instr(cctx, ISN_PROF_END);
7904 // the "catch" is also counted
7905 generate_instr(cctx, ISN_PROF_START);
7906 }
7907#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007908 }
7909
7910 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007911 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007912 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007913 scope->se_u.se_try.ts_caught_all = TRUE;
7914 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007915 }
7916 else
7917 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007918 char_u *end;
7919 char_u *pat;
7920 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007921 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007922 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007923
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007924 // Push v:exception, push {expr} and MATCH
7925 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7926
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007927 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007928 if (*end != *p)
7929 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007930 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007931 vim_free(tofree);
7932 return FAIL;
7933 }
7934 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007935 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007936 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007937 len = (int)(end - tofree);
7938 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007939 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007940 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007941 if (pat == NULL)
7942 return FAIL;
7943 if (generate_PUSHS(cctx, pat) == FAIL)
7944 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007945
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007946 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7947 return NULL;
7948
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007949 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007950 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7951 return NULL;
7952 }
7953
Bram Moolenaar69f70502021-01-01 16:10:46 +01007954 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007955 return NULL;
7956
7957 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7958 return NULL;
7959 return p;
7960}
7961
7962 static char_u *
7963compile_finally(char_u *arg, cctx_T *cctx)
7964{
7965 scope_T *scope = cctx->ctx_scope;
7966 garray_T *instr = &cctx->ctx_instr;
7967 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007968 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007969
Bram Moolenaarfa984412021-03-25 22:15:28 +01007970 if (misplaced_cmdmod(cctx))
7971 return NULL;
7972
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007973 // end block scope from :try or :catch
7974 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7975 compile_endblock(cctx);
7976 scope = cctx->ctx_scope;
7977
7978 // Error if not in a :try scope
7979 if (scope == NULL || scope->se_type != TRY_SCOPE)
7980 {
7981 emsg(_(e_finally));
7982 return NULL;
7983 }
7984
7985 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007986 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007987 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007988 {
7989 emsg(_(e_finally_dup));
7990 return NULL;
7991 }
7992
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007993 this_instr = instr->ga_len;
7994#ifdef FEAT_PROFILE
7995 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7996 .isn_type == ISN_PROF_START)
7997 // jump to the profile start of the "finally"
7998 --this_instr;
7999#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008000
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008001 // Fill in the "end" label in jumps at the end of the blocks.
8002 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8003 this_instr, cctx);
8004
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008005 // If there is no :catch then an exception jumps to :finally.
8006 if (isn->isn_arg.try.try_ref->try_catch == 0)
8007 isn->isn_arg.try.try_ref->try_catch = this_instr;
8008 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008009 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008010 {
8011 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008012 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008013 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008014 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008015 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008016 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8017 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008018
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008019 // TODO: set index in ts_finally_label jumps
8020
8021 return arg;
8022}
8023
8024 static char_u *
8025compile_endtry(char_u *arg, cctx_T *cctx)
8026{
8027 scope_T *scope = cctx->ctx_scope;
8028 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008029 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008030
Bram Moolenaarfa984412021-03-25 22:15:28 +01008031 if (misplaced_cmdmod(cctx))
8032 return NULL;
8033
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008034 // end block scope from :catch or :finally
8035 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8036 compile_endblock(cctx);
8037 scope = cctx->ctx_scope;
8038
8039 // Error if not in a :try scope
8040 if (scope == NULL || scope->se_type != TRY_SCOPE)
8041 {
8042 if (scope == NULL)
8043 emsg(_(e_no_endtry));
8044 else if (scope->se_type == WHILE_SCOPE)
8045 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008046 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008047 emsg(_(e_endfor));
8048 else
8049 emsg(_(e_endif));
8050 return NULL;
8051 }
8052
Bram Moolenaarc150c092021-02-13 15:02:46 +01008053 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008054 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008055 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008056 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8057 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008058 {
8059 emsg(_(e_missing_catch_or_finally));
8060 return NULL;
8061 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008062
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008063#ifdef FEAT_PROFILE
8064 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8065 .isn_type == ISN_PROF_START)
8066 // move the profile start after "endtry" so that it's not counted when
8067 // the exception is rethrown.
8068 --instr->ga_len;
8069#endif
8070
Bram Moolenaar69f70502021-01-01 16:10:46 +01008071 // Fill in the "end" label in jumps at the end of the blocks, if not
8072 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008073 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8074 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008075
Bram Moolenaar69f70502021-01-01 16:10:46 +01008076 if (scope->se_u.se_try.ts_catch_label != 0)
8077 {
8078 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008079 isn_T *isn = ((isn_T *)instr->ga_data)
8080 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008081 isn->isn_arg.jump.jump_where = instr->ga_len;
8082 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008083 }
8084
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008085 compile_endblock(cctx);
8086
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008087 if (cctx->ctx_skip != SKIP_YES)
8088 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008089 // End :catch or :finally scope: set instruction index in ISN_TRY
8090 // instruction
8091 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008092 if (cctx->ctx_skip != SKIP_YES
8093 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8094 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008095#ifdef FEAT_PROFILE
8096 if (cctx->ctx_profiling)
8097 generate_instr(cctx, ISN_PROF_START);
8098#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008099 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008100 return arg;
8101}
8102
8103/*
8104 * compile "throw {expr}"
8105 */
8106 static char_u *
8107compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8108{
8109 char_u *p = skipwhite(arg);
8110
Bram Moolenaara5565e42020-05-09 15:44:01 +02008111 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008112 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008113 if (cctx->ctx_skip == SKIP_YES)
8114 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008115 if (may_generate_2STRING(-1, cctx) == FAIL)
8116 return NULL;
8117 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8118 return NULL;
8119
8120 return p;
8121}
8122
8123/*
8124 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008125 * compile "echomsg expr"
8126 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008127 * compile "execute expr"
8128 */
8129 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008130compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008131{
8132 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008133 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008134 int count = 0;
8135
8136 for (;;)
8137 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008138 if (ends_excmd2(prev, p))
8139 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008140 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008141 return NULL;
8142 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008143 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008144 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008145 }
8146
Bram Moolenaare4984292020-12-13 14:19:25 +01008147 if (count > 0)
8148 {
8149 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8150 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8151 else if (cmdidx == CMD_execute)
8152 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8153 else if (cmdidx == CMD_echomsg)
8154 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8155 else
8156 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
8157 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008158 return p;
8159}
8160
8161/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008162 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008163 * instruction to compute it and return OK.
8164 * Otherwise return FAIL, the caller must deal with any range.
8165 */
8166 static int
8167compile_variable_range(exarg_T *eap, cctx_T *cctx)
8168{
8169 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8170 char_u *p = skipdigits(eap->cmd);
8171
8172 if (p == range_end)
8173 return FAIL;
8174 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8175}
8176
8177/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008178 * :put r
8179 * :put ={expr}
8180 */
8181 static char_u *
8182compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8183{
8184 char_u *line = arg;
8185 linenr_T lnum;
8186 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008187 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008188
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008189 eap->regname = *line;
8190
8191 if (eap->regname == '=')
8192 {
8193 char_u *p = line + 1;
8194
8195 if (compile_expr0(&p, cctx) == FAIL)
8196 return NULL;
8197 line = p;
8198 }
8199 else if (eap->regname != NUL)
8200 ++line;
8201
Bram Moolenaar08597872020-12-10 19:43:40 +01008202 if (compile_variable_range(eap, cctx) == OK)
8203 {
8204 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8205 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008206 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008207 {
8208 // Either no range or a number.
8209 // "errormsg" will not be set because the range is ADDR_LINES.
8210 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008211 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008212 return NULL;
8213 if (eap->addr_count == 0)
8214 lnum = -1;
8215 else
8216 lnum = eap->line2;
8217 if (above)
8218 --lnum;
8219 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008220
8221 generate_PUT(cctx, eap->regname, lnum);
8222 return line;
8223}
8224
8225/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008226 * A command that is not compiled, execute with legacy code.
8227 */
8228 static char_u *
8229compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8230{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008231 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008232 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008233 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008234
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008235 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008236 goto theend;
8237
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008238 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008239 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008240 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008241 int usefilter = FALSE;
8242
8243 has_expr = argt & (EX_XFILE | EX_EXPAND);
8244
8245 // If the command can be followed by a bar, find the bar and truncate
8246 // it, so that the following command can be compiled.
8247 // The '|' is overwritten with a NUL, it is put back below.
8248 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8249 && *eap->arg == '!')
8250 // :w !filter or :r !filter or :r! filter
8251 usefilter = TRUE;
8252 if ((argt & EX_TRLBAR) && !usefilter)
8253 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008254 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008255 separate_nextcmd(eap);
8256 if (eap->nextcmd != NULL)
8257 nextcmd = eap->nextcmd;
8258 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008259 else if (eap->cmdidx == CMD_wincmd)
8260 {
8261 p = eap->arg;
8262 if (*p != NUL)
8263 ++p;
8264 if (*p == 'g' || *p == Ctrl_G)
8265 ++p;
8266 p = skipwhite(p);
8267 if (*p == '|')
8268 {
8269 *p = NUL;
8270 nextcmd = p + 1;
8271 }
8272 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008273 }
8274
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008275 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8276 {
8277 // expand filename in "syntax include [@group] filename"
8278 has_expr = TRUE;
8279 eap->arg = skipwhite(eap->arg + 7);
8280 if (*eap->arg == '@')
8281 eap->arg = skiptowhite(eap->arg);
8282 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008283
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008284 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8285 && STRLEN(eap->arg) > 4)
8286 {
8287 int delim = *eap->arg;
8288
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008289 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008290 if (*p == delim)
8291 {
8292 eap->arg = p + 1;
8293 has_expr = TRUE;
8294 }
8295 }
8296
Bram Moolenaarecac5912021-01-05 19:23:28 +01008297 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8298 {
8299 // TODO: should only expand when appropriate for the command
8300 eap->arg = skiptowhite(eap->arg);
8301 has_expr = TRUE;
8302 }
8303
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008304 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008305 {
8306 int count = 0;
8307 char_u *start = skipwhite(line);
8308
8309 // :cmd xxx`=expr1`yyy`=expr2`zzz
8310 // PUSHS ":cmd xxx"
8311 // eval expr1
8312 // PUSHS "yyy"
8313 // eval expr2
8314 // PUSHS "zzz"
8315 // EXECCONCAT 5
8316 for (;;)
8317 {
8318 if (p > start)
8319 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008320 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008321 ++count;
8322 }
8323 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008324 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008325 return NULL;
8326 may_generate_2STRING(-1, cctx);
8327 ++count;
8328 p = skipwhite(p);
8329 if (*p != '`')
8330 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008331 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008332 return NULL;
8333 }
8334 start = p + 1;
8335
8336 p = (char_u *)strstr((char *)start, "`=");
8337 if (p == NULL)
8338 {
8339 if (*skipwhite(start) != NUL)
8340 {
8341 generate_PUSHS(cctx, vim_strsave(start));
8342 ++count;
8343 }
8344 break;
8345 }
8346 }
8347 generate_EXECCONCAT(cctx, count);
8348 }
8349 else
8350 generate_EXEC(cctx, line);
8351
8352theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008353 if (*nextcmd != NUL)
8354 {
8355 // the parser expects a pointer to the bar, put it back
8356 --nextcmd;
8357 *nextcmd = '|';
8358 }
8359
8360 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008361}
8362
8363/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008364 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008365 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008366 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008367 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008368add_def_function(ufunc_T *ufunc)
8369{
8370 dfunc_T *dfunc;
8371
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008372 if (def_functions.ga_len == 0)
8373 {
8374 // The first position is not used, so that a zero uf_dfunc_idx means it
8375 // wasn't set.
8376 if (ga_grow(&def_functions, 1) == FAIL)
8377 return FAIL;
8378 ++def_functions.ga_len;
8379 }
8380
Bram Moolenaar09689a02020-05-09 22:50:08 +02008381 // Add the function to "def_functions".
8382 if (ga_grow(&def_functions, 1) == FAIL)
8383 return FAIL;
8384 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8385 CLEAR_POINTER(dfunc);
8386 dfunc->df_idx = def_functions.ga_len;
8387 ufunc->uf_dfunc_idx = dfunc->df_idx;
8388 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008389 dfunc->df_name = vim_strsave(ufunc->uf_name);
8390 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008391 ++def_functions.ga_len;
8392 return OK;
8393}
8394
8395/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008396 * After ex_function() has collected all the function lines: parse and compile
8397 * the lines into instructions.
8398 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008399 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8400 * the return statement (used for lambda). When uf_ret_type is already set
8401 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008402 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008403 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008404 * This can be used recursively through compile_lambda(), which may reallocate
8405 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008406 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008407 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008408 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008409compile_def_function(
8410 ufunc_T *ufunc,
8411 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008412 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008413 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008414{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008415 char_u *line = NULL;
8416 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008417 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008418 cctx_T cctx;
8419 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008420 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008421 int ret = FAIL;
8422 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008423 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008424 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008425 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008426#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008427 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008428#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008429
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008430 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008431 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008432 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008433 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008434 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8435 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008436 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008437 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008438 else
8439 {
8440 if (add_def_function(ufunc) == FAIL)
8441 return FAIL;
8442 new_def_function = TRUE;
8443 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008444
Bram Moolenaar985116a2020-07-12 17:31:09 +02008445 ufunc->uf_def_status = UF_COMPILING;
8446
Bram Moolenaara80faa82020-04-12 19:37:17 +02008447 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008448
Bram Moolenaarf002a412021-01-24 13:34:18 +01008449#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008450 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008451#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008452 cctx.ctx_ufunc = ufunc;
8453 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008454 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008455 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8456 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8457 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8458 cctx.ctx_type_list = &ufunc->uf_type_list;
8459 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8460 instr = &cctx.ctx_instr;
8461
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008462 // Set the context to the function, it may be compiled when called from
8463 // another script. Set the script version to the most modern one.
8464 // The line number will be set in next_line_from_context().
8465 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008466 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8467
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008468 // Make sure error messages are OK.
8469 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8470 if (do_estack_push)
8471 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008472 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008473
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008474 if (ufunc->uf_def_args.ga_len > 0)
8475 {
8476 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008477 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008478 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008479 int i;
8480 char_u *arg;
8481 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008482 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008483
8484 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01008485 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008486 for (i = 0; i < count; ++i)
8487 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008488 garray_T *stack = &cctx.ctx_type_stack;
8489 type_T *val_type;
8490 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008491 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008492 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008493 int jump_instr_idx = instr->ga_len;
8494 isn_T *isn;
8495
8496 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
8497 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
8498 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008499
8500 // Make sure later arguments are not found.
8501 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008502
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008503 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01008504 r = compile_expr0(&arg, &cctx);
8505
8506 ufunc->uf_args.ga_len = uf_args_len;
8507 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008508 goto erret;
8509
8510 // If no type specified use the type of the default value.
8511 // Otherwise check that the default value type matches the
8512 // specified type.
8513 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008514 where.wt_index = arg_idx + 1;
8515 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008516 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008517 {
8518 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008519 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008520 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02008521 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008522 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008523 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008524
8525 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008526 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008527
8528 // set instruction index in JUMP_IF_ARG_SET to here
8529 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
8530 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008531 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008532
8533 if (did_set_arg_type)
8534 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008535 }
8536
8537 /*
8538 * Loop over all the lines of the function and generate instructions.
8539 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008540 for (;;)
8541 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008542 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008543 int starts_with_colon = FALSE;
8544 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02008545 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008546
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008547 // Bail out on the first error to avoid a flood of errors and report
8548 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01008549 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008550 goto erret;
8551
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008552 if (line != NULL && *line == '|')
8553 // the line continues after a '|'
8554 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02008555 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02008556 && !(*line == '#' && (line == cctx.ctx_line_start
8557 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008558 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02008559 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008560 goto erret;
8561 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01008562 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
8563 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008564 else
8565 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02008566 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02008567 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008568 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008569 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01008570#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008571 if (cctx.ctx_skip != SKIP_YES)
8572 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008573#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008574 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01008575 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008576 }
8577
Bram Moolenaara80faa82020-04-12 19:37:17 +02008578 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008579 ea.cmdlinep = &line;
8580 ea.cmd = skipwhite(line);
8581
Bram Moolenaarb2049902021-01-24 12:53:53 +01008582 if (*ea.cmd == '#')
8583 {
8584 // "#" starts a comment
8585 line = (char_u *)"";
8586 continue;
8587 }
8588
Bram Moolenaarf002a412021-01-24 13:34:18 +01008589#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008590 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
8591 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008592 {
8593 may_generate_prof_end(&cctx, prof_lnum);
8594
8595 prof_lnum = cctx.ctx_lnum;
8596 generate_instr(&cctx, ISN_PROF_START);
8597 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01008598#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008599
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008600 // Some things can be recognized by the first character.
8601 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008602 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008603 case '}':
8604 {
8605 // "}" ends a block scope
8606 scopetype_T stype = cctx.ctx_scope == NULL
8607 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008608
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008609 if (stype == BLOCK_SCOPE)
8610 {
8611 compile_endblock(&cctx);
8612 line = ea.cmd;
8613 }
8614 else
8615 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008616 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008617 goto erret;
8618 }
8619 if (line != NULL)
8620 line = skipwhite(ea.cmd + 1);
8621 continue;
8622 }
8623
8624 case '{':
8625 // "{" starts a block scope
8626 // "{'a': 1}->func() is something else
8627 if (ends_excmd(*skipwhite(ea.cmd + 1)))
8628 {
8629 line = compile_block(ea.cmd, &cctx);
8630 continue;
8631 }
8632 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008633 }
8634
8635 /*
8636 * COMMAND MODIFIERS
8637 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02008638 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02008639 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
8640 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008641 {
8642 if (errormsg != NULL)
8643 goto erret;
8644 // empty line or comment
8645 line = (char_u *)"";
8646 continue;
8647 }
Bram Moolenaare1004402020-10-24 20:49:43 +02008648 generate_cmdmods(&cctx, &local_cmdmod);
8649 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008650
Bram Moolenaare88c8e82020-11-01 17:03:37 +01008651 // Check if there was a colon after the last command modifier or before
8652 // the current position.
8653 for (p = ea.cmd; p >= line; --p)
8654 {
8655 if (*p == ':')
8656 starts_with_colon = TRUE;
8657 if (p < ea.cmd && !VIM_ISWHITE(*p))
8658 break;
8659 }
8660
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008661 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008662 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008663 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008664 {
8665 if (*ea.cmd == '(')
8666 // not for "call()"
8667 ea.cmd = p;
8668 else
8669 ea.cmd = skipwhite(ea.cmd);
8670 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008671
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008672 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008673 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008674 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008675
Bram Moolenaar17126b12021-01-07 22:03:02 +01008676 // Check for assignment after command modifiers.
8677 assign = may_compile_assignment(&ea, &line, &cctx);
8678 if (assign == OK)
8679 goto nextline;
8680 if (assign == FAIL)
8681 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008682 }
8683
8684 /*
8685 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008686 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008687 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008688 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008689 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008690 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008691 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008692 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008693 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008694 if (!starts_with_colon)
8695 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008696 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008697 goto erret;
8698 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01008699 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008700 if (ends_excmd2(line, ea.cmd))
8701 {
8702 // A range without a command: jump to the line.
8703 // TODO: compile to a more efficient command, possibly
8704 // calling parse_cmd_address().
8705 ea.cmdidx = CMD_SIZE;
8706 line = compile_exec(line, &ea, &cctx);
8707 goto nextline;
8708 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008709 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008710 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01008711 p = find_ex_command(&ea, NULL, starts_with_colon
8712 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008713
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008714 if (p == NULL)
8715 {
8716 if (cctx.ctx_skip != SKIP_YES)
8717 emsg(_(e_ambiguous_use_of_user_defined_command));
8718 goto erret;
8719 }
8720
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008721 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8722 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008723 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008724 {
8725 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008726 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008727 }
8728
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008729 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008730 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008731 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008732 // CMD_var cannot happen, compile_assignment() above would be
8733 // used. Most likely an assignment to a non-existing variable.
8734 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008735 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008736 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008737 }
8738
Bram Moolenaar3988f642020-08-27 22:43:03 +02008739 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008740 && ea.cmdidx != CMD_elseif
8741 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008742 && ea.cmdidx != CMD_endif
8743 && ea.cmdidx != CMD_endfor
8744 && ea.cmdidx != CMD_endwhile
8745 && ea.cmdidx != CMD_catch
8746 && ea.cmdidx != CMD_finally
8747 && ea.cmdidx != CMD_endtry)
8748 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008749 emsg(_(e_unreachable_code_after_return));
8750 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008751 }
8752
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008753 p = skipwhite(p);
8754 if (ea.cmdidx != CMD_SIZE
8755 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8756 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008757 if (ea.cmdidx >= 0)
8758 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008759 if ((ea.argt & EX_BANG) && *p == '!')
8760 {
8761 ea.forceit = TRUE;
8762 p = skipwhite(p + 1);
8763 }
8764 }
8765
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008766 switch (ea.cmdidx)
8767 {
8768 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008769 ea.arg = p;
8770 line = compile_nested_function(&ea, &cctx);
8771 break;
8772
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008773 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008774 // TODO: should we allow this, e.g. to declare a global
8775 // function?
8776 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008777 goto erret;
8778
8779 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008780 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008781 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008782 break;
8783
8784 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008785 emsg(_(e_cannot_use_let_in_vim9_script));
8786 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008787 case CMD_var:
8788 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008789 case CMD_const:
8790 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008791 if (line == p)
8792 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008793 break;
8794
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008795 case CMD_unlet:
8796 case CMD_unlockvar:
8797 case CMD_lockvar:
8798 line = compile_unletlock(p, &ea, &cctx);
8799 break;
8800
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008801 case CMD_import:
8802 line = compile_import(p, &cctx);
8803 break;
8804
8805 case CMD_if:
8806 line = compile_if(p, &cctx);
8807 break;
8808 case CMD_elseif:
8809 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008810 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008811 break;
8812 case CMD_else:
8813 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008814 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008815 break;
8816 case CMD_endif:
8817 line = compile_endif(p, &cctx);
8818 break;
8819
8820 case CMD_while:
8821 line = compile_while(p, &cctx);
8822 break;
8823 case CMD_endwhile:
8824 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008825 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008826 break;
8827
8828 case CMD_for:
8829 line = compile_for(p, &cctx);
8830 break;
8831 case CMD_endfor:
8832 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008833 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008834 break;
8835 case CMD_continue:
8836 line = compile_continue(p, &cctx);
8837 break;
8838 case CMD_break:
8839 line = compile_break(p, &cctx);
8840 break;
8841
8842 case CMD_try:
8843 line = compile_try(p, &cctx);
8844 break;
8845 case CMD_catch:
8846 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008847 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008848 break;
8849 case CMD_finally:
8850 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008851 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008852 break;
8853 case CMD_endtry:
8854 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008855 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008856 break;
8857 case CMD_throw:
8858 line = compile_throw(p, &cctx);
8859 break;
8860
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008861 case CMD_eval:
8862 if (compile_expr0(&p, &cctx) == FAIL)
8863 goto erret;
8864
Bram Moolenaar3988f642020-08-27 22:43:03 +02008865 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008866 generate_instr_drop(&cctx, ISN_DROP, 1);
8867
8868 line = skipwhite(p);
8869 break;
8870
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008871 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008872 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008873 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008874 case CMD_echomsg:
8875 case CMD_echoerr:
8876 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008877 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008878
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008879 case CMD_put:
8880 ea.cmd = cmd;
8881 line = compile_put(p, &ea, &cctx);
8882 break;
8883
Bram Moolenaar3988f642020-08-27 22:43:03 +02008884 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008885
Bram Moolenaarae616492020-07-28 20:07:27 +02008886 case CMD_append:
8887 case CMD_change:
8888 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01008889 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008890 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008891 case CMD_xit:
8892 not_in_vim9(&ea);
8893 goto erret;
8894
Bram Moolenaar002262f2020-07-08 17:47:57 +02008895 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008896 if (cctx.ctx_skip != SKIP_YES)
8897 {
8898 semsg(_(e_invalid_command_str), ea.cmd);
8899 goto erret;
8900 }
8901 // We don't check for a next command here.
8902 line = (char_u *)"";
8903 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008904
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008905 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008906 if (cctx.ctx_skip == SKIP_YES)
8907 {
8908 // We don't check for a next command here.
8909 line = (char_u *)"";
8910 }
8911 else
8912 {
8913 // Not recognized, execute with do_cmdline_cmd().
8914 ea.arg = p;
8915 line = compile_exec(line, &ea, &cctx);
8916 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008917 break;
8918 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008919nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008920 if (line == NULL)
8921 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008922 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008923
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008924 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008925 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008926
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008927 if (cctx.ctx_type_stack.ga_len < 0)
8928 {
8929 iemsg("Type stack underflow");
8930 goto erret;
8931 }
8932 }
8933
8934 if (cctx.ctx_scope != NULL)
8935 {
8936 if (cctx.ctx_scope->se_type == IF_SCOPE)
8937 emsg(_(e_endif));
8938 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8939 emsg(_(e_endwhile));
8940 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8941 emsg(_(e_endfor));
8942 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008943 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008944 goto erret;
8945 }
8946
Bram Moolenaarefd88552020-06-18 20:50:10 +02008947 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008948 {
8949 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8950 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008951 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008952 goto erret;
8953 }
8954
8955 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008956 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008957 }
8958
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008959 {
8960 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8961 + ufunc->uf_dfunc_idx;
8962 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008963 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008964#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008965 if (cctx.ctx_profiling)
8966 {
8967 dfunc->df_instr_prof = instr->ga_data;
8968 dfunc->df_instr_prof_count = instr->ga_len;
8969 }
8970 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01008971#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008972 {
8973 dfunc->df_instr = instr->ga_data;
8974 dfunc->df_instr_count = instr->ga_len;
8975 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008976 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008977 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008978 if (cctx.ctx_outer_used)
8979 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008980 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008981 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008982
8983 ret = OK;
8984
8985erret:
8986 if (ret == FAIL)
8987 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008988 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008989 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8990 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008991
8992 for (idx = 0; idx < instr->ga_len; ++idx)
8993 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008994 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008995 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008996
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008997 // If using the last entry in the table and it was added above, we
8998 // might as well remove it.
8999 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009000 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009001 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009002 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009003 ufunc->uf_dfunc_idx = 0;
9004 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009005 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009006
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009007 while (cctx.ctx_scope != NULL)
9008 drop_scope(&cctx);
9009
Bram Moolenaar20431c92020-03-20 18:39:46 +01009010 // Don't execute this function body.
9011 ga_clear_strings(&ufunc->uf_lines);
9012
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009013 if (errormsg != NULL)
9014 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009015 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009016 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009017 }
9018
9019 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009020 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009021 if (do_estack_push)
9022 estack_pop();
9023
Bram Moolenaar20431c92020-03-20 18:39:46 +01009024 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009025 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009026 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009027 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009028}
9029
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009030 void
9031set_function_type(ufunc_T *ufunc)
9032{
9033 int varargs = ufunc->uf_va_name != NULL;
9034 int argcount = ufunc->uf_args.ga_len;
9035
9036 // Create a type for the function, with the return type and any
9037 // argument types.
9038 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9039 // The type is included in "tt_args".
9040 if (argcount > 0 || varargs)
9041 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009042 if (ufunc->uf_type_list.ga_itemsize == 0)
9043 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009044 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9045 argcount, &ufunc->uf_type_list);
9046 // Add argument types to the function type.
9047 if (func_type_add_arg_types(ufunc->uf_func_type,
9048 argcount + varargs,
9049 &ufunc->uf_type_list) == FAIL)
9050 return;
9051 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9052 ufunc->uf_func_type->tt_min_argcount =
9053 argcount - ufunc->uf_def_args.ga_len;
9054 if (ufunc->uf_arg_types == NULL)
9055 {
9056 int i;
9057
9058 // lambda does not have argument types.
9059 for (i = 0; i < argcount; ++i)
9060 ufunc->uf_func_type->tt_args[i] = &t_any;
9061 }
9062 else
9063 mch_memmove(ufunc->uf_func_type->tt_args,
9064 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9065 if (varargs)
9066 {
9067 ufunc->uf_func_type->tt_args[argcount] =
9068 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
9069 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9070 }
9071 }
9072 else
9073 // No arguments, can use a predefined type.
9074 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9075 argcount, &ufunc->uf_type_list);
9076}
9077
9078
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009079/*
9080 * Delete an instruction, free what it contains.
9081 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009082 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009083delete_instr(isn_T *isn)
9084{
9085 switch (isn->isn_type)
9086 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009087 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009088 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009089 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009090 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009091 case ISN_LOADENV:
9092 case ISN_LOADG:
9093 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009094 case ISN_LOADT:
9095 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009096 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009097 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009098 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009099 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009100 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009101 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009102 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009103 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009104 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009105 case ISN_STOREW:
9106 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009107 vim_free(isn->isn_arg.string);
9108 break;
9109
9110 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009111 case ISN_STORES:
9112 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009113 break;
9114
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009115 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02009116 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009117 vim_free(isn->isn_arg.unlet.ul_name);
9118 break;
9119
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009120 case ISN_STOREOPT:
9121 vim_free(isn->isn_arg.storeopt.so_name);
9122 break;
9123
9124 case ISN_PUSHBLOB: // push blob isn_arg.blob
9125 blob_unref(isn->isn_arg.blob);
9126 break;
9127
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009128 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009129#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009130 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009131#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009132 break;
9133
9134 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009135#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009136 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009137#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009138 break;
9139
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009140 case ISN_UCALL:
9141 vim_free(isn->isn_arg.ufunc.cuf_name);
9142 break;
9143
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009144 case ISN_FUNCREF:
9145 {
9146 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9147 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01009148 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02009149
Bram Moolenaar077a4232020-12-22 18:33:27 +01009150 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
9151 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02009152 }
9153 break;
9154
9155 case ISN_DCALL:
9156 {
9157 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9158 + isn->isn_arg.dfunc.cdf_idx;
9159
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009160 if (dfunc->df_ufunc != NULL
9161 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02009162 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009163 }
9164 break;
9165
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009166 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02009167 {
9168 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
9169 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
9170
9171 if (ufunc != NULL)
9172 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009173 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02009174 func_ptr_unref(ufunc);
9175 }
9176
9177 vim_free(lambda);
9178 vim_free(isn->isn_arg.newfunc.nf_global);
9179 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009180 break;
9181
Bram Moolenaar5e654232020-09-16 15:22:00 +02009182 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01009183 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02009184 free_type(isn->isn_arg.type.ct_type);
9185 break;
9186
Bram Moolenaar02194d22020-10-24 23:08:38 +02009187 case ISN_CMDMOD:
9188 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9189 ->cmod_filter_regmatch.regprog);
9190 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9191 break;
9192
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009193 case ISN_LOADSCRIPT:
9194 case ISN_STORESCRIPT:
9195 vim_free(isn->isn_arg.script.scriptref);
9196 break;
9197
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009198 case ISN_TRY:
9199 vim_free(isn->isn_arg.try.try_ref);
9200 break;
9201
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009202 case ISN_2BOOL:
9203 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02009204 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009205 case ISN_ADDBLOB:
9206 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009207 case ISN_ANYINDEX:
9208 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009209 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02009210 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009211 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009212 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009213 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02009214 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009215 case ISN_COMPAREANY:
9216 case ISN_COMPAREBLOB:
9217 case ISN_COMPAREBOOL:
9218 case ISN_COMPAREDICT:
9219 case ISN_COMPAREFLOAT:
9220 case ISN_COMPAREFUNC:
9221 case ISN_COMPARELIST:
9222 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009223 case ISN_COMPARESPECIAL:
9224 case ISN_COMPARESTRING:
9225 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02009226 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009227 case ISN_DROP:
9228 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009229 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009230 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009231 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009232 case ISN_EXECCONCAT:
9233 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009234 case ISN_FINALLY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009235 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009236 case ISN_GETITEM:
9237 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009238 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02009239 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02009240 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02009241 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009242 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009243 case ISN_LOADBDICT:
9244 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009245 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009246 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009247 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009248 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009249 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009250 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009251 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009252 case ISN_NEGATENR:
9253 case ISN_NEWDICT:
9254 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009255 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009256 case ISN_OPFLOAT:
9257 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009258 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02009259 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01009260 case ISN_PROF_END:
9261 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009262 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009263 case ISN_PUSHF:
9264 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009265 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009266 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009267 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01009268 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009269 case ISN_SHUFFLE:
9270 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009271 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01009272 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009273 case ISN_STORENR:
9274 case ISN_STOREOUTER:
9275 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009276 case ISN_STOREV:
9277 case ISN_STRINDEX:
9278 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009279 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +01009280 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +01009281 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01009282 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +01009283 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009284 // nothing allocated
9285 break;
9286 }
9287}
9288
9289/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009290 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01009291 */
9292 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009293delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009294{
9295 int idx;
9296
9297 ga_clear(&dfunc->df_def_args_isn);
9298
9299 if (dfunc->df_instr != NULL)
9300 {
9301 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
9302 delete_instr(dfunc->df_instr + idx);
9303 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009304 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009305 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01009306#ifdef FEAT_PROFILE
9307 if (dfunc->df_instr_prof != NULL)
9308 {
9309 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
9310 delete_instr(dfunc->df_instr_prof + idx);
9311 VIM_CLEAR(dfunc->df_instr_prof);
9312 dfunc->df_instr_prof = NULL;
9313 }
9314#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01009315
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009316 if (mark_deleted)
9317 dfunc->df_deleted = TRUE;
9318 if (dfunc->df_ufunc != NULL)
9319 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009320}
9321
9322/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009323 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009324 * function, unless another user function still uses it.
9325 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009326 */
9327 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009328unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009329{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009330 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009331 {
9332 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9333 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009334
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009335 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009336 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009337 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009338 ufunc->uf_dfunc_idx = 0;
9339 if (dfunc->df_ufunc == ufunc)
9340 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009341 }
9342}
9343
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009344/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009345 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009346 */
9347 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009348link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009349{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009350 if (ufunc->uf_dfunc_idx > 0)
9351 {
9352 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9353 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009354
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009355 ++dfunc->df_refcount;
9356 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009357}
9358
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009359#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009360/*
9361 * Free all functions defined with ":def".
9362 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009363 void
9364free_def_functions(void)
9365{
Bram Moolenaar20431c92020-03-20 18:39:46 +01009366 int idx;
9367
9368 for (idx = 0; idx < def_functions.ga_len; ++idx)
9369 {
9370 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
9371
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009372 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009373 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009374 }
9375
9376 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009377}
9378#endif
9379
9380
9381#endif // FEAT_EVAL