blob: 535de057c62dc937652f5c789a2de7ddcdba4c93 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
Bram Moolenaarced68a02021-01-24 17:53:47 +010047 int is_seen_skip_not; // a block was unconditionally executed
Bram Moolenaarefd88552020-06-18 20:50:10 +020048 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010049 int is_if_label; // instruction idx at IF or ELSEIF
50 endlabel_T *is_end_label; // instructions to set end label
51} ifscope_T;
52
53/*
54 * info specific for the scope of :while
55 */
56typedef struct {
57 int ws_top_label; // instruction idx at WHILE
58 endlabel_T *ws_end_label; // instructions to set end
59} whilescope_T;
60
61/*
62 * info specific for the scope of :for
63 */
64typedef struct {
65 int fs_top_label; // instruction idx at FOR
66 endlabel_T *fs_end_label; // break instructions
67} forscope_T;
68
69/*
70 * info specific for the scope of :try
71 */
72typedef struct {
73 int ts_try_label; // instruction idx at TRY
74 endlabel_T *ts_end_label; // jump to :finally or :endtry
75 int ts_catch_label; // instruction idx of last CATCH
76 int ts_caught_all; // "catch" without argument encountered
77} tryscope_T;
78
79typedef enum {
80 NO_SCOPE,
81 IF_SCOPE,
82 WHILE_SCOPE,
83 FOR_SCOPE,
84 TRY_SCOPE,
85 BLOCK_SCOPE
86} scopetype_T;
87
88/*
89 * Info for one scope, pointed to by "ctx_scope".
90 */
91typedef struct scope_S scope_T;
92struct scope_S {
93 scope_T *se_outer; // scope containing this one
94 scopetype_T se_type;
95 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020096 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010097 union {
98 ifscope_T se_if;
99 whilescope_T se_while;
100 forscope_T se_for;
101 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100102 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100103};
104
105/*
106 * Entry for "ctx_locals". Used for arguments and local variables.
107 */
108typedef struct {
109 char_u *lv_name;
110 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200111 int lv_idx; // index of the variable on the stack
Bram Moolenaarab360522021-01-10 14:02:28 +0100112 int lv_from_outer; // nesting level, using ctx_outer scope
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200113 int lv_const; // when TRUE cannot be assigned to
114 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115} lvar_T;
116
117/*
118 * Context for compiling lines of Vim script.
119 * Stores info about the local variables and condition stack.
120 */
121struct cctx_S {
122 ufunc_T *ctx_ufunc; // current function
123 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200124 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100125 garray_T ctx_instr; // generated instructions
126
Bram Moolenaarb2049902021-01-24 12:53:53 +0100127 int ctx_profiling; // when TRUE generate ISN_PROF_START
128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100129 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200130 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100131
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200132 int ctx_has_closure; // set to one if a closures was created in
133 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200134
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 garray_T ctx_imports; // imported items
136
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200137 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100138 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200139 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100140
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141 cctx_T *ctx_outer; // outer scope for lambda or nested
142 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200143 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200144
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100145 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200146 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200147
Bram Moolenaar02194d22020-10-24 23:08:38 +0200148 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149};
150
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100151static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100152
153/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100154 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100155 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100156 * If "lvar" is NULL only check if the variable can be found.
157 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100159 static int
160lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100161{
162 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100163 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100165 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100166 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167
168 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
170 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100171 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
172 if (STRNCMP(name, lvp->lv_name, len) == 0
173 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200174 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100175 if (lvar != NULL)
176 {
177 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100178 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100179 }
180 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200181 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200183
184 // Find local in outer function scope.
185 if (cctx->ctx_outer != NULL)
186 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100187 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200188 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100189 if (lvar != NULL)
190 {
191 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100192 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100193 }
194 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200195 }
196 }
197
Bram Moolenaar709664c2020-12-12 14:33:41 +0100198 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100199}
200
201/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200202 * Lookup an argument in the current function and an enclosing function.
203 * Returns the argument index in "idxp"
204 * Returns the argument type in "type"
205 * Sets "gen_load_outer" to TRUE if found in outer scope.
206 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207 */
208 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200209arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200210 char_u *name,
211 size_t len,
212 int *idxp,
213 type_T **type,
214 int *gen_load_outer,
215 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216{
217 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200218 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100220 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200221 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100222 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
223 {
224 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
225
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200226 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
227 {
228 if (idxp != NULL)
229 {
230 // Arguments are located above the frame pointer. One further
231 // if there is a vararg argument
232 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
233 + STACK_FRAME_SIZE)
234 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
235
236 if (cctx->ctx_ufunc->uf_arg_types != NULL)
237 *type = cctx->ctx_ufunc->uf_arg_types[idx];
238 else
239 *type = &t_any;
240 }
241 return OK;
242 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100243 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200245 va_name = cctx->ctx_ufunc->uf_va_name;
246 if (va_name != NULL
247 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
248 {
249 if (idxp != NULL)
250 {
251 // varargs is always the last argument
252 *idxp = -STACK_FRAME_SIZE - 1;
253 *type = cctx->ctx_ufunc->uf_va_type;
254 }
255 return OK;
256 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200258 if (cctx->ctx_outer != NULL)
259 {
260 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200261 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200262 == OK)
263 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100264 if (gen_load_outer != NULL)
265 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200266 return OK;
267 }
268 }
269
270 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271}
272
273/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200274 * Lookup a script-local variable in the current script, possibly defined in a
275 * block that contains the function "cctx->ctx_ufunc".
276 * "cctx" is NULL at the script level.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100277 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200278 * Return NULL when not found.
279 */
280 static sallvar_T *
281find_script_var(char_u *name, size_t len, cctx_T *cctx)
282{
283 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
284 hashitem_T *hi;
285 int cc;
286 sallvar_T *sav;
287 ufunc_T *ufunc;
288
289 // Find the list of all script variables with the right name.
290 if (len > 0)
291 {
292 cc = name[len];
293 name[len] = NUL;
294 }
295 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
296 if (len > 0)
297 name[len] = cc;
298 if (HASHITEM_EMPTY(hi))
299 return NULL;
300
301 sav = HI2SAV(hi);
302 if (sav->sav_block_id == 0 || cctx == NULL)
303 // variable defined in the script scope or not in a function.
304 return sav;
305
306 // Go over the variables with this name and find one that was visible
307 // from the function.
308 ufunc = cctx->ctx_ufunc;
309 while (sav != NULL)
310 {
311 int idx;
312
313 // Go over the blocks that this function was defined in. If the
314 // variable block ID matches it was visible to the function.
315 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
316 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
317 return sav;
318 sav = sav->sav_next;
319 }
320
321 return NULL;
322}
323
324/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100325 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200326 */
327 static int
328script_is_vim9()
329{
330 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
331}
332
333/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200334 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200335 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
336 * without "s:".
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200337 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100338 * Returns OK or FAIL.
339 */
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100340 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200341script_var_exists(char_u *name, size_t len, int vim9script, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100342{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200343 int is_vim9_script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100344
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200345 if (current_sctx.sc_sid <= 0)
346 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200347 is_vim9_script = script_is_vim9();
348 if (vim9script && !is_vim9_script)
Bram Moolenaar53900992020-08-22 19:02:02 +0200349 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200350 if (is_vim9_script)
351 {
352 // Check script variables that were visible where the function was
353 // defined.
354 if (find_script_var(name, len, cctx) != NULL)
355 return OK;
356 }
357 else
358 {
359 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
360 dictitem_T *di;
361 int cc;
362
363 // Check script variables that are currently visible
364 cc = name[len];
365 name[len] = NUL;
366 di = find_var_in_ht(ht, 0, name, TRUE);
367 name[len] = cc;
368 if (di != NULL)
369 return OK;
370 }
371
372 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100373}
374
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100375/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100376 * Return TRUE if "name" is a local variable, argument, script variable or
377 * imported.
378 */
379 static int
380variable_exists(char_u *name, size_t len, cctx_T *cctx)
381{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100382 return (cctx != NULL
383 && (lookup_local(name, len, NULL, cctx) == OK
384 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaare0890d62021-02-17 14:52:14 +0100385 || script_var_exists(name, len, FALSE, cctx) == OK
386 || find_imported(name, len, cctx) != NULL;
387}
388
389/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100390 * Return TRUE if "name" is a local variable, argument, script variable,
391 * imported or function.
392 */
393 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100394item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100395{
396 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100397 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100398
399 if (variable_exists(name, len, cctx))
400 return TRUE;
401
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100402 // This is similar to what is in lookup_scriptitem():
403 // Find a function, so that a following "->" works.
404 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
405 // a function call.
406 p = skipwhite(name + len);
407
408 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
409 {
410 // Do not check for an internal function, since it might also be a
411 // valid command, such as ":split" versuse "split()".
412 // Skip "g:" before a function name.
413 is_global = (name[0] == 'g' && name[1] == ':');
414 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
415 }
416 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100417}
418
419/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100420 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200421 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200422 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100423 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100424 * Return FAIL and give an error if it defined.
425 */
426 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100427check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100428{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200429 int c = p[len];
430 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200431
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100432 if (script_var_exists(p, len, FALSE, cctx) == OK)
433 {
434 if (is_arg)
435 semsg(_(e_argument_already_declared_in_script_str), p);
436 else
437 semsg(_(e_variable_already_declared_in_script_str), p);
438 return FAIL;
439 }
440
Bram Moolenaarad486a02020-08-01 23:22:18 +0200441 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100442 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100443 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200444 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200445 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200446 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100447 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200448 // A local or script-local function can shadow a global function.
449 if (ufunc == NULL || !func_is_global(ufunc)
450 || (p[0] == 'g' && p[1] == ':'))
451 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100452 if (is_arg)
453 semsg(_(e_argument_name_shadows_existing_variable_str), p);
454 else
455 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200456 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200457 return FAIL;
458 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100459 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200460 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100461 return OK;
462}
463
Bram Moolenaar65b95452020-07-19 14:03:09 +0200464
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100465/////////////////////////////////////////////////////////////////////
466// Following generate_ functions expect the caller to call ga_grow().
467
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200468#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
469#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100470
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100471/*
472 * Generate an instruction without arguments.
473 * Returns a pointer to the new instruction, NULL if failed.
474 */
475 static isn_T *
476generate_instr(cctx_T *cctx, isntype_T isn_type)
477{
478 garray_T *instr = &cctx->ctx_instr;
479 isn_T *isn;
480
Bram Moolenaar080457c2020-03-03 21:53:32 +0100481 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100482 if (ga_grow(instr, 1) == FAIL)
483 return NULL;
484 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
485 isn->isn_type = isn_type;
486 isn->isn_lnum = cctx->ctx_lnum + 1;
487 ++instr->ga_len;
488
489 return isn;
490}
491
492/*
493 * Generate an instruction without arguments.
494 * "drop" will be removed from the stack.
495 * Returns a pointer to the new instruction, NULL if failed.
496 */
497 static isn_T *
498generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
499{
500 garray_T *stack = &cctx->ctx_type_stack;
501
Bram Moolenaar080457c2020-03-03 21:53:32 +0100502 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100503 stack->ga_len -= drop;
504 return generate_instr(cctx, isn_type);
505}
506
507/*
508 * Generate instruction "isn_type" and put "type" on the type stack.
509 */
510 static isn_T *
511generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
512{
513 isn_T *isn;
514 garray_T *stack = &cctx->ctx_type_stack;
515
516 if ((isn = generate_instr(cctx, isn_type)) == NULL)
517 return NULL;
518
519 if (ga_grow(stack, 1) == FAIL)
520 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200521 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100522 ++stack->ga_len;
523
524 return isn;
525}
526
527/*
528 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200529 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530 */
531 static int
532may_generate_2STRING(int offset, cctx_T *cctx)
533{
534 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200535 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100536 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100537 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100538
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100539 RETURN_OK_IF_SKIP(cctx);
540 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200541 switch ((*type)->tt_type)
542 {
543 // nothing to be done
544 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100545
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200546 // conversion possible
547 case VAR_SPECIAL:
548 case VAR_BOOL:
549 case VAR_NUMBER:
550 case VAR_FLOAT:
551 break;
552
553 // conversion possible (with runtime check)
554 case VAR_ANY:
555 case VAR_UNKNOWN:
556 isntype = ISN_2STRING_ANY;
557 break;
558
559 // conversion not possible
560 case VAR_VOID:
561 case VAR_BLOB:
562 case VAR_FUNC:
563 case VAR_PARTIAL:
564 case VAR_LIST:
565 case VAR_DICT:
566 case VAR_JOB:
567 case VAR_CHANNEL:
568 to_string_error((*type)->tt_type);
569 return FAIL;
570 }
571
572 *type = &t_string;
573 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574 return FAIL;
575 isn->isn_arg.number = offset;
576
577 return OK;
578}
579
580 static int
581check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
582{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200583 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100584 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200585 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586 {
587 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200588 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200590 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591 return FAIL;
592 }
593 return OK;
594}
595
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200596 static int
597generate_add_instr(
598 cctx_T *cctx,
599 vartype_T vartype,
600 type_T *type1,
601 type_T *type2)
602{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100603 garray_T *stack = &cctx->ctx_type_stack;
604 isn_T *isn = generate_instr_drop(cctx,
605 vartype == VAR_NUMBER ? ISN_OPNR
606 : vartype == VAR_LIST ? ISN_ADDLIST
607 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200608#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100609 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200610#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100611 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200612
613 if (vartype != VAR_LIST && vartype != VAR_BLOB
614 && type1->tt_type != VAR_ANY
615 && type2->tt_type != VAR_ANY
616 && check_number_or_float(
617 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
618 return FAIL;
619
620 if (isn != NULL)
621 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100622
623 // When concatenating two lists with different member types the member type
624 // becomes "any".
625 if (vartype == VAR_LIST
626 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
627 && type1->tt_member != type2->tt_member)
628 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
629
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200630 return isn == NULL ? FAIL : OK;
631}
632
633/*
634 * Get the type to use for an instruction for an operation on "type1" and
635 * "type2". If they are matching use a type-specific instruction. Otherwise
636 * fall back to runtime type checking.
637 */
638 static vartype_T
639operator_type(type_T *type1, type_T *type2)
640{
641 if (type1->tt_type == type2->tt_type
642 && (type1->tt_type == VAR_NUMBER
643 || type1->tt_type == VAR_LIST
644#ifdef FEAT_FLOAT
645 || type1->tt_type == VAR_FLOAT
646#endif
647 || type1->tt_type == VAR_BLOB))
648 return type1->tt_type;
649 return VAR_ANY;
650}
651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100652/*
653 * Generate an instruction with two arguments. The instruction depends on the
654 * type of the arguments.
655 */
656 static int
657generate_two_op(cctx_T *cctx, char_u *op)
658{
659 garray_T *stack = &cctx->ctx_type_stack;
660 type_T *type1;
661 type_T *type2;
662 vartype_T vartype;
663 isn_T *isn;
664
Bram Moolenaar080457c2020-03-03 21:53:32 +0100665 RETURN_OK_IF_SKIP(cctx);
666
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200667 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100668 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
669 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200670 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100671
672 switch (*op)
673 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200674 case '+':
675 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100676 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100677 break;
678
679 case '-':
680 case '*':
681 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
682 op) == FAIL)
683 return FAIL;
684 if (vartype == VAR_NUMBER)
685 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
686#ifdef FEAT_FLOAT
687 else if (vartype == VAR_FLOAT)
688 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
689#endif
690 else
691 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
692 if (isn != NULL)
693 isn->isn_arg.op.op_type = *op == '*'
694 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
695 break;
696
Bram Moolenaar4c683752020-04-05 21:38:23 +0200697 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100698 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200699 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100700 && type2->tt_type != VAR_NUMBER))
701 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200702 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100703 return FAIL;
704 }
705 isn = generate_instr_drop(cctx,
706 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
707 if (isn != NULL)
708 isn->isn_arg.op.op_type = EXPR_REM;
709 break;
710 }
711
712 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200713 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100714 {
715 type_T *type = &t_any;
716
717#ifdef FEAT_FLOAT
718 // float+number and number+float results in float
719 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
720 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
721 type = &t_float;
722#endif
723 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
724 }
725
726 return OK;
727}
728
729/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200730 * Get the instruction to use for comparing "type1" with "type2"
731 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100732 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200733 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100734get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100735{
736 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100737
Bram Moolenaar4c683752020-04-05 21:38:23 +0200738 if (type1 == VAR_UNKNOWN)
739 type1 = VAR_ANY;
740 if (type2 == VAR_UNKNOWN)
741 type2 = VAR_ANY;
742
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743 if (type1 == type2)
744 {
745 switch (type1)
746 {
747 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
748 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
749 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
750 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
751 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
752 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
753 case VAR_LIST: isntype = ISN_COMPARELIST; break;
754 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
755 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756 default: isntype = ISN_COMPAREANY; break;
757 }
758 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200759 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100760 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100761 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762 isntype = ISN_COMPAREANY;
763
Bram Moolenaar657137c2021-01-09 15:45:23 +0100764 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100765 && (isntype == ISN_COMPAREBOOL
766 || isntype == ISN_COMPARESPECIAL
767 || isntype == ISN_COMPARENR
768 || isntype == ISN_COMPAREFLOAT))
769 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200770 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100771 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200772 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100773 }
774 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100775 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
777 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100778 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
779 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100780 && (type1 == VAR_BLOB || type2 == VAR_BLOB
781 || type1 == VAR_LIST || type2 == VAR_LIST))))
782 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200783 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200785 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100786 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200787 return isntype;
788}
789
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200790 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100791check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200792{
793 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
794 return FAIL;
795 return OK;
796}
797
Bram Moolenaara5565e42020-05-09 15:44:01 +0200798/*
799 * Generate an ISN_COMPARE* instruction with a boolean result.
800 */
801 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100802generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200803{
804 isntype_T isntype;
805 isn_T *isn;
806 garray_T *stack = &cctx->ctx_type_stack;
807 vartype_T type1;
808 vartype_T type2;
809
810 RETURN_OK_IF_SKIP(cctx);
811
812 // Get the known type of the two items on the stack. If they are matching
813 // use a type-specific instruction. Otherwise fall back to runtime type
814 // checking.
815 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
816 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100817 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200818 if (isntype == ISN_DROP)
819 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100820
821 if ((isn = generate_instr(cctx, isntype)) == NULL)
822 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100823 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100824 isn->isn_arg.op.op_ic = ic;
825
826 // takes two arguments, puts one bool back
827 if (stack->ga_len >= 2)
828 {
829 --stack->ga_len;
830 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
831 }
832
833 return OK;
834}
835
836/*
837 * Generate an ISN_2BOOL instruction.
838 */
839 static int
840generate_2BOOL(cctx_T *cctx, int invert)
841{
842 isn_T *isn;
843 garray_T *stack = &cctx->ctx_type_stack;
844
Bram Moolenaar080457c2020-03-03 21:53:32 +0100845 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100846 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
847 return FAIL;
848 isn->isn_arg.number = invert;
849
850 // type becomes bool
851 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
852
853 return OK;
854}
855
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200856/*
857 * Generate an ISN_COND2BOOL instruction.
858 */
859 static int
860generate_COND2BOOL(cctx_T *cctx)
861{
862 isn_T *isn;
863 garray_T *stack = &cctx->ctx_type_stack;
864
865 RETURN_OK_IF_SKIP(cctx);
866 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
867 return FAIL;
868
869 // type becomes bool
870 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
871
872 return OK;
873}
874
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100875 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200876generate_TYPECHECK(
877 cctx_T *cctx,
878 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100879 int offset,
880 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100881{
882 isn_T *isn;
883 garray_T *stack = &cctx->ctx_type_stack;
884
Bram Moolenaar080457c2020-03-03 21:53:32 +0100885 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100886 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
887 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200888 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100889 isn->isn_arg.type.ct_off = (int8_T)offset;
890 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891
Bram Moolenaar5e654232020-09-16 15:22:00 +0200892 // type becomes expected
893 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100894
895 return OK;
896}
897
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100898 static int
899generate_SETTYPE(
900 cctx_T *cctx,
901 type_T *expected)
902{
903 isn_T *isn;
904
905 RETURN_OK_IF_SKIP(cctx);
906 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
907 return FAIL;
908 isn->isn_arg.type.ct_type = alloc_type(expected);
909 return OK;
910}
911
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100912/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200913 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
914 * used. Return FALSE if the types will never match.
915 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100916 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200917use_typecheck(type_T *actual, type_T *expected)
918{
919 if (actual->tt_type == VAR_ANY
920 || actual->tt_type == VAR_UNKNOWN
921 || (actual->tt_type == VAR_FUNC
922 && (expected->tt_type == VAR_FUNC
923 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100924 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
925 && ((actual->tt_member == &t_void)
926 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200927 return TRUE;
928 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
929 && actual->tt_type == expected->tt_type)
930 // This takes care of a nested list or dict.
931 return use_typecheck(actual->tt_member, expected->tt_member);
932 return FALSE;
933}
934
935/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200936 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200937 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200938 * - "actual" is a type that can be "expected" type: add a runtime check; or
939 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200940 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
941 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200942 */
Bram Moolenaar351ead02021-01-16 16:07:01 +0100943 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200944need_type(
945 type_T *actual,
946 type_T *expected,
947 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100948 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200949 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200950 int silent,
951 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200952{
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100953 where_T where;
954
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200955 if (expected == &t_bool && actual != &t_bool
956 && (actual->tt_flags & TTFLAG_BOOL_OK))
957 {
958 // Using "0", "1" or the result of an expression with "&&" or "||" as a
959 // boolean is OK but requires a conversion.
960 generate_2BOOL(cctx, FALSE);
961 return OK;
962 }
963
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100964 where.wt_index = arg_idx;
965 where.wt_variable = FALSE;
966 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200967 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200968
969 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200970 // If it's a constant a runtime check makes no sense.
971 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200972 {
Bram Moolenaare32e5162021-01-21 20:21:29 +0100973 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200974 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200975 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200976
977 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100978 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200979 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200980}
981
982/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100983 * Check that the top of the type stack has a type that can be used as a
984 * condition. Give an error and return FAIL if not.
985 */
986 static int
987bool_on_stack(cctx_T *cctx)
988{
989 garray_T *stack = &cctx->ctx_type_stack;
990 type_T *type;
991
992 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
993 if (type == &t_bool)
994 return OK;
995
996 if (type == &t_any || type == &t_number)
997 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
998 // This requires a runtime type check.
999 return generate_COND2BOOL(cctx);
1000
Bram Moolenaar351ead02021-01-16 16:07:01 +01001001 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001002}
1003
1004/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001005 * Generate an ISN_PUSHNR instruction.
1006 */
1007 static int
1008generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1009{
1010 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001011 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001012
Bram Moolenaar080457c2020-03-03 21:53:32 +01001013 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001014 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1015 return FAIL;
1016 isn->isn_arg.number = number;
1017
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001018 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001019 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001020 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001021 return OK;
1022}
1023
1024/*
1025 * Generate an ISN_PUSHBOOL instruction.
1026 */
1027 static int
1028generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1029{
1030 isn_T *isn;
1031
Bram Moolenaar080457c2020-03-03 21:53:32 +01001032 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1034 return FAIL;
1035 isn->isn_arg.number = number;
1036
1037 return OK;
1038}
1039
1040/*
1041 * Generate an ISN_PUSHSPEC instruction.
1042 */
1043 static int
1044generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1045{
1046 isn_T *isn;
1047
Bram Moolenaar080457c2020-03-03 21:53:32 +01001048 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001049 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1050 return FAIL;
1051 isn->isn_arg.number = number;
1052
1053 return OK;
1054}
1055
1056#ifdef FEAT_FLOAT
1057/*
1058 * Generate an ISN_PUSHF instruction.
1059 */
1060 static int
1061generate_PUSHF(cctx_T *cctx, float_T fnumber)
1062{
1063 isn_T *isn;
1064
Bram Moolenaar080457c2020-03-03 21:53:32 +01001065 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1067 return FAIL;
1068 isn->isn_arg.fnumber = fnumber;
1069
1070 return OK;
1071}
1072#endif
1073
1074/*
1075 * Generate an ISN_PUSHS instruction.
1076 * Consumes "str".
1077 */
1078 static int
1079generate_PUSHS(cctx_T *cctx, char_u *str)
1080{
1081 isn_T *isn;
1082
Bram Moolenaar508b5612021-01-02 18:17:26 +01001083 if (cctx->ctx_skip == SKIP_YES)
1084 {
1085 vim_free(str);
1086 return OK;
1087 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1089 return FAIL;
1090 isn->isn_arg.string = str;
1091
1092 return OK;
1093}
1094
1095/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001096 * Generate an ISN_PUSHCHANNEL instruction.
1097 * Consumes "channel".
1098 */
1099 static int
1100generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1101{
1102 isn_T *isn;
1103
Bram Moolenaar080457c2020-03-03 21:53:32 +01001104 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001105 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1106 return FAIL;
1107 isn->isn_arg.channel = channel;
1108
1109 return OK;
1110}
1111
1112/*
1113 * Generate an ISN_PUSHJOB instruction.
1114 * Consumes "job".
1115 */
1116 static int
1117generate_PUSHJOB(cctx_T *cctx, job_T *job)
1118{
1119 isn_T *isn;
1120
Bram Moolenaar080457c2020-03-03 21:53:32 +01001121 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001122 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001123 return FAIL;
1124 isn->isn_arg.job = job;
1125
1126 return OK;
1127}
1128
1129/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001130 * Generate an ISN_PUSHBLOB instruction.
1131 * Consumes "blob".
1132 */
1133 static int
1134generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1135{
1136 isn_T *isn;
1137
Bram Moolenaar080457c2020-03-03 21:53:32 +01001138 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001139 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1140 return FAIL;
1141 isn->isn_arg.blob = blob;
1142
1143 return OK;
1144}
1145
1146/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001147 * Generate an ISN_PUSHFUNC instruction with name "name".
1148 * Consumes "name".
1149 */
1150 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001151generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001152{
1153 isn_T *isn;
1154
Bram Moolenaar080457c2020-03-03 21:53:32 +01001155 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001156 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001157 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001158 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001159
1160 return OK;
1161}
1162
1163/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001164 * Generate an ISN_GETITEM instruction with "index".
1165 */
1166 static int
1167generate_GETITEM(cctx_T *cctx, int index)
1168{
1169 isn_T *isn;
1170 garray_T *stack = &cctx->ctx_type_stack;
1171 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1172 type_T *item_type = &t_any;
1173
1174 RETURN_OK_IF_SKIP(cctx);
1175
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001176 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001177 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001178 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001179 emsg(_(e_listreq));
1180 return FAIL;
1181 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001182 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001183 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1184 return FAIL;
1185 isn->isn_arg.number = index;
1186
1187 // add the item type to the type stack
1188 if (ga_grow(stack, 1) == FAIL)
1189 return FAIL;
1190 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1191 ++stack->ga_len;
1192 return OK;
1193}
1194
1195/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001196 * Generate an ISN_SLICE instruction with "count".
1197 */
1198 static int
1199generate_SLICE(cctx_T *cctx, int count)
1200{
1201 isn_T *isn;
1202
1203 RETURN_OK_IF_SKIP(cctx);
1204 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1205 return FAIL;
1206 isn->isn_arg.number = count;
1207 return OK;
1208}
1209
1210/*
1211 * Generate an ISN_CHECKLEN instruction with "min_len".
1212 */
1213 static int
1214generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1215{
1216 isn_T *isn;
1217
1218 RETURN_OK_IF_SKIP(cctx);
1219
1220 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1221 return FAIL;
1222 isn->isn_arg.checklen.cl_min_len = min_len;
1223 isn->isn_arg.checklen.cl_more_OK = more_OK;
1224
1225 return OK;
1226}
1227
1228/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001229 * Generate an ISN_STORE instruction.
1230 */
1231 static int
1232generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1233{
1234 isn_T *isn;
1235
Bram Moolenaar080457c2020-03-03 21:53:32 +01001236 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001237 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1238 return FAIL;
1239 if (name != NULL)
1240 isn->isn_arg.string = vim_strsave(name);
1241 else
1242 isn->isn_arg.number = idx;
1243
1244 return OK;
1245}
1246
1247/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001248 * Generate an ISN_STOREOUTER instruction.
1249 */
1250 static int
1251generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1252{
1253 isn_T *isn;
1254
1255 RETURN_OK_IF_SKIP(cctx);
1256 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1257 return FAIL;
1258 isn->isn_arg.outer.outer_idx = idx;
1259 isn->isn_arg.outer.outer_depth = level;
1260
1261 return OK;
1262}
1263
1264/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001265 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1266 */
1267 static int
1268generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1269{
1270 isn_T *isn;
1271
Bram Moolenaar080457c2020-03-03 21:53:32 +01001272 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1274 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001275 isn->isn_arg.storenr.stnr_idx = idx;
1276 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277
1278 return OK;
1279}
1280
1281/*
1282 * Generate an ISN_STOREOPT instruction
1283 */
1284 static int
1285generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1286{
1287 isn_T *isn;
1288
Bram Moolenaar080457c2020-03-03 21:53:32 +01001289 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001290 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291 return FAIL;
1292 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1293 isn->isn_arg.storeopt.so_flags = opt_flags;
1294
1295 return OK;
1296}
1297
1298/*
1299 * Generate an ISN_LOAD or similar instruction.
1300 */
1301 static int
1302generate_LOAD(
1303 cctx_T *cctx,
1304 isntype_T isn_type,
1305 int idx,
1306 char_u *name,
1307 type_T *type)
1308{
1309 isn_T *isn;
1310
Bram Moolenaar080457c2020-03-03 21:53:32 +01001311 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1313 return FAIL;
1314 if (name != NULL)
1315 isn->isn_arg.string = vim_strsave(name);
1316 else
1317 isn->isn_arg.number = idx;
1318
1319 return OK;
1320}
1321
1322/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001323 * Generate an ISN_LOADOUTER instruction
1324 */
1325 static int
1326generate_LOADOUTER(
1327 cctx_T *cctx,
1328 int idx,
1329 int nesting,
1330 type_T *type)
1331{
1332 isn_T *isn;
1333
1334 RETURN_OK_IF_SKIP(cctx);
1335 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1336 return FAIL;
1337 isn->isn_arg.outer.outer_idx = idx;
1338 isn->isn_arg.outer.outer_depth = nesting;
1339
1340 return OK;
1341}
1342
1343/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001344 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001345 */
1346 static int
1347generate_LOADV(
1348 cctx_T *cctx,
1349 char_u *name,
1350 int error)
1351{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001352 int di_flags;
1353 int vidx = find_vim_var(name, &di_flags);
1354 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001355
Bram Moolenaar080457c2020-03-03 21:53:32 +01001356 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001357 if (vidx < 0)
1358 {
1359 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001360 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001361 return FAIL;
1362 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001363 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001364
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001365 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001366}
1367
1368/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001369 * Generate an ISN_UNLET instruction.
1370 */
1371 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001372generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001373{
1374 isn_T *isn;
1375
1376 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001377 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001378 return FAIL;
1379 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1380 isn->isn_arg.unlet.ul_forceit = forceit;
1381
1382 return OK;
1383}
1384
1385/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001386 * Generate an ISN_LOCKCONST instruction.
1387 */
1388 static int
1389generate_LOCKCONST(cctx_T *cctx)
1390{
1391 isn_T *isn;
1392
1393 RETURN_OK_IF_SKIP(cctx);
1394 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1395 return FAIL;
1396 return OK;
1397}
1398
1399/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400 * Generate an ISN_LOADS instruction.
1401 */
1402 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001403generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001405 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001407 int sid,
1408 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409{
1410 isn_T *isn;
1411
Bram Moolenaar080457c2020-03-03 21:53:32 +01001412 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001413 if (isn_type == ISN_LOADS)
1414 isn = generate_instr_type(cctx, isn_type, type);
1415 else
1416 isn = generate_instr_drop(cctx, isn_type, 1);
1417 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001418 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001419 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1420 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421
1422 return OK;
1423}
1424
1425/*
1426 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1427 */
1428 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001429generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001430 cctx_T *cctx,
1431 isntype_T isn_type,
1432 int sid,
1433 int idx,
1434 type_T *type)
1435{
1436 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001437 scriptref_T *sref;
1438 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001439
Bram Moolenaar080457c2020-03-03 21:53:32 +01001440 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441 if (isn_type == ISN_LOADSCRIPT)
1442 isn = generate_instr_type(cctx, isn_type, type);
1443 else
1444 isn = generate_instr_drop(cctx, isn_type, 1);
1445 if (isn == NULL)
1446 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001447
1448 // This requires three arguments, which doesn't fit in an instruction, thus
1449 // we need to allocate a struct for this.
1450 sref = ALLOC_ONE(scriptref_T);
1451 if (sref == NULL)
1452 return FAIL;
1453 isn->isn_arg.script.scriptref = sref;
1454 sref->sref_sid = sid;
1455 sref->sref_idx = idx;
1456 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001457 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001458 return OK;
1459}
1460
1461/*
1462 * Generate an ISN_NEWLIST instruction.
1463 */
1464 static int
1465generate_NEWLIST(cctx_T *cctx, int count)
1466{
1467 isn_T *isn;
1468 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001469 type_T *type;
1470 type_T *member;
1471
Bram Moolenaar080457c2020-03-03 21:53:32 +01001472 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1474 return FAIL;
1475 isn->isn_arg.number = count;
1476
Bram Moolenaar127542b2020-08-09 17:22:04 +02001477 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001478 if (count == 0)
1479 member = &t_void;
1480 else
1481 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001482 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1483 cctx->ctx_type_list);
1484 type = get_list_type(member, cctx->ctx_type_list);
1485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001486 // drop the value types
1487 stack->ga_len -= count;
1488
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001489 // add the list type to the type stack
1490 if (ga_grow(stack, 1) == FAIL)
1491 return FAIL;
1492 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1493 ++stack->ga_len;
1494
1495 return OK;
1496}
1497
1498/*
1499 * Generate an ISN_NEWDICT instruction.
1500 */
1501 static int
1502generate_NEWDICT(cctx_T *cctx, int count)
1503{
1504 isn_T *isn;
1505 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001506 type_T *type;
1507 type_T *member;
1508
Bram Moolenaar080457c2020-03-03 21:53:32 +01001509 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1511 return FAIL;
1512 isn->isn_arg.number = count;
1513
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001514 if (count == 0)
1515 member = &t_void;
1516 else
1517 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001518 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1519 cctx->ctx_type_list);
1520 type = get_dict_type(member, cctx->ctx_type_list);
1521
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522 // drop the key and value types
1523 stack->ga_len -= 2 * count;
1524
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001525 // add the dict type to the type stack
1526 if (ga_grow(stack, 1) == FAIL)
1527 return FAIL;
1528 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1529 ++stack->ga_len;
1530
1531 return OK;
1532}
1533
1534/*
1535 * Generate an ISN_FUNCREF instruction.
1536 */
1537 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001538generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001539{
1540 isn_T *isn;
1541 garray_T *stack = &cctx->ctx_type_stack;
1542
Bram Moolenaar080457c2020-03-03 21:53:32 +01001543 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1545 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001546 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001547 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001548
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001549 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001550 // the nested context, including this one.
1551 if (ufunc->uf_flags & FC_CLOSURE)
1552 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1553
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001554 if (ga_grow(stack, 1) == FAIL)
1555 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001556 ((type_T **)stack->ga_data)[stack->ga_len] =
1557 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 ++stack->ga_len;
1559
1560 return OK;
1561}
1562
1563/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001564 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001565 * "lambda_name" and "func_name" must be in allocated memory and will be
1566 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001567 */
1568 static int
1569generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1570{
1571 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001572
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001573 if (cctx->ctx_skip == SKIP_YES)
1574 {
1575 vim_free(lambda_name);
1576 vim_free(func_name);
1577 return OK;
1578 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001579 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001580 {
1581 vim_free(lambda_name);
1582 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001583 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001584 }
1585 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001586 isn->isn_arg.newfunc.nf_global = func_name;
1587
1588 return OK;
1589}
1590
1591/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001592 * Generate an ISN_DEF instruction: list functions
1593 */
1594 static int
1595generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1596{
1597 isn_T *isn;
1598
1599 RETURN_OK_IF_SKIP(cctx);
1600 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1601 return FAIL;
1602 if (len > 0)
1603 {
1604 isn->isn_arg.string = vim_strnsave(name, len);
1605 if (isn->isn_arg.string == NULL)
1606 return FAIL;
1607 }
1608 return OK;
1609}
1610
1611/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001612 * Generate an ISN_JUMP instruction.
1613 */
1614 static int
1615generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1616{
1617 isn_T *isn;
1618 garray_T *stack = &cctx->ctx_type_stack;
1619
Bram Moolenaar080457c2020-03-03 21:53:32 +01001620 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1622 return FAIL;
1623 isn->isn_arg.jump.jump_when = when;
1624 isn->isn_arg.jump.jump_where = where;
1625
1626 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1627 --stack->ga_len;
1628
1629 return OK;
1630}
1631
1632 static int
1633generate_FOR(cctx_T *cctx, int loop_idx)
1634{
1635 isn_T *isn;
1636 garray_T *stack = &cctx->ctx_type_stack;
1637
Bram Moolenaar080457c2020-03-03 21:53:32 +01001638 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001639 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1640 return FAIL;
1641 isn->isn_arg.forloop.for_idx = loop_idx;
1642
1643 if (ga_grow(stack, 1) == FAIL)
1644 return FAIL;
1645 // type doesn't matter, will be stored next
1646 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1647 ++stack->ga_len;
1648
1649 return OK;
1650}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001651/*
1652 * Generate an ISN_TRYCONT instruction.
1653 */
1654 static int
1655generate_TRYCONT(cctx_T *cctx, int levels, int where)
1656{
1657 isn_T *isn;
1658
1659 RETURN_OK_IF_SKIP(cctx);
1660 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1661 return FAIL;
1662 isn->isn_arg.trycont.tct_levels = levels;
1663 isn->isn_arg.trycont.tct_where = where;
1664
1665 return OK;
1666}
1667
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001668
1669/*
1670 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001671 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001672 * Return FAIL if the number of arguments is wrong.
1673 */
1674 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001675generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001676{
1677 isn_T *isn;
1678 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001679 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001680 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001681 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001682
Bram Moolenaar080457c2020-03-03 21:53:32 +01001683 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001684 argoff = check_internal_func(func_idx, argcount);
1685 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001686 return FAIL;
1687
Bram Moolenaar389df252020-07-09 21:20:47 +02001688 if (method_call && argoff > 1)
1689 {
1690 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1691 return FAIL;
1692 isn->isn_arg.shuffle.shfl_item = argcount;
1693 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1694 }
1695
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001696 if (argcount > 0)
1697 {
1698 // Check the types of the arguments.
1699 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001700 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1701 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001702 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001703 if (internal_func_is_map(func_idx))
1704 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001705 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001706
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1708 return FAIL;
1709 isn->isn_arg.bfunc.cbf_idx = func_idx;
1710 isn->isn_arg.bfunc.cbf_argcount = argcount;
1711
Bram Moolenaar94738d82020-10-21 14:25:07 +02001712 // Drop the argument types and push the return type.
1713 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001714 if (ga_grow(stack, 1) == FAIL)
1715 return FAIL;
1716 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001717 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001718 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001720 if (maptype != NULL && maptype->tt_member != NULL
1721 && maptype->tt_member != &t_any)
1722 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001723 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001724
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001725 return OK;
1726}
1727
1728/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001729 * Generate an ISN_LISTAPPEND instruction. Works like add().
1730 * Argument count is already checked.
1731 */
1732 static int
1733generate_LISTAPPEND(cctx_T *cctx)
1734{
1735 garray_T *stack = &cctx->ctx_type_stack;
1736 type_T *list_type;
1737 type_T *item_type;
1738 type_T *expected;
1739
1740 // Caller already checked that list_type is a list.
1741 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1742 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1743 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001744 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001745 return FAIL;
1746
1747 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1748 return FAIL;
1749
1750 --stack->ga_len; // drop the argument
1751 return OK;
1752}
1753
1754/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001755 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1756 * Argument count is already checked.
1757 */
1758 static int
1759generate_BLOBAPPEND(cctx_T *cctx)
1760{
1761 garray_T *stack = &cctx->ctx_type_stack;
1762 type_T *item_type;
1763
1764 // Caller already checked that blob_type is a blob.
1765 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001766 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001767 return FAIL;
1768
1769 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1770 return FAIL;
1771
1772 --stack->ga_len; // drop the argument
1773 return OK;
1774}
1775
1776/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001777 * Return TRUE if "ufunc" should be compiled, taking into account whether
1778 * "profile" indicates profiling is to be done.
1779 */
1780 int
Bram Moolenaarf002a412021-01-24 13:34:18 +01001781func_needs_compiling(ufunc_T *ufunc, int profile UNUSED)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001782{
1783 switch (ufunc->uf_def_status)
1784 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001785 case UF_NOT_COMPILED: break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001786 case UF_TO_BE_COMPILED: return TRUE;
1787 case UF_COMPILED:
1788 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001789#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001790 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1791 + ufunc->uf_dfunc_idx;
1792
1793 return profile ? dfunc->df_instr_prof == NULL
1794 : dfunc->df_instr == NULL;
Bram Moolenaarf002a412021-01-24 13:34:18 +01001795#else
1796 break;
1797#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01001798 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001799 case UF_COMPILING: break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001800 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001801 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001802}
1803
1804/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805 * Generate an ISN_DCALL or ISN_UCALL instruction.
1806 * Return FAIL if the number of arguments is wrong.
1807 */
1808 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001809generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810{
1811 isn_T *isn;
1812 garray_T *stack = &cctx->ctx_type_stack;
1813 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001814 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001815
Bram Moolenaar080457c2020-03-03 21:53:32 +01001816 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001817 if (argcount > regular_args && !has_varargs(ufunc))
1818 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001819 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820 return FAIL;
1821 }
1822 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1823 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001824 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825 return FAIL;
1826 }
1827
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001828 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001829 {
1830 int i;
1831
1832 for (i = 0; i < argcount; ++i)
1833 {
1834 type_T *expected;
1835 type_T *actual;
1836
1837 if (i < regular_args)
1838 {
1839 if (ufunc->uf_arg_types == NULL)
1840 continue;
1841 expected = ufunc->uf_arg_types[i];
1842 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001843 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1844 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001845 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001846 else
1847 expected = ufunc->uf_va_type->tt_member;
1848 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001849 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001850 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001851 {
1852 arg_type_mismatch(expected, actual, i + 1);
1853 return FAIL;
1854 }
1855 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001856 if (func_needs_compiling(ufunc, PROFILING(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001857 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001858 PROFILING(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001859 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001860 }
1861
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001862 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001863 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001864 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001865 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001866 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001867 {
1868 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1869 isn->isn_arg.dfunc.cdf_argcount = argcount;
1870 }
1871 else
1872 {
1873 // A user function may be deleted and redefined later, can't use the
1874 // ufunc pointer, need to look it up again at runtime.
1875 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1876 isn->isn_arg.ufunc.cuf_argcount = argcount;
1877 }
1878
1879 stack->ga_len -= argcount; // drop the arguments
1880 if (ga_grow(stack, 1) == FAIL)
1881 return FAIL;
1882 // add return value
1883 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1884 ++stack->ga_len;
1885
1886 return OK;
1887}
1888
1889/*
1890 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1891 */
1892 static int
1893generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1894{
1895 isn_T *isn;
1896 garray_T *stack = &cctx->ctx_type_stack;
1897
Bram Moolenaar080457c2020-03-03 21:53:32 +01001898 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001899 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1900 return FAIL;
1901 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1902 isn->isn_arg.ufunc.cuf_argcount = argcount;
1903
1904 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001905 if (ga_grow(stack, 1) == FAIL)
1906 return FAIL;
1907 // add return value
1908 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1909 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001910
1911 return OK;
1912}
1913
1914/*
1915 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001916 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917 */
1918 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001919generate_PCALL(
1920 cctx_T *cctx,
1921 int argcount,
1922 char_u *name,
1923 type_T *type,
1924 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925{
1926 isn_T *isn;
1927 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001928 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929
Bram Moolenaar080457c2020-03-03 21:53:32 +01001930 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001931
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001932 if (type->tt_type == VAR_ANY)
1933 ret_type = &t_any;
1934 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001935 {
1936 if (type->tt_argcount != -1)
1937 {
1938 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1939
1940 if (argcount < type->tt_min_argcount - varargs)
1941 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001942 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001943 return FAIL;
1944 }
1945 if (!varargs && argcount > type->tt_argcount)
1946 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001947 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001948 return FAIL;
1949 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001950 if (type->tt_args != NULL)
1951 {
1952 int i;
1953
1954 for (i = 0; i < argcount; ++i)
1955 {
1956 int offset = -argcount + i - 1;
1957 type_T *actual = ((type_T **)stack->ga_data)[
1958 stack->ga_len + offset];
1959 type_T *expected;
1960
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001961 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001962 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001963 type->tt_argcount - 1]->tt_member;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001964 else
1965 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001966 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001967 cctx, TRUE, FALSE) == FAIL)
1968 {
1969 arg_type_mismatch(expected, actual, i + 1);
1970 return FAIL;
1971 }
1972 }
1973 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001974 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001975 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001976 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001977 else
1978 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001979 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001980 return FAIL;
1981 }
1982
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1984 return FAIL;
1985 isn->isn_arg.pfunc.cpf_top = at_top;
1986 isn->isn_arg.pfunc.cpf_argcount = argcount;
1987
1988 stack->ga_len -= argcount; // drop the arguments
1989
1990 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001991 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001992
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001993 // If partial is above the arguments it must be cleared and replaced with
1994 // the return value.
1995 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1996 return FAIL;
1997
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001998 return OK;
1999}
2000
2001/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002002 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 */
2004 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002005generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006{
2007 isn_T *isn;
2008 garray_T *stack = &cctx->ctx_type_stack;
2009 type_T *type;
2010
Bram Moolenaar080457c2020-03-03 21:53:32 +01002011 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002012 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002014 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002015
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002016 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002018 if (type->tt_type != VAR_DICT && type != &t_any)
2019 {
2020 emsg(_(e_dictreq));
2021 return FAIL;
2022 }
2023 // change dict type to dict member type
2024 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002025 {
2026 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2027 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2028 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029
2030 return OK;
2031}
2032
2033/*
2034 * Generate an ISN_ECHO instruction.
2035 */
2036 static int
2037generate_ECHO(cctx_T *cctx, int with_white, int count)
2038{
2039 isn_T *isn;
2040
Bram Moolenaar080457c2020-03-03 21:53:32 +01002041 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2043 return FAIL;
2044 isn->isn_arg.echo.echo_with_white = with_white;
2045 isn->isn_arg.echo.echo_count = count;
2046
2047 return OK;
2048}
2049
Bram Moolenaarad39c092020-02-26 18:23:43 +01002050/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002051 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002052 */
2053 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002054generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002055{
2056 isn_T *isn;
2057
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002058 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002059 return FAIL;
2060 isn->isn_arg.number = count;
2061
2062 return OK;
2063}
2064
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002065/*
2066 * Generate an ISN_PUT instruction.
2067 */
2068 static int
2069generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2070{
2071 isn_T *isn;
2072
2073 RETURN_OK_IF_SKIP(cctx);
2074 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2075 return FAIL;
2076 isn->isn_arg.put.put_regname = regname;
2077 isn->isn_arg.put.put_lnum = lnum;
2078 return OK;
2079}
2080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002081 static int
2082generate_EXEC(cctx_T *cctx, char_u *line)
2083{
2084 isn_T *isn;
2085
Bram Moolenaar080457c2020-03-03 21:53:32 +01002086 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002087 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2088 return FAIL;
2089 isn->isn_arg.string = vim_strsave(line);
2090 return OK;
2091}
2092
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002093 static int
2094generate_EXECCONCAT(cctx_T *cctx, int count)
2095{
2096 isn_T *isn;
2097
2098 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2099 return FAIL;
2100 isn->isn_arg.number = count;
2101 return OK;
2102}
2103
Bram Moolenaar08597872020-12-10 19:43:40 +01002104/*
2105 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2106 */
2107 static int
2108generate_RANGE(cctx_T *cctx, char_u *range)
2109{
2110 isn_T *isn;
2111 garray_T *stack = &cctx->ctx_type_stack;
2112
2113 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2114 return FAIL;
2115 isn->isn_arg.string = range;
2116
2117 if (ga_grow(stack, 1) == FAIL)
2118 return FAIL;
2119 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2120 ++stack->ga_len;
2121 return OK;
2122}
2123
Bram Moolenaar792f7862020-11-23 08:31:18 +01002124 static int
2125generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2126{
2127 isn_T *isn;
2128
2129 RETURN_OK_IF_SKIP(cctx);
2130 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2131 return FAIL;
2132 isn->isn_arg.unpack.unp_count = var_count;
2133 isn->isn_arg.unpack.unp_semicolon = semicolon;
2134 return OK;
2135}
2136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002137/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002138 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002139 */
2140 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002141generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002142{
2143 isn_T *isn;
2144
Bram Moolenaar02194d22020-10-24 23:08:38 +02002145 if (cmod->cmod_flags != 0
2146 || cmod->cmod_split != 0
2147 || cmod->cmod_verbose != 0
2148 || cmod->cmod_tab != 0
2149 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002150 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002151 cctx->ctx_has_cmdmod = TRUE;
2152
2153 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002154 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002155 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2156 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2157 return FAIL;
2158 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002159 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002160 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002161 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002162
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002163 return OK;
2164}
2165
2166 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002167generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002168{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002169 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2170 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002171 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002172 return OK;
2173}
2174
Bram Moolenaarf002a412021-01-24 13:34:18 +01002175#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002176 static void
2177may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2178{
2179 if (cctx->ctx_profiling && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002180 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002181}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002182#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002183
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002184/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002185 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002186 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002187 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002188 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002189reserve_local(
2190 cctx_T *cctx,
2191 char_u *name,
2192 size_t len,
2193 int isConst,
2194 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002195{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002196 lvar_T *lvar;
2197
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002198 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002199 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002200 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002201 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002202 }
2203
2204 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002205 return NULL;
2206 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002207 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002208
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002209 // Every local variable uses the next entry on the stack. We could re-use
2210 // the last ones when leaving a scope, but then variables used in a closure
2211 // might get overwritten. To keep things simple do not re-use stack
2212 // entries. This is less efficient, but memory is cheap these days.
2213 lvar->lv_idx = cctx->ctx_locals_count++;
2214
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002215 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002216 lvar->lv_const = isConst;
2217 lvar->lv_type = type;
2218
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002219 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002220}
2221
2222/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002223 * Remove local variables above "new_top".
2224 */
2225 static void
2226unwind_locals(cctx_T *cctx, int new_top)
2227{
2228 if (cctx->ctx_locals.ga_len > new_top)
2229 {
2230 int idx;
2231 lvar_T *lvar;
2232
2233 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2234 {
2235 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2236 vim_free(lvar->lv_name);
2237 }
2238 }
2239 cctx->ctx_locals.ga_len = new_top;
2240}
2241
2242/*
2243 * Free all local variables.
2244 */
2245 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002246free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002247{
2248 unwind_locals(cctx, 0);
2249 ga_clear(&cctx->ctx_locals);
2250}
2251
2252/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002253 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2254 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2255 * error if the variable was defined with :const.
2256 */
2257 static int
2258check_item_writable(svar_T *sv, int check_writable, char_u *name)
2259{
2260 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2261 || (check_writable == ASSIGN_FINAL
2262 && sv->sv_const == ASSIGN_CONST))
2263 {
2264 semsg(_(e_readonlyvar), name);
2265 return FAIL;
2266 }
2267 return OK;
2268}
2269
2270/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002272 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002273 * Returns the index in "sn_var_vals" if found.
2274 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002275 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002276 */
2277 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002278get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002279{
2280 hashtab_T *ht;
2281 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002282 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002283 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284 int idx;
2285
Bram Moolenaare3d46852020-08-29 13:39:17 +02002286 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002287 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002288 if (sid == current_sctx.sc_sid)
2289 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002290 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002291
2292 if (sav == NULL)
2293 return -2;
2294 idx = sav->sav_var_vals_idx;
2295 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002296 if (check_item_writable(sv, check_writable, name) == FAIL)
2297 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002298 return idx;
2299 }
2300
2301 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002302 ht = &SCRIPT_VARS(sid);
2303 di = find_var_in_ht(ht, 0, name, TRUE);
2304 if (di == NULL)
2305 return -2;
2306
2307 // Now find the svar_T index in sn_var_vals.
2308 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2309 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002310 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311 if (sv->sv_tv == &di->di_tv)
2312 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002313 if (check_item_writable(sv, check_writable, name) == FAIL)
2314 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002315 return idx;
2316 }
2317 }
2318 return -1;
2319}
2320
2321/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002322 * Find "name" in imported items of the current script or in "cctx" if not
2323 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002324 */
2325 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002326find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002327{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002328 int idx;
2329
Bram Moolenaare3d46852020-08-29 13:39:17 +02002330 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002331 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002332 if (cctx != NULL)
2333 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2334 {
2335 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2336 + idx;
2337
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002338 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2339 : STRLEN(import->imp_name) == len
2340 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002341 return import;
2342 }
2343
Bram Moolenaarefa94442020-08-08 22:16:00 +02002344 return find_imported_in_script(name, len, current_sctx.sc_sid);
2345}
2346
2347 imported_T *
2348find_imported_in_script(char_u *name, size_t len, int sid)
2349{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002350 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002351 int idx;
2352
Bram Moolenaare3d46852020-08-29 13:39:17 +02002353 if (!SCRIPT_ID_VALID(sid))
2354 return NULL;
2355 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002356 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2357 {
2358 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2359
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002360 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2361 : STRLEN(import->imp_name) == len
2362 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002363 return import;
2364 }
2365 return NULL;
2366}
2367
2368/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002369 * Free all imported variables.
2370 */
2371 static void
2372free_imported(cctx_T *cctx)
2373{
2374 int idx;
2375
2376 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2377 {
2378 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2379
2380 vim_free(import->imp_name);
2381 }
2382 ga_clear(&cctx->ctx_imports);
2383}
2384
2385/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002386 * Return a pointer to the next line that isn't empty or only contains a
2387 * comment. Skips over white space.
2388 * Returns NULL if there is none.
2389 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002390 char_u *
2391peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002392{
2393 int lnum = cctx->ctx_lnum;
2394
2395 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2396 {
2397 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002398 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002399
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002400 // ignore NULLs inserted for continuation lines
2401 if (line != NULL)
2402 {
2403 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002404 if (vim9_bad_comment(p))
2405 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002406 if (*p != NUL && !vim9_comment_start(p))
2407 return p;
2408 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002409 }
2410 return NULL;
2411}
2412
2413/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002414 * Called when checking for a following operator at "arg". When the rest of
2415 * the line is empty or only a comment, peek the next line. If there is a next
2416 * line return a pointer to it and set "nextp".
2417 * Otherwise skip over white space.
2418 */
2419 static char_u *
2420may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2421{
2422 char_u *p = skipwhite(arg);
2423
2424 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002425 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002426 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002427 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002428 if (*nextp != NULL)
2429 return *nextp;
2430 }
2431 return p;
2432}
2433
2434/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002435 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002436 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002437 * Returns NULL when at the end.
2438 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002439 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002440next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002441{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002442 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002443
2444 do
2445 {
2446 ++cctx->ctx_lnum;
2447 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002448 {
2449 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002450 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002451 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002452 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002453 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002454 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002455 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002456 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002457 return line;
2458}
2459
2460/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002461 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002462 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002463 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002464 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2465 */
2466 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002467may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002468{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002469 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002470 if (vim9_bad_comment(*arg))
2471 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002472 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002473 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002474 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002475
2476 if (next == NULL)
2477 return FAIL;
2478 *arg = skipwhite(next);
2479 }
2480 return OK;
2481}
2482
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002483/*
2484 * Idem, and give an error when failed.
2485 */
2486 static int
2487may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2488{
2489 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2490 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002491 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002492 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002493 return FAIL;
2494 }
2495 return OK;
2496}
2497
2498
Bram Moolenaara5565e42020-05-09 15:44:01 +02002499// Structure passed between the compile_expr* functions to keep track of
2500// constants that have been parsed but for which no code was produced yet. If
2501// possible expressions on these constants are applied at compile time. If
2502// that is not possible, the code to push the constants needs to be generated
2503// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002504// Using 50 should be more than enough of 5 levels of ().
2505#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002506typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002507 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002508 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002509 int pp_is_const; // all generated code was constants, used for a
2510 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002511} ppconst_T;
2512
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002513static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002514static int compile_expr0(char_u **arg, cctx_T *cctx);
2515static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2516
Bram Moolenaara5565e42020-05-09 15:44:01 +02002517/*
2518 * Generate a PUSH instruction for "tv".
2519 * "tv" will be consumed or cleared.
2520 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2521 */
2522 static int
2523generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2524{
2525 if (tv != NULL)
2526 {
2527 switch (tv->v_type)
2528 {
2529 case VAR_UNKNOWN:
2530 break;
2531 case VAR_BOOL:
2532 generate_PUSHBOOL(cctx, tv->vval.v_number);
2533 break;
2534 case VAR_SPECIAL:
2535 generate_PUSHSPEC(cctx, tv->vval.v_number);
2536 break;
2537 case VAR_NUMBER:
2538 generate_PUSHNR(cctx, tv->vval.v_number);
2539 break;
2540#ifdef FEAT_FLOAT
2541 case VAR_FLOAT:
2542 generate_PUSHF(cctx, tv->vval.v_float);
2543 break;
2544#endif
2545 case VAR_BLOB:
2546 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2547 tv->vval.v_blob = NULL;
2548 break;
2549 case VAR_STRING:
2550 generate_PUSHS(cctx, tv->vval.v_string);
2551 tv->vval.v_string = NULL;
2552 break;
2553 default:
2554 iemsg("constant type not supported");
2555 clear_tv(tv);
2556 return FAIL;
2557 }
2558 tv->v_type = VAR_UNKNOWN;
2559 }
2560 return OK;
2561}
2562
2563/*
2564 * Generate code for any ppconst entries.
2565 */
2566 static int
2567generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2568{
2569 int i;
2570 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002571 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002572
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002573 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002574 for (i = 0; i < ppconst->pp_used; ++i)
2575 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2576 ret = FAIL;
2577 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002578 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002579 return ret;
2580}
2581
2582/*
2583 * Clear ppconst constants. Used when failing.
2584 */
2585 static void
2586clear_ppconst(ppconst_T *ppconst)
2587{
2588 int i;
2589
2590 for (i = 0; i < ppconst->pp_used; ++i)
2591 clear_tv(&ppconst->pp_tv[i]);
2592 ppconst->pp_used = 0;
2593}
2594
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002595/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002596 * Generate an instruction to load script-local variable "name", without the
2597 * leading "s:".
2598 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 */
2600 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002601compile_load_scriptvar(
2602 cctx_T *cctx,
2603 char_u *name, // variable NUL terminated
2604 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002605 char_u **end, // end of variable
2606 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002607{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002608 scriptitem_T *si;
2609 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002610 imported_T *import;
2611
Bram Moolenaare3d46852020-08-29 13:39:17 +02002612 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2613 return FAIL;
2614 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002615 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002616 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002617 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002618 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002619 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2620 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621 }
2622 if (idx >= 0)
2623 {
2624 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2625
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002626 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002627 current_sctx.sc_sid, idx, sv->sv_type);
2628 return OK;
2629 }
2630
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002631 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632 if (import != NULL)
2633 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002634 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002635 {
2636 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002637 char_u *exp_name;
2638 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002639 ufunc_T *ufunc;
2640 type_T *type;
2641
2642 // Used "import * as Name", need to lookup the member.
2643 if (*p != '.')
2644 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002645 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002646 return FAIL;
2647 }
2648 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002649 if (VIM_ISWHITE(*p))
2650 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002651 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002652 return FAIL;
2653 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002654
Bram Moolenaar1c991142020-07-04 13:15:31 +02002655 // isolate one name
2656 exp_name = p;
2657 while (eval_isnamec(*p))
2658 ++p;
2659 cc = *p;
2660 *p = NUL;
2661
Bram Moolenaaredba7072021-03-13 21:14:18 +01002662 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2663 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002664 *p = cc;
2665 p = skipwhite(p);
2666
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002667 // TODO: what if it is a function?
2668 if (idx < 0)
2669 return FAIL;
2670 *end = p;
2671
2672 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2673 import->imp_sid,
2674 idx,
2675 type);
2676 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002677 else if (import->imp_funcname != NULL)
2678 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002679 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002680 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2681 import->imp_sid,
2682 import->imp_var_vals_idx,
2683 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 return OK;
2685 }
2686
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002687 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002688 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002689 return FAIL;
2690}
2691
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002692 static int
2693generate_funcref(cctx_T *cctx, char_u *name)
2694{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002695 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002696
2697 if (ufunc == NULL)
2698 return FAIL;
2699
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002700 // Need to compile any default values to get the argument types.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01002701 if (func_needs_compiling(ufunc, PROFILING(ufunc))
2702 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002703 == FAIL)
2704 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002705 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002706}
2707
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002708/*
2709 * Compile a variable name into a load instruction.
2710 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002711 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002712 * When "error" is FALSE do not give an error when not found.
2713 */
2714 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002715compile_load(
2716 char_u **arg,
2717 char_u *end_arg,
2718 cctx_T *cctx,
2719 int is_expr,
2720 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002721{
2722 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002723 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002724 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002725 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002726 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727
2728 if (*(*arg + 1) == ':')
2729 {
2730 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002731 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002732 {
2733 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002734
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002735 switch (**arg)
2736 {
2737 case 'g': isn_type = ISN_LOADGDICT; break;
2738 case 'w': isn_type = ISN_LOADWDICT; break;
2739 case 't': isn_type = ISN_LOADTDICT; break;
2740 case 'b': isn_type = ISN_LOADBDICT; break;
2741 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002742 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002743 goto theend;
2744 }
2745 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2746 goto theend;
2747 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002748 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002749 else
2750 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002751 isntype_T isn_type = ISN_DROP;
2752
2753 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2754 if (name == NULL)
2755 return FAIL;
2756
2757 switch (**arg)
2758 {
2759 case 'v': res = generate_LOADV(cctx, name, error);
2760 break;
2761 case 's': res = compile_load_scriptvar(cctx, name,
2762 NULL, NULL, error);
2763 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002764 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2765 isn_type = ISN_LOADG;
2766 else
2767 {
2768 isn_type = ISN_LOADAUTO;
2769 vim_free(name);
2770 name = vim_strnsave(*arg, end - *arg);
2771 if (name == NULL)
2772 return FAIL;
2773 }
2774 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002775 case 'w': isn_type = ISN_LOADW; break;
2776 case 't': isn_type = ISN_LOADT; break;
2777 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002778 default: // cannot happen, just in case
2779 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002780 goto theend;
2781 }
2782 if (isn_type != ISN_DROP)
2783 {
2784 // Global, Buffer-local, Window-local and Tabpage-local
2785 // variables can be defined later, thus we don't check if it
2786 // exists, give error at runtime.
2787 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2788 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789 }
2790 }
2791 else
2792 {
2793 size_t len = end - *arg;
2794 int idx;
2795 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002796 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797
2798 name = vim_strnsave(*arg, end - *arg);
2799 if (name == NULL)
2800 return FAIL;
2801
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002802 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002804 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002805 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806 }
2807 else
2808 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002809 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002810
Bram Moolenaar709664c2020-12-12 14:33:41 +01002811 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002812 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002813 type = lvar.lv_type;
2814 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01002815 if (lvar.lv_from_outer != 0)
2816 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002817 else
2818 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 }
2820 else
2821 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002822 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002823 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002824 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002825 || find_imported(name, 0, cctx) != NULL)
2826 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002827
Bram Moolenaar0f769812020-09-12 18:32:34 +02002828 // When evaluating an expression and the name starts with an
2829 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002830 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002831 if (res == FAIL && is_expr
2832 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002833 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002834 }
2835 }
2836 if (gen_load)
2837 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01002838 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002839 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002840 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002841 cctx->ctx_outer_used = TRUE;
2842 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843 }
2844
2845 *arg = end;
2846
2847theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002848 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002849 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850 vim_free(name);
2851 return res;
2852}
2853
2854/*
2855 * Compile the argument expressions.
2856 * "arg" points to just after the "(" and is advanced to after the ")"
2857 */
2858 static int
2859compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2860{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002861 char_u *p = *arg;
2862 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002863 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002864
Bram Moolenaare6085c52020-04-12 20:19:16 +02002865 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002867 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2868 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002869 if (*p == ')')
2870 {
2871 *arg = p + 1;
2872 return OK;
2873 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002874 if (must_end)
2875 {
2876 semsg(_(e_missing_comma_before_argument_str), p);
2877 return FAIL;
2878 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002879
Bram Moolenaara5565e42020-05-09 15:44:01 +02002880 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881 return FAIL;
2882 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002883
2884 if (*p != ',' && *skipwhite(p) == ',')
2885 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01002886 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002887 p = skipwhite(p);
2888 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002890 {
2891 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002892 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01002893 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002894 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002895 else
2896 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002897 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002898 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002900failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002901 emsg(_(e_missing_close));
2902 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002903}
2904
2905/*
2906 * Compile a function call: name(arg1, arg2)
2907 * "arg" points to "name", "arg + varlen" to the "(".
2908 * "argcount_init" is 1 for "value->method()"
2909 * Instructions:
2910 * EVAL arg1
2911 * EVAL arg2
2912 * BCALL / DCALL / UCALL
2913 */
2914 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002915compile_call(
2916 char_u **arg,
2917 size_t varlen,
2918 cctx_T *cctx,
2919 ppconst_T *ppconst,
2920 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921{
2922 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002923 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 int argcount = argcount_init;
2925 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002926 char_u fname_buf[FLEN_FIXED + 1];
2927 char_u *tofree = NULL;
2928 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002929 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002930 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002931 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932
Bram Moolenaara5565e42020-05-09 15:44:01 +02002933 // we can evaluate "has('name')" at compile time
2934 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2935 {
2936 char_u *s = skipwhite(*arg + varlen + 1);
2937 typval_T argvars[2];
2938
2939 argvars[0].v_type = VAR_UNKNOWN;
2940 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002941 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002942 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002943 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002944 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002945 if (*s == ')' && argvars[0].v_type == VAR_STRING
2946 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002947 {
2948 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2949
2950 *arg = s + 1;
2951 argvars[1].v_type = VAR_UNKNOWN;
2952 tv->v_type = VAR_NUMBER;
2953 tv->vval.v_number = 0;
2954 f_has(argvars, tv);
2955 clear_tv(&argvars[0]);
2956 ++ppconst->pp_used;
2957 return OK;
2958 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002959 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002960 }
2961
2962 if (generate_ppconst(cctx, ppconst) == FAIL)
2963 return FAIL;
2964
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 if (varlen >= sizeof(namebuf))
2966 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002967 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 return FAIL;
2969 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002970 vim_strncpy(namebuf, *arg, varlen);
2971 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972
2973 *arg = skipwhite(*arg + varlen + 1);
2974 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002975 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976
Bram Moolenaar03290b82020-12-19 16:30:44 +01002977 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002978 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 {
2980 int idx;
2981
2982 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002983 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002985 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01002986 if (STRCMP(name, "flatten") == 0)
2987 {
2988 emsg(_(e_cannot_use_flatten_in_vim9_script));
2989 goto theend;
2990 }
2991
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002992 if (STRCMP(name, "add") == 0 && argcount == 2)
2993 {
2994 garray_T *stack = &cctx->ctx_type_stack;
2995 type_T *type = ((type_T **)stack->ga_data)[
2996 stack->ga_len - 2];
2997
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002998 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002999 if (type->tt_type == VAR_LIST)
3000 {
3001 // inline "add(list, item)" so that the type can be checked
3002 res = generate_LISTAPPEND(cctx);
3003 idx = -1;
3004 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003005 else if (type->tt_type == VAR_BLOB)
3006 {
3007 // inline "add(blob, nr)" so that the type can be checked
3008 res = generate_BLOBAPPEND(cctx);
3009 idx = -1;
3010 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003011 }
3012
3013 if (idx >= 0)
3014 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3015 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003016 else
3017 semsg(_(e_unknownfunc), namebuf);
3018 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 }
3020
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003021 // An argument or local variable can be a function reference, this
3022 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003023 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003024 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003025 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003026 // If we can find the function by name generate the right call.
3027 // Skip global functions here, a local funcref takes precedence.
3028 ufunc = find_func(name, FALSE, cctx);
3029 if (ufunc != NULL && !func_is_global(ufunc))
3030 {
3031 res = generate_CALL(cctx, ufunc, argcount);
3032 goto theend;
3033 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003034 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035
3036 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003037 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003038 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003040 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003041 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003042 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003043 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003044 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003045
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003046 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003047 goto theend;
3048 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049
Bram Moolenaar0f769812020-09-12 18:32:34 +02003050 // If we can find a global function by name generate the right call.
3051 if (ufunc != NULL)
3052 {
3053 res = generate_CALL(cctx, ufunc, argcount);
3054 goto theend;
3055 }
3056
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003057 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003058 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003059 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003060 res = generate_UCALL(cctx, name, argcount);
3061 else
3062 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003063
3064theend:
3065 vim_free(tofree);
3066 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067}
3068
3069// like NAMESPACE_CHAR but with 'a' and 'l'.
3070#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3071
3072/*
3073 * Find the end of a variable or function name. Unlike find_name_end() this
3074 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003075 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 * Return a pointer to just after the name. Equal to "arg" if there is no
3077 * valid name.
3078 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003079 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003080to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081{
3082 char_u *p;
3083
3084 // Quick check for valid starting character.
3085 if (!eval_isnamec1(*arg))
3086 return arg;
3087
3088 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3089 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3090 // and can be used in slice "[n:]".
3091 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003092 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3094 break;
3095 return p;
3096}
3097
3098/*
3099 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003100 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 */
3102 char_u *
3103to_name_const_end(char_u *arg)
3104{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003105 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106 typval_T rettv;
3107
3108 if (p == arg && *arg == '[')
3109 {
3110
3111 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003112 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113 p = arg;
3114 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 return p;
3116}
3117
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118/*
3119 * parse a list: [expr, expr]
3120 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003121 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003122 */
3123 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003124compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125{
3126 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003127 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003129 int is_const;
3130 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003132 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003134 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003135 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003136 semsg(_(e_list_end), *arg);
3137 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003138 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003139 if (*p == ',')
3140 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003141 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003142 return FAIL;
3143 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003144 if (*p == ']')
3145 {
3146 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003147 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003148 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003149 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003150 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003151 if (!is_const)
3152 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003153 ++count;
3154 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003155 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003157 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3158 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003159 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003160 return FAIL;
3161 }
3162 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003163 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 p = skipwhite(p);
3165 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003166 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003168 ppconst->pp_is_const = is_all_const;
3169 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170}
3171
3172/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003173 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003174 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003175 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 */
3177 static int
3178compile_lambda(char_u **arg, cctx_T *cctx)
3179{
Bram Moolenaare462f522020-12-27 14:43:30 +01003180 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181 typval_T rettv;
3182 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003183 evalarg_T evalarg;
3184
3185 CLEAR_FIELD(evalarg);
3186 evalarg.eval_flags = EVAL_EVALUATE;
3187 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188
3189 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003190 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3191 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003192 {
3193 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003194 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003195 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003196
Bram Moolenaar65c44152020-12-24 15:14:01 +01003197 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003199 ++ufunc->uf_refcount;
3200 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201
Bram Moolenaar65c44152020-12-24 15:14:01 +01003202 // Compile the function into instructions.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01003203 compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003205 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3206 // points into it. Point to the original line to avoid a dangling pointer.
3207 if (evalarg.eval_tofree_cmdline != NULL)
3208 {
3209 size_t off = *arg - evalarg.eval_tofree_cmdline;
3210
3211 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3212 + off;
3213 }
3214
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003215 clear_evalarg(&evalarg, NULL);
3216
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003217 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003218 {
3219 // The return type will now be known.
3220 set_function_type(ufunc);
3221
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003222 // The function reference count will be 1. When the ISN_FUNCREF
3223 // instruction is deleted the reference count is decremented and the
3224 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003225 return generate_FUNCREF(cctx, ufunc);
3226 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003227
3228 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229 return FAIL;
3230}
3231
3232/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003233 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003235 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 */
3237 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003238compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239{
3240 garray_T *instr = &cctx->ctx_instr;
3241 int count = 0;
3242 dict_T *d = dict_alloc();
3243 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003244 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003245 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003246 int is_const;
3247 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248
3249 if (d == NULL)
3250 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003251 if (generate_ppconst(cctx, ppconst) == FAIL)
3252 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003253 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003255 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256
Bram Moolenaar23c55272020-06-21 16:58:13 +02003257 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003258 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003259 *arg = NULL;
3260 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003261 }
3262
3263 if (**arg == '}')
3264 break;
3265
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003266 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003268 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003270 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003271 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003272 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003275 if (isn->isn_type == ISN_PUSHNR)
3276 {
3277 char buf[NUMBUFLEN];
3278
3279 // Convert to string at compile time.
3280 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3281 isn->isn_type = ISN_PUSHS;
3282 isn->isn_arg.string = vim_strsave((char_u *)buf);
3283 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 if (isn->isn_type == ISN_PUSHS)
3285 key = isn->isn_arg.string;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003286 else if (may_generate_2STRING(-1, cctx) == FAIL)
3287 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003288 *arg = skipwhite(*arg);
3289 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003290 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003291 emsg(_(e_missing_matching_bracket_after_dict_key));
3292 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003293 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003294 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003296 else
3297 {
3298 // {"name": value},
3299 // {'name': value},
3300 // {name: value} use "name" as a literal key
3301 key = get_literal_key(arg);
3302 if (key == NULL)
3303 return FAIL;
3304 if (generate_PUSHS(cctx, key) == FAIL)
3305 return FAIL;
3306 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307
3308 // Check for duplicate keys, if using string keys.
3309 if (key != NULL)
3310 {
3311 item = dict_find(d, key, -1);
3312 if (item != NULL)
3313 {
3314 semsg(_(e_duplicate_key), key);
3315 goto failret;
3316 }
3317 item = dictitem_alloc(key);
3318 if (item != NULL)
3319 {
3320 item->di_tv.v_type = VAR_UNKNOWN;
3321 item->di_tv.v_lock = 0;
3322 if (dict_add(d, item) == FAIL)
3323 dictitem_free(item);
3324 }
3325 }
3326
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 if (**arg != ':')
3328 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003329 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003330 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003331 else
3332 semsg(_(e_missing_dict_colon), *arg);
3333 return FAIL;
3334 }
3335 whitep = *arg + 1;
3336 if (!IS_WHITE_OR_NUL(*whitep))
3337 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003338 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339 return FAIL;
3340 }
3341
Bram Moolenaar23c55272020-06-21 16:58:13 +02003342 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003343 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003344 *arg = NULL;
3345 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003346 }
3347
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003348 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003350 if (!is_const)
3351 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003352 ++count;
3353
Bram Moolenaar2c330432020-04-13 14:41:35 +02003354 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003355 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003356 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003357 *arg = NULL;
3358 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003359 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 if (**arg == '}')
3361 break;
3362 if (**arg != ',')
3363 {
3364 semsg(_(e_missing_dict_comma), *arg);
3365 goto failret;
3366 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003367 if (IS_WHITE_OR_NUL(*whitep))
3368 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003369 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003370 return FAIL;
3371 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003372 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003373 if (!IS_WHITE_OR_NUL(*whitep))
3374 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003375 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003376 return FAIL;
3377 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003378 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379 }
3380
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003381 *arg = *arg + 1;
3382
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003383 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003384 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003385 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003386 *arg += STRLEN(*arg);
3387
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003389 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390 return generate_NEWDICT(cctx, count);
3391
3392failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003393 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003394 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003395 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003396 *arg = (char_u *)"";
3397 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 dict_unref(d);
3399 return FAIL;
3400}
3401
3402/*
3403 * Compile "&option".
3404 */
3405 static int
3406compile_get_option(char_u **arg, cctx_T *cctx)
3407{
3408 typval_T rettv;
3409 char_u *start = *arg;
3410 int ret;
3411
3412 // parse the option and get the current value to get the type.
3413 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003414 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415 if (ret == OK)
3416 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003417 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003418 char_u *name = vim_strnsave(start, *arg - start);
3419 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3420 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421
3422 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3423 vim_free(name);
3424 }
3425 clear_tv(&rettv);
3426
3427 return ret;
3428}
3429
3430/*
3431 * Compile "$VAR".
3432 */
3433 static int
3434compile_get_env(char_u **arg, cctx_T *cctx)
3435{
3436 char_u *start = *arg;
3437 int len;
3438 int ret;
3439 char_u *name;
3440
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003441 ++*arg;
3442 len = get_env_len(arg);
3443 if (len == 0)
3444 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003445 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 return FAIL;
3447 }
3448
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003449 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450 name = vim_strnsave(start, len + 1);
3451 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3452 vim_free(name);
3453 return ret;
3454}
3455
3456/*
3457 * Compile "@r".
3458 */
3459 static int
3460compile_get_register(char_u **arg, cctx_T *cctx)
3461{
3462 int ret;
3463
3464 ++*arg;
3465 if (**arg == NUL)
3466 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003467 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003468 return FAIL;
3469 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003470 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003471 {
3472 emsg_invreg(**arg);
3473 return FAIL;
3474 }
3475 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3476 ++*arg;
3477 return ret;
3478}
3479
3480/*
3481 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003482 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003483 */
3484 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003485apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003486{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003487 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003488
3489 // this works from end to start
3490 while (p > start)
3491 {
3492 --p;
3493 if (*p == '-' || *p == '+')
3494 {
3495 // only '-' has an effect, for '+' we only check the type
3496#ifdef FEAT_FLOAT
3497 if (rettv->v_type == VAR_FLOAT)
3498 {
3499 if (*p == '-')
3500 rettv->vval.v_float = -rettv->vval.v_float;
3501 }
3502 else
3503#endif
3504 {
3505 varnumber_T val;
3506 int error = FALSE;
3507
3508 // tv_get_number_chk() accepts a string, but we don't want that
3509 // here
3510 if (check_not_string(rettv) == FAIL)
3511 return FAIL;
3512 val = tv_get_number_chk(rettv, &error);
3513 clear_tv(rettv);
3514 if (error)
3515 return FAIL;
3516 if (*p == '-')
3517 val = -val;
3518 rettv->v_type = VAR_NUMBER;
3519 rettv->vval.v_number = val;
3520 }
3521 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003522 else if (numeric_only)
3523 {
3524 ++p;
3525 break;
3526 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003527 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003528 {
3529 int v = tv2bool(rettv);
3530
3531 // '!' is permissive in the type.
3532 clear_tv(rettv);
3533 rettv->v_type = VAR_BOOL;
3534 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3535 }
3536 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003537 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538 return OK;
3539}
3540
3541/*
3542 * Recognize v: variables that are constants and set "rettv".
3543 */
3544 static void
3545get_vim_constant(char_u **arg, typval_T *rettv)
3546{
3547 if (STRNCMP(*arg, "v:true", 6) == 0)
3548 {
3549 rettv->v_type = VAR_BOOL;
3550 rettv->vval.v_number = VVAL_TRUE;
3551 *arg += 6;
3552 }
3553 else if (STRNCMP(*arg, "v:false", 7) == 0)
3554 {
3555 rettv->v_type = VAR_BOOL;
3556 rettv->vval.v_number = VVAL_FALSE;
3557 *arg += 7;
3558 }
3559 else if (STRNCMP(*arg, "v:null", 6) == 0)
3560 {
3561 rettv->v_type = VAR_SPECIAL;
3562 rettv->vval.v_number = VVAL_NULL;
3563 *arg += 6;
3564 }
3565 else if (STRNCMP(*arg, "v:none", 6) == 0)
3566 {
3567 rettv->v_type = VAR_SPECIAL;
3568 rettv->vval.v_number = VVAL_NONE;
3569 *arg += 6;
3570 }
3571}
3572
Bram Moolenaar657137c2021-01-09 15:45:23 +01003573 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003574get_compare_type(char_u *p, int *len, int *type_is)
3575{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003576 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003577 int i;
3578
3579 switch (p[0])
3580 {
3581 case '=': if (p[1] == '=')
3582 type = EXPR_EQUAL;
3583 else if (p[1] == '~')
3584 type = EXPR_MATCH;
3585 break;
3586 case '!': if (p[1] == '=')
3587 type = EXPR_NEQUAL;
3588 else if (p[1] == '~')
3589 type = EXPR_NOMATCH;
3590 break;
3591 case '>': if (p[1] != '=')
3592 {
3593 type = EXPR_GREATER;
3594 *len = 1;
3595 }
3596 else
3597 type = EXPR_GEQUAL;
3598 break;
3599 case '<': if (p[1] != '=')
3600 {
3601 type = EXPR_SMALLER;
3602 *len = 1;
3603 }
3604 else
3605 type = EXPR_SEQUAL;
3606 break;
3607 case 'i': if (p[1] == 's')
3608 {
3609 // "is" and "isnot"; but not a prefix of a name
3610 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3611 *len = 5;
3612 i = p[*len];
3613 if (!isalnum(i) && i != '_')
3614 {
3615 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3616 *type_is = TRUE;
3617 }
3618 }
3619 break;
3620 }
3621 return type;
3622}
3623
3624/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003625 * Skip over an expression, ignoring most errors.
3626 */
3627 static void
3628skip_expr_cctx(char_u **arg, cctx_T *cctx)
3629{
3630 evalarg_T evalarg;
3631
3632 CLEAR_FIELD(evalarg);
3633 evalarg.eval_cctx = cctx;
3634 skip_expr(arg, &evalarg);
3635}
3636
3637/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003639 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 */
3641 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003642compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003644 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645
3646 // this works from end to start
3647 while (p > start)
3648 {
3649 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003650 while (VIM_ISWHITE(*p))
3651 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 if (*p == '-' || *p == '+')
3653 {
3654 int negate = *p == '-';
3655 isn_T *isn;
3656
3657 // TODO: check type
3658 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3659 {
3660 --p;
3661 if (*p == '-')
3662 negate = !negate;
3663 }
3664 // only '-' has an effect, for '+' we only check the type
3665 if (negate)
3666 isn = generate_instr(cctx, ISN_NEGATENR);
3667 else
3668 isn = generate_instr(cctx, ISN_CHECKNR);
3669 if (isn == NULL)
3670 return FAIL;
3671 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003672 else if (numeric_only)
3673 {
3674 ++p;
3675 break;
3676 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677 else
3678 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003679 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003681 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003683 if (p[-1] == '!')
3684 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686 }
3687 if (generate_2BOOL(cctx, invert) == FAIL)
3688 return FAIL;
3689 }
3690 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003691 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692 return OK;
3693}
3694
3695/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003696 * Compile "(expression)": recursive!
3697 * Return FAIL/OK.
3698 */
3699 static int
3700compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3701{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003702 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003703 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003704
Bram Moolenaar24156692021-01-14 20:35:49 +01003705 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3706 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003707 if (ppconst->pp_used <= PPSIZE - 10)
3708 {
3709 ret = compile_expr1(arg, cctx, ppconst);
3710 }
3711 else
3712 {
3713 // Not enough space in ppconst, flush constants.
3714 if (generate_ppconst(cctx, ppconst) == FAIL)
3715 return FAIL;
3716 ret = compile_expr0(arg, cctx);
3717 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003718 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3719 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003720 if (**arg == ')')
3721 ++*arg;
3722 else if (ret == OK)
3723 {
3724 emsg(_(e_missing_close));
3725 ret = FAIL;
3726 }
3727 return ret;
3728}
3729
3730/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003732 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 */
3734 static int
3735compile_subscript(
3736 char_u **arg,
3737 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003738 char_u *start_leader,
3739 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003740 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003742 char_u *name_start = *end_leader;
3743
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744 for (;;)
3745 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003746 char_u *p = skipwhite(*arg);
3747
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003748 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003749 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003750 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003751
3752 // If a following line starts with "->{" or "->X" advance to that
3753 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003754 // Also if a following line starts with ".x".
3755 if (next != NULL &&
3756 ((next[0] == '-' && next[1] == '>'
3757 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003758 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003759 {
3760 next = next_line_from_context(cctx, TRUE);
3761 if (next == NULL)
3762 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003763 *arg = next;
3764 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003765 }
3766 }
3767
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003768 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003769 // not a function call.
3770 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003772 garray_T *stack = &cctx->ctx_type_stack;
3773 type_T *type;
3774 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003775
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003776 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003777 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003778 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003779
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003781 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3782
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003783 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3785 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003786 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787 return FAIL;
3788 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003789 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003791 char_u *pstart = p;
3792
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003793 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003794 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003795 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003796
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797 // something->method()
3798 // Apply the '!', '-' and '+' first:
3799 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003800 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003803 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003804 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003805 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003806 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003807 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003808 int argcount = 1;
3809 char_u *expr;
3810 garray_T *stack;
3811 type_T *type;
3812
3813 // Funcref call: list->(Refs[2])(arg)
3814 // or lambda: list->((arg) => expr)(arg)
3815 // Fist compile the arguments.
3816 expr = *arg;
3817 *arg = skipwhite(*arg + 1);
3818 skip_expr_cctx(arg, cctx);
3819 *arg = skipwhite(*arg);
3820 if (**arg != ')')
3821 {
3822 semsg(_(e_missing_paren), *arg);
3823 return FAIL;
3824 }
3825 ++*arg;
3826 if (**arg != '(')
3827 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003828 if (*skipwhite(*arg) == '(')
3829 emsg(_(e_nowhitespace));
3830 else
3831 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01003832 return FAIL;
3833 }
3834
3835 *arg = skipwhite(*arg + 1);
3836 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3837 return FAIL;
3838
3839 // Compile the function expression.
3840 if (compile_parenthesis(&expr, cctx, ppconst) == FAIL)
3841 return FAIL;
3842
3843 stack = &cctx->ctx_type_stack;
3844 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3845 if (generate_PCALL(cctx, argcount,
3846 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 return FAIL;
3848 }
3849 else
3850 {
3851 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003852 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003853 if (!eval_isnamec1(*p))
3854 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003855 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003856 return FAIL;
3857 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003858 if (ASCII_ISALPHA(*p) && p[1] == ':')
3859 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003860 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861 ;
3862 if (*p != '(')
3863 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003864 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 return FAIL;
3866 }
3867 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003868 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 return FAIL;
3870 }
3871 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003872 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003874 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003875 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003876 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003877 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003878 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003879
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003880 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003881 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003882 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003883 // TODO: blob index
3884 // TODO: more arguments
3885 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003886 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003887 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003888 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003889
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003890 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003891 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003892 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003893 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003894 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003895 // missing first index is equal to zero
3896 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003897 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003898 else
3899 {
3900 if (compile_expr0(arg, cctx) == FAIL)
3901 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003902 if (**arg == ':')
3903 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003904 semsg(_(e_white_space_required_before_and_after_str_at_str),
3905 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003906 return FAIL;
3907 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003908 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003909 return FAIL;
3910 *arg = skipwhite(*arg);
3911 }
3912 if (**arg == ':')
3913 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003914 is_slice = TRUE;
3915 ++*arg;
3916 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
3917 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003918 semsg(_(e_white_space_required_before_and_after_str_at_str),
3919 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003920 return FAIL;
3921 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003922 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003923 return FAIL;
3924 if (**arg == ']')
3925 // missing second index is equal to end of string
3926 generate_PUSHNR(cctx, -1);
3927 else
3928 {
3929 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003930 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003931 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003932 return FAIL;
3933 *arg = skipwhite(*arg);
3934 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003935 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003936
3937 if (**arg != ']')
3938 {
3939 emsg(_(e_missbrac));
3940 return FAIL;
3941 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003942 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003944 // We can index a list and a dict. If we don't know the type
3945 // we can use the index value type.
3946 // TODO: If we don't know use an instruction to figure it out at
3947 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003948 typep = ((type_T **)stack->ga_data) + stack->ga_len
3949 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003950 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003951 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3952 // If the index is a string, the variable must be a Dict.
3953 if (*typep == &t_any && valtype == &t_string)
3954 vtype = VAR_DICT;
3955 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003956 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003957 if (need_type(valtype, &t_number, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003958 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003959 return FAIL;
3960 if (is_slice)
3961 {
3962 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar351ead02021-01-16 16:07:01 +01003963 if (need_type(valtype, &t_number, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003964 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003965 return FAIL;
3966 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003967 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003968
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003969 if (vtype == VAR_DICT)
3970 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003971 if (is_slice)
3972 {
3973 emsg(_(e_cannot_slice_dictionary));
3974 return FAIL;
3975 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003976 if ((*typep)->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003977 {
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003978 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003979 if (*typep == &t_unknown)
3980 // empty dict was used
3981 *typep = &t_any;
3982 }
Bram Moolenaar7892b952020-07-20 22:09:34 +02003983 else
3984 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003985 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003986 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003987 return FAIL;
3988 *typep = &t_any;
3989 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003990 if (may_generate_2STRING(-1, cctx) == FAIL)
3991 return FAIL;
3992 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3993 return FAIL;
3994 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003995 else if (vtype == VAR_STRING)
3996 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003997 *typep = &t_string;
3998 if ((is_slice
3999 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
4000 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004001 return FAIL;
4002 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004003 else if (vtype == VAR_BLOB)
4004 {
4005 emsg("Sorry, blob index and slice not implemented yet");
4006 return FAIL;
4007 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004008 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01004009 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004010 if (is_slice)
4011 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004012 if (generate_instr_drop(cctx,
4013 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
4014 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02004015 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004016 }
Bram Moolenaared591872020-08-15 22:14:53 +02004017 else
4018 {
4019 if ((*typep)->tt_type == VAR_LIST)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01004020 {
Bram Moolenaared591872020-08-15 22:14:53 +02004021 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01004022 if (*typep == &t_unknown)
4023 // empty list was used
4024 *typep = &t_any;
4025 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004026 if (generate_instr_drop(cctx,
4027 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
4028 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02004029 return FAIL;
4030 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004031 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004032 else
4033 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02004034 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01004035 return FAIL;
4036 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004037 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004038 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004040 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004041 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004042 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004043 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004044
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004045 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004046 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004047 {
4048 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004049 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004050 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004051 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004052 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053 while (eval_isnamec(*p))
4054 MB_PTR_ADV(p);
4055 if (p == *arg)
4056 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004057 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058 return FAIL;
4059 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004060 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061 return FAIL;
4062 *arg = p;
4063 }
4064 else
4065 break;
4066 }
4067
4068 // TODO - see handle_subscript():
4069 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4070 // Don't do this when "Func" is already a partial that was bound
4071 // explicitly (pt_auto is FALSE).
4072
4073 return OK;
4074}
4075
4076/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004077 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4078 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004079 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004080 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004081 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004082 *
4083 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 */
4085
4086/*
4087 * number number constant
4088 * 0zFFFFFFFF Blob constant
4089 * "string" string constant
4090 * 'string' literal string constant
4091 * &option-name option value
4092 * @r register contents
4093 * identifier variable value
4094 * function() function call
4095 * $VAR environment variable
4096 * (expression) nested expression
4097 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004098 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099 *
4100 * Also handle:
4101 * ! in front logical NOT
4102 * - in front unary minus
4103 * + in front unary plus (ignored)
4104 * trailing (arg) funcref/partial call
4105 * trailing [] subscript in String or List
4106 * trailing .name entry in Dictionary
4107 * trailing ->name() method call
4108 */
4109 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004110compile_expr7(
4111 char_u **arg,
4112 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004113 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004114{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115 char_u *start_leader, *end_leader;
4116 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004117 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004118 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004120 ppconst->pp_is_const = FALSE;
4121
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122 /*
4123 * Skip '!', '-' and '+' characters. They are handled later.
4124 */
4125 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004126 if (eval_leader(arg, TRUE) == FAIL)
4127 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004128 end_leader = *arg;
4129
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004130 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004131 switch (**arg)
4132 {
4133 /*
4134 * Number constant.
4135 */
4136 case '0': // also for blob starting with 0z
4137 case '1':
4138 case '2':
4139 case '3':
4140 case '4':
4141 case '5':
4142 case '6':
4143 case '7':
4144 case '8':
4145 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004146 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004148 // Apply "-" and "+" just before the number now, right to
4149 // left. Matters especially when "->" follows. Stops at
4150 // '!'.
4151 if (apply_leader(rettv, TRUE,
4152 start_leader, &end_leader) == FAIL)
4153 {
4154 clear_tv(rettv);
4155 return FAIL;
4156 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 break;
4158
4159 /*
4160 * String constant: "string".
4161 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004162 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 return FAIL;
4164 break;
4165
4166 /*
4167 * Literal string constant: 'str''ing'.
4168 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004169 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170 return FAIL;
4171 break;
4172
4173 /*
4174 * Constant Vim variable.
4175 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004176 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177 ret = NOTDONE;
4178 break;
4179
4180 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004181 * "true" constant
4182 */
4183 case 't': if (STRNCMP(*arg, "true", 4) == 0
4184 && !eval_isnamec((*arg)[4]))
4185 {
4186 *arg += 4;
4187 rettv->v_type = VAR_BOOL;
4188 rettv->vval.v_number = VVAL_TRUE;
4189 }
4190 else
4191 ret = NOTDONE;
4192 break;
4193
4194 /*
4195 * "false" constant
4196 */
4197 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4198 && !eval_isnamec((*arg)[5]))
4199 {
4200 *arg += 5;
4201 rettv->v_type = VAR_BOOL;
4202 rettv->vval.v_number = VVAL_FALSE;
4203 }
4204 else
4205 ret = NOTDONE;
4206 break;
4207
4208 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004209 * "null" constant
4210 */
4211 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004212 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004213 {
4214 *arg += 4;
4215 rettv->v_type = VAR_SPECIAL;
4216 rettv->vval.v_number = VVAL_NULL;
4217 }
4218 else
4219 ret = NOTDONE;
4220 break;
4221
4222 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004223 * List: [expr, expr]
4224 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004225 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4226 return FAIL;
4227 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 break;
4229
4230 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231 * Dictionary: {'key': val, 'key': val}
4232 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004233 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4234 return FAIL;
4235 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236 break;
4237
4238 /*
4239 * Option value: &name
4240 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004241 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4242 return FAIL;
4243 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244 break;
4245
4246 /*
4247 * Environment variable: $VAR.
4248 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004249 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4250 return FAIL;
4251 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004252 break;
4253
4254 /*
4255 * Register contents: @r.
4256 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004257 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4258 return FAIL;
4259 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260 break;
4261 /*
4262 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004263 * lambda: (arg, arg) => expr
4264 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004266 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4267 ret = compile_lambda(arg, cctx);
4268 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004269 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004270 break;
4271
4272 default: ret = NOTDONE;
4273 break;
4274 }
4275 if (ret == FAIL)
4276 return FAIL;
4277
Bram Moolenaar1c747212020-05-09 18:28:34 +02004278 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004280 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004281 clear_tv(rettv);
4282 else
4283 // A constant expression can possibly be handled compile time,
4284 // return the value instead of generating code.
4285 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004286 }
4287 else if (ret == NOTDONE)
4288 {
4289 char_u *p;
4290 int r;
4291
4292 if (!eval_isnamec1(**arg))
4293 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004294 if (!vim9_bad_comment(*arg))
4295 {
4296 if (ends_excmd(*skipwhite(*arg)))
4297 semsg(_(e_empty_expression_str), *arg);
4298 else
4299 semsg(_(e_name_expected_str), *arg);
4300 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004301 return FAIL;
4302 }
4303
4304 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004305 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004306 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004307 {
4308 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4309 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004310 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004311 {
4312 if (generate_ppconst(cctx, ppconst) == FAIL)
4313 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004314 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004315 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 if (r == FAIL)
4317 return FAIL;
4318 }
4319
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004320 // Handle following "[]", ".member", etc.
4321 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004322 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004323 ppconst) == FAIL)
4324 return FAIL;
4325 if (ppconst->pp_used > 0)
4326 {
4327 // apply the '!', '-' and '+' before the constant
4328 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004329 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004330 return FAIL;
4331 return OK;
4332 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004333 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004334 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004335 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336}
4337
4338/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004339 * Give the "white on both sides" error, taking the operator from "p[len]".
4340 */
4341 void
4342error_white_both(char_u *op, int len)
4343{
4344 char_u buf[10];
4345
4346 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004347 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004348}
4349
4350/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004351 * <type>expr7: runtime type check / conversion
4352 */
4353 static int
4354compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4355{
4356 type_T *want_type = NULL;
4357
4358 // Recognize <type>
4359 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4360 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004361 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004362 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4363 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004364 return FAIL;
4365
4366 if (**arg != '>')
4367 {
4368 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004369 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004370 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004371 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004372 return FAIL;
4373 }
4374 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004375 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004376 return FAIL;
4377 }
4378
4379 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4380 return FAIL;
4381
4382 if (want_type != NULL)
4383 {
4384 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004385 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004386 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004387
Bram Moolenaard1103582020-08-14 22:44:25 +02004388 generate_ppconst(cctx, ppconst);
4389 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004390 where.wt_index = 0;
4391 where.wt_variable = FALSE;
4392 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004393 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004394 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004395 return FAIL;
4396 }
4397 }
4398
4399 return OK;
4400}
4401
4402/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403 * * number multiplication
4404 * / number division
4405 * % number modulo
4406 */
4407 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004408compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409{
4410 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004411 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004412 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004414 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004415 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416 return FAIL;
4417
4418 /*
4419 * Repeat computing, until no "*", "/" or "%" is following.
4420 */
4421 for (;;)
4422 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004423 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424 if (*op != '*' && *op != '/' && *op != '%')
4425 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004426 if (next != NULL)
4427 {
4428 *arg = next_line_from_context(cctx, TRUE);
4429 op = skipwhite(*arg);
4430 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004431
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004432 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004434 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004435 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004436 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004437 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004438 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004440 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004441 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004443
4444 if (ppconst->pp_used == ppconst_used + 2
4445 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4446 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004447 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004448 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4449 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4450 varnumber_T res = 0;
4451 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004452
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004453 // both are numbers: compute the result
4454 switch (*op)
4455 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004456 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004457 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004458 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004459 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004460 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004461 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004462 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004463 break;
4464 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004465 if (failed)
4466 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004467 tv1->vval.v_number = res;
4468 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004469 }
4470 else
4471 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004472 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004473 generate_two_op(cctx, op);
4474 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004475 }
4476
4477 return OK;
4478}
4479
4480/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004481 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004482 * - number subtraction
4483 * .. string concatenation
4484 */
4485 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004486compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004487{
4488 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004489 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004491 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004492
4493 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004494 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004495 return FAIL;
4496
4497 /*
4498 * Repeat computing, until no "+", "-" or ".." is following.
4499 */
4500 for (;;)
4501 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004502 op = may_peek_next_line(cctx, *arg, &next);
4503 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004504 break;
4505 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004506 if (next != NULL)
4507 {
4508 *arg = next_line_from_context(cctx, TRUE);
4509 op = skipwhite(*arg);
4510 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004511
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004512 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004513 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004514 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004515 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516 }
4517
Bram Moolenaare0de1712020-12-02 17:36:54 +01004518 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004519 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004520
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004521 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004522 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523 return FAIL;
4524
Bram Moolenaara5565e42020-05-09 15:44:01 +02004525 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004526 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004527 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4528 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4529 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4530 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004531 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004532 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4533 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004534
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004535 // concat/subtract/add constant numbers
4536 if (*op == '+')
4537 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4538 else if (*op == '-')
4539 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4540 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004541 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004542 // concatenate constant strings
4543 char_u *s1 = tv1->vval.v_string;
4544 char_u *s2 = tv2->vval.v_string;
4545 size_t len1 = STRLEN(s1);
4546
4547 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4548 if (tv1->vval.v_string == NULL)
4549 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004550 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004551 return FAIL;
4552 }
4553 mch_memmove(tv1->vval.v_string, s1, len1);
4554 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004555 vim_free(s1);
4556 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004557 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004558 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004559 }
4560 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004561 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004562 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004563 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004564 if (*op == '.')
4565 {
4566 if (may_generate_2STRING(-2, cctx) == FAIL
4567 || may_generate_2STRING(-1, cctx) == FAIL)
4568 return FAIL;
4569 generate_instr_drop(cctx, ISN_CONCAT, 1);
4570 }
4571 else
4572 generate_two_op(cctx, op);
4573 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004574 }
4575
4576 return OK;
4577}
4578
4579/*
4580 * expr5a == expr5b
4581 * expr5a =~ expr5b
4582 * expr5a != expr5b
4583 * expr5a !~ expr5b
4584 * expr5a > expr5b
4585 * expr5a >= expr5b
4586 * expr5a < expr5b
4587 * expr5a <= expr5b
4588 * expr5a is expr5b
4589 * expr5a isnot expr5b
4590 *
4591 * Produces instructions:
4592 * EVAL expr5a Push result of "expr5a"
4593 * EVAL expr5b Push result of "expr5b"
4594 * COMPARE one of the compare instructions
4595 */
4596 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004597compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004599 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004601 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004603 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004604 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004605
4606 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004607 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 return FAIL;
4609
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004610 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004611 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004612
4613 /*
4614 * If there is a comparative operator, use it.
4615 */
4616 if (type != EXPR_UNKNOWN)
4617 {
4618 int ic = FALSE; // Default: do not ignore case
4619
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004620 if (next != NULL)
4621 {
4622 *arg = next_line_from_context(cctx, TRUE);
4623 p = skipwhite(*arg);
4624 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004625 if (type_is && (p[len] == '?' || p[len] == '#'))
4626 {
4627 semsg(_(e_invexpr2), *arg);
4628 return FAIL;
4629 }
4630 // extra question mark appended: ignore case
4631 if (p[len] == '?')
4632 {
4633 ic = TRUE;
4634 ++len;
4635 }
4636 // extra '#' appended: match case (ignored)
4637 else if (p[len] == '#')
4638 ++len;
4639 // nothing appended: match case
4640
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004641 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004643 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004644 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004645 }
4646
4647 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004648 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004649 return FAIL;
4650
Bram Moolenaara5565e42020-05-09 15:44:01 +02004651 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652 return FAIL;
4653
Bram Moolenaara5565e42020-05-09 15:44:01 +02004654 if (ppconst->pp_used == ppconst_used + 2)
4655 {
4656 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4657 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4658 int ret;
4659
4660 // Both sides are a constant, compute the result now.
4661 // First check for a valid combination of types, this is more
4662 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004663 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004664 ret = FAIL;
4665 else
4666 {
4667 ret = typval_compare(tv1, tv2, type, ic);
4668 tv1->v_type = VAR_BOOL;
4669 tv1->vval.v_number = tv1->vval.v_number
4670 ? VVAL_TRUE : VVAL_FALSE;
4671 clear_tv(tv2);
4672 --ppconst->pp_used;
4673 }
4674 return ret;
4675 }
4676
4677 generate_ppconst(cctx, ppconst);
4678 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004679 }
4680
4681 return OK;
4682}
4683
Bram Moolenaar7f141552020-05-09 17:35:53 +02004684static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4685
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004686/*
4687 * Compile || or &&.
4688 */
4689 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004690compile_and_or(
4691 char_u **arg,
4692 cctx_T *cctx,
4693 char *op,
4694 ppconst_T *ppconst,
4695 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004696{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004697 char_u *next;
4698 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004699 int opchar = *op;
4700
4701 if (p[0] == opchar && p[1] == opchar)
4702 {
4703 garray_T *instr = &cctx->ctx_instr;
4704 garray_T end_ga;
4705
4706 /*
4707 * Repeat until there is no following "||" or "&&"
4708 */
4709 ga_init2(&end_ga, sizeof(int), 10);
4710 while (p[0] == opchar && p[1] == opchar)
4711 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004712 if (next != NULL)
4713 {
4714 *arg = next_line_from_context(cctx, TRUE);
4715 p = skipwhite(*arg);
4716 }
4717
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004718 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4719 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004720 semsg(_(e_white_space_required_before_and_after_str_at_str),
4721 op, *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004722 return FAIL;
4723 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004724
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004725 // TODO: use ppconst if the value is a constant and check
4726 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004727 generate_ppconst(cctx, ppconst);
4728
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004729 // Every part must evaluate to a bool.
4730 if (bool_on_stack(cctx) == FAIL)
4731 {
4732 ga_clear(&end_ga);
4733 return FAIL;
4734 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004735
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004736 if (ga_grow(&end_ga, 1) == FAIL)
4737 {
4738 ga_clear(&end_ga);
4739 return FAIL;
4740 }
4741 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4742 ++end_ga.ga_len;
4743 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004744 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004745
4746 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004747 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004748 {
4749 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004750 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004751 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004752
Bram Moolenaara5565e42020-05-09 15:44:01 +02004753 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4754 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004755 {
4756 ga_clear(&end_ga);
4757 return FAIL;
4758 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004759
4760 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004761 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004762 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004763
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004764 // Every part must evaluate to a bool.
4765 if (bool_on_stack(cctx) == FAIL)
4766 {
4767 ga_clear(&end_ga);
4768 return FAIL;
4769 }
4770
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004771 // Fill in the end label in all jumps.
4772 while (end_ga.ga_len > 0)
4773 {
4774 isn_T *isn;
4775
4776 --end_ga.ga_len;
4777 isn = ((isn_T *)instr->ga_data)
4778 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4779 isn->isn_arg.jump.jump_where = instr->ga_len;
4780 }
4781 ga_clear(&end_ga);
4782 }
4783
4784 return OK;
4785}
4786
4787/*
4788 * expr4a && expr4a && expr4a logical AND
4789 *
4790 * Produces instructions:
4791 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004792 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004793 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004794 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004795 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004796 * EVAL expr4c Push result of "expr4c"
4797 * end:
4798 */
4799 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004800compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004801{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004802 int ppconst_used = ppconst->pp_used;
4803
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004804 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004805 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004806 return FAIL;
4807
4808 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004809 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004810}
4811
4812/*
4813 * expr3a || expr3b || expr3c logical OR
4814 *
4815 * Produces instructions:
4816 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004817 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004818 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004819 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004820 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004821 * EVAL expr3c Push result of "expr3c"
4822 * end:
4823 */
4824 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004825compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004826{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004827 int ppconst_used = ppconst->pp_used;
4828
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004829 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004830 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004831 return FAIL;
4832
4833 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004834 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004835}
4836
4837/*
4838 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004839 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004840 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004841 * JUMP_IF_FALSE alt jump if false
4842 * EVAL expr1a
4843 * JUMP_ALWAYS end
4844 * alt: EVAL expr1b
4845 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004846 *
4847 * Toplevel expression: expr2 ?? expr1
4848 * Produces instructions:
4849 * EVAL expr2 Push result of "expr2"
4850 * JUMP_AND_KEEP_IF_TRUE end jump if true
4851 * EVAL expr1
4852 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004853 */
4854 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004855compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004856{
4857 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004858 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004859 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004860
Bram Moolenaar3988f642020-08-27 22:43:03 +02004861 // Ignore all kinds of errors when not producing code.
4862 if (cctx->ctx_skip == SKIP_YES)
4863 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004864 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004865 return OK;
4866 }
4867
Bram Moolenaar61a89812020-05-07 16:58:17 +02004868 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004869 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870 return FAIL;
4871
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004872 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004873 if (*p == '?')
4874 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004875 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004876 garray_T *instr = &cctx->ctx_instr;
4877 garray_T *stack = &cctx->ctx_type_stack;
4878 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004879 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004880 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004881 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004882 int has_const_expr = FALSE;
4883 int const_value = FALSE;
4884 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004885
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004886 if (next != NULL)
4887 {
4888 *arg = next_line_from_context(cctx, TRUE);
4889 p = skipwhite(*arg);
4890 }
4891
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004892 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004893 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004894 semsg(_(e_white_space_required_before_and_after_str_at_str),
4895 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004896 return FAIL;
4897 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004898
Bram Moolenaara5565e42020-05-09 15:44:01 +02004899 if (ppconst->pp_used == ppconst_used + 1)
4900 {
4901 // the condition is a constant, we know whether the ? or the :
4902 // expression is to be evaluated.
4903 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004904 if (op_falsy)
4905 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4906 else
4907 {
4908 int error = FALSE;
4909
4910 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4911 &error);
4912 if (error)
4913 return FAIL;
4914 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004915 cctx->ctx_skip = save_skip == SKIP_YES ||
4916 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4917
4918 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4919 // "left ?? right" and "left" is truthy: produce "left"
4920 generate_ppconst(cctx, ppconst);
4921 else
4922 {
4923 clear_tv(&ppconst->pp_tv[ppconst_used]);
4924 --ppconst->pp_used;
4925 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004926 }
4927 else
4928 {
4929 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004930 if (op_falsy)
4931 end_idx = instr->ga_len;
4932 generate_JUMP(cctx, op_falsy
4933 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4934 if (op_falsy)
4935 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004936 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004937
4938 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01004939 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004940 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004941 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004942 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004943
Bram Moolenaara5565e42020-05-09 15:44:01 +02004944 if (!has_const_expr)
4945 {
4946 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004947
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004948 if (!op_falsy)
4949 {
4950 // remember the type and drop it
4951 --stack->ga_len;
4952 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004953
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004954 end_idx = instr->ga_len;
4955 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004956
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004957 // jump here from JUMP_IF_FALSE
4958 isn = ((isn_T *)instr->ga_data) + alt_idx;
4959 isn->isn_arg.jump.jump_where = instr->ga_len;
4960 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004961 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004962
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004963 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004964 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004965 // Check for the ":".
4966 p = may_peek_next_line(cctx, *arg, &next);
4967 if (*p != ':')
4968 {
4969 emsg(_(e_missing_colon));
4970 return FAIL;
4971 }
4972 if (next != NULL)
4973 {
4974 *arg = next_line_from_context(cctx, TRUE);
4975 p = skipwhite(*arg);
4976 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004977
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004978 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4979 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004980 semsg(_(e_white_space_required_before_and_after_str_at_str),
4981 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004982 return FAIL;
4983 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004984
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004985 // evaluate the third expression
4986 if (has_const_expr)
4987 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004988 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01004989 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004990 return FAIL;
4991 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4992 return FAIL;
4993 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004994
Bram Moolenaara5565e42020-05-09 15:44:01 +02004995 if (!has_const_expr)
4996 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004997 type_T **typep;
4998
Bram Moolenaara5565e42020-05-09 15:44:01 +02004999 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005000
Bram Moolenaara5565e42020-05-09 15:44:01 +02005001 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005002 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5003 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005004
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005005 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005006 isn = ((isn_T *)instr->ga_data) + end_idx;
5007 isn->isn_arg.jump.jump_where = instr->ga_len;
5008 }
5009
5010 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005011 }
5012 return OK;
5013}
5014
5015/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005016 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005017 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5018 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005019 */
5020 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005021compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005022{
5023 ppconst_T ppconst;
5024
5025 CLEAR_FIELD(ppconst);
5026 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5027 {
5028 clear_ppconst(&ppconst);
5029 return FAIL;
5030 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005031 if (is_const != NULL)
5032 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005033 if (generate_ppconst(cctx, &ppconst) == FAIL)
5034 return FAIL;
5035 return OK;
5036}
5037
5038/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005039 * Toplevel expression.
5040 */
5041 static int
5042compile_expr0(char_u **arg, cctx_T *cctx)
5043{
5044 return compile_expr0_ext(arg, cctx, NULL);
5045}
5046
5047/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005048 * compile "return [expr]"
5049 */
5050 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005051compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005052{
5053 char_u *p = arg;
5054 garray_T *stack = &cctx->ctx_type_stack;
5055 type_T *stack_type;
5056
5057 if (*p != NUL && *p != '|' && *p != '\n')
5058 {
5059 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02005060 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005061 return NULL;
5062
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005063 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005064 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005065 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01005066 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005067 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5068 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005069 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005070 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005071 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005072 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005073 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005074 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5075 && stack_type->tt_type != VAR_VOID
5076 && stack_type->tt_type != VAR_UNKNOWN)
5077 {
5078 emsg(_(e_returning_value_in_function_without_return_type));
5079 return NULL;
5080 }
5081 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005082 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005083 return NULL;
5084 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005085 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005086 }
5087 else
5088 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005089 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005090 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005091 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5092 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005093 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005094 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005095 return NULL;
5096 }
5097
5098 // No argument, return zero.
5099 generate_PUSHNR(cctx, 0);
5100 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005101
5102 // Undo any command modifiers.
5103 generate_undo_cmdmods(cctx);
5104
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005105 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005106 return NULL;
5107
5108 // "return val | endif" is possible
5109 return skipwhite(p);
5110}
5111
5112/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005113 * Get a line from the compilation context, compatible with exarg_T getline().
5114 * Return a pointer to the line in allocated memory.
5115 * Return NULL for end-of-file or some error.
5116 */
5117 static char_u *
5118exarg_getline(
5119 int c UNUSED,
5120 void *cookie,
5121 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005122 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005123{
5124 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005125 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005126
Bram Moolenaar66250c92020-08-20 15:02:42 +02005127 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005128 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005129 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005130 return NULL;
5131 ++cctx->ctx_lnum;
5132 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5133 // Comment lines result in NULL pointers, skip them.
5134 if (p != NULL)
5135 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005136 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005137}
5138
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005139 void
5140fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5141{
5142 eap->getline = exarg_getline;
5143 eap->cookie = cctx;
5144}
5145
Bram Moolenaar04b12692020-05-04 23:24:44 +02005146/*
5147 * Compile a nested :def command.
5148 */
5149 static char_u *
5150compile_nested_function(exarg_T *eap, cctx_T *cctx)
5151{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005152 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005153 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005154 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005155 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005156 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005157 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005158
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005159 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005160 {
5161 emsg(_(e_cannot_use_bang_with_nested_def));
5162 return NULL;
5163 }
5164
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005165 if (*name_start == '/')
5166 {
5167 name_end = skip_regexp(name_start + 1, '/', TRUE);
5168 if (*name_end == '/')
5169 ++name_end;
5170 eap->nextcmd = check_nextcmd(name_end);
5171 }
5172 if (name_end == name_start || *skipwhite(name_end) != '(')
5173 {
5174 if (!ends_excmd2(name_start, name_end))
5175 {
5176 semsg(_(e_invalid_command_str), eap->cmd);
5177 return NULL;
5178 }
5179
5180 // "def" or "def Name": list functions
5181 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5182 return NULL;
5183 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5184 }
5185
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005186 // Only g:Func() can use a namespace.
5187 if (name_start[1] == ':' && !is_global)
5188 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005189 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005190 return NULL;
5191 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005192 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005193 return NULL;
5194
Bram Moolenaar04b12692020-05-04 23:24:44 +02005195 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005196 fill_exarg_from_cctx(eap, cctx);
5197
Bram Moolenaar04b12692020-05-04 23:24:44 +02005198 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005199 lambda_name = vim_strsave(get_lambda_name());
5200 if (lambda_name == NULL)
5201 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005202 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005203
Bram Moolenaar822ba242020-05-24 23:00:18 +02005204 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005205 {
5206 r = eap->skip ? OK : FAIL;
5207 goto theend;
5208 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005209
5210 // copy over the block scope IDs before compiling
5211 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5212 {
5213 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5214
5215 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5216 if (ufunc->uf_block_ids != NULL)
5217 {
5218 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5219 sizeof(int) * block_depth);
5220 ufunc->uf_block_depth = block_depth;
5221 }
5222 }
5223
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005224 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5225 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005226 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005227 {
5228 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005229 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005230 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005231
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005232 if (is_global)
5233 {
5234 char_u *func_name = vim_strnsave(name_start + 2,
5235 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005236
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005237 if (func_name == NULL)
5238 r = FAIL;
5239 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005240 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005241 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005242 lambda_name = NULL;
5243 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005244 }
5245 else
5246 {
5247 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005248 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005249 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005250
Bram Moolenaareef21022020-08-01 22:16:43 +02005251 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005252 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005253 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005254 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005255 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5256 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005257 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005258
5259theend:
5260 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005261 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005262}
5263
5264/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005265 * Return the length of an assignment operator, or zero if there isn't one.
5266 */
5267 int
5268assignment_len(char_u *p, int *heredoc)
5269{
5270 if (*p == '=')
5271 {
5272 if (p[1] == '<' && p[2] == '<')
5273 {
5274 *heredoc = TRUE;
5275 return 3;
5276 }
5277 return 1;
5278 }
5279 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5280 return 2;
5281 if (STRNCMP(p, "..=", 3) == 0)
5282 return 3;
5283 return 0;
5284}
5285
5286// words that cannot be used as a variable
5287static char *reserved[] = {
5288 "true",
5289 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005290 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005291 NULL
5292};
5293
Bram Moolenaar752fc692021-01-04 21:57:11 +01005294// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005295typedef enum {
5296 dest_local,
5297 dest_option,
5298 dest_env,
5299 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005300 dest_buffer,
5301 dest_window,
5302 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005303 dest_vimvar,
5304 dest_script,
5305 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005306 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005307} assign_dest_T;
5308
Bram Moolenaar752fc692021-01-04 21:57:11 +01005309// Used by compile_lhs() to store information about the LHS of an assignment
5310// and one argument of ":unlet" with an index.
5311typedef struct {
5312 assign_dest_T lhs_dest; // type of destination
5313
5314 char_u *lhs_name; // allocated name including
5315 // "[expr]" or ".name".
5316 size_t lhs_varlen; // length of the variable without
5317 // "[expr]" or ".name"
5318 char_u *lhs_dest_end; // end of the destination, including
5319 // "[expr]" or ".name".
5320
5321 int lhs_has_index; // has "[expr]" or ".name"
5322
5323 int lhs_new_local; // create new local variable
5324 int lhs_opt_flags; // for when destination is an option
5325 int lhs_vimvaridx; // for when destination is a v:var
5326
5327 lvar_T lhs_local_lvar; // used for existing local destination
5328 lvar_T lhs_arg_lvar; // used for argument destination
5329 lvar_T *lhs_lvar; // points to destination lvar
5330 int lhs_scriptvar_sid;
5331 int lhs_scriptvar_idx;
5332
5333 int lhs_has_type; // type was specified
5334 type_T *lhs_type;
5335 type_T *lhs_member_type;
5336} lhs_T;
5337
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005338/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005339 * Generate the load instruction for "name".
5340 */
5341 static void
5342generate_loadvar(
5343 cctx_T *cctx,
5344 assign_dest_T dest,
5345 char_u *name,
5346 lvar_T *lvar,
5347 type_T *type)
5348{
5349 switch (dest)
5350 {
5351 case dest_option:
5352 // TODO: check the option exists
5353 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5354 break;
5355 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005356 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5357 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5358 else
5359 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005360 break;
5361 case dest_buffer:
5362 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5363 break;
5364 case dest_window:
5365 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5366 break;
5367 case dest_tab:
5368 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5369 break;
5370 case dest_script:
5371 compile_load_scriptvar(cctx,
5372 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5373 break;
5374 case dest_env:
5375 // Include $ in the name here
5376 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5377 break;
5378 case dest_reg:
5379 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5380 break;
5381 case dest_vimvar:
5382 generate_LOADV(cctx, name + 2, TRUE);
5383 break;
5384 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005385 if (lvar->lv_from_outer > 0)
5386 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5387 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005388 else
5389 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5390 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005391 case dest_expr:
5392 // list or dict value should already be on the stack.
5393 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005394 }
5395}
5396
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005397/*
5398 * Skip over "[expr]" or ".member".
5399 * Does not check for any errors.
5400 */
5401 static char_u *
5402skip_index(char_u *start)
5403{
5404 char_u *p = start;
5405
5406 if (*p == '[')
5407 {
5408 p = skipwhite(p + 1);
5409 (void)skip_expr(&p, NULL);
5410 p = skipwhite(p);
5411 if (*p == ']')
5412 return p + 1;
5413 return p;
5414 }
5415 // if (*p == '.')
5416 return to_name_end(p + 1, TRUE);
5417}
5418
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005419 void
5420vim9_declare_error(char_u *name)
5421{
5422 char *scope = "";
5423
5424 switch (*name)
5425 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005426 case 'g': scope = _("global"); break;
5427 case 'b': scope = _("buffer"); break;
5428 case 'w': scope = _("window"); break;
5429 case 't': scope = _("tab"); break;
5430 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005431 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005432 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005433 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005434 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005435 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005436 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005437 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005438 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005439 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005440}
5441
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005442/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005443 * For one assignment figure out the type of destination. Return it in "dest".
5444 * When not recognized "dest" is not set.
5445 * For an option "opt_flags" is set.
5446 * For a v:var "vimvaridx" is set.
5447 * "type" is set to the destination type if known, unchanted otherwise.
5448 * Return FAIL if an error message was given.
5449 */
5450 static int
5451get_var_dest(
5452 char_u *name,
5453 assign_dest_T *dest,
5454 int cmdidx,
5455 int *opt_flags,
5456 int *vimvaridx,
5457 type_T **type,
5458 cctx_T *cctx)
5459{
5460 char_u *p;
5461
5462 if (*name == '&')
5463 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005464 int cc;
5465 long numval;
5466 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005467
5468 *dest = dest_option;
5469 if (cmdidx == CMD_final || cmdidx == CMD_const)
5470 {
5471 emsg(_(e_const_option));
5472 return FAIL;
5473 }
5474 p = name;
5475 p = find_option_end(&p, opt_flags);
5476 if (p == NULL)
5477 {
5478 // cannot happen?
5479 emsg(_(e_letunexp));
5480 return FAIL;
5481 }
5482 cc = *p;
5483 *p = NUL;
5484 opt_type = get_option_value(skip_option_env_lead(name),
5485 &numval, NULL, *opt_flags);
5486 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005487 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005488 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005489 case gov_unknown:
5490 semsg(_(e_unknown_option), name);
5491 return FAIL;
5492 case gov_string:
5493 case gov_hidden_string:
5494 *type = &t_string;
5495 break;
5496 case gov_bool:
5497 case gov_hidden_bool:
5498 *type = &t_bool;
5499 break;
5500 case gov_number:
5501 case gov_hidden_number:
5502 *type = &t_number;
5503 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005504 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005505 }
5506 else if (*name == '$')
5507 {
5508 *dest = dest_env;
5509 *type = &t_string;
5510 }
5511 else if (*name == '@')
5512 {
5513 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5514 {
5515 emsg_invreg(name[1]);
5516 return FAIL;
5517 }
5518 *dest = dest_reg;
5519 *type = &t_string;
5520 }
5521 else if (STRNCMP(name, "g:", 2) == 0)
5522 {
5523 *dest = dest_global;
5524 }
5525 else if (STRNCMP(name, "b:", 2) == 0)
5526 {
5527 *dest = dest_buffer;
5528 }
5529 else if (STRNCMP(name, "w:", 2) == 0)
5530 {
5531 *dest = dest_window;
5532 }
5533 else if (STRNCMP(name, "t:", 2) == 0)
5534 {
5535 *dest = dest_tab;
5536 }
5537 else if (STRNCMP(name, "v:", 2) == 0)
5538 {
5539 typval_T *vtv;
5540 int di_flags;
5541
5542 *vimvaridx = find_vim_var(name + 2, &di_flags);
5543 if (*vimvaridx < 0)
5544 {
5545 semsg(_(e_variable_not_found_str), name);
5546 return FAIL;
5547 }
5548 // We use the current value of "sandbox" here, is that OK?
5549 if (var_check_ro(di_flags, name, FALSE))
5550 return FAIL;
5551 *dest = dest_vimvar;
5552 vtv = get_vim_var_tv(*vimvaridx);
5553 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5554 }
5555 return OK;
5556}
5557
5558/*
5559 * Generate a STORE instruction for "dest", not being "dest_local".
5560 * Return FAIL when out of memory.
5561 */
5562 static int
5563generate_store_var(
5564 cctx_T *cctx,
5565 assign_dest_T dest,
5566 int opt_flags,
5567 int vimvaridx,
5568 int scriptvar_idx,
5569 int scriptvar_sid,
5570 type_T *type,
5571 char_u *name)
5572{
5573 switch (dest)
5574 {
5575 case dest_option:
5576 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5577 opt_flags);
5578 case dest_global:
5579 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005580 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5581 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005582 case dest_buffer:
5583 // include b: with the name, easier to execute that way
5584 return generate_STORE(cctx, ISN_STOREB, 0, name);
5585 case dest_window:
5586 // include w: with the name, easier to execute that way
5587 return generate_STORE(cctx, ISN_STOREW, 0, name);
5588 case dest_tab:
5589 // include t: with the name, easier to execute that way
5590 return generate_STORE(cctx, ISN_STORET, 0, name);
5591 case dest_env:
5592 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5593 case dest_reg:
5594 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5595 case dest_vimvar:
5596 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5597 case dest_script:
5598 if (scriptvar_idx < 0)
5599 {
5600 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005601 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005602
Bram Moolenaar41d61962020-12-06 20:12:43 +01005603 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005604 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5605 scriptvar_sid, type);
5606 if (name_s != name)
5607 vim_free(name_s);
5608 return r;
5609 }
5610 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5611 scriptvar_sid, scriptvar_idx, type);
5612 case dest_local:
5613 case dest_expr:
5614 // cannot happen
5615 break;
5616 }
5617 return FAIL;
5618}
5619
Bram Moolenaar752fc692021-01-04 21:57:11 +01005620 static int
5621is_decl_command(int cmdidx)
5622{
5623 return cmdidx == CMD_let || cmdidx == CMD_var
5624 || cmdidx == CMD_final || cmdidx == CMD_const;
5625}
5626
5627/*
5628 * Figure out the LHS type and other properties for an assignment or one item
5629 * of ":unlet" with an index.
5630 * Returns OK or FAIL.
5631 */
5632 static int
5633compile_lhs(
5634 char_u *var_start,
5635 lhs_T *lhs,
5636 int cmdidx,
5637 int heredoc,
5638 int oplen,
5639 cctx_T *cctx)
5640{
5641 char_u *var_end;
5642 int is_decl = is_decl_command(cmdidx);
5643
5644 CLEAR_POINTER(lhs);
5645 lhs->lhs_dest = dest_local;
5646 lhs->lhs_vimvaridx = -1;
5647 lhs->lhs_scriptvar_idx = -1;
5648
5649 // "dest_end" is the end of the destination, including "[expr]" or
5650 // ".name".
5651 // "var_end" is the end of the variable/option/etc. name.
5652 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5653 if (*var_start == '@')
5654 var_end = var_start + 2;
5655 else
5656 {
5657 // skip over the leading "&", "&l:", "&g:" and "$"
5658 var_end = skip_option_env_lead(var_start);
5659 var_end = to_name_end(var_end, TRUE);
5660 }
5661
5662 // "a: type" is declaring variable "a" with a type, not dict "a:".
5663 if (is_decl && lhs->lhs_dest_end == var_start + 2
5664 && lhs->lhs_dest_end[-1] == ':')
5665 --lhs->lhs_dest_end;
5666 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5667 --var_end;
5668
5669 // compute the length of the destination without "[expr]" or ".name"
5670 lhs->lhs_varlen = var_end - var_start;
5671 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5672 if (lhs->lhs_name == NULL)
5673 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005674
5675 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5676 // Something follows after the variable: "var[idx]" or "var.key".
5677 lhs->lhs_has_index = TRUE;
5678
Bram Moolenaar752fc692021-01-04 21:57:11 +01005679 if (heredoc)
5680 lhs->lhs_type = &t_list_string;
5681 else
5682 lhs->lhs_type = &t_any;
5683
5684 if (cctx->ctx_skip != SKIP_YES)
5685 {
5686 int declare_error = FALSE;
5687
5688 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5689 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5690 &lhs->lhs_type, cctx) == FAIL)
5691 return FAIL;
5692 if (lhs->lhs_dest != dest_local)
5693 {
5694 // Specific kind of variable recognized.
5695 declare_error = is_decl;
5696 }
5697 else
5698 {
5699 int idx;
5700
5701 // No specific kind of variable recognized, just a name.
5702 for (idx = 0; reserved[idx] != NULL; ++idx)
5703 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5704 {
5705 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5706 return FAIL;
5707 }
5708
5709
5710 if (lookup_local(var_start, lhs->lhs_varlen,
5711 &lhs->lhs_local_lvar, cctx) == OK)
5712 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5713 else
5714 {
5715 CLEAR_FIELD(lhs->lhs_arg_lvar);
5716 if (arg_exists(var_start, lhs->lhs_varlen,
5717 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5718 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5719 {
5720 if (is_decl)
5721 {
5722 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5723 return FAIL;
5724 }
5725 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5726 }
5727 }
5728 if (lhs->lhs_lvar != NULL)
5729 {
5730 if (is_decl)
5731 {
5732 semsg(_(e_variable_already_declared), lhs->lhs_name);
5733 return FAIL;
5734 }
5735 }
5736 else
5737 {
5738 int script_namespace = lhs->lhs_varlen > 1
5739 && STRNCMP(var_start, "s:", 2) == 0;
5740 int script_var = (script_namespace
5741 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
5742 FALSE, cctx)
5743 : script_var_exists(var_start, lhs->lhs_varlen,
5744 TRUE, cctx)) == OK;
5745 imported_T *import =
5746 find_imported(var_start, lhs->lhs_varlen, cctx);
5747
5748 if (script_namespace || script_var || import != NULL)
5749 {
5750 char_u *rawname = lhs->lhs_name
5751 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5752
5753 if (is_decl)
5754 {
5755 if (script_namespace)
5756 semsg(_(e_cannot_declare_script_variable_in_function),
5757 lhs->lhs_name);
5758 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005759 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01005760 lhs->lhs_name);
5761 return FAIL;
5762 }
5763 else if (cctx->ctx_ufunc->uf_script_ctx_version
5764 == SCRIPT_VERSION_VIM9
5765 && script_namespace
5766 && !script_var && import == NULL)
5767 {
5768 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5769 return FAIL;
5770 }
5771
5772 lhs->lhs_dest = dest_script;
5773
5774 // existing script-local variables should have a type
5775 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5776 if (import != NULL)
5777 lhs->lhs_scriptvar_sid = import->imp_sid;
5778 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5779 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005780 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005781 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005782 lhs->lhs_scriptvar_sid, rawname,
5783 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5784 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005785 if (lhs->lhs_scriptvar_idx >= 0)
5786 {
5787 scriptitem_T *si = SCRIPT_ITEM(
5788 lhs->lhs_scriptvar_sid);
5789 svar_T *sv =
5790 ((svar_T *)si->sn_var_vals.ga_data)
5791 + lhs->lhs_scriptvar_idx;
5792 lhs->lhs_type = sv->sv_type;
5793 }
5794 }
5795 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005796 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005797 == FAIL)
5798 return FAIL;
5799 }
5800 }
5801
5802 if (declare_error)
5803 {
5804 vim9_declare_error(lhs->lhs_name);
5805 return FAIL;
5806 }
5807 }
5808
5809 // handle "a:name" as a name, not index "name" on "a"
5810 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5811 var_end = lhs->lhs_dest_end;
5812
5813 if (lhs->lhs_dest != dest_option)
5814 {
5815 if (is_decl && *var_end == ':')
5816 {
5817 char_u *p;
5818
5819 // parse optional type: "let var: type = expr"
5820 if (!VIM_ISWHITE(var_end[1]))
5821 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01005822 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005823 return FAIL;
5824 }
5825 p = skipwhite(var_end + 1);
5826 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5827 if (lhs->lhs_type == NULL)
5828 return FAIL;
5829 lhs->lhs_has_type = TRUE;
5830 }
5831 else if (lhs->lhs_lvar != NULL)
5832 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5833 }
5834
5835 if (oplen == 3 && !heredoc && lhs->lhs_dest != dest_global
5836 && lhs->lhs_type->tt_type != VAR_STRING
5837 && lhs->lhs_type->tt_type != VAR_ANY)
5838 {
5839 emsg(_(e_can_only_concatenate_to_string));
5840 return FAIL;
5841 }
5842
5843 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5844 && cctx->ctx_skip != SKIP_YES)
5845 {
5846 if (oplen > 1 && !heredoc)
5847 {
5848 // +=, /=, etc. require an existing variable
5849 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5850 return FAIL;
5851 }
5852 if (!is_decl)
5853 {
5854 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5855 return FAIL;
5856 }
5857
Bram Moolenaar3f327882021-03-17 20:56:38 +01005858 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005859 if ((lhs->lhs_type->tt_type == VAR_FUNC
5860 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01005861 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01005862 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01005863
5864 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005865 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5866 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5867 if (lhs->lhs_lvar == NULL)
5868 return FAIL;
5869 lhs->lhs_new_local = TRUE;
5870 }
5871
5872 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01005873 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005874 {
5875 // Something follows after the variable: "var[idx]" or "var.key".
5876 // TODO: should we also handle "->func()" here?
5877 if (is_decl)
5878 {
5879 emsg(_(e_cannot_use_index_when_declaring_variable));
5880 return FAIL;
5881 }
5882
5883 if (var_start[lhs->lhs_varlen] == '['
5884 || var_start[lhs->lhs_varlen] == '.')
5885 {
5886 char_u *after = var_start + lhs->lhs_varlen;
5887 char_u *p;
5888
5889 // Only the last index is used below, if there are others
5890 // before it generate code for the expression. Thus for
5891 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5892 for (;;)
5893 {
5894 p = skip_index(after);
5895 if (*p != '[' && *p != '.')
5896 break;
5897 after = p;
5898 }
5899 if (after > var_start + lhs->lhs_varlen)
5900 {
5901 lhs->lhs_varlen = after - var_start;
5902 lhs->lhs_dest = dest_expr;
5903 // We don't know the type before evaluating the expression,
5904 // use "any" until then.
5905 lhs->lhs_type = &t_any;
5906 }
5907
Bram Moolenaar752fc692021-01-04 21:57:11 +01005908 if (lhs->lhs_type->tt_member == NULL)
5909 lhs->lhs_member_type = &t_any;
5910 else
5911 lhs->lhs_member_type = lhs->lhs_type->tt_member;
5912 }
5913 else
5914 {
5915 semsg("Not supported yet: %s", var_start);
5916 return FAIL;
5917 }
5918 }
5919 return OK;
5920}
5921
5922/*
5923 * Assignment to a list or dict member, or ":unlet" for the item, using the
5924 * information in "lhs".
5925 * Returns OK or FAIL.
5926 */
5927 static int
5928compile_assign_unlet(
5929 char_u *var_start,
5930 lhs_T *lhs,
5931 int is_assign,
5932 type_T *rhs_type,
5933 cctx_T *cctx)
5934{
5935 char_u *p;
5936 int r;
5937 vartype_T dest_type;
5938 size_t varlen = lhs->lhs_varlen;
5939 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01005940 int range = FALSE;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005941
5942 // Compile the "idx" in "var[idx]" or "key" in "var.key".
5943 p = var_start + varlen;
5944 if (*p == '[')
5945 {
5946 p = skipwhite(p + 1);
5947 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01005948
5949 if (r == OK && *skipwhite(p) == ':')
5950 {
5951 // unlet var[idx : idx]
5952 if (is_assign)
5953 {
5954 semsg(_(e_cannot_use_range_with_assignment_str), p);
5955 return FAIL;
5956 }
5957 range = TRUE;
5958 p = skipwhite(p);
5959 if (!IS_WHITE_OR_NUL(p[-1]) || !IS_WHITE_OR_NUL(p[1]))
5960 {
5961 semsg(_(e_white_space_required_before_and_after_str_at_str),
5962 ":", p);
5963 return FAIL;
5964 }
5965 p = skipwhite(p + 1);
5966 r = compile_expr0(&p, cctx);
5967 }
5968
Bram Moolenaar752fc692021-01-04 21:57:11 +01005969 if (r == OK && *skipwhite(p) != ']')
5970 {
5971 // this should not happen
5972 emsg(_(e_missbrac));
5973 r = FAIL;
5974 }
5975 }
5976 else // if (*p == '.')
5977 {
5978 char_u *key_end = to_name_end(p + 1, TRUE);
5979 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5980
5981 r = generate_PUSHS(cctx, key);
5982 }
5983 if (r == FAIL)
5984 return FAIL;
5985
5986 if (lhs->lhs_type == &t_any)
5987 {
5988 // Index on variable of unknown type: check at runtime.
5989 dest_type = VAR_ANY;
5990 }
5991 else
5992 {
5993 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01005994 if (dest_type == VAR_DICT && range)
5995 {
5996 emsg(e_cannot_use_range_with_dictionary);
5997 return FAIL;
5998 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01005999 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
6000 return FAIL;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006001 if (dest_type == VAR_LIST)
6002 {
6003 if (range
6004 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 2],
Bram Moolenaarf30a14d2021-01-17 21:51:24 +01006005 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006006 return FAIL;
6007 if (need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
6008 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
6009 return FAIL;
6010 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006011 }
6012
6013 // Load the dict or list. On the stack we then have:
6014 // - value (for assignment, not for :unlet)
6015 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006016 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006017 // - variable
6018 if (lhs->lhs_dest == dest_expr)
6019 {
6020 int c = var_start[varlen];
6021
6022 // Evaluate "ll[expr]" of "ll[expr][idx]"
6023 p = var_start;
6024 var_start[varlen] = NUL;
6025 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6026 {
6027 // this should not happen
6028 emsg(_(e_missbrac));
6029 return FAIL;
6030 }
6031 var_start[varlen] = c;
6032
6033 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6034 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6035 // now we can properly check the type
6036 if (lhs->lhs_type->tt_member != NULL && rhs_type != &t_void
Bram Moolenaar351ead02021-01-16 16:07:01 +01006037 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006038 FALSE, FALSE) == FAIL)
6039 return FAIL;
6040 }
6041 else
6042 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6043 lhs->lhs_lvar, lhs->lhs_type);
6044
6045 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
6046 {
6047 if (is_assign)
6048 {
6049 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
6050
6051 if (isn == NULL)
6052 return FAIL;
6053 isn->isn_arg.vartype = dest_type;
6054 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006055 else if (range)
6056 {
6057 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6058 return FAIL;
6059 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006060 else
6061 {
6062 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6063 return FAIL;
6064 }
6065 }
6066 else
6067 {
6068 emsg(_(e_indexable_type_required));
6069 return FAIL;
6070 }
6071
6072 return OK;
6073}
6074
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006075/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006076 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006077 * "let name"
6078 * "var name = expr"
6079 * "final name = expr"
6080 * "const name = expr"
6081 * "name = expr"
6082 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006083 * Return NULL for an error.
6084 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006085 */
6086 static char_u *
6087compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6088{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006089 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006090 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006091 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006092 char_u *ret = NULL;
6093 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006094 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006095 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006096 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006097 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006098 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006099 int oplen = 0;
6100 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006101 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006102 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006103 int is_decl = is_decl_command(cmdidx);
6104 lhs_T lhs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006105
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006106 // Skip over the "var" or "[var, var]" to get to any "=".
6107 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6108 if (p == NULL)
6109 return *arg == '[' ? arg : NULL;
6110
6111 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006112 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006113 // TODO: should we allow this, and figure out type inference from list
6114 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006115 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006116 return NULL;
6117 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006118 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006119
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006120 sp = p;
6121 p = skipwhite(p);
6122 op = p;
6123 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006124
6125 if (var_count > 0 && oplen == 0)
6126 // can be something like "[1, 2]->func()"
6127 return arg;
6128
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006129 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006130 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006131 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006132 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006133 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006134
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006135 if (heredoc)
6136 {
6137 list_T *l;
6138 listitem_T *li;
6139
6140 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006141 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006142 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006143 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006144 if (l == NULL)
6145 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006146
Bram Moolenaar078269b2020-09-21 20:35:55 +02006147 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006148 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006149 // Push each line and the create the list.
6150 FOR_ALL_LIST_ITEMS(l, li)
6151 {
6152 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6153 li->li_tv.vval.v_string = NULL;
6154 }
6155 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006156 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006157 list_free(l);
6158 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006159 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006160 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006161 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006162 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006163 char_u *wp;
6164
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006165 // for "[var, var] = expr" evaluate the expression here, loop over the
6166 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006167 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006168
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006169 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006170 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006171 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006172 if (compile_expr0(&p, cctx) == FAIL)
6173 return NULL;
6174 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006175
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006176 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006177 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006178 type_T *stacktype;
6179
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006180 stacktype = stack->ga_len == 0 ? &t_void
6181 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006182 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006183 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006184 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006185 goto theend;
6186 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006187 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006188 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006189 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006190 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006191 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6192 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006193 if (stacktype->tt_member != NULL)
6194 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006195 }
6196 }
6197
6198 /*
6199 * Loop over variables in "[var, var] = expr".
6200 * For "var = expr" and "let var: type" this is done only once.
6201 */
6202 if (var_count > 0)
6203 var_start = skipwhite(arg + 1); // skip over the "["
6204 else
6205 var_start = arg;
6206 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6207 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006208 int instr_count = -1;
6209
Bram Moolenaar752fc692021-01-04 21:57:11 +01006210 vim_free(lhs.lhs_name);
6211
6212 /*
6213 * Figure out the LHS type and other properties.
6214 */
6215 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6216 goto theend;
6217
6218 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006219 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006220 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006221 goto theend;
6222 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006223 if (!is_decl && lhs.lhs_lvar != NULL
6224 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006225 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006226 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006227 goto theend;
6228 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006229
6230 if (!heredoc)
6231 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006232 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006233 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006234 if (oplen > 0 && var_count == 0)
6235 {
6236 // skip over the "=" and the expression
6237 p = skipwhite(op + oplen);
6238 compile_expr0(&p, cctx);
6239 }
6240 }
6241 else if (oplen > 0)
6242 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006243 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006244 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006245
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006246 // For "var = expr" evaluate the expression.
6247 if (var_count == 0)
6248 {
6249 int r;
6250
6251 // for "+=", "*=", "..=" etc. first load the current value
6252 if (*op != '=')
6253 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006254 generate_loadvar(cctx, lhs.lhs_dest, lhs.lhs_name,
6255 lhs.lhs_lvar, lhs.lhs_type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006256
Bram Moolenaar752fc692021-01-04 21:57:11 +01006257 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006258 {
6259 // TODO: get member from list or dict
6260 emsg("Index with operation not supported yet");
6261 goto theend;
6262 }
6263 }
6264
6265 // Compile the expression. Temporarily hide the new local
6266 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006267 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006268 --cctx->ctx_locals.ga_len;
6269 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006270 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006271 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006272 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006273 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006274 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006275 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006276 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006277 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006278 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006279 ++cctx->ctx_locals.ga_len;
6280 if (r == FAIL)
6281 goto theend;
6282 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006283 else if (semicolon && var_idx == var_count - 1)
6284 {
6285 // For "[var; var] = expr" get the rest of the list
6286 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6287 goto theend;
6288 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006289 else
6290 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006291 // For "[var, var] = expr" get the "var_idx" item from the
6292 // list.
6293 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006294 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006295 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006296
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006297 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006298 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006299 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006300 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006301 if ((rhs_type->tt_type == VAR_FUNC
6302 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006303 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006304 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006305 goto theend;
6306
Bram Moolenaar752fc692021-01-04 21:57:11 +01006307 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006308 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006309 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006310 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006311 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006312 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006313 }
6314 else
6315 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006316 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006317 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006318 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006319 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006320 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006321 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006322 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006323 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006324 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006325 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006326 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006327 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006328 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006329 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006330 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006331
Bram Moolenaar71abe482020-09-14 22:28:30 +02006332 // without operator check type here, otherwise below
Bram Moolenaar752fc692021-01-04 21:57:11 +01006333 if (lhs.lhs_has_index)
6334 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006335 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006336 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006337 goto theend;
6338 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006339 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006340 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006341 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006342 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006343 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006344 else if (cmdidx == CMD_final)
6345 {
6346 emsg(_(e_final_requires_a_value));
6347 goto theend;
6348 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006349 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006350 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006351 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006352 goto theend;
6353 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006354 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006355 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006356 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006357 goto theend;
6358 }
6359 else
6360 {
6361 // variables are always initialized
6362 if (ga_grow(instr, 1) == FAIL)
6363 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006364 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006365 {
6366 case VAR_BOOL:
6367 generate_PUSHBOOL(cctx, VVAL_FALSE);
6368 break;
6369 case VAR_FLOAT:
6370#ifdef FEAT_FLOAT
6371 generate_PUSHF(cctx, 0.0);
6372#endif
6373 break;
6374 case VAR_STRING:
6375 generate_PUSHS(cctx, NULL);
6376 break;
6377 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006378 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006379 break;
6380 case VAR_FUNC:
6381 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6382 break;
6383 case VAR_LIST:
6384 generate_NEWLIST(cctx, 0);
6385 break;
6386 case VAR_DICT:
6387 generate_NEWDICT(cctx, 0);
6388 break;
6389 case VAR_JOB:
6390 generate_PUSHJOB(cctx, NULL);
6391 break;
6392 case VAR_CHANNEL:
6393 generate_PUSHCHANNEL(cctx, NULL);
6394 break;
6395 case VAR_NUMBER:
6396 case VAR_UNKNOWN:
6397 case VAR_ANY:
6398 case VAR_PARTIAL:
6399 case VAR_VOID:
6400 case VAR_SPECIAL: // cannot happen
6401 generate_PUSHNR(cctx, 0);
6402 break;
6403 }
6404 }
6405 if (var_count == 0)
6406 end = p;
6407 }
6408
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006409 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006410 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006411 break;
6412
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006413 if (oplen > 0 && *op != '=')
6414 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006415 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006416 type_T *stacktype;
6417
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006418 if (*op == '.')
6419 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006420 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006421 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006422 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006423 if (
6424#ifdef FEAT_FLOAT
6425 // If variable is float operation with number is OK.
6426 !(expected == &t_float && stacktype == &t_number) &&
6427#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006428 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006429 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006430 goto theend;
6431
6432 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006433 {
6434 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6435 goto theend;
6436 }
6437 else if (*op == '+')
6438 {
6439 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006440 operator_type(lhs.lhs_member_type, stacktype),
6441 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006442 goto theend;
6443 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006444 else if (generate_two_op(cctx, op) == FAIL)
6445 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006446 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006447
Bram Moolenaar752fc692021-01-04 21:57:11 +01006448 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006449 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006450 // Use the info in "lhs" to store the value at the index in the
6451 // list or dict.
6452 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6453 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006454 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006455 }
6456 else
6457 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006458 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
6459 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006460 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006461 generate_LOCKCONST(cctx);
6462
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006463 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006464 && (lhs.lhs_type->tt_type == VAR_DICT
6465 || lhs.lhs_type->tt_type == VAR_LIST)
6466 && lhs.lhs_type->tt_member != NULL
6467 && lhs.lhs_type->tt_member != &t_any
6468 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006469 // Set the type in the list or dict, so that it can be checked,
6470 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006471 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006472
Bram Moolenaar752fc692021-01-04 21:57:11 +01006473 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006474 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006475 if (generate_store_var(cctx, lhs.lhs_dest,
6476 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6477 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6478 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006479 goto theend;
6480 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006481 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006482 {
6483 isn_T *isn = ((isn_T *)instr->ga_data)
6484 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006485
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006486 // optimization: turn "var = 123" from ISN_PUSHNR +
6487 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006488 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006489 && instr->ga_len == instr_count + 1
6490 && isn->isn_type == ISN_PUSHNR)
6491 {
6492 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006493
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006494 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006495 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006496 isn->isn_arg.storenr.stnr_val = val;
6497 if (stack->ga_len > 0)
6498 --stack->ga_len;
6499 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006500 else if (lhs.lhs_lvar->lv_from_outer > 0)
6501 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6502 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006503 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006504 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006505 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006506 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006507
6508 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006509 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006510 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006511
6512 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006513 if (var_count > 0 && !semicolon)
6514 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006515 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006516 goto theend;
6517 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006518
Bram Moolenaarb2097502020-07-19 17:17:02 +02006519 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006520
6521theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006522 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006523 return ret;
6524}
6525
6526/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006527 * Check for an assignment at "eap->cmd", compile it if found.
6528 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6529 */
6530 static int
6531may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6532{
6533 char_u *pskip;
6534 char_u *p;
6535
6536 // Assuming the command starts with a variable or function name,
6537 // find what follows.
6538 // Skip over "var.member", "var[idx]" and the like.
6539 // Also "&opt = val", "$ENV = val" and "@r = val".
6540 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6541 ? eap->cmd + 1 : eap->cmd;
6542 p = to_name_end(pskip, TRUE);
6543 if (p > eap->cmd && *p != NUL)
6544 {
6545 char_u *var_end;
6546 int oplen;
6547 int heredoc;
6548
6549 if (eap->cmd[0] == '@')
6550 var_end = eap->cmd + 2;
6551 else
6552 var_end = find_name_end(pskip, NULL, NULL,
6553 FNE_CHECK_START | FNE_INCL_BR);
6554 oplen = assignment_len(skipwhite(var_end), &heredoc);
6555 if (oplen > 0)
6556 {
6557 size_t len = p - eap->cmd;
6558
6559 // Recognize an assignment if we recognize the variable
6560 // name:
6561 // "g:var = expr"
6562 // "local = expr" where "local" is a local var.
6563 // "script = expr" where "script" is a script-local var.
6564 // "import = expr" where "import" is an imported var
6565 // "&opt = expr"
6566 // "$ENV = expr"
6567 // "@r = expr"
6568 if (*eap->cmd == '&'
6569 || *eap->cmd == '$'
6570 || *eap->cmd == '@'
6571 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01006572 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01006573 {
6574 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6575 if (*line == NULL || *line == eap->cmd)
6576 return FAIL;
6577 return OK;
6578 }
6579 }
6580 }
6581
6582 if (*eap->cmd == '[')
6583 {
6584 // [var, var] = expr
6585 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6586 if (*line == NULL)
6587 return FAIL;
6588 if (*line != eap->cmd)
6589 return OK;
6590 }
6591 return NOTDONE;
6592}
6593
6594/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006595 * Check if "name" can be "unlet".
6596 */
6597 int
6598check_vim9_unlet(char_u *name)
6599{
6600 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6601 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006602 // "unlet s:var" is allowed in legacy script.
6603 if (*name == 's' && !script_is_vim9())
6604 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006605 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006606 return FAIL;
6607 }
6608 return OK;
6609}
6610
6611/*
6612 * Callback passed to ex_unletlock().
6613 */
6614 static int
6615compile_unlet(
6616 lval_T *lvp,
6617 char_u *name_end,
6618 exarg_T *eap,
6619 int deep UNUSED,
6620 void *coookie)
6621{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006622 cctx_T *cctx = coookie;
6623 char_u *p = lvp->ll_name;
6624 int cc = *name_end;
6625 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006626
Bram Moolenaar752fc692021-01-04 21:57:11 +01006627 if (cctx->ctx_skip == SKIP_YES)
6628 return OK;
6629
6630 *name_end = NUL;
6631 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006632 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006633 // :unlet $ENV_VAR
6634 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6635 }
6636 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6637 {
6638 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006639
Bram Moolenaar752fc692021-01-04 21:57:11 +01006640 // This is similar to assigning: lookup the list/dict, compile the
6641 // idx/key. Then instead of storing the value unlet the item.
6642 // unlet {list}[idx]
6643 // unlet {dict}[key] dict.key
6644 //
6645 // Figure out the LHS type and other properties.
6646 //
6647 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6648
6649 // : unlet an indexed item
6650 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006651 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006652 iemsg("called compile_lhs() without an index");
6653 ret = FAIL;
6654 }
6655 else
6656 {
6657 // Use the info in "lhs" to unlet the item at the index in the
6658 // list or dict.
6659 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006660 }
6661
Bram Moolenaar752fc692021-01-04 21:57:11 +01006662 vim_free(lhs.lhs_name);
6663 }
6664 else if (check_vim9_unlet(p) == FAIL)
6665 {
6666 ret = FAIL;
6667 }
6668 else
6669 {
6670 // Normal name. Only supports g:, w:, t: and b: namespaces.
6671 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006672 }
6673
Bram Moolenaar752fc692021-01-04 21:57:11 +01006674 *name_end = cc;
6675 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006676}
6677
6678/*
6679 * compile "unlet var", "lock var" and "unlock var"
6680 * "arg" points to "var".
6681 */
6682 static char_u *
6683compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6684{
6685 char_u *p = arg;
6686
6687 if (eap->cmdidx != CMD_unlet)
6688 {
6689 emsg("Sorry, :lock and unlock not implemented yet");
6690 return NULL;
6691 }
6692
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006693 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6694 compile_unlet, cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006695 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6696}
6697
6698/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006699 * Compile an :import command.
6700 */
6701 static char_u *
6702compile_import(char_u *arg, cctx_T *cctx)
6703{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006704 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006705}
6706
6707/*
6708 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6709 */
6710 static int
6711compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6712{
6713 garray_T *instr = &cctx->ctx_instr;
6714 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6715
6716 if (endlabel == NULL)
6717 return FAIL;
6718 endlabel->el_next = *el;
6719 *el = endlabel;
6720 endlabel->el_end_label = instr->ga_len;
6721
6722 generate_JUMP(cctx, when, 0);
6723 return OK;
6724}
6725
6726 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006727compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006728{
6729 garray_T *instr = &cctx->ctx_instr;
6730
6731 while (*el != NULL)
6732 {
6733 endlabel_T *cur = (*el);
6734 isn_T *isn;
6735
6736 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006737 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006738 *el = cur->el_next;
6739 vim_free(cur);
6740 }
6741}
6742
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006743 static void
6744compile_free_jump_to_end(endlabel_T **el)
6745{
6746 while (*el != NULL)
6747 {
6748 endlabel_T *cur = (*el);
6749
6750 *el = cur->el_next;
6751 vim_free(cur);
6752 }
6753}
6754
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006755/*
6756 * Create a new scope and set up the generic items.
6757 */
6758 static scope_T *
6759new_scope(cctx_T *cctx, scopetype_T type)
6760{
6761 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6762
6763 if (scope == NULL)
6764 return NULL;
6765 scope->se_outer = cctx->ctx_scope;
6766 cctx->ctx_scope = scope;
6767 scope->se_type = type;
6768 scope->se_local_count = cctx->ctx_locals.ga_len;
6769 return scope;
6770}
6771
6772/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006773 * Free the current scope and go back to the outer scope.
6774 */
6775 static void
6776drop_scope(cctx_T *cctx)
6777{
6778 scope_T *scope = cctx->ctx_scope;
6779
6780 if (scope == NULL)
6781 {
6782 iemsg("calling drop_scope() without a scope");
6783 return;
6784 }
6785 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006786 switch (scope->se_type)
6787 {
6788 case IF_SCOPE:
6789 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6790 case FOR_SCOPE:
6791 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6792 case WHILE_SCOPE:
6793 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6794 case TRY_SCOPE:
6795 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6796 case NO_SCOPE:
6797 case BLOCK_SCOPE:
6798 break;
6799 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006800 vim_free(scope);
6801}
6802
6803/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006804 * compile "if expr"
6805 *
6806 * "if expr" Produces instructions:
6807 * EVAL expr Push result of "expr"
6808 * JUMP_IF_FALSE end
6809 * ... body ...
6810 * end:
6811 *
6812 * "if expr | else" Produces instructions:
6813 * EVAL expr Push result of "expr"
6814 * JUMP_IF_FALSE else
6815 * ... body ...
6816 * JUMP_ALWAYS end
6817 * else:
6818 * ... body ...
6819 * end:
6820 *
6821 * "if expr1 | elseif expr2 | else" Produces instructions:
6822 * EVAL expr Push result of "expr"
6823 * JUMP_IF_FALSE elseif
6824 * ... body ...
6825 * JUMP_ALWAYS end
6826 * elseif:
6827 * EVAL expr Push result of "expr"
6828 * JUMP_IF_FALSE else
6829 * ... body ...
6830 * JUMP_ALWAYS end
6831 * else:
6832 * ... body ...
6833 * end:
6834 */
6835 static char_u *
6836compile_if(char_u *arg, cctx_T *cctx)
6837{
6838 char_u *p = arg;
6839 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006840 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006841 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006842 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006843 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006844
Bram Moolenaara5565e42020-05-09 15:44:01 +02006845 CLEAR_FIELD(ppconst);
6846 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006847 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006848 clear_ppconst(&ppconst);
6849 return NULL;
6850 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01006851 if (!ends_excmd2(arg, skipwhite(p)))
6852 {
6853 semsg(_(e_trailing_arg), p);
6854 return NULL;
6855 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006856 if (cctx->ctx_skip == SKIP_YES)
6857 clear_ppconst(&ppconst);
6858 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006859 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006860 int error = FALSE;
6861 int v;
6862
Bram Moolenaara5565e42020-05-09 15:44:01 +02006863 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006864 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006865 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006866 if (error)
6867 return NULL;
6868 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006869 }
6870 else
6871 {
6872 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006873 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006874 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006875 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006876 if (bool_on_stack(cctx) == FAIL)
6877 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006878 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006879
6880 scope = new_scope(cctx, IF_SCOPE);
6881 if (scope == NULL)
6882 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006883 scope->se_skip_save = skip_save;
6884 // "is_had_return" will be reset if any block does not end in :return
6885 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006886
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006887 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006888 {
6889 // "where" is set when ":elseif", "else" or ":endif" is found
6890 scope->se_u.se_if.is_if_label = instr->ga_len;
6891 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6892 }
6893 else
6894 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006895
Bram Moolenaarced68a02021-01-24 17:53:47 +01006896#ifdef FEAT_PROFILE
6897 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
6898 && skip_save != SKIP_YES)
6899 {
6900 // generated a profile start, need to generate a profile end, since it
6901 // won't be done after returning
6902 cctx->ctx_skip = SKIP_NOT;
6903 generate_instr(cctx, ISN_PROF_END);
6904 cctx->ctx_skip = SKIP_YES;
6905 }
6906#endif
6907
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006908 return p;
6909}
6910
6911 static char_u *
6912compile_elseif(char_u *arg, cctx_T *cctx)
6913{
6914 char_u *p = arg;
6915 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006916 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006917 isn_T *isn;
6918 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006919 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006920 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006921
6922 if (scope == NULL || scope->se_type != IF_SCOPE)
6923 {
6924 emsg(_(e_elseif_without_if));
6925 return NULL;
6926 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006927 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006928 if (!cctx->ctx_had_return)
6929 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006930
Bram Moolenaarced68a02021-01-24 17:53:47 +01006931 if (cctx->ctx_skip == SKIP_NOT)
6932 {
6933 // previous block was executed, this one and following will not
6934 cctx->ctx_skip = SKIP_YES;
6935 scope->se_u.se_if.is_seen_skip_not = TRUE;
6936 }
6937 if (scope->se_u.se_if.is_seen_skip_not)
6938 {
6939 // A previous block was executed, skip over expression and bail out.
6940 // Do not count the "elseif" for profiling.
6941#ifdef FEAT_PROFILE
6942 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
6943 .isn_type == ISN_PROF_START)
6944 --instr->ga_len;
6945#endif
6946 skip_expr_cctx(&p, cctx);
6947 return p;
6948 }
6949
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006950 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006951 {
6952 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006953 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006954 return NULL;
6955 // previous "if" or "elseif" jumps here
6956 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6957 isn->isn_arg.jump.jump_where = instr->ga_len;
6958 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006959
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006960 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006961 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006962 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01006963 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02006964 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01006965#ifdef FEAT_PROFILE
6966 if (cctx->ctx_profiling)
6967 {
6968 // the previous block was skipped, need to profile this line
6969 generate_instr(cctx, ISN_PROF_START);
6970 instr_count = instr->ga_len;
6971 }
6972#endif
6973 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02006974 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006975 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006976 clear_ppconst(&ppconst);
6977 return NULL;
6978 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006979 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01006980 if (!ends_excmd2(arg, skipwhite(p)))
6981 {
6982 semsg(_(e_trailing_arg), p);
6983 return NULL;
6984 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006985 if (scope->se_skip_save == SKIP_YES)
6986 clear_ppconst(&ppconst);
6987 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006988 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006989 int error = FALSE;
6990 int v;
6991
Bram Moolenaar7f141552020-05-09 17:35:53 +02006992 // The expression results in a constant.
6993 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006994 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6995 if (error)
6996 return NULL;
6997 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006998 clear_ppconst(&ppconst);
6999 scope->se_u.se_if.is_if_label = -1;
7000 }
7001 else
7002 {
7003 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007004 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007005 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007006 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007007 if (bool_on_stack(cctx) == FAIL)
7008 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007009
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007010 // "where" is set when ":elseif", "else" or ":endif" is found
7011 scope->se_u.se_if.is_if_label = instr->ga_len;
7012 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7013 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007014
7015 return p;
7016}
7017
7018 static char_u *
7019compile_else(char_u *arg, cctx_T *cctx)
7020{
7021 char_u *p = arg;
7022 garray_T *instr = &cctx->ctx_instr;
7023 isn_T *isn;
7024 scope_T *scope = cctx->ctx_scope;
7025
7026 if (scope == NULL || scope->se_type != IF_SCOPE)
7027 {
7028 emsg(_(e_else_without_if));
7029 return NULL;
7030 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007031 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007032 if (!cctx->ctx_had_return)
7033 scope->se_u.se_if.is_had_return = FALSE;
7034 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007035
Bram Moolenaarced68a02021-01-24 17:53:47 +01007036#ifdef FEAT_PROFILE
7037 if (cctx->ctx_profiling)
7038 {
7039 if (cctx->ctx_skip == SKIP_NOT
7040 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7041 .isn_type == ISN_PROF_START)
7042 // the previous block was executed, do not count "else" for profiling
7043 --instr->ga_len;
7044 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7045 {
7046 // the previous block was not executed, this one will, do count the
7047 // "else" for profiling
7048 cctx->ctx_skip = SKIP_NOT;
7049 generate_instr(cctx, ISN_PROF_END);
7050 generate_instr(cctx, ISN_PROF_START);
7051 cctx->ctx_skip = SKIP_YES;
7052 }
7053 }
7054#endif
7055
7056 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007057 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007058 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007059 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007060 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007061 if (!cctx->ctx_had_return
7062 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7063 JUMP_ALWAYS, cctx) == FAIL)
7064 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007065 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007066
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007067 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007068 {
7069 if (scope->se_u.se_if.is_if_label >= 0)
7070 {
7071 // previous "if" or "elseif" jumps here
7072 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7073 isn->isn_arg.jump.jump_where = instr->ga_len;
7074 scope->se_u.se_if.is_if_label = -1;
7075 }
7076 }
7077
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007078 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007079 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7080 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007081
7082 return p;
7083}
7084
7085 static char_u *
7086compile_endif(char_u *arg, cctx_T *cctx)
7087{
7088 scope_T *scope = cctx->ctx_scope;
7089 ifscope_T *ifscope;
7090 garray_T *instr = &cctx->ctx_instr;
7091 isn_T *isn;
7092
7093 if (scope == NULL || scope->se_type != IF_SCOPE)
7094 {
7095 emsg(_(e_endif_without_if));
7096 return NULL;
7097 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007098 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007099 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007100 if (!cctx->ctx_had_return)
7101 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007102
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007103 if (scope->se_u.se_if.is_if_label >= 0)
7104 {
7105 // previous "if" or "elseif" jumps here
7106 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7107 isn->isn_arg.jump.jump_where = instr->ga_len;
7108 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007109 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007110 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007111
7112#ifdef FEAT_PROFILE
7113 // even when skipping we count the endif as executed, unless the block it's
7114 // in is skipped
7115 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7116 && scope->se_skip_save != SKIP_YES)
7117 {
7118 cctx->ctx_skip = SKIP_NOT;
7119 generate_instr(cctx, ISN_PROF_START);
7120 }
7121#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007122 cctx->ctx_skip = scope->se_skip_save;
7123
7124 // If all the blocks end in :return and there is an :else then the
7125 // had_return flag is set.
7126 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007127
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007128 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007129 return arg;
7130}
7131
7132/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007133 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007134 *
7135 * Produces instructions:
7136 * PUSHNR -1
7137 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007138 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007139 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7140 * - if beyond end, jump to "end"
7141 * - otherwise get item from list and push it
7142 * STORE var Store item in "var"
7143 * ... body ...
7144 * JUMP top Jump back to repeat
7145 * end: DROP Drop the result of "expr"
7146 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007147 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7148 * UNPACK 2 Split item in 2
7149 * STORE var1 Store item in "var1"
7150 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007151 */
7152 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007153compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007154{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007155 char_u *arg;
7156 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007157 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007158 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007159 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007160 int var_count = 0;
7161 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007162 size_t varlen;
7163 garray_T *instr = &cctx->ctx_instr;
7164 garray_T *stack = &cctx->ctx_type_stack;
7165 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007166 lvar_T *loop_lvar; // loop iteration variable
7167 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007168 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007169 type_T *item_type = &t_any;
7170 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007171
Bram Moolenaar792f7862020-11-23 08:31:18 +01007172 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007173 if (p == NULL)
7174 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007175 if (var_count == 0)
7176 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007177
7178 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007179 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007180 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7181 return NULL;
7182 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007183 {
7184 emsg(_(e_missing_in));
7185 return NULL;
7186 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007187 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007188 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7189 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007190
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007191 scope = new_scope(cctx, FOR_SCOPE);
7192 if (scope == NULL)
7193 return NULL;
7194
Bram Moolenaar792f7862020-11-23 08:31:18 +01007195 // Reserve a variable to store the loop iteration counter and initialize it
7196 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007197 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7198 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007199 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007200 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007201 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007202 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007203 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007204 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007205
7206 // compile "expr", it remains on the stack until "endfor"
7207 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007208 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007209 {
7210 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007211 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007212 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007213 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007214
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007215 // Now that we know the type of "var", check that it is a list, now or at
7216 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007217 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01007218 if (need_type(vartype, &t_list_any, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007219 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007220 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007221 return NULL;
7222 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007223
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007224 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007225 {
7226 if (var_count == 1)
7227 item_type = vartype->tt_member;
7228 else if (vartype->tt_member->tt_type == VAR_LIST
7229 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
7230 item_type = vartype->tt_member->tt_member;
7231 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007232
7233 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007234 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007235 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007236
Bram Moolenaar792f7862020-11-23 08:31:18 +01007237 arg = arg_start;
7238 if (var_count > 1)
7239 {
7240 generate_UNPACK(cctx, var_count, semicolon);
7241 arg = skipwhite(arg + 1); // skip white after '['
7242
7243 // the list item is replaced by a number of items
7244 if (ga_grow(stack, var_count - 1) == FAIL)
7245 {
7246 drop_scope(cctx);
7247 return NULL;
7248 }
7249 --stack->ga_len;
7250 for (idx = 0; idx < var_count; ++idx)
7251 {
7252 ((type_T **)stack->ga_data)[stack->ga_len] =
7253 (semicolon && idx == 0) ? vartype : item_type;
7254 ++stack->ga_len;
7255 }
7256 }
7257
7258 for (idx = 0; idx < var_count; ++idx)
7259 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007260 assign_dest_T dest = dest_local;
7261 int opt_flags = 0;
7262 int vimvaridx = -1;
7263 type_T *type = &t_any;
7264
7265 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007266 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007267 name = vim_strnsave(arg, varlen);
7268 if (name == NULL)
7269 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007270
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007271 // TODO: script var not supported?
7272 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7273 &vimvaridx, &type, cctx) == FAIL)
7274 goto failed;
7275 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007276 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007277 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7278 0, 0, type, name) == FAIL)
7279 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007280 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007281 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007282 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007283 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007284 {
7285 semsg(_(e_variable_already_declared), arg);
7286 goto failed;
7287 }
7288
Bram Moolenaarea870692020-12-02 14:24:30 +01007289 if (STRNCMP(name, "s:", 2) == 0)
7290 {
7291 semsg(_(e_cannot_declare_script_variable_in_function), name);
7292 goto failed;
7293 }
7294
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007295 // Reserve a variable to store "var".
7296 // TODO: check for type
7297 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
7298 if (var_lvar == NULL)
7299 // out of memory or used as an argument
7300 goto failed;
7301
7302 if (semicolon && idx == var_count - 1)
7303 var_lvar->lv_type = vartype;
7304 else
7305 var_lvar->lv_type = item_type;
7306 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7307 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007308
Bram Moolenaar036d0712021-01-17 20:23:38 +01007309 if (*p == ':')
7310 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007311 if (*p == ',' || *p == ';')
7312 ++p;
7313 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007314 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007315 }
7316
7317 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007318
7319failed:
7320 vim_free(name);
7321 drop_scope(cctx);
7322 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007323}
7324
7325/*
7326 * compile "endfor"
7327 */
7328 static char_u *
7329compile_endfor(char_u *arg, cctx_T *cctx)
7330{
7331 garray_T *instr = &cctx->ctx_instr;
7332 scope_T *scope = cctx->ctx_scope;
7333 forscope_T *forscope;
7334 isn_T *isn;
7335
7336 if (scope == NULL || scope->se_type != FOR_SCOPE)
7337 {
7338 emsg(_(e_for));
7339 return NULL;
7340 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007341 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007342 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007343 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007344
7345 // At end of ":for" scope jump back to the FOR instruction.
7346 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7347
7348 // Fill in the "end" label in the FOR statement so it can jump here
7349 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7350 isn->isn_arg.forloop.for_end = instr->ga_len;
7351
7352 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007353 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007354
7355 // Below the ":for" scope drop the "expr" list from the stack.
7356 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7357 return NULL;
7358
7359 vim_free(scope);
7360
7361 return arg;
7362}
7363
7364/*
7365 * compile "while expr"
7366 *
7367 * Produces instructions:
7368 * top: EVAL expr Push result of "expr"
7369 * JUMP_IF_FALSE end jump if false
7370 * ... body ...
7371 * JUMP top Jump back to repeat
7372 * end:
7373 *
7374 */
7375 static char_u *
7376compile_while(char_u *arg, cctx_T *cctx)
7377{
7378 char_u *p = arg;
7379 garray_T *instr = &cctx->ctx_instr;
7380 scope_T *scope;
7381
7382 scope = new_scope(cctx, WHILE_SCOPE);
7383 if (scope == NULL)
7384 return NULL;
7385
Bram Moolenaarb2049902021-01-24 12:53:53 +01007386 // "endwhile" jumps back here, one before when profiling
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007387 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007388#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007389 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7390 .isn_type == ISN_PROF_START)
7391 --scope->se_u.se_while.ws_top_label;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007392#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007393
7394 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007395 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007396 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007397 if (!ends_excmd2(arg, skipwhite(p)))
7398 {
7399 semsg(_(e_trailing_arg), p);
7400 return NULL;
7401 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007402
Bram Moolenaar13106602020-10-04 16:06:05 +02007403 if (bool_on_stack(cctx) == FAIL)
7404 return FAIL;
7405
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007406 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007407 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007408 JUMP_IF_FALSE, cctx) == FAIL)
7409 return FAIL;
7410
7411 return p;
7412}
7413
7414/*
7415 * compile "endwhile"
7416 */
7417 static char_u *
7418compile_endwhile(char_u *arg, cctx_T *cctx)
7419{
7420 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007421 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007422
7423 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7424 {
7425 emsg(_(e_while));
7426 return NULL;
7427 }
7428 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007429 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007430
Bram Moolenaarf002a412021-01-24 13:34:18 +01007431#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007432 // count the endwhile before jumping
7433 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007434#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007435
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007436 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007437 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007438
7439 // Fill in the "end" label in the WHILE statement so it can jump here.
7440 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007441 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7442 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007443
7444 vim_free(scope);
7445
7446 return arg;
7447}
7448
7449/*
7450 * compile "continue"
7451 */
7452 static char_u *
7453compile_continue(char_u *arg, cctx_T *cctx)
7454{
7455 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007456 int try_scopes = 0;
7457 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007458
7459 for (;;)
7460 {
7461 if (scope == NULL)
7462 {
7463 emsg(_(e_continue));
7464 return NULL;
7465 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01007466 if (scope->se_type == FOR_SCOPE)
7467 {
7468 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007469 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007470 }
7471 if (scope->se_type == WHILE_SCOPE)
7472 {
7473 loop_label = scope->se_u.se_while.ws_top_label;
7474 break;
7475 }
7476 if (scope->se_type == TRY_SCOPE)
7477 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007478 scope = scope->se_outer;
7479 }
7480
Bram Moolenaarc150c092021-02-13 15:02:46 +01007481 if (try_scopes > 0)
7482 // Inside one or more try/catch blocks we first need to jump to the
7483 // "finally" or "endtry" to cleanup.
7484 generate_TRYCONT(cctx, try_scopes, loop_label);
7485 else
7486 // Jump back to the FOR or WHILE instruction.
7487 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
7488
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007489 return arg;
7490}
7491
7492/*
7493 * compile "break"
7494 */
7495 static char_u *
7496compile_break(char_u *arg, cctx_T *cctx)
7497{
7498 scope_T *scope = cctx->ctx_scope;
7499 endlabel_T **el;
7500
7501 for (;;)
7502 {
7503 if (scope == NULL)
7504 {
7505 emsg(_(e_break));
7506 return NULL;
7507 }
7508 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7509 break;
7510 scope = scope->se_outer;
7511 }
7512
7513 // Jump to the end of the FOR or WHILE loop.
7514 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007515 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007516 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007517 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007518 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7519 return FAIL;
7520
7521 return arg;
7522}
7523
7524/*
7525 * compile "{" start of block
7526 */
7527 static char_u *
7528compile_block(char_u *arg, cctx_T *cctx)
7529{
7530 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7531 return NULL;
7532 return skipwhite(arg + 1);
7533}
7534
7535/*
7536 * compile end of block: drop one scope
7537 */
7538 static void
7539compile_endblock(cctx_T *cctx)
7540{
7541 scope_T *scope = cctx->ctx_scope;
7542
7543 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007544 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007545 vim_free(scope);
7546}
7547
7548/*
7549 * compile "try"
7550 * Creates a new scope for the try-endtry, pointing to the first catch and
7551 * finally.
7552 * Creates another scope for the "try" block itself.
7553 * TRY instruction sets up exception handling at runtime.
7554 *
7555 * "try"
7556 * TRY -> catch1, -> finally push trystack entry
7557 * ... try block
7558 * "throw {exception}"
7559 * EVAL {exception}
7560 * THROW create exception
7561 * ... try block
7562 * " catch {expr}"
7563 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007564 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007565 * EVAL {expr}
7566 * MATCH
7567 * JUMP nomatch -> catch2
7568 * CATCH remove exception
7569 * ... catch block
7570 * " catch"
7571 * JUMP -> finally
7572 * catch2: CATCH remove exception
7573 * ... catch block
7574 * " finally"
7575 * finally:
7576 * ... finally block
7577 * " endtry"
7578 * ENDTRY pop trystack entry, may rethrow
7579 */
7580 static char_u *
7581compile_try(char_u *arg, cctx_T *cctx)
7582{
7583 garray_T *instr = &cctx->ctx_instr;
7584 scope_T *try_scope;
7585 scope_T *scope;
7586
7587 // scope that holds the jumps that go to catch/finally/endtry
7588 try_scope = new_scope(cctx, TRY_SCOPE);
7589 if (try_scope == NULL)
7590 return NULL;
7591
Bram Moolenaar69f70502021-01-01 16:10:46 +01007592 if (cctx->ctx_skip != SKIP_YES)
7593 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007594 isn_T *isn;
7595
7596 // "try_catch" is set when the first ":catch" is found or when no catch
7597 // is found and ":finally" is found.
7598 // "try_finally" is set when ":finally" is found
7599 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01007600 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007601 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
7602 return NULL;
7603 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
7604 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007605 return NULL;
7606 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007607
7608 // scope for the try block itself
7609 scope = new_scope(cctx, BLOCK_SCOPE);
7610 if (scope == NULL)
7611 return NULL;
7612
7613 return arg;
7614}
7615
7616/*
7617 * compile "catch {expr}"
7618 */
7619 static char_u *
7620compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7621{
7622 scope_T *scope = cctx->ctx_scope;
7623 garray_T *instr = &cctx->ctx_instr;
7624 char_u *p;
7625 isn_T *isn;
7626
7627 // end block scope from :try or :catch
7628 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7629 compile_endblock(cctx);
7630 scope = cctx->ctx_scope;
7631
7632 // Error if not in a :try scope
7633 if (scope == NULL || scope->se_type != TRY_SCOPE)
7634 {
7635 emsg(_(e_catch));
7636 return NULL;
7637 }
7638
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007639 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007640 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007641 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007642 return NULL;
7643 }
7644
Bram Moolenaar69f70502021-01-01 16:10:46 +01007645 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007646 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007647#ifdef FEAT_PROFILE
7648 // the profile-start should be after the jump
7649 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7650 .isn_type == ISN_PROF_START)
7651 --instr->ga_len;
7652#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01007653 // Jump from end of previous block to :finally or :endtry
7654 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7655 JUMP_ALWAYS, cctx) == FAIL)
7656 return NULL;
7657
7658 // End :try or :catch scope: set value in ISN_TRY instruction
7659 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007660 if (isn->isn_arg.try.try_ref->try_catch == 0)
7661 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007662 if (scope->se_u.se_try.ts_catch_label != 0)
7663 {
7664 // Previous catch without match jumps here
7665 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7666 isn->isn_arg.jump.jump_where = instr->ga_len;
7667 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007668#ifdef FEAT_PROFILE
7669 if (cctx->ctx_profiling)
7670 {
7671 // a "throw" that jumps here needs to be counted
7672 generate_instr(cctx, ISN_PROF_END);
7673 // the "catch" is also counted
7674 generate_instr(cctx, ISN_PROF_START);
7675 }
7676#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007677 }
7678
7679 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007680 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007681 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007682 scope->se_u.se_try.ts_caught_all = TRUE;
7683 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007684 }
7685 else
7686 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007687 char_u *end;
7688 char_u *pat;
7689 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007690 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007691 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007692
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007693 // Push v:exception, push {expr} and MATCH
7694 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7695
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007696 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007697 if (*end != *p)
7698 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007699 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007700 vim_free(tofree);
7701 return FAIL;
7702 }
7703 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007704 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007705 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007706 len = (int)(end - tofree);
7707 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007708 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007709 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007710 if (pat == NULL)
7711 return FAIL;
7712 if (generate_PUSHS(cctx, pat) == FAIL)
7713 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007714
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007715 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7716 return NULL;
7717
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007718 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007719 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7720 return NULL;
7721 }
7722
Bram Moolenaar69f70502021-01-01 16:10:46 +01007723 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007724 return NULL;
7725
7726 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7727 return NULL;
7728 return p;
7729}
7730
7731 static char_u *
7732compile_finally(char_u *arg, cctx_T *cctx)
7733{
7734 scope_T *scope = cctx->ctx_scope;
7735 garray_T *instr = &cctx->ctx_instr;
7736 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007737 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007738
7739 // end block scope from :try or :catch
7740 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7741 compile_endblock(cctx);
7742 scope = cctx->ctx_scope;
7743
7744 // Error if not in a :try scope
7745 if (scope == NULL || scope->se_type != TRY_SCOPE)
7746 {
7747 emsg(_(e_finally));
7748 return NULL;
7749 }
7750
7751 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007752 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007753 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007754 {
7755 emsg(_(e_finally_dup));
7756 return NULL;
7757 }
7758
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007759 this_instr = instr->ga_len;
7760#ifdef FEAT_PROFILE
7761 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7762 .isn_type == ISN_PROF_START)
7763 // jump to the profile start of the "finally"
7764 --this_instr;
7765#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007766
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007767 // Fill in the "end" label in jumps at the end of the blocks.
7768 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
7769 this_instr, cctx);
7770
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007771 // If there is no :catch then an exception jumps to :finally.
7772 if (isn->isn_arg.try.try_ref->try_catch == 0)
7773 isn->isn_arg.try.try_ref->try_catch = this_instr;
7774 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007775 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007776 {
7777 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007778 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007779 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02007780 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007781 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007782 if (generate_instr(cctx, ISN_FINALLY) == NULL)
7783 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007784
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007785 // TODO: set index in ts_finally_label jumps
7786
7787 return arg;
7788}
7789
7790 static char_u *
7791compile_endtry(char_u *arg, cctx_T *cctx)
7792{
7793 scope_T *scope = cctx->ctx_scope;
7794 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007795 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007796
7797 // end block scope from :catch or :finally
7798 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7799 compile_endblock(cctx);
7800 scope = cctx->ctx_scope;
7801
7802 // Error if not in a :try scope
7803 if (scope == NULL || scope->se_type != TRY_SCOPE)
7804 {
7805 if (scope == NULL)
7806 emsg(_(e_no_endtry));
7807 else if (scope->se_type == WHILE_SCOPE)
7808 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007809 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007810 emsg(_(e_endfor));
7811 else
7812 emsg(_(e_endif));
7813 return NULL;
7814 }
7815
Bram Moolenaarc150c092021-02-13 15:02:46 +01007816 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007817 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007818 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007819 if (try_isn->isn_arg.try.try_ref->try_catch == 0
7820 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007821 {
7822 emsg(_(e_missing_catch_or_finally));
7823 return NULL;
7824 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007825
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007826#ifdef FEAT_PROFILE
7827 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7828 .isn_type == ISN_PROF_START)
7829 // move the profile start after "endtry" so that it's not counted when
7830 // the exception is rethrown.
7831 --instr->ga_len;
7832#endif
7833
Bram Moolenaar69f70502021-01-01 16:10:46 +01007834 // Fill in the "end" label in jumps at the end of the blocks, if not
7835 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007836 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
7837 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007838
Bram Moolenaar69f70502021-01-01 16:10:46 +01007839 if (scope->se_u.se_try.ts_catch_label != 0)
7840 {
7841 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01007842 isn_T *isn = ((isn_T *)instr->ga_data)
7843 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007844 isn->isn_arg.jump.jump_where = instr->ga_len;
7845 }
Bram Moolenaare8593122020-07-18 15:17:02 +02007846 }
7847
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007848 compile_endblock(cctx);
7849
Bram Moolenaar4afa7742021-02-14 16:34:59 +01007850 if (cctx->ctx_skip != SKIP_YES)
7851 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007852 // End :catch or :finally scope: set instruction index in ISN_TRY
7853 // instruction
7854 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01007855 if (cctx->ctx_skip != SKIP_YES
7856 && generate_instr(cctx, ISN_ENDTRY) == NULL)
7857 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007858#ifdef FEAT_PROFILE
7859 if (cctx->ctx_profiling)
7860 generate_instr(cctx, ISN_PROF_START);
7861#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01007862 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007863 return arg;
7864}
7865
7866/*
7867 * compile "throw {expr}"
7868 */
7869 static char_u *
7870compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7871{
7872 char_u *p = skipwhite(arg);
7873
Bram Moolenaara5565e42020-05-09 15:44:01 +02007874 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007875 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01007876 if (cctx->ctx_skip == SKIP_YES)
7877 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007878 if (may_generate_2STRING(-1, cctx) == FAIL)
7879 return NULL;
7880 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7881 return NULL;
7882
7883 return p;
7884}
7885
7886/*
7887 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007888 * compile "echomsg expr"
7889 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007890 * compile "execute expr"
7891 */
7892 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007893compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007894{
7895 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007896 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007897 int count = 0;
7898
7899 for (;;)
7900 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007901 if (ends_excmd2(prev, p))
7902 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007903 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007904 return NULL;
7905 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007906 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007907 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007908 }
7909
Bram Moolenaare4984292020-12-13 14:19:25 +01007910 if (count > 0)
7911 {
7912 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7913 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7914 else if (cmdidx == CMD_execute)
7915 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7916 else if (cmdidx == CMD_echomsg)
7917 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7918 else
7919 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7920 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007921 return p;
7922}
7923
7924/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007925 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01007926 * instruction to compute it and return OK.
7927 * Otherwise return FAIL, the caller must deal with any range.
7928 */
7929 static int
7930compile_variable_range(exarg_T *eap, cctx_T *cctx)
7931{
7932 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7933 char_u *p = skipdigits(eap->cmd);
7934
7935 if (p == range_end)
7936 return FAIL;
7937 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7938}
7939
7940/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007941 * :put r
7942 * :put ={expr}
7943 */
7944 static char_u *
7945compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7946{
7947 char_u *line = arg;
7948 linenr_T lnum;
7949 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007950 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007951
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007952 eap->regname = *line;
7953
7954 if (eap->regname == '=')
7955 {
7956 char_u *p = line + 1;
7957
7958 if (compile_expr0(&p, cctx) == FAIL)
7959 return NULL;
7960 line = p;
7961 }
7962 else if (eap->regname != NUL)
7963 ++line;
7964
Bram Moolenaar08597872020-12-10 19:43:40 +01007965 if (compile_variable_range(eap, cctx) == OK)
7966 {
7967 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
7968 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007969 else
Bram Moolenaar08597872020-12-10 19:43:40 +01007970 {
7971 // Either no range or a number.
7972 // "errormsg" will not be set because the range is ADDR_LINES.
7973 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01007974 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01007975 return NULL;
7976 if (eap->addr_count == 0)
7977 lnum = -1;
7978 else
7979 lnum = eap->line2;
7980 if (above)
7981 --lnum;
7982 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007983
7984 generate_PUT(cctx, eap->regname, lnum);
7985 return line;
7986}
7987
7988/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007989 * A command that is not compiled, execute with legacy code.
7990 */
7991 static char_u *
7992compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7993{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007994 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007995 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007996 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007997
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007998 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007999 goto theend;
8000
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008001 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008002 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008003 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008004 int usefilter = FALSE;
8005
8006 has_expr = argt & (EX_XFILE | EX_EXPAND);
8007
8008 // If the command can be followed by a bar, find the bar and truncate
8009 // it, so that the following command can be compiled.
8010 // The '|' is overwritten with a NUL, it is put back below.
8011 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8012 && *eap->arg == '!')
8013 // :w !filter or :r !filter or :r! filter
8014 usefilter = TRUE;
8015 if ((argt & EX_TRLBAR) && !usefilter)
8016 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008017 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008018 separate_nextcmd(eap);
8019 if (eap->nextcmd != NULL)
8020 nextcmd = eap->nextcmd;
8021 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008022 else if (eap->cmdidx == CMD_wincmd)
8023 {
8024 p = eap->arg;
8025 if (*p != NUL)
8026 ++p;
8027 if (*p == 'g' || *p == Ctrl_G)
8028 ++p;
8029 p = skipwhite(p);
8030 if (*p == '|')
8031 {
8032 *p = NUL;
8033 nextcmd = p + 1;
8034 }
8035 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008036 }
8037
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008038 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8039 {
8040 // expand filename in "syntax include [@group] filename"
8041 has_expr = TRUE;
8042 eap->arg = skipwhite(eap->arg + 7);
8043 if (*eap->arg == '@')
8044 eap->arg = skiptowhite(eap->arg);
8045 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008046
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008047 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8048 && STRLEN(eap->arg) > 4)
8049 {
8050 int delim = *eap->arg;
8051
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008052 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008053 if (*p == delim)
8054 {
8055 eap->arg = p + 1;
8056 has_expr = TRUE;
8057 }
8058 }
8059
Bram Moolenaarecac5912021-01-05 19:23:28 +01008060 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8061 {
8062 // TODO: should only expand when appropriate for the command
8063 eap->arg = skiptowhite(eap->arg);
8064 has_expr = TRUE;
8065 }
8066
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008067 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008068 {
8069 int count = 0;
8070 char_u *start = skipwhite(line);
8071
8072 // :cmd xxx`=expr1`yyy`=expr2`zzz
8073 // PUSHS ":cmd xxx"
8074 // eval expr1
8075 // PUSHS "yyy"
8076 // eval expr2
8077 // PUSHS "zzz"
8078 // EXECCONCAT 5
8079 for (;;)
8080 {
8081 if (p > start)
8082 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008083 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008084 ++count;
8085 }
8086 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008087 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008088 return NULL;
8089 may_generate_2STRING(-1, cctx);
8090 ++count;
8091 p = skipwhite(p);
8092 if (*p != '`')
8093 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008094 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008095 return NULL;
8096 }
8097 start = p + 1;
8098
8099 p = (char_u *)strstr((char *)start, "`=");
8100 if (p == NULL)
8101 {
8102 if (*skipwhite(start) != NUL)
8103 {
8104 generate_PUSHS(cctx, vim_strsave(start));
8105 ++count;
8106 }
8107 break;
8108 }
8109 }
8110 generate_EXECCONCAT(cctx, count);
8111 }
8112 else
8113 generate_EXEC(cctx, line);
8114
8115theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008116 if (*nextcmd != NUL)
8117 {
8118 // the parser expects a pointer to the bar, put it back
8119 --nextcmd;
8120 *nextcmd = '|';
8121 }
8122
8123 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008124}
8125
8126/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008127 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008128 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008129 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008130 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008131add_def_function(ufunc_T *ufunc)
8132{
8133 dfunc_T *dfunc;
8134
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008135 if (def_functions.ga_len == 0)
8136 {
8137 // The first position is not used, so that a zero uf_dfunc_idx means it
8138 // wasn't set.
8139 if (ga_grow(&def_functions, 1) == FAIL)
8140 return FAIL;
8141 ++def_functions.ga_len;
8142 }
8143
Bram Moolenaar09689a02020-05-09 22:50:08 +02008144 // Add the function to "def_functions".
8145 if (ga_grow(&def_functions, 1) == FAIL)
8146 return FAIL;
8147 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8148 CLEAR_POINTER(dfunc);
8149 dfunc->df_idx = def_functions.ga_len;
8150 ufunc->uf_dfunc_idx = dfunc->df_idx;
8151 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008152 dfunc->df_name = vim_strsave(ufunc->uf_name);
8153 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008154 ++def_functions.ga_len;
8155 return OK;
8156}
8157
8158/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008159 * After ex_function() has collected all the function lines: parse and compile
8160 * the lines into instructions.
8161 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008162 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8163 * the return statement (used for lambda). When uf_ret_type is already set
8164 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008165 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008166 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008167 * This can be used recursively through compile_lambda(), which may reallocate
8168 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008169 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008170 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008171 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008172compile_def_function(
8173 ufunc_T *ufunc,
8174 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008175 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008176 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008177{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008178 char_u *line = NULL;
8179 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008180 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008181 cctx_T cctx;
8182 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008183 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008184 int ret = FAIL;
8185 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008186 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008187 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008188 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008189#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008190 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008191#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008192
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008193 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008194 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008195 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008196 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008197 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8198 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008199 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008200 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008201 else
8202 {
8203 if (add_def_function(ufunc) == FAIL)
8204 return FAIL;
8205 new_def_function = TRUE;
8206 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008207
Bram Moolenaar985116a2020-07-12 17:31:09 +02008208 ufunc->uf_def_status = UF_COMPILING;
8209
Bram Moolenaara80faa82020-04-12 19:37:17 +02008210 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008211
Bram Moolenaarf002a412021-01-24 13:34:18 +01008212#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008213 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008214#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008215 cctx.ctx_ufunc = ufunc;
8216 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008217 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008218 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8219 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8220 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8221 cctx.ctx_type_list = &ufunc->uf_type_list;
8222 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8223 instr = &cctx.ctx_instr;
8224
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008225 // Set the context to the function, it may be compiled when called from
8226 // another script. Set the script version to the most modern one.
8227 // The line number will be set in next_line_from_context().
8228 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008229 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8230
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008231 // Make sure error messages are OK.
8232 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8233 if (do_estack_push)
8234 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008235 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008236
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008237 if (ufunc->uf_def_args.ga_len > 0)
8238 {
8239 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008240 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008241 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008242 int i;
8243 char_u *arg;
8244 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008245 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008246
8247 // Produce instructions for the default values of optional arguments.
8248 // Store the instruction index in uf_def_arg_idx[] so that we know
8249 // where to start when the function is called, depending on the number
8250 // of arguments.
8251 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
8252 if (ufunc->uf_def_arg_idx == NULL)
8253 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008254 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008255 for (i = 0; i < count; ++i)
8256 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008257 garray_T *stack = &cctx.ctx_type_stack;
8258 type_T *val_type;
8259 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008260 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008261 int r;
8262
8263 // Make sure later arguments are not found.
8264 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008265
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008266 ufunc->uf_def_arg_idx[i] = instr->ga_len;
8267 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01008268 r = compile_expr0(&arg, &cctx);
8269
8270 ufunc->uf_args.ga_len = uf_args_len;
8271 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008272 goto erret;
8273
8274 // If no type specified use the type of the default value.
8275 // Otherwise check that the default value type matches the
8276 // specified type.
8277 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008278 where.wt_index = arg_idx + 1;
8279 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008280 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008281 {
8282 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008283 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008284 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02008285 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008286 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008287 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008288
8289 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008290 goto erret;
8291 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008292 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008293
8294 if (did_set_arg_type)
8295 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008296 }
8297
8298 /*
8299 * Loop over all the lines of the function and generate instructions.
8300 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008301 for (;;)
8302 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008303 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008304 int starts_with_colon = FALSE;
8305 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02008306 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008307
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008308 // Bail out on the first error to avoid a flood of errors and report
8309 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01008310 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008311 goto erret;
8312
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008313 if (line != NULL && *line == '|')
8314 // the line continues after a '|'
8315 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02008316 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02008317 && !(*line == '#' && (line == cctx.ctx_line_start
8318 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008319 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02008320 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008321 goto erret;
8322 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01008323 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
8324 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008325 else
8326 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02008327 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02008328 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008329 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008330 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01008331#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008332 if (cctx.ctx_skip != SKIP_YES)
8333 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008334#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008335 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01008336 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008337 }
8338
Bram Moolenaara80faa82020-04-12 19:37:17 +02008339 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008340 ea.cmdlinep = &line;
8341 ea.cmd = skipwhite(line);
8342
Bram Moolenaarb2049902021-01-24 12:53:53 +01008343 if (*ea.cmd == '#')
8344 {
8345 // "#" starts a comment
8346 line = (char_u *)"";
8347 continue;
8348 }
8349
Bram Moolenaarf002a412021-01-24 13:34:18 +01008350#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008351 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
8352 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008353 {
8354 may_generate_prof_end(&cctx, prof_lnum);
8355
8356 prof_lnum = cctx.ctx_lnum;
8357 generate_instr(&cctx, ISN_PROF_START);
8358 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01008359#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008360
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008361 // Some things can be recognized by the first character.
8362 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008363 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008364 case '}':
8365 {
8366 // "}" ends a block scope
8367 scopetype_T stype = cctx.ctx_scope == NULL
8368 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008369
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008370 if (stype == BLOCK_SCOPE)
8371 {
8372 compile_endblock(&cctx);
8373 line = ea.cmd;
8374 }
8375 else
8376 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008377 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008378 goto erret;
8379 }
8380 if (line != NULL)
8381 line = skipwhite(ea.cmd + 1);
8382 continue;
8383 }
8384
8385 case '{':
8386 // "{" starts a block scope
8387 // "{'a': 1}->func() is something else
8388 if (ends_excmd(*skipwhite(ea.cmd + 1)))
8389 {
8390 line = compile_block(ea.cmd, &cctx);
8391 continue;
8392 }
8393 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008394 }
8395
8396 /*
8397 * COMMAND MODIFIERS
8398 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02008399 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02008400 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
8401 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008402 {
8403 if (errormsg != NULL)
8404 goto erret;
8405 // empty line or comment
8406 line = (char_u *)"";
8407 continue;
8408 }
Bram Moolenaare1004402020-10-24 20:49:43 +02008409 generate_cmdmods(&cctx, &local_cmdmod);
8410 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008411
Bram Moolenaare88c8e82020-11-01 17:03:37 +01008412 // Check if there was a colon after the last command modifier or before
8413 // the current position.
8414 for (p = ea.cmd; p >= line; --p)
8415 {
8416 if (*p == ':')
8417 starts_with_colon = TRUE;
8418 if (p < ea.cmd && !VIM_ISWHITE(*p))
8419 break;
8420 }
8421
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008422 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008423 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008424 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008425 {
8426 if (*ea.cmd == '(')
8427 // not for "call()"
8428 ea.cmd = p;
8429 else
8430 ea.cmd = skipwhite(ea.cmd);
8431 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008432
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008433 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008434 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008435 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008436
Bram Moolenaar17126b12021-01-07 22:03:02 +01008437 // Check for assignment after command modifiers.
8438 assign = may_compile_assignment(&ea, &line, &cctx);
8439 if (assign == OK)
8440 goto nextline;
8441 if (assign == FAIL)
8442 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008443 }
8444
8445 /*
8446 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008447 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008448 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008449 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008450 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008451 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008452 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008453 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008454 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008455 if (!starts_with_colon)
8456 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008457 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008458 goto erret;
8459 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01008460 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008461 if (ends_excmd2(line, ea.cmd))
8462 {
8463 // A range without a command: jump to the line.
8464 // TODO: compile to a more efficient command, possibly
8465 // calling parse_cmd_address().
8466 ea.cmdidx = CMD_SIZE;
8467 line = compile_exec(line, &ea, &cctx);
8468 goto nextline;
8469 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008470 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008471 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01008472 p = find_ex_command(&ea, NULL, starts_with_colon
8473 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008474
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008475 if (p == NULL)
8476 {
8477 if (cctx.ctx_skip != SKIP_YES)
8478 emsg(_(e_ambiguous_use_of_user_defined_command));
8479 goto erret;
8480 }
8481
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008482 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8483 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008484 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008485 {
8486 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008487 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008488 }
8489
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008490 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008491 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008492 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008493 // CMD_var cannot happen, compile_assignment() above would be
8494 // used. Most likely an assignment to a non-existing variable.
8495 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008496 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008497 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008498 }
8499
Bram Moolenaar3988f642020-08-27 22:43:03 +02008500 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008501 && ea.cmdidx != CMD_elseif
8502 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008503 && ea.cmdidx != CMD_endif
8504 && ea.cmdidx != CMD_endfor
8505 && ea.cmdidx != CMD_endwhile
8506 && ea.cmdidx != CMD_catch
8507 && ea.cmdidx != CMD_finally
8508 && ea.cmdidx != CMD_endtry)
8509 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008510 emsg(_(e_unreachable_code_after_return));
8511 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008512 }
8513
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008514 p = skipwhite(p);
8515 if (ea.cmdidx != CMD_SIZE
8516 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8517 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008518 if (ea.cmdidx >= 0)
8519 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008520 if ((ea.argt & EX_BANG) && *p == '!')
8521 {
8522 ea.forceit = TRUE;
8523 p = skipwhite(p + 1);
8524 }
8525 }
8526
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008527 switch (ea.cmdidx)
8528 {
8529 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008530 ea.arg = p;
8531 line = compile_nested_function(&ea, &cctx);
8532 break;
8533
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008534 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008535 // TODO: should we allow this, e.g. to declare a global
8536 // function?
8537 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008538 goto erret;
8539
8540 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008541 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008542 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008543 break;
8544
8545 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008546 emsg(_(e_cannot_use_let_in_vim9_script));
8547 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008548 case CMD_var:
8549 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008550 case CMD_const:
8551 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008552 if (line == p)
8553 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008554 break;
8555
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008556 case CMD_unlet:
8557 case CMD_unlockvar:
8558 case CMD_lockvar:
8559 line = compile_unletlock(p, &ea, &cctx);
8560 break;
8561
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008562 case CMD_import:
8563 line = compile_import(p, &cctx);
8564 break;
8565
8566 case CMD_if:
8567 line = compile_if(p, &cctx);
8568 break;
8569 case CMD_elseif:
8570 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008571 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008572 break;
8573 case CMD_else:
8574 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008575 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008576 break;
8577 case CMD_endif:
8578 line = compile_endif(p, &cctx);
8579 break;
8580
8581 case CMD_while:
8582 line = compile_while(p, &cctx);
8583 break;
8584 case CMD_endwhile:
8585 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008586 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008587 break;
8588
8589 case CMD_for:
8590 line = compile_for(p, &cctx);
8591 break;
8592 case CMD_endfor:
8593 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008594 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008595 break;
8596 case CMD_continue:
8597 line = compile_continue(p, &cctx);
8598 break;
8599 case CMD_break:
8600 line = compile_break(p, &cctx);
8601 break;
8602
8603 case CMD_try:
8604 line = compile_try(p, &cctx);
8605 break;
8606 case CMD_catch:
8607 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008608 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008609 break;
8610 case CMD_finally:
8611 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008612 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008613 break;
8614 case CMD_endtry:
8615 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008616 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008617 break;
8618 case CMD_throw:
8619 line = compile_throw(p, &cctx);
8620 break;
8621
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008622 case CMD_eval:
8623 if (compile_expr0(&p, &cctx) == FAIL)
8624 goto erret;
8625
Bram Moolenaar3988f642020-08-27 22:43:03 +02008626 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008627 generate_instr_drop(&cctx, ISN_DROP, 1);
8628
8629 line = skipwhite(p);
8630 break;
8631
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008632 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008633 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008634 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008635 case CMD_echomsg:
8636 case CMD_echoerr:
8637 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008638 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008639
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008640 case CMD_put:
8641 ea.cmd = cmd;
8642 line = compile_put(p, &ea, &cctx);
8643 break;
8644
Bram Moolenaar3988f642020-08-27 22:43:03 +02008645 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008646
Bram Moolenaarae616492020-07-28 20:07:27 +02008647 case CMD_append:
8648 case CMD_change:
8649 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01008650 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008651 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008652 case CMD_xit:
8653 not_in_vim9(&ea);
8654 goto erret;
8655
Bram Moolenaar002262f2020-07-08 17:47:57 +02008656 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008657 if (cctx.ctx_skip != SKIP_YES)
8658 {
8659 semsg(_(e_invalid_command_str), ea.cmd);
8660 goto erret;
8661 }
8662 // We don't check for a next command here.
8663 line = (char_u *)"";
8664 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008665
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008666 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008667 if (cctx.ctx_skip == SKIP_YES)
8668 {
8669 // We don't check for a next command here.
8670 line = (char_u *)"";
8671 }
8672 else
8673 {
8674 // Not recognized, execute with do_cmdline_cmd().
8675 ea.arg = p;
8676 line = compile_exec(line, &ea, &cctx);
8677 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008678 break;
8679 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008680nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008681 if (line == NULL)
8682 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008683 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008684
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008685 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008686 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008687
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008688 if (cctx.ctx_type_stack.ga_len < 0)
8689 {
8690 iemsg("Type stack underflow");
8691 goto erret;
8692 }
8693 }
8694
8695 if (cctx.ctx_scope != NULL)
8696 {
8697 if (cctx.ctx_scope->se_type == IF_SCOPE)
8698 emsg(_(e_endif));
8699 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8700 emsg(_(e_endwhile));
8701 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8702 emsg(_(e_endfor));
8703 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008704 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008705 goto erret;
8706 }
8707
Bram Moolenaarefd88552020-06-18 20:50:10 +02008708 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008709 {
8710 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8711 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008712 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008713 goto erret;
8714 }
8715
8716 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008717 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008718 }
8719
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008720 {
8721 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8722 + ufunc->uf_dfunc_idx;
8723 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008724 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008725#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008726 if (cctx.ctx_profiling)
8727 {
8728 dfunc->df_instr_prof = instr->ga_data;
8729 dfunc->df_instr_prof_count = instr->ga_len;
8730 }
8731 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01008732#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008733 {
8734 dfunc->df_instr = instr->ga_data;
8735 dfunc->df_instr_count = instr->ga_len;
8736 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008737 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008738 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008739 if (cctx.ctx_outer_used)
8740 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008741 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008742 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008743
8744 ret = OK;
8745
8746erret:
8747 if (ret == FAIL)
8748 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008749 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008750 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8751 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008752
8753 for (idx = 0; idx < instr->ga_len; ++idx)
8754 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008755 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008756 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008757
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008758 // If using the last entry in the table and it was added above, we
8759 // might as well remove it.
8760 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008761 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008762 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008763 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008764 ufunc->uf_dfunc_idx = 0;
8765 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008766 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008767
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008768 while (cctx.ctx_scope != NULL)
8769 drop_scope(&cctx);
8770
Bram Moolenaar20431c92020-03-20 18:39:46 +01008771 // Don't execute this function body.
8772 ga_clear_strings(&ufunc->uf_lines);
8773
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008774 if (errormsg != NULL)
8775 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008776 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008777 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008778 }
8779
8780 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008781 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008782 if (do_estack_push)
8783 estack_pop();
8784
Bram Moolenaar20431c92020-03-20 18:39:46 +01008785 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008786 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008787 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008788 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008789}
8790
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008791 void
8792set_function_type(ufunc_T *ufunc)
8793{
8794 int varargs = ufunc->uf_va_name != NULL;
8795 int argcount = ufunc->uf_args.ga_len;
8796
8797 // Create a type for the function, with the return type and any
8798 // argument types.
8799 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8800 // The type is included in "tt_args".
8801 if (argcount > 0 || varargs)
8802 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01008803 if (ufunc->uf_type_list.ga_itemsize == 0)
8804 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008805 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8806 argcount, &ufunc->uf_type_list);
8807 // Add argument types to the function type.
8808 if (func_type_add_arg_types(ufunc->uf_func_type,
8809 argcount + varargs,
8810 &ufunc->uf_type_list) == FAIL)
8811 return;
8812 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8813 ufunc->uf_func_type->tt_min_argcount =
8814 argcount - ufunc->uf_def_args.ga_len;
8815 if (ufunc->uf_arg_types == NULL)
8816 {
8817 int i;
8818
8819 // lambda does not have argument types.
8820 for (i = 0; i < argcount; ++i)
8821 ufunc->uf_func_type->tt_args[i] = &t_any;
8822 }
8823 else
8824 mch_memmove(ufunc->uf_func_type->tt_args,
8825 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8826 if (varargs)
8827 {
8828 ufunc->uf_func_type->tt_args[argcount] =
8829 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8830 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8831 }
8832 }
8833 else
8834 // No arguments, can use a predefined type.
8835 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8836 argcount, &ufunc->uf_type_list);
8837}
8838
8839
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008840/*
8841 * Delete an instruction, free what it contains.
8842 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008843 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008844delete_instr(isn_T *isn)
8845{
8846 switch (isn->isn_type)
8847 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008848 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008849 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008850 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008851 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008852 case ISN_LOADENV:
8853 case ISN_LOADG:
8854 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008855 case ISN_LOADT:
8856 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008857 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008858 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008859 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008860 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008861 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008862 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008863 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008864 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008865 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008866 case ISN_STOREW:
8867 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008868 vim_free(isn->isn_arg.string);
8869 break;
8870
8871 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008872 case ISN_STORES:
8873 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008874 break;
8875
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008876 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008877 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008878 vim_free(isn->isn_arg.unlet.ul_name);
8879 break;
8880
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008881 case ISN_STOREOPT:
8882 vim_free(isn->isn_arg.storeopt.so_name);
8883 break;
8884
8885 case ISN_PUSHBLOB: // push blob isn_arg.blob
8886 blob_unref(isn->isn_arg.blob);
8887 break;
8888
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008889 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008890#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008891 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008892#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008893 break;
8894
8895 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008896#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008897 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008898#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008899 break;
8900
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008901 case ISN_UCALL:
8902 vim_free(isn->isn_arg.ufunc.cuf_name);
8903 break;
8904
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008905 case ISN_FUNCREF:
8906 {
8907 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8908 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01008909 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008910
Bram Moolenaar077a4232020-12-22 18:33:27 +01008911 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
8912 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02008913 }
8914 break;
8915
8916 case ISN_DCALL:
8917 {
8918 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8919 + isn->isn_arg.dfunc.cdf_idx;
8920
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008921 if (dfunc->df_ufunc != NULL
8922 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008923 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008924 }
8925 break;
8926
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008927 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008928 {
8929 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8930 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8931
8932 if (ufunc != NULL)
8933 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008934 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02008935 func_ptr_unref(ufunc);
8936 }
8937
8938 vim_free(lambda);
8939 vim_free(isn->isn_arg.newfunc.nf_global);
8940 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008941 break;
8942
Bram Moolenaar5e654232020-09-16 15:22:00 +02008943 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01008944 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02008945 free_type(isn->isn_arg.type.ct_type);
8946 break;
8947
Bram Moolenaar02194d22020-10-24 23:08:38 +02008948 case ISN_CMDMOD:
8949 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8950 ->cmod_filter_regmatch.regprog);
8951 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8952 break;
8953
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008954 case ISN_LOADSCRIPT:
8955 case ISN_STORESCRIPT:
8956 vim_free(isn->isn_arg.script.scriptref);
8957 break;
8958
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008959 case ISN_TRY:
8960 vim_free(isn->isn_arg.try.try_ref);
8961 break;
8962
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008963 case ISN_2BOOL:
8964 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008965 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008966 case ISN_ADDBLOB:
8967 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008968 case ISN_ANYINDEX:
8969 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008970 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008971 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008972 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008973 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008974 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008975 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008976 case ISN_COMPAREANY:
8977 case ISN_COMPAREBLOB:
8978 case ISN_COMPAREBOOL:
8979 case ISN_COMPAREDICT:
8980 case ISN_COMPAREFLOAT:
8981 case ISN_COMPAREFUNC:
8982 case ISN_COMPARELIST:
8983 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008984 case ISN_COMPARESPECIAL:
8985 case ISN_COMPARESTRING:
8986 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008987 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008988 case ISN_DROP:
8989 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008990 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008991 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008992 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008993 case ISN_EXECCONCAT:
8994 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008995 case ISN_FINALLY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008996 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008997 case ISN_GETITEM:
8998 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008999 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02009000 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02009001 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009002 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009003 case ISN_LOADBDICT:
9004 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009005 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009006 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009007 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009008 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009009 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009010 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009011 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009012 case ISN_NEGATENR:
9013 case ISN_NEWDICT:
9014 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009015 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009016 case ISN_OPFLOAT:
9017 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009018 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02009019 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01009020 case ISN_PROF_END:
9021 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009022 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009023 case ISN_PUSHF:
9024 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009025 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009026 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009027 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01009028 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009029 case ISN_SHUFFLE:
9030 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009031 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01009032 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009033 case ISN_STORENR:
9034 case ISN_STOREOUTER:
9035 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009036 case ISN_STOREV:
9037 case ISN_STRINDEX:
9038 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009039 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +01009040 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +01009041 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01009042 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +01009043 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009044 // nothing allocated
9045 break;
9046 }
9047}
9048
9049/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009050 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01009051 */
9052 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009053delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009054{
9055 int idx;
9056
9057 ga_clear(&dfunc->df_def_args_isn);
9058
9059 if (dfunc->df_instr != NULL)
9060 {
9061 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
9062 delete_instr(dfunc->df_instr + idx);
9063 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009064 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009065 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01009066#ifdef FEAT_PROFILE
9067 if (dfunc->df_instr_prof != NULL)
9068 {
9069 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
9070 delete_instr(dfunc->df_instr_prof + idx);
9071 VIM_CLEAR(dfunc->df_instr_prof);
9072 dfunc->df_instr_prof = NULL;
9073 }
9074#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01009075
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009076 if (mark_deleted)
9077 dfunc->df_deleted = TRUE;
9078 if (dfunc->df_ufunc != NULL)
9079 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009080}
9081
9082/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009083 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009084 * function, unless another user function still uses it.
9085 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009086 */
9087 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009088unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009089{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009090 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009091 {
9092 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9093 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009094
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009095 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009096 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009097 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009098 ufunc->uf_dfunc_idx = 0;
9099 if (dfunc->df_ufunc == ufunc)
9100 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009101 }
9102}
9103
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009104/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009105 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009106 */
9107 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009108link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009109{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009110 if (ufunc->uf_dfunc_idx > 0)
9111 {
9112 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9113 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009114
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009115 ++dfunc->df_refcount;
9116 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009117}
9118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009119#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009120/*
9121 * Free all functions defined with ":def".
9122 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009123 void
9124free_def_functions(void)
9125{
Bram Moolenaar20431c92020-03-20 18:39:46 +01009126 int idx;
9127
9128 for (idx = 0; idx < def_functions.ga_len; ++idx)
9129 {
9130 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
9131
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009132 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009133 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009134 }
9135
9136 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009137}
9138#endif
9139
9140
9141#endif // FEAT_EVAL