blob: 1bb30ca0cf1dfbb7cd514e62aa95c0b630f4c0e8 [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
394item_exists(char_u *name, size_t len, cctx_T *cctx)
395{
396 int is_global;
397
398 if (variable_exists(name, len, cctx))
399 return TRUE;
400
401 // Find a function, so that a following "->" works. Skip "g:" before a
402 // function name.
403 // Do not check for an internal function, since it might also be a
404 // valid command, such as ":split" versuse "split()".
405 is_global = (name[0] == 'g' && name[1] == ':');
406 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
407}
408
409/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100410 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200411 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200412 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100413 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100414 * Return FAIL and give an error if it defined.
415 */
416 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100417check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100418{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200419 int c = p[len];
420 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200421
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100422 if (script_var_exists(p, len, FALSE, cctx) == OK)
423 {
424 if (is_arg)
425 semsg(_(e_argument_already_declared_in_script_str), p);
426 else
427 semsg(_(e_variable_already_declared_in_script_str), p);
428 return FAIL;
429 }
430
Bram Moolenaarad486a02020-08-01 23:22:18 +0200431 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100432 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100433 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200434 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200435 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200436 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100437 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200438 // A local or script-local function can shadow a global function.
439 if (ufunc == NULL || !func_is_global(ufunc)
440 || (p[0] == 'g' && p[1] == ':'))
441 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100442 if (is_arg)
443 semsg(_(e_argument_name_shadows_existing_variable_str), p);
444 else
445 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200446 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200447 return FAIL;
448 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100449 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200450 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100451 return OK;
452}
453
Bram Moolenaar65b95452020-07-19 14:03:09 +0200454
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100455/////////////////////////////////////////////////////////////////////
456// Following generate_ functions expect the caller to call ga_grow().
457
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200458#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
459#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100460
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100461/*
462 * Generate an instruction without arguments.
463 * Returns a pointer to the new instruction, NULL if failed.
464 */
465 static isn_T *
466generate_instr(cctx_T *cctx, isntype_T isn_type)
467{
468 garray_T *instr = &cctx->ctx_instr;
469 isn_T *isn;
470
Bram Moolenaar080457c2020-03-03 21:53:32 +0100471 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100472 if (ga_grow(instr, 1) == FAIL)
473 return NULL;
474 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
475 isn->isn_type = isn_type;
476 isn->isn_lnum = cctx->ctx_lnum + 1;
477 ++instr->ga_len;
478
479 return isn;
480}
481
482/*
483 * Generate an instruction without arguments.
484 * "drop" will be removed from the stack.
485 * Returns a pointer to the new instruction, NULL if failed.
486 */
487 static isn_T *
488generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
489{
490 garray_T *stack = &cctx->ctx_type_stack;
491
Bram Moolenaar080457c2020-03-03 21:53:32 +0100492 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100493 stack->ga_len -= drop;
494 return generate_instr(cctx, isn_type);
495}
496
497/*
498 * Generate instruction "isn_type" and put "type" on the type stack.
499 */
500 static isn_T *
501generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
502{
503 isn_T *isn;
504 garray_T *stack = &cctx->ctx_type_stack;
505
506 if ((isn = generate_instr(cctx, isn_type)) == NULL)
507 return NULL;
508
509 if (ga_grow(stack, 1) == FAIL)
510 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200511 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100512 ++stack->ga_len;
513
514 return isn;
515}
516
517/*
518 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200519 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100520 */
521 static int
522may_generate_2STRING(int offset, cctx_T *cctx)
523{
524 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200525 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100526 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100527 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100528
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100529 RETURN_OK_IF_SKIP(cctx);
530 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200531 switch ((*type)->tt_type)
532 {
533 // nothing to be done
534 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200536 // conversion possible
537 case VAR_SPECIAL:
538 case VAR_BOOL:
539 case VAR_NUMBER:
540 case VAR_FLOAT:
541 break;
542
543 // conversion possible (with runtime check)
544 case VAR_ANY:
545 case VAR_UNKNOWN:
546 isntype = ISN_2STRING_ANY;
547 break;
548
549 // conversion not possible
550 case VAR_VOID:
551 case VAR_BLOB:
552 case VAR_FUNC:
553 case VAR_PARTIAL:
554 case VAR_LIST:
555 case VAR_DICT:
556 case VAR_JOB:
557 case VAR_CHANNEL:
558 to_string_error((*type)->tt_type);
559 return FAIL;
560 }
561
562 *type = &t_string;
563 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100564 return FAIL;
565 isn->isn_arg.number = offset;
566
567 return OK;
568}
569
570 static int
571check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
572{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200573 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200575 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100576 {
577 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200578 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100579 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200580 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100581 return FAIL;
582 }
583 return OK;
584}
585
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200586 static int
587generate_add_instr(
588 cctx_T *cctx,
589 vartype_T vartype,
590 type_T *type1,
591 type_T *type2)
592{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100593 garray_T *stack = &cctx->ctx_type_stack;
594 isn_T *isn = generate_instr_drop(cctx,
595 vartype == VAR_NUMBER ? ISN_OPNR
596 : vartype == VAR_LIST ? ISN_ADDLIST
597 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200598#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100599 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200600#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100601 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200602
603 if (vartype != VAR_LIST && vartype != VAR_BLOB
604 && type1->tt_type != VAR_ANY
605 && type2->tt_type != VAR_ANY
606 && check_number_or_float(
607 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
608 return FAIL;
609
610 if (isn != NULL)
611 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100612
613 // When concatenating two lists with different member types the member type
614 // becomes "any".
615 if (vartype == VAR_LIST
616 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
617 && type1->tt_member != type2->tt_member)
618 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
619
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200620 return isn == NULL ? FAIL : OK;
621}
622
623/*
624 * Get the type to use for an instruction for an operation on "type1" and
625 * "type2". If they are matching use a type-specific instruction. Otherwise
626 * fall back to runtime type checking.
627 */
628 static vartype_T
629operator_type(type_T *type1, type_T *type2)
630{
631 if (type1->tt_type == type2->tt_type
632 && (type1->tt_type == VAR_NUMBER
633 || type1->tt_type == VAR_LIST
634#ifdef FEAT_FLOAT
635 || type1->tt_type == VAR_FLOAT
636#endif
637 || type1->tt_type == VAR_BLOB))
638 return type1->tt_type;
639 return VAR_ANY;
640}
641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100642/*
643 * Generate an instruction with two arguments. The instruction depends on the
644 * type of the arguments.
645 */
646 static int
647generate_two_op(cctx_T *cctx, char_u *op)
648{
649 garray_T *stack = &cctx->ctx_type_stack;
650 type_T *type1;
651 type_T *type2;
652 vartype_T vartype;
653 isn_T *isn;
654
Bram Moolenaar080457c2020-03-03 21:53:32 +0100655 RETURN_OK_IF_SKIP(cctx);
656
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200657 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100658 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
659 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200660 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100661
662 switch (*op)
663 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200664 case '+':
665 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100666 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100667 break;
668
669 case '-':
670 case '*':
671 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
672 op) == FAIL)
673 return FAIL;
674 if (vartype == VAR_NUMBER)
675 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
676#ifdef FEAT_FLOAT
677 else if (vartype == VAR_FLOAT)
678 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
679#endif
680 else
681 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
682 if (isn != NULL)
683 isn->isn_arg.op.op_type = *op == '*'
684 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
685 break;
686
Bram Moolenaar4c683752020-04-05 21:38:23 +0200687 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100688 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200689 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100690 && type2->tt_type != VAR_NUMBER))
691 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200692 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100693 return FAIL;
694 }
695 isn = generate_instr_drop(cctx,
696 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
697 if (isn != NULL)
698 isn->isn_arg.op.op_type = EXPR_REM;
699 break;
700 }
701
702 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200703 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100704 {
705 type_T *type = &t_any;
706
707#ifdef FEAT_FLOAT
708 // float+number and number+float results in float
709 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
710 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
711 type = &t_float;
712#endif
713 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
714 }
715
716 return OK;
717}
718
719/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200720 * Get the instruction to use for comparing "type1" with "type2"
721 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200723 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100724get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100725{
726 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100727
Bram Moolenaar4c683752020-04-05 21:38:23 +0200728 if (type1 == VAR_UNKNOWN)
729 type1 = VAR_ANY;
730 if (type2 == VAR_UNKNOWN)
731 type2 = VAR_ANY;
732
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100733 if (type1 == type2)
734 {
735 switch (type1)
736 {
737 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
738 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
739 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
740 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
741 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
742 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
743 case VAR_LIST: isntype = ISN_COMPARELIST; break;
744 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
745 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100746 default: isntype = ISN_COMPAREANY; break;
747 }
748 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200749 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100750 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100751 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100752 isntype = ISN_COMPAREANY;
753
Bram Moolenaar657137c2021-01-09 15:45:23 +0100754 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100755 && (isntype == ISN_COMPAREBOOL
756 || isntype == ISN_COMPARESPECIAL
757 || isntype == ISN_COMPARENR
758 || isntype == ISN_COMPAREFLOAT))
759 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200760 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100761 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200762 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100763 }
764 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100765 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100766 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
767 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100768 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
769 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770 && (type1 == VAR_BLOB || type2 == VAR_BLOB
771 || type1 == VAR_LIST || type2 == VAR_LIST))))
772 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200773 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100774 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200775 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200777 return isntype;
778}
779
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200780 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100781check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200782{
783 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
784 return FAIL;
785 return OK;
786}
787
Bram Moolenaara5565e42020-05-09 15:44:01 +0200788/*
789 * Generate an ISN_COMPARE* instruction with a boolean result.
790 */
791 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100792generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200793{
794 isntype_T isntype;
795 isn_T *isn;
796 garray_T *stack = &cctx->ctx_type_stack;
797 vartype_T type1;
798 vartype_T type2;
799
800 RETURN_OK_IF_SKIP(cctx);
801
802 // Get the known type of the two items on the stack. If they are matching
803 // use a type-specific instruction. Otherwise fall back to runtime type
804 // checking.
805 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
806 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100807 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200808 if (isntype == ISN_DROP)
809 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810
811 if ((isn = generate_instr(cctx, isntype)) == NULL)
812 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100813 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100814 isn->isn_arg.op.op_ic = ic;
815
816 // takes two arguments, puts one bool back
817 if (stack->ga_len >= 2)
818 {
819 --stack->ga_len;
820 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
821 }
822
823 return OK;
824}
825
826/*
827 * Generate an ISN_2BOOL instruction.
828 */
829 static int
830generate_2BOOL(cctx_T *cctx, int invert)
831{
832 isn_T *isn;
833 garray_T *stack = &cctx->ctx_type_stack;
834
Bram Moolenaar080457c2020-03-03 21:53:32 +0100835 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100836 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
837 return FAIL;
838 isn->isn_arg.number = invert;
839
840 // type becomes bool
841 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
842
843 return OK;
844}
845
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200846/*
847 * Generate an ISN_COND2BOOL instruction.
848 */
849 static int
850generate_COND2BOOL(cctx_T *cctx)
851{
852 isn_T *isn;
853 garray_T *stack = &cctx->ctx_type_stack;
854
855 RETURN_OK_IF_SKIP(cctx);
856 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
857 return FAIL;
858
859 // type becomes bool
860 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
861
862 return OK;
863}
864
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100865 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200866generate_TYPECHECK(
867 cctx_T *cctx,
868 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100869 int offset,
870 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100871{
872 isn_T *isn;
873 garray_T *stack = &cctx->ctx_type_stack;
874
Bram Moolenaar080457c2020-03-03 21:53:32 +0100875 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100876 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
877 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200878 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100879 isn->isn_arg.type.ct_off = (int8_T)offset;
880 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100881
Bram Moolenaar5e654232020-09-16 15:22:00 +0200882 // type becomes expected
883 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100884
885 return OK;
886}
887
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100888 static int
889generate_SETTYPE(
890 cctx_T *cctx,
891 type_T *expected)
892{
893 isn_T *isn;
894
895 RETURN_OK_IF_SKIP(cctx);
896 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
897 return FAIL;
898 isn->isn_arg.type.ct_type = alloc_type(expected);
899 return OK;
900}
901
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100902/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200903 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
904 * used. Return FALSE if the types will never match.
905 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100906 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200907use_typecheck(type_T *actual, type_T *expected)
908{
909 if (actual->tt_type == VAR_ANY
910 || actual->tt_type == VAR_UNKNOWN
911 || (actual->tt_type == VAR_FUNC
912 && (expected->tt_type == VAR_FUNC
913 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100914 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
915 && ((actual->tt_member == &t_void)
916 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200917 return TRUE;
918 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
919 && actual->tt_type == expected->tt_type)
920 // This takes care of a nested list or dict.
921 return use_typecheck(actual->tt_member, expected->tt_member);
922 return FALSE;
923}
924
925/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200926 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200927 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200928 * - "actual" is a type that can be "expected" type: add a runtime check; or
929 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200930 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
931 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200932 */
Bram Moolenaar351ead02021-01-16 16:07:01 +0100933 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200934need_type(
935 type_T *actual,
936 type_T *expected,
937 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100938 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200939 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200940 int silent,
941 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200942{
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100943 where_T where;
944
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200945 if (expected == &t_bool && actual != &t_bool
946 && (actual->tt_flags & TTFLAG_BOOL_OK))
947 {
948 // Using "0", "1" or the result of an expression with "&&" or "||" as a
949 // boolean is OK but requires a conversion.
950 generate_2BOOL(cctx, FALSE);
951 return OK;
952 }
953
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100954 where.wt_index = arg_idx;
955 where.wt_variable = FALSE;
956 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200957 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200958
959 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200960 // If it's a constant a runtime check makes no sense.
961 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200962 {
Bram Moolenaare32e5162021-01-21 20:21:29 +0100963 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200964 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200965 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200966
967 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100968 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200969 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200970}
971
972/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100973 * Check that the top of the type stack has a type that can be used as a
974 * condition. Give an error and return FAIL if not.
975 */
976 static int
977bool_on_stack(cctx_T *cctx)
978{
979 garray_T *stack = &cctx->ctx_type_stack;
980 type_T *type;
981
982 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
983 if (type == &t_bool)
984 return OK;
985
986 if (type == &t_any || type == &t_number)
987 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
988 // This requires a runtime type check.
989 return generate_COND2BOOL(cctx);
990
Bram Moolenaar351ead02021-01-16 16:07:01 +0100991 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100992}
993
994/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100995 * Generate an ISN_PUSHNR instruction.
996 */
997 static int
998generate_PUSHNR(cctx_T *cctx, varnumber_T number)
999{
1000 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001001 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001002
Bram Moolenaar080457c2020-03-03 21:53:32 +01001003 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001004 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1005 return FAIL;
1006 isn->isn_arg.number = number;
1007
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001008 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001009 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001010 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001011 return OK;
1012}
1013
1014/*
1015 * Generate an ISN_PUSHBOOL instruction.
1016 */
1017 static int
1018generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1019{
1020 isn_T *isn;
1021
Bram Moolenaar080457c2020-03-03 21:53:32 +01001022 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001023 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1024 return FAIL;
1025 isn->isn_arg.number = number;
1026
1027 return OK;
1028}
1029
1030/*
1031 * Generate an ISN_PUSHSPEC instruction.
1032 */
1033 static int
1034generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1035{
1036 isn_T *isn;
1037
Bram Moolenaar080457c2020-03-03 21:53:32 +01001038 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001039 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1040 return FAIL;
1041 isn->isn_arg.number = number;
1042
1043 return OK;
1044}
1045
1046#ifdef FEAT_FLOAT
1047/*
1048 * Generate an ISN_PUSHF instruction.
1049 */
1050 static int
1051generate_PUSHF(cctx_T *cctx, float_T fnumber)
1052{
1053 isn_T *isn;
1054
Bram Moolenaar080457c2020-03-03 21:53:32 +01001055 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1057 return FAIL;
1058 isn->isn_arg.fnumber = fnumber;
1059
1060 return OK;
1061}
1062#endif
1063
1064/*
1065 * Generate an ISN_PUSHS instruction.
1066 * Consumes "str".
1067 */
1068 static int
1069generate_PUSHS(cctx_T *cctx, char_u *str)
1070{
1071 isn_T *isn;
1072
Bram Moolenaar508b5612021-01-02 18:17:26 +01001073 if (cctx->ctx_skip == SKIP_YES)
1074 {
1075 vim_free(str);
1076 return OK;
1077 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001078 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1079 return FAIL;
1080 isn->isn_arg.string = str;
1081
1082 return OK;
1083}
1084
1085/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001086 * Generate an ISN_PUSHCHANNEL instruction.
1087 * Consumes "channel".
1088 */
1089 static int
1090generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1091{
1092 isn_T *isn;
1093
Bram Moolenaar080457c2020-03-03 21:53:32 +01001094 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001095 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1096 return FAIL;
1097 isn->isn_arg.channel = channel;
1098
1099 return OK;
1100}
1101
1102/*
1103 * Generate an ISN_PUSHJOB instruction.
1104 * Consumes "job".
1105 */
1106 static int
1107generate_PUSHJOB(cctx_T *cctx, job_T *job)
1108{
1109 isn_T *isn;
1110
Bram Moolenaar080457c2020-03-03 21:53:32 +01001111 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001112 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001113 return FAIL;
1114 isn->isn_arg.job = job;
1115
1116 return OK;
1117}
1118
1119/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001120 * Generate an ISN_PUSHBLOB instruction.
1121 * Consumes "blob".
1122 */
1123 static int
1124generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1125{
1126 isn_T *isn;
1127
Bram Moolenaar080457c2020-03-03 21:53:32 +01001128 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001129 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1130 return FAIL;
1131 isn->isn_arg.blob = blob;
1132
1133 return OK;
1134}
1135
1136/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001137 * Generate an ISN_PUSHFUNC instruction with name "name".
1138 * Consumes "name".
1139 */
1140 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001141generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001142{
1143 isn_T *isn;
1144
Bram Moolenaar080457c2020-03-03 21:53:32 +01001145 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001146 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001147 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001148 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001149
1150 return OK;
1151}
1152
1153/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001154 * Generate an ISN_GETITEM instruction with "index".
1155 */
1156 static int
1157generate_GETITEM(cctx_T *cctx, int index)
1158{
1159 isn_T *isn;
1160 garray_T *stack = &cctx->ctx_type_stack;
1161 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1162 type_T *item_type = &t_any;
1163
1164 RETURN_OK_IF_SKIP(cctx);
1165
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001166 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001167 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001168 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001169 emsg(_(e_listreq));
1170 return FAIL;
1171 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001172 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001173 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1174 return FAIL;
1175 isn->isn_arg.number = index;
1176
1177 // add the item type to the type stack
1178 if (ga_grow(stack, 1) == FAIL)
1179 return FAIL;
1180 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1181 ++stack->ga_len;
1182 return OK;
1183}
1184
1185/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001186 * Generate an ISN_SLICE instruction with "count".
1187 */
1188 static int
1189generate_SLICE(cctx_T *cctx, int count)
1190{
1191 isn_T *isn;
1192
1193 RETURN_OK_IF_SKIP(cctx);
1194 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1195 return FAIL;
1196 isn->isn_arg.number = count;
1197 return OK;
1198}
1199
1200/*
1201 * Generate an ISN_CHECKLEN instruction with "min_len".
1202 */
1203 static int
1204generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1205{
1206 isn_T *isn;
1207
1208 RETURN_OK_IF_SKIP(cctx);
1209
1210 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1211 return FAIL;
1212 isn->isn_arg.checklen.cl_min_len = min_len;
1213 isn->isn_arg.checklen.cl_more_OK = more_OK;
1214
1215 return OK;
1216}
1217
1218/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219 * Generate an ISN_STORE instruction.
1220 */
1221 static int
1222generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1223{
1224 isn_T *isn;
1225
Bram Moolenaar080457c2020-03-03 21:53:32 +01001226 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001227 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1228 return FAIL;
1229 if (name != NULL)
1230 isn->isn_arg.string = vim_strsave(name);
1231 else
1232 isn->isn_arg.number = idx;
1233
1234 return OK;
1235}
1236
1237/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001238 * Generate an ISN_STOREOUTER instruction.
1239 */
1240 static int
1241generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1242{
1243 isn_T *isn;
1244
1245 RETURN_OK_IF_SKIP(cctx);
1246 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1247 return FAIL;
1248 isn->isn_arg.outer.outer_idx = idx;
1249 isn->isn_arg.outer.outer_depth = level;
1250
1251 return OK;
1252}
1253
1254/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001255 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1256 */
1257 static int
1258generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1259{
1260 isn_T *isn;
1261
Bram Moolenaar080457c2020-03-03 21:53:32 +01001262 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1264 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001265 isn->isn_arg.storenr.stnr_idx = idx;
1266 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001267
1268 return OK;
1269}
1270
1271/*
1272 * Generate an ISN_STOREOPT instruction
1273 */
1274 static int
1275generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1276{
1277 isn_T *isn;
1278
Bram Moolenaar080457c2020-03-03 21:53:32 +01001279 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001280 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001281 return FAIL;
1282 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1283 isn->isn_arg.storeopt.so_flags = opt_flags;
1284
1285 return OK;
1286}
1287
1288/*
1289 * Generate an ISN_LOAD or similar instruction.
1290 */
1291 static int
1292generate_LOAD(
1293 cctx_T *cctx,
1294 isntype_T isn_type,
1295 int idx,
1296 char_u *name,
1297 type_T *type)
1298{
1299 isn_T *isn;
1300
Bram Moolenaar080457c2020-03-03 21:53:32 +01001301 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001302 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1303 return FAIL;
1304 if (name != NULL)
1305 isn->isn_arg.string = vim_strsave(name);
1306 else
1307 isn->isn_arg.number = idx;
1308
1309 return OK;
1310}
1311
1312/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001313 * Generate an ISN_LOADOUTER instruction
1314 */
1315 static int
1316generate_LOADOUTER(
1317 cctx_T *cctx,
1318 int idx,
1319 int nesting,
1320 type_T *type)
1321{
1322 isn_T *isn;
1323
1324 RETURN_OK_IF_SKIP(cctx);
1325 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1326 return FAIL;
1327 isn->isn_arg.outer.outer_idx = idx;
1328 isn->isn_arg.outer.outer_depth = nesting;
1329
1330 return OK;
1331}
1332
1333/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001334 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001335 */
1336 static int
1337generate_LOADV(
1338 cctx_T *cctx,
1339 char_u *name,
1340 int error)
1341{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001342 int di_flags;
1343 int vidx = find_vim_var(name, &di_flags);
1344 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001345
Bram Moolenaar080457c2020-03-03 21:53:32 +01001346 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001347 if (vidx < 0)
1348 {
1349 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001350 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001351 return FAIL;
1352 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001353 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001354
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001355 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001356}
1357
1358/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001359 * Generate an ISN_UNLET instruction.
1360 */
1361 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001362generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001363{
1364 isn_T *isn;
1365
1366 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001367 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001368 return FAIL;
1369 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1370 isn->isn_arg.unlet.ul_forceit = forceit;
1371
1372 return OK;
1373}
1374
1375/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001376 * Generate an ISN_LOCKCONST instruction.
1377 */
1378 static int
1379generate_LOCKCONST(cctx_T *cctx)
1380{
1381 isn_T *isn;
1382
1383 RETURN_OK_IF_SKIP(cctx);
1384 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1385 return FAIL;
1386 return OK;
1387}
1388
1389/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001390 * Generate an ISN_LOADS instruction.
1391 */
1392 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001393generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001394 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001395 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001396 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001397 int sid,
1398 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399{
1400 isn_T *isn;
1401
Bram Moolenaar080457c2020-03-03 21:53:32 +01001402 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001403 if (isn_type == ISN_LOADS)
1404 isn = generate_instr_type(cctx, isn_type, type);
1405 else
1406 isn = generate_instr_drop(cctx, isn_type, 1);
1407 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001408 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001409 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1410 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411
1412 return OK;
1413}
1414
1415/*
1416 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1417 */
1418 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001419generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001420 cctx_T *cctx,
1421 isntype_T isn_type,
1422 int sid,
1423 int idx,
1424 type_T *type)
1425{
1426 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001427 scriptref_T *sref;
1428 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429
Bram Moolenaar080457c2020-03-03 21:53:32 +01001430 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001431 if (isn_type == ISN_LOADSCRIPT)
1432 isn = generate_instr_type(cctx, isn_type, type);
1433 else
1434 isn = generate_instr_drop(cctx, isn_type, 1);
1435 if (isn == NULL)
1436 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001437
1438 // This requires three arguments, which doesn't fit in an instruction, thus
1439 // we need to allocate a struct for this.
1440 sref = ALLOC_ONE(scriptref_T);
1441 if (sref == NULL)
1442 return FAIL;
1443 isn->isn_arg.script.scriptref = sref;
1444 sref->sref_sid = sid;
1445 sref->sref_idx = idx;
1446 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001447 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001448 return OK;
1449}
1450
1451/*
1452 * Generate an ISN_NEWLIST instruction.
1453 */
1454 static int
1455generate_NEWLIST(cctx_T *cctx, int count)
1456{
1457 isn_T *isn;
1458 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001459 type_T *type;
1460 type_T *member;
1461
Bram Moolenaar080457c2020-03-03 21:53:32 +01001462 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001463 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1464 return FAIL;
1465 isn->isn_arg.number = count;
1466
Bram Moolenaar127542b2020-08-09 17:22:04 +02001467 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001468 if (count == 0)
1469 member = &t_void;
1470 else
1471 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001472 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1473 cctx->ctx_type_list);
1474 type = get_list_type(member, cctx->ctx_type_list);
1475
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001476 // drop the value types
1477 stack->ga_len -= count;
1478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 // add the list type to the type stack
1480 if (ga_grow(stack, 1) == FAIL)
1481 return FAIL;
1482 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1483 ++stack->ga_len;
1484
1485 return OK;
1486}
1487
1488/*
1489 * Generate an ISN_NEWDICT instruction.
1490 */
1491 static int
1492generate_NEWDICT(cctx_T *cctx, int count)
1493{
1494 isn_T *isn;
1495 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496 type_T *type;
1497 type_T *member;
1498
Bram Moolenaar080457c2020-03-03 21:53:32 +01001499 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001500 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1501 return FAIL;
1502 isn->isn_arg.number = count;
1503
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001504 if (count == 0)
1505 member = &t_void;
1506 else
1507 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001508 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1509 cctx->ctx_type_list);
1510 type = get_dict_type(member, cctx->ctx_type_list);
1511
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001512 // drop the key and value types
1513 stack->ga_len -= 2 * count;
1514
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001515 // add the dict type to the type stack
1516 if (ga_grow(stack, 1) == FAIL)
1517 return FAIL;
1518 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1519 ++stack->ga_len;
1520
1521 return OK;
1522}
1523
1524/*
1525 * Generate an ISN_FUNCREF instruction.
1526 */
1527 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001528generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001529{
1530 isn_T *isn;
1531 garray_T *stack = &cctx->ctx_type_stack;
1532
Bram Moolenaar080457c2020-03-03 21:53:32 +01001533 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001534 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1535 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001536 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001537 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001538
Bram Moolenaarab360522021-01-10 14:02:28 +01001539 // if the referenced function is a closure, it may use items further up in
1540 // the nested context, including this one.
1541 if (ufunc->uf_flags & FC_CLOSURE)
1542 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1543
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 if (ga_grow(stack, 1) == FAIL)
1545 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001546 ((type_T **)stack->ga_data)[stack->ga_len] =
1547 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001548 ++stack->ga_len;
1549
1550 return OK;
1551}
1552
1553/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001554 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001555 * "lambda_name" and "func_name" must be in allocated memory and will be
1556 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001557 */
1558 static int
1559generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1560{
1561 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001562
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001563 if (cctx->ctx_skip == SKIP_YES)
1564 {
1565 vim_free(lambda_name);
1566 vim_free(func_name);
1567 return OK;
1568 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001569 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001570 {
1571 vim_free(lambda_name);
1572 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001573 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001574 }
1575 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001576 isn->isn_arg.newfunc.nf_global = func_name;
1577
1578 return OK;
1579}
1580
1581/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001582 * Generate an ISN_DEF instruction: list functions
1583 */
1584 static int
1585generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1586{
1587 isn_T *isn;
1588
1589 RETURN_OK_IF_SKIP(cctx);
1590 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1591 return FAIL;
1592 if (len > 0)
1593 {
1594 isn->isn_arg.string = vim_strnsave(name, len);
1595 if (isn->isn_arg.string == NULL)
1596 return FAIL;
1597 }
1598 return OK;
1599}
1600
1601/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001602 * Generate an ISN_JUMP instruction.
1603 */
1604 static int
1605generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1606{
1607 isn_T *isn;
1608 garray_T *stack = &cctx->ctx_type_stack;
1609
Bram Moolenaar080457c2020-03-03 21:53:32 +01001610 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1612 return FAIL;
1613 isn->isn_arg.jump.jump_when = when;
1614 isn->isn_arg.jump.jump_where = where;
1615
1616 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1617 --stack->ga_len;
1618
1619 return OK;
1620}
1621
1622 static int
1623generate_FOR(cctx_T *cctx, int loop_idx)
1624{
1625 isn_T *isn;
1626 garray_T *stack = &cctx->ctx_type_stack;
1627
Bram Moolenaar080457c2020-03-03 21:53:32 +01001628 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1630 return FAIL;
1631 isn->isn_arg.forloop.for_idx = loop_idx;
1632
1633 if (ga_grow(stack, 1) == FAIL)
1634 return FAIL;
1635 // type doesn't matter, will be stored next
1636 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1637 ++stack->ga_len;
1638
1639 return OK;
1640}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001641/*
1642 * Generate an ISN_TRYCONT instruction.
1643 */
1644 static int
1645generate_TRYCONT(cctx_T *cctx, int levels, int where)
1646{
1647 isn_T *isn;
1648
1649 RETURN_OK_IF_SKIP(cctx);
1650 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1651 return FAIL;
1652 isn->isn_arg.trycont.tct_levels = levels;
1653 isn->isn_arg.trycont.tct_where = where;
1654
1655 return OK;
1656}
1657
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658
1659/*
1660 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001661 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001662 * Return FAIL if the number of arguments is wrong.
1663 */
1664 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001665generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001666{
1667 isn_T *isn;
1668 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001669 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001670 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001671 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001672
Bram Moolenaar080457c2020-03-03 21:53:32 +01001673 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001674 argoff = check_internal_func(func_idx, argcount);
1675 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001676 return FAIL;
1677
Bram Moolenaar389df252020-07-09 21:20:47 +02001678 if (method_call && argoff > 1)
1679 {
1680 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1681 return FAIL;
1682 isn->isn_arg.shuffle.shfl_item = argcount;
1683 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1684 }
1685
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001686 if (argcount > 0)
1687 {
1688 // Check the types of the arguments.
1689 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001690 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1691 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001692 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001693 if (internal_func_is_map(func_idx))
1694 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001695 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001696
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1698 return FAIL;
1699 isn->isn_arg.bfunc.cbf_idx = func_idx;
1700 isn->isn_arg.bfunc.cbf_argcount = argcount;
1701
Bram Moolenaar94738d82020-10-21 14:25:07 +02001702 // Drop the argument types and push the return type.
1703 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001704 if (ga_grow(stack, 1) == FAIL)
1705 return FAIL;
1706 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001707 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001708 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001709
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001710 if (maptype != NULL && maptype->tt_member != NULL
1711 && maptype->tt_member != &t_any)
1712 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001713 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001714
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001715 return OK;
1716}
1717
1718/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001719 * Generate an ISN_LISTAPPEND instruction. Works like add().
1720 * Argument count is already checked.
1721 */
1722 static int
1723generate_LISTAPPEND(cctx_T *cctx)
1724{
1725 garray_T *stack = &cctx->ctx_type_stack;
1726 type_T *list_type;
1727 type_T *item_type;
1728 type_T *expected;
1729
1730 // Caller already checked that list_type is a list.
1731 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1732 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1733 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001734 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001735 return FAIL;
1736
1737 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1738 return FAIL;
1739
1740 --stack->ga_len; // drop the argument
1741 return OK;
1742}
1743
1744/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001745 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1746 * Argument count is already checked.
1747 */
1748 static int
1749generate_BLOBAPPEND(cctx_T *cctx)
1750{
1751 garray_T *stack = &cctx->ctx_type_stack;
1752 type_T *item_type;
1753
1754 // Caller already checked that blob_type is a blob.
1755 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001756 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001757 return FAIL;
1758
1759 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1760 return FAIL;
1761
1762 --stack->ga_len; // drop the argument
1763 return OK;
1764}
1765
1766/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001767 * Return TRUE if "ufunc" should be compiled, taking into account whether
1768 * "profile" indicates profiling is to be done.
1769 */
1770 int
Bram Moolenaarf002a412021-01-24 13:34:18 +01001771func_needs_compiling(ufunc_T *ufunc, int profile UNUSED)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001772{
1773 switch (ufunc->uf_def_status)
1774 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001775 case UF_NOT_COMPILED: break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001776 case UF_TO_BE_COMPILED: return TRUE;
1777 case UF_COMPILED:
1778 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001779#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001780 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1781 + ufunc->uf_dfunc_idx;
1782
1783 return profile ? dfunc->df_instr_prof == NULL
1784 : dfunc->df_instr == NULL;
Bram Moolenaarf002a412021-01-24 13:34:18 +01001785#else
1786 break;
1787#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01001788 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001789 case UF_COMPILING: break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001790 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001791 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001792}
1793
1794/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795 * Generate an ISN_DCALL or ISN_UCALL instruction.
1796 * Return FAIL if the number of arguments is wrong.
1797 */
1798 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001799generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001800{
1801 isn_T *isn;
1802 garray_T *stack = &cctx->ctx_type_stack;
1803 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001804 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805
Bram Moolenaar080457c2020-03-03 21:53:32 +01001806 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807 if (argcount > regular_args && !has_varargs(ufunc))
1808 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001809 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810 return FAIL;
1811 }
1812 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1813 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001814 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001815 return FAIL;
1816 }
1817
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001818 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001819 {
1820 int i;
1821
1822 for (i = 0; i < argcount; ++i)
1823 {
1824 type_T *expected;
1825 type_T *actual;
1826
1827 if (i < regular_args)
1828 {
1829 if (ufunc->uf_arg_types == NULL)
1830 continue;
1831 expected = ufunc->uf_arg_types[i];
1832 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001833 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1834 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001835 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001836 else
1837 expected = ufunc->uf_va_type->tt_member;
1838 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001839 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001840 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001841 {
1842 arg_type_mismatch(expected, actual, i + 1);
1843 return FAIL;
1844 }
1845 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001846 if (func_needs_compiling(ufunc, PROFILING(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001847 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001848 PROFILING(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001849 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001850 }
1851
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001852 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001853 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001854 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001855 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001856 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001857 {
1858 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1859 isn->isn_arg.dfunc.cdf_argcount = argcount;
1860 }
1861 else
1862 {
1863 // A user function may be deleted and redefined later, can't use the
1864 // ufunc pointer, need to look it up again at runtime.
1865 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1866 isn->isn_arg.ufunc.cuf_argcount = argcount;
1867 }
1868
1869 stack->ga_len -= argcount; // drop the arguments
1870 if (ga_grow(stack, 1) == FAIL)
1871 return FAIL;
1872 // add return value
1873 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1874 ++stack->ga_len;
1875
1876 return OK;
1877}
1878
1879/*
1880 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1881 */
1882 static int
1883generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1884{
1885 isn_T *isn;
1886 garray_T *stack = &cctx->ctx_type_stack;
1887
Bram Moolenaar080457c2020-03-03 21:53:32 +01001888 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001889 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1890 return FAIL;
1891 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1892 isn->isn_arg.ufunc.cuf_argcount = argcount;
1893
1894 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001895 if (ga_grow(stack, 1) == FAIL)
1896 return FAIL;
1897 // add return value
1898 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1899 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001900
1901 return OK;
1902}
1903
1904/*
1905 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001906 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001907 */
1908 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001909generate_PCALL(
1910 cctx_T *cctx,
1911 int argcount,
1912 char_u *name,
1913 type_T *type,
1914 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001915{
1916 isn_T *isn;
1917 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001918 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001919
Bram Moolenaar080457c2020-03-03 21:53:32 +01001920 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001921
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001922 if (type->tt_type == VAR_ANY)
1923 ret_type = &t_any;
1924 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001925 {
1926 if (type->tt_argcount != -1)
1927 {
1928 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1929
1930 if (argcount < type->tt_min_argcount - varargs)
1931 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001932 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001933 return FAIL;
1934 }
1935 if (!varargs && argcount > type->tt_argcount)
1936 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001937 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001938 return FAIL;
1939 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001940 if (type->tt_args != NULL)
1941 {
1942 int i;
1943
1944 for (i = 0; i < argcount; ++i)
1945 {
1946 int offset = -argcount + i - 1;
1947 type_T *actual = ((type_T **)stack->ga_data)[
1948 stack->ga_len + offset];
1949 type_T *expected;
1950
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001951 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001952 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001953 type->tt_argcount - 1]->tt_member;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001954 else
1955 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001956 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001957 cctx, TRUE, FALSE) == FAIL)
1958 {
1959 arg_type_mismatch(expected, actual, i + 1);
1960 return FAIL;
1961 }
1962 }
1963 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001964 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001965 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001966 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001967 else
1968 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001969 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001970 return FAIL;
1971 }
1972
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001973 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1974 return FAIL;
1975 isn->isn_arg.pfunc.cpf_top = at_top;
1976 isn->isn_arg.pfunc.cpf_argcount = argcount;
1977
1978 stack->ga_len -= argcount; // drop the arguments
1979
1980 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001981 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001982
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001983 // If partial is above the arguments it must be cleared and replaced with
1984 // the return value.
1985 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1986 return FAIL;
1987
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988 return OK;
1989}
1990
1991/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001992 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993 */
1994 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001995generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001996{
1997 isn_T *isn;
1998 garray_T *stack = &cctx->ctx_type_stack;
1999 type_T *type;
2000
Bram Moolenaar080457c2020-03-03 21:53:32 +01002001 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002002 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002004 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002005
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002006 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002008 if (type->tt_type != VAR_DICT && type != &t_any)
2009 {
2010 emsg(_(e_dictreq));
2011 return FAIL;
2012 }
2013 // change dict type to dict member type
2014 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002015 {
2016 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2017 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2018 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002019
2020 return OK;
2021}
2022
2023/*
2024 * Generate an ISN_ECHO instruction.
2025 */
2026 static int
2027generate_ECHO(cctx_T *cctx, int with_white, int count)
2028{
2029 isn_T *isn;
2030
Bram Moolenaar080457c2020-03-03 21:53:32 +01002031 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002032 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2033 return FAIL;
2034 isn->isn_arg.echo.echo_with_white = with_white;
2035 isn->isn_arg.echo.echo_count = count;
2036
2037 return OK;
2038}
2039
Bram Moolenaarad39c092020-02-26 18:23:43 +01002040/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002041 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002042 */
2043 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002044generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002045{
2046 isn_T *isn;
2047
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002048 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002049 return FAIL;
2050 isn->isn_arg.number = count;
2051
2052 return OK;
2053}
2054
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002055/*
2056 * Generate an ISN_PUT instruction.
2057 */
2058 static int
2059generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2060{
2061 isn_T *isn;
2062
2063 RETURN_OK_IF_SKIP(cctx);
2064 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2065 return FAIL;
2066 isn->isn_arg.put.put_regname = regname;
2067 isn->isn_arg.put.put_lnum = lnum;
2068 return OK;
2069}
2070
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002071 static int
2072generate_EXEC(cctx_T *cctx, char_u *line)
2073{
2074 isn_T *isn;
2075
Bram Moolenaar080457c2020-03-03 21:53:32 +01002076 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002077 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2078 return FAIL;
2079 isn->isn_arg.string = vim_strsave(line);
2080 return OK;
2081}
2082
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002083 static int
2084generate_EXECCONCAT(cctx_T *cctx, int count)
2085{
2086 isn_T *isn;
2087
2088 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2089 return FAIL;
2090 isn->isn_arg.number = count;
2091 return OK;
2092}
2093
Bram Moolenaar08597872020-12-10 19:43:40 +01002094/*
2095 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2096 */
2097 static int
2098generate_RANGE(cctx_T *cctx, char_u *range)
2099{
2100 isn_T *isn;
2101 garray_T *stack = &cctx->ctx_type_stack;
2102
2103 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2104 return FAIL;
2105 isn->isn_arg.string = range;
2106
2107 if (ga_grow(stack, 1) == FAIL)
2108 return FAIL;
2109 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2110 ++stack->ga_len;
2111 return OK;
2112}
2113
Bram Moolenaar792f7862020-11-23 08:31:18 +01002114 static int
2115generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2116{
2117 isn_T *isn;
2118
2119 RETURN_OK_IF_SKIP(cctx);
2120 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2121 return FAIL;
2122 isn->isn_arg.unpack.unp_count = var_count;
2123 isn->isn_arg.unpack.unp_semicolon = semicolon;
2124 return OK;
2125}
2126
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002127/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002128 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002129 */
2130 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002131generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002132{
2133 isn_T *isn;
2134
Bram Moolenaar02194d22020-10-24 23:08:38 +02002135 if (cmod->cmod_flags != 0
2136 || cmod->cmod_split != 0
2137 || cmod->cmod_verbose != 0
2138 || cmod->cmod_tab != 0
2139 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002140 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002141 cctx->ctx_has_cmdmod = TRUE;
2142
2143 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002144 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002145 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2146 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2147 return FAIL;
2148 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002149 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002150 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002151 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002152
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002153 return OK;
2154}
2155
2156 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002157generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002158{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002159 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2160 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002161 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002162 return OK;
2163}
2164
Bram Moolenaarf002a412021-01-24 13:34:18 +01002165#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002166 static void
2167may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2168{
2169 if (cctx->ctx_profiling && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002170 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002171}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002172#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002173
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002174/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002176 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002177 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002178 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002179reserve_local(
2180 cctx_T *cctx,
2181 char_u *name,
2182 size_t len,
2183 int isConst,
2184 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002185{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002186 lvar_T *lvar;
2187
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002188 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002189 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002190 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002191 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002192 }
2193
2194 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002195 return NULL;
2196 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002197 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002198
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002199 // Every local variable uses the next entry on the stack. We could re-use
2200 // the last ones when leaving a scope, but then variables used in a closure
2201 // might get overwritten. To keep things simple do not re-use stack
2202 // entries. This is less efficient, but memory is cheap these days.
2203 lvar->lv_idx = cctx->ctx_locals_count++;
2204
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002205 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002206 lvar->lv_const = isConst;
2207 lvar->lv_type = type;
2208
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002209 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002210}
2211
2212/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002213 * Remove local variables above "new_top".
2214 */
2215 static void
2216unwind_locals(cctx_T *cctx, int new_top)
2217{
2218 if (cctx->ctx_locals.ga_len > new_top)
2219 {
2220 int idx;
2221 lvar_T *lvar;
2222
2223 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2224 {
2225 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2226 vim_free(lvar->lv_name);
2227 }
2228 }
2229 cctx->ctx_locals.ga_len = new_top;
2230}
2231
2232/*
2233 * Free all local variables.
2234 */
2235 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002236free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002237{
2238 unwind_locals(cctx, 0);
2239 ga_clear(&cctx->ctx_locals);
2240}
2241
2242/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002243 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2244 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2245 * error if the variable was defined with :const.
2246 */
2247 static int
2248check_item_writable(svar_T *sv, int check_writable, char_u *name)
2249{
2250 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2251 || (check_writable == ASSIGN_FINAL
2252 && sv->sv_const == ASSIGN_CONST))
2253 {
2254 semsg(_(e_readonlyvar), name);
2255 return FAIL;
2256 }
2257 return OK;
2258}
2259
2260/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002261 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002262 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002263 * Returns the index in "sn_var_vals" if found.
2264 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002265 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002266 */
2267 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002268get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002269{
2270 hashtab_T *ht;
2271 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002272 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002273 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002274 int idx;
2275
Bram Moolenaare3d46852020-08-29 13:39:17 +02002276 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002277 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002278 if (sid == current_sctx.sc_sid)
2279 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002280 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002281
2282 if (sav == NULL)
2283 return -2;
2284 idx = sav->sav_var_vals_idx;
2285 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002286 if (check_item_writable(sv, check_writable, name) == FAIL)
2287 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002288 return idx;
2289 }
2290
2291 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002292 ht = &SCRIPT_VARS(sid);
2293 di = find_var_in_ht(ht, 0, name, TRUE);
2294 if (di == NULL)
2295 return -2;
2296
2297 // Now find the svar_T index in sn_var_vals.
2298 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2299 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002300 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002301 if (sv->sv_tv == &di->di_tv)
2302 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002303 if (check_item_writable(sv, check_writable, name) == FAIL)
2304 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305 return idx;
2306 }
2307 }
2308 return -1;
2309}
2310
2311/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002312 * Find "name" in imported items of the current script or in "cctx" if not
2313 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002314 */
2315 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002316find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002317{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002318 int idx;
2319
Bram Moolenaare3d46852020-08-29 13:39:17 +02002320 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002321 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322 if (cctx != NULL)
2323 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2324 {
2325 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2326 + idx;
2327
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002328 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2329 : STRLEN(import->imp_name) == len
2330 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002331 return import;
2332 }
2333
Bram Moolenaarefa94442020-08-08 22:16:00 +02002334 return find_imported_in_script(name, len, current_sctx.sc_sid);
2335}
2336
2337 imported_T *
2338find_imported_in_script(char_u *name, size_t len, int sid)
2339{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002340 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002341 int idx;
2342
Bram Moolenaare3d46852020-08-29 13:39:17 +02002343 if (!SCRIPT_ID_VALID(sid))
2344 return NULL;
2345 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002346 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2347 {
2348 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2349
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002350 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2351 : STRLEN(import->imp_name) == len
2352 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353 return import;
2354 }
2355 return NULL;
2356}
2357
2358/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002359 * Free all imported variables.
2360 */
2361 static void
2362free_imported(cctx_T *cctx)
2363{
2364 int idx;
2365
2366 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2367 {
2368 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2369
2370 vim_free(import->imp_name);
2371 }
2372 ga_clear(&cctx->ctx_imports);
2373}
2374
2375/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002376 * Return a pointer to the next line that isn't empty or only contains a
2377 * comment. Skips over white space.
2378 * Returns NULL if there is none.
2379 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002380 char_u *
2381peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002382{
2383 int lnum = cctx->ctx_lnum;
2384
2385 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2386 {
2387 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002388 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002389
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002390 // ignore NULLs inserted for continuation lines
2391 if (line != NULL)
2392 {
2393 p = skipwhite(line);
2394 if (*p != NUL && !vim9_comment_start(p))
2395 return p;
2396 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002397 }
2398 return NULL;
2399}
2400
2401/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002402 * Called when checking for a following operator at "arg". When the rest of
2403 * the line is empty or only a comment, peek the next line. If there is a next
2404 * line return a pointer to it and set "nextp".
2405 * Otherwise skip over white space.
2406 */
2407 static char_u *
2408may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2409{
2410 char_u *p = skipwhite(arg);
2411
2412 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002413 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002414 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002415 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002416 if (*nextp != NULL)
2417 return *nextp;
2418 }
2419 return p;
2420}
2421
2422/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002423 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002424 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002425 * Returns NULL when at the end.
2426 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002427 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002428next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002429{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002430 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002431
2432 do
2433 {
2434 ++cctx->ctx_lnum;
2435 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002436 {
2437 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002438 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002439 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002440 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002441 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002442 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002443 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002444 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002445 return line;
2446}
2447
2448/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002449 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002450 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002451 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002452 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2453 */
2454 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002455may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002456{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002457 *arg = skipwhite(whitep);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002458 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002459 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002460 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002461
2462 if (next == NULL)
2463 return FAIL;
2464 *arg = skipwhite(next);
2465 }
2466 return OK;
2467}
2468
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002469/*
2470 * Idem, and give an error when failed.
2471 */
2472 static int
2473may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2474{
2475 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2476 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002477 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002478 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002479 return FAIL;
2480 }
2481 return OK;
2482}
2483
2484
Bram Moolenaara5565e42020-05-09 15:44:01 +02002485// Structure passed between the compile_expr* functions to keep track of
2486// constants that have been parsed but for which no code was produced yet. If
2487// possible expressions on these constants are applied at compile time. If
2488// that is not possible, the code to push the constants needs to be generated
2489// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002490// Using 50 should be more than enough of 5 levels of ().
2491#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002492typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002493 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002494 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002495 int pp_is_const; // all generated code was constants, used for a
2496 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002497} ppconst_T;
2498
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002499static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002500static int compile_expr0(char_u **arg, cctx_T *cctx);
2501static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2502
Bram Moolenaara5565e42020-05-09 15:44:01 +02002503/*
2504 * Generate a PUSH instruction for "tv".
2505 * "tv" will be consumed or cleared.
2506 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2507 */
2508 static int
2509generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2510{
2511 if (tv != NULL)
2512 {
2513 switch (tv->v_type)
2514 {
2515 case VAR_UNKNOWN:
2516 break;
2517 case VAR_BOOL:
2518 generate_PUSHBOOL(cctx, tv->vval.v_number);
2519 break;
2520 case VAR_SPECIAL:
2521 generate_PUSHSPEC(cctx, tv->vval.v_number);
2522 break;
2523 case VAR_NUMBER:
2524 generate_PUSHNR(cctx, tv->vval.v_number);
2525 break;
2526#ifdef FEAT_FLOAT
2527 case VAR_FLOAT:
2528 generate_PUSHF(cctx, tv->vval.v_float);
2529 break;
2530#endif
2531 case VAR_BLOB:
2532 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2533 tv->vval.v_blob = NULL;
2534 break;
2535 case VAR_STRING:
2536 generate_PUSHS(cctx, tv->vval.v_string);
2537 tv->vval.v_string = NULL;
2538 break;
2539 default:
2540 iemsg("constant type not supported");
2541 clear_tv(tv);
2542 return FAIL;
2543 }
2544 tv->v_type = VAR_UNKNOWN;
2545 }
2546 return OK;
2547}
2548
2549/*
2550 * Generate code for any ppconst entries.
2551 */
2552 static int
2553generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2554{
2555 int i;
2556 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002557 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002558
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002559 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002560 for (i = 0; i < ppconst->pp_used; ++i)
2561 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2562 ret = FAIL;
2563 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002564 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002565 return ret;
2566}
2567
2568/*
2569 * Clear ppconst constants. Used when failing.
2570 */
2571 static void
2572clear_ppconst(ppconst_T *ppconst)
2573{
2574 int i;
2575
2576 for (i = 0; i < ppconst->pp_used; ++i)
2577 clear_tv(&ppconst->pp_tv[i]);
2578 ppconst->pp_used = 0;
2579}
2580
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002581/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002582 * Generate an instruction to load script-local variable "name", without the
2583 * leading "s:".
2584 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585 */
2586 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002587compile_load_scriptvar(
2588 cctx_T *cctx,
2589 char_u *name, // variable NUL terminated
2590 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002591 char_u **end, // end of variable
2592 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002594 scriptitem_T *si;
2595 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596 imported_T *import;
2597
Bram Moolenaare3d46852020-08-29 13:39:17 +02002598 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2599 return FAIL;
2600 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002601 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002602 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002604 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002605 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2606 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002607 }
2608 if (idx >= 0)
2609 {
2610 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2611
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002612 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002613 current_sctx.sc_sid, idx, sv->sv_type);
2614 return OK;
2615 }
2616
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002617 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002618 if (import != NULL)
2619 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002620 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002621 {
2622 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002623 char_u *exp_name;
2624 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002625 ufunc_T *ufunc;
2626 type_T *type;
2627
2628 // Used "import * as Name", need to lookup the member.
2629 if (*p != '.')
2630 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002631 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002632 return FAIL;
2633 }
2634 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002635 if (VIM_ISWHITE(*p))
2636 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002637 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002638 return FAIL;
2639 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002640
Bram Moolenaar1c991142020-07-04 13:15:31 +02002641 // isolate one name
2642 exp_name = p;
2643 while (eval_isnamec(*p))
2644 ++p;
2645 cc = *p;
2646 *p = NUL;
2647
Bram Moolenaaredba7072021-03-13 21:14:18 +01002648 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2649 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002650 *p = cc;
2651 p = skipwhite(p);
2652
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002653 // TODO: what if it is a function?
2654 if (idx < 0)
2655 return FAIL;
2656 *end = p;
2657
2658 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2659 import->imp_sid,
2660 idx,
2661 type);
2662 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002663 else if (import->imp_funcname != NULL)
2664 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002665 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002666 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2667 import->imp_sid,
2668 import->imp_var_vals_idx,
2669 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002670 return OK;
2671 }
2672
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002673 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002674 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002675 return FAIL;
2676}
2677
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002678 static int
2679generate_funcref(cctx_T *cctx, char_u *name)
2680{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002681 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002682
2683 if (ufunc == NULL)
2684 return FAIL;
2685
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002686 // Need to compile any default values to get the argument types.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01002687 if (func_needs_compiling(ufunc, PROFILING(ufunc))
2688 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002689 == FAIL)
2690 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002691 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002692}
2693
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002694/*
2695 * Compile a variable name into a load instruction.
2696 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002697 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002698 * When "error" is FALSE do not give an error when not found.
2699 */
2700 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002701compile_load(
2702 char_u **arg,
2703 char_u *end_arg,
2704 cctx_T *cctx,
2705 int is_expr,
2706 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707{
2708 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002709 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002710 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002712 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713
2714 if (*(*arg + 1) == ':')
2715 {
2716 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002717 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002718 {
2719 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002720
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002721 switch (**arg)
2722 {
2723 case 'g': isn_type = ISN_LOADGDICT; break;
2724 case 'w': isn_type = ISN_LOADWDICT; break;
2725 case 't': isn_type = ISN_LOADTDICT; break;
2726 case 'b': isn_type = ISN_LOADBDICT; break;
2727 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002728 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002729 goto theend;
2730 }
2731 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2732 goto theend;
2733 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002734 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002735 else
2736 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002737 isntype_T isn_type = ISN_DROP;
2738
2739 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2740 if (name == NULL)
2741 return FAIL;
2742
2743 switch (**arg)
2744 {
2745 case 'v': res = generate_LOADV(cctx, name, error);
2746 break;
2747 case 's': res = compile_load_scriptvar(cctx, name,
2748 NULL, NULL, error);
2749 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002750 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2751 isn_type = ISN_LOADG;
2752 else
2753 {
2754 isn_type = ISN_LOADAUTO;
2755 vim_free(name);
2756 name = vim_strnsave(*arg, end - *arg);
2757 if (name == NULL)
2758 return FAIL;
2759 }
2760 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002761 case 'w': isn_type = ISN_LOADW; break;
2762 case 't': isn_type = ISN_LOADT; break;
2763 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002764 default: // cannot happen, just in case
2765 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002766 goto theend;
2767 }
2768 if (isn_type != ISN_DROP)
2769 {
2770 // Global, Buffer-local, Window-local and Tabpage-local
2771 // variables can be defined later, thus we don't check if it
2772 // exists, give error at runtime.
2773 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2774 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775 }
2776 }
2777 else
2778 {
2779 size_t len = end - *arg;
2780 int idx;
2781 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002782 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783
2784 name = vim_strnsave(*arg, end - *arg);
2785 if (name == NULL)
2786 return FAIL;
2787
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002788 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002790 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002791 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002792 }
2793 else
2794 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002795 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002796
Bram Moolenaar709664c2020-12-12 14:33:41 +01002797 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002799 type = lvar.lv_type;
2800 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01002801 if (lvar.lv_from_outer != 0)
2802 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002803 else
2804 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002805 }
2806 else
2807 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002808 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002809 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002810 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002811 || find_imported(name, 0, cctx) != NULL)
2812 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002813
Bram Moolenaar0f769812020-09-12 18:32:34 +02002814 // When evaluating an expression and the name starts with an
2815 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002816 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002817 if (res == FAIL && is_expr
2818 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002819 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820 }
2821 }
2822 if (gen_load)
2823 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01002824 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002825 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002826 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002827 cctx->ctx_outer_used = TRUE;
2828 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 }
2830
2831 *arg = end;
2832
2833theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002834 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002835 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002836 vim_free(name);
2837 return res;
2838}
2839
2840/*
2841 * Compile the argument expressions.
2842 * "arg" points to just after the "(" and is advanced to after the ")"
2843 */
2844 static int
2845compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2846{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002847 char_u *p = *arg;
2848 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002849 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850
Bram Moolenaare6085c52020-04-12 20:19:16 +02002851 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002853 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2854 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002855 if (*p == ')')
2856 {
2857 *arg = p + 1;
2858 return OK;
2859 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002860 if (must_end)
2861 {
2862 semsg(_(e_missing_comma_before_argument_str), p);
2863 return FAIL;
2864 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002865
Bram Moolenaara5565e42020-05-09 15:44:01 +02002866 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 return FAIL;
2868 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002869
2870 if (*p != ',' && *skipwhite(p) == ',')
2871 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01002872 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002873 p = skipwhite(p);
2874 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002876 {
2877 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002878 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01002879 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002880 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002881 else
2882 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002883 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002884 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002886failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002887 emsg(_(e_missing_close));
2888 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889}
2890
2891/*
2892 * Compile a function call: name(arg1, arg2)
2893 * "arg" points to "name", "arg + varlen" to the "(".
2894 * "argcount_init" is 1 for "value->method()"
2895 * Instructions:
2896 * EVAL arg1
2897 * EVAL arg2
2898 * BCALL / DCALL / UCALL
2899 */
2900 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002901compile_call(
2902 char_u **arg,
2903 size_t varlen,
2904 cctx_T *cctx,
2905 ppconst_T *ppconst,
2906 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907{
2908 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002909 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910 int argcount = argcount_init;
2911 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002912 char_u fname_buf[FLEN_FIXED + 1];
2913 char_u *tofree = NULL;
2914 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002915 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002916 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002917 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918
Bram Moolenaara5565e42020-05-09 15:44:01 +02002919 // we can evaluate "has('name')" at compile time
2920 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2921 {
2922 char_u *s = skipwhite(*arg + varlen + 1);
2923 typval_T argvars[2];
2924
2925 argvars[0].v_type = VAR_UNKNOWN;
2926 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002927 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002928 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002929 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002930 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002931 if (*s == ')' && argvars[0].v_type == VAR_STRING
2932 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002933 {
2934 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2935
2936 *arg = s + 1;
2937 argvars[1].v_type = VAR_UNKNOWN;
2938 tv->v_type = VAR_NUMBER;
2939 tv->vval.v_number = 0;
2940 f_has(argvars, tv);
2941 clear_tv(&argvars[0]);
2942 ++ppconst->pp_used;
2943 return OK;
2944 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002945 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002946 }
2947
2948 if (generate_ppconst(cctx, ppconst) == FAIL)
2949 return FAIL;
2950
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951 if (varlen >= sizeof(namebuf))
2952 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002953 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002954 return FAIL;
2955 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002956 vim_strncpy(namebuf, *arg, varlen);
2957 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958
2959 *arg = skipwhite(*arg + varlen + 1);
2960 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002961 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962
Bram Moolenaar03290b82020-12-19 16:30:44 +01002963 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002964 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 {
2966 int idx;
2967
2968 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002969 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002971 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01002972 if (STRCMP(name, "flatten") == 0)
2973 {
2974 emsg(_(e_cannot_use_flatten_in_vim9_script));
2975 goto theend;
2976 }
2977
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002978 if (STRCMP(name, "add") == 0 && argcount == 2)
2979 {
2980 garray_T *stack = &cctx->ctx_type_stack;
2981 type_T *type = ((type_T **)stack->ga_data)[
2982 stack->ga_len - 2];
2983
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002984 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002985 if (type->tt_type == VAR_LIST)
2986 {
2987 // inline "add(list, item)" so that the type can be checked
2988 res = generate_LISTAPPEND(cctx);
2989 idx = -1;
2990 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002991 else if (type->tt_type == VAR_BLOB)
2992 {
2993 // inline "add(blob, nr)" so that the type can be checked
2994 res = generate_BLOBAPPEND(cctx);
2995 idx = -1;
2996 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002997 }
2998
2999 if (idx >= 0)
3000 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3001 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003002 else
3003 semsg(_(e_unknownfunc), namebuf);
3004 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 }
3006
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003007 // An argument or local variable can be a function reference, this
3008 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003009 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003010 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003011 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003012 // If we can find the function by name generate the right call.
3013 // Skip global functions here, a local funcref takes precedence.
3014 ufunc = find_func(name, FALSE, cctx);
3015 if (ufunc != NULL && !func_is_global(ufunc))
3016 {
3017 res = generate_CALL(cctx, ufunc, argcount);
3018 goto theend;
3019 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003020 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021
3022 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003023 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003024 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003026 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003027 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003028 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003029 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003030 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003031
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003032 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003033 goto theend;
3034 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035
Bram Moolenaar0f769812020-09-12 18:32:34 +02003036 // If we can find a global function by name generate the right call.
3037 if (ufunc != NULL)
3038 {
3039 res = generate_CALL(cctx, ufunc, argcount);
3040 goto theend;
3041 }
3042
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003043 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003044 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003045 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003046 res = generate_UCALL(cctx, name, argcount);
3047 else
3048 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003049
3050theend:
3051 vim_free(tofree);
3052 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053}
3054
3055// like NAMESPACE_CHAR but with 'a' and 'l'.
3056#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3057
3058/*
3059 * Find the end of a variable or function name. Unlike find_name_end() this
3060 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003061 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062 * Return a pointer to just after the name. Equal to "arg" if there is no
3063 * valid name.
3064 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003065 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003066to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067{
3068 char_u *p;
3069
3070 // Quick check for valid starting character.
3071 if (!eval_isnamec1(*arg))
3072 return arg;
3073
3074 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3075 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3076 // and can be used in slice "[n:]".
3077 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003078 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3080 break;
3081 return p;
3082}
3083
3084/*
3085 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003086 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087 */
3088 char_u *
3089to_name_const_end(char_u *arg)
3090{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003091 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092 typval_T rettv;
3093
3094 if (p == arg && *arg == '[')
3095 {
3096
3097 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003098 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099 p = arg;
3100 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 return p;
3102}
3103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104/*
3105 * parse a list: [expr, expr]
3106 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003107 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 */
3109 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003110compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111{
3112 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003113 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003115 int is_const;
3116 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003118 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003119 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003120 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003121 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003122 semsg(_(e_list_end), *arg);
3123 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003124 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003125 if (*p == ',')
3126 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003127 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003128 return FAIL;
3129 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003130 if (*p == ']')
3131 {
3132 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003133 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003134 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003135 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003136 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003137 if (!is_const)
3138 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139 ++count;
3140 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003141 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003143 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3144 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003145 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003146 return FAIL;
3147 }
3148 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003149 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 p = skipwhite(p);
3151 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003152 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003153
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003154 ppconst->pp_is_const = is_all_const;
3155 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156}
3157
3158/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003159 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160 * "*arg" points to the '{'.
Bram Moolenaare462f522020-12-27 14:43:30 +01003161 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 */
3163 static int
3164compile_lambda(char_u **arg, cctx_T *cctx)
3165{
Bram Moolenaare462f522020-12-27 14:43:30 +01003166 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 typval_T rettv;
3168 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003169 evalarg_T evalarg;
3170
3171 CLEAR_FIELD(evalarg);
3172 evalarg.eval_flags = EVAL_EVALUATE;
3173 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174
3175 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003176 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3177 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003178 {
3179 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003180 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003181 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003182
Bram Moolenaar65c44152020-12-24 15:14:01 +01003183 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003185 ++ufunc->uf_refcount;
3186 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187
Bram Moolenaar65c44152020-12-24 15:14:01 +01003188 // Compile the function into instructions.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01003189 compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003190
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003191 clear_evalarg(&evalarg, NULL);
3192
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003193 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003194 {
3195 // The return type will now be known.
3196 set_function_type(ufunc);
3197
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003198 // The function reference count will be 1. When the ISN_FUNCREF
3199 // instruction is deleted the reference count is decremented and the
3200 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003201 return generate_FUNCREF(cctx, ufunc);
3202 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003203
3204 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 return FAIL;
3206}
3207
3208/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003209 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003211 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 */
3213 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003214compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215{
3216 garray_T *instr = &cctx->ctx_instr;
3217 int count = 0;
3218 dict_T *d = dict_alloc();
3219 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003220 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003221 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003222 int is_const;
3223 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224
3225 if (d == NULL)
3226 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003227 if (generate_ppconst(cctx, ppconst) == FAIL)
3228 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003229 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003231 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232
Bram Moolenaar23c55272020-06-21 16:58:13 +02003233 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003234 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003235 *arg = NULL;
3236 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003237 }
3238
3239 if (**arg == '}')
3240 break;
3241
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003242 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003244 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003246 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003247 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003248 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003251 if (isn->isn_type == ISN_PUSHNR)
3252 {
3253 char buf[NUMBUFLEN];
3254
3255 // Convert to string at compile time.
3256 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3257 isn->isn_type = ISN_PUSHS;
3258 isn->isn_arg.string = vim_strsave((char_u *)buf);
3259 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260 if (isn->isn_type == ISN_PUSHS)
3261 key = isn->isn_arg.string;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003262 else if (may_generate_2STRING(-1, cctx) == FAIL)
3263 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003264 *arg = skipwhite(*arg);
3265 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003266 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003267 emsg(_(e_missing_matching_bracket_after_dict_key));
3268 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003269 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003270 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003272 else
3273 {
3274 // {"name": value},
3275 // {'name': value},
3276 // {name: value} use "name" as a literal key
3277 key = get_literal_key(arg);
3278 if (key == NULL)
3279 return FAIL;
3280 if (generate_PUSHS(cctx, key) == FAIL)
3281 return FAIL;
3282 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003283
3284 // Check for duplicate keys, if using string keys.
3285 if (key != NULL)
3286 {
3287 item = dict_find(d, key, -1);
3288 if (item != NULL)
3289 {
3290 semsg(_(e_duplicate_key), key);
3291 goto failret;
3292 }
3293 item = dictitem_alloc(key);
3294 if (item != NULL)
3295 {
3296 item->di_tv.v_type = VAR_UNKNOWN;
3297 item->di_tv.v_lock = 0;
3298 if (dict_add(d, item) == FAIL)
3299 dictitem_free(item);
3300 }
3301 }
3302
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 if (**arg != ':')
3304 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003305 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003306 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003307 else
3308 semsg(_(e_missing_dict_colon), *arg);
3309 return FAIL;
3310 }
3311 whitep = *arg + 1;
3312 if (!IS_WHITE_OR_NUL(*whitep))
3313 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003314 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003315 return FAIL;
3316 }
3317
Bram Moolenaar23c55272020-06-21 16:58:13 +02003318 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003319 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003320 *arg = NULL;
3321 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003322 }
3323
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003324 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003325 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003326 if (!is_const)
3327 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003328 ++count;
3329
Bram Moolenaar2c330432020-04-13 14:41:35 +02003330 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003331 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003332 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003333 *arg = NULL;
3334 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003335 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003336 if (**arg == '}')
3337 break;
3338 if (**arg != ',')
3339 {
3340 semsg(_(e_missing_dict_comma), *arg);
3341 goto failret;
3342 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003343 if (IS_WHITE_OR_NUL(*whitep))
3344 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003345 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003346 return FAIL;
3347 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003348 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003349 if (!IS_WHITE_OR_NUL(*whitep))
3350 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003351 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003352 return FAIL;
3353 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003354 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 }
3356
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357 *arg = *arg + 1;
3358
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003359 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003360 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003361 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003362 *arg += STRLEN(*arg);
3363
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003365 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003366 return generate_NEWDICT(cctx, count);
3367
3368failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003369 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003370 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003371 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003372 *arg = (char_u *)"";
3373 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374 dict_unref(d);
3375 return FAIL;
3376}
3377
3378/*
3379 * Compile "&option".
3380 */
3381 static int
3382compile_get_option(char_u **arg, cctx_T *cctx)
3383{
3384 typval_T rettv;
3385 char_u *start = *arg;
3386 int ret;
3387
3388 // parse the option and get the current value to get the type.
3389 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003390 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 if (ret == OK)
3392 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003393 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003394 char_u *name = vim_strnsave(start, *arg - start);
3395 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3396 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397
3398 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3399 vim_free(name);
3400 }
3401 clear_tv(&rettv);
3402
3403 return ret;
3404}
3405
3406/*
3407 * Compile "$VAR".
3408 */
3409 static int
3410compile_get_env(char_u **arg, cctx_T *cctx)
3411{
3412 char_u *start = *arg;
3413 int len;
3414 int ret;
3415 char_u *name;
3416
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417 ++*arg;
3418 len = get_env_len(arg);
3419 if (len == 0)
3420 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003421 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 return FAIL;
3423 }
3424
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003425 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 name = vim_strnsave(start, len + 1);
3427 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3428 vim_free(name);
3429 return ret;
3430}
3431
3432/*
3433 * Compile "@r".
3434 */
3435 static int
3436compile_get_register(char_u **arg, cctx_T *cctx)
3437{
3438 int ret;
3439
3440 ++*arg;
3441 if (**arg == NUL)
3442 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003443 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003444 return FAIL;
3445 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003446 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447 {
3448 emsg_invreg(**arg);
3449 return FAIL;
3450 }
3451 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3452 ++*arg;
3453 return ret;
3454}
3455
3456/*
3457 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003458 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003459 */
3460 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003461apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003462{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003463 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003464
3465 // this works from end to start
3466 while (p > start)
3467 {
3468 --p;
3469 if (*p == '-' || *p == '+')
3470 {
3471 // only '-' has an effect, for '+' we only check the type
3472#ifdef FEAT_FLOAT
3473 if (rettv->v_type == VAR_FLOAT)
3474 {
3475 if (*p == '-')
3476 rettv->vval.v_float = -rettv->vval.v_float;
3477 }
3478 else
3479#endif
3480 {
3481 varnumber_T val;
3482 int error = FALSE;
3483
3484 // tv_get_number_chk() accepts a string, but we don't want that
3485 // here
3486 if (check_not_string(rettv) == FAIL)
3487 return FAIL;
3488 val = tv_get_number_chk(rettv, &error);
3489 clear_tv(rettv);
3490 if (error)
3491 return FAIL;
3492 if (*p == '-')
3493 val = -val;
3494 rettv->v_type = VAR_NUMBER;
3495 rettv->vval.v_number = val;
3496 }
3497 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003498 else if (numeric_only)
3499 {
3500 ++p;
3501 break;
3502 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003503 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 {
3505 int v = tv2bool(rettv);
3506
3507 // '!' is permissive in the type.
3508 clear_tv(rettv);
3509 rettv->v_type = VAR_BOOL;
3510 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3511 }
3512 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003513 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514 return OK;
3515}
3516
3517/*
3518 * Recognize v: variables that are constants and set "rettv".
3519 */
3520 static void
3521get_vim_constant(char_u **arg, typval_T *rettv)
3522{
3523 if (STRNCMP(*arg, "v:true", 6) == 0)
3524 {
3525 rettv->v_type = VAR_BOOL;
3526 rettv->vval.v_number = VVAL_TRUE;
3527 *arg += 6;
3528 }
3529 else if (STRNCMP(*arg, "v:false", 7) == 0)
3530 {
3531 rettv->v_type = VAR_BOOL;
3532 rettv->vval.v_number = VVAL_FALSE;
3533 *arg += 7;
3534 }
3535 else if (STRNCMP(*arg, "v:null", 6) == 0)
3536 {
3537 rettv->v_type = VAR_SPECIAL;
3538 rettv->vval.v_number = VVAL_NULL;
3539 *arg += 6;
3540 }
3541 else if (STRNCMP(*arg, "v:none", 6) == 0)
3542 {
3543 rettv->v_type = VAR_SPECIAL;
3544 rettv->vval.v_number = VVAL_NONE;
3545 *arg += 6;
3546 }
3547}
3548
Bram Moolenaar657137c2021-01-09 15:45:23 +01003549 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003550get_compare_type(char_u *p, int *len, int *type_is)
3551{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003552 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003553 int i;
3554
3555 switch (p[0])
3556 {
3557 case '=': if (p[1] == '=')
3558 type = EXPR_EQUAL;
3559 else if (p[1] == '~')
3560 type = EXPR_MATCH;
3561 break;
3562 case '!': if (p[1] == '=')
3563 type = EXPR_NEQUAL;
3564 else if (p[1] == '~')
3565 type = EXPR_NOMATCH;
3566 break;
3567 case '>': if (p[1] != '=')
3568 {
3569 type = EXPR_GREATER;
3570 *len = 1;
3571 }
3572 else
3573 type = EXPR_GEQUAL;
3574 break;
3575 case '<': if (p[1] != '=')
3576 {
3577 type = EXPR_SMALLER;
3578 *len = 1;
3579 }
3580 else
3581 type = EXPR_SEQUAL;
3582 break;
3583 case 'i': if (p[1] == 's')
3584 {
3585 // "is" and "isnot"; but not a prefix of a name
3586 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3587 *len = 5;
3588 i = p[*len];
3589 if (!isalnum(i) && i != '_')
3590 {
3591 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3592 *type_is = TRUE;
3593 }
3594 }
3595 break;
3596 }
3597 return type;
3598}
3599
3600/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003601 * Skip over an expression, ignoring most errors.
3602 */
3603 static void
3604skip_expr_cctx(char_u **arg, cctx_T *cctx)
3605{
3606 evalarg_T evalarg;
3607
3608 CLEAR_FIELD(evalarg);
3609 evalarg.eval_cctx = cctx;
3610 skip_expr(arg, &evalarg);
3611}
3612
3613/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003615 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003616 */
3617 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003618compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003620 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621
3622 // this works from end to start
3623 while (p > start)
3624 {
3625 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003626 while (VIM_ISWHITE(*p))
3627 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 if (*p == '-' || *p == '+')
3629 {
3630 int negate = *p == '-';
3631 isn_T *isn;
3632
3633 // TODO: check type
3634 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3635 {
3636 --p;
3637 if (*p == '-')
3638 negate = !negate;
3639 }
3640 // only '-' has an effect, for '+' we only check the type
3641 if (negate)
3642 isn = generate_instr(cctx, ISN_NEGATENR);
3643 else
3644 isn = generate_instr(cctx, ISN_CHECKNR);
3645 if (isn == NULL)
3646 return FAIL;
3647 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003648 else if (numeric_only)
3649 {
3650 ++p;
3651 break;
3652 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653 else
3654 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003655 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003657 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003659 if (p[-1] == '!')
3660 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003662 }
3663 if (generate_2BOOL(cctx, invert) == FAIL)
3664 return FAIL;
3665 }
3666 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003667 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668 return OK;
3669}
3670
3671/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003672 * Compile "(expression)": recursive!
3673 * Return FAIL/OK.
3674 */
3675 static int
3676compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3677{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003678 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003679 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003680
Bram Moolenaar24156692021-01-14 20:35:49 +01003681 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3682 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003683 if (ppconst->pp_used <= PPSIZE - 10)
3684 {
3685 ret = compile_expr1(arg, cctx, ppconst);
3686 }
3687 else
3688 {
3689 // Not enough space in ppconst, flush constants.
3690 if (generate_ppconst(cctx, ppconst) == FAIL)
3691 return FAIL;
3692 ret = compile_expr0(arg, cctx);
3693 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003694 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3695 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003696 if (**arg == ')')
3697 ++*arg;
3698 else if (ret == OK)
3699 {
3700 emsg(_(e_missing_close));
3701 ret = FAIL;
3702 }
3703 return ret;
3704}
3705
3706/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003708 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003709 */
3710 static int
3711compile_subscript(
3712 char_u **arg,
3713 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003714 char_u *start_leader,
3715 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003716 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003718 char_u *name_start = *end_leader;
3719
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720 for (;;)
3721 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003722 char_u *p = skipwhite(*arg);
3723
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003724 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003725 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003726 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003727
3728 // If a following line starts with "->{" or "->X" advance to that
3729 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003730 // Also if a following line starts with ".x".
3731 if (next != NULL &&
3732 ((next[0] == '-' && next[1] == '>'
3733 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003734 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003735 {
3736 next = next_line_from_context(cctx, TRUE);
3737 if (next == NULL)
3738 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003739 *arg = next;
3740 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003741 }
3742 }
3743
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003744 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003745 // not a function call.
3746 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003748 garray_T *stack = &cctx->ctx_type_stack;
3749 type_T *type;
3750 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003751
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003752 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003753 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003754 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003755
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003757 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3758
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003759 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3761 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003762 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003763 return FAIL;
3764 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003765 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003767 char_u *pstart = p;
3768
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003769 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003770 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003771 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003772
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773 // something->method()
3774 // Apply the '!', '-' and '+' first:
3775 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003776 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003779 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003780 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003781 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003782 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003783 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003784 int argcount = 1;
3785 char_u *expr;
3786 garray_T *stack;
3787 type_T *type;
3788
3789 // Funcref call: list->(Refs[2])(arg)
3790 // or lambda: list->((arg) => expr)(arg)
3791 // Fist compile the arguments.
3792 expr = *arg;
3793 *arg = skipwhite(*arg + 1);
3794 skip_expr_cctx(arg, cctx);
3795 *arg = skipwhite(*arg);
3796 if (**arg != ')')
3797 {
3798 semsg(_(e_missing_paren), *arg);
3799 return FAIL;
3800 }
3801 ++*arg;
3802 if (**arg != '(')
3803 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003804 if (*skipwhite(*arg) == '(')
3805 emsg(_(e_nowhitespace));
3806 else
3807 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01003808 return FAIL;
3809 }
3810
3811 *arg = skipwhite(*arg + 1);
3812 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3813 return FAIL;
3814
3815 // Compile the function expression.
3816 if (compile_parenthesis(&expr, cctx, ppconst) == FAIL)
3817 return FAIL;
3818
3819 stack = &cctx->ctx_type_stack;
3820 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3821 if (generate_PCALL(cctx, argcount,
3822 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 return FAIL;
3824 }
3825 else
3826 {
3827 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003828 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003829 if (!eval_isnamec1(*p))
3830 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003831 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003832 return FAIL;
3833 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003834 if (ASCII_ISALPHA(*p) && p[1] == ':')
3835 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003836 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837 ;
3838 if (*p != '(')
3839 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003840 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 return FAIL;
3842 }
3843 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003844 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 return FAIL;
3846 }
3847 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003848 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003850 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003851 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003852 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003853 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003854 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003855
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003856 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003857 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003858 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003859 // TODO: blob index
3860 // TODO: more arguments
3861 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003862 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003863 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003864 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003865
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003866 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003867 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003868 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003869 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003870 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003871 // missing first index is equal to zero
3872 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003873 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003874 else
3875 {
3876 if (compile_expr0(arg, cctx) == FAIL)
3877 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003878 if (**arg == ':')
3879 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003880 semsg(_(e_white_space_required_before_and_after_str_at_str),
3881 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003882 return FAIL;
3883 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003884 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003885 return FAIL;
3886 *arg = skipwhite(*arg);
3887 }
3888 if (**arg == ':')
3889 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003890 is_slice = TRUE;
3891 ++*arg;
3892 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
3893 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003894 semsg(_(e_white_space_required_before_and_after_str_at_str),
3895 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003896 return FAIL;
3897 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003898 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003899 return FAIL;
3900 if (**arg == ']')
3901 // missing second index is equal to end of string
3902 generate_PUSHNR(cctx, -1);
3903 else
3904 {
3905 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003906 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003907 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003908 return FAIL;
3909 *arg = skipwhite(*arg);
3910 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003911 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912
3913 if (**arg != ']')
3914 {
3915 emsg(_(e_missbrac));
3916 return FAIL;
3917 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003918 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003920 // We can index a list and a dict. If we don't know the type
3921 // we can use the index value type.
3922 // TODO: If we don't know use an instruction to figure it out at
3923 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003924 typep = ((type_T **)stack->ga_data) + stack->ga_len
3925 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003926 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003927 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3928 // If the index is a string, the variable must be a Dict.
3929 if (*typep == &t_any && valtype == &t_string)
3930 vtype = VAR_DICT;
3931 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003932 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003933 if (need_type(valtype, &t_number, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003934 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003935 return FAIL;
3936 if (is_slice)
3937 {
3938 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar351ead02021-01-16 16:07:01 +01003939 if (need_type(valtype, &t_number, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003940 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003941 return FAIL;
3942 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003943 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003944
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003945 if (vtype == VAR_DICT)
3946 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003947 if (is_slice)
3948 {
3949 emsg(_(e_cannot_slice_dictionary));
3950 return FAIL;
3951 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003952 if ((*typep)->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003953 {
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003954 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003955 if (*typep == &t_unknown)
3956 // empty dict was used
3957 *typep = &t_any;
3958 }
Bram Moolenaar7892b952020-07-20 22:09:34 +02003959 else
3960 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003961 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003962 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003963 return FAIL;
3964 *typep = &t_any;
3965 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003966 if (may_generate_2STRING(-1, cctx) == FAIL)
3967 return FAIL;
3968 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3969 return FAIL;
3970 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003971 else if (vtype == VAR_STRING)
3972 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003973 *typep = &t_string;
3974 if ((is_slice
3975 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3976 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003977 return FAIL;
3978 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003979 else if (vtype == VAR_BLOB)
3980 {
3981 emsg("Sorry, blob index and slice not implemented yet");
3982 return FAIL;
3983 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003984 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003985 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003986 if (is_slice)
3987 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003988 if (generate_instr_drop(cctx,
3989 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3990 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003991 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003992 }
Bram Moolenaared591872020-08-15 22:14:53 +02003993 else
3994 {
3995 if ((*typep)->tt_type == VAR_LIST)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003996 {
Bram Moolenaared591872020-08-15 22:14:53 +02003997 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003998 if (*typep == &t_unknown)
3999 // empty list was used
4000 *typep = &t_any;
4001 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004002 if (generate_instr_drop(cctx,
4003 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
4004 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02004005 return FAIL;
4006 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004007 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004008 else
4009 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02004010 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01004011 return FAIL;
4012 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004013 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004014 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004015 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004016 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004017 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004018 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004019 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004020
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004021 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004022 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004023 {
4024 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004025 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004026 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004027 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004028 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029 while (eval_isnamec(*p))
4030 MB_PTR_ADV(p);
4031 if (p == *arg)
4032 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004033 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034 return FAIL;
4035 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004036 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004037 return FAIL;
4038 *arg = p;
4039 }
4040 else
4041 break;
4042 }
4043
4044 // TODO - see handle_subscript():
4045 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4046 // Don't do this when "Func" is already a partial that was bound
4047 // explicitly (pt_auto is FALSE).
4048
4049 return OK;
4050}
4051
4052/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004053 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4054 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004056 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004057 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004058 *
4059 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060 */
4061
4062/*
4063 * number number constant
4064 * 0zFFFFFFFF Blob constant
4065 * "string" string constant
4066 * 'string' literal string constant
4067 * &option-name option value
4068 * @r register contents
4069 * identifier variable value
4070 * function() function call
4071 * $VAR environment variable
4072 * (expression) nested expression
4073 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004074 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075 *
4076 * Also handle:
4077 * ! in front logical NOT
4078 * - in front unary minus
4079 * + in front unary plus (ignored)
4080 * trailing (arg) funcref/partial call
4081 * trailing [] subscript in String or List
4082 * trailing .name entry in Dictionary
4083 * trailing ->name() method call
4084 */
4085 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004086compile_expr7(
4087 char_u **arg,
4088 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004089 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091 char_u *start_leader, *end_leader;
4092 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004093 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004094 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004096 ppconst->pp_is_const = FALSE;
4097
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098 /*
4099 * Skip '!', '-' and '+' characters. They are handled later.
4100 */
4101 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004102 if (eval_leader(arg, TRUE) == FAIL)
4103 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104 end_leader = *arg;
4105
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004106 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107 switch (**arg)
4108 {
4109 /*
4110 * Number constant.
4111 */
4112 case '0': // also for blob starting with 0z
4113 case '1':
4114 case '2':
4115 case '3':
4116 case '4':
4117 case '5':
4118 case '6':
4119 case '7':
4120 case '8':
4121 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004122 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004124 // Apply "-" and "+" just before the number now, right to
4125 // left. Matters especially when "->" follows. Stops at
4126 // '!'.
4127 if (apply_leader(rettv, TRUE,
4128 start_leader, &end_leader) == FAIL)
4129 {
4130 clear_tv(rettv);
4131 return FAIL;
4132 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 break;
4134
4135 /*
4136 * String constant: "string".
4137 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004138 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139 return FAIL;
4140 break;
4141
4142 /*
4143 * Literal string constant: 'str''ing'.
4144 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004145 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146 return FAIL;
4147 break;
4148
4149 /*
4150 * Constant Vim variable.
4151 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004152 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004153 ret = NOTDONE;
4154 break;
4155
4156 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004157 * "true" constant
4158 */
4159 case 't': if (STRNCMP(*arg, "true", 4) == 0
4160 && !eval_isnamec((*arg)[4]))
4161 {
4162 *arg += 4;
4163 rettv->v_type = VAR_BOOL;
4164 rettv->vval.v_number = VVAL_TRUE;
4165 }
4166 else
4167 ret = NOTDONE;
4168 break;
4169
4170 /*
4171 * "false" constant
4172 */
4173 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4174 && !eval_isnamec((*arg)[5]))
4175 {
4176 *arg += 5;
4177 rettv->v_type = VAR_BOOL;
4178 rettv->vval.v_number = VVAL_FALSE;
4179 }
4180 else
4181 ret = NOTDONE;
4182 break;
4183
4184 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004185 * "null" constant
4186 */
4187 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004188 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004189 {
4190 *arg += 4;
4191 rettv->v_type = VAR_SPECIAL;
4192 rettv->vval.v_number = VVAL_NULL;
4193 }
4194 else
4195 ret = NOTDONE;
4196 break;
4197
4198 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004199 * List: [expr, expr]
4200 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004201 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4202 return FAIL;
4203 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204 break;
4205
4206 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207 * Dictionary: {'key': val, 'key': val}
4208 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004209 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4210 return FAIL;
4211 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212 break;
4213
4214 /*
4215 * Option value: &name
4216 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004217 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4218 return FAIL;
4219 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 break;
4221
4222 /*
4223 * Environment variable: $VAR.
4224 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004225 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4226 return FAIL;
4227 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 break;
4229
4230 /*
4231 * Register contents: @r.
4232 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004233 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4234 return FAIL;
4235 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236 break;
4237 /*
4238 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004239 * lambda: (arg, arg) => expr
4240 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004242 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4243 ret = compile_lambda(arg, cctx);
4244 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004245 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004246 break;
4247
4248 default: ret = NOTDONE;
4249 break;
4250 }
4251 if (ret == FAIL)
4252 return FAIL;
4253
Bram Moolenaar1c747212020-05-09 18:28:34 +02004254 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004256 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004257 clear_tv(rettv);
4258 else
4259 // A constant expression can possibly be handled compile time,
4260 // return the value instead of generating code.
4261 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004262 }
4263 else if (ret == NOTDONE)
4264 {
4265 char_u *p;
4266 int r;
4267
4268 if (!eval_isnamec1(**arg))
4269 {
Bram Moolenaare4984292020-12-13 14:19:25 +01004270 if (ends_excmd(*skipwhite(*arg)))
4271 semsg(_(e_empty_expression_str), *arg);
4272 else
4273 semsg(_(e_name_expected_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274 return FAIL;
4275 }
4276
4277 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004278 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004280 {
4281 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4282 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004284 {
4285 if (generate_ppconst(cctx, ppconst) == FAIL)
4286 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004287 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004288 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004289 if (r == FAIL)
4290 return FAIL;
4291 }
4292
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004293 // Handle following "[]", ".member", etc.
4294 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004295 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004296 ppconst) == FAIL)
4297 return FAIL;
4298 if (ppconst->pp_used > 0)
4299 {
4300 // apply the '!', '-' and '+' before the constant
4301 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004302 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004303 return FAIL;
4304 return OK;
4305 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004306 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004308 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004309}
4310
4311/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004312 * Give the "white on both sides" error, taking the operator from "p[len]".
4313 */
4314 void
4315error_white_both(char_u *op, int len)
4316{
4317 char_u buf[10];
4318
4319 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004320 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004321}
4322
4323/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004324 * <type>expr7: runtime type check / conversion
4325 */
4326 static int
4327compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4328{
4329 type_T *want_type = NULL;
4330
4331 // Recognize <type>
4332 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4333 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004334 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004335 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4336 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004337 return FAIL;
4338
4339 if (**arg != '>')
4340 {
4341 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004342 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004343 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004344 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004345 return FAIL;
4346 }
4347 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004348 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004349 return FAIL;
4350 }
4351
4352 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4353 return FAIL;
4354
4355 if (want_type != NULL)
4356 {
4357 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004358 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004359 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004360
Bram Moolenaard1103582020-08-14 22:44:25 +02004361 generate_ppconst(cctx, ppconst);
4362 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004363 where.wt_index = 0;
4364 where.wt_variable = FALSE;
4365 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004366 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004367 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004368 return FAIL;
4369 }
4370 }
4371
4372 return OK;
4373}
4374
4375/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 * * number multiplication
4377 * / number division
4378 * % number modulo
4379 */
4380 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004381compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004382{
4383 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004384 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004385 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004387 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004388 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 return FAIL;
4390
4391 /*
4392 * Repeat computing, until no "*", "/" or "%" is following.
4393 */
4394 for (;;)
4395 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004396 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004397 if (*op != '*' && *op != '/' && *op != '%')
4398 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004399 if (next != NULL)
4400 {
4401 *arg = next_line_from_context(cctx, TRUE);
4402 op = skipwhite(*arg);
4403 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004404
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004405 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004407 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004408 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004410 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004411 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004413 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004414 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004415 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004416
4417 if (ppconst->pp_used == ppconst_used + 2
4418 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4419 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004420 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004421 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4422 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4423 varnumber_T res = 0;
4424 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004426 // both are numbers: compute the result
4427 switch (*op)
4428 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004429 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004430 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004431 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004432 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004433 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004434 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004435 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004436 break;
4437 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004438 if (failed)
4439 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004440 tv1->vval.v_number = res;
4441 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004442 }
4443 else
4444 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004445 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004446 generate_two_op(cctx, op);
4447 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 }
4449
4450 return OK;
4451}
4452
4453/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004454 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004455 * - number subtraction
4456 * .. string concatenation
4457 */
4458 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004459compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004460{
4461 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004462 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004463 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004464 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004465
4466 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004467 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468 return FAIL;
4469
4470 /*
4471 * Repeat computing, until no "+", "-" or ".." is following.
4472 */
4473 for (;;)
4474 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004475 op = may_peek_next_line(cctx, *arg, &next);
4476 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004477 break;
4478 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004479 if (next != NULL)
4480 {
4481 *arg = next_line_from_context(cctx, TRUE);
4482 op = skipwhite(*arg);
4483 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004485 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004486 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004487 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004488 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489 }
4490
Bram Moolenaare0de1712020-12-02 17:36:54 +01004491 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004492 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004493
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004494 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004495 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004496 return FAIL;
4497
Bram Moolenaara5565e42020-05-09 15:44:01 +02004498 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004499 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004500 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4501 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4502 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4503 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004504 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004505 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4506 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004507
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004508 // concat/subtract/add constant numbers
4509 if (*op == '+')
4510 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4511 else if (*op == '-')
4512 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4513 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004514 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004515 // concatenate constant strings
4516 char_u *s1 = tv1->vval.v_string;
4517 char_u *s2 = tv2->vval.v_string;
4518 size_t len1 = STRLEN(s1);
4519
4520 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4521 if (tv1->vval.v_string == NULL)
4522 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004523 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004524 return FAIL;
4525 }
4526 mch_memmove(tv1->vval.v_string, s1, len1);
4527 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004528 vim_free(s1);
4529 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004530 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004531 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004532 }
4533 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004534 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004535 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004536 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004537 if (*op == '.')
4538 {
4539 if (may_generate_2STRING(-2, cctx) == FAIL
4540 || may_generate_2STRING(-1, cctx) == FAIL)
4541 return FAIL;
4542 generate_instr_drop(cctx, ISN_CONCAT, 1);
4543 }
4544 else
4545 generate_two_op(cctx, op);
4546 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004547 }
4548
4549 return OK;
4550}
4551
4552/*
4553 * expr5a == expr5b
4554 * expr5a =~ expr5b
4555 * expr5a != expr5b
4556 * expr5a !~ expr5b
4557 * expr5a > expr5b
4558 * expr5a >= expr5b
4559 * expr5a < expr5b
4560 * expr5a <= expr5b
4561 * expr5a is expr5b
4562 * expr5a isnot expr5b
4563 *
4564 * Produces instructions:
4565 * EVAL expr5a Push result of "expr5a"
4566 * EVAL expr5b Push result of "expr5b"
4567 * COMPARE one of the compare instructions
4568 */
4569 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004570compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004572 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004574 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004577 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004578
4579 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004580 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004581 return FAIL;
4582
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004583 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004584 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585
4586 /*
4587 * If there is a comparative operator, use it.
4588 */
4589 if (type != EXPR_UNKNOWN)
4590 {
4591 int ic = FALSE; // Default: do not ignore case
4592
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004593 if (next != NULL)
4594 {
4595 *arg = next_line_from_context(cctx, TRUE);
4596 p = skipwhite(*arg);
4597 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 if (type_is && (p[len] == '?' || p[len] == '#'))
4599 {
4600 semsg(_(e_invexpr2), *arg);
4601 return FAIL;
4602 }
4603 // extra question mark appended: ignore case
4604 if (p[len] == '?')
4605 {
4606 ic = TRUE;
4607 ++len;
4608 }
4609 // extra '#' appended: match case (ignored)
4610 else if (p[len] == '#')
4611 ++len;
4612 // nothing appended: match case
4613
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004614 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004615 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004616 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004617 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618 }
4619
4620 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004621 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004622 return FAIL;
4623
Bram Moolenaara5565e42020-05-09 15:44:01 +02004624 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004625 return FAIL;
4626
Bram Moolenaara5565e42020-05-09 15:44:01 +02004627 if (ppconst->pp_used == ppconst_used + 2)
4628 {
4629 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4630 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4631 int ret;
4632
4633 // Both sides are a constant, compute the result now.
4634 // First check for a valid combination of types, this is more
4635 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004636 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004637 ret = FAIL;
4638 else
4639 {
4640 ret = typval_compare(tv1, tv2, type, ic);
4641 tv1->v_type = VAR_BOOL;
4642 tv1->vval.v_number = tv1->vval.v_number
4643 ? VVAL_TRUE : VVAL_FALSE;
4644 clear_tv(tv2);
4645 --ppconst->pp_used;
4646 }
4647 return ret;
4648 }
4649
4650 generate_ppconst(cctx, ppconst);
4651 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652 }
4653
4654 return OK;
4655}
4656
Bram Moolenaar7f141552020-05-09 17:35:53 +02004657static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4658
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004659/*
4660 * Compile || or &&.
4661 */
4662 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004663compile_and_or(
4664 char_u **arg,
4665 cctx_T *cctx,
4666 char *op,
4667 ppconst_T *ppconst,
4668 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004669{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004670 char_u *next;
4671 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004672 int opchar = *op;
4673
4674 if (p[0] == opchar && p[1] == opchar)
4675 {
4676 garray_T *instr = &cctx->ctx_instr;
4677 garray_T end_ga;
4678
4679 /*
4680 * Repeat until there is no following "||" or "&&"
4681 */
4682 ga_init2(&end_ga, sizeof(int), 10);
4683 while (p[0] == opchar && p[1] == opchar)
4684 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004685 if (next != NULL)
4686 {
4687 *arg = next_line_from_context(cctx, TRUE);
4688 p = skipwhite(*arg);
4689 }
4690
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004691 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4692 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004693 semsg(_(e_white_space_required_before_and_after_str_at_str),
4694 op, *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004695 return FAIL;
4696 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004698 // TODO: use ppconst if the value is a constant and check
4699 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004700 generate_ppconst(cctx, ppconst);
4701
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004702 // Every part must evaluate to a bool.
4703 if (bool_on_stack(cctx) == FAIL)
4704 {
4705 ga_clear(&end_ga);
4706 return FAIL;
4707 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004708
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004709 if (ga_grow(&end_ga, 1) == FAIL)
4710 {
4711 ga_clear(&end_ga);
4712 return FAIL;
4713 }
4714 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4715 ++end_ga.ga_len;
4716 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004717 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004718
4719 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004720 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004721 {
4722 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004723 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004724 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004725
Bram Moolenaara5565e42020-05-09 15:44:01 +02004726 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4727 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004728 {
4729 ga_clear(&end_ga);
4730 return FAIL;
4731 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004732
4733 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004734 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004735 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004736
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004737 // Every part must evaluate to a bool.
4738 if (bool_on_stack(cctx) == FAIL)
4739 {
4740 ga_clear(&end_ga);
4741 return FAIL;
4742 }
4743
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744 // Fill in the end label in all jumps.
4745 while (end_ga.ga_len > 0)
4746 {
4747 isn_T *isn;
4748
4749 --end_ga.ga_len;
4750 isn = ((isn_T *)instr->ga_data)
4751 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4752 isn->isn_arg.jump.jump_where = instr->ga_len;
4753 }
4754 ga_clear(&end_ga);
4755 }
4756
4757 return OK;
4758}
4759
4760/*
4761 * expr4a && expr4a && expr4a logical AND
4762 *
4763 * Produces instructions:
4764 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004765 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004766 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004767 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004768 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004769 * EVAL expr4c Push result of "expr4c"
4770 * end:
4771 */
4772 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004773compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004774{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004775 int ppconst_used = ppconst->pp_used;
4776
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004777 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004778 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004779 return FAIL;
4780
4781 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004782 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004783}
4784
4785/*
4786 * expr3a || expr3b || expr3c logical OR
4787 *
4788 * Produces instructions:
4789 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004790 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004791 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004793 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004794 * EVAL expr3c Push result of "expr3c"
4795 * end:
4796 */
4797 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004798compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004799{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004800 int ppconst_used = ppconst->pp_used;
4801
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004802 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004803 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004804 return FAIL;
4805
4806 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004807 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004808}
4809
4810/*
4811 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004812 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004813 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004814 * JUMP_IF_FALSE alt jump if false
4815 * EVAL expr1a
4816 * JUMP_ALWAYS end
4817 * alt: EVAL expr1b
4818 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004819 *
4820 * Toplevel expression: expr2 ?? expr1
4821 * Produces instructions:
4822 * EVAL expr2 Push result of "expr2"
4823 * JUMP_AND_KEEP_IF_TRUE end jump if true
4824 * EVAL expr1
4825 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004826 */
4827 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004828compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004829{
4830 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004831 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004832 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004833
Bram Moolenaar3988f642020-08-27 22:43:03 +02004834 // Ignore all kinds of errors when not producing code.
4835 if (cctx->ctx_skip == SKIP_YES)
4836 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004837 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004838 return OK;
4839 }
4840
Bram Moolenaar61a89812020-05-07 16:58:17 +02004841 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004842 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004843 return FAIL;
4844
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004845 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004846 if (*p == '?')
4847 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004848 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004849 garray_T *instr = &cctx->ctx_instr;
4850 garray_T *stack = &cctx->ctx_type_stack;
4851 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004852 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004853 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004854 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004855 int has_const_expr = FALSE;
4856 int const_value = FALSE;
4857 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004858
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004859 if (next != NULL)
4860 {
4861 *arg = next_line_from_context(cctx, TRUE);
4862 p = skipwhite(*arg);
4863 }
4864
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004865 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004866 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004867 semsg(_(e_white_space_required_before_and_after_str_at_str),
4868 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004869 return FAIL;
4870 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004871
Bram Moolenaara5565e42020-05-09 15:44:01 +02004872 if (ppconst->pp_used == ppconst_used + 1)
4873 {
4874 // the condition is a constant, we know whether the ? or the :
4875 // expression is to be evaluated.
4876 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004877 if (op_falsy)
4878 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4879 else
4880 {
4881 int error = FALSE;
4882
4883 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4884 &error);
4885 if (error)
4886 return FAIL;
4887 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004888 cctx->ctx_skip = save_skip == SKIP_YES ||
4889 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4890
4891 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4892 // "left ?? right" and "left" is truthy: produce "left"
4893 generate_ppconst(cctx, ppconst);
4894 else
4895 {
4896 clear_tv(&ppconst->pp_tv[ppconst_used]);
4897 --ppconst->pp_used;
4898 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004899 }
4900 else
4901 {
4902 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004903 if (op_falsy)
4904 end_idx = instr->ga_len;
4905 generate_JUMP(cctx, op_falsy
4906 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4907 if (op_falsy)
4908 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004909 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004910
4911 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01004912 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004913 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004914 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004915 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004916
Bram Moolenaara5565e42020-05-09 15:44:01 +02004917 if (!has_const_expr)
4918 {
4919 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004920
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004921 if (!op_falsy)
4922 {
4923 // remember the type and drop it
4924 --stack->ga_len;
4925 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004926
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004927 end_idx = instr->ga_len;
4928 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004929
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004930 // jump here from JUMP_IF_FALSE
4931 isn = ((isn_T *)instr->ga_data) + alt_idx;
4932 isn->isn_arg.jump.jump_where = instr->ga_len;
4933 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004934 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004935
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004936 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004937 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004938 // Check for the ":".
4939 p = may_peek_next_line(cctx, *arg, &next);
4940 if (*p != ':')
4941 {
4942 emsg(_(e_missing_colon));
4943 return FAIL;
4944 }
4945 if (next != NULL)
4946 {
4947 *arg = next_line_from_context(cctx, TRUE);
4948 p = skipwhite(*arg);
4949 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004950
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004951 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4952 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004953 semsg(_(e_white_space_required_before_and_after_str_at_str),
4954 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004955 return FAIL;
4956 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004957
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004958 // evaluate the third expression
4959 if (has_const_expr)
4960 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004961 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01004962 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004963 return FAIL;
4964 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4965 return FAIL;
4966 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004967
Bram Moolenaara5565e42020-05-09 15:44:01 +02004968 if (!has_const_expr)
4969 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004970 type_T **typep;
4971
Bram Moolenaara5565e42020-05-09 15:44:01 +02004972 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004973
Bram Moolenaara5565e42020-05-09 15:44:01 +02004974 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004975 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4976 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004977
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004978 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004979 isn = ((isn_T *)instr->ga_data) + end_idx;
4980 isn->isn_arg.jump.jump_where = instr->ga_len;
4981 }
4982
4983 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004984 }
4985 return OK;
4986}
4987
4988/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004989 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004990 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4991 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004992 */
4993 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004994compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004995{
4996 ppconst_T ppconst;
4997
4998 CLEAR_FIELD(ppconst);
4999 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5000 {
5001 clear_ppconst(&ppconst);
5002 return FAIL;
5003 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005004 if (is_const != NULL)
5005 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005006 if (generate_ppconst(cctx, &ppconst) == FAIL)
5007 return FAIL;
5008 return OK;
5009}
5010
5011/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005012 * Toplevel expression.
5013 */
5014 static int
5015compile_expr0(char_u **arg, cctx_T *cctx)
5016{
5017 return compile_expr0_ext(arg, cctx, NULL);
5018}
5019
5020/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005021 * compile "return [expr]"
5022 */
5023 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005024compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005025{
5026 char_u *p = arg;
5027 garray_T *stack = &cctx->ctx_type_stack;
5028 type_T *stack_type;
5029
5030 if (*p != NUL && *p != '|' && *p != '\n')
5031 {
5032 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02005033 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005034 return NULL;
5035
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005036 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005037 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005038 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01005039 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005040 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5041 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005042 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005043 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005044 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005045 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005046 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005047 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5048 && stack_type->tt_type != VAR_VOID
5049 && stack_type->tt_type != VAR_UNKNOWN)
5050 {
5051 emsg(_(e_returning_value_in_function_without_return_type));
5052 return NULL;
5053 }
5054 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005055 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005056 return NULL;
5057 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005058 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005059 }
5060 else
5061 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005062 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005063 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005064 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5065 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005066 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005067 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005068 return NULL;
5069 }
5070
5071 // No argument, return zero.
5072 generate_PUSHNR(cctx, 0);
5073 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005074
5075 // Undo any command modifiers.
5076 generate_undo_cmdmods(cctx);
5077
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005078 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005079 return NULL;
5080
5081 // "return val | endif" is possible
5082 return skipwhite(p);
5083}
5084
5085/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005086 * Get a line from the compilation context, compatible with exarg_T getline().
5087 * Return a pointer to the line in allocated memory.
5088 * Return NULL for end-of-file or some error.
5089 */
5090 static char_u *
5091exarg_getline(
5092 int c UNUSED,
5093 void *cookie,
5094 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005095 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005096{
5097 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005098 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005099
Bram Moolenaar66250c92020-08-20 15:02:42 +02005100 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005101 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005102 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005103 return NULL;
5104 ++cctx->ctx_lnum;
5105 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5106 // Comment lines result in NULL pointers, skip them.
5107 if (p != NULL)
5108 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005109 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005110}
5111
5112/*
5113 * Compile a nested :def command.
5114 */
5115 static char_u *
5116compile_nested_function(exarg_T *eap, cctx_T *cctx)
5117{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005118 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005119 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005120 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005121 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005122 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005123 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005124
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005125 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005126 {
5127 emsg(_(e_cannot_use_bang_with_nested_def));
5128 return NULL;
5129 }
5130
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005131 if (*name_start == '/')
5132 {
5133 name_end = skip_regexp(name_start + 1, '/', TRUE);
5134 if (*name_end == '/')
5135 ++name_end;
5136 eap->nextcmd = check_nextcmd(name_end);
5137 }
5138 if (name_end == name_start || *skipwhite(name_end) != '(')
5139 {
5140 if (!ends_excmd2(name_start, name_end))
5141 {
5142 semsg(_(e_invalid_command_str), eap->cmd);
5143 return NULL;
5144 }
5145
5146 // "def" or "def Name": list functions
5147 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5148 return NULL;
5149 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5150 }
5151
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005152 // Only g:Func() can use a namespace.
5153 if (name_start[1] == ':' && !is_global)
5154 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005155 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005156 return NULL;
5157 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005158 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005159 return NULL;
5160
Bram Moolenaar04b12692020-05-04 23:24:44 +02005161 eap->arg = name_end;
5162 eap->getline = exarg_getline;
5163 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005164 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005165 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005166 lambda_name = vim_strsave(get_lambda_name());
5167 if (lambda_name == NULL)
5168 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005169 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005170
Bram Moolenaar822ba242020-05-24 23:00:18 +02005171 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005172 {
5173 r = eap->skip ? OK : FAIL;
5174 goto theend;
5175 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005176 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5177 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005178 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005179 {
5180 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005181 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005182 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005183
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005184 if (is_global)
5185 {
5186 char_u *func_name = vim_strnsave(name_start + 2,
5187 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005188
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005189 if (func_name == NULL)
5190 r = FAIL;
5191 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005192 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005193 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005194 lambda_name = NULL;
5195 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005196 }
5197 else
5198 {
5199 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005200 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005201 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005202 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02005203
Bram Moolenaareef21022020-08-01 22:16:43 +02005204 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005205 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005206 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005207 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005208 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005209
5210 // copy over the block scope IDs
5211 if (block_depth > 0)
5212 {
5213 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5214 if (ufunc->uf_block_ids != NULL)
5215 {
5216 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5217 sizeof(int) * block_depth);
5218 ufunc->uf_block_depth = block_depth;
5219 }
5220 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005221 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005222 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005223
5224theend:
5225 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005226 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005227}
5228
5229/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005230 * Return the length of an assignment operator, or zero if there isn't one.
5231 */
5232 int
5233assignment_len(char_u *p, int *heredoc)
5234{
5235 if (*p == '=')
5236 {
5237 if (p[1] == '<' && p[2] == '<')
5238 {
5239 *heredoc = TRUE;
5240 return 3;
5241 }
5242 return 1;
5243 }
5244 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5245 return 2;
5246 if (STRNCMP(p, "..=", 3) == 0)
5247 return 3;
5248 return 0;
5249}
5250
5251// words that cannot be used as a variable
5252static char *reserved[] = {
5253 "true",
5254 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005255 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005256 NULL
5257};
5258
Bram Moolenaar752fc692021-01-04 21:57:11 +01005259// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005260typedef enum {
5261 dest_local,
5262 dest_option,
5263 dest_env,
5264 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005265 dest_buffer,
5266 dest_window,
5267 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005268 dest_vimvar,
5269 dest_script,
5270 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005271 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005272} assign_dest_T;
5273
Bram Moolenaar752fc692021-01-04 21:57:11 +01005274// Used by compile_lhs() to store information about the LHS of an assignment
5275// and one argument of ":unlet" with an index.
5276typedef struct {
5277 assign_dest_T lhs_dest; // type of destination
5278
5279 char_u *lhs_name; // allocated name including
5280 // "[expr]" or ".name".
5281 size_t lhs_varlen; // length of the variable without
5282 // "[expr]" or ".name"
5283 char_u *lhs_dest_end; // end of the destination, including
5284 // "[expr]" or ".name".
5285
5286 int lhs_has_index; // has "[expr]" or ".name"
5287
5288 int lhs_new_local; // create new local variable
5289 int lhs_opt_flags; // for when destination is an option
5290 int lhs_vimvaridx; // for when destination is a v:var
5291
5292 lvar_T lhs_local_lvar; // used for existing local destination
5293 lvar_T lhs_arg_lvar; // used for argument destination
5294 lvar_T *lhs_lvar; // points to destination lvar
5295 int lhs_scriptvar_sid;
5296 int lhs_scriptvar_idx;
5297
5298 int lhs_has_type; // type was specified
5299 type_T *lhs_type;
5300 type_T *lhs_member_type;
5301} lhs_T;
5302
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005303/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005304 * Generate the load instruction for "name".
5305 */
5306 static void
5307generate_loadvar(
5308 cctx_T *cctx,
5309 assign_dest_T dest,
5310 char_u *name,
5311 lvar_T *lvar,
5312 type_T *type)
5313{
5314 switch (dest)
5315 {
5316 case dest_option:
5317 // TODO: check the option exists
5318 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5319 break;
5320 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005321 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5322 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5323 else
5324 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005325 break;
5326 case dest_buffer:
5327 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5328 break;
5329 case dest_window:
5330 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5331 break;
5332 case dest_tab:
5333 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5334 break;
5335 case dest_script:
5336 compile_load_scriptvar(cctx,
5337 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5338 break;
5339 case dest_env:
5340 // Include $ in the name here
5341 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5342 break;
5343 case dest_reg:
5344 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5345 break;
5346 case dest_vimvar:
5347 generate_LOADV(cctx, name + 2, TRUE);
5348 break;
5349 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005350 if (lvar->lv_from_outer > 0)
5351 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5352 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005353 else
5354 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5355 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005356 case dest_expr:
5357 // list or dict value should already be on the stack.
5358 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005359 }
5360}
5361
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005362/*
5363 * Skip over "[expr]" or ".member".
5364 * Does not check for any errors.
5365 */
5366 static char_u *
5367skip_index(char_u *start)
5368{
5369 char_u *p = start;
5370
5371 if (*p == '[')
5372 {
5373 p = skipwhite(p + 1);
5374 (void)skip_expr(&p, NULL);
5375 p = skipwhite(p);
5376 if (*p == ']')
5377 return p + 1;
5378 return p;
5379 }
5380 // if (*p == '.')
5381 return to_name_end(p + 1, TRUE);
5382}
5383
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005384 void
5385vim9_declare_error(char_u *name)
5386{
5387 char *scope = "";
5388
5389 switch (*name)
5390 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005391 case 'g': scope = _("global"); break;
5392 case 'b': scope = _("buffer"); break;
5393 case 'w': scope = _("window"); break;
5394 case 't': scope = _("tab"); break;
5395 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005396 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005397 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005398 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005399 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005400 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005401 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005402 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005403 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005404 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005405}
5406
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005407/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005408 * For one assignment figure out the type of destination. Return it in "dest".
5409 * When not recognized "dest" is not set.
5410 * For an option "opt_flags" is set.
5411 * For a v:var "vimvaridx" is set.
5412 * "type" is set to the destination type if known, unchanted otherwise.
5413 * Return FAIL if an error message was given.
5414 */
5415 static int
5416get_var_dest(
5417 char_u *name,
5418 assign_dest_T *dest,
5419 int cmdidx,
5420 int *opt_flags,
5421 int *vimvaridx,
5422 type_T **type,
5423 cctx_T *cctx)
5424{
5425 char_u *p;
5426
5427 if (*name == '&')
5428 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005429 int cc;
5430 long numval;
5431 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005432
5433 *dest = dest_option;
5434 if (cmdidx == CMD_final || cmdidx == CMD_const)
5435 {
5436 emsg(_(e_const_option));
5437 return FAIL;
5438 }
5439 p = name;
5440 p = find_option_end(&p, opt_flags);
5441 if (p == NULL)
5442 {
5443 // cannot happen?
5444 emsg(_(e_letunexp));
5445 return FAIL;
5446 }
5447 cc = *p;
5448 *p = NUL;
5449 opt_type = get_option_value(skip_option_env_lead(name),
5450 &numval, NULL, *opt_flags);
5451 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005452 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005453 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005454 case gov_unknown:
5455 semsg(_(e_unknown_option), name);
5456 return FAIL;
5457 case gov_string:
5458 case gov_hidden_string:
5459 *type = &t_string;
5460 break;
5461 case gov_bool:
5462 case gov_hidden_bool:
5463 *type = &t_bool;
5464 break;
5465 case gov_number:
5466 case gov_hidden_number:
5467 *type = &t_number;
5468 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005469 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005470 }
5471 else if (*name == '$')
5472 {
5473 *dest = dest_env;
5474 *type = &t_string;
5475 }
5476 else if (*name == '@')
5477 {
5478 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5479 {
5480 emsg_invreg(name[1]);
5481 return FAIL;
5482 }
5483 *dest = dest_reg;
5484 *type = &t_string;
5485 }
5486 else if (STRNCMP(name, "g:", 2) == 0)
5487 {
5488 *dest = dest_global;
5489 }
5490 else if (STRNCMP(name, "b:", 2) == 0)
5491 {
5492 *dest = dest_buffer;
5493 }
5494 else if (STRNCMP(name, "w:", 2) == 0)
5495 {
5496 *dest = dest_window;
5497 }
5498 else if (STRNCMP(name, "t:", 2) == 0)
5499 {
5500 *dest = dest_tab;
5501 }
5502 else if (STRNCMP(name, "v:", 2) == 0)
5503 {
5504 typval_T *vtv;
5505 int di_flags;
5506
5507 *vimvaridx = find_vim_var(name + 2, &di_flags);
5508 if (*vimvaridx < 0)
5509 {
5510 semsg(_(e_variable_not_found_str), name);
5511 return FAIL;
5512 }
5513 // We use the current value of "sandbox" here, is that OK?
5514 if (var_check_ro(di_flags, name, FALSE))
5515 return FAIL;
5516 *dest = dest_vimvar;
5517 vtv = get_vim_var_tv(*vimvaridx);
5518 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5519 }
5520 return OK;
5521}
5522
5523/*
5524 * Generate a STORE instruction for "dest", not being "dest_local".
5525 * Return FAIL when out of memory.
5526 */
5527 static int
5528generate_store_var(
5529 cctx_T *cctx,
5530 assign_dest_T dest,
5531 int opt_flags,
5532 int vimvaridx,
5533 int scriptvar_idx,
5534 int scriptvar_sid,
5535 type_T *type,
5536 char_u *name)
5537{
5538 switch (dest)
5539 {
5540 case dest_option:
5541 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5542 opt_flags);
5543 case dest_global:
5544 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005545 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5546 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005547 case dest_buffer:
5548 // include b: with the name, easier to execute that way
5549 return generate_STORE(cctx, ISN_STOREB, 0, name);
5550 case dest_window:
5551 // include w: with the name, easier to execute that way
5552 return generate_STORE(cctx, ISN_STOREW, 0, name);
5553 case dest_tab:
5554 // include t: with the name, easier to execute that way
5555 return generate_STORE(cctx, ISN_STORET, 0, name);
5556 case dest_env:
5557 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5558 case dest_reg:
5559 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5560 case dest_vimvar:
5561 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5562 case dest_script:
5563 if (scriptvar_idx < 0)
5564 {
5565 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005566 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005567
Bram Moolenaar41d61962020-12-06 20:12:43 +01005568 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005569 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5570 scriptvar_sid, type);
5571 if (name_s != name)
5572 vim_free(name_s);
5573 return r;
5574 }
5575 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5576 scriptvar_sid, scriptvar_idx, type);
5577 case dest_local:
5578 case dest_expr:
5579 // cannot happen
5580 break;
5581 }
5582 return FAIL;
5583}
5584
Bram Moolenaar752fc692021-01-04 21:57:11 +01005585 static int
5586is_decl_command(int cmdidx)
5587{
5588 return cmdidx == CMD_let || cmdidx == CMD_var
5589 || cmdidx == CMD_final || cmdidx == CMD_const;
5590}
5591
5592/*
5593 * Figure out the LHS type and other properties for an assignment or one item
5594 * of ":unlet" with an index.
5595 * Returns OK or FAIL.
5596 */
5597 static int
5598compile_lhs(
5599 char_u *var_start,
5600 lhs_T *lhs,
5601 int cmdidx,
5602 int heredoc,
5603 int oplen,
5604 cctx_T *cctx)
5605{
5606 char_u *var_end;
5607 int is_decl = is_decl_command(cmdidx);
5608
5609 CLEAR_POINTER(lhs);
5610 lhs->lhs_dest = dest_local;
5611 lhs->lhs_vimvaridx = -1;
5612 lhs->lhs_scriptvar_idx = -1;
5613
5614 // "dest_end" is the end of the destination, including "[expr]" or
5615 // ".name".
5616 // "var_end" is the end of the variable/option/etc. name.
5617 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5618 if (*var_start == '@')
5619 var_end = var_start + 2;
5620 else
5621 {
5622 // skip over the leading "&", "&l:", "&g:" and "$"
5623 var_end = skip_option_env_lead(var_start);
5624 var_end = to_name_end(var_end, TRUE);
5625 }
5626
5627 // "a: type" is declaring variable "a" with a type, not dict "a:".
5628 if (is_decl && lhs->lhs_dest_end == var_start + 2
5629 && lhs->lhs_dest_end[-1] == ':')
5630 --lhs->lhs_dest_end;
5631 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5632 --var_end;
5633
5634 // compute the length of the destination without "[expr]" or ".name"
5635 lhs->lhs_varlen = var_end - var_start;
5636 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5637 if (lhs->lhs_name == NULL)
5638 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005639
5640 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5641 // Something follows after the variable: "var[idx]" or "var.key".
5642 lhs->lhs_has_index = TRUE;
5643
Bram Moolenaar752fc692021-01-04 21:57:11 +01005644 if (heredoc)
5645 lhs->lhs_type = &t_list_string;
5646 else
5647 lhs->lhs_type = &t_any;
5648
5649 if (cctx->ctx_skip != SKIP_YES)
5650 {
5651 int declare_error = FALSE;
5652
5653 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5654 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5655 &lhs->lhs_type, cctx) == FAIL)
5656 return FAIL;
5657 if (lhs->lhs_dest != dest_local)
5658 {
5659 // Specific kind of variable recognized.
5660 declare_error = is_decl;
5661 }
5662 else
5663 {
5664 int idx;
5665
5666 // No specific kind of variable recognized, just a name.
5667 for (idx = 0; reserved[idx] != NULL; ++idx)
5668 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5669 {
5670 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5671 return FAIL;
5672 }
5673
5674
5675 if (lookup_local(var_start, lhs->lhs_varlen,
5676 &lhs->lhs_local_lvar, cctx) == OK)
5677 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5678 else
5679 {
5680 CLEAR_FIELD(lhs->lhs_arg_lvar);
5681 if (arg_exists(var_start, lhs->lhs_varlen,
5682 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5683 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5684 {
5685 if (is_decl)
5686 {
5687 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5688 return FAIL;
5689 }
5690 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5691 }
5692 }
5693 if (lhs->lhs_lvar != NULL)
5694 {
5695 if (is_decl)
5696 {
5697 semsg(_(e_variable_already_declared), lhs->lhs_name);
5698 return FAIL;
5699 }
5700 }
5701 else
5702 {
5703 int script_namespace = lhs->lhs_varlen > 1
5704 && STRNCMP(var_start, "s:", 2) == 0;
5705 int script_var = (script_namespace
5706 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
5707 FALSE, cctx)
5708 : script_var_exists(var_start, lhs->lhs_varlen,
5709 TRUE, cctx)) == OK;
5710 imported_T *import =
5711 find_imported(var_start, lhs->lhs_varlen, cctx);
5712
5713 if (script_namespace || script_var || import != NULL)
5714 {
5715 char_u *rawname = lhs->lhs_name
5716 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5717
5718 if (is_decl)
5719 {
5720 if (script_namespace)
5721 semsg(_(e_cannot_declare_script_variable_in_function),
5722 lhs->lhs_name);
5723 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005724 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01005725 lhs->lhs_name);
5726 return FAIL;
5727 }
5728 else if (cctx->ctx_ufunc->uf_script_ctx_version
5729 == SCRIPT_VERSION_VIM9
5730 && script_namespace
5731 && !script_var && import == NULL)
5732 {
5733 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5734 return FAIL;
5735 }
5736
5737 lhs->lhs_dest = dest_script;
5738
5739 // existing script-local variables should have a type
5740 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5741 if (import != NULL)
5742 lhs->lhs_scriptvar_sid = import->imp_sid;
5743 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5744 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005745 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005746 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005747 lhs->lhs_scriptvar_sid, rawname,
5748 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5749 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005750 if (lhs->lhs_scriptvar_idx >= 0)
5751 {
5752 scriptitem_T *si = SCRIPT_ITEM(
5753 lhs->lhs_scriptvar_sid);
5754 svar_T *sv =
5755 ((svar_T *)si->sn_var_vals.ga_data)
5756 + lhs->lhs_scriptvar_idx;
5757 lhs->lhs_type = sv->sv_type;
5758 }
5759 }
5760 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005761 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005762 == FAIL)
5763 return FAIL;
5764 }
5765 }
5766
5767 if (declare_error)
5768 {
5769 vim9_declare_error(lhs->lhs_name);
5770 return FAIL;
5771 }
5772 }
5773
5774 // handle "a:name" as a name, not index "name" on "a"
5775 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5776 var_end = lhs->lhs_dest_end;
5777
5778 if (lhs->lhs_dest != dest_option)
5779 {
5780 if (is_decl && *var_end == ':')
5781 {
5782 char_u *p;
5783
5784 // parse optional type: "let var: type = expr"
5785 if (!VIM_ISWHITE(var_end[1]))
5786 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01005787 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005788 return FAIL;
5789 }
5790 p = skipwhite(var_end + 1);
5791 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5792 if (lhs->lhs_type == NULL)
5793 return FAIL;
5794 lhs->lhs_has_type = TRUE;
5795 }
5796 else if (lhs->lhs_lvar != NULL)
5797 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5798 }
5799
5800 if (oplen == 3 && !heredoc && lhs->lhs_dest != dest_global
5801 && lhs->lhs_type->tt_type != VAR_STRING
5802 && lhs->lhs_type->tt_type != VAR_ANY)
5803 {
5804 emsg(_(e_can_only_concatenate_to_string));
5805 return FAIL;
5806 }
5807
5808 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5809 && cctx->ctx_skip != SKIP_YES)
5810 {
5811 if (oplen > 1 && !heredoc)
5812 {
5813 // +=, /=, etc. require an existing variable
5814 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5815 return FAIL;
5816 }
5817 if (!is_decl)
5818 {
5819 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5820 return FAIL;
5821 }
5822
5823 // new local variable
5824 if ((lhs->lhs_type->tt_type == VAR_FUNC
5825 || lhs->lhs_type->tt_type == VAR_PARTIAL)
5826 && var_wrong_func_name(lhs->lhs_name, TRUE))
5827 return FAIL;
5828 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5829 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5830 if (lhs->lhs_lvar == NULL)
5831 return FAIL;
5832 lhs->lhs_new_local = TRUE;
5833 }
5834
5835 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01005836 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005837 {
5838 // Something follows after the variable: "var[idx]" or "var.key".
5839 // TODO: should we also handle "->func()" here?
5840 if (is_decl)
5841 {
5842 emsg(_(e_cannot_use_index_when_declaring_variable));
5843 return FAIL;
5844 }
5845
5846 if (var_start[lhs->lhs_varlen] == '['
5847 || var_start[lhs->lhs_varlen] == '.')
5848 {
5849 char_u *after = var_start + lhs->lhs_varlen;
5850 char_u *p;
5851
5852 // Only the last index is used below, if there are others
5853 // before it generate code for the expression. Thus for
5854 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5855 for (;;)
5856 {
5857 p = skip_index(after);
5858 if (*p != '[' && *p != '.')
5859 break;
5860 after = p;
5861 }
5862 if (after > var_start + lhs->lhs_varlen)
5863 {
5864 lhs->lhs_varlen = after - var_start;
5865 lhs->lhs_dest = dest_expr;
5866 // We don't know the type before evaluating the expression,
5867 // use "any" until then.
5868 lhs->lhs_type = &t_any;
5869 }
5870
Bram Moolenaar752fc692021-01-04 21:57:11 +01005871 if (lhs->lhs_type->tt_member == NULL)
5872 lhs->lhs_member_type = &t_any;
5873 else
5874 lhs->lhs_member_type = lhs->lhs_type->tt_member;
5875 }
5876 else
5877 {
5878 semsg("Not supported yet: %s", var_start);
5879 return FAIL;
5880 }
5881 }
5882 return OK;
5883}
5884
5885/*
5886 * Assignment to a list or dict member, or ":unlet" for the item, using the
5887 * information in "lhs".
5888 * Returns OK or FAIL.
5889 */
5890 static int
5891compile_assign_unlet(
5892 char_u *var_start,
5893 lhs_T *lhs,
5894 int is_assign,
5895 type_T *rhs_type,
5896 cctx_T *cctx)
5897{
5898 char_u *p;
5899 int r;
5900 vartype_T dest_type;
5901 size_t varlen = lhs->lhs_varlen;
5902 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01005903 int range = FALSE;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005904
5905 // Compile the "idx" in "var[idx]" or "key" in "var.key".
5906 p = var_start + varlen;
5907 if (*p == '[')
5908 {
5909 p = skipwhite(p + 1);
5910 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01005911
5912 if (r == OK && *skipwhite(p) == ':')
5913 {
5914 // unlet var[idx : idx]
5915 if (is_assign)
5916 {
5917 semsg(_(e_cannot_use_range_with_assignment_str), p);
5918 return FAIL;
5919 }
5920 range = TRUE;
5921 p = skipwhite(p);
5922 if (!IS_WHITE_OR_NUL(p[-1]) || !IS_WHITE_OR_NUL(p[1]))
5923 {
5924 semsg(_(e_white_space_required_before_and_after_str_at_str),
5925 ":", p);
5926 return FAIL;
5927 }
5928 p = skipwhite(p + 1);
5929 r = compile_expr0(&p, cctx);
5930 }
5931
Bram Moolenaar752fc692021-01-04 21:57:11 +01005932 if (r == OK && *skipwhite(p) != ']')
5933 {
5934 // this should not happen
5935 emsg(_(e_missbrac));
5936 r = FAIL;
5937 }
5938 }
5939 else // if (*p == '.')
5940 {
5941 char_u *key_end = to_name_end(p + 1, TRUE);
5942 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5943
5944 r = generate_PUSHS(cctx, key);
5945 }
5946 if (r == FAIL)
5947 return FAIL;
5948
5949 if (lhs->lhs_type == &t_any)
5950 {
5951 // Index on variable of unknown type: check at runtime.
5952 dest_type = VAR_ANY;
5953 }
5954 else
5955 {
5956 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01005957 if (dest_type == VAR_DICT && range)
5958 {
5959 emsg(e_cannot_use_range_with_dictionary);
5960 return FAIL;
5961 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01005962 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
5963 return FAIL;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01005964 if (dest_type == VAR_LIST)
5965 {
5966 if (range
5967 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 2],
Bram Moolenaarf30a14d2021-01-17 21:51:24 +01005968 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01005969 return FAIL;
5970 if (need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
5971 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
5972 return FAIL;
5973 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01005974 }
5975
5976 // Load the dict or list. On the stack we then have:
5977 // - value (for assignment, not for :unlet)
5978 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01005979 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01005980 // - variable
5981 if (lhs->lhs_dest == dest_expr)
5982 {
5983 int c = var_start[varlen];
5984
5985 // Evaluate "ll[expr]" of "ll[expr][idx]"
5986 p = var_start;
5987 var_start[varlen] = NUL;
5988 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
5989 {
5990 // this should not happen
5991 emsg(_(e_missbrac));
5992 return FAIL;
5993 }
5994 var_start[varlen] = c;
5995
5996 lhs->lhs_type = stack->ga_len == 0 ? &t_void
5997 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5998 // now we can properly check the type
5999 if (lhs->lhs_type->tt_member != NULL && rhs_type != &t_void
Bram Moolenaar351ead02021-01-16 16:07:01 +01006000 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006001 FALSE, FALSE) == FAIL)
6002 return FAIL;
6003 }
6004 else
6005 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6006 lhs->lhs_lvar, lhs->lhs_type);
6007
6008 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
6009 {
6010 if (is_assign)
6011 {
6012 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
6013
6014 if (isn == NULL)
6015 return FAIL;
6016 isn->isn_arg.vartype = dest_type;
6017 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006018 else if (range)
6019 {
6020 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6021 return FAIL;
6022 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006023 else
6024 {
6025 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6026 return FAIL;
6027 }
6028 }
6029 else
6030 {
6031 emsg(_(e_indexable_type_required));
6032 return FAIL;
6033 }
6034
6035 return OK;
6036}
6037
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006038/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006039 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006040 * "let name"
6041 * "var name = expr"
6042 * "final name = expr"
6043 * "const name = expr"
6044 * "name = expr"
6045 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006046 * Return NULL for an error.
6047 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006048 */
6049 static char_u *
6050compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6051{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006052 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006053 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006054 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006055 char_u *ret = NULL;
6056 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006057 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006058 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006059 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006060 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006061 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006062 int oplen = 0;
6063 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006064 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006065 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006066 int is_decl = is_decl_command(cmdidx);
6067 lhs_T lhs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006068
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006069 // Skip over the "var" or "[var, var]" to get to any "=".
6070 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6071 if (p == NULL)
6072 return *arg == '[' ? arg : NULL;
6073
6074 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006075 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006076 // TODO: should we allow this, and figure out type inference from list
6077 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006078 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006079 return NULL;
6080 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006081 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006082
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006083 sp = p;
6084 p = skipwhite(p);
6085 op = p;
6086 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006087
6088 if (var_count > 0 && oplen == 0)
6089 // can be something like "[1, 2]->func()"
6090 return arg;
6091
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006092 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006093 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006094 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006095 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006096 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006097
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006098 if (heredoc)
6099 {
6100 list_T *l;
6101 listitem_T *li;
6102
6103 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006104 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006105 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006106 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006107 if (l == NULL)
6108 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006109
Bram Moolenaar078269b2020-09-21 20:35:55 +02006110 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006111 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006112 // Push each line and the create the list.
6113 FOR_ALL_LIST_ITEMS(l, li)
6114 {
6115 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6116 li->li_tv.vval.v_string = NULL;
6117 }
6118 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006119 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006120 list_free(l);
6121 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006122 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006123 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006124 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006125 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006126 char_u *wp;
6127
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006128 // for "[var, var] = expr" evaluate the expression here, loop over the
6129 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006130 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006131
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006132 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006133 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006134 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006135 if (compile_expr0(&p, cctx) == FAIL)
6136 return NULL;
6137 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006138
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006139 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006140 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006141 type_T *stacktype;
6142
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006143 stacktype = stack->ga_len == 0 ? &t_void
6144 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006145 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006146 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006147 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006148 goto theend;
6149 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006150 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006151 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006152 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006153 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006154 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6155 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006156 if (stacktype->tt_member != NULL)
6157 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006158 }
6159 }
6160
6161 /*
6162 * Loop over variables in "[var, var] = expr".
6163 * For "var = expr" and "let var: type" this is done only once.
6164 */
6165 if (var_count > 0)
6166 var_start = skipwhite(arg + 1); // skip over the "["
6167 else
6168 var_start = arg;
6169 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6170 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006171 int instr_count = -1;
6172
Bram Moolenaar752fc692021-01-04 21:57:11 +01006173 vim_free(lhs.lhs_name);
6174
6175 /*
6176 * Figure out the LHS type and other properties.
6177 */
6178 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6179 goto theend;
6180
6181 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006182 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006183 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006184 goto theend;
6185 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006186 if (!is_decl && lhs.lhs_lvar != NULL
6187 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006188 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006189 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006190 goto theend;
6191 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006192
6193 if (!heredoc)
6194 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006195 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006196 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006197 if (oplen > 0 && var_count == 0)
6198 {
6199 // skip over the "=" and the expression
6200 p = skipwhite(op + oplen);
6201 compile_expr0(&p, cctx);
6202 }
6203 }
6204 else if (oplen > 0)
6205 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006206 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006207 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006208
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006209 // For "var = expr" evaluate the expression.
6210 if (var_count == 0)
6211 {
6212 int r;
6213
6214 // for "+=", "*=", "..=" etc. first load the current value
6215 if (*op != '=')
6216 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006217 generate_loadvar(cctx, lhs.lhs_dest, lhs.lhs_name,
6218 lhs.lhs_lvar, lhs.lhs_type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006219
Bram Moolenaar752fc692021-01-04 21:57:11 +01006220 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006221 {
6222 // TODO: get member from list or dict
6223 emsg("Index with operation not supported yet");
6224 goto theend;
6225 }
6226 }
6227
6228 // Compile the expression. Temporarily hide the new local
6229 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006230 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006231 --cctx->ctx_locals.ga_len;
6232 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006233 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006234 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006235 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006236 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006237 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006238 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006239 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006240 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006241 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006242 ++cctx->ctx_locals.ga_len;
6243 if (r == FAIL)
6244 goto theend;
6245 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006246 else if (semicolon && var_idx == var_count - 1)
6247 {
6248 // For "[var; var] = expr" get the rest of the list
6249 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6250 goto theend;
6251 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006252 else
6253 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006254 // For "[var, var] = expr" get the "var_idx" item from the
6255 // list.
6256 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006257 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006258 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006259
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006260 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006261 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006262 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006263 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006264 if ((rhs_type->tt_type == VAR_FUNC
6265 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006266 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006267 goto theend;
6268
Bram Moolenaar752fc692021-01-04 21:57:11 +01006269 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006270 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006271 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006272 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006273 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006274 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006275 }
6276 else
6277 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006278 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006279 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006280 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006281 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006282 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006283 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006284 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006285 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006286 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006287 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006288 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006289 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006290 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006291 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006292 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006293
Bram Moolenaar71abe482020-09-14 22:28:30 +02006294 // without operator check type here, otherwise below
Bram Moolenaar752fc692021-01-04 21:57:11 +01006295 if (lhs.lhs_has_index)
6296 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006297 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006298 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006299 goto theend;
6300 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006301 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006302 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006303 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006304 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006305 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006306 else if (cmdidx == CMD_final)
6307 {
6308 emsg(_(e_final_requires_a_value));
6309 goto theend;
6310 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006311 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006312 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006313 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006314 goto theend;
6315 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006316 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006317 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006318 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006319 goto theend;
6320 }
6321 else
6322 {
6323 // variables are always initialized
6324 if (ga_grow(instr, 1) == FAIL)
6325 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006326 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006327 {
6328 case VAR_BOOL:
6329 generate_PUSHBOOL(cctx, VVAL_FALSE);
6330 break;
6331 case VAR_FLOAT:
6332#ifdef FEAT_FLOAT
6333 generate_PUSHF(cctx, 0.0);
6334#endif
6335 break;
6336 case VAR_STRING:
6337 generate_PUSHS(cctx, NULL);
6338 break;
6339 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006340 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006341 break;
6342 case VAR_FUNC:
6343 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6344 break;
6345 case VAR_LIST:
6346 generate_NEWLIST(cctx, 0);
6347 break;
6348 case VAR_DICT:
6349 generate_NEWDICT(cctx, 0);
6350 break;
6351 case VAR_JOB:
6352 generate_PUSHJOB(cctx, NULL);
6353 break;
6354 case VAR_CHANNEL:
6355 generate_PUSHCHANNEL(cctx, NULL);
6356 break;
6357 case VAR_NUMBER:
6358 case VAR_UNKNOWN:
6359 case VAR_ANY:
6360 case VAR_PARTIAL:
6361 case VAR_VOID:
6362 case VAR_SPECIAL: // cannot happen
6363 generate_PUSHNR(cctx, 0);
6364 break;
6365 }
6366 }
6367 if (var_count == 0)
6368 end = p;
6369 }
6370
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006371 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006372 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006373 break;
6374
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006375 if (oplen > 0 && *op != '=')
6376 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006377 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006378 type_T *stacktype;
6379
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006380 if (*op == '.')
6381 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006382 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006383 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006384 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006385 if (
6386#ifdef FEAT_FLOAT
6387 // If variable is float operation with number is OK.
6388 !(expected == &t_float && stacktype == &t_number) &&
6389#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006390 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006391 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006392 goto theend;
6393
6394 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006395 {
6396 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6397 goto theend;
6398 }
6399 else if (*op == '+')
6400 {
6401 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006402 operator_type(lhs.lhs_member_type, stacktype),
6403 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006404 goto theend;
6405 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006406 else if (generate_two_op(cctx, op) == FAIL)
6407 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006408 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006409
Bram Moolenaar752fc692021-01-04 21:57:11 +01006410 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006411 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006412 // Use the info in "lhs" to store the value at the index in the
6413 // list or dict.
6414 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6415 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006416 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006417 }
6418 else
6419 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006420 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
6421 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006422 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006423 generate_LOCKCONST(cctx);
6424
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006425 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006426 && (lhs.lhs_type->tt_type == VAR_DICT
6427 || lhs.lhs_type->tt_type == VAR_LIST)
6428 && lhs.lhs_type->tt_member != NULL
6429 && lhs.lhs_type->tt_member != &t_any
6430 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006431 // Set the type in the list or dict, so that it can be checked,
6432 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006433 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006434
Bram Moolenaar752fc692021-01-04 21:57:11 +01006435 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006436 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006437 if (generate_store_var(cctx, lhs.lhs_dest,
6438 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6439 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6440 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006441 goto theend;
6442 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006443 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006444 {
6445 isn_T *isn = ((isn_T *)instr->ga_data)
6446 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006447
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006448 // optimization: turn "var = 123" from ISN_PUSHNR +
6449 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006450 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006451 && instr->ga_len == instr_count + 1
6452 && isn->isn_type == ISN_PUSHNR)
6453 {
6454 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006455
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006456 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006457 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006458 isn->isn_arg.storenr.stnr_val = val;
6459 if (stack->ga_len > 0)
6460 --stack->ga_len;
6461 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006462 else if (lhs.lhs_lvar->lv_from_outer > 0)
6463 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6464 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006465 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006466 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006467 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006468 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006469
6470 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006471 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006472 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006473
6474 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006475 if (var_count > 0 && !semicolon)
6476 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006477 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006478 goto theend;
6479 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006480
Bram Moolenaarb2097502020-07-19 17:17:02 +02006481 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006482
6483theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006484 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006485 return ret;
6486}
6487
6488/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006489 * Check for an assignment at "eap->cmd", compile it if found.
6490 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6491 */
6492 static int
6493may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6494{
6495 char_u *pskip;
6496 char_u *p;
6497
6498 // Assuming the command starts with a variable or function name,
6499 // find what follows.
6500 // Skip over "var.member", "var[idx]" and the like.
6501 // Also "&opt = val", "$ENV = val" and "@r = val".
6502 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6503 ? eap->cmd + 1 : eap->cmd;
6504 p = to_name_end(pskip, TRUE);
6505 if (p > eap->cmd && *p != NUL)
6506 {
6507 char_u *var_end;
6508 int oplen;
6509 int heredoc;
6510
6511 if (eap->cmd[0] == '@')
6512 var_end = eap->cmd + 2;
6513 else
6514 var_end = find_name_end(pskip, NULL, NULL,
6515 FNE_CHECK_START | FNE_INCL_BR);
6516 oplen = assignment_len(skipwhite(var_end), &heredoc);
6517 if (oplen > 0)
6518 {
6519 size_t len = p - eap->cmd;
6520
6521 // Recognize an assignment if we recognize the variable
6522 // name:
6523 // "g:var = expr"
6524 // "local = expr" where "local" is a local var.
6525 // "script = expr" where "script" is a script-local var.
6526 // "import = expr" where "import" is an imported var
6527 // "&opt = expr"
6528 // "$ENV = expr"
6529 // "@r = expr"
6530 if (*eap->cmd == '&'
6531 || *eap->cmd == '$'
6532 || *eap->cmd == '@'
6533 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01006534 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01006535 {
6536 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6537 if (*line == NULL || *line == eap->cmd)
6538 return FAIL;
6539 return OK;
6540 }
6541 }
6542 }
6543
6544 if (*eap->cmd == '[')
6545 {
6546 // [var, var] = expr
6547 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6548 if (*line == NULL)
6549 return FAIL;
6550 if (*line != eap->cmd)
6551 return OK;
6552 }
6553 return NOTDONE;
6554}
6555
6556/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006557 * Check if "name" can be "unlet".
6558 */
6559 int
6560check_vim9_unlet(char_u *name)
6561{
6562 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6563 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006564 // "unlet s:var" is allowed in legacy script.
6565 if (*name == 's' && !script_is_vim9())
6566 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006567 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006568 return FAIL;
6569 }
6570 return OK;
6571}
6572
6573/*
6574 * Callback passed to ex_unletlock().
6575 */
6576 static int
6577compile_unlet(
6578 lval_T *lvp,
6579 char_u *name_end,
6580 exarg_T *eap,
6581 int deep UNUSED,
6582 void *coookie)
6583{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006584 cctx_T *cctx = coookie;
6585 char_u *p = lvp->ll_name;
6586 int cc = *name_end;
6587 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006588
Bram Moolenaar752fc692021-01-04 21:57:11 +01006589 if (cctx->ctx_skip == SKIP_YES)
6590 return OK;
6591
6592 *name_end = NUL;
6593 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006594 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006595 // :unlet $ENV_VAR
6596 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6597 }
6598 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6599 {
6600 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006601
Bram Moolenaar752fc692021-01-04 21:57:11 +01006602 // This is similar to assigning: lookup the list/dict, compile the
6603 // idx/key. Then instead of storing the value unlet the item.
6604 // unlet {list}[idx]
6605 // unlet {dict}[key] dict.key
6606 //
6607 // Figure out the LHS type and other properties.
6608 //
6609 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6610
6611 // : unlet an indexed item
6612 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006613 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006614 iemsg("called compile_lhs() without an index");
6615 ret = FAIL;
6616 }
6617 else
6618 {
6619 // Use the info in "lhs" to unlet the item at the index in the
6620 // list or dict.
6621 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006622 }
6623
Bram Moolenaar752fc692021-01-04 21:57:11 +01006624 vim_free(lhs.lhs_name);
6625 }
6626 else if (check_vim9_unlet(p) == FAIL)
6627 {
6628 ret = FAIL;
6629 }
6630 else
6631 {
6632 // Normal name. Only supports g:, w:, t: and b: namespaces.
6633 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006634 }
6635
Bram Moolenaar752fc692021-01-04 21:57:11 +01006636 *name_end = cc;
6637 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006638}
6639
6640/*
6641 * compile "unlet var", "lock var" and "unlock var"
6642 * "arg" points to "var".
6643 */
6644 static char_u *
6645compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6646{
6647 char_u *p = arg;
6648
6649 if (eap->cmdidx != CMD_unlet)
6650 {
6651 emsg("Sorry, :lock and unlock not implemented yet");
6652 return NULL;
6653 }
6654
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006655 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6656 compile_unlet, cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006657 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6658}
6659
6660/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006661 * Compile an :import command.
6662 */
6663 static char_u *
6664compile_import(char_u *arg, cctx_T *cctx)
6665{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006666 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006667}
6668
6669/*
6670 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6671 */
6672 static int
6673compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6674{
6675 garray_T *instr = &cctx->ctx_instr;
6676 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6677
6678 if (endlabel == NULL)
6679 return FAIL;
6680 endlabel->el_next = *el;
6681 *el = endlabel;
6682 endlabel->el_end_label = instr->ga_len;
6683
6684 generate_JUMP(cctx, when, 0);
6685 return OK;
6686}
6687
6688 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006689compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006690{
6691 garray_T *instr = &cctx->ctx_instr;
6692
6693 while (*el != NULL)
6694 {
6695 endlabel_T *cur = (*el);
6696 isn_T *isn;
6697
6698 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006699 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006700 *el = cur->el_next;
6701 vim_free(cur);
6702 }
6703}
6704
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006705 static void
6706compile_free_jump_to_end(endlabel_T **el)
6707{
6708 while (*el != NULL)
6709 {
6710 endlabel_T *cur = (*el);
6711
6712 *el = cur->el_next;
6713 vim_free(cur);
6714 }
6715}
6716
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006717/*
6718 * Create a new scope and set up the generic items.
6719 */
6720 static scope_T *
6721new_scope(cctx_T *cctx, scopetype_T type)
6722{
6723 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6724
6725 if (scope == NULL)
6726 return NULL;
6727 scope->se_outer = cctx->ctx_scope;
6728 cctx->ctx_scope = scope;
6729 scope->se_type = type;
6730 scope->se_local_count = cctx->ctx_locals.ga_len;
6731 return scope;
6732}
6733
6734/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006735 * Free the current scope and go back to the outer scope.
6736 */
6737 static void
6738drop_scope(cctx_T *cctx)
6739{
6740 scope_T *scope = cctx->ctx_scope;
6741
6742 if (scope == NULL)
6743 {
6744 iemsg("calling drop_scope() without a scope");
6745 return;
6746 }
6747 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006748 switch (scope->se_type)
6749 {
6750 case IF_SCOPE:
6751 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6752 case FOR_SCOPE:
6753 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6754 case WHILE_SCOPE:
6755 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6756 case TRY_SCOPE:
6757 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6758 case NO_SCOPE:
6759 case BLOCK_SCOPE:
6760 break;
6761 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006762 vim_free(scope);
6763}
6764
6765/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006766 * compile "if expr"
6767 *
6768 * "if expr" Produces instructions:
6769 * EVAL expr Push result of "expr"
6770 * JUMP_IF_FALSE end
6771 * ... body ...
6772 * end:
6773 *
6774 * "if expr | else" Produces instructions:
6775 * EVAL expr Push result of "expr"
6776 * JUMP_IF_FALSE else
6777 * ... body ...
6778 * JUMP_ALWAYS end
6779 * else:
6780 * ... body ...
6781 * end:
6782 *
6783 * "if expr1 | elseif expr2 | else" Produces instructions:
6784 * EVAL expr Push result of "expr"
6785 * JUMP_IF_FALSE elseif
6786 * ... body ...
6787 * JUMP_ALWAYS end
6788 * elseif:
6789 * EVAL expr Push result of "expr"
6790 * JUMP_IF_FALSE else
6791 * ... body ...
6792 * JUMP_ALWAYS end
6793 * else:
6794 * ... body ...
6795 * end:
6796 */
6797 static char_u *
6798compile_if(char_u *arg, cctx_T *cctx)
6799{
6800 char_u *p = arg;
6801 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006802 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006803 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006804 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006805 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006806
Bram Moolenaara5565e42020-05-09 15:44:01 +02006807 CLEAR_FIELD(ppconst);
6808 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006809 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006810 clear_ppconst(&ppconst);
6811 return NULL;
6812 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01006813 if (!ends_excmd2(arg, skipwhite(p)))
6814 {
6815 semsg(_(e_trailing_arg), p);
6816 return NULL;
6817 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006818 if (cctx->ctx_skip == SKIP_YES)
6819 clear_ppconst(&ppconst);
6820 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006821 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006822 int error = FALSE;
6823 int v;
6824
Bram Moolenaara5565e42020-05-09 15:44:01 +02006825 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006826 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006827 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006828 if (error)
6829 return NULL;
6830 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006831 }
6832 else
6833 {
6834 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006835 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006836 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006837 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006838 if (bool_on_stack(cctx) == FAIL)
6839 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006840 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006841
6842 scope = new_scope(cctx, IF_SCOPE);
6843 if (scope == NULL)
6844 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006845 scope->se_skip_save = skip_save;
6846 // "is_had_return" will be reset if any block does not end in :return
6847 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006848
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006849 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006850 {
6851 // "where" is set when ":elseif", "else" or ":endif" is found
6852 scope->se_u.se_if.is_if_label = instr->ga_len;
6853 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6854 }
6855 else
6856 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006857
Bram Moolenaarced68a02021-01-24 17:53:47 +01006858#ifdef FEAT_PROFILE
6859 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
6860 && skip_save != SKIP_YES)
6861 {
6862 // generated a profile start, need to generate a profile end, since it
6863 // won't be done after returning
6864 cctx->ctx_skip = SKIP_NOT;
6865 generate_instr(cctx, ISN_PROF_END);
6866 cctx->ctx_skip = SKIP_YES;
6867 }
6868#endif
6869
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006870 return p;
6871}
6872
6873 static char_u *
6874compile_elseif(char_u *arg, cctx_T *cctx)
6875{
6876 char_u *p = arg;
6877 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006878 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006879 isn_T *isn;
6880 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006881 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006882 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006883
6884 if (scope == NULL || scope->se_type != IF_SCOPE)
6885 {
6886 emsg(_(e_elseif_without_if));
6887 return NULL;
6888 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006889 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006890 if (!cctx->ctx_had_return)
6891 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006892
Bram Moolenaarced68a02021-01-24 17:53:47 +01006893 if (cctx->ctx_skip == SKIP_NOT)
6894 {
6895 // previous block was executed, this one and following will not
6896 cctx->ctx_skip = SKIP_YES;
6897 scope->se_u.se_if.is_seen_skip_not = TRUE;
6898 }
6899 if (scope->se_u.se_if.is_seen_skip_not)
6900 {
6901 // A previous block was executed, skip over expression and bail out.
6902 // Do not count the "elseif" for profiling.
6903#ifdef FEAT_PROFILE
6904 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
6905 .isn_type == ISN_PROF_START)
6906 --instr->ga_len;
6907#endif
6908 skip_expr_cctx(&p, cctx);
6909 return p;
6910 }
6911
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006912 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006913 {
6914 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006915 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006916 return NULL;
6917 // previous "if" or "elseif" jumps here
6918 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6919 isn->isn_arg.jump.jump_where = instr->ga_len;
6920 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006921
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006922 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006923 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006924 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01006925 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02006926 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01006927#ifdef FEAT_PROFILE
6928 if (cctx->ctx_profiling)
6929 {
6930 // the previous block was skipped, need to profile this line
6931 generate_instr(cctx, ISN_PROF_START);
6932 instr_count = instr->ga_len;
6933 }
6934#endif
6935 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02006936 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006937 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006938 clear_ppconst(&ppconst);
6939 return NULL;
6940 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006941 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01006942 if (!ends_excmd2(arg, skipwhite(p)))
6943 {
6944 semsg(_(e_trailing_arg), p);
6945 return NULL;
6946 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006947 if (scope->se_skip_save == SKIP_YES)
6948 clear_ppconst(&ppconst);
6949 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006950 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006951 int error = FALSE;
6952 int v;
6953
Bram Moolenaar7f141552020-05-09 17:35:53 +02006954 // The expression results in a constant.
6955 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006956 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6957 if (error)
6958 return NULL;
6959 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006960 clear_ppconst(&ppconst);
6961 scope->se_u.se_if.is_if_label = -1;
6962 }
6963 else
6964 {
6965 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006966 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006967 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006968 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006969 if (bool_on_stack(cctx) == FAIL)
6970 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006971
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006972 // "where" is set when ":elseif", "else" or ":endif" is found
6973 scope->se_u.se_if.is_if_label = instr->ga_len;
6974 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6975 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006976
6977 return p;
6978}
6979
6980 static char_u *
6981compile_else(char_u *arg, cctx_T *cctx)
6982{
6983 char_u *p = arg;
6984 garray_T *instr = &cctx->ctx_instr;
6985 isn_T *isn;
6986 scope_T *scope = cctx->ctx_scope;
6987
6988 if (scope == NULL || scope->se_type != IF_SCOPE)
6989 {
6990 emsg(_(e_else_without_if));
6991 return NULL;
6992 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006993 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006994 if (!cctx->ctx_had_return)
6995 scope->se_u.se_if.is_had_return = FALSE;
6996 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006997
Bram Moolenaarced68a02021-01-24 17:53:47 +01006998#ifdef FEAT_PROFILE
6999 if (cctx->ctx_profiling)
7000 {
7001 if (cctx->ctx_skip == SKIP_NOT
7002 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7003 .isn_type == ISN_PROF_START)
7004 // the previous block was executed, do not count "else" for profiling
7005 --instr->ga_len;
7006 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7007 {
7008 // the previous block was not executed, this one will, do count the
7009 // "else" for profiling
7010 cctx->ctx_skip = SKIP_NOT;
7011 generate_instr(cctx, ISN_PROF_END);
7012 generate_instr(cctx, ISN_PROF_START);
7013 cctx->ctx_skip = SKIP_YES;
7014 }
7015 }
7016#endif
7017
7018 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007019 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007020 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007021 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007022 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007023 if (!cctx->ctx_had_return
7024 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7025 JUMP_ALWAYS, cctx) == FAIL)
7026 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007027 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007028
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007029 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007030 {
7031 if (scope->se_u.se_if.is_if_label >= 0)
7032 {
7033 // previous "if" or "elseif" jumps here
7034 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7035 isn->isn_arg.jump.jump_where = instr->ga_len;
7036 scope->se_u.se_if.is_if_label = -1;
7037 }
7038 }
7039
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007040 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007041 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7042 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007043
7044 return p;
7045}
7046
7047 static char_u *
7048compile_endif(char_u *arg, cctx_T *cctx)
7049{
7050 scope_T *scope = cctx->ctx_scope;
7051 ifscope_T *ifscope;
7052 garray_T *instr = &cctx->ctx_instr;
7053 isn_T *isn;
7054
7055 if (scope == NULL || scope->se_type != IF_SCOPE)
7056 {
7057 emsg(_(e_endif_without_if));
7058 return NULL;
7059 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007060 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007061 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007062 if (!cctx->ctx_had_return)
7063 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007064
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007065 if (scope->se_u.se_if.is_if_label >= 0)
7066 {
7067 // previous "if" or "elseif" jumps here
7068 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7069 isn->isn_arg.jump.jump_where = instr->ga_len;
7070 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007071 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007072 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007073
7074#ifdef FEAT_PROFILE
7075 // even when skipping we count the endif as executed, unless the block it's
7076 // in is skipped
7077 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7078 && scope->se_skip_save != SKIP_YES)
7079 {
7080 cctx->ctx_skip = SKIP_NOT;
7081 generate_instr(cctx, ISN_PROF_START);
7082 }
7083#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007084 cctx->ctx_skip = scope->se_skip_save;
7085
7086 // If all the blocks end in :return and there is an :else then the
7087 // had_return flag is set.
7088 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007089
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007090 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007091 return arg;
7092}
7093
7094/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007095 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007096 *
7097 * Produces instructions:
7098 * PUSHNR -1
7099 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007100 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007101 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7102 * - if beyond end, jump to "end"
7103 * - otherwise get item from list and push it
7104 * STORE var Store item in "var"
7105 * ... body ...
7106 * JUMP top Jump back to repeat
7107 * end: DROP Drop the result of "expr"
7108 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007109 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7110 * UNPACK 2 Split item in 2
7111 * STORE var1 Store item in "var1"
7112 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007113 */
7114 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007115compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007116{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007117 char_u *arg;
7118 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007119 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007120 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007121 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007122 int var_count = 0;
7123 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007124 size_t varlen;
7125 garray_T *instr = &cctx->ctx_instr;
7126 garray_T *stack = &cctx->ctx_type_stack;
7127 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007128 lvar_T *loop_lvar; // loop iteration variable
7129 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007130 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007131 type_T *item_type = &t_any;
7132 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007133
Bram Moolenaar792f7862020-11-23 08:31:18 +01007134 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007135 if (p == NULL)
7136 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007137 if (var_count == 0)
7138 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007139
7140 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007141 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007142 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7143 return NULL;
7144 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007145 {
7146 emsg(_(e_missing_in));
7147 return NULL;
7148 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007149 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007150 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7151 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007152
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007153 scope = new_scope(cctx, FOR_SCOPE);
7154 if (scope == NULL)
7155 return NULL;
7156
Bram Moolenaar792f7862020-11-23 08:31:18 +01007157 // Reserve a variable to store the loop iteration counter and initialize it
7158 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007159 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7160 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007161 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007162 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007163 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007164 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007165 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007166 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007167
7168 // compile "expr", it remains on the stack until "endfor"
7169 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007170 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007171 {
7172 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007173 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007174 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007175 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007176
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007177 // Now that we know the type of "var", check that it is a list, now or at
7178 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007179 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01007180 if (need_type(vartype, &t_list_any, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007181 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007182 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007183 return NULL;
7184 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007185
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007186 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007187 {
7188 if (var_count == 1)
7189 item_type = vartype->tt_member;
7190 else if (vartype->tt_member->tt_type == VAR_LIST
7191 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
7192 item_type = vartype->tt_member->tt_member;
7193 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007194
7195 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007196 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007197 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007198
Bram Moolenaar792f7862020-11-23 08:31:18 +01007199 arg = arg_start;
7200 if (var_count > 1)
7201 {
7202 generate_UNPACK(cctx, var_count, semicolon);
7203 arg = skipwhite(arg + 1); // skip white after '['
7204
7205 // the list item is replaced by a number of items
7206 if (ga_grow(stack, var_count - 1) == FAIL)
7207 {
7208 drop_scope(cctx);
7209 return NULL;
7210 }
7211 --stack->ga_len;
7212 for (idx = 0; idx < var_count; ++idx)
7213 {
7214 ((type_T **)stack->ga_data)[stack->ga_len] =
7215 (semicolon && idx == 0) ? vartype : item_type;
7216 ++stack->ga_len;
7217 }
7218 }
7219
7220 for (idx = 0; idx < var_count; ++idx)
7221 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007222 assign_dest_T dest = dest_local;
7223 int opt_flags = 0;
7224 int vimvaridx = -1;
7225 type_T *type = &t_any;
7226
7227 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007228 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007229 name = vim_strnsave(arg, varlen);
7230 if (name == NULL)
7231 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007232
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007233 // TODO: script var not supported?
7234 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7235 &vimvaridx, &type, cctx) == FAIL)
7236 goto failed;
7237 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007238 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007239 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7240 0, 0, type, name) == FAIL)
7241 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007242 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007243 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007244 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007245 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007246 {
7247 semsg(_(e_variable_already_declared), arg);
7248 goto failed;
7249 }
7250
Bram Moolenaarea870692020-12-02 14:24:30 +01007251 if (STRNCMP(name, "s:", 2) == 0)
7252 {
7253 semsg(_(e_cannot_declare_script_variable_in_function), name);
7254 goto failed;
7255 }
7256
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007257 // Reserve a variable to store "var".
7258 // TODO: check for type
7259 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
7260 if (var_lvar == NULL)
7261 // out of memory or used as an argument
7262 goto failed;
7263
7264 if (semicolon && idx == var_count - 1)
7265 var_lvar->lv_type = vartype;
7266 else
7267 var_lvar->lv_type = item_type;
7268 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7269 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007270
Bram Moolenaar036d0712021-01-17 20:23:38 +01007271 if (*p == ':')
7272 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007273 if (*p == ',' || *p == ';')
7274 ++p;
7275 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007276 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007277 }
7278
7279 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007280
7281failed:
7282 vim_free(name);
7283 drop_scope(cctx);
7284 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007285}
7286
7287/*
7288 * compile "endfor"
7289 */
7290 static char_u *
7291compile_endfor(char_u *arg, cctx_T *cctx)
7292{
7293 garray_T *instr = &cctx->ctx_instr;
7294 scope_T *scope = cctx->ctx_scope;
7295 forscope_T *forscope;
7296 isn_T *isn;
7297
7298 if (scope == NULL || scope->se_type != FOR_SCOPE)
7299 {
7300 emsg(_(e_for));
7301 return NULL;
7302 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007303 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007304 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007305 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007306
7307 // At end of ":for" scope jump back to the FOR instruction.
7308 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7309
7310 // Fill in the "end" label in the FOR statement so it can jump here
7311 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7312 isn->isn_arg.forloop.for_end = instr->ga_len;
7313
7314 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007315 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007316
7317 // Below the ":for" scope drop the "expr" list from the stack.
7318 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7319 return NULL;
7320
7321 vim_free(scope);
7322
7323 return arg;
7324}
7325
7326/*
7327 * compile "while expr"
7328 *
7329 * Produces instructions:
7330 * top: EVAL expr Push result of "expr"
7331 * JUMP_IF_FALSE end jump if false
7332 * ... body ...
7333 * JUMP top Jump back to repeat
7334 * end:
7335 *
7336 */
7337 static char_u *
7338compile_while(char_u *arg, cctx_T *cctx)
7339{
7340 char_u *p = arg;
7341 garray_T *instr = &cctx->ctx_instr;
7342 scope_T *scope;
7343
7344 scope = new_scope(cctx, WHILE_SCOPE);
7345 if (scope == NULL)
7346 return NULL;
7347
Bram Moolenaarb2049902021-01-24 12:53:53 +01007348 // "endwhile" jumps back here, one before when profiling
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007349 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007350#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007351 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7352 .isn_type == ISN_PROF_START)
7353 --scope->se_u.se_while.ws_top_label;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007354#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007355
7356 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007357 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007358 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007359 if (!ends_excmd2(arg, skipwhite(p)))
7360 {
7361 semsg(_(e_trailing_arg), p);
7362 return NULL;
7363 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007364
Bram Moolenaar13106602020-10-04 16:06:05 +02007365 if (bool_on_stack(cctx) == FAIL)
7366 return FAIL;
7367
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007368 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007369 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007370 JUMP_IF_FALSE, cctx) == FAIL)
7371 return FAIL;
7372
7373 return p;
7374}
7375
7376/*
7377 * compile "endwhile"
7378 */
7379 static char_u *
7380compile_endwhile(char_u *arg, cctx_T *cctx)
7381{
7382 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007383 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007384
7385 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7386 {
7387 emsg(_(e_while));
7388 return NULL;
7389 }
7390 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007391 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007392
Bram Moolenaarf002a412021-01-24 13:34:18 +01007393#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007394 // count the endwhile before jumping
7395 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007396#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007397
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007398 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007399 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007400
7401 // Fill in the "end" label in the WHILE statement so it can jump here.
7402 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007403 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7404 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007405
7406 vim_free(scope);
7407
7408 return arg;
7409}
7410
7411/*
7412 * compile "continue"
7413 */
7414 static char_u *
7415compile_continue(char_u *arg, cctx_T *cctx)
7416{
7417 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007418 int try_scopes = 0;
7419 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007420
7421 for (;;)
7422 {
7423 if (scope == NULL)
7424 {
7425 emsg(_(e_continue));
7426 return NULL;
7427 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01007428 if (scope->se_type == FOR_SCOPE)
7429 {
7430 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007431 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007432 }
7433 if (scope->se_type == WHILE_SCOPE)
7434 {
7435 loop_label = scope->se_u.se_while.ws_top_label;
7436 break;
7437 }
7438 if (scope->se_type == TRY_SCOPE)
7439 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007440 scope = scope->se_outer;
7441 }
7442
Bram Moolenaarc150c092021-02-13 15:02:46 +01007443 if (try_scopes > 0)
7444 // Inside one or more try/catch blocks we first need to jump to the
7445 // "finally" or "endtry" to cleanup.
7446 generate_TRYCONT(cctx, try_scopes, loop_label);
7447 else
7448 // Jump back to the FOR or WHILE instruction.
7449 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
7450
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007451 return arg;
7452}
7453
7454/*
7455 * compile "break"
7456 */
7457 static char_u *
7458compile_break(char_u *arg, cctx_T *cctx)
7459{
7460 scope_T *scope = cctx->ctx_scope;
7461 endlabel_T **el;
7462
7463 for (;;)
7464 {
7465 if (scope == NULL)
7466 {
7467 emsg(_(e_break));
7468 return NULL;
7469 }
7470 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7471 break;
7472 scope = scope->se_outer;
7473 }
7474
7475 // Jump to the end of the FOR or WHILE loop.
7476 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007477 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007478 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007479 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007480 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7481 return FAIL;
7482
7483 return arg;
7484}
7485
7486/*
7487 * compile "{" start of block
7488 */
7489 static char_u *
7490compile_block(char_u *arg, cctx_T *cctx)
7491{
7492 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7493 return NULL;
7494 return skipwhite(arg + 1);
7495}
7496
7497/*
7498 * compile end of block: drop one scope
7499 */
7500 static void
7501compile_endblock(cctx_T *cctx)
7502{
7503 scope_T *scope = cctx->ctx_scope;
7504
7505 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007506 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007507 vim_free(scope);
7508}
7509
7510/*
7511 * compile "try"
7512 * Creates a new scope for the try-endtry, pointing to the first catch and
7513 * finally.
7514 * Creates another scope for the "try" block itself.
7515 * TRY instruction sets up exception handling at runtime.
7516 *
7517 * "try"
7518 * TRY -> catch1, -> finally push trystack entry
7519 * ... try block
7520 * "throw {exception}"
7521 * EVAL {exception}
7522 * THROW create exception
7523 * ... try block
7524 * " catch {expr}"
7525 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007526 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007527 * EVAL {expr}
7528 * MATCH
7529 * JUMP nomatch -> catch2
7530 * CATCH remove exception
7531 * ... catch block
7532 * " catch"
7533 * JUMP -> finally
7534 * catch2: CATCH remove exception
7535 * ... catch block
7536 * " finally"
7537 * finally:
7538 * ... finally block
7539 * " endtry"
7540 * ENDTRY pop trystack entry, may rethrow
7541 */
7542 static char_u *
7543compile_try(char_u *arg, cctx_T *cctx)
7544{
7545 garray_T *instr = &cctx->ctx_instr;
7546 scope_T *try_scope;
7547 scope_T *scope;
7548
7549 // scope that holds the jumps that go to catch/finally/endtry
7550 try_scope = new_scope(cctx, TRY_SCOPE);
7551 if (try_scope == NULL)
7552 return NULL;
7553
Bram Moolenaar69f70502021-01-01 16:10:46 +01007554 if (cctx->ctx_skip != SKIP_YES)
7555 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007556 isn_T *isn;
7557
7558 // "try_catch" is set when the first ":catch" is found or when no catch
7559 // is found and ":finally" is found.
7560 // "try_finally" is set when ":finally" is found
7561 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01007562 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007563 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
7564 return NULL;
7565 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
7566 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007567 return NULL;
7568 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007569
7570 // scope for the try block itself
7571 scope = new_scope(cctx, BLOCK_SCOPE);
7572 if (scope == NULL)
7573 return NULL;
7574
7575 return arg;
7576}
7577
7578/*
7579 * compile "catch {expr}"
7580 */
7581 static char_u *
7582compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7583{
7584 scope_T *scope = cctx->ctx_scope;
7585 garray_T *instr = &cctx->ctx_instr;
7586 char_u *p;
7587 isn_T *isn;
7588
7589 // end block scope from :try or :catch
7590 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7591 compile_endblock(cctx);
7592 scope = cctx->ctx_scope;
7593
7594 // Error if not in a :try scope
7595 if (scope == NULL || scope->se_type != TRY_SCOPE)
7596 {
7597 emsg(_(e_catch));
7598 return NULL;
7599 }
7600
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007601 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007602 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007603 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007604 return NULL;
7605 }
7606
Bram Moolenaar69f70502021-01-01 16:10:46 +01007607 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007608 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007609#ifdef FEAT_PROFILE
7610 // the profile-start should be after the jump
7611 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7612 .isn_type == ISN_PROF_START)
7613 --instr->ga_len;
7614#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01007615 // Jump from end of previous block to :finally or :endtry
7616 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7617 JUMP_ALWAYS, cctx) == FAIL)
7618 return NULL;
7619
7620 // End :try or :catch scope: set value in ISN_TRY instruction
7621 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007622 if (isn->isn_arg.try.try_ref->try_catch == 0)
7623 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007624 if (scope->se_u.se_try.ts_catch_label != 0)
7625 {
7626 // Previous catch without match jumps here
7627 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7628 isn->isn_arg.jump.jump_where = instr->ga_len;
7629 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007630#ifdef FEAT_PROFILE
7631 if (cctx->ctx_profiling)
7632 {
7633 // a "throw" that jumps here needs to be counted
7634 generate_instr(cctx, ISN_PROF_END);
7635 // the "catch" is also counted
7636 generate_instr(cctx, ISN_PROF_START);
7637 }
7638#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007639 }
7640
7641 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007642 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007643 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007644 scope->se_u.se_try.ts_caught_all = TRUE;
7645 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007646 }
7647 else
7648 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007649 char_u *end;
7650 char_u *pat;
7651 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007652 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007653 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007654
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007655 // Push v:exception, push {expr} and MATCH
7656 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7657
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007658 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007659 if (*end != *p)
7660 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007661 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007662 vim_free(tofree);
7663 return FAIL;
7664 }
7665 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007666 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007667 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007668 len = (int)(end - tofree);
7669 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007670 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007671 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007672 if (pat == NULL)
7673 return FAIL;
7674 if (generate_PUSHS(cctx, pat) == FAIL)
7675 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007676
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007677 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7678 return NULL;
7679
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007680 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007681 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7682 return NULL;
7683 }
7684
Bram Moolenaar69f70502021-01-01 16:10:46 +01007685 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007686 return NULL;
7687
7688 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7689 return NULL;
7690 return p;
7691}
7692
7693 static char_u *
7694compile_finally(char_u *arg, cctx_T *cctx)
7695{
7696 scope_T *scope = cctx->ctx_scope;
7697 garray_T *instr = &cctx->ctx_instr;
7698 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007699 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007700
7701 // end block scope from :try or :catch
7702 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7703 compile_endblock(cctx);
7704 scope = cctx->ctx_scope;
7705
7706 // Error if not in a :try scope
7707 if (scope == NULL || scope->se_type != TRY_SCOPE)
7708 {
7709 emsg(_(e_finally));
7710 return NULL;
7711 }
7712
7713 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007714 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007715 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007716 {
7717 emsg(_(e_finally_dup));
7718 return NULL;
7719 }
7720
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007721 this_instr = instr->ga_len;
7722#ifdef FEAT_PROFILE
7723 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7724 .isn_type == ISN_PROF_START)
7725 // jump to the profile start of the "finally"
7726 --this_instr;
7727#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007728
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007729 // Fill in the "end" label in jumps at the end of the blocks.
7730 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
7731 this_instr, cctx);
7732
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007733 // If there is no :catch then an exception jumps to :finally.
7734 if (isn->isn_arg.try.try_ref->try_catch == 0)
7735 isn->isn_arg.try.try_ref->try_catch = this_instr;
7736 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007737 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007738 {
7739 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007740 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007741 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02007742 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007743 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007744 if (generate_instr(cctx, ISN_FINALLY) == NULL)
7745 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007746
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007747 // TODO: set index in ts_finally_label jumps
7748
7749 return arg;
7750}
7751
7752 static char_u *
7753compile_endtry(char_u *arg, cctx_T *cctx)
7754{
7755 scope_T *scope = cctx->ctx_scope;
7756 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007757 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007758
7759 // end block scope from :catch or :finally
7760 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7761 compile_endblock(cctx);
7762 scope = cctx->ctx_scope;
7763
7764 // Error if not in a :try scope
7765 if (scope == NULL || scope->se_type != TRY_SCOPE)
7766 {
7767 if (scope == NULL)
7768 emsg(_(e_no_endtry));
7769 else if (scope->se_type == WHILE_SCOPE)
7770 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007771 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007772 emsg(_(e_endfor));
7773 else
7774 emsg(_(e_endif));
7775 return NULL;
7776 }
7777
Bram Moolenaarc150c092021-02-13 15:02:46 +01007778 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007779 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007780 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007781 if (try_isn->isn_arg.try.try_ref->try_catch == 0
7782 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007783 {
7784 emsg(_(e_missing_catch_or_finally));
7785 return NULL;
7786 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007787
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007788#ifdef FEAT_PROFILE
7789 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7790 .isn_type == ISN_PROF_START)
7791 // move the profile start after "endtry" so that it's not counted when
7792 // the exception is rethrown.
7793 --instr->ga_len;
7794#endif
7795
Bram Moolenaar69f70502021-01-01 16:10:46 +01007796 // Fill in the "end" label in jumps at the end of the blocks, if not
7797 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007798 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
7799 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007800
Bram Moolenaar69f70502021-01-01 16:10:46 +01007801 if (scope->se_u.se_try.ts_catch_label != 0)
7802 {
7803 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01007804 isn_T *isn = ((isn_T *)instr->ga_data)
7805 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007806 isn->isn_arg.jump.jump_where = instr->ga_len;
7807 }
Bram Moolenaare8593122020-07-18 15:17:02 +02007808 }
7809
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007810 compile_endblock(cctx);
7811
Bram Moolenaar4afa7742021-02-14 16:34:59 +01007812 if (cctx->ctx_skip != SKIP_YES)
7813 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007814 // End :catch or :finally scope: set instruction index in ISN_TRY
7815 // instruction
7816 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01007817 if (cctx->ctx_skip != SKIP_YES
7818 && generate_instr(cctx, ISN_ENDTRY) == NULL)
7819 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007820#ifdef FEAT_PROFILE
7821 if (cctx->ctx_profiling)
7822 generate_instr(cctx, ISN_PROF_START);
7823#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01007824 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007825 return arg;
7826}
7827
7828/*
7829 * compile "throw {expr}"
7830 */
7831 static char_u *
7832compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7833{
7834 char_u *p = skipwhite(arg);
7835
Bram Moolenaara5565e42020-05-09 15:44:01 +02007836 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007837 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01007838 if (cctx->ctx_skip == SKIP_YES)
7839 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007840 if (may_generate_2STRING(-1, cctx) == FAIL)
7841 return NULL;
7842 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7843 return NULL;
7844
7845 return p;
7846}
7847
7848/*
7849 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007850 * compile "echomsg expr"
7851 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007852 * compile "execute expr"
7853 */
7854 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007855compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007856{
7857 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007858 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007859 int count = 0;
7860
7861 for (;;)
7862 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007863 if (ends_excmd2(prev, p))
7864 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007865 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007866 return NULL;
7867 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007868 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007869 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007870 }
7871
Bram Moolenaare4984292020-12-13 14:19:25 +01007872 if (count > 0)
7873 {
7874 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7875 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7876 else if (cmdidx == CMD_execute)
7877 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7878 else if (cmdidx == CMD_echomsg)
7879 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7880 else
7881 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7882 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007883 return p;
7884}
7885
7886/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007887 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01007888 * instruction to compute it and return OK.
7889 * Otherwise return FAIL, the caller must deal with any range.
7890 */
7891 static int
7892compile_variable_range(exarg_T *eap, cctx_T *cctx)
7893{
7894 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7895 char_u *p = skipdigits(eap->cmd);
7896
7897 if (p == range_end)
7898 return FAIL;
7899 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7900}
7901
7902/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007903 * :put r
7904 * :put ={expr}
7905 */
7906 static char_u *
7907compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7908{
7909 char_u *line = arg;
7910 linenr_T lnum;
7911 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007912 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007913
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007914 eap->regname = *line;
7915
7916 if (eap->regname == '=')
7917 {
7918 char_u *p = line + 1;
7919
7920 if (compile_expr0(&p, cctx) == FAIL)
7921 return NULL;
7922 line = p;
7923 }
7924 else if (eap->regname != NUL)
7925 ++line;
7926
Bram Moolenaar08597872020-12-10 19:43:40 +01007927 if (compile_variable_range(eap, cctx) == OK)
7928 {
7929 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
7930 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007931 else
Bram Moolenaar08597872020-12-10 19:43:40 +01007932 {
7933 // Either no range or a number.
7934 // "errormsg" will not be set because the range is ADDR_LINES.
7935 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01007936 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01007937 return NULL;
7938 if (eap->addr_count == 0)
7939 lnum = -1;
7940 else
7941 lnum = eap->line2;
7942 if (above)
7943 --lnum;
7944 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007945
7946 generate_PUT(cctx, eap->regname, lnum);
7947 return line;
7948}
7949
7950/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007951 * A command that is not compiled, execute with legacy code.
7952 */
7953 static char_u *
7954compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7955{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007956 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007957 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007958 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007959
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007960 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007961 goto theend;
7962
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007963 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007964 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007965 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007966 int usefilter = FALSE;
7967
7968 has_expr = argt & (EX_XFILE | EX_EXPAND);
7969
7970 // If the command can be followed by a bar, find the bar and truncate
7971 // it, so that the following command can be compiled.
7972 // The '|' is overwritten with a NUL, it is put back below.
7973 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7974 && *eap->arg == '!')
7975 // :w !filter or :r !filter or :r! filter
7976 usefilter = TRUE;
7977 if ((argt & EX_TRLBAR) && !usefilter)
7978 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007979 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007980 separate_nextcmd(eap);
7981 if (eap->nextcmd != NULL)
7982 nextcmd = eap->nextcmd;
7983 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01007984 else if (eap->cmdidx == CMD_wincmd)
7985 {
7986 p = eap->arg;
7987 if (*p != NUL)
7988 ++p;
7989 if (*p == 'g' || *p == Ctrl_G)
7990 ++p;
7991 p = skipwhite(p);
7992 if (*p == '|')
7993 {
7994 *p = NUL;
7995 nextcmd = p + 1;
7996 }
7997 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007998 }
7999
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008000 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8001 {
8002 // expand filename in "syntax include [@group] filename"
8003 has_expr = TRUE;
8004 eap->arg = skipwhite(eap->arg + 7);
8005 if (*eap->arg == '@')
8006 eap->arg = skiptowhite(eap->arg);
8007 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008008
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008009 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8010 && STRLEN(eap->arg) > 4)
8011 {
8012 int delim = *eap->arg;
8013
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008014 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008015 if (*p == delim)
8016 {
8017 eap->arg = p + 1;
8018 has_expr = TRUE;
8019 }
8020 }
8021
Bram Moolenaarecac5912021-01-05 19:23:28 +01008022 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8023 {
8024 // TODO: should only expand when appropriate for the command
8025 eap->arg = skiptowhite(eap->arg);
8026 has_expr = TRUE;
8027 }
8028
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008029 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008030 {
8031 int count = 0;
8032 char_u *start = skipwhite(line);
8033
8034 // :cmd xxx`=expr1`yyy`=expr2`zzz
8035 // PUSHS ":cmd xxx"
8036 // eval expr1
8037 // PUSHS "yyy"
8038 // eval expr2
8039 // PUSHS "zzz"
8040 // EXECCONCAT 5
8041 for (;;)
8042 {
8043 if (p > start)
8044 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008045 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008046 ++count;
8047 }
8048 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008049 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008050 return NULL;
8051 may_generate_2STRING(-1, cctx);
8052 ++count;
8053 p = skipwhite(p);
8054 if (*p != '`')
8055 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008056 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008057 return NULL;
8058 }
8059 start = p + 1;
8060
8061 p = (char_u *)strstr((char *)start, "`=");
8062 if (p == NULL)
8063 {
8064 if (*skipwhite(start) != NUL)
8065 {
8066 generate_PUSHS(cctx, vim_strsave(start));
8067 ++count;
8068 }
8069 break;
8070 }
8071 }
8072 generate_EXECCONCAT(cctx, count);
8073 }
8074 else
8075 generate_EXEC(cctx, line);
8076
8077theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008078 if (*nextcmd != NUL)
8079 {
8080 // the parser expects a pointer to the bar, put it back
8081 --nextcmd;
8082 *nextcmd = '|';
8083 }
8084
8085 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008086}
8087
8088/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008089 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008090 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008091 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008092 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008093add_def_function(ufunc_T *ufunc)
8094{
8095 dfunc_T *dfunc;
8096
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008097 if (def_functions.ga_len == 0)
8098 {
8099 // The first position is not used, so that a zero uf_dfunc_idx means it
8100 // wasn't set.
8101 if (ga_grow(&def_functions, 1) == FAIL)
8102 return FAIL;
8103 ++def_functions.ga_len;
8104 }
8105
Bram Moolenaar09689a02020-05-09 22:50:08 +02008106 // Add the function to "def_functions".
8107 if (ga_grow(&def_functions, 1) == FAIL)
8108 return FAIL;
8109 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8110 CLEAR_POINTER(dfunc);
8111 dfunc->df_idx = def_functions.ga_len;
8112 ufunc->uf_dfunc_idx = dfunc->df_idx;
8113 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008114 dfunc->df_name = vim_strsave(ufunc->uf_name);
8115 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008116 ++def_functions.ga_len;
8117 return OK;
8118}
8119
8120/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008121 * After ex_function() has collected all the function lines: parse and compile
8122 * the lines into instructions.
8123 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008124 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8125 * the return statement (used for lambda). When uf_ret_type is already set
8126 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008127 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008128 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008129 * This can be used recursively through compile_lambda(), which may reallocate
8130 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008131 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008132 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008133 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008134compile_def_function(
8135 ufunc_T *ufunc,
8136 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008137 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008138 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008139{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008140 char_u *line = NULL;
8141 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008142 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008143 cctx_T cctx;
8144 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008145 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008146 int ret = FAIL;
8147 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008148 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008149 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008150 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008151#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008152 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008153#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008154
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008155 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008156 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008157 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008158 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008159 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8160 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008161 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008162 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008163 else
8164 {
8165 if (add_def_function(ufunc) == FAIL)
8166 return FAIL;
8167 new_def_function = TRUE;
8168 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008169
Bram Moolenaar985116a2020-07-12 17:31:09 +02008170 ufunc->uf_def_status = UF_COMPILING;
8171
Bram Moolenaara80faa82020-04-12 19:37:17 +02008172 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008173
Bram Moolenaarf002a412021-01-24 13:34:18 +01008174#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008175 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008176#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008177 cctx.ctx_ufunc = ufunc;
8178 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008179 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008180 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8181 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8182 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8183 cctx.ctx_type_list = &ufunc->uf_type_list;
8184 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8185 instr = &cctx.ctx_instr;
8186
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008187 // Set the context to the function, it may be compiled when called from
8188 // another script. Set the script version to the most modern one.
8189 // The line number will be set in next_line_from_context().
8190 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008191 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8192
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008193 // Make sure error messages are OK.
8194 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8195 if (do_estack_push)
8196 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008197 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008198
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008199 if (ufunc->uf_def_args.ga_len > 0)
8200 {
8201 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008202 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008203 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008204 int i;
8205 char_u *arg;
8206 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008207 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008208
8209 // Produce instructions for the default values of optional arguments.
8210 // Store the instruction index in uf_def_arg_idx[] so that we know
8211 // where to start when the function is called, depending on the number
8212 // of arguments.
8213 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
8214 if (ufunc->uf_def_arg_idx == NULL)
8215 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008216 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008217 for (i = 0; i < count; ++i)
8218 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008219 garray_T *stack = &cctx.ctx_type_stack;
8220 type_T *val_type;
8221 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008222 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008223 int r;
8224
8225 // Make sure later arguments are not found.
8226 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008227
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008228 ufunc->uf_def_arg_idx[i] = instr->ga_len;
8229 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01008230 r = compile_expr0(&arg, &cctx);
8231
8232 ufunc->uf_args.ga_len = uf_args_len;
8233 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008234 goto erret;
8235
8236 // If no type specified use the type of the default value.
8237 // Otherwise check that the default value type matches the
8238 // specified type.
8239 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008240 where.wt_index = arg_idx + 1;
8241 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008242 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008243 {
8244 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008245 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008246 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02008247 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008248 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008249 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008250
8251 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008252 goto erret;
8253 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008254 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008255
8256 if (did_set_arg_type)
8257 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008258 }
8259
8260 /*
8261 * Loop over all the lines of the function and generate instructions.
8262 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008263 for (;;)
8264 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008265 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008266 int starts_with_colon = FALSE;
8267 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02008268 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008269
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008270 // Bail out on the first error to avoid a flood of errors and report
8271 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01008272 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008273 goto erret;
8274
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008275 if (line != NULL && *line == '|')
8276 // the line continues after a '|'
8277 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02008278 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02008279 && !(*line == '#' && (line == cctx.ctx_line_start
8280 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008281 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02008282 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008283 goto erret;
8284 }
8285 else
8286 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02008287 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02008288 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008289 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008290 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01008291#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008292 if (cctx.ctx_skip != SKIP_YES)
8293 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008294#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008295 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01008296 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008297 }
8298
Bram Moolenaara80faa82020-04-12 19:37:17 +02008299 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008300 ea.cmdlinep = &line;
8301 ea.cmd = skipwhite(line);
8302
Bram Moolenaarb2049902021-01-24 12:53:53 +01008303 if (*ea.cmd == '#')
8304 {
8305 // "#" starts a comment
8306 line = (char_u *)"";
8307 continue;
8308 }
8309
Bram Moolenaarf002a412021-01-24 13:34:18 +01008310#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008311 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
8312 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008313 {
8314 may_generate_prof_end(&cctx, prof_lnum);
8315
8316 prof_lnum = cctx.ctx_lnum;
8317 generate_instr(&cctx, ISN_PROF_START);
8318 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01008319#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008320
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008321 // Some things can be recognized by the first character.
8322 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008323 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008324 case '}':
8325 {
8326 // "}" ends a block scope
8327 scopetype_T stype = cctx.ctx_scope == NULL
8328 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008329
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008330 if (stype == BLOCK_SCOPE)
8331 {
8332 compile_endblock(&cctx);
8333 line = ea.cmd;
8334 }
8335 else
8336 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008337 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008338 goto erret;
8339 }
8340 if (line != NULL)
8341 line = skipwhite(ea.cmd + 1);
8342 continue;
8343 }
8344
8345 case '{':
8346 // "{" starts a block scope
8347 // "{'a': 1}->func() is something else
8348 if (ends_excmd(*skipwhite(ea.cmd + 1)))
8349 {
8350 line = compile_block(ea.cmd, &cctx);
8351 continue;
8352 }
8353 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008354 }
8355
8356 /*
8357 * COMMAND MODIFIERS
8358 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02008359 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02008360 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
8361 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008362 {
8363 if (errormsg != NULL)
8364 goto erret;
8365 // empty line or comment
8366 line = (char_u *)"";
8367 continue;
8368 }
Bram Moolenaare1004402020-10-24 20:49:43 +02008369 generate_cmdmods(&cctx, &local_cmdmod);
8370 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008371
Bram Moolenaare88c8e82020-11-01 17:03:37 +01008372 // Check if there was a colon after the last command modifier or before
8373 // the current position.
8374 for (p = ea.cmd; p >= line; --p)
8375 {
8376 if (*p == ':')
8377 starts_with_colon = TRUE;
8378 if (p < ea.cmd && !VIM_ISWHITE(*p))
8379 break;
8380 }
8381
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008382 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008383 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008384 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008385 {
8386 if (*ea.cmd == '(')
8387 // not for "call()"
8388 ea.cmd = p;
8389 else
8390 ea.cmd = skipwhite(ea.cmd);
8391 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008392
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008393 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008394 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008395 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008396
Bram Moolenaar17126b12021-01-07 22:03:02 +01008397 // Check for assignment after command modifiers.
8398 assign = may_compile_assignment(&ea, &line, &cctx);
8399 if (assign == OK)
8400 goto nextline;
8401 if (assign == FAIL)
8402 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008403 }
8404
8405 /*
8406 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008407 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008408 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008409 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008410 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008411 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008412 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008413 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008414 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008415 if (!starts_with_colon)
8416 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008417 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008418 goto erret;
8419 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01008420 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008421 if (ends_excmd2(line, ea.cmd))
8422 {
8423 // A range without a command: jump to the line.
8424 // TODO: compile to a more efficient command, possibly
8425 // calling parse_cmd_address().
8426 ea.cmdidx = CMD_SIZE;
8427 line = compile_exec(line, &ea, &cctx);
8428 goto nextline;
8429 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008430 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008431 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008432 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaar6914e872021-03-06 21:01:09 +01008433 : (int (*)(char_u *, size_t, cctx_T *))item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008434
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008435 if (p == NULL)
8436 {
8437 if (cctx.ctx_skip != SKIP_YES)
8438 emsg(_(e_ambiguous_use_of_user_defined_command));
8439 goto erret;
8440 }
8441
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008442 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8443 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008444 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008445 {
8446 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008447 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008448 }
8449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008450 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008451 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008452 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008453 // CMD_var cannot happen, compile_assignment() above would be
8454 // used. Most likely an assignment to a non-existing variable.
8455 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008456 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008457 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008458 }
8459
Bram Moolenaar3988f642020-08-27 22:43:03 +02008460 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008461 && ea.cmdidx != CMD_elseif
8462 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008463 && ea.cmdidx != CMD_endif
8464 && ea.cmdidx != CMD_endfor
8465 && ea.cmdidx != CMD_endwhile
8466 && ea.cmdidx != CMD_catch
8467 && ea.cmdidx != CMD_finally
8468 && ea.cmdidx != CMD_endtry)
8469 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008470 emsg(_(e_unreachable_code_after_return));
8471 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008472 }
8473
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008474 p = skipwhite(p);
8475 if (ea.cmdidx != CMD_SIZE
8476 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8477 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008478 if (ea.cmdidx >= 0)
8479 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008480 if ((ea.argt & EX_BANG) && *p == '!')
8481 {
8482 ea.forceit = TRUE;
8483 p = skipwhite(p + 1);
8484 }
8485 }
8486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008487 switch (ea.cmdidx)
8488 {
8489 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008490 ea.arg = p;
8491 line = compile_nested_function(&ea, &cctx);
8492 break;
8493
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008494 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008495 // TODO: should we allow this, e.g. to declare a global
8496 // function?
8497 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008498 goto erret;
8499
8500 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008501 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008502 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008503 break;
8504
8505 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008506 emsg(_(e_cannot_use_let_in_vim9_script));
8507 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008508 case CMD_var:
8509 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008510 case CMD_const:
8511 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008512 if (line == p)
8513 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008514 break;
8515
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008516 case CMD_unlet:
8517 case CMD_unlockvar:
8518 case CMD_lockvar:
8519 line = compile_unletlock(p, &ea, &cctx);
8520 break;
8521
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008522 case CMD_import:
8523 line = compile_import(p, &cctx);
8524 break;
8525
8526 case CMD_if:
8527 line = compile_if(p, &cctx);
8528 break;
8529 case CMD_elseif:
8530 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008531 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008532 break;
8533 case CMD_else:
8534 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008535 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008536 break;
8537 case CMD_endif:
8538 line = compile_endif(p, &cctx);
8539 break;
8540
8541 case CMD_while:
8542 line = compile_while(p, &cctx);
8543 break;
8544 case CMD_endwhile:
8545 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008546 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008547 break;
8548
8549 case CMD_for:
8550 line = compile_for(p, &cctx);
8551 break;
8552 case CMD_endfor:
8553 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008554 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008555 break;
8556 case CMD_continue:
8557 line = compile_continue(p, &cctx);
8558 break;
8559 case CMD_break:
8560 line = compile_break(p, &cctx);
8561 break;
8562
8563 case CMD_try:
8564 line = compile_try(p, &cctx);
8565 break;
8566 case CMD_catch:
8567 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008568 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008569 break;
8570 case CMD_finally:
8571 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008572 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008573 break;
8574 case CMD_endtry:
8575 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008576 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008577 break;
8578 case CMD_throw:
8579 line = compile_throw(p, &cctx);
8580 break;
8581
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008582 case CMD_eval:
8583 if (compile_expr0(&p, &cctx) == FAIL)
8584 goto erret;
8585
Bram Moolenaar3988f642020-08-27 22:43:03 +02008586 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008587 generate_instr_drop(&cctx, ISN_DROP, 1);
8588
8589 line = skipwhite(p);
8590 break;
8591
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008592 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008593 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008594 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008595 case CMD_echomsg:
8596 case CMD_echoerr:
8597 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008598 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008599
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008600 case CMD_put:
8601 ea.cmd = cmd;
8602 line = compile_put(p, &ea, &cctx);
8603 break;
8604
Bram Moolenaar3988f642020-08-27 22:43:03 +02008605 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008606
Bram Moolenaarae616492020-07-28 20:07:27 +02008607 case CMD_append:
8608 case CMD_change:
8609 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01008610 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008611 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008612 case CMD_xit:
8613 not_in_vim9(&ea);
8614 goto erret;
8615
Bram Moolenaar002262f2020-07-08 17:47:57 +02008616 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008617 if (cctx.ctx_skip != SKIP_YES)
8618 {
8619 semsg(_(e_invalid_command_str), ea.cmd);
8620 goto erret;
8621 }
8622 // We don't check for a next command here.
8623 line = (char_u *)"";
8624 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008625
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008626 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008627 if (cctx.ctx_skip == SKIP_YES)
8628 {
8629 // We don't check for a next command here.
8630 line = (char_u *)"";
8631 }
8632 else
8633 {
8634 // Not recognized, execute with do_cmdline_cmd().
8635 ea.arg = p;
8636 line = compile_exec(line, &ea, &cctx);
8637 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008638 break;
8639 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008640nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008641 if (line == NULL)
8642 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008643 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008644
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008645 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008646 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008647
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008648 if (cctx.ctx_type_stack.ga_len < 0)
8649 {
8650 iemsg("Type stack underflow");
8651 goto erret;
8652 }
8653 }
8654
8655 if (cctx.ctx_scope != NULL)
8656 {
8657 if (cctx.ctx_scope->se_type == IF_SCOPE)
8658 emsg(_(e_endif));
8659 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8660 emsg(_(e_endwhile));
8661 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8662 emsg(_(e_endfor));
8663 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008664 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008665 goto erret;
8666 }
8667
Bram Moolenaarefd88552020-06-18 20:50:10 +02008668 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008669 {
8670 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8671 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008672 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008673 goto erret;
8674 }
8675
8676 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008677 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008678 }
8679
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008680 {
8681 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8682 + ufunc->uf_dfunc_idx;
8683 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008684 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008685#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008686 if (cctx.ctx_profiling)
8687 {
8688 dfunc->df_instr_prof = instr->ga_data;
8689 dfunc->df_instr_prof_count = instr->ga_len;
8690 }
8691 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01008692#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008693 {
8694 dfunc->df_instr = instr->ga_data;
8695 dfunc->df_instr_count = instr->ga_len;
8696 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008697 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008698 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008699 if (cctx.ctx_outer_used)
8700 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008701 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008702 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008703
8704 ret = OK;
8705
8706erret:
8707 if (ret == FAIL)
8708 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008709 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008710 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8711 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008712
8713 for (idx = 0; idx < instr->ga_len; ++idx)
8714 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008715 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008716 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008717
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008718 // If using the last entry in the table and it was added above, we
8719 // might as well remove it.
8720 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008721 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008722 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008723 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008724 ufunc->uf_dfunc_idx = 0;
8725 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008726 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008727
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008728 while (cctx.ctx_scope != NULL)
8729 drop_scope(&cctx);
8730
Bram Moolenaar20431c92020-03-20 18:39:46 +01008731 // Don't execute this function body.
8732 ga_clear_strings(&ufunc->uf_lines);
8733
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008734 if (errormsg != NULL)
8735 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008736 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008737 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008738 }
8739
8740 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008741 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008742 if (do_estack_push)
8743 estack_pop();
8744
Bram Moolenaar20431c92020-03-20 18:39:46 +01008745 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008746 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008747 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008748 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008749}
8750
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008751 void
8752set_function_type(ufunc_T *ufunc)
8753{
8754 int varargs = ufunc->uf_va_name != NULL;
8755 int argcount = ufunc->uf_args.ga_len;
8756
8757 // Create a type for the function, with the return type and any
8758 // argument types.
8759 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8760 // The type is included in "tt_args".
8761 if (argcount > 0 || varargs)
8762 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01008763 if (ufunc->uf_type_list.ga_itemsize == 0)
8764 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008765 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8766 argcount, &ufunc->uf_type_list);
8767 // Add argument types to the function type.
8768 if (func_type_add_arg_types(ufunc->uf_func_type,
8769 argcount + varargs,
8770 &ufunc->uf_type_list) == FAIL)
8771 return;
8772 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8773 ufunc->uf_func_type->tt_min_argcount =
8774 argcount - ufunc->uf_def_args.ga_len;
8775 if (ufunc->uf_arg_types == NULL)
8776 {
8777 int i;
8778
8779 // lambda does not have argument types.
8780 for (i = 0; i < argcount; ++i)
8781 ufunc->uf_func_type->tt_args[i] = &t_any;
8782 }
8783 else
8784 mch_memmove(ufunc->uf_func_type->tt_args,
8785 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8786 if (varargs)
8787 {
8788 ufunc->uf_func_type->tt_args[argcount] =
8789 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8790 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8791 }
8792 }
8793 else
8794 // No arguments, can use a predefined type.
8795 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8796 argcount, &ufunc->uf_type_list);
8797}
8798
8799
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008800/*
8801 * Delete an instruction, free what it contains.
8802 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008803 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008804delete_instr(isn_T *isn)
8805{
8806 switch (isn->isn_type)
8807 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008808 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008809 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008810 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008811 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008812 case ISN_LOADENV:
8813 case ISN_LOADG:
8814 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008815 case ISN_LOADT:
8816 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008817 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008818 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008819 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008820 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008821 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008822 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008823 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008824 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008825 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008826 case ISN_STOREW:
8827 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008828 vim_free(isn->isn_arg.string);
8829 break;
8830
8831 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008832 case ISN_STORES:
8833 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008834 break;
8835
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008836 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008837 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008838 vim_free(isn->isn_arg.unlet.ul_name);
8839 break;
8840
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008841 case ISN_STOREOPT:
8842 vim_free(isn->isn_arg.storeopt.so_name);
8843 break;
8844
8845 case ISN_PUSHBLOB: // push blob isn_arg.blob
8846 blob_unref(isn->isn_arg.blob);
8847 break;
8848
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008849 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008850#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008851 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008852#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008853 break;
8854
8855 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008856#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008857 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008858#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008859 break;
8860
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008861 case ISN_UCALL:
8862 vim_free(isn->isn_arg.ufunc.cuf_name);
8863 break;
8864
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008865 case ISN_FUNCREF:
8866 {
8867 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8868 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01008869 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008870
Bram Moolenaar077a4232020-12-22 18:33:27 +01008871 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
8872 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02008873 }
8874 break;
8875
8876 case ISN_DCALL:
8877 {
8878 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8879 + isn->isn_arg.dfunc.cdf_idx;
8880
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008881 if (dfunc->df_ufunc != NULL
8882 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008883 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008884 }
8885 break;
8886
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008887 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008888 {
8889 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8890 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8891
8892 if (ufunc != NULL)
8893 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008894 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02008895 func_ptr_unref(ufunc);
8896 }
8897
8898 vim_free(lambda);
8899 vim_free(isn->isn_arg.newfunc.nf_global);
8900 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008901 break;
8902
Bram Moolenaar5e654232020-09-16 15:22:00 +02008903 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01008904 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02008905 free_type(isn->isn_arg.type.ct_type);
8906 break;
8907
Bram Moolenaar02194d22020-10-24 23:08:38 +02008908 case ISN_CMDMOD:
8909 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8910 ->cmod_filter_regmatch.regprog);
8911 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8912 break;
8913
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008914 case ISN_LOADSCRIPT:
8915 case ISN_STORESCRIPT:
8916 vim_free(isn->isn_arg.script.scriptref);
8917 break;
8918
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008919 case ISN_TRY:
8920 vim_free(isn->isn_arg.try.try_ref);
8921 break;
8922
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008923 case ISN_2BOOL:
8924 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008925 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008926 case ISN_ADDBLOB:
8927 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008928 case ISN_ANYINDEX:
8929 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008930 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008931 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008932 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008933 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008934 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008935 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008936 case ISN_COMPAREANY:
8937 case ISN_COMPAREBLOB:
8938 case ISN_COMPAREBOOL:
8939 case ISN_COMPAREDICT:
8940 case ISN_COMPAREFLOAT:
8941 case ISN_COMPAREFUNC:
8942 case ISN_COMPARELIST:
8943 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008944 case ISN_COMPARESPECIAL:
8945 case ISN_COMPARESTRING:
8946 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008947 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008948 case ISN_DROP:
8949 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008950 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008951 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008952 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008953 case ISN_EXECCONCAT:
8954 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008955 case ISN_FINALLY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008956 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008957 case ISN_GETITEM:
8958 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008959 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008960 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008961 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008962 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008963 case ISN_LOADBDICT:
8964 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008965 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008966 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008967 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008968 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008969 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008970 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008971 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008972 case ISN_NEGATENR:
8973 case ISN_NEWDICT:
8974 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008975 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008976 case ISN_OPFLOAT:
8977 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008978 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008979 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01008980 case ISN_PROF_END:
8981 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008982 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008983 case ISN_PUSHF:
8984 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008985 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008986 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008987 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01008988 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008989 case ISN_SHUFFLE:
8990 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008991 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01008992 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008993 case ISN_STORENR:
8994 case ISN_STOREOUTER:
8995 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008996 case ISN_STOREV:
8997 case ISN_STRINDEX:
8998 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008999 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +01009000 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +01009001 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01009002 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +01009003 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009004 // nothing allocated
9005 break;
9006 }
9007}
9008
9009/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009010 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01009011 */
9012 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009013delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009014{
9015 int idx;
9016
9017 ga_clear(&dfunc->df_def_args_isn);
9018
9019 if (dfunc->df_instr != NULL)
9020 {
9021 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
9022 delete_instr(dfunc->df_instr + idx);
9023 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009024 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009025 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01009026#ifdef FEAT_PROFILE
9027 if (dfunc->df_instr_prof != NULL)
9028 {
9029 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
9030 delete_instr(dfunc->df_instr_prof + idx);
9031 VIM_CLEAR(dfunc->df_instr_prof);
9032 dfunc->df_instr_prof = NULL;
9033 }
9034#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01009035
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009036 if (mark_deleted)
9037 dfunc->df_deleted = TRUE;
9038 if (dfunc->df_ufunc != NULL)
9039 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009040}
9041
9042/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009043 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009044 * function, unless another user function still uses it.
9045 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009046 */
9047 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009048unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009049{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009050 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009051 {
9052 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9053 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009054
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009055 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009056 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009057 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009058 ufunc->uf_dfunc_idx = 0;
9059 if (dfunc->df_ufunc == ufunc)
9060 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009061 }
9062}
9063
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009064/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009065 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009066 */
9067 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009068link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009069{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009070 if (ufunc->uf_dfunc_idx > 0)
9071 {
9072 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9073 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009074
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009075 ++dfunc->df_refcount;
9076 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009077}
9078
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009079#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009080/*
9081 * Free all functions defined with ":def".
9082 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009083 void
9084free_def_functions(void)
9085{
Bram Moolenaar20431c92020-03-20 18:39:46 +01009086 int idx;
9087
9088 for (idx = 0; idx < def_functions.ga_len; ++idx)
9089 {
9090 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
9091
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009092 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009093 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009094 }
9095
9096 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009097}
9098#endif
9099
9100
9101#endif // FEAT_EVAL